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