]>
Commit | Line | Data |
---|---|---|
ddb634fe JL |
1 | /* |
2 | * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
f9f859ad | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
ddb634fe JL |
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 | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
10 | #include "internal/cryptlib.h" | |
11 | #include <openssl/asn1t.h> | |
12 | #include <openssl/ec.h> | |
13 | #include <openssl/evp.h> | |
14 | #include "internal/evp_int.h" | |
15 | #include "internal/sm2.h" | |
16 | #include "internal/sm2err.h" | |
17 | ||
18 | /* EC pkey context structure */ | |
19 | ||
20 | typedef struct { | |
21 | /* Key and paramgen group */ | |
22 | EC_GROUP *gen_group; | |
23 | /* message digest */ | |
24 | const EVP_MD *md; | |
4803717f | 25 | /* Distinguishing Identifier, ISO/IEC 15946-3 */ |
00433bad PY |
26 | uint8_t *id; |
27 | size_t id_len; | |
4803717f PY |
28 | /* id_set indicates if the 'id' field is set (1) or not (0) */ |
29 | int id_set; | |
ddb634fe JL |
30 | } SM2_PKEY_CTX; |
31 | ||
32 | static int pkey_sm2_init(EVP_PKEY_CTX *ctx) | |
33 | { | |
4803717f | 34 | SM2_PKEY_CTX *smctx; |
ddb634fe | 35 | |
4803717f | 36 | if ((smctx = OPENSSL_zalloc(sizeof(*smctx))) == NULL) { |
ddb634fe JL |
37 | SM2err(SM2_F_PKEY_SM2_INIT, ERR_R_MALLOC_FAILURE); |
38 | return 0; | |
39 | } | |
40 | ||
4803717f | 41 | ctx->data = smctx; |
ddb634fe JL |
42 | return 1; |
43 | } | |
44 | ||
45 | static void pkey_sm2_cleanup(EVP_PKEY_CTX *ctx) | |
46 | { | |
4803717f | 47 | SM2_PKEY_CTX *smctx = ctx->data; |
ddb634fe | 48 | |
4803717f PY |
49 | if (smctx != NULL) { |
50 | EC_GROUP_free(smctx->gen_group); | |
51 | OPENSSL_free(smctx->id); | |
52 | OPENSSL_free(smctx); | |
ddb634fe JL |
53 | ctx->data = NULL; |
54 | } | |
55 | } | |
56 | ||
57 | static int pkey_sm2_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) | |
58 | { | |
59 | SM2_PKEY_CTX *dctx, *sctx; | |
60 | ||
61 | if (!pkey_sm2_init(dst)) | |
62 | return 0; | |
63 | sctx = src->data; | |
64 | dctx = dst->data; | |
65 | if (sctx->gen_group != NULL) { | |
66 | dctx->gen_group = EC_GROUP_dup(sctx->gen_group); | |
67 | if (dctx->gen_group == NULL) { | |
68 | pkey_sm2_cleanup(dst); | |
69 | return 0; | |
70 | } | |
71 | } | |
4803717f PY |
72 | if (sctx->id != NULL) { |
73 | dctx->id = OPENSSL_malloc(sctx->id_len); | |
74 | if (dctx->id == NULL) { | |
f922dac8 | 75 | SM2err(SM2_F_PKEY_SM2_COPY, ERR_R_MALLOC_FAILURE); |
4803717f PY |
76 | pkey_sm2_cleanup(dst); |
77 | return 0; | |
78 | } | |
79 | memcpy(dctx->id, sctx->id, sctx->id_len); | |
80 | } | |
81 | dctx->id_len = sctx->id_len; | |
82 | dctx->id_set = sctx->id_set; | |
ddb634fe JL |
83 | dctx->md = sctx->md; |
84 | ||
85 | return 1; | |
86 | } | |
87 | ||
88 | static int pkey_sm2_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, | |
89 | const unsigned char *tbs, size_t tbslen) | |
90 | { | |
91 | int ret; | |
92 | unsigned int sltmp; | |
93 | EC_KEY *ec = ctx->pkey->pkey.ec; | |
94 | const int sig_sz = ECDSA_size(ctx->pkey->pkey.ec); | |
95 | ||
96 | if (sig_sz <= 0) { | |
97 | return 0; | |
98 | } | |
99 | ||
100 | if (sig == NULL) { | |
101 | *siglen = (size_t)sig_sz; | |
102 | return 1; | |
103 | } | |
104 | ||
105 | if (*siglen < (size_t)sig_sz) { | |
106 | SM2err(SM2_F_PKEY_SM2_SIGN, SM2_R_BUFFER_TOO_SMALL); | |
107 | return 0; | |
108 | } | |
109 | ||
110 | ret = sm2_sign(tbs, tbslen, sig, &sltmp, ec); | |
111 | ||
112 | if (ret <= 0) | |
113 | return ret; | |
114 | *siglen = (size_t)sltmp; | |
115 | return 1; | |
116 | } | |
117 | ||
118 | static int pkey_sm2_verify(EVP_PKEY_CTX *ctx, | |
119 | const unsigned char *sig, size_t siglen, | |
120 | const unsigned char *tbs, size_t tbslen) | |
121 | { | |
122 | EC_KEY *ec = ctx->pkey->pkey.ec; | |
123 | ||
124 | return sm2_verify(tbs, tbslen, sig, siglen, ec); | |
125 | } | |
126 | ||
127 | static int pkey_sm2_encrypt(EVP_PKEY_CTX *ctx, | |
128 | unsigned char *out, size_t *outlen, | |
129 | const unsigned char *in, size_t inlen) | |
130 | { | |
131 | EC_KEY *ec = ctx->pkey->pkey.ec; | |
132 | SM2_PKEY_CTX *dctx = ctx->data; | |
133 | const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md; | |
134 | ||
135 | if (out == NULL) { | |
136 | if (!sm2_ciphertext_size(ec, md, inlen, outlen)) | |
137 | return -1; | |
138 | else | |
139 | return 1; | |
140 | } | |
141 | ||
142 | return sm2_encrypt(ec, md, in, inlen, out, outlen); | |
143 | } | |
144 | ||
145 | static int pkey_sm2_decrypt(EVP_PKEY_CTX *ctx, | |
146 | unsigned char *out, size_t *outlen, | |
147 | const unsigned char *in, size_t inlen) | |
148 | { | |
149 | EC_KEY *ec = ctx->pkey->pkey.ec; | |
150 | SM2_PKEY_CTX *dctx = ctx->data; | |
151 | const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md; | |
152 | ||
153 | if (out == NULL) { | |
154 | if (!sm2_plaintext_size(ec, md, inlen, outlen)) | |
155 | return -1; | |
156 | else | |
157 | return 1; | |
158 | } | |
159 | ||
160 | return sm2_decrypt(ec, md, in, inlen, out, outlen); | |
161 | } | |
162 | ||
163 | static int pkey_sm2_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) | |
164 | { | |
4803717f | 165 | SM2_PKEY_CTX *smctx = ctx->data; |
ddb634fe | 166 | EC_GROUP *group; |
675f4cee | 167 | uint8_t *tmp_id; |
ddb634fe JL |
168 | |
169 | switch (type) { | |
170 | case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID: | |
171 | group = EC_GROUP_new_by_curve_name(p1); | |
172 | if (group == NULL) { | |
173 | SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_INVALID_CURVE); | |
174 | return 0; | |
175 | } | |
4803717f PY |
176 | EC_GROUP_free(smctx->gen_group); |
177 | smctx->gen_group = group; | |
ddb634fe JL |
178 | return 1; |
179 | ||
180 | case EVP_PKEY_CTRL_EC_PARAM_ENC: | |
4803717f | 181 | if (smctx->gen_group == NULL) { |
ddb634fe JL |
182 | SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_NO_PARAMETERS_SET); |
183 | return 0; | |
184 | } | |
4803717f | 185 | EC_GROUP_set_asn1_flag(smctx->gen_group, p1); |
ddb634fe JL |
186 | return 1; |
187 | ||
188 | case EVP_PKEY_CTRL_MD: | |
4803717f | 189 | smctx->md = p2; |
ddb634fe JL |
190 | return 1; |
191 | ||
192 | case EVP_PKEY_CTRL_GET_MD: | |
4803717f PY |
193 | *(const EVP_MD **)p2 = smctx->md; |
194 | return 1; | |
195 | ||
196 | case EVP_PKEY_CTRL_SET1_ID: | |
4803717f | 197 | if (p1 > 0) { |
675f4cee | 198 | tmp_id = OPENSSL_malloc(p1); |
f922dac8 PY |
199 | if (tmp_id == NULL) { |
200 | SM2err(SM2_F_PKEY_SM2_CTRL, ERR_R_MALLOC_FAILURE); | |
4803717f | 201 | return 0; |
f922dac8 | 202 | } |
675f4cee PY |
203 | memcpy(tmp_id, p2, p1); |
204 | OPENSSL_free(smctx->id); | |
205 | smctx->id = tmp_id; | |
4803717f PY |
206 | } else { |
207 | /* set null-ID */ | |
675f4cee | 208 | OPENSSL_free(smctx->id); |
4803717f PY |
209 | smctx->id = NULL; |
210 | } | |
211 | smctx->id_len = (size_t)p1; | |
212 | smctx->id_set = 1; | |
213 | return 1; | |
214 | ||
215 | case EVP_PKEY_CTRL_GET1_ID: | |
216 | memcpy(p2, smctx->id, smctx->id_len); | |
217 | return 1; | |
218 | ||
219 | case EVP_PKEY_CTRL_GET1_ID_LEN: | |
220 | *(size_t *)p2 = smctx->id_len; | |
ddb634fe JL |
221 | return 1; |
222 | ||
223 | default: | |
224 | return -2; | |
ddb634fe JL |
225 | } |
226 | } | |
227 | ||
228 | static int pkey_sm2_ctrl_str(EVP_PKEY_CTX *ctx, | |
229 | const char *type, const char *value) | |
230 | { | |
231 | if (strcmp(type, "ec_paramgen_curve") == 0) { | |
232 | int nid = NID_undef; | |
233 | ||
234 | if (((nid = EC_curve_nist2nid(value)) == NID_undef) | |
235 | && ((nid = OBJ_sn2nid(value)) == NID_undef) | |
236 | && ((nid = OBJ_ln2nid(value)) == NID_undef)) { | |
237 | SM2err(SM2_F_PKEY_SM2_CTRL_STR, SM2_R_INVALID_CURVE); | |
238 | return 0; | |
239 | } | |
240 | return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid); | |
241 | } else if (strcmp(type, "ec_param_enc") == 0) { | |
242 | int param_enc; | |
243 | ||
244 | if (strcmp(value, "explicit") == 0) | |
245 | param_enc = 0; | |
246 | else if (strcmp(value, "named_curve") == 0) | |
247 | param_enc = OPENSSL_EC_NAMED_CURVE; | |
248 | else | |
249 | return -2; | |
250 | return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc); | |
a7cef52f PY |
251 | } else if (strcmp(type, "sm2_id") == 0) { |
252 | return pkey_sm2_ctrl(ctx, EVP_PKEY_CTRL_SET1_ID, | |
253 | (int)strlen(value), (void *)value); | |
ddb634fe JL |
254 | } |
255 | ||
256 | return -2; | |
257 | } | |
258 | ||
00433bad PY |
259 | static int pkey_sm2_digest_custom(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx) |
260 | { | |
261 | uint8_t z[EVP_MAX_MD_SIZE]; | |
4803717f | 262 | SM2_PKEY_CTX *smctx = ctx->data; |
00433bad PY |
263 | EC_KEY *ec = ctx->pkey->pkey.ec; |
264 | const EVP_MD *md = EVP_MD_CTX_md(mctx); | |
d689f313 | 265 | int mdlen = EVP_MD_size(md); |
00433bad | 266 | |
4803717f PY |
267 | if (!smctx->id_set) { |
268 | /* | |
269 | * An ID value must be set. The specifications are not clear whether a | |
270 | * NULL is allowed. We only allow it if set explicitly for maximum | |
271 | * flexibility. | |
00433bad | 272 | */ |
675f4cee | 273 | SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM, SM2_R_ID_NOT_SET); |
00433bad PY |
274 | return 0; |
275 | } | |
276 | ||
d689f313 MC |
277 | if (mdlen < 0) { |
278 | SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM, SM2_R_INVALID_DIGEST); | |
279 | return 0; | |
280 | } | |
281 | ||
4803717f PY |
282 | /* get hashed prefix 'z' of tbs message */ |
283 | if (!sm2_compute_z_digest(z, md, smctx->id, smctx->id_len, ec)) | |
00433bad PY |
284 | return 0; |
285 | ||
d689f313 | 286 | return EVP_DigestUpdate(mctx, z, (size_t)mdlen); |
00433bad PY |
287 | } |
288 | ||
ddb634fe JL |
289 | const EVP_PKEY_METHOD sm2_pkey_meth = { |
290 | EVP_PKEY_SM2, | |
291 | 0, | |
292 | pkey_sm2_init, | |
293 | pkey_sm2_copy, | |
294 | pkey_sm2_cleanup, | |
295 | ||
296 | 0, | |
297 | 0, | |
298 | ||
299 | 0, | |
300 | 0, | |
301 | ||
302 | 0, | |
303 | pkey_sm2_sign, | |
304 | ||
305 | 0, | |
306 | pkey_sm2_verify, | |
307 | ||
308 | 0, 0, | |
309 | ||
310 | 0, 0, 0, 0, | |
311 | ||
312 | 0, | |
313 | pkey_sm2_encrypt, | |
314 | ||
315 | 0, | |
316 | pkey_sm2_decrypt, | |
317 | ||
318 | 0, | |
319 | 0, | |
320 | pkey_sm2_ctrl, | |
00433bad PY |
321 | pkey_sm2_ctrl_str, |
322 | ||
323 | 0, 0, | |
324 | ||
325 | 0, 0, 0, | |
326 | ||
327 | pkey_sm2_digest_custom | |
ddb634fe | 328 | }; |