rsa_local.h 7.4 KB

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