Deprecate the ECDSA and EV_KEY_METHOD functions.
[openssl.git] / crypto / sm2 / sm2_pmeth.c
1 /*
2  * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 /*
11  * ECDSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
16 #include "internal/cryptlib.h"
17 #include <openssl/asn1t.h>
18 #include <openssl/ec.h>
19 #include <openssl/evp.h>
20 #include "crypto/evp.h"
21 #include "crypto/sm2.h"
22 #include "crypto/sm2err.h"
23
24 /* EC pkey context structure */
25
26 typedef struct {
27     /* message digest */
28     const EVP_MD *md;
29     /* Distinguishing Identifier, ISO/IEC 15946-3 */
30     uint8_t *id;
31     size_t id_len;
32     /* id_set indicates if the 'id' field is set (1) or not (0) */
33     int id_set;
34 } SM2_PKEY_CTX;
35
36 static int pkey_sm2_init(EVP_PKEY_CTX *ctx)
37 {
38     SM2_PKEY_CTX *smctx;
39
40     if ((smctx = OPENSSL_zalloc(sizeof(*smctx))) == NULL) {
41         SM2err(SM2_F_PKEY_SM2_INIT, ERR_R_MALLOC_FAILURE);
42         return 0;
43     }
44
45     ctx->data = smctx;
46     return 1;
47 }
48
49 static void pkey_sm2_cleanup(EVP_PKEY_CTX *ctx)
50 {
51     SM2_PKEY_CTX *smctx = ctx->data;
52
53     if (smctx != NULL) {
54         OPENSSL_free(smctx->id);
55         OPENSSL_free(smctx);
56         ctx->data = NULL;
57     }
58 }
59
60 static int pkey_sm2_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
61 {
62     SM2_PKEY_CTX *dctx, *sctx;
63
64     if (!pkey_sm2_init(dst))
65         return 0;
66     sctx = src->data;
67     dctx = dst->data;
68     if (sctx->id != NULL) {
69         dctx->id = OPENSSL_malloc(sctx->id_len);
70         if (dctx->id == NULL) {
71             SM2err(SM2_F_PKEY_SM2_COPY, ERR_R_MALLOC_FAILURE);
72             pkey_sm2_cleanup(dst);
73             return 0;
74         }
75         memcpy(dctx->id, sctx->id, sctx->id_len);
76     }
77     dctx->id_len = sctx->id_len;
78     dctx->id_set = sctx->id_set;
79     dctx->md = sctx->md;
80
81     return 1;
82 }
83
84 static int pkey_sm2_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
85                          const unsigned char *tbs, size_t tbslen)
86 {
87     int ret;
88     unsigned int sltmp;
89     EC_KEY *ec = ctx->pkey->pkey.ec;
90     const int sig_sz = ECDSA_size(ctx->pkey->pkey.ec);
91
92     if (sig_sz <= 0) {
93         return 0;
94     }
95
96     if (sig == NULL) {
97         *siglen = (size_t)sig_sz;
98         return 1;
99     }
100
101     if (*siglen < (size_t)sig_sz) {
102         SM2err(SM2_F_PKEY_SM2_SIGN, SM2_R_BUFFER_TOO_SMALL);
103         return 0;
104     }
105
106     ret = sm2_sign(tbs, tbslen, sig, &sltmp, ec);
107
108     if (ret <= 0)
109         return ret;
110     *siglen = (size_t)sltmp;
111     return 1;
112 }
113
114 static int pkey_sm2_verify(EVP_PKEY_CTX *ctx,
115                            const unsigned char *sig, size_t siglen,
116                            const unsigned char *tbs, size_t tbslen)
117 {
118     EC_KEY *ec = ctx->pkey->pkey.ec;
119
120     return sm2_verify(tbs, tbslen, sig, siglen, ec);
121 }
122
123 static int pkey_sm2_encrypt(EVP_PKEY_CTX *ctx,
124                             unsigned char *out, size_t *outlen,
125                             const unsigned char *in, size_t inlen)
126 {
127     EC_KEY *ec = ctx->pkey->pkey.ec;
128     SM2_PKEY_CTX *dctx = ctx->data;
129     const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
130
131     if (out == NULL) {
132         if (!sm2_ciphertext_size(ec, md, inlen, outlen))
133             return -1;
134         else
135             return 1;
136     }
137
138     return sm2_encrypt(ec, md, in, inlen, out, outlen);
139 }
140
141 static int pkey_sm2_decrypt(EVP_PKEY_CTX *ctx,
142                             unsigned char *out, size_t *outlen,
143                             const unsigned char *in, size_t inlen)
144 {
145     EC_KEY *ec = ctx->pkey->pkey.ec;
146     SM2_PKEY_CTX *dctx = ctx->data;
147     const EVP_MD *md = (dctx->md == NULL) ? EVP_sm3() : dctx->md;
148
149     if (out == NULL) {
150         if (!sm2_plaintext_size(ec, md, inlen, outlen))
151             return -1;
152         else
153             return 1;
154     }
155
156     return sm2_decrypt(ec, md, in, inlen, out, outlen);
157 }
158
159 static int pkey_sm2_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
160 {
161     SM2_PKEY_CTX *smctx = ctx->data;
162     uint8_t *tmp_id;
163
164     switch (type) {
165     case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
166         /*
167          * This control could be removed, which would signal it being
168          * unsupported.  However, that means that when the caller uses
169          * the correct curve, it may interpret the unsupported signal
170          * as an error, so it's better to accept the control, check the
171          * value and return a corresponding value.
172          */
173         if (p1 != NID_sm2) {
174             SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_INVALID_CURVE);
175             return 0;
176         }
177         return 1;
178
179     case EVP_PKEY_CTRL_MD:
180         smctx->md = p2;
181         return 1;
182
183     case EVP_PKEY_CTRL_GET_MD:
184         *(const EVP_MD **)p2 = smctx->md;
185         return 1;
186
187     case EVP_PKEY_CTRL_SET1_ID:
188         if (p1 > 0) {
189             tmp_id = OPENSSL_malloc(p1);
190             if (tmp_id == NULL) {
191                 SM2err(SM2_F_PKEY_SM2_CTRL, ERR_R_MALLOC_FAILURE);
192                 return 0;
193             }
194             memcpy(tmp_id, p2, p1);
195             OPENSSL_free(smctx->id);
196             smctx->id = tmp_id;
197         } else {
198             /* set null-ID */
199             OPENSSL_free(smctx->id);
200             smctx->id = NULL;
201         }
202         smctx->id_len = (size_t)p1;
203         smctx->id_set = 1;
204         return 1;
205
206     case EVP_PKEY_CTRL_GET1_ID:
207         memcpy(p2, smctx->id, smctx->id_len);
208         return 1;
209
210     case EVP_PKEY_CTRL_GET1_ID_LEN:
211         *(size_t *)p2 = smctx->id_len;
212         return 1;
213
214     case EVP_PKEY_CTRL_DIGESTINIT:
215         /* nothing to be inited, this is to suppress the error... */
216         return 1;
217
218     default:
219         return -2;
220     }
221 }
222
223 static int pkey_sm2_ctrl_str(EVP_PKEY_CTX *ctx,
224                              const char *type, const char *value)
225 {
226     uint8_t *hex_id;
227     long hex_len = 0;
228     int ret = 0;
229
230     if (strcmp(type, "ec_paramgen_curve") == 0) {
231         int nid = NID_undef;
232
233         if (((nid = EC_curve_nist2nid(value)) == NID_undef)
234             && ((nid = OBJ_sn2nid(value)) == NID_undef)
235             && ((nid = OBJ_ln2nid(value)) == NID_undef)) {
236             SM2err(SM2_F_PKEY_SM2_CTRL_STR, SM2_R_INVALID_CURVE);
237             return 0;
238         }
239         return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, nid);
240     } else if (strcmp(type, "ec_param_enc") == 0) {
241         int param_enc;
242
243         if (strcmp(value, "explicit") == 0)
244             param_enc = 0;
245         else if (strcmp(value, "named_curve") == 0)
246             param_enc = OPENSSL_EC_NAMED_CURVE;
247         else
248             return -2;
249         return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
250     } else if (strcmp(type, "sm2_id") == 0) {
251         return pkey_sm2_ctrl(ctx, EVP_PKEY_CTRL_SET1_ID,
252                              (int)strlen(value), (void *)value);
253     } else if (strcmp(type, "sm2_hex_id") == 0) {
254         /*
255          * TODO(3.0): reconsider the name "sm2_hex_id", OR change
256          * OSSL_PARAM_construct_from_text() / OSSL_PARAM_allocate_from_text()
257          * to handle infix "_hex_"
258          */
259         hex_id = OPENSSL_hexstr2buf((const char *)value, &hex_len);
260         if (hex_id == NULL) {
261             SM2err(SM2_F_PKEY_SM2_CTRL_STR, ERR_R_PASSED_INVALID_ARGUMENT);
262             return 0;
263         }
264         ret = pkey_sm2_ctrl(ctx, EVP_PKEY_CTRL_SET1_ID, (int)hex_len,
265                             (void *)hex_id);
266         OPENSSL_free(hex_id);
267         return ret;
268     }
269
270     return -2;
271 }
272
273 static int pkey_sm2_digest_custom(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
274 {
275     uint8_t z[EVP_MAX_MD_SIZE];
276     SM2_PKEY_CTX *smctx = ctx->data;
277     EC_KEY *ec = ctx->pkey->pkey.ec;
278     const EVP_MD *md = EVP_MD_CTX_md(mctx);
279     int mdlen = EVP_MD_size(md);
280
281     if (!smctx->id_set) {
282         /*
283          * An ID value must be set. The specifications are not clear whether a
284          * NULL is allowed. We only allow it if set explicitly for maximum
285          * flexibility.
286          */
287         SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM, SM2_R_ID_NOT_SET);
288         return 0;
289     }
290
291     if (mdlen < 0) {
292         SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM, SM2_R_INVALID_DIGEST);
293         return 0;
294     }
295
296     /* get hashed prefix 'z' of tbs message */
297     if (!sm2_compute_z_digest(z, md, smctx->id, smctx->id_len, ec))
298         return 0;
299
300     return EVP_DigestUpdate(mctx, z, (size_t)mdlen);
301 }
302
303 static int pkey_sm2_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
304 {
305     EC_KEY *ec = NULL;
306     int ret;
307
308     ec = EC_KEY_new_by_curve_name(NID_sm2);
309     if (ec == NULL)
310         return 0;
311     if (!ossl_assert(ret = EVP_PKEY_assign_EC_KEY(pkey, ec)))
312         EC_KEY_free(ec);
313     return ret;
314 }
315
316 static int pkey_sm2_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
317 {
318     EC_KEY *ec = NULL;
319
320     ec = EC_KEY_new_by_curve_name(NID_sm2);
321     if (ec == NULL)
322         return 0;
323     if (!ossl_assert(EVP_PKEY_assign_EC_KEY(pkey, ec))) {
324         EC_KEY_free(ec);
325         return 0;
326     }
327     /* Note: if error is returned, we count on caller to free pkey->pkey.ec */
328     if (ctx->pkey != NULL
329         && !EVP_PKEY_copy_parameters(pkey, ctx->pkey))
330         return 0;
331
332     return EC_KEY_generate_key(ec);
333 }
334
335 static const EVP_PKEY_METHOD sm2_pkey_meth = {
336     EVP_PKEY_SM2,
337     0,
338     pkey_sm2_init,
339     pkey_sm2_copy,
340     pkey_sm2_cleanup,
341
342     0,
343     pkey_sm2_paramgen,
344
345     0,
346     pkey_sm2_keygen,
347
348     0,
349     pkey_sm2_sign,
350
351     0,
352     pkey_sm2_verify,
353
354     0, 0,
355
356     0, 0, 0, 0,
357
358     0,
359     pkey_sm2_encrypt,
360
361     0,
362     pkey_sm2_decrypt,
363
364     0,
365     0,
366     pkey_sm2_ctrl,
367     pkey_sm2_ctrl_str,
368
369     0, 0,
370
371     0, 0, 0,
372
373     pkey_sm2_digest_custom
374 };
375
376 const EVP_PKEY_METHOD *sm2_pkey_method(void)
377 {
378     return &sm2_pkey_meth;
379 }