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