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