2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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
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"
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;
33 #define ECX_POSSIBLE_SELECTIONS (OSSL_KEYMGMT_SELECT_KEYPAIR)
35 static void *x25519_new_key(void *provctx)
37 return ecx_key_new(ECX_KEY_TYPE_X25519, 0);
40 static void *x448_new_key(void *provctx)
42 return ecx_key_new(ECX_KEY_TYPE_X448, 0);
45 static void *ed25519_new_key(void *provctx)
47 return ecx_key_new(ECX_KEY_TYPE_ED25519, 0);
50 static void *ed448_new_key(void *provctx)
52 return ecx_key_new(ECX_KEY_TYPE_ED448, 0);
55 static int ecx_has(void *keydata, int selection)
57 ECX_KEY *key = keydata;
61 if ((selection & ECX_POSSIBLE_SELECTIONS) != 0)
64 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
65 ok = ok && key->haspubkey;
67 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
68 ok = ok && key->privkey != NULL;
73 static int ecx_import(void *keydata, int selection, const OSSL_PARAM params[])
75 ECX_KEY *key = keydata;
77 int include_private = 0;
82 if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
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);
92 static int key_to_params(ECX_KEY *key, OSSL_PARAM_BLD *tmpl,
98 if (!ossl_param_build_set_octet_string(tmpl, params,
99 OSSL_PKEY_PARAM_PUB_KEY,
100 key->pubkey, key->keylen))
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))
112 static int ecx_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
115 ECX_KEY *key = keydata;
116 OSSL_PARAM_BLD *tmpl;
117 OSSL_PARAM *params = NULL;
123 tmpl = OSSL_PARAM_BLD_new();
127 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0
128 && !key_to_params(key, tmpl, NULL))
131 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0
132 && !key_to_params(key, tmpl, NULL))
135 params = OSSL_PARAM_BLD_to_param(tmpl);
139 ret = param_cb(params, cbarg);
140 OSSL_PARAM_BLD_free_params(params);
142 OSSL_PARAM_BLD_free(tmpl);
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)
150 static const OSSL_PARAM ecx_key_types[] = {
154 static const OSSL_PARAM *ecx_imexport_types(int selection)
156 if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
157 return ecx_key_types;
161 static int ecx_get_params(void *key, OSSL_PARAM params[], int bits, int secbits,
167 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
168 && !OSSL_PARAM_set_int(p, bits))
170 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
171 && !OSSL_PARAM_set_int(p, secbits))
173 if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
174 && !OSSL_PARAM_set_int(p, size))
176 return key_to_params(ecx, NULL, params);
179 static int x25519_get_params(void *key, OSSL_PARAM params[])
181 return ecx_get_params(key, params, X25519_BITS, X25519_SECURITY_BITS,
185 static int x448_get_params(void *key, OSSL_PARAM params[])
187 return ecx_get_params(key, params, X448_BITS, X448_SECURITY_BITS,
191 static int ed25519_get_params(void *key, OSSL_PARAM params[])
193 return ecx_get_params(key, params, ED25519_BITS, ED25519_SECURITY_BITS,
197 static int ed448_get_params(void *key, OSSL_PARAM params[])
199 return ecx_get_params(key, params, ED448_BITS, ED448_SECURITY_BITS,
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),
211 static const OSSL_PARAM *ecx_gettable_params(void)
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 }, \
230 MAKE_KEYMGMT_FUNCTIONS(x25519)
231 MAKE_KEYMGMT_FUNCTIONS(x448)
232 MAKE_KEYMGMT_FUNCTIONS(ed25519)
233 MAKE_KEYMGMT_FUNCTIONS(ed448)