2 * Copyright 2006-2017 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/rsa.h>
11 #include "internal/refcount.h"
13 #define RSA_MAX_PRIME_NUM 5
14 #define RSA_MIN_MODULUS_BITS 512
16 typedef struct rsa_prime_info_st {
20 /* save product of primes prior to this one */
25 DECLARE_ASN1_ITEM(RSA_PRIME_INFO)
26 DEFINE_STACK_OF(RSA_PRIME_INFO)
30 * The first parameter is used to pickup errors where this is passed
31 * instead of an EVP_PKEY, it is set to 0
35 const RSA_METHOD *meth;
36 /* functional reference if 'meth' is ENGINE-provided */
46 /* for multi-prime RSA, defined in RFC 8017 */
47 STACK_OF(RSA_PRIME_INFO) *prime_infos;
48 /* If a PSS only key this contains the parameter restrictions */
50 /* be careful using this if the RSA structure is shared */
51 CRYPTO_EX_DATA ex_data;
52 CRYPTO_REF_COUNT references;
54 /* Used to cache montgomery values */
55 BN_MONT_CTX *_method_mod_n;
56 BN_MONT_CTX *_method_mod_p;
57 BN_MONT_CTX *_method_mod_q;
59 * all BIGNUM values are actually in the following data, if it is not
63 BN_BLINDING *blinding;
64 BN_BLINDING *mt_blinding;
70 int (*rsa_pub_enc) (int flen, const unsigned char *from,
71 unsigned char *to, RSA *rsa, int padding);
72 int (*rsa_pub_dec) (int flen, const unsigned char *from,
73 unsigned char *to, RSA *rsa, int padding);
74 int (*rsa_priv_enc) (int flen, const unsigned char *from,
75 unsigned char *to, RSA *rsa, int padding);
76 int (*rsa_priv_dec) (int flen, const unsigned char *from,
77 unsigned char *to, RSA *rsa, int padding);
79 int (*rsa_mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
81 int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
82 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
84 int (*init) (RSA *rsa);
86 int (*finish) (RSA *rsa);
87 /* RSA_METHOD_FLAG_* things */
92 * New sign and verify functions: some libraries don't allow arbitrary
93 * data to be signed/verified: this allows them to be used. Note: for
94 * this to work the RSA_public_decrypt() and RSA_private_encrypt() should
95 * *NOT* be used RSA_sign(), RSA_verify() should be used instead.
97 int (*rsa_sign) (int type,
98 const unsigned char *m, unsigned int m_length,
99 unsigned char *sigret, unsigned int *siglen,
101 int (*rsa_verify) (int dtype, const unsigned char *m,
102 unsigned int m_length, const unsigned char *sigbuf,
103 unsigned int siglen, const RSA *rsa);
105 * If this callback is NULL, the builtin software RSA key-gen will be
106 * used. This is for behavioural compatibility whilst the code gets
107 * rewired, but one day it would be nice to assume there are no such
108 * things as "builtin software" implementations.
110 int (*rsa_keygen) (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
111 int (*rsa_multi_prime_keygen) (RSA *rsa, int bits, int primes,
112 BIGNUM *e, BN_GENCB *cb);
115 extern int int_rsa_verify(int dtype, const unsigned char *m,
116 unsigned int m_len, unsigned char *rm,
117 size_t *prm_len, const unsigned char *sigbuf,
118 size_t siglen, RSA *rsa);
119 /* Macros to test if a pkey or ctx is for a PSS key */
120 #define pkey_is_pss(pkey) (pkey->ameth->pkey_id == EVP_PKEY_RSA_PSS)
121 #define pkey_ctx_is_pss(ctx) (ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS)
123 RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
124 const EVP_MD *mgf1md, int saltlen);
125 int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
126 const EVP_MD **pmgf1md, int *psaltlen);
127 /* internal function to clear and free multi-prime parameters */
128 void rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo);
129 void rsa_multip_info_free(RSA_PRIME_INFO *pinfo);
130 RSA_PRIME_INFO *rsa_multip_info_new(void);
131 int rsa_multip_calc_product(RSA *rsa);
132 int rsa_multip_cap(int bits);