b078c6de58d5eea854cd0a58aae1c0ae03342baa
[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 <openssl/params.h>
14 #include "internal/param_build.h"
15 #include "crypto/ecx.h"
16 #include "prov/implementations.h"
17 #include "prov/providercommon.h"
18
19 static OSSL_OP_keymgmt_new_fn x25519_new_key;
20 static OSSL_OP_keymgmt_new_fn x448_new_key;
21 static OSSL_OP_keymgmt_new_fn ed25519_new_key;
22 static OSSL_OP_keymgmt_new_fn ed448_new_key;
23 static OSSL_OP_keymgmt_get_params_fn x25519_get_params;
24 static OSSL_OP_keymgmt_get_params_fn x448_get_params;
25 static OSSL_OP_keymgmt_get_params_fn ed25519_get_params;
26 static OSSL_OP_keymgmt_get_params_fn ed448_get_params;
27 static OSSL_OP_keymgmt_gettable_params_fn ecx_gettable_params;
28 static OSSL_OP_keymgmt_has_fn ecx_has;
29 static OSSL_OP_keymgmt_import_fn ecx_import;
30 static OSSL_OP_keymgmt_import_types_fn ecx_imexport_types;
31 static OSSL_OP_keymgmt_export_fn ecx_export;
32 static OSSL_OP_keymgmt_export_types_fn ecx_imexport_types;
33
34 #define ECX_POSSIBLE_SELECTIONS (OSSL_KEYMGMT_SELECT_KEYPAIR)
35
36 static void *x25519_new_key(void *provctx)
37 {
38     return ecx_key_new(X25519_KEYLEN, 0);
39 }
40
41 static void *x448_new_key(void *provctx)
42 {
43     return ecx_key_new(X448_KEYLEN, 0);
44 }
45
46 static void *ed25519_new_key(void *provctx)
47 {
48     return ecx_key_new(ED25519_KEYLEN, 0);
49 }
50
51 static void *ed448_new_key(void *provctx)
52 {
53     return ecx_key_new(ED448_KEYLEN, 0);
54 }
55
56 static int ecx_has(void *keydata, int selection)
57 {
58     ECX_KEY *key = keydata;
59     int ok = 1;
60
61     if ((selection & ECX_POSSIBLE_SELECTIONS) == 0)
62         return 0;
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     size_t privkeylen = 0, pubkeylen;
77     const OSSL_PARAM *param_priv_key = NULL, *param_pub_key;
78     unsigned char *pubkey;
79
80     if (key == NULL)
81         return 0;
82
83     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
84         return 0;
85
86     param_pub_key =
87         OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
88
89     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
90         param_priv_key =
91             OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
92     /*
93      * If a private key is present then a public key must also be present.
94      * Alternatively we've just got a public key.
95      */
96     if (param_pub_key == NULL)
97         return 0;
98
99     if (param_priv_key != NULL
100              && !OSSL_PARAM_get_octet_string(param_priv_key,
101                                             (void **)&key->privkey, key->keylen,
102                                              &privkeylen))
103         return 0;
104
105     pubkey = key->pubkey;
106     if (!OSSL_PARAM_get_octet_string(param_pub_key,
107                                      (void **)&pubkey,
108                                      sizeof(key->pubkey), &pubkeylen))
109         return 0;
110
111     if (pubkeylen != key->keylen
112             || (param_priv_key != NULL && privkeylen != key->keylen))
113         return 0;
114
115     key->haspubkey = 1;
116
117     return 1;
118 }
119
120 static int key_to_params(ECX_KEY *key, OSSL_PARAM_BLD *tmpl)
121 {
122     if (key == NULL)
123         return 0;
124
125     if (!ossl_param_bld_push_octet_string(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
126                                           key->pubkey, key->keylen))
127         return 0;
128
129     if (key->privkey != NULL
130         && !ossl_param_bld_push_octet_string(tmpl, OSSL_PKEY_PARAM_PRIV_KEY,
131                                              key->privkey, key->keylen))
132         return 0;
133
134     return 1;
135 }
136
137 static int ecx_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
138                       void *cbarg)
139 {
140     ECX_KEY *key = keydata;
141     OSSL_PARAM_BLD tmpl;
142     OSSL_PARAM *params = NULL;
143     int ret;
144
145     if (key == NULL)
146         return 0;
147
148     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0
149             && !key_to_params(key, &tmpl))
150         return 0;
151
152     ossl_param_bld_init(&tmpl);
153     params = ossl_param_bld_to_param(&tmpl);
154     if (params == NULL) {
155         ossl_param_bld_free(params);
156         return 0;
157     }
158
159     ret = param_cb(params, cbarg);
160     ossl_param_bld_free(params);
161     return ret;
162 }
163
164 static const OSSL_PARAM ecx_key_types[] = {
165     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0),
166     OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
167     OSSL_PARAM_END
168 };
169 static const OSSL_PARAM *ecx_imexport_types(int selection)
170 {
171     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
172         return ecx_key_types;
173     return NULL;
174 }
175
176 static int ecx_get_params(OSSL_PARAM params[], int bits, int secbits,
177                           int size)
178 {
179     OSSL_PARAM *p;
180
181     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
182         && !OSSL_PARAM_set_int(p, bits))
183         return 0;
184     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
185         && !OSSL_PARAM_set_int(p, secbits))
186         return 0;
187     if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
188         && !OSSL_PARAM_set_int(p, size))
189         return 0;
190     return 1;
191 }
192
193 static int x25519_get_params(void *key, OSSL_PARAM params[])
194 {
195     return ecx_get_params(params, X25519_BITS, X25519_SECURITY_BITS, X25519_KEYLEN);
196 }
197
198 static int x448_get_params(void *key, OSSL_PARAM params[])
199 {
200     return ecx_get_params(params, X448_BITS, X448_SECURITY_BITS, X448_KEYLEN);
201 }
202
203 static int ed25519_get_params(void *key, OSSL_PARAM params[])
204 {
205     return ecx_get_params(params, ED25519_BITS, ED25519_SECURITY_BITS, ED25519_KEYLEN);
206 }
207
208 static int ed448_get_params(void *key, OSSL_PARAM params[])
209 {
210     return ecx_get_params(params, ED448_BITS, ED448_SECURITY_BITS, ED448_KEYLEN);
211 }
212
213 static const OSSL_PARAM ecx_params[] = {
214     OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
215     OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
216     OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
217     OSSL_PARAM_END
218 };
219
220 static const OSSL_PARAM *ecx_gettable_params(void)
221 {
222     return ecx_params;
223 }
224
225 #define MAKE_KEYMGMT_FUNCTIONS(alg) \
226     const OSSL_DISPATCH alg##_keymgmt_functions[] = { \
227         { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))alg##_new_key }, \
228         { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ecx_key_free }, \
229         { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))alg##_get_params }, \
230         { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ecx_gettable_params }, \
231         { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ecx_has }, \
232         { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ecx_import }, \
233         { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ecx_imexport_types }, \
234         { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ecx_export }, \
235         { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ecx_imexport_types }, \
236         { 0, NULL } \
237     };
238
239 MAKE_KEYMGMT_FUNCTIONS(x25519)
240 MAKE_KEYMGMT_FUNCTIONS(x448)
241 MAKE_KEYMGMT_FUNCTIONS(ed25519)
242 MAKE_KEYMGMT_FUNCTIONS(ed448)