rsa_local.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright 2006-2017 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 <openssl/rsa.h>
  12. #include "internal/refcount.h"
  13. #define RSA_MAX_PRIME_NUM 5
  14. #define RSA_MIN_MODULUS_BITS 512
  15. typedef struct rsa_prime_info_st {
  16. BIGNUM *r;
  17. BIGNUM *d;
  18. BIGNUM *t;
  19. /* save product of primes prior to this one */
  20. BIGNUM *pp;
  21. BN_MONT_CTX *m;
  22. } RSA_PRIME_INFO;
  23. DECLARE_ASN1_ITEM(RSA_PRIME_INFO)
  24. DEFINE_STACK_OF(RSA_PRIME_INFO)
  25. struct rsa_st {
  26. /*
  27. * The first parameter is used to pickup errors where this is passed
  28. * instead of an EVP_PKEY, it is set to 0
  29. */
  30. int pad;
  31. int32_t version;
  32. const RSA_METHOD *meth;
  33. /* functional reference if 'meth' is ENGINE-provided */
  34. ENGINE *engine;
  35. BIGNUM *n;
  36. BIGNUM *e;
  37. BIGNUM *d;
  38. BIGNUM *p;
  39. BIGNUM *q;
  40. BIGNUM *dmp1;
  41. BIGNUM *dmq1;
  42. BIGNUM *iqmp;
  43. /* for multi-prime RSA, defined in RFC 8017 */
  44. STACK_OF(RSA_PRIME_INFO) *prime_infos;
  45. /* If a PSS only key this contains the parameter restrictions */
  46. RSA_PSS_PARAMS *pss;
  47. /* be careful using this if the RSA structure is shared */
  48. CRYPTO_EX_DATA ex_data;
  49. CRYPTO_REF_COUNT references;
  50. int flags;
  51. /* Used to cache montgomery values */
  52. BN_MONT_CTX *_method_mod_n;
  53. BN_MONT_CTX *_method_mod_p;
  54. BN_MONT_CTX *_method_mod_q;
  55. /*
  56. * all BIGNUM values are actually in the following data, if it is not
  57. * NULL
  58. */
  59. char *bignum_data;
  60. BN_BLINDING *blinding;
  61. BN_BLINDING *mt_blinding;
  62. CRYPTO_RWLOCK *lock;
  63. };
  64. struct rsa_meth_st {
  65. char *name;
  66. int (*rsa_pub_enc) (int flen, const unsigned char *from,
  67. unsigned char *to, RSA *rsa, int padding);
  68. int (*rsa_pub_dec) (int flen, const unsigned char *from,
  69. unsigned char *to, RSA *rsa, int padding);
  70. int (*rsa_priv_enc) (int flen, const unsigned char *from,
  71. unsigned char *to, RSA *rsa, int padding);
  72. int (*rsa_priv_dec) (int flen, const unsigned char *from,
  73. unsigned char *to, RSA *rsa, int padding);
  74. /* Can be null */
  75. int (*rsa_mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
  76. /* Can be null */
  77. int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  78. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  79. /* called at new */
  80. int (*init) (RSA *rsa);
  81. /* called at free */
  82. int (*finish) (RSA *rsa);
  83. /* RSA_METHOD_FLAG_* things */
  84. int flags;
  85. /* may be needed! */
  86. char *app_data;
  87. /*
  88. * New sign and verify functions: some libraries don't allow arbitrary
  89. * data to be signed/verified: this allows them to be used. Note: for
  90. * this to work the RSA_public_decrypt() and RSA_private_encrypt() should
  91. * *NOT* be used RSA_sign(), RSA_verify() should be used instead.
  92. */
  93. int (*rsa_sign) (int type,
  94. const unsigned char *m, unsigned int m_length,
  95. unsigned char *sigret, unsigned int *siglen,
  96. const RSA *rsa);
  97. int (*rsa_verify) (int dtype, const unsigned char *m,
  98. unsigned int m_length, const unsigned char *sigbuf,
  99. unsigned int siglen, const RSA *rsa);
  100. /*
  101. * If this callback is NULL, the builtin software RSA key-gen will be
  102. * used. This is for behavioural compatibility whilst the code gets
  103. * rewired, but one day it would be nice to assume there are no such
  104. * things as "builtin software" implementations.
  105. */
  106. int (*rsa_keygen) (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
  107. int (*rsa_multi_prime_keygen) (RSA *rsa, int bits, int primes,
  108. BIGNUM *e, BN_GENCB *cb);
  109. };
  110. extern int int_rsa_verify(int dtype, const unsigned char *m,
  111. unsigned int m_len, unsigned char *rm,
  112. size_t *prm_len, const unsigned char *sigbuf,
  113. size_t siglen, RSA *rsa);
  114. /* Macros to test if a pkey or ctx is for a PSS key */
  115. #define pkey_is_pss(pkey) (pkey->ameth->pkey_id == EVP_PKEY_RSA_PSS)
  116. #define pkey_ctx_is_pss(ctx) (ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS)
  117. RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
  118. const EVP_MD *mgf1md, int saltlen);
  119. int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
  120. const EVP_MD **pmgf1md, int *psaltlen);
  121. /* internal function to clear and free multi-prime parameters */
  122. void rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo);
  123. void rsa_multip_info_free(RSA_PRIME_INFO *pinfo);
  124. RSA_PRIME_INFO *rsa_multip_info_new(void);
  125. int rsa_multip_calc_product(RSA *rsa);
  126. int rsa_multip_cap(int bits);
  127. uint16_t rsa_compute_security_bits(int n);
  128. int rsa_sp800_56b_validate_strength(int nbits, int strength);
  129. int rsa_check_pminusq_diff(BIGNUM *diff, const BIGNUM *p, const BIGNUM *q,
  130. int nbits);
  131. int rsa_get_lcm(BN_CTX *ctx, const BIGNUM *p, const BIGNUM *q,
  132. BIGNUM *lcm, BIGNUM *gcd, BIGNUM *p1, BIGNUM *q1,
  133. BIGNUM *p1q1);
  134. int rsa_check_public_exponent(const BIGNUM *e);
  135. int rsa_check_private_exponent(const RSA *rsa, int nbits, BN_CTX *ctx);
  136. int rsa_check_prime_factor(BIGNUM *p, BIGNUM *e, int nbits, BN_CTX *ctx);
  137. int rsa_check_prime_factor_range(const BIGNUM *p, int nbits, BN_CTX *ctx);
  138. int rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx);
  139. int rsa_sp800_56b_pairwise_test(RSA *rsa, BN_CTX *ctx);
  140. int rsa_sp800_56b_check_public(const RSA *rsa);
  141. int rsa_sp800_56b_check_private(const RSA *rsa);
  142. int rsa_sp800_56b_check_keypair(const RSA *rsa, const BIGNUM *efixed,
  143. int strength, int nbits);
  144. int rsa_sp800_56b_generate_key(RSA *rsa, int nbits, const BIGNUM *efixed,
  145. BN_GENCB *cb);
  146. int rsa_sp800_56b_derive_params_from_pq(RSA *rsa, int nbits,
  147. const BIGNUM *e, BN_CTX *ctx);
  148. int rsa_fips186_4_gen_prob_primes(RSA *rsa, BIGNUM *p1, BIGNUM *p2,
  149. BIGNUM *Xpout, const BIGNUM *Xp,
  150. const BIGNUM *Xp1, const BIGNUM *Xp2,
  151. BIGNUM *q1, BIGNUM *q2, BIGNUM *Xqout,
  152. const BIGNUM *Xq, const BIGNUM *Xq1,
  153. const BIGNUM *Xq2, int nbits,
  154. const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb);
  155. #endif /* OSSL_CRYPTO_RSA_LOCAL_H */