]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/exchange/ecx_exch.c
Add HPKE DHKEM provider support for EC, X25519 and X448.
[thirdparty/openssl.git] / providers / implementations / exchange / ecx_exch.c
1 /*
2 * Copyright 2020-2021 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/crypto.h>
11 #include <openssl/core_dispatch.h>
12 #include <openssl/core_names.h>
13 #include <openssl/params.h>
14 #include <openssl/err.h>
15 #include <openssl/proverr.h>
16 #include "internal/cryptlib.h"
17 #include "crypto/ecx.h"
18 #include "prov/implementations.h"
19 #include "prov/providercommon.h"
20
21 static OSSL_FUNC_keyexch_newctx_fn x25519_newctx;
22 static OSSL_FUNC_keyexch_newctx_fn x448_newctx;
23 static OSSL_FUNC_keyexch_init_fn ecx_init;
24 static OSSL_FUNC_keyexch_set_peer_fn ecx_set_peer;
25 static OSSL_FUNC_keyexch_derive_fn ecx_derive;
26 static OSSL_FUNC_keyexch_freectx_fn ecx_freectx;
27 static OSSL_FUNC_keyexch_dupctx_fn ecx_dupctx;
28
29 /*
30 * What's passed as an actual key is defined by the KEYMGMT interface.
31 * We happen to know that our KEYMGMT simply passes ECX_KEY structures, so
32 * we use that here too.
33 */
34
35 typedef struct {
36 size_t keylen;
37 ECX_KEY *key;
38 ECX_KEY *peerkey;
39 } PROV_ECX_CTX;
40
41 static void *ecx_newctx(void *provctx, size_t keylen)
42 {
43 PROV_ECX_CTX *ctx;
44
45 if (!ossl_prov_is_running())
46 return NULL;
47
48 ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX));
49 if (ctx == NULL) {
50 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
51 return NULL;
52 }
53
54 ctx->keylen = keylen;
55
56 return ctx;
57 }
58
59 static void *x25519_newctx(void *provctx)
60 {
61 return ecx_newctx(provctx, X25519_KEYLEN);
62 }
63
64 static void *x448_newctx(void *provctx)
65 {
66 return ecx_newctx(provctx, X448_KEYLEN);
67 }
68
69 static int ecx_init(void *vecxctx, void *vkey,
70 ossl_unused const OSSL_PARAM params[])
71 {
72 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
73 ECX_KEY *key = vkey;
74
75 if (!ossl_prov_is_running())
76 return 0;
77
78 if (ecxctx == NULL
79 || key == NULL
80 || key->keylen != ecxctx->keylen
81 || !ossl_ecx_key_up_ref(key)) {
82 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
83 return 0;
84 }
85
86 ossl_ecx_key_free(ecxctx->key);
87 ecxctx->key = key;
88
89 return 1;
90 }
91
92 static int ecx_set_peer(void *vecxctx, void *vkey)
93 {
94 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
95 ECX_KEY *key = vkey;
96
97 if (!ossl_prov_is_running())
98 return 0;
99
100 if (ecxctx == NULL
101 || key == NULL
102 || key->keylen != ecxctx->keylen
103 || !ossl_ecx_key_up_ref(key)) {
104 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
105 return 0;
106 }
107 ossl_ecx_key_free(ecxctx->peerkey);
108 ecxctx->peerkey = key;
109
110 return 1;
111 }
112
113 static int ecx_derive(void *vecxctx, unsigned char *secret, size_t *secretlen,
114 size_t outlen)
115 {
116 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
117
118 if (!ossl_prov_is_running())
119 return 0;
120 return ossl_ecx_compute_key(ecxctx->peerkey, ecxctx->key, ecxctx->keylen,
121 secret, secretlen, outlen);
122 }
123
124 static void ecx_freectx(void *vecxctx)
125 {
126 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
127
128 ossl_ecx_key_free(ecxctx->key);
129 ossl_ecx_key_free(ecxctx->peerkey);
130
131 OPENSSL_free(ecxctx);
132 }
133
134 static void *ecx_dupctx(void *vecxctx)
135 {
136 PROV_ECX_CTX *srcctx = (PROV_ECX_CTX *)vecxctx;
137 PROV_ECX_CTX *dstctx;
138
139 if (!ossl_prov_is_running())
140 return NULL;
141
142 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
143 if (dstctx == NULL) {
144 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
145 return NULL;
146 }
147
148 *dstctx = *srcctx;
149 if (dstctx->key != NULL && !ossl_ecx_key_up_ref(dstctx->key)) {
150 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
151 OPENSSL_free(dstctx);
152 return NULL;
153 }
154
155 if (dstctx->peerkey != NULL && !ossl_ecx_key_up_ref(dstctx->peerkey)) {
156 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
157 ossl_ecx_key_free(dstctx->key);
158 OPENSSL_free(dstctx);
159 return NULL;
160 }
161
162 return dstctx;
163 }
164
165 const OSSL_DISPATCH ossl_x25519_keyexch_functions[] = {
166 { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x25519_newctx },
167 { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init },
168 { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
169 { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
170 { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
171 { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
172 { 0, NULL }
173 };
174
175 const OSSL_DISPATCH ossl_x448_keyexch_functions[] = {
176 { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x448_newctx },
177 { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init },
178 { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
179 { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
180 { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
181 { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
182 { 0, NULL }
183 };