API参考
载入中...
搜索中...
未找到
rsa_local.h
1/*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16#ifndef RSA_LOCAL_H
17#define RSA_LOCAL_H
18
19#include "hitls_build.h"
20#ifdef HITLS_CRYPTO_RSA
21
22#include "crypt_rsa.h"
23#include "crypt_bn.h"
24#include "crypt_local_types.h"
25#include "crypt_types.h"
26#include "sal_atomic.h"
27
28#ifdef __cplusplus
29extern "C" {
30#endif /* __cpluscplus */
31
32#define HASH_MAX_MDSIZE (64)
33
34#define PARAMISNULL(a) ((a) == NULL || (a)->value == NULL)
35
36typedef struct RSA_BlindSt {
37 BN_BigNum *r;
38 BN_BigNum *rInv;
39} RSA_Blind;
40
41typedef struct {
42 BN_BigNum *n; // pub key n needed for no padding
43 BN_BigNum *d; // private key d needed for asn encoding
44 BN_BigNum *p; // prime factor p
45 BN_BigNum *q; // prime factor q
46 BN_BigNum *dP; // exponent dP for CRT
47 BN_BigNum *dQ; // exponent dQ for CRT
48 BN_BigNum *qInv; // CRT coefficient qInv
49 BN_BigNum *e; // public key e
50} CRYPT_RSA_PrvKey;
51
52typedef struct {
53 BN_BigNum *n; // modulo Value - converted.Not in char
54 BN_BigNum *e; // Exponent Value -converted.Not in char
55
56 // Montgomery pre-calculation cache
57 BN_Mont *mont;
58} CRYPT_RSA_PubKey;
59
60#ifdef HITLS_CRYPTO_ACVP_TESTS
61typedef struct {
62 BN_BigNum *xp; // main seed for prime p
63 BN_BigNum *xp1; // auxiliary seed1 for prime p
64 BN_BigNum *xp2; // auxiliary seed2 for prime p
65 BN_BigNum *xq; // main seed for prime q
66 BN_BigNum *xq1; // auxiliary seed1 for prime q
67 BN_BigNum *xq2; // auxiliary seed2 for prime q
68} RSA_FIPS_AUX_PRIME_SEEDS;
69
70typedef struct {
71 union {
72 RSA_FIPS_AUX_PRIME_SEEDS fipsPrimeSeeds;
73 } primeSeed;
74} RSA_ACVP_TESTS;
75#endif
76
77struct RSA_Para {
78 BN_BigNum *e; // Exponent Value -converted.Not in char
79 uint32_t bits; // length in bits of modulus
80 BN_BigNum *p; // prime factor p
81 BN_BigNum *q; // prime factor q
82#ifdef HITLS_CRYPTO_ACVP_TESTS
83 RSA_ACVP_TESTS acvpTests;
84#endif
85};
86
87#ifdef HITLS_CRYPTO_RSA_BSSA
88typedef enum {
89 RSABSSA = 1,
90} RSA_BlindType;
91
92typedef struct {
93 RSA_BlindType type;
94 union {
95 RSA_Blind *bssa;
96 } para;
97} RSA_BlindParam;
98#endif
99
105typedef enum {
106 EMSA_PKCSV15 = 1,
107 EMSA_PSS,
108 RSAES_OAEP,
109 RSAES_PKCSV15,
110 RSA_NO_PAD,
111 RSAES_PKCSV15_TLS, /* Specific RSA pkcs1.5 padding verification process
112 to prevent possible Bleichenbacher attacks */
113 EMSA_ISO9796_2,
114} RSA_PadType;
115
121typedef struct {
122 CRYPT_MD_AlgId mdId;
123} RSA_PkcsV15Para;
124
125typedef struct {
126 RSA_PadType type;
127 union {
128 RSA_PkcsV15Para pkcsv15;
129 RSA_PadingPara pss;
130 RSA_PadingPara iso9796_2;
131 RSA_PadingPara oaep;
132 } para;
133 CRYPT_Data salt; // Used for the KAT test.
134} RSAPad;
135
136struct RSA_Ctx {
137 CRYPT_RSA_PrvKey *prvKey;
138 CRYPT_RSA_PubKey *pubKey;
139 CRYPT_RSA_Para *para;
140#ifdef HITLS_CRYPTO_RSA_BLINDING
141 RSA_Blind *scBlind; // Preventing side channel attacks
142#endif
143 RSAPad pad;
144 uint32_t flags;
145 CRYPT_Data label; // Used for oaep padding
146 BSL_SAL_RefCount references;
147#ifdef HITLS_CRYPTO_RSA_BSSA
148 RSA_BlindParam *blindParam;
149#endif
150 void *libCtx;
151 char *mdAttr;
152};
153
154#define LIBCTX_FROM_RSA_CTX(ctx) ((ctx) == NULL ? NULL : (ctx)->libCtx)
155#define MDATTR_FROM_RSA_CTX(ctx) ((ctx) == NULL ? NULL : (ctx)->mdAttr)
156
157CRYPT_RSA_PrvKey *RSA_NewPrvKey(uint32_t bits);
158CRYPT_RSA_PubKey *RSA_NewPubKey(uint32_t bits);
159void RSA_FreePrvKey(CRYPT_RSA_PrvKey *prvKey);
160void RSA_FreePubKey(CRYPT_RSA_PubKey *pubKey);
161int32_t RSA_CalcPrvKey(const CRYPT_RSA_Para *para, CRYPT_RSA_Ctx *ctx, BN_Optimizer *optimizer);
162void ShallowCopyCtx(CRYPT_RSA_Ctx *ctx, CRYPT_RSA_Ctx *newCtx);
163CRYPT_RSA_Para *CRYPT_RSA_DupPara(const CRYPT_RSA_Para *para);
164#if defined(HITLS_CRYPTO_RSA_EMSA_PKCSV15) || defined(HITLS_CRYPTO_RSA_RECOVER)
165int32_t CRYPT_RSA_UnPackPkcsV15Type1(uint8_t *data, uint32_t dataLen, uint8_t *out, uint32_t *outLen);
166#endif
167
168#ifdef HITLS_CRYPTO_RSA_EMSA_ISO9796_2
169int32_t CRYPT_RSA_SetIso9796_2(const uint8_t *mlHash, uint32_t mlHashLen,
170 uint8_t *pad, uint32_t padLen);
171int32_t CRYPT_RSA_VerifyIso9796_2(const uint8_t *mlHash, uint32_t mlHashLen,
172 const uint8_t *pad, uint32_t padLen);
173#endif
174
175#if defined(HITLS_CRYPTO_RSA_BLINDING) || defined(HITLS_CRYPTO_RSA_BSSA)
182RSA_Blind *RSA_BlindNewCtx(void);
183
192void RSA_BlindFreeCtx(RSA_Blind *b);
204int32_t RSA_BlindCovert(RSA_Blind *b, BN_BigNum *data, BN_BigNum *n, BN_Optimizer *opt);
205
217int32_t RSA_BlindInvert(RSA_Blind *b, BN_BigNum *data, BN_BigNum *n, BN_Optimizer *opt);
231int32_t RSA_BlindCreateParam(void *libCtx, RSA_Blind *b, BN_BigNum *e, BN_BigNum *n, uint32_t bits, BN_Optimizer *opt);
232
233int32_t RSA_CreateBlind(RSA_Blind *b, uint32_t bits);
234#endif
235
236#define RSA_FREE_PRV_KEY(prvKey_) \
237do { \
238 RSA_FreePrvKey((prvKey_)); \
239 (prvKey_) = NULL; \
240 } while (0)
241
242#define RSA_FREE_PUB_KEY(pubKey_) \
243 do { \
244 RSA_FreePubKey((pubKey_)); \
245 (pubKey_) = NULL; \
246 } while (0)
247
248#define RSA_FREE_PARA(para_) \
249 do { \
250 CRYPT_RSA_FreePara((para_)); \
251 (para_) = NULL; \
252 } while (0)
253
254#ifdef __cplusplus
255}
256#endif
257
258#endif // HITLS_CRYPTO_RSA
259
260#endif
CRYPT_MD_AlgId
定义 crypt_algid.h:68
定义 crypt_types.h:38
定义 crypt_local_types.h:354