2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/err.h>
11 #include "crypto/ecx.h"
12 #include "prov/bio.h" /* ossl_prov_bio_printf() */
13 #include "prov/implementations.h" /* ecx_keymgmt_functions */
14 #include "serializer_local.h"
16 void ecx_get_new_free_import(ECX_KEY_TYPE type,
17 OSSL_OP_keymgmt_new_fn **ecx_new,
18 OSSL_OP_keymgmt_free_fn **ecx_free,
19 OSSL_OP_keymgmt_import_fn **ecx_import)
21 if (type == ECX_KEY_TYPE_X25519) {
22 *ecx_new = ossl_prov_get_keymgmt_new(x25519_keymgmt_functions);
23 *ecx_free = ossl_prov_get_keymgmt_free(x25519_keymgmt_functions);
24 *ecx_import = ossl_prov_get_keymgmt_import(x25519_keymgmt_functions);
25 } else if (type == ECX_KEY_TYPE_X448) {
26 *ecx_new = ossl_prov_get_keymgmt_new(x448_keymgmt_functions);
27 *ecx_free = ossl_prov_get_keymgmt_free(x448_keymgmt_functions);
28 *ecx_import = ossl_prov_get_keymgmt_import(x448_keymgmt_functions);
37 int ossl_prov_print_ecx(BIO *out, ECX_KEY *ecxkey, enum ecx_print_type type)
39 const char *type_label = NULL;
43 switch (ecxkey->keylen) {
45 type_label = "X25519 Private-Key";
48 type_label = "X448 Private-Key";
53 switch (ecxkey->keylen) {
55 type_label = "X25519 Public-Key";
58 type_label = "X448 Public-Key";
64 if (type == ecx_print_priv && ecxkey->privkey == NULL) {
65 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
69 if (ossl_prov_bio_printf(out, "%s:\n", type_label) <= 0)
71 if (type == ecx_print_priv
72 && !ossl_prov_print_labeled_buf(out, "priv:", ecxkey->privkey,
75 if (!ossl_prov_print_labeled_buf(out, "pub:", ecxkey->pubkey,
83 int ossl_prov_ecx_pub_to_der(const void *vecxkey, unsigned char **pder)
85 const ECX_KEY *ecxkey = vecxkey;
86 unsigned char *keyblob;
89 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
93 keyblob = OPENSSL_memdup(ecxkey->pubkey, ecxkey->keylen);
94 if (keyblob == NULL) {
95 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
100 return ecxkey->keylen;
103 int ossl_prov_ecx_priv_to_der(const void *vecxkey, unsigned char **pder)
105 const ECX_KEY *ecxkey = vecxkey;
106 ASN1_OCTET_STRING oct;
109 if (ecxkey == NULL || ecxkey->privkey == NULL) {
110 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
114 oct.data = ecxkey->privkey;
115 oct.length = ecxkey->keylen;
118 keybloblen = i2d_ASN1_OCTET_STRING(&oct, pder);
119 if (keybloblen < 0) {
120 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);