]>
Commit | Line | Data |
---|---|---|
6f7d2135 | 1 | /* |
da1c088f | 2 | * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved. |
6f7d2135 MC |
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> | |
23c48d94 | 11 | #include <openssl/core_dispatch.h> |
6f7d2135 MC |
12 | #include <openssl/core_names.h> |
13 | #include <openssl/params.h> | |
14 | #include <openssl/err.h> | |
2741128e | 15 | #include <openssl/proverr.h> |
6f7d2135 MC |
16 | #include "internal/cryptlib.h" |
17 | #include "crypto/ecx.h" | |
18 | #include "prov/implementations.h" | |
ca94057f | 19 | #include "prov/providercommon.h" |
6f7d2135 | 20 | |
363b1e5d DMSP |
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; | |
6f7d2135 MC |
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 | { | |
ca94057f | 43 | PROV_ECX_CTX *ctx; |
6f7d2135 | 44 | |
ca94057f P |
45 | if (!ossl_prov_is_running()) |
46 | return NULL; | |
47 | ||
48 | ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX)); | |
e077455e | 49 | if (ctx == NULL) |
6f7d2135 | 50 | return NULL; |
6f7d2135 MC |
51 | |
52 | ctx->keylen = keylen; | |
53 | ||
54 | return ctx; | |
55 | } | |
56 | ||
57 | static void *x25519_newctx(void *provctx) | |
58 | { | |
59 | return ecx_newctx(provctx, X25519_KEYLEN); | |
60 | } | |
61 | ||
62 | static void *x448_newctx(void *provctx) | |
63 | { | |
64 | return ecx_newctx(provctx, X448_KEYLEN); | |
65 | } | |
66 | ||
2b2f4f9b P |
67 | static int ecx_init(void *vecxctx, void *vkey, |
68 | ossl_unused const OSSL_PARAM params[]) | |
6f7d2135 MC |
69 | { |
70 | PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx; | |
71 | ECX_KEY *key = vkey; | |
72 | ||
ca94057f P |
73 | if (!ossl_prov_is_running()) |
74 | return 0; | |
75 | ||
6f7d2135 MC |
76 | if (ecxctx == NULL |
77 | || key == NULL | |
78 | || key->keylen != ecxctx->keylen | |
32ab57cb | 79 | || !ossl_ecx_key_up_ref(key)) { |
6f7d2135 MC |
80 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
81 | return 0; | |
82 | } | |
83 | ||
32ab57cb | 84 | ossl_ecx_key_free(ecxctx->key); |
6f7d2135 MC |
85 | ecxctx->key = key; |
86 | ||
87 | return 1; | |
88 | } | |
89 | ||
90 | static int ecx_set_peer(void *vecxctx, void *vkey) | |
91 | { | |
92 | PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx; | |
93 | ECX_KEY *key = vkey; | |
94 | ||
ca94057f P |
95 | if (!ossl_prov_is_running()) |
96 | return 0; | |
97 | ||
6f7d2135 MC |
98 | if (ecxctx == NULL |
99 | || key == NULL | |
100 | || key->keylen != ecxctx->keylen | |
32ab57cb | 101 | || !ossl_ecx_key_up_ref(key)) { |
6f7d2135 MC |
102 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
103 | return 0; | |
104 | } | |
32ab57cb | 105 | ossl_ecx_key_free(ecxctx->peerkey); |
6f7d2135 MC |
106 | ecxctx->peerkey = key; |
107 | ||
108 | return 1; | |
109 | } | |
110 | ||
111 | static int ecx_derive(void *vecxctx, unsigned char *secret, size_t *secretlen, | |
112 | size_t outlen) | |
113 | { | |
114 | PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx; | |
115 | ||
ca94057f P |
116 | if (!ossl_prov_is_running()) |
117 | return 0; | |
78c44b05 | 118 | return ossl_ecx_compute_key(ecxctx->peerkey, ecxctx->key, ecxctx->keylen, |
119 | secret, secretlen, outlen); | |
6f7d2135 MC |
120 | } |
121 | ||
122 | static void ecx_freectx(void *vecxctx) | |
123 | { | |
124 | PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx; | |
125 | ||
32ab57cb SL |
126 | ossl_ecx_key_free(ecxctx->key); |
127 | ossl_ecx_key_free(ecxctx->peerkey); | |
6f7d2135 MC |
128 | |
129 | OPENSSL_free(ecxctx); | |
130 | } | |
131 | ||
132 | static void *ecx_dupctx(void *vecxctx) | |
133 | { | |
134 | PROV_ECX_CTX *srcctx = (PROV_ECX_CTX *)vecxctx; | |
135 | PROV_ECX_CTX *dstctx; | |
136 | ||
ca94057f P |
137 | if (!ossl_prov_is_running()) |
138 | return NULL; | |
139 | ||
6f7d2135 | 140 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
e077455e | 141 | if (dstctx == NULL) |
6f7d2135 | 142 | return NULL; |
6f7d2135 MC |
143 | |
144 | *dstctx = *srcctx; | |
32ab57cb | 145 | if (dstctx->key != NULL && !ossl_ecx_key_up_ref(dstctx->key)) { |
6f7d2135 MC |
146 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
147 | OPENSSL_free(dstctx); | |
148 | return NULL; | |
149 | } | |
150 | ||
32ab57cb | 151 | if (dstctx->peerkey != NULL && !ossl_ecx_key_up_ref(dstctx->peerkey)) { |
6f7d2135 | 152 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
32ab57cb | 153 | ossl_ecx_key_free(dstctx->key); |
6f7d2135 MC |
154 | OPENSSL_free(dstctx); |
155 | return NULL; | |
156 | } | |
157 | ||
158 | return dstctx; | |
159 | } | |
160 | ||
1be63951 | 161 | const OSSL_DISPATCH ossl_x25519_keyexch_functions[] = { |
6f7d2135 MC |
162 | { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x25519_newctx }, |
163 | { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init }, | |
164 | { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive }, | |
165 | { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer }, | |
166 | { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx }, | |
167 | { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx }, | |
1e6bd31e | 168 | OSSL_DISPATCH_END |
6f7d2135 MC |
169 | }; |
170 | ||
1be63951 | 171 | const OSSL_DISPATCH ossl_x448_keyexch_functions[] = { |
6f7d2135 MC |
172 | { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x448_newctx }, |
173 | { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init }, | |
174 | { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive }, | |
175 | { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer }, | |
176 | { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx }, | |
177 | { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx }, | |
1e6bd31e | 178 | OSSL_DISPATCH_END |
6f7d2135 | 179 | }; |