Add the ability to ECX to import keys with only the private key
[openssl.git] / crypto / ec / ecx_backend.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 <openssl/core_names.h>
11 #include <openssl/params.h>
12 #include <openssl/ec.h>
13 #include <openssl/err.h>
14 #include "crypto/ecx.h"
15 #include "ecx_backend.h"
16
17 /*
18  * The intention with the "backend" source file is to offer backend support
19  * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
20  * implementations alike.
21  */
22
23 int ecx_public_from_private(ECX_KEY *key)
24 {
25     switch (key->type) {
26     case ECX_KEY_TYPE_X25519:
27         X25519_public_from_private(key->pubkey, key->privkey);
28         break;
29     case ECX_KEY_TYPE_ED25519:
30         if (!ED25519_public_from_private(key->libctx, key->pubkey, key->privkey)) {
31             ECerr(0, EC_R_FAILED_MAKING_PUBLIC_KEY);
32             return 0;
33         }
34         break;
35     case ECX_KEY_TYPE_X448:
36         X448_public_from_private(key->pubkey, key->privkey);
37         break;
38     case ECX_KEY_TYPE_ED448:
39         if (!ED448_public_from_private(key->libctx, key->pubkey, key->privkey)) {
40             ECerr(0, EC_R_FAILED_MAKING_PUBLIC_KEY);
41             return 0;
42         }
43         break;
44     }
45     return 1;
46 }
47
48 int ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
49                      int include_private)
50 {
51     size_t privkeylen = 0, pubkeylen = 0;
52     const OSSL_PARAM *param_priv_key = NULL, *param_pub_key;
53     unsigned char *pubkey;
54
55     if (ecx == NULL)
56         return 0;
57
58     param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
59     if (include_private)
60         param_priv_key =
61             OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
62
63     if (param_pub_key == NULL && param_priv_key == NULL)
64         return 0;
65
66     if (param_priv_key != NULL
67         && !OSSL_PARAM_get_octet_string(param_priv_key,
68                                         (void **)&ecx->privkey, ecx->keylen,
69                                         &privkeylen))
70         return 0;
71
72     pubkey = ecx->pubkey;
73     if (param_pub_key != NULL
74         && !OSSL_PARAM_get_octet_string(param_pub_key,
75                                         (void **)&pubkey,
76                                          sizeof(ecx->pubkey), &pubkeylen))
77         return 0;
78
79     if ((param_pub_key != NULL && pubkeylen != ecx->keylen)
80         || (param_priv_key != NULL && privkeylen != ecx->keylen))
81         return 0;
82
83     if (param_pub_key == NULL && !ecx_public_from_private(ecx))
84         return 0;
85
86     ecx->haspubkey = 1;
87
88     return 1;
89 }
90