rsa.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* rsa.h
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* rsa.h for openSSL */
  22. #ifndef WOLFSSL_RSA_H_
  23. #define WOLFSSL_RSA_H_
  24. #include <wolfssl/openssl/bn.h>
  25. #include <wolfssl/openssl/err.h>
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  31. /* Padding types */
  32. #define RSA_PKCS1_PADDING 0
  33. #define RSA_PKCS1_OAEP_PADDING 1
  34. #define RSA_PKCS1_PSS_PADDING 2
  35. #define RSA_NO_PADDING 3
  36. /* Emulate OpenSSL flags */
  37. #define RSA_METHOD_FLAG_NO_CHECK (1 << 1)
  38. #define RSA_FLAG_CACHE_PUBLIC (1 << 2)
  39. #define RSA_FLAG_CACHE_PRIVATE (1 << 3)
  40. #define RSA_FLAG_BLINDING (1 << 4)
  41. #define RSA_FLAG_THREAD_SAFE (1 << 5)
  42. #define RSA_FLAG_EXT_PKEY (1 << 6)
  43. #define RSA_FLAG_NO_BLINDING (1 << 7)
  44. #define RSA_FLAG_NO_CONSTTIME (1 << 8)
  45. /* Salt length same as digest length */
  46. #define RSA_PSS_SALTLEN_DIGEST (-1)
  47. /* Old max salt length */
  48. #define RSA_PSS_SALTLEN_MAX_SIGN (-2)
  49. /* Max salt length */
  50. #define RSA_PSS_SALTLEN_MAX (-3)
  51. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  52. typedef struct WOLFSSL_RSA_METHOD {
  53. /* Flags of RSA key implementation. */
  54. int flags;
  55. /* Name of RSA key implementation. */
  56. char *name;
  57. /* RSA method dynamically allocated. */
  58. word16 dynamic:1;
  59. } WOLFSSL_RSA_METHOD;
  60. #ifndef WOLFSSL_RSA_TYPE_DEFINED /* guard on redeclaration */
  61. #define WOLFSSL_RSA_TYPE_DEFINED
  62. /* RSA key compatible with OpenSSL. */
  63. typedef struct WOLFSSL_RSA {
  64. WOLFSSL_BIGNUM* n; /* Modulus. */
  65. WOLFSSL_BIGNUM* e; /* Public exponent. */
  66. WOLFSSL_BIGNUM* d; /* Private exponent. */
  67. WOLFSSL_BIGNUM* p; /* First prime. */
  68. WOLFSSL_BIGNUM* q; /* Second prime. */
  69. WOLFSSL_BIGNUM* dmp1; /* dP = d mod (p - 1) */
  70. WOLFSSL_BIGNUM* dmq1; /* dQ = d mod (q - 1) */
  71. WOLFSSL_BIGNUM* iqmp; /* u = (1 / q) mod p */
  72. void* heap; /* Heap used for memory allocations. */
  73. void* internal; /* wolfCrypt RSA key. */
  74. #if defined(OPENSSL_EXTRA)
  75. const WOLFSSL_RSA_METHOD* meth; /* RSA method. */
  76. #endif
  77. #ifdef HAVE_EX_DATA
  78. WOLFSSL_CRYPTO_EX_DATA ex_data; /* external data */
  79. #endif
  80. wolfSSL_Ref ref; /* Reference count information. */
  81. word16 pkcs8HeaderSz; /* Size of PKCS#8 header from decode. */
  82. int flags; /* Flags of implementation. */
  83. /* bits */
  84. byte inSet:1; /* Internal set from external. */
  85. byte exSet:1; /* External set from internal. */
  86. byte ownRng:1; /* Rng needs to be free'd. */
  87. } WOLFSSL_RSA;
  88. #endif
  89. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  90. typedef WOLFSSL_RSA RSA;
  91. typedef WOLFSSL_RSA_METHOD RSA_METHOD;
  92. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  93. WOLFSSL_API WOLFSSL_RSA* wolfSSL_RSA_new_ex(void* heap, int devId);
  94. WOLFSSL_API WOLFSSL_RSA* wolfSSL_RSA_new(void);
  95. WOLFSSL_API void wolfSSL_RSA_free(WOLFSSL_RSA* rsa);
  96. WOLFSSL_API int wolfSSL_RSA_generate_key_ex(WOLFSSL_RSA* rsa, int bits,
  97. WOLFSSL_BIGNUM* bn, void* cb);
  98. WOLFSSL_API int wolfSSL_RSA_blinding_on(WOLFSSL_RSA* rsa, WOLFSSL_BN_CTX* bn);
  99. WOLFSSL_API int wolfSSL_RSA_check_key(const WOLFSSL_RSA* rsa);
  100. WOLFSSL_API int wolfSSL_RSA_public_encrypt(int len, const unsigned char* fr,
  101. unsigned char* to, WOLFSSL_RSA* rsa,
  102. int padding);
  103. WOLFSSL_API int wolfSSL_RSA_private_decrypt(int len, const unsigned char* fr,
  104. unsigned char* to, WOLFSSL_RSA* rsa,
  105. int padding);
  106. WOLFSSL_API int wolfSSL_RSA_private_encrypt(int len, const unsigned char* in,
  107. unsigned char* out, WOLFSSL_RSA* rsa, int padding);
  108. WOLFSSL_API int wolfSSL_RSA_size(const WOLFSSL_RSA* rsa);
  109. WOLFSSL_API int wolfSSL_RSA_bits(const WOLFSSL_RSA* rsa);
  110. WOLFSSL_API int wolfSSL_RSA_sign(int type, const unsigned char* m,
  111. unsigned int mLen, unsigned char* sigRet,
  112. unsigned int* sigLen, WOLFSSL_RSA* rsa);
  113. WOLFSSL_API int wolfSSL_RSA_sign_ex(int type, const unsigned char* m,
  114. unsigned int mLen, unsigned char* sigRet,
  115. unsigned int* sigLen, WOLFSSL_RSA* rsa,
  116. int flag);
  117. WOLFSSL_API int wolfSSL_RSA_sign_generic_padding(int type, const unsigned char* m,
  118. unsigned int mLen, unsigned char* sigRet,
  119. unsigned int* sigLen, WOLFSSL_RSA* rsa, int flag,
  120. int padding);
  121. WOLFSSL_API int wolfSSL_RSA_verify(int type, const unsigned char* m,
  122. unsigned int mLen, const unsigned char* sig,
  123. unsigned int sigLen, WOLFSSL_RSA* rsa);
  124. WOLFSSL_API int wolfSSL_RSA_verify_ex(int type, const unsigned char* m,
  125. unsigned int mLen, const unsigned char* sig,
  126. unsigned int sigLen, WOLFSSL_RSA* rsa,
  127. int padding);
  128. WOLFSSL_API int wolfSSL_RSA_public_decrypt(int flen, const unsigned char* from,
  129. unsigned char* to, WOLFSSL_RSA* rsa, int padding);
  130. WOLFSSL_API int wolfSSL_RSA_GenAdd(WOLFSSL_RSA* rsa);
  131. WOLFSSL_API int wolfSSL_RSA_LoadDer(WOLFSSL_RSA* rsa,
  132. const unsigned char* derBuf, int derSz);
  133. WOLFSSL_API int wolfSSL_RSA_LoadDer_ex(WOLFSSL_RSA* rsa,
  134. const unsigned char* derBuf, int derSz, int opt);
  135. WOLFSSL_API WOLFSSL_RSA_METHOD *wolfSSL_RSA_meth_new(const char *name, int flags);
  136. WOLFSSL_API void wolfSSL_RSA_meth_free(WOLFSSL_RSA_METHOD *meth);
  137. WOLFSSL_API int wolfSSL_RSA_meth_set(WOLFSSL_RSA_METHOD *rsa, void* p);
  138. WOLFSSL_API int wolfSSL_RSA_set_method(WOLFSSL_RSA *rsa, WOLFSSL_RSA_METHOD *meth);
  139. WOLFSSL_API const WOLFSSL_RSA_METHOD* wolfSSL_RSA_get_method(const WOLFSSL_RSA *rsa);
  140. WOLFSSL_API const WOLFSSL_RSA_METHOD* wolfSSL_RSA_get_default_method(void);
  141. WOLFSSL_API void wolfSSL_RSA_get0_crt_params(const WOLFSSL_RSA *r,
  142. const WOLFSSL_BIGNUM **dmp1,
  143. const WOLFSSL_BIGNUM **dmq1,
  144. const WOLFSSL_BIGNUM **iqmp);
  145. WOLFSSL_API int wolfSSL_RSA_set0_crt_params(WOLFSSL_RSA *r, WOLFSSL_BIGNUM *dmp1,
  146. WOLFSSL_BIGNUM *dmq1, WOLFSSL_BIGNUM *iqmp);
  147. WOLFSSL_API void wolfSSL_RSA_get0_factors(const WOLFSSL_RSA *r, const WOLFSSL_BIGNUM **p,
  148. const WOLFSSL_BIGNUM **q);
  149. WOLFSSL_API int wolfSSL_RSA_set0_factors(WOLFSSL_RSA *r, WOLFSSL_BIGNUM *p, WOLFSSL_BIGNUM *q);
  150. WOLFSSL_API void wolfSSL_RSA_get0_key(const WOLFSSL_RSA *r, const WOLFSSL_BIGNUM **n,
  151. const WOLFSSL_BIGNUM **e, const WOLFSSL_BIGNUM **d);
  152. WOLFSSL_API int wolfSSL_RSA_set0_key(WOLFSSL_RSA *r, WOLFSSL_BIGNUM *n, WOLFSSL_BIGNUM *e,
  153. WOLFSSL_BIGNUM *d);
  154. WOLFSSL_API int wolfSSL_RSA_flags(const WOLFSSL_RSA *r);
  155. WOLFSSL_API void wolfSSL_RSA_set_flags(WOLFSSL_RSA *r, int flags);
  156. WOLFSSL_API void wolfSSL_RSA_clear_flags(WOLFSSL_RSA *r, int flags);
  157. WOLFSSL_API int wolfSSL_RSA_test_flags(const WOLFSSL_RSA *r, int flags);
  158. WOLFSSL_API WOLFSSL_RSA* wolfSSL_RSAPublicKey_dup(WOLFSSL_RSA *rsa);
  159. WOLFSSL_API void* wolfSSL_RSA_get_ex_data(const WOLFSSL_RSA *rsa, int idx);
  160. WOLFSSL_API int wolfSSL_RSA_set_ex_data(WOLFSSL_RSA *rsa, int idx, void *data);
  161. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  162. WOLFSSL_API int wolfSSL_RSA_set_ex_data_with_cleanup(
  163. WOLFSSL_RSA *rsa,
  164. int idx,
  165. void *data,
  166. wolfSSL_ex_data_cleanup_routine_t cleanup_routine);
  167. #endif
  168. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  169. #define WOLFSSL_RSA_LOAD_PRIVATE 1
  170. #define WOLFSSL_RSA_LOAD_PUBLIC 2
  171. #define WOLFSSL_RSA_F4 0x10001L
  172. #define RSA_new wolfSSL_RSA_new
  173. #define RSA_free wolfSSL_RSA_free
  174. #define RSA_generate_key_ex wolfSSL_RSA_generate_key_ex
  175. #define RSA_blinding_on wolfSSL_RSA_blinding_on
  176. #define RSA_check_key wolfSSL_RSA_check_key
  177. #define RSA_public_encrypt wolfSSL_RSA_public_encrypt
  178. #define RSA_private_decrypt wolfSSL_RSA_private_decrypt
  179. #define RSA_private_encrypt wolfSSL_RSA_private_encrypt
  180. #define RSA_size wolfSSL_RSA_size
  181. #define RSA_sign wolfSSL_RSA_sign
  182. #define RSA_verify wolfSSL_RSA_verify
  183. #define RSA_public_decrypt wolfSSL_RSA_public_decrypt
  184. #define RSA_meth_new wolfSSL_RSA_meth_new
  185. #define RSA_meth_free wolfSSL_RSA_meth_free
  186. #define RSA_meth_set_pub_enc wolfSSL_RSA_meth_set
  187. #define RSA_meth_set_pub_dec wolfSSL_RSA_meth_set
  188. #define RSA_meth_set_priv_enc wolfSSL_RSA_meth_set
  189. #define RSA_meth_set_priv_dec wolfSSL_RSA_meth_set
  190. #define RSA_meth_set_init wolfSSL_RSA_meth_set
  191. #define RSA_meth_set_finish wolfSSL_RSA_meth_set
  192. #define RSA_meth_set0_app_data wolfSSL_RSA_meth_set
  193. #define RSA_get_default_method wolfSSL_RSA_get_default_method
  194. #define RSA_get_method wolfSSL_RSA_get_method
  195. #define RSA_set_method wolfSSL_RSA_set_method
  196. #define RSA_get0_crt_params wolfSSL_RSA_get0_crt_params
  197. #define RSA_set0_crt_params wolfSSL_RSA_set0_crt_params
  198. #define RSA_get0_factors wolfSSL_RSA_get0_factors
  199. #define RSA_set0_factors wolfSSL_RSA_set0_factors
  200. #define RSA_get0_key wolfSSL_RSA_get0_key
  201. #define RSA_set0_key wolfSSL_RSA_set0_key
  202. #define RSA_flags wolfSSL_RSA_flags
  203. #define RSA_set_flags wolfSSL_RSA_set_flags
  204. #define RSA_clear_flags wolfSSL_RSA_clear_flags
  205. #define RSA_test_flags wolfSSL_RSA_test_flags
  206. #define RSAPublicKey_dup wolfSSL_RSAPublicKey_dup
  207. #define RSA_get_ex_data wolfSSL_RSA_get_ex_data
  208. #define RSA_set_ex_data wolfSSL_RSA_set_ex_data
  209. #define RSA_get0_key wolfSSL_RSA_get0_key
  210. #define RSA_F4 WOLFSSL_RSA_F4
  211. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  212. #ifdef __cplusplus
  213. } /* extern "C" */
  214. #endif
  215. #endif /* header */