]>
Commit | Line | Data |
---|---|---|
ddb634fe JL |
1 | /* |
2 | * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
4 | * Licensed under the OpenSSL license (the "License"). You may not use | |
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) { | |
75 | pkey_sm2_cleanup(dst); | |
76 | return 0; | |
77 | } | |
78 | memcpy(dctx->id, sctx->id, sctx->id_len); | |
79 | } | |
80 | dctx->id_len = sctx->id_len; | |
81 | dctx->id_set = sctx->id_set; | |
ddb634fe JL |
82 | dctx->md = sctx->md; |
83 | ||
84 | return 1; | |
85 | } | |
86 | ||
87 | static int pkey_sm2_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, | |
88 | const unsigned char *tbs, size_t tbslen) | |
89 | { | |
90 | int ret; | |
91 | unsigned int sltmp; | |
92 | EC_KEY *ec = ctx->pkey->pkey.ec; | |
93 | const int sig_sz = ECDSA_size(ctx->pkey->pkey.ec); | |
94 | ||
95 | if (sig_sz <= 0) { | |
96 | return 0; | |
97 | } | |
98 | ||
99 | if (sig == NULL) { | |
100 | *siglen = (size_t)sig_sz; | |
101 | return 1; | |
102 | } | |
103 | ||
104 | if (*siglen < (size_t)sig_sz) { | |
105 | SM2err(SM2_F_PKEY_SM2_SIGN, SM2_R_BUFFER_TOO_SMALL); | |
106 | return 0; | |
107 | } | |
108 | ||
109 | ret = sm2_sign(tbs, tbslen, sig, &sltmp, ec); | |
110 | ||
111 | if (ret <= 0) | |
112 | return ret; | |
113 | *siglen = (size_t)sltmp; | |
114 | return 1; | |
115 | } | |
116 | ||
117 | static int pkey_sm2_verify(EVP_PKEY_CTX *ctx, | |
118 | const unsigned char *sig, size_t siglen, | |
119 | const unsigned char *tbs, size_t tbslen) | |
120 | { | |
121 | EC_KEY *ec = ctx->pkey->pkey.ec; | |
122 | ||
123 | return sm2_verify(tbs, tbslen, sig, siglen, ec); | |
124 | } | |
125 | ||
126 | static int pkey_sm2_encrypt(EVP_PKEY_CTX *ctx, | |
127 | unsigned char *out, size_t *outlen, | |
128 | const unsigned char *in, size_t inlen) | |
129 | { | |
130 | EC_KEY *ec = ctx->pkey->pkey.ec; | |
131 | SM2_PKEY_CTX *dctx = ctx->data; | |
132 | const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md; | |
133 | ||
134 | if (out == NULL) { | |
135 | if (!sm2_ciphertext_size(ec, md, inlen, outlen)) | |
136 | return -1; | |
137 | else | |
138 | return 1; | |
139 | } | |
140 | ||
141 | return sm2_encrypt(ec, md, in, inlen, out, outlen); | |
142 | } | |
143 | ||
144 | static int pkey_sm2_decrypt(EVP_PKEY_CTX *ctx, | |
145 | unsigned char *out, size_t *outlen, | |
146 | const unsigned char *in, size_t inlen) | |
147 | { | |
148 | EC_KEY *ec = ctx->pkey->pkey.ec; | |
149 | SM2_PKEY_CTX *dctx = ctx->data; | |
150 | const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md; | |
151 | ||
152 | if (out == NULL) { | |
153 | if (!sm2_plaintext_size(ec, md, inlen, outlen)) | |
154 | return -1; | |
155 | else | |
156 | return 1; | |
157 | } | |
158 | ||
159 | return sm2_decrypt(ec, md, in, inlen, out, outlen); | |
160 | } | |
161 | ||
162 | static int pkey_sm2_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) | |
163 | { | |
4803717f | 164 | SM2_PKEY_CTX *smctx = ctx->data; |
ddb634fe | 165 | EC_GROUP *group; |
675f4cee | 166 | uint8_t *tmp_id; |
ddb634fe JL |
167 | |
168 | switch (type) { | |
169 | case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID: | |
170 | group = EC_GROUP_new_by_curve_name(p1); | |
171 | if (group == NULL) { | |
172 | SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_INVALID_CURVE); | |
173 | return 0; | |
174 | } | |
4803717f PY |
175 | EC_GROUP_free(smctx->gen_group); |
176 | smctx->gen_group = group; | |
ddb634fe JL |
177 | return 1; |
178 | ||
179 | case EVP_PKEY_CTRL_EC_PARAM_ENC: | |
4803717f | 180 | if (smctx->gen_group == NULL) { |
ddb634fe JL |
181 | SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_NO_PARAMETERS_SET); |
182 | return 0; | |
183 | } | |
4803717f | 184 | EC_GROUP_set_asn1_flag(smctx->gen_group, p1); |
ddb634fe JL |
185 | return 1; |
186 | ||
187 | case EVP_PKEY_CTRL_MD: | |
4803717f | 188 | smctx->md = p2; |
ddb634fe JL |
189 | return 1; |
190 | ||
191 | case EVP_PKEY_CTRL_GET_MD: | |
4803717f PY |
192 | *(const EVP_MD **)p2 = smctx->md; |
193 | return 1; | |
194 | ||
195 | case EVP_PKEY_CTRL_SET1_ID: | |
4803717f | 196 | if (p1 > 0) { |
675f4cee PY |
197 | tmp_id = OPENSSL_malloc(p1); |
198 | if (tmp_id == NULL) | |
4803717f | 199 | return 0; |
675f4cee PY |
200 | memcpy(tmp_id, p2, p1); |
201 | OPENSSL_free(smctx->id); | |
202 | smctx->id = tmp_id; | |
4803717f PY |
203 | } else { |
204 | /* set null-ID */ | |
675f4cee | 205 | OPENSSL_free(smctx->id); |
4803717f PY |
206 | smctx->id = NULL; |
207 | } | |
208 | smctx->id_len = (size_t)p1; | |
209 | smctx->id_set = 1; | |
210 | return 1; | |
211 | ||
212 | case EVP_PKEY_CTRL_GET1_ID: | |
213 | memcpy(p2, smctx->id, smctx->id_len); | |
214 | return 1; | |
215 | ||
216 | case EVP_PKEY_CTRL_GET1_ID_LEN: | |
217 | *(size_t *)p2 = smctx->id_len; | |
ddb634fe JL |
218 | return 1; |
219 | ||
220 | default: | |
221 | return -2; | |
ddb634fe JL |
222 | } |
223 | } | |
224 | ||
225 | static int pkey_sm2_ctrl_str(EVP_PKEY_CTX *ctx, | |
226 | const char *type, const char *value) | |
227 | { | |
228 | if (strcmp(type, "ec_paramgen_curve") == 0) { | |
229 | int nid = NID_undef; | |
230 | ||
231 | if (((nid = EC_curve_nist2nid(value)) == NID_undef) | |
232 | && ((nid = OBJ_sn2nid(value)) == NID_undef) | |
233 | && ((nid = OBJ_ln2nid(value)) == NID_undef)) { | |
234 | SM2err(SM2_F_PKEY_SM2_CTRL_STR, SM2_R_INVALID_CURVE); | |
235 | return 0; | |
236 | } | |
237 | return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid); | |
238 | } else if (strcmp(type, "ec_param_enc") == 0) { | |
239 | int param_enc; | |
240 | ||
241 | if (strcmp(value, "explicit") == 0) | |
242 | param_enc = 0; | |
243 | else if (strcmp(value, "named_curve") == 0) | |
244 | param_enc = OPENSSL_EC_NAMED_CURVE; | |
245 | else | |
246 | return -2; | |
247 | return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc); | |
248 | } | |
249 | ||
250 | return -2; | |
251 | } | |
252 | ||
00433bad PY |
253 | static int pkey_sm2_digest_custom(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx) |
254 | { | |
255 | uint8_t z[EVP_MAX_MD_SIZE]; | |
4803717f | 256 | SM2_PKEY_CTX *smctx = ctx->data; |
00433bad PY |
257 | EC_KEY *ec = ctx->pkey->pkey.ec; |
258 | const EVP_MD *md = EVP_MD_CTX_md(mctx); | |
259 | ||
4803717f PY |
260 | if (!smctx->id_set) { |
261 | /* | |
262 | * An ID value must be set. The specifications are not clear whether a | |
263 | * NULL is allowed. We only allow it if set explicitly for maximum | |
264 | * flexibility. | |
00433bad | 265 | */ |
675f4cee | 266 | SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM, SM2_R_ID_NOT_SET); |
00433bad PY |
267 | return 0; |
268 | } | |
269 | ||
4803717f PY |
270 | /* get hashed prefix 'z' of tbs message */ |
271 | if (!sm2_compute_z_digest(z, md, smctx->id, smctx->id_len, ec)) | |
00433bad PY |
272 | return 0; |
273 | ||
274 | return EVP_DigestUpdate(mctx, z, EVP_MD_size(md)); | |
275 | } | |
276 | ||
ddb634fe JL |
277 | const EVP_PKEY_METHOD sm2_pkey_meth = { |
278 | EVP_PKEY_SM2, | |
279 | 0, | |
280 | pkey_sm2_init, | |
281 | pkey_sm2_copy, | |
282 | pkey_sm2_cleanup, | |
283 | ||
284 | 0, | |
285 | 0, | |
286 | ||
287 | 0, | |
288 | 0, | |
289 | ||
290 | 0, | |
291 | pkey_sm2_sign, | |
292 | ||
293 | 0, | |
294 | pkey_sm2_verify, | |
295 | ||
296 | 0, 0, | |
297 | ||
298 | 0, 0, 0, 0, | |
299 | ||
300 | 0, | |
301 | pkey_sm2_encrypt, | |
302 | ||
303 | 0, | |
304 | pkey_sm2_decrypt, | |
305 | ||
306 | 0, | |
307 | 0, | |
308 | pkey_sm2_ctrl, | |
00433bad PY |
309 | pkey_sm2_ctrl_str, |
310 | ||
311 | 0, 0, | |
312 | ||
313 | 0, 0, 0, | |
314 | ||
315 | pkey_sm2_digest_custom | |
ddb634fe | 316 | }; |