rsa_local.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_CRYPTO_RSA_LOCAL_H
  10. #define OSSL_CRYPTO_RSA_LOCAL_H
  11. #include "crypto/rsa.h"
  12. #include "internal/refcount.h"
  13. #include "crypto/rsa.h"
  14. #define RSA_MAX_PRIME_NUM 5
  15. #define RSA_MIN_MODULUS_BITS 512
  16. typedef struct rsa_prime_info_st {
  17. BIGNUM *r;
  18. BIGNUM *d;
  19. BIGNUM *t;
  20. /* save product of primes prior to this one */
  21. BIGNUM *pp;
  22. BN_MONT_CTX *m;
  23. } RSA_PRIME_INFO;
  24. DECLARE_ASN1_ITEM(RSA_PRIME_INFO)
  25. DEFINE_STACK_OF(RSA_PRIME_INFO)
  26. #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
  27. struct rsa_acvp_test_st {
  28. /* optional inputs */
  29. BIGNUM *Xp1;
  30. BIGNUM *Xp2;
  31. BIGNUM *Xq1;
  32. BIGNUM *Xq2;
  33. BIGNUM *Xp;
  34. BIGNUM *Xq;
  35. /* optional outputs */
  36. BIGNUM *p1;
  37. BIGNUM *p2;
  38. BIGNUM *q1;
  39. BIGNUM *q2;
  40. };
  41. #endif
  42. struct rsa_st {
  43. /*
  44. * #legacy
  45. * The first field is used to pickup errors where this is passed
  46. * instead of an EVP_PKEY. It is always zero.
  47. * THIS MUST REMAIN THE FIRST FIELD.
  48. */
  49. int dummy_zero;
  50. OSSL_LIB_CTX *libctx;
  51. int32_t version;
  52. const RSA_METHOD *meth;
  53. /* functional reference if 'meth' is ENGINE-provided */
  54. ENGINE *engine;
  55. BIGNUM *n;
  56. BIGNUM *e;
  57. BIGNUM *d;
  58. BIGNUM *p;
  59. BIGNUM *q;
  60. BIGNUM *dmp1;
  61. BIGNUM *dmq1;
  62. BIGNUM *iqmp;
  63. /*
  64. * If a PSS only key this contains the parameter restrictions.
  65. * There are two structures for the same thing, used in different cases.
  66. */
  67. /* This is used uniquely by OpenSSL provider implementations. */
  68. RSA_PSS_PARAMS_30 pss_params;
  69. #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
  70. RSA_ACVP_TEST *acvp_test;
  71. #endif
  72. #ifndef FIPS_MODULE
  73. /* This is used uniquely by rsa_ameth.c and rsa_pmeth.c. */
  74. RSA_PSS_PARAMS *pss;
  75. /* for multi-prime RSA, defined in RFC 8017 */
  76. STACK_OF(RSA_PRIME_INFO) *prime_infos;
  77. /* Be careful using this if the RSA structure is shared */
  78. CRYPTO_EX_DATA ex_data;
  79. #endif
  80. CRYPTO_REF_COUNT references;
  81. int flags;
  82. /* Used to cache montgomery values */
  83. BN_MONT_CTX *_method_mod_n;
  84. BN_MONT_CTX *_method_mod_p;
  85. BN_MONT_CTX *_method_mod_q;
  86. /*
  87. * all BIGNUM values are actually in the following data, if it is not
  88. * NULL
  89. */
  90. char *bignum_data;
  91. BN_BLINDING *blinding;
  92. BN_BLINDING *mt_blinding;
  93. CRYPTO_RWLOCK *lock;
  94. int dirty_cnt;
  95. };
  96. struct rsa_meth_st {
  97. char *name;
  98. int (*rsa_pub_enc) (int flen, const unsigned char *from,
  99. unsigned char *to, RSA *rsa, int padding);
  100. int (*rsa_pub_dec) (int flen, const unsigned char *from,
  101. unsigned char *to, RSA *rsa, int padding);
  102. int (*rsa_priv_enc) (int flen, const unsigned char *from,
  103. unsigned char *to, RSA *rsa, int padding);
  104. int (*rsa_priv_dec) (int flen, const unsigned char *from,
  105. unsigned char *to, RSA *rsa, int padding);
  106. /* Can be null */
  107. int (*rsa_mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
  108. /* Can be null */
  109. int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  110. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  111. /* called at new */
  112. int (*init) (RSA *rsa);
  113. /* called at free */
  114. int (*finish) (RSA *rsa);
  115. /* RSA_METHOD_FLAG_* things */
  116. int flags;
  117. /* may be needed! */
  118. char *app_data;
  119. /*
  120. * New sign and verify functions: some libraries don't allow arbitrary
  121. * data to be signed/verified: this allows them to be used. Note: for
  122. * this to work the RSA_public_decrypt() and RSA_private_encrypt() should
  123. * *NOT* be used RSA_sign(), RSA_verify() should be used instead.
  124. */
  125. int (*rsa_sign) (int type,
  126. const unsigned char *m, unsigned int m_length,
  127. unsigned char *sigret, unsigned int *siglen,
  128. const RSA *rsa);
  129. int (*rsa_verify) (int dtype, const unsigned char *m,
  130. unsigned int m_length, const unsigned char *sigbuf,
  131. unsigned int siglen, const RSA *rsa);
  132. /*
  133. * If this callback is NULL, the builtin software RSA key-gen will be
  134. * used. This is for behavioural compatibility whilst the code gets
  135. * rewired, but one day it would be nice to assume there are no such
  136. * things as "builtin software" implementations.
  137. */
  138. int (*rsa_keygen) (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
  139. int (*rsa_multi_prime_keygen) (RSA *rsa, int bits, int primes,
  140. BIGNUM *e, BN_GENCB *cb);
  141. };
  142. /* Macros to test if a pkey or ctx is for a PSS key */
  143. #define pkey_is_pss(pkey) (pkey->ameth->pkey_id == EVP_PKEY_RSA_PSS)
  144. #define pkey_ctx_is_pss(ctx) (ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS)
  145. RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
  146. const EVP_MD *mgf1md, int saltlen);
  147. int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
  148. const EVP_MD **pmgf1md, int *psaltlen);
  149. /* internal function to clear and free multi-prime parameters */
  150. void rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo);
  151. void rsa_multip_info_free(RSA_PRIME_INFO *pinfo);
  152. RSA_PRIME_INFO *rsa_multip_info_new(void);
  153. int rsa_multip_calc_product(RSA *rsa);
  154. int rsa_multip_cap(int bits);
  155. int ossl_rsa_sp800_56b_validate_strength(int nbits, int strength);
  156. int ossl_rsa_check_pminusq_diff(BIGNUM *diff, const BIGNUM *p, const BIGNUM *q,
  157. int nbits);
  158. int ossl_rsa_get_lcm(BN_CTX *ctx, const BIGNUM *p, const BIGNUM *q,
  159. BIGNUM *lcm, BIGNUM *gcd, BIGNUM *p1, BIGNUM *q1,
  160. BIGNUM *p1q1);
  161. int ossl_rsa_check_public_exponent(const BIGNUM *e);
  162. int ossl_rsa_check_private_exponent(const RSA *rsa, int nbits, BN_CTX *ctx);
  163. int ossl_rsa_check_prime_factor(BIGNUM *p, BIGNUM *e, int nbits, BN_CTX *ctx);
  164. int ossl_rsa_check_prime_factor_range(const BIGNUM *p, int nbits, BN_CTX *ctx);
  165. int ossl_rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx);
  166. int ossl_rsa_sp800_56b_pairwise_test(RSA *rsa, BN_CTX *ctx);
  167. int ossl_rsa_sp800_56b_check_public(const RSA *rsa);
  168. int ossl_rsa_sp800_56b_check_private(const RSA *rsa);
  169. int ossl_rsa_sp800_56b_check_keypair(const RSA *rsa, const BIGNUM *efixed,
  170. int strength, int nbits);
  171. int ossl_rsa_sp800_56b_generate_key(RSA *rsa, int nbits, const BIGNUM *efixed,
  172. BN_GENCB *cb);
  173. int ossl_rsa_sp800_56b_derive_params_from_pq(RSA *rsa, int nbits,
  174. const BIGNUM *e, BN_CTX *ctx);
  175. int ossl_rsa_fips186_4_gen_prob_primes(RSA *rsa, RSA_ACVP_TEST *test,
  176. int nbits, const BIGNUM *e, BN_CTX *ctx,
  177. BN_GENCB *cb);
  178. int ossl_rsa_padding_add_SSLv23_ex(OSSL_LIB_CTX *libctx, unsigned char *to,
  179. int tlen, const unsigned char *from,
  180. int flen);
  181. int ossl_rsa_padding_add_PKCS1_type_2_ex(OSSL_LIB_CTX *libctx, unsigned char *to,
  182. int tlen, const unsigned char *from,
  183. int flen);
  184. #endif /* OSSL_CRYPTO_RSA_LOCAL_H */