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