]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ec/ecx_backend.c
Update copyright year
[thirdparty/openssl.git] / crypto / ec / ecx_backend.c
1 /*
2 * Copyright 2020-2021 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 ossl_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 key->propq)) {
32 ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
33 return 0;
34 }
35 break;
36 case ECX_KEY_TYPE_X448:
37 X448_public_from_private(key->pubkey, key->privkey);
38 break;
39 case ECX_KEY_TYPE_ED448:
40 if (!ED448_public_from_private(key->libctx, key->pubkey, key->privkey,
41 key->propq)) {
42 ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
43 return 0;
44 }
45 break;
46 }
47 return 1;
48 }
49
50 int ossl_ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
51 int include_private)
52 {
53 size_t privkeylen = 0, pubkeylen = 0;
54 const OSSL_PARAM *param_priv_key = NULL, *param_pub_key;
55 unsigned char *pubkey;
56
57 if (ecx == NULL)
58 return 0;
59
60 param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
61 if (include_private)
62 param_priv_key =
63 OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
64
65 if (param_pub_key == NULL && param_priv_key == NULL)
66 return 0;
67
68 if (param_priv_key != NULL
69 && !OSSL_PARAM_get_octet_string(param_priv_key,
70 (void **)&ecx->privkey, ecx->keylen,
71 &privkeylen))
72 return 0;
73
74 pubkey = ecx->pubkey;
75 if (param_pub_key != NULL
76 && !OSSL_PARAM_get_octet_string(param_pub_key,
77 (void **)&pubkey,
78 sizeof(ecx->pubkey), &pubkeylen))
79 return 0;
80
81 if ((param_pub_key != NULL && pubkeylen != ecx->keylen)
82 || (param_priv_key != NULL && privkeylen != ecx->keylen))
83 return 0;
84
85 if (param_pub_key == NULL && !ossl_ecx_public_from_private(ecx))
86 return 0;
87
88 ecx->haspubkey = 1;
89
90 return 1;
91 }
92