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