rsa_locl.h 4.9 KB

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