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 "encoder_local.h"
16 void ecx_get_new_free_import(ECX_KEY_TYPE type
,
17 OSSL_FUNC_keymgmt_new_fn
**ecx_new
,
18 OSSL_FUNC_keymgmt_free_fn
**ecx_free
,
19 OSSL_FUNC_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
);
29 } else if (type
== ECX_KEY_TYPE_ED25519
) {
30 *ecx_new
= ossl_prov_get_keymgmt_new(ed25519_keymgmt_functions
);
31 *ecx_free
= ossl_prov_get_keymgmt_free(ed25519_keymgmt_functions
);
32 *ecx_import
= ossl_prov_get_keymgmt_import(ed25519_keymgmt_functions
);
33 } else if (type
== ECX_KEY_TYPE_ED448
) {
34 *ecx_new
= ossl_prov_get_keymgmt_new(ed448_keymgmt_functions
);
35 *ecx_free
= ossl_prov_get_keymgmt_free(ed448_keymgmt_functions
);
36 *ecx_import
= ossl_prov_get_keymgmt_import(ed448_keymgmt_functions
);
45 int ossl_prov_print_ecx(BIO
*out
, ECX_KEY
*ecxkey
, enum ecx_print_type type
)
47 const char *type_label
= NULL
;
51 switch (ecxkey
->type
) {
52 case ECX_KEY_TYPE_X25519
:
53 type_label
= "X25519 Private-Key";
55 case ECX_KEY_TYPE_X448
:
56 type_label
= "X448 Private-Key";
58 case ECX_KEY_TYPE_ED25519
:
59 type_label
= "ED25519 Private-Key";
61 case ECX_KEY_TYPE_ED448
:
62 type_label
= "ED448 Private-Key";
67 switch (ecxkey
->type
) {
68 case ECX_KEY_TYPE_X25519
:
69 type_label
= "X25519 Public-Key";
71 case ECX_KEY_TYPE_X448
:
72 type_label
= "X448 Public-Key";
74 case ECX_KEY_TYPE_ED25519
:
75 type_label
= "ED25519 Public-Key";
77 case ECX_KEY_TYPE_ED448
:
78 type_label
= "ED448 Public-Key";
84 if (type
== ecx_print_priv
&& ecxkey
->privkey
== NULL
) {
85 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_NULL_PARAMETER
);
89 if (BIO_printf(out
, "%s:\n", type_label
) <= 0)
91 if (type
== ecx_print_priv
92 && !ossl_prov_print_labeled_buf(out
, "priv:", ecxkey
->privkey
,
95 if (!ossl_prov_print_labeled_buf(out
, "pub:", ecxkey
->pubkey
,
103 int ossl_prov_ecx_pub_to_der(const void *vecxkey
, unsigned char **pder
)
105 const ECX_KEY
*ecxkey
= vecxkey
;
106 unsigned char *keyblob
;
108 if (ecxkey
== NULL
) {
109 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_NULL_PARAMETER
);
113 keyblob
= OPENSSL_memdup(ecxkey
->pubkey
, ecxkey
->keylen
);
114 if (keyblob
== NULL
) {
115 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
120 return ecxkey
->keylen
;
123 int ossl_prov_ecx_priv_to_der(const void *vecxkey
, unsigned char **pder
)
125 const ECX_KEY
*ecxkey
= vecxkey
;
126 ASN1_OCTET_STRING oct
;
129 if (ecxkey
== NULL
|| ecxkey
->privkey
== NULL
) {
130 ERR_raise(ERR_LIB_PROV
, ERR_R_PASSED_NULL_PARAMETER
);
134 oct
.data
= ecxkey
->privkey
;
135 oct
.length
= ecxkey
->keylen
;
138 keybloblen
= i2d_ASN1_OCTET_STRING(&oct
, pder
);
139 if (keybloblen
< 0) {
140 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);