]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/serializers/serializer_ecx.c
ef16d97b97a7f5ff6e244d47f07767f64b1aef5c
[thirdparty/openssl.git] / providers / implementations / serializers / serializer_ecx.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/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"
15
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)
20 {
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);
37 } else {
38 *ecx_new = NULL;
39 *ecx_free = NULL;
40 *ecx_import = NULL;
41 }
42 }
43
44
45 int ossl_prov_print_ecx(BIO *out, ECX_KEY *ecxkey, enum ecx_print_type type)
46 {
47 const char *type_label = NULL;
48
49 switch (type) {
50 case ecx_print_priv:
51 switch (ecxkey->type) {
52 case ECX_KEY_TYPE_X25519:
53 type_label = "X25519 Private-Key";
54 break;
55 case ECX_KEY_TYPE_X448:
56 type_label = "X448 Private-Key";
57 break;
58 case ECX_KEY_TYPE_ED25519:
59 type_label = "ED25519 Private-Key";
60 break;
61 case ECX_KEY_TYPE_ED448:
62 type_label = "ED448 Private-Key";
63 break;
64 }
65 break;
66 case ecx_print_pub:
67 switch (ecxkey->type) {
68 case ECX_KEY_TYPE_X25519:
69 type_label = "X25519 Public-Key";
70 break;
71 case ECX_KEY_TYPE_X448:
72 type_label = "X448 Public-Key";
73 break;
74 case ECX_KEY_TYPE_ED25519:
75 type_label = "ED25519 Public-Key";
76 break;
77 case ECX_KEY_TYPE_ED448:
78 type_label = "ED448 Public-Key";
79 break;
80 }
81 break;
82 }
83
84 if (type == ecx_print_priv && ecxkey->privkey == NULL) {
85 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
86 return 0;
87 }
88
89 if (BIO_printf(out, "%s:\n", type_label) <= 0)
90 return 0;
91 if (type == ecx_print_priv
92 && !ossl_prov_print_labeled_buf(out, "priv:", ecxkey->privkey,
93 ecxkey->keylen))
94 return 0;
95 if (!ossl_prov_print_labeled_buf(out, "pub:", ecxkey->pubkey,
96 ecxkey->keylen))
97 return 0;
98
99 return 1;
100 }
101
102
103 int ossl_prov_ecx_pub_to_der(const void *vecxkey, unsigned char **pder)
104 {
105 const ECX_KEY *ecxkey = vecxkey;
106 unsigned char *keyblob;
107
108 if (ecxkey == NULL) {
109 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
110 return 0;
111 }
112
113 keyblob = OPENSSL_memdup(ecxkey->pubkey, ecxkey->keylen);
114 if (keyblob == NULL) {
115 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
116 return 0;
117 }
118
119 *pder = keyblob;
120 return ecxkey->keylen;
121 }
122
123 int ossl_prov_ecx_priv_to_der(const void *vecxkey, unsigned char **pder)
124 {
125 const ECX_KEY *ecxkey = vecxkey;
126 ASN1_OCTET_STRING oct;
127 int keybloblen;
128
129 if (ecxkey == NULL || ecxkey->privkey == NULL) {
130 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
131 return 0;
132 }
133
134 oct.data = ecxkey->privkey;
135 oct.length = ecxkey->keylen;
136 oct.flags = 0;
137
138 keybloblen = i2d_ASN1_OCTET_STRING(&oct, pder);
139 if (keybloblen < 0) {
140 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
141 return 0;
142 }
143
144 return keybloblen;
145 }