ca53a93f5ec8e644f80086cad8d0563a24a85599
[oweals/openssl.git] / providers / implementations / keymgmt / ecx_kmgmt.c
1 /*
2  * Copyright 2020 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 <assert.h>
11 #include <openssl/core_numbers.h>
12 #include <openssl/core_names.h>
13 #include "crypto/ecx.h"
14 #include "prov/implementations.h"
15 #include "prov/providercommon.h"
16 #include "internal/param_build_set.h"
17
18 static OSSL_OP_keymgmt_new_fn x25519_new_key;
19 static OSSL_OP_keymgmt_new_fn x448_new_key;
20 static OSSL_OP_keymgmt_new_fn ed25519_new_key;
21 static OSSL_OP_keymgmt_new_fn ed448_new_key;
22 static OSSL_OP_keymgmt_get_params_fn x25519_get_params;
23 static OSSL_OP_keymgmt_get_params_fn x448_get_params;
24 static OSSL_OP_keymgmt_get_params_fn ed25519_get_params;
25 static OSSL_OP_keymgmt_get_params_fn ed448_get_params;
26 static OSSL_OP_keymgmt_gettable_params_fn ecx_gettable_params;
27 static OSSL_OP_keymgmt_has_fn ecx_has;
28 static OSSL_OP_keymgmt_import_fn ecx_import;
29 static OSSL_OP_keymgmt_import_types_fn ecx_imexport_types;
30 static OSSL_OP_keymgmt_export_fn ecx_export;
31 static OSSL_OP_keymgmt_export_types_fn ecx_imexport_types;
32
33 #define ECX_POSSIBLE_SELECTIONS (OSSL_KEYMGMT_SELECT_KEYPAIR)
34
35 static void *x25519_new_key(void *provctx)
36 {
37     return ecx_key_new(ECX_KEY_TYPE_X25519, 0);
38 }
39
40 static void *x448_new_key(void *provctx)
41 {
42     return ecx_key_new(ECX_KEY_TYPE_X448, 0);
43 }
44
45 static void *ed25519_new_key(void *provctx)
46 {
47     return ecx_key_new(ECX_KEY_TYPE_ED25519, 0);
48 }
49
50 static void *ed448_new_key(void *provctx)
51 {
52     return ecx_key_new(ECX_KEY_TYPE_ED448, 0);
53 }
54
55 static int ecx_has(void *keydata, int selection)
56 {
57     ECX_KEY *key = keydata;
58     int ok = 0;
59
60     if (key != NULL) {
61         if ((selection & ECX_POSSIBLE_SELECTIONS) != 0)
62             ok = 1;
63
64         if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
65             ok = ok && key->haspubkey;
66
67         if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
68             ok = ok && key->privkey != NULL;
69     }
70     return ok;
71 }
72
73 static int ecx_import(void *keydata, int selection, const OSSL_PARAM params[])
74 {
75     ECX_KEY *key = keydata;
76     int ok = 1;
77     int include_private = 0;
78
79     if (key == NULL)
80         return 0;
81
82     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
83         return 0;
84
85     include_private = ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0);
86     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
87         ok = ok && ecx_key_fromdata(key, params, include_private);
88
89     return ok;
90 }
91
92 static int key_to_params(ECX_KEY *key, OSSL_PARAM_BLD *tmpl,
93                          OSSL_PARAM params[])
94 {
95     if (key == NULL)
96         return 0;
97
98     if (!ossl_param_build_set_octet_string(tmpl, params,
99                                            OSSL_PKEY_PARAM_PUB_KEY,
100                                            key->pubkey, key->keylen))
101         return 0;
102
103     if (key->privkey != NULL
104         && !ossl_param_build_set_octet_string(tmpl, params,
105                                               OSSL_PKEY_PARAM_PRIV_KEY,
106                                               key->privkey, key->keylen))
107         return 0;
108
109     return 1;
110 }
111
112 static int ecx_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
113                       void *cbarg)
114 {
115     ECX_KEY *key = keydata;
116     OSSL_PARAM_BLD *tmpl;
117     OSSL_PARAM *params = NULL;
118     int ret = 0;
119
120     if (key == NULL)
121         return 0;
122
123     tmpl = OSSL_PARAM_BLD_new();
124     if (tmpl == NULL)
125         return 0;
126
127     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0
128          && !key_to_params(key, tmpl, NULL))
129         goto err;
130
131     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0
132          && !key_to_params(key, tmpl, NULL))
133         goto err;
134
135     params = OSSL_PARAM_BLD_to_param(tmpl);
136     if (params == NULL)
137         goto err;
138
139     ret = param_cb(params, cbarg);
140     OSSL_PARAM_BLD_free_params(params);
141 err:
142     OSSL_PARAM_BLD_free(tmpl);
143     return ret;
144 }
145
146 #define ECX_KEY_TYPES()                                                        \
147 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0),                     \
148 OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
149
150 static const OSSL_PARAM ecx_key_types[] = {
151     ECX_KEY_TYPES(),
152     OSSL_PARAM_END
153 };
154 static const OSSL_PARAM *ecx_imexport_types(int selection)
155 {
156     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
157         return ecx_key_types;
158     return NULL;
159 }
160
161 static int ecx_get_params(void *key, OSSL_PARAM params[], int bits, int secbits,
162                           int size)
163 {
164     ECX_KEY *ecx = key;
165     OSSL_PARAM *p;
166
167     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
168         && !OSSL_PARAM_set_int(p, bits))
169         return 0;
170     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
171         && !OSSL_PARAM_set_int(p, secbits))
172         return 0;
173     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
174         && !OSSL_PARAM_set_int(p, size))
175         return 0;
176     return key_to_params(ecx, NULL, params);
177 }
178
179 static int x25519_get_params(void *key, OSSL_PARAM params[])
180 {
181     return ecx_get_params(key, params, X25519_BITS, X25519_SECURITY_BITS,
182                           X25519_KEYLEN);
183 }
184
185 static int x448_get_params(void *key, OSSL_PARAM params[])
186 {
187     return ecx_get_params(key, params, X448_BITS, X448_SECURITY_BITS,
188                           X448_KEYLEN);
189 }
190
191 static int ed25519_get_params(void *key, OSSL_PARAM params[])
192 {
193     return ecx_get_params(key, params, ED25519_BITS, ED25519_SECURITY_BITS,
194                           ED25519_KEYLEN);
195 }
196
197 static int ed448_get_params(void *key, OSSL_PARAM params[])
198 {
199     return ecx_get_params(key, params, ED448_BITS, ED448_SECURITY_BITS,
200                           ED448_KEYLEN);
201 }
202
203 static const OSSL_PARAM ecx_params[] = {
204     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
205     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
206     OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
207     ECX_KEY_TYPES(),
208     OSSL_PARAM_END
209 };
210
211 static const OSSL_PARAM *ecx_gettable_params(void)
212 {
213     return ecx_params;
214 }
215
216 #define MAKE_KEYMGMT_FUNCTIONS(alg) \
217     const OSSL_DISPATCH alg##_keymgmt_functions[] = { \
218         { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))alg##_new_key }, \
219         { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ecx_key_free }, \
220         { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))alg##_get_params }, \
221         { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ecx_gettable_params }, \
222         { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ecx_has }, \
223         { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ecx_import }, \
224         { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ecx_imexport_types }, \
225         { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ecx_export }, \
226         { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ecx_imexport_types }, \
227         { 0, NULL } \
228     };
229
230 MAKE_KEYMGMT_FUNCTIONS(x25519)
231 MAKE_KEYMGMT_FUNCTIONS(x448)
232 MAKE_KEYMGMT_FUNCTIONS(ed25519)
233 MAKE_KEYMGMT_FUNCTIONS(ed448)