xref: /netbsd-src/crypto/external/bsd/openssl/dist/crypto/ec/ecx_backend.c (revision 0e2e28bced52bda3788c857106bde6c44d2df3b8)
1b0d17251Schristos /*
2*0e2e28bcSchristos  * Copyright 2020-2024 The OpenSSL Project Authors. All Rights Reserved.
3b0d17251Schristos  *
4b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5b0d17251Schristos  * this file except in compliance with the License.  You can obtain a copy
6b0d17251Schristos  * in the file LICENSE in the source distribution or at
7b0d17251Schristos  * https://www.openssl.org/source/license.html
8b0d17251Schristos  */
9b0d17251Schristos 
10b0d17251Schristos #include <string.h>
11b0d17251Schristos #include <openssl/core_names.h>
12b0d17251Schristos #include <openssl/params.h>
13b0d17251Schristos #include <openssl/ec.h>
14b0d17251Schristos #include <openssl/rand.h>
15b0d17251Schristos #include <openssl/err.h>
16b0d17251Schristos #ifndef FIPS_MODULE
17b0d17251Schristos # include <openssl/x509.h>
18b0d17251Schristos #endif
19b0d17251Schristos #include "crypto/ecx.h"
20b0d17251Schristos #include "ecx_backend.h"
21b0d17251Schristos 
22b0d17251Schristos /*
23b0d17251Schristos  * The intention with the "backend" source file is to offer backend support
24b0d17251Schristos  * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
25b0d17251Schristos  * implementations alike.
26b0d17251Schristos  */
27b0d17251Schristos 
ossl_ecx_public_from_private(ECX_KEY * key)28b0d17251Schristos int ossl_ecx_public_from_private(ECX_KEY *key)
29b0d17251Schristos {
30b0d17251Schristos     switch (key->type) {
31b0d17251Schristos     case ECX_KEY_TYPE_X25519:
32b0d17251Schristos         ossl_x25519_public_from_private(key->pubkey, key->privkey);
33b0d17251Schristos         break;
34b0d17251Schristos     case ECX_KEY_TYPE_ED25519:
35b0d17251Schristos         if (!ossl_ed25519_public_from_private(key->libctx, key->pubkey,
36b0d17251Schristos                                               key->privkey, key->propq)) {
37b0d17251Schristos             ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
38b0d17251Schristos             return 0;
39b0d17251Schristos         }
40b0d17251Schristos         break;
41b0d17251Schristos     case ECX_KEY_TYPE_X448:
42b0d17251Schristos         ossl_x448_public_from_private(key->pubkey, key->privkey);
43b0d17251Schristos         break;
44b0d17251Schristos     case ECX_KEY_TYPE_ED448:
45b0d17251Schristos         if (!ossl_ed448_public_from_private(key->libctx, key->pubkey,
46b0d17251Schristos                                             key->privkey, key->propq)) {
47b0d17251Schristos             ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
48b0d17251Schristos             return 0;
49b0d17251Schristos         }
50b0d17251Schristos         break;
51b0d17251Schristos     }
52b0d17251Schristos     return 1;
53b0d17251Schristos }
54b0d17251Schristos 
ossl_ecx_key_fromdata(ECX_KEY * ecx,const OSSL_PARAM params[],int include_private)55b0d17251Schristos int ossl_ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
56b0d17251Schristos                           int include_private)
57b0d17251Schristos {
58b0d17251Schristos     size_t privkeylen = 0, pubkeylen = 0;
59b0d17251Schristos     const OSSL_PARAM *param_priv_key = NULL, *param_pub_key;
60b0d17251Schristos     unsigned char *pubkey;
61b0d17251Schristos 
62b0d17251Schristos     if (ecx == NULL)
63b0d17251Schristos         return 0;
64b0d17251Schristos 
65b0d17251Schristos     param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
66b0d17251Schristos     if (include_private)
67b0d17251Schristos         param_priv_key =
68b0d17251Schristos             OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
69b0d17251Schristos 
70b0d17251Schristos     if (param_pub_key == NULL && param_priv_key == NULL)
71b0d17251Schristos         return 0;
72b0d17251Schristos 
73b0d17251Schristos     if (param_priv_key != NULL) {
74b0d17251Schristos         if (!OSSL_PARAM_get_octet_string(param_priv_key,
75b0d17251Schristos                                          (void **)&ecx->privkey, ecx->keylen,
76b0d17251Schristos                                          &privkeylen))
77b0d17251Schristos             return 0;
78b0d17251Schristos         if (privkeylen != ecx->keylen) {
79b0d17251Schristos             /*
80b0d17251Schristos              * Invalid key length. We will clear what we've received now. We
81b0d17251Schristos              * can't leave it to ossl_ecx_key_free() because that will call
82b0d17251Schristos              * OPENSSL_secure_clear_free() and assume the correct key length
83b0d17251Schristos              */
84b0d17251Schristos             OPENSSL_secure_clear_free(ecx->privkey, privkeylen);
85b0d17251Schristos             ecx->privkey = NULL;
86b0d17251Schristos             return 0;
87b0d17251Schristos         }
88b0d17251Schristos     }
89b0d17251Schristos 
90b0d17251Schristos 
91b0d17251Schristos     pubkey = ecx->pubkey;
92b0d17251Schristos     if (param_pub_key != NULL
93b0d17251Schristos         && !OSSL_PARAM_get_octet_string(param_pub_key,
94b0d17251Schristos                                         (void **)&pubkey,
95b0d17251Schristos                                          sizeof(ecx->pubkey), &pubkeylen))
96b0d17251Schristos         return 0;
97b0d17251Schristos 
98b0d17251Schristos     if ((param_pub_key != NULL && pubkeylen != ecx->keylen))
99b0d17251Schristos         return 0;
100b0d17251Schristos 
101b0d17251Schristos     if (param_pub_key == NULL && !ossl_ecx_public_from_private(ecx))
102b0d17251Schristos         return 0;
103b0d17251Schristos 
104b0d17251Schristos     ecx->haspubkey = 1;
105b0d17251Schristos 
106b0d17251Schristos     return 1;
107b0d17251Schristos }
108b0d17251Schristos 
ossl_ecx_key_dup(const ECX_KEY * key,int selection)109b0d17251Schristos ECX_KEY *ossl_ecx_key_dup(const ECX_KEY *key, int selection)
110b0d17251Schristos {
111b0d17251Schristos     ECX_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
112b0d17251Schristos 
113b0d17251Schristos     if (ret == NULL) {
114b0d17251Schristos         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
115b0d17251Schristos         return NULL;
116b0d17251Schristos     }
117b0d17251Schristos 
118b0d17251Schristos     ret->lock = CRYPTO_THREAD_lock_new();
119b0d17251Schristos     if (ret->lock == NULL) {
120b0d17251Schristos         OPENSSL_free(ret);
121b0d17251Schristos         return NULL;
122b0d17251Schristos     }
123b0d17251Schristos 
124b0d17251Schristos     ret->libctx = key->libctx;
125*0e2e28bcSchristos     ret->haspubkey = 0;
126b0d17251Schristos     ret->keylen = key->keylen;
127b0d17251Schristos     ret->type = key->type;
128b0d17251Schristos     ret->references = 1;
129b0d17251Schristos 
130b0d17251Schristos     if (key->propq != NULL) {
131b0d17251Schristos         ret->propq = OPENSSL_strdup(key->propq);
132b0d17251Schristos         if (ret->propq == NULL)
133b0d17251Schristos             goto err;
134b0d17251Schristos     }
135b0d17251Schristos 
136*0e2e28bcSchristos     if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
137*0e2e28bcSchristos         && key->haspubkey == 1) {
138b0d17251Schristos         memcpy(ret->pubkey, key->pubkey, sizeof(ret->pubkey));
139*0e2e28bcSchristos         ret->haspubkey = 1;
140*0e2e28bcSchristos     }
141b0d17251Schristos 
142b0d17251Schristos     if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
143b0d17251Schristos         && key->privkey != NULL) {
144b0d17251Schristos         if (ossl_ecx_key_allocate_privkey(ret) == NULL)
145b0d17251Schristos             goto err;
146b0d17251Schristos         memcpy(ret->privkey, key->privkey, ret->keylen);
147b0d17251Schristos     }
148b0d17251Schristos 
149b0d17251Schristos     return ret;
150b0d17251Schristos 
151b0d17251Schristos err:
152b0d17251Schristos     ossl_ecx_key_free(ret);
153b0d17251Schristos     ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
154b0d17251Schristos     return NULL;
155b0d17251Schristos }
156b0d17251Schristos 
157b0d17251Schristos #ifndef FIPS_MODULE
ossl_ecx_key_op(const X509_ALGOR * palg,const unsigned char * p,int plen,int id,ecx_key_op_t op,OSSL_LIB_CTX * libctx,const char * propq)158b0d17251Schristos ECX_KEY *ossl_ecx_key_op(const X509_ALGOR *palg,
159b0d17251Schristos                          const unsigned char *p, int plen,
160b0d17251Schristos                          int id, ecx_key_op_t op,
161b0d17251Schristos                          OSSL_LIB_CTX *libctx, const char *propq)
162b0d17251Schristos {
163b0d17251Schristos     ECX_KEY *key = NULL;
164b0d17251Schristos     unsigned char *privkey, *pubkey;
165b0d17251Schristos 
166b0d17251Schristos     if (op != KEY_OP_KEYGEN) {
167b0d17251Schristos         if (palg != NULL) {
168b0d17251Schristos             int ptype;
169b0d17251Schristos 
170b0d17251Schristos             /* Algorithm parameters must be absent */
171b0d17251Schristos             X509_ALGOR_get0(NULL, &ptype, NULL, palg);
172b0d17251Schristos             if (ptype != V_ASN1_UNDEF) {
173b0d17251Schristos                 ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
174b0d17251Schristos                 return 0;
175b0d17251Schristos             }
176b0d17251Schristos             if (id == EVP_PKEY_NONE)
177b0d17251Schristos                 id = OBJ_obj2nid(palg->algorithm);
178b0d17251Schristos             else if (id != OBJ_obj2nid(palg->algorithm)) {
179b0d17251Schristos                 ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
180b0d17251Schristos                 return 0;
181b0d17251Schristos             }
182b0d17251Schristos         }
183b0d17251Schristos 
184b0d17251Schristos         if (p == NULL || id == EVP_PKEY_NONE || plen != KEYLENID(id)) {
185b0d17251Schristos             ERR_raise(ERR_LIB_EC, EC_R_INVALID_ENCODING);
186b0d17251Schristos             return 0;
187b0d17251Schristos         }
188b0d17251Schristos     }
189b0d17251Schristos 
190b0d17251Schristos     key = ossl_ecx_key_new(libctx, KEYNID2TYPE(id), 1, propq);
191b0d17251Schristos     if (key == NULL) {
192b0d17251Schristos         ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
193b0d17251Schristos         return 0;
194b0d17251Schristos     }
195b0d17251Schristos     pubkey = key->pubkey;
196b0d17251Schristos 
197b0d17251Schristos     if (op == KEY_OP_PUBLIC) {
198b0d17251Schristos         memcpy(pubkey, p, plen);
199b0d17251Schristos     } else {
200b0d17251Schristos         privkey = ossl_ecx_key_allocate_privkey(key);
201b0d17251Schristos         if (privkey == NULL) {
202b0d17251Schristos             ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
203b0d17251Schristos             goto err;
204b0d17251Schristos         }
205b0d17251Schristos         if (op == KEY_OP_KEYGEN) {
206b0d17251Schristos             if (id != EVP_PKEY_NONE) {
207b0d17251Schristos                 if (RAND_priv_bytes_ex(libctx, privkey, KEYLENID(id), 0) <= 0)
208b0d17251Schristos                     goto err;
209b0d17251Schristos                 if (id == EVP_PKEY_X25519) {
210b0d17251Schristos                     privkey[0] &= 248;
211b0d17251Schristos                     privkey[X25519_KEYLEN - 1] &= 127;
212b0d17251Schristos                     privkey[X25519_KEYLEN - 1] |= 64;
213b0d17251Schristos                 } else if (id == EVP_PKEY_X448) {
214b0d17251Schristos                     privkey[0] &= 252;
215b0d17251Schristos                     privkey[X448_KEYLEN - 1] |= 128;
216b0d17251Schristos                 }
217b0d17251Schristos             }
218b0d17251Schristos         } else {
219b0d17251Schristos             memcpy(privkey, p, KEYLENID(id));
220b0d17251Schristos         }
221b0d17251Schristos         if (!ossl_ecx_public_from_private(key)) {
222b0d17251Schristos             ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
223b0d17251Schristos             goto err;
224b0d17251Schristos         }
225b0d17251Schristos     }
226b0d17251Schristos 
227b0d17251Schristos     return key;
228b0d17251Schristos  err:
229b0d17251Schristos     ossl_ecx_key_free(key);
230b0d17251Schristos     return NULL;
231b0d17251Schristos }
232b0d17251Schristos 
ossl_ecx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO * p8inf,OSSL_LIB_CTX * libctx,const char * propq)233b0d17251Schristos ECX_KEY *ossl_ecx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
234b0d17251Schristos                                  OSSL_LIB_CTX *libctx, const char *propq)
235b0d17251Schristos {
236b0d17251Schristos     ECX_KEY *ecx = NULL;
237b0d17251Schristos     const unsigned char *p;
238b0d17251Schristos     int plen;
239b0d17251Schristos     ASN1_OCTET_STRING *oct = NULL;
240b0d17251Schristos     const X509_ALGOR *palg;
241b0d17251Schristos 
242b0d17251Schristos     if (!PKCS8_pkey_get0(NULL, &p, &plen, &palg, p8inf))
243b0d17251Schristos         return 0;
244b0d17251Schristos 
245b0d17251Schristos     oct = d2i_ASN1_OCTET_STRING(NULL, &p, plen);
246b0d17251Schristos     if (oct == NULL) {
247b0d17251Schristos         p = NULL;
248b0d17251Schristos         plen = 0;
249b0d17251Schristos     } else {
250b0d17251Schristos         p = ASN1_STRING_get0_data(oct);
251b0d17251Schristos         plen = ASN1_STRING_length(oct);
252b0d17251Schristos     }
253b0d17251Schristos 
254b0d17251Schristos     /*
255b0d17251Schristos      * EVP_PKEY_NONE means that ecx_key_op() has to figure out the key type
256b0d17251Schristos      * on its own.
257b0d17251Schristos      */
258b0d17251Schristos     ecx = ossl_ecx_key_op(palg, p, plen, EVP_PKEY_NONE, KEY_OP_PRIVATE,
259b0d17251Schristos                           libctx, propq);
260b0d17251Schristos     ASN1_OCTET_STRING_free(oct);
261b0d17251Schristos     return ecx;
262b0d17251Schristos }
263b0d17251Schristos #endif
264