2016-04-02 21:12:58 +08:00
|
|
|
/*
|
2017-08-02 02:19:43 +08:00
|
|
|
* Copyright 2006-2017 The OpenSSL Project Authors. All Rights Reserved.
|
2016-04-02 21:12:58 +08:00
|
|
|
*
|
2016-05-18 02:51:34 +08:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
2016-04-02 21:12:58 +08:00
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <openssl/rsa.h>
|
2016-08-27 22:01:08 +08:00
|
|
|
#include "internal/refcount.h"
|
2016-04-02 21:12:58 +08:00
|
|
|
|
2017-08-02 02:19:43 +08:00
|
|
|
#define RSA_MAX_PRIME_NUM 16
|
|
|
|
#define RSA_MIN_PRIME_SIZE 64
|
2017-10-18 19:30:23 +08:00
|
|
|
#define RSA_MIN_MODULUS_BITS 512
|
2017-08-02 02:19:43 +08:00
|
|
|
|
|
|
|
typedef struct rsa_prime_info_st {
|
|
|
|
BIGNUM *r;
|
|
|
|
BIGNUM *d;
|
|
|
|
BIGNUM *t;
|
|
|
|
/* save product of primes prior to this one */
|
|
|
|
BIGNUM *pp;
|
|
|
|
BN_MONT_CTX *m;
|
|
|
|
} RSA_PRIME_INFO;
|
|
|
|
|
|
|
|
DECLARE_ASN1_ITEM(RSA_PRIME_INFO)
|
|
|
|
DEFINE_STACK_OF(RSA_PRIME_INFO)
|
|
|
|
|
2016-04-02 21:12:58 +08:00
|
|
|
struct rsa_st {
|
|
|
|
/*
|
|
|
|
* The first parameter is used to pickup errors where this is passed
|
2017-12-05 19:01:14 +08:00
|
|
|
* instead of an EVP_PKEY, it is set to 0
|
2016-04-02 21:12:58 +08:00
|
|
|
*/
|
|
|
|
int pad;
|
2017-04-05 19:24:14 +08:00
|
|
|
int32_t version;
|
2016-04-02 21:12:58 +08:00
|
|
|
const RSA_METHOD *meth;
|
|
|
|
/* functional reference if 'meth' is ENGINE-provided */
|
|
|
|
ENGINE *engine;
|
|
|
|
BIGNUM *n;
|
|
|
|
BIGNUM *e;
|
|
|
|
BIGNUM *d;
|
|
|
|
BIGNUM *p;
|
|
|
|
BIGNUM *q;
|
|
|
|
BIGNUM *dmp1;
|
|
|
|
BIGNUM *dmq1;
|
|
|
|
BIGNUM *iqmp;
|
2017-08-02 02:19:43 +08:00
|
|
|
/* for multi-prime RSA, defined in RFC 8017 */
|
|
|
|
STACK_OF(RSA_PRIME_INFO) *prime_infos;
|
2016-11-21 09:34:56 +08:00
|
|
|
/* If a PSS only key this contains the parameter restrictions */
|
|
|
|
RSA_PSS_PARAMS *pss;
|
2016-04-02 21:12:58 +08:00
|
|
|
/* be careful using this if the RSA structure is shared */
|
|
|
|
CRYPTO_EX_DATA ex_data;
|
2016-08-27 22:01:08 +08:00
|
|
|
CRYPTO_REF_COUNT references;
|
2016-04-02 21:12:58 +08:00
|
|
|
int flags;
|
|
|
|
/* Used to cache montgomery values */
|
|
|
|
BN_MONT_CTX *_method_mod_n;
|
|
|
|
BN_MONT_CTX *_method_mod_p;
|
|
|
|
BN_MONT_CTX *_method_mod_q;
|
|
|
|
/*
|
|
|
|
* all BIGNUM values are actually in the following data, if it is not
|
|
|
|
* NULL
|
|
|
|
*/
|
|
|
|
char *bignum_data;
|
|
|
|
BN_BLINDING *blinding;
|
|
|
|
BN_BLINDING *mt_blinding;
|
|
|
|
CRYPTO_RWLOCK *lock;
|
|
|
|
};
|
|
|
|
|
2016-04-03 00:46:17 +08:00
|
|
|
struct rsa_meth_st {
|
|
|
|
char *name;
|
|
|
|
int (*rsa_pub_enc) (int flen, const unsigned char *from,
|
|
|
|
unsigned char *to, RSA *rsa, int padding);
|
|
|
|
int (*rsa_pub_dec) (int flen, const unsigned char *from,
|
|
|
|
unsigned char *to, RSA *rsa, int padding);
|
|
|
|
int (*rsa_priv_enc) (int flen, const unsigned char *from,
|
|
|
|
unsigned char *to, RSA *rsa, int padding);
|
|
|
|
int (*rsa_priv_dec) (int flen, const unsigned char *from,
|
|
|
|
unsigned char *to, RSA *rsa, int padding);
|
|
|
|
/* Can be null */
|
|
|
|
int (*rsa_mod_exp) (BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
|
|
|
|
/* Can be null */
|
|
|
|
int (*bn_mod_exp) (BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
|
|
|
|
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
|
|
|
|
/* called at new */
|
|
|
|
int (*init) (RSA *rsa);
|
|
|
|
/* called at free */
|
|
|
|
int (*finish) (RSA *rsa);
|
|
|
|
/* RSA_METHOD_FLAG_* things */
|
|
|
|
int flags;
|
|
|
|
/* may be needed! */
|
|
|
|
char *app_data;
|
|
|
|
/*
|
|
|
|
* New sign and verify functions: some libraries don't allow arbitrary
|
|
|
|
* data to be signed/verified: this allows them to be used. Note: for
|
|
|
|
* this to work the RSA_public_decrypt() and RSA_private_encrypt() should
|
|
|
|
* *NOT* be used RSA_sign(), RSA_verify() should be used instead.
|
|
|
|
*/
|
|
|
|
int (*rsa_sign) (int type,
|
|
|
|
const unsigned char *m, unsigned int m_length,
|
|
|
|
unsigned char *sigret, unsigned int *siglen,
|
|
|
|
const RSA *rsa);
|
|
|
|
int (*rsa_verify) (int dtype, const unsigned char *m,
|
|
|
|
unsigned int m_length, const unsigned char *sigbuf,
|
|
|
|
unsigned int siglen, const RSA *rsa);
|
|
|
|
/*
|
|
|
|
* If this callback is NULL, the builtin software RSA key-gen will be
|
|
|
|
* used. This is for behavioural compatibility whilst the code gets
|
|
|
|
* rewired, but one day it would be nice to assume there are no such
|
|
|
|
* things as "builtin software" implementations.
|
|
|
|
*/
|
|
|
|
int (*rsa_keygen) (RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
|
2017-08-02 02:19:43 +08:00
|
|
|
int (*rsa_multi_prime_keygen) (RSA *rsa, int bits, int primes,
|
|
|
|
BIGNUM *e, BN_GENCB *cb);
|
2016-04-03 00:46:17 +08:00
|
|
|
};
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
extern int int_rsa_verify(int dtype, const unsigned char *m,
|
|
|
|
unsigned int m_len, unsigned char *rm,
|
|
|
|
size_t *prm_len, const unsigned char *sigbuf,
|
|
|
|
size_t siglen, RSA *rsa);
|
2016-12-02 05:46:31 +08:00
|
|
|
/* Macros to test if a pkey or ctx is for a PSS key */
|
|
|
|
#define pkey_is_pss(pkey) (pkey->ameth->pkey_id == EVP_PKEY_RSA_PSS)
|
|
|
|
#define pkey_ctx_is_pss(ctx) (ctx->pmeth->pkey_id == EVP_PKEY_RSA_PSS)
|
2016-11-21 09:35:30 +08:00
|
|
|
|
|
|
|
RSA_PSS_PARAMS *rsa_pss_params_create(const EVP_MD *sigmd,
|
|
|
|
const EVP_MD *mgf1md, int saltlen);
|
2016-12-05 22:00:48 +08:00
|
|
|
int rsa_pss_get_param(const RSA_PSS_PARAMS *pss, const EVP_MD **pmd,
|
|
|
|
const EVP_MD **pmgf1md, int *psaltlen);
|
2017-08-02 02:19:43 +08:00
|
|
|
/* internal function to clear and free multi-prime parameters */
|
|
|
|
void rsa_multip_info_free_ex(RSA_PRIME_INFO *pinfo);
|
|
|
|
void rsa_multip_info_free(RSA_PRIME_INFO *pinfo);
|
|
|
|
RSA_PRIME_INFO *rsa_multip_info_new(void);
|
|
|
|
int rsa_multip_calc_product(RSA *rsa);
|
2017-11-25 04:31:11 +08:00
|
|
|
int rsa_multip_cap(int bits);
|