EVP: Add a temporary SM2 hack to key generation
[openssl.git] / crypto / evp / pmeth_gn.c
1 /*
2  * Copyright 2006-2016 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 #include <stdio.h>
11 #include <stdlib.h>
12 #include <openssl/core.h>
13 #include <openssl/core_names.h>
14 #include "internal/cryptlib.h"
15 #include "internal/core.h"
16 #include <openssl/objects.h>
17 #include <openssl/evp.h>
18 #include "crypto/bn.h"
19 #include "crypto/asn1.h"
20 #include "crypto/evp.h"
21 #include "evp_local.h"
22
23 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_EC)
24 # define TMP_SM2_HACK
25 #endif
26
27 /* TODO(3.0) remove when provider SM2 key generation is implemented */
28 #ifdef TMP_SM2_HACK
29 # include <openssl/ec.h>
30 # include <openssl/serializer.h>
31 # include "internal/sizes.h"
32 #endif
33
34 static int gen_init(EVP_PKEY_CTX *ctx, int operation)
35 {
36     int ret = 0;
37
38     if (ctx == NULL)
39         goto not_supported;
40
41     evp_pkey_ctx_free_old_ops(ctx);
42     ctx->operation = operation;
43
44     if (ctx->keymgmt == NULL || ctx->keymgmt->gen_init == NULL)
45         goto legacy;
46
47 /* TODO remove when provider SM2 key generation is implemented */
48 #ifdef TMP_SM2_HACK
49     if (ctx->pmeth != NULL && ctx->pmeth->pkey_id == EVP_PKEY_SM2)
50         goto legacy;
51 #endif
52
53     switch (operation) {
54     case EVP_PKEY_OP_PARAMGEN:
55         ctx->op.keymgmt.genctx =
56             evp_keymgmt_gen_init(ctx->keymgmt,
57                                  OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
58         break;
59     case EVP_PKEY_OP_KEYGEN:
60         ctx->op.keymgmt.genctx =
61             evp_keymgmt_gen_init(ctx->keymgmt, OSSL_KEYMGMT_SELECT_KEYPAIR);
62         break;
63     }
64
65     if (ctx->op.keymgmt.genctx == NULL)
66         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
67     else
68         ret = 1;
69     goto end;
70
71  legacy:
72 #ifdef FIPS_MODE
73     goto not_supported;
74 #else
75     if (ctx->pmeth == NULL
76         || (operation == EVP_PKEY_OP_PARAMGEN
77             && ctx->pmeth->paramgen == NULL)
78         || (operation == EVP_PKEY_OP_KEYGEN
79             && ctx->pmeth->keygen == NULL))
80         goto not_supported;
81
82     ret = 1;
83     switch (operation) {
84     case EVP_PKEY_OP_PARAMGEN:
85         if (ctx->pmeth->paramgen_init != NULL)
86             ret = ctx->pmeth->paramgen_init(ctx);
87         break;
88     case EVP_PKEY_OP_KEYGEN:
89         if (ctx->pmeth->keygen_init != NULL)
90             ret = ctx->pmeth->keygen_init(ctx);
91         break;
92     }
93 #endif
94
95  end:
96     if (ret <= 0)
97         ctx->operation = EVP_PKEY_OP_UNDEFINED;
98     return ret;
99
100  not_supported:
101     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
102     ret = -2;
103     goto end;
104 }
105
106 int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx)
107 {
108     return gen_init(ctx, EVP_PKEY_OP_PARAMGEN);
109 }
110
111 int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx)
112 {
113     return gen_init(ctx, EVP_PKEY_OP_KEYGEN);
114 }
115
116 static int ossl_callback_to_pkey_gencb(const OSSL_PARAM params[], void *arg)
117 {
118     EVP_PKEY_CTX *ctx = arg;
119     const OSSL_PARAM *param = NULL;
120     int p = -1, n = -1;
121
122     if (ctx->pkey_gencb == NULL)
123         return 1;                /* No callback?  That's fine */
124
125     if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_POTENTIAL))
126         == NULL
127         || !OSSL_PARAM_get_int(param, &p))
128         return 0;
129     if ((param = OSSL_PARAM_locate_const(params, OSSL_GEN_PARAM_ITERATION))
130         == NULL
131         || !OSSL_PARAM_get_int(param, &n))
132         return 0;
133
134     ctx->keygen_info[0] = p;
135     ctx->keygen_info[1] = n;
136
137     return ctx->pkey_gencb(ctx);
138 }
139
140 int EVP_PKEY_gen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
141 {
142     int ret = 0;
143     OSSL_CALLBACK cb;
144     EVP_PKEY *allocated_pkey = NULL;
145
146     if (ppkey == NULL)
147         return -1;
148
149     if (ctx == NULL)
150         goto not_supported;
151
152     if ((ctx->operation & EVP_PKEY_OP_TYPE_GEN) == 0)
153         goto not_initialized;
154
155     if (*ppkey == NULL)
156         *ppkey = allocated_pkey = EVP_PKEY_new();
157
158     if (*ppkey == NULL) {
159         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
160         return -1;
161     }
162
163     if (ctx->op.keymgmt.genctx == NULL)
164         goto legacy;
165
166     ret = 1;
167     if (ctx->pkey != NULL) {
168         EVP_KEYMGMT *tmp_keymgmt = ctx->keymgmt;
169         void *keydata =
170             evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
171                                         &tmp_keymgmt, ctx->propquery);
172
173         if (keydata == NULL)
174             goto not_supported;
175         ret = evp_keymgmt_gen_set_template(ctx->keymgmt,
176                                            ctx->op.keymgmt.genctx, keydata);
177     }
178
179     /*
180      * the returned value from evp_keymgmt_util_gen() is cached in *ppkey,
181      * so we so not need to save it, just check it.
182      */
183     ret = ret
184         && (evp_keymgmt_util_gen(*ppkey, ctx->keymgmt, ctx->op.keymgmt.genctx,
185                                  ossl_callback_to_pkey_gencb, ctx)
186             != NULL);
187
188 #ifndef FIPS_MODE
189     /* In case |*ppkey| was originally a legacy key */
190     if (ret)
191         evp_pkey_free_legacy(*ppkey);
192 #endif
193
194 /* TODO remove when SM2 key have been cleanly separated from EC keys */
195 #ifdef TMP_SM2_HACK
196     /*
197      * Legacy SM2 keys are implemented as EC_KEY with a twist.  The legacy
198      * key generation detects the SM2 curve and "magically" changes the pkey
199      * id accordingly.
200      * Since we don't have SM2 in the provider implementation, we need to
201      * downgrade the generated provider side key to a legacy one under the
202      * same conditions.
203      *
204      * THIS IS AN UGLY BUT TEMPORARY HACK
205      */
206     {
207         char curve_name[OSSL_MAX_NAME_SIZE] = "";
208
209         if (EVP_PKEY_CTX_get_ec_paramgen_curve_name(ctx, curve_name,
210                                                     sizeof(curve_name)) < 1
211             || strcmp(curve_name, "SM2") != 0)
212             goto end;
213     }
214
215     if (!evp_pkey_downgrade(*ppkey)
216         || !EVP_PKEY_set_alias_type(*ppkey, EVP_PKEY_SM2))
217         ret = 0;
218 #endif
219     goto end;
220
221  legacy:
222 #ifdef FIPS_MODE
223     goto not_supported;
224 #else
225     if (ctx->pkey && !evp_pkey_downgrade(ctx->pkey))
226         goto not_accessible;
227     switch (ctx->operation) {
228     case EVP_PKEY_OP_PARAMGEN:
229         ret = ctx->pmeth->paramgen(ctx, *ppkey);
230         break;
231     case EVP_PKEY_OP_KEYGEN:
232         ret = ctx->pmeth->keygen(ctx, *ppkey);
233         break;
234     default:
235         goto not_supported;
236     }
237 #endif
238
239  end:
240     if (ret <= 0) {
241         if (allocated_pkey != NULL)
242             *ppkey = NULL;
243         EVP_PKEY_free(allocated_pkey);
244     }
245     return ret;
246
247  not_supported:
248     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
249     ret = -2;
250     goto end;
251  not_initialized:
252     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
253     ret = -1;
254     goto end;
255 #ifndef FIPS_MODE
256  not_accessible:
257     ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_DOMAIN_PARAMETERS);
258     ret = -1;
259     goto end;
260 #endif
261 }
262
263 int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
264 {
265     if (ctx->operation != EVP_PKEY_OP_PARAMGEN) {
266         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
267         return -1;
268     }
269     return EVP_PKEY_gen(ctx, ppkey);
270 }
271
272 int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey)
273 {
274     if (ctx->operation != EVP_PKEY_OP_KEYGEN) {
275         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATON_NOT_INITIALIZED);
276         return -1;
277     }
278     return EVP_PKEY_gen(ctx, ppkey);
279 }
280
281 void EVP_PKEY_CTX_set_cb(EVP_PKEY_CTX *ctx, EVP_PKEY_gen_cb *cb)
282 {
283     ctx->pkey_gencb = cb;
284 }
285
286 EVP_PKEY_gen_cb *EVP_PKEY_CTX_get_cb(EVP_PKEY_CTX *ctx)
287 {
288     return ctx->pkey_gencb;
289 }
290
291 /*
292  * "translation callback" to call EVP_PKEY_CTX callbacks using BN_GENCB style
293  * callbacks.
294  */
295
296 static int trans_cb(int a, int b, BN_GENCB *gcb)
297 {
298     EVP_PKEY_CTX *ctx = BN_GENCB_get_arg(gcb);
299     ctx->keygen_info[0] = a;
300     ctx->keygen_info[1] = b;
301     return ctx->pkey_gencb(ctx);
302 }
303
304 void evp_pkey_set_cb_translate(BN_GENCB *cb, EVP_PKEY_CTX *ctx)
305 {
306     BN_GENCB_set(cb, trans_cb, ctx);
307 }
308
309 int EVP_PKEY_CTX_get_keygen_info(EVP_PKEY_CTX *ctx, int idx)
310 {
311     if (idx == -1)
312         return ctx->keygen_info_count;
313     if (idx < 0 || idx > ctx->keygen_info_count)
314         return 0;
315     return ctx->keygen_info[idx];
316 }
317
318 #ifndef FIPS_MODE
319
320 EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
321                                const unsigned char *key, int keylen)
322 {
323     EVP_PKEY_CTX *mac_ctx = NULL;
324     EVP_PKEY *mac_key = NULL;
325     mac_ctx = EVP_PKEY_CTX_new_id(type, e);
326     if (!mac_ctx)
327         return NULL;
328     if (EVP_PKEY_keygen_init(mac_ctx) <= 0)
329         goto merr;
330     if (EVP_PKEY_CTX_set_mac_key(mac_ctx, key, keylen) <= 0)
331         goto merr;
332     if (EVP_PKEY_keygen(mac_ctx, &mac_key) <= 0)
333         goto merr;
334  merr:
335     EVP_PKEY_CTX_free(mac_ctx);
336     return mac_key;
337 }
338
339 #endif /* FIPS_MODE */
340
341 /*- All methods below can also be used in FIPS_MODE */
342
343 static int fromdata_init(EVP_PKEY_CTX *ctx, int operation)
344 {
345     if (ctx == NULL || ctx->keytype == NULL)
346         goto not_supported;
347
348     evp_pkey_ctx_free_old_ops(ctx);
349     if (ctx->keymgmt == NULL)
350         goto not_supported;
351
352     ctx->operation = operation;
353     return 1;
354
355  not_supported:
356     ctx->operation = EVP_PKEY_OP_UNDEFINED;
357     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
358     return -2;
359 }
360
361 int EVP_PKEY_param_fromdata_init(EVP_PKEY_CTX *ctx)
362 {
363     return fromdata_init(ctx, EVP_PKEY_OP_PARAMFROMDATA);
364 }
365
366 int EVP_PKEY_key_fromdata_init(EVP_PKEY_CTX *ctx)
367 {
368     return fromdata_init(ctx, EVP_PKEY_OP_KEYFROMDATA);
369 }
370
371 int EVP_PKEY_fromdata(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey, OSSL_PARAM params[])
372 {
373     void *keydata = NULL;
374     int selection;
375
376     if (ctx == NULL || (ctx->operation & EVP_PKEY_OP_TYPE_FROMDATA) == 0) {
377         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
378         return -2;
379     }
380
381     if (ppkey == NULL)
382         return -1;
383
384     if (*ppkey == NULL)
385         *ppkey = EVP_PKEY_new();
386
387     if (*ppkey == NULL) {
388         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
389         return -1;
390     }
391
392     if (ctx->operation == EVP_PKEY_OP_PARAMFROMDATA)
393         selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
394     else
395         selection = OSSL_KEYMGMT_SELECT_ALL;
396     keydata = evp_keymgmt_util_fromdata(*ppkey, ctx->keymgmt, selection,
397                                         params);
398
399     if (keydata == NULL)
400         return 0;
401     /* keydata is cached in *ppkey, so we need not bother with it further */
402     return 1;
403 }
404
405 /*
406  * TODO(3.0) Re-evaluate the names, it's possible that we find these to be
407  * better:
408  *
409  * EVP_PKEY_param_settable()
410  * EVP_PKEY_param_gettable()
411  */
412 const OSSL_PARAM *EVP_PKEY_param_fromdata_settable(EVP_PKEY_CTX *ctx)
413 {
414     /* We call fromdata_init to get ctx->keymgmt populated */
415     if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
416         return evp_keymgmt_import_types(ctx->keymgmt,
417                                         OSSL_KEYMGMT_SELECT_ALL_PARAMETERS);
418     return NULL;
419 }
420
421 const OSSL_PARAM *EVP_PKEY_key_fromdata_settable(EVP_PKEY_CTX *ctx)
422 {
423     /* We call fromdata_init to get ctx->keymgmt populated */
424     if (fromdata_init(ctx, EVP_PKEY_OP_UNDEFINED))
425         return evp_keymgmt_import_types(ctx->keymgmt,
426                                         OSSL_KEYMGMT_SELECT_ALL);
427     return NULL;
428 }