2020-03-23 12:40:47 +08:00
|
|
|
/*
|
2021-03-11 21:27:36 +08:00
|
|
|
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2020-03-23 12:40:47 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <openssl/core_names.h>
|
|
|
|
#include <openssl/params.h>
|
2020-04-24 22:32:34 +08:00
|
|
|
#include <openssl/ec.h>
|
|
|
|
#include <openssl/err.h>
|
2020-03-23 12:40:47 +08:00
|
|
|
#include "crypto/ecx.h"
|
|
|
|
#include "ecx_backend.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The intention with the "backend" source file is to offer backend support
|
|
|
|
* for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
|
|
|
|
* implementations alike.
|
|
|
|
*/
|
|
|
|
|
2021-02-18 18:27:26 +08:00
|
|
|
int ossl_ecx_public_from_private(ECX_KEY *key)
|
2020-04-24 22:32:34 +08:00
|
|
|
{
|
|
|
|
switch (key->type) {
|
|
|
|
case ECX_KEY_TYPE_X25519:
|
|
|
|
X25519_public_from_private(key->pubkey, key->privkey);
|
|
|
|
break;
|
|
|
|
case ECX_KEY_TYPE_ED25519:
|
2020-09-22 13:51:49 +08:00
|
|
|
if (!ED25519_public_from_private(key->libctx, key->pubkey, key->privkey,
|
|
|
|
key->propq)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
|
2020-04-24 22:32:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ECX_KEY_TYPE_X448:
|
|
|
|
X448_public_from_private(key->pubkey, key->privkey);
|
|
|
|
break;
|
|
|
|
case ECX_KEY_TYPE_ED448:
|
2020-09-22 13:51:49 +08:00
|
|
|
if (!ED448_public_from_private(key->libctx, key->pubkey, key->privkey,
|
|
|
|
key->propq)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EC, EC_R_FAILED_MAKING_PUBLIC_KEY);
|
2020-04-24 22:32:34 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-02-18 18:27:26 +08:00
|
|
|
int ossl_ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
|
|
|
|
int include_private)
|
2020-03-23 12:40:47 +08:00
|
|
|
{
|
2020-04-24 22:32:34 +08:00
|
|
|
size_t privkeylen = 0, pubkeylen = 0;
|
2020-03-23 12:40:47 +08:00
|
|
|
const OSSL_PARAM *param_priv_key = NULL, *param_pub_key;
|
|
|
|
unsigned char *pubkey;
|
|
|
|
|
|
|
|
if (ecx == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
|
|
|
|
if (include_private)
|
|
|
|
param_priv_key =
|
|
|
|
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
|
2020-04-24 22:32:34 +08:00
|
|
|
|
|
|
|
if (param_pub_key == NULL && param_priv_key == NULL)
|
2020-03-23 12:40:47 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (param_priv_key != NULL
|
|
|
|
&& !OSSL_PARAM_get_octet_string(param_priv_key,
|
|
|
|
(void **)&ecx->privkey, ecx->keylen,
|
|
|
|
&privkeylen))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pubkey = ecx->pubkey;
|
2020-04-24 22:32:34 +08:00
|
|
|
if (param_pub_key != NULL
|
|
|
|
&& !OSSL_PARAM_get_octet_string(param_pub_key,
|
|
|
|
(void **)&pubkey,
|
|
|
|
sizeof(ecx->pubkey), &pubkeylen))
|
2020-03-23 12:40:47 +08:00
|
|
|
return 0;
|
|
|
|
|
2020-04-24 22:32:34 +08:00
|
|
|
if ((param_pub_key != NULL && pubkeylen != ecx->keylen)
|
2020-03-23 12:40:47 +08:00
|
|
|
|| (param_priv_key != NULL && privkeylen != ecx->keylen))
|
|
|
|
return 0;
|
|
|
|
|
2021-02-18 18:27:26 +08:00
|
|
|
if (param_pub_key == NULL && !ossl_ecx_public_from_private(ecx))
|
2020-04-24 22:32:34 +08:00
|
|
|
return 0;
|
|
|
|
|
2020-03-23 12:40:47 +08:00
|
|
|
ecx->haspubkey = 1;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|