forked from guanzhi/GmSSL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdf_sansec.c
More file actions
executable file
·266 lines (233 loc) · 6.03 KB
/
Copy pathsdf_sansec.c
File metadata and controls
executable file
·266 lines (233 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <string.h>
#include "sdf.h"
#include "sdf_int.h"
#include "sdf_sansec.h"
#define SDFerr(a,b)
typedef struct {
unsigned int std_id;
unsigned int vendor_id;
} SDF_ALGOR_PAIR;
static SDF_ALGOR_PAIR sansec_ciphers[] = {
{ SGD_SM1, SANSEC_SM1 },
{ SGD_SM1_ECB, SANSEC_SM1_ECB },
{ SGD_SM1_CBC, SANSEC_SM1_CBC },
{ SGD_SM1_CFB, SANSEC_SM1_CFB },
{ SGD_SM1_OFB, SANSEC_SM1_OFB },
{ SGD_SM1_MAC, SANSEC_SM1_MAC },
{ SGD_SM4, SANSEC_SM4 },
{ SGD_SM4_ECB, SANSEC_SM4_ECB },
{ SGD_SM4_CBC, SANSEC_SM4_CBC },
{ SGD_SM4_CFB, SANSEC_SM4_CFB },
{ SGD_SM4_OFB, SANSEC_SM4_OFB },
{ SGD_SM4_MAC, SANSEC_SM4_MAC },
{ SGD_SSF33, SANSEC_SSF33 },
{ SGD_SSF33_ECB, SANSEC_SSF33_ECB },
{ SGD_SSF33_CBC, SANSEC_SSF33_CBC },
{ SGD_SSF33_CFB, SANSEC_SSF33_CFB },
{ SGD_SSF33_OFB, SANSEC_SSF33_OFB },
{ SGD_SSF33_MAC, SANSEC_SSF33_MAC },
{ 0, SANSEC_AES },
{ 0, SANSEC_AES_ECB },
{ 0, SANSEC_AES_CBC },
{ 0, SANSEC_AES_CFB },
{ 0, SANSEC_AES_OFB },
{ 0, SANSEC_AES_MAC },
{ 0, SANSEC_DES },
{ 0, SANSEC_DES_ECB },
{ 0, SANSEC_DES_CBC },
{ 0, SANSEC_DES_CFB },
{ 0, SANSEC_DES_OFB },
{ 0, SANSEC_DES_MAC },
{ 0, SANSEC_3DES },
{ 0, SANSEC_3DES_ECB },
{ 0, SANSEC_3DES_CBC },
{ 0, SANSEC_3DES_CFB },
{ 0, SANSEC_3DES_OFB },
{ 0, SANSEC_3DES_MAC },
};
static unsigned int sansec_cipher_vendor2std(unsigned int vendor_id)
{
size_t i;
for (i = 0; i < sizeof(sansec_ciphers)/sizeof(sansec_ciphers[0]); i++) {
if (vendor_id == sansec_ciphers[i].vendor_id) {
return sansec_ciphers[i].std_id;
}
}
return 0;
}
static unsigned int sansec_cipher_std2vendor(unsigned int std_id)
{
size_t i;
for (i = 0; i < sizeof(sansec_ciphers)/sizeof(sansec_ciphers[0]); i++) {
if (std_id == sansec_ciphers[i].std_id) {
return sansec_ciphers[i].vendor_id;
}
}
return 0;
}
static unsigned int sansec_cipher_cap(unsigned int vendor_cap)
{
unsigned int std_cap = 0;
size_t i;
for (i = 0; i < sizeof(sansec_ciphers)/sizeof(sansec_ciphers[0]); i++) {
if (vendor_cap & sansec_ciphers[i].vendor_id) {
std_cap |= sansec_ciphers[i].std_id;
}
}
return std_cap;
}
static SDF_ALGOR_PAIR sansec_digests[] = {
{ SGD_SM3, SANSEC_SM3 },
{ SGD_SHA1, SANSEC_SHA1 },
{ SGD_SHA256, SANSEC_SHA256 },
{ 0, SANSEC_SHA512 },
{ 0, SANSEC_SHA384 },
{ 0, SANSEC_SHA224 },
{ 0, SANSEC_MD5 },
};
static unsigned int sansec_digest_vendor2std(unsigned int vendor_id)
{
size_t i;
for (i = 0; i < sizeof(sansec_digests)/sizeof(sansec_digests[0]); i++) {
if (vendor_id == sansec_digests[i].vendor_id) {
return sansec_digests[i].std_id;
}
}
return 0;
}
static unsigned int sansec_digest_std2vendor(unsigned int std_id)
{
size_t i;
for (i = 0; i < sizeof(sansec_digests)/sizeof(sansec_digests[0]); i++) {
if (std_id == sansec_digests[i].std_id) {
return sansec_digests[i].vendor_id;
}
}
return 0;
}
static unsigned int sansec_digest_cap(unsigned int vendor_cap)
{
unsigned int std_cap = 0;
size_t i;
for (i = 0; i < sizeof(sansec_digests)/sizeof(sansec_digests[0]); i++) {
if (vendor_cap & sansec_digests[i].vendor_id) {
std_cap |= sansec_digests[i].std_id;
}
}
return std_cap;
}
static SDF_ALGOR_PAIR sansec_pkeys[] = {
{ SGD_RSA,SANSEC_RSA },
{ SGD_RSA_SIGN,SANSEC_RSA_SIGN },
{ SGD_RSA_ENC,SANSEC_RSA_ENC },
{ SGD_SM2,SANSEC_SM2 },
{ SGD_SM2_1,SANSEC_SM2_1 },
{ SGD_SM2_2,SANSEC_SM2_2 },
{ SGD_SM2_3,SANSEC_SM2_3 },
};
static unsigned int sansec_pkey_vendor2std(unsigned int vendor_id)
{
size_t i;
for (i = 0; i < sizeof(sansec_pkeys)/sizeof(sansec_pkeys[0]); i++) {
if (vendor_id == sansec_pkeys[i].vendor_id) {
return sansec_pkeys[i].std_id;
}
}
return 0;
}
static unsigned int sansec_pkey_std2vendor(unsigned int std_id)
{
size_t i;
for (i = 0; i < sizeof(sansec_pkeys)/sizeof(sansec_pkeys[0]); i++) {
if (std_id == sansec_pkeys[i].std_id) {
return sansec_pkeys[i].vendor_id;
}
}
return 0;
}
static unsigned int sansec_pkey_cap(unsigned int vendor_cap)
{
unsigned int std_cap = 0;
size_t i;
for (i = 0; i < sizeof(sansec_pkeys)/sizeof(sansec_pkeys[0]); i++) {
if (vendor_cap & sansec_pkeys[i].vendor_id) {
std_cap |= sansec_pkeys[i].std_id;
}
}
return std_cap;
}
static int sansec_encode_ecccipher(const ECCCipher *ec, void *vendor)
{
int ret;
SANSEC_ECCCipher *sansec = vendor;
ret = sizeof(SANSEC_ECCCipher);
if (ec->L > sizeof(sansec->C)) {
SDFerr(SDF_F_SANSEC_ENCODE_ECCCIPHER,
SDF_R_INVALID_SANSEC_ECCCIPHER_LENGTH);
return 0;
}
if (vendor) {
sansec->clength = ec->L;
memcpy(sansec->x, ec->x, sizeof(ec->x));
memcpy(sansec->y, ec->y, sizeof(ec->y));
memcpy(sansec->M, ec->M, sizeof(ec->M));
memset(sansec->M + sizeof(ec->M), 0, sizeof(sansec->M) - sizeof(ec->M));
memcpy(sansec->C, ec->C, ec->L);
memset(sansec->C + ec->L, 0, sizeof(sansec->C) - ec->L);
}
return ret;
}
static int sansec_decode_ecccipher(ECCCipher *ec, const void *vendor)
{
int ret;
const SANSEC_ECCCipher *sansec = vendor;
ret = sizeof(ECCCipher) -1 + sansec->clength;
if (sansec->clength > sizeof(sansec->C)) {
SDFerr(SDF_F_SANSEC_DECODE_ECCCIPHER,
SDF_R_INVALID_SANSEC_ECCCIPHER_LENGTH);
return 0;
}
if (ec) {
memcpy(ec->x, sansec->x, sizeof(ec->x));
memcpy(ec->y, sansec->y, sizeof(ec->y));
memcpy(ec->M, sansec->M, sizeof(ec->M));
ec->L = sansec->clength;
memcpy(ec->C, sansec->C, sansec->clength);
}
return ret;
}
static unsigned long sansec_get_error_reason(int err)
{
/*
size_t i = 0;
for (i = 0; i < OSSL_NELEM(sansec_errors); i++) {
if (err == sansec_errors[i].err) {
return sansec_errors[i].reason;
}
}
*/
return 0;
}
SDF_VENDOR sdf_sansec = {
"sansec",
sansec_cipher_vendor2std,
sansec_cipher_std2vendor,
sansec_cipher_cap,
sansec_digest_vendor2std,
sansec_digest_std2vendor,
sansec_digest_cap,
sansec_pkey_vendor2std,
sansec_pkey_std2vendor,
sansec_pkey_cap,
sansec_encode_ecccipher,
sansec_decode_ecccipher,
sansec_get_error_reason,
};