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