pcpngrsa.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2016 Intel Corporation. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Intel Corporation nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #if !defined(_CP_NG_RSA_H)
  32. #define _CP_NG_RSA_H
  33. #include "pcpbn.h"
  34. #include "pcpmontgomery.h"
  35. struct _cpRSA_public_key {
  36. IppCtxId id; /* key ID */
  37. int maxbitSizeN;
  38. int maxbitSizeE;
  39. int bitSizeN; /* RSA modulus bitsize */
  40. int bitSizeE; /* RSA public exp bitsize */
  41. BNU_CHUNK_T* pDataE; /* public exp */
  42. IppsMontState* pMontN; /* montgomery engine (N) */
  43. };
  44. /* access */
  45. #define RSA_PUB_KEY_MAXSIZE_N(x) ((x)->maxbitSizeN)
  46. #define RSA_PUB_KEY_MAXSIZE_E(x) ((x)->maxbitSizeE)
  47. #define RSA_PUB_KEY_ID(x) ((x)->id)
  48. #define RSA_PUB_KEY_BITSIZE_N(x) ((x)->bitSizeN)
  49. #define RSA_PUB_KEY_BITSIZE_E(x) ((x)->bitSizeE)
  50. #define RSA_PUB_KEY_E(x) ((x)->pDataE)
  51. #define RSA_PUB_KEY_NMONT(x) ((x)->pMontN)
  52. #define RSA_PUB_KEY_VALID_ID(x) (RSA_PUB_KEY_ID((x))==idCtxRSA_PubKey)
  53. #define RSA_PUB_KEY_IS_SET(x) (RSA_PUB_KEY_BITSIZE_N((x))>0)
  54. /* alignment */
  55. #define RSA_PUBLIC_KEY_ALIGNMENT ((int)(sizeof(void*)))
  56. struct _cpRSA_private_key {
  57. IppCtxId id; /* key ID */
  58. int maxbitSizeN;
  59. int maxbitSizeD;
  60. int bitSizeN; /* RSA modulus bitsize */
  61. int bitSizeD; /* RSA private exp bitsize */
  62. int bitSizeP; /* RSA p-factor bitsize */
  63. int bitSizeQ; /* RSA q-factor bitsize */
  64. BNU_CHUNK_T* pDataD; /* private exp */
  65. BNU_CHUNK_T* pDataDp; /* dp private exp */
  66. BNU_CHUNK_T* pDataDq; /* dq private exp */
  67. BNU_CHUNK_T* pDataQinv; /* qinv coeff */
  68. IppsMontState* pMontP; /* montgomery engine (P) */
  69. IppsMontState* pMontQ; /* montgomery engine (Q) */
  70. IppsMontState* pMontN; /* montgomery engine (N) */
  71. };
  72. /* access */
  73. #define RSA_PRV_KEY_MAXSIZE_N(x) ((x)->maxbitSizeN)
  74. #define RSA_PRV_KEY_MAXSIZE_D(x) ((x)->maxbitSizeD)
  75. #define RSA_PRV_KEY_ID(x) ((x)->id)
  76. #define RSA_PRV_KEY_BITSIZE_N(x) ((x)->bitSizeN)
  77. #define RSA_PRV_KEY_BITSIZE_D(x) ((x)->bitSizeD)
  78. #define RSA_PRV_KEY_BITSIZE_P(x) ((x)->bitSizeP)
  79. #define RSA_PRV_KEY_BITSIZE_Q(x) ((x)->bitSizeQ)
  80. #define RSA_PRV_KEY_D(x) ((x)->pDataD)
  81. #define RSA_PRV_KEY_DP(x) ((x)->pDataDp)
  82. #define RSA_PRV_KEY_DQ(x) ((x)->pDataDq)
  83. #define RSA_PRV_KEY_INVQ(x) ((x)->pDataQinv)
  84. #define RSA_PRV_KEY_PMONT(x) ((x)->pMontP)
  85. #define RSA_PRV_KEY_QMONT(x) ((x)->pMontQ)
  86. #define RSA_PRV_KEY_NMONT(x) ((x)->pMontN)
  87. #define RSA_PRV_KEY1_VALID_ID(x) (RSA_PRV_KEY_ID((x))==idCtxRSA_PrvKey1)
  88. #define RSA_PRV_KEY2_VALID_ID(x) (RSA_PRV_KEY_ID((x))==idCtxRSA_PrvKey2)
  89. #define RSA_PRV_KEY_VALID_ID(x) (RSA_PRV_KEY1_VALID_ID((x)) || RSA_PRV_KEY2_VALID_ID((x)))
  90. #define RSA_PRV_KEY_IS_SET(x) (RSA_PRV_KEY_BITSIZE_N((x))>0)
  91. /* alignment */
  92. #define RSA_PRIVATE_KEY_ALIGNMENT ((int)(sizeof(void*)))
  93. /* pubic and private key operations */
  94. void gsRSApub_cipher(IppsBigNumState* pY, const IppsBigNumState* pX, const IppsRSAPublicKeyState* pKey, BNU_CHUNK_T* pScratchBuffer);
  95. void gsRSAprv_cipher(IppsBigNumState* pY, const IppsBigNumState* pX, const IppsRSAPrivateKeyState* pKey, BNU_CHUNK_T* pScratchBuffer);
  96. void gsRSAprv_cipher_crt(IppsBigNumState* pY, const IppsBigNumState* pX, const IppsRSAPrivateKeyState* pKey, BNU_CHUNK_T* pScratchBuffer);
  97. #endif /* _CP_NG_RSA_H */