Implement the ECX Serializers
[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_OP_keymgmt_new_fn **ecx_new,
18                              OSSL_OP_keymgmt_free_fn **ecx_free,
19                              OSSL_OP_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 {
30         *ecx_new = NULL;
31         *ecx_free = NULL;
32         *ecx_import = NULL;
33     }
34 }
35
36
37 int ossl_prov_print_ecx(BIO *out, ECX_KEY *ecxkey, enum ecx_print_type type)
38 {
39     const char *type_label = NULL;
40
41     switch (type) {
42     case ecx_print_priv:
43         switch (ecxkey->keylen) {
44         case X25519_KEYLEN:
45             type_label = "X25519 Private-Key";
46             break;
47         case X448_KEYLEN:
48             type_label = "X448 Private-Key";
49             break;
50         }
51         break;
52     case ecx_print_pub:
53         switch (ecxkey->keylen) {
54         case X25519_KEYLEN:
55             type_label = "X25519 Public-Key";
56             break;
57         case X448_KEYLEN:
58             type_label = "X448 Public-Key";
59             break;
60         }
61         break;
62     }
63
64     if (type == ecx_print_priv && ecxkey->privkey == NULL) {
65         ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
66         return 0;
67     }
68
69     if (ossl_prov_bio_printf(out, "%s:\n", type_label) <= 0)
70         return 0;
71     if (type == ecx_print_priv
72             && !ossl_prov_print_labeled_buf(out, "priv:", ecxkey->privkey,
73                                             ecxkey->keylen))
74         return 0;
75     if (!ossl_prov_print_labeled_buf(out, "pub:", ecxkey->pubkey,
76                                      ecxkey->keylen))
77         return 0;
78
79     return 1;
80 }
81
82
83 int ossl_prov_ecx_pub_to_der(const void *vecxkey, unsigned char **pder)
84 {
85     const ECX_KEY *ecxkey = vecxkey;
86     unsigned char *keyblob;
87
88     if (ecxkey == NULL) {
89         ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
90         return 0;
91     }
92
93     keyblob = OPENSSL_memdup(ecxkey->pubkey, ecxkey->keylen);
94     if (keyblob == NULL) {
95         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
96         return 0;
97     }
98
99     *pder = keyblob;
100     return ecxkey->keylen;
101 }
102
103 int ossl_prov_ecx_priv_to_der(const void *vecxkey, unsigned char **pder)
104 {
105     const ECX_KEY *ecxkey = vecxkey;
106     ASN1_OCTET_STRING oct;
107     int keybloblen;
108
109     if (ecxkey == NULL || ecxkey->privkey == NULL) {
110         ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
111         return 0;
112     }
113
114     oct.data = ecxkey->privkey;
115     oct.length = ecxkey->keylen;
116     oct.flags = 0;
117
118     keybloblen = i2d_ASN1_OCTET_STRING(&oct, pder);
119     if (keybloblen < 0) {
120         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
121         return 0;
122     }
123
124     return keybloblen;
125 }