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