]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/exchange/ecx_exch.c
Make the naming scheme for dispatched functions more consistent
[thirdparty/openssl.git] / providers / implementations / exchange / ecx_exch.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/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 "internal/cryptlib.h"
16 #include "crypto/ecx.h"
17 #include "prov/implementations.h"
18 #include "prov/providercommonerr.h"
19 #ifdef S390X_EC_ASM
20 # include "s390x_arch.h"
21 #endif
22
23 static OSSL_FUNC_keyexch_newctx_fn x25519_newctx;
24 static OSSL_FUNC_keyexch_newctx_fn x448_newctx;
25 static OSSL_FUNC_keyexch_init_fn ecx_init;
26 static OSSL_FUNC_keyexch_set_peer_fn ecx_set_peer;
27 static OSSL_FUNC_keyexch_derive_fn ecx_derive;
28 static OSSL_FUNC_keyexch_freectx_fn ecx_freectx;
29 static OSSL_FUNC_keyexch_dupctx_fn ecx_dupctx;
30
31 /*
32 * What's passed as an actual key is defined by the KEYMGMT interface.
33 * We happen to know that our KEYMGMT simply passes ECX_KEY structures, so
34 * we use that here too.
35 */
36
37 typedef struct {
38 size_t keylen;
39 ECX_KEY *key;
40 ECX_KEY *peerkey;
41 } PROV_ECX_CTX;
42
43 static void *ecx_newctx(void *provctx, size_t keylen)
44 {
45 PROV_ECX_CTX *ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX));
46
47 if (ctx == NULL) {
48 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
49 return NULL;
50 }
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
67 static int ecx_init(void *vecxctx, void *vkey)
68 {
69 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
70 ECX_KEY *key = vkey;
71
72 if (ecxctx == NULL
73 || key == NULL
74 || key->keylen != ecxctx->keylen
75 || !ecx_key_up_ref(key)) {
76 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
77 return 0;
78 }
79
80 ecx_key_free(ecxctx->key);
81 ecxctx->key = key;
82
83 return 1;
84 }
85
86 static int ecx_set_peer(void *vecxctx, void *vkey)
87 {
88 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
89 ECX_KEY *key = vkey;
90
91 if (ecxctx == NULL
92 || key == NULL
93 || key->keylen != ecxctx->keylen
94 || !ecx_key_up_ref(key)) {
95 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
96 return 0;
97 }
98 ecx_key_free(ecxctx->peerkey);
99 ecxctx->peerkey = key;
100
101 return 1;
102 }
103
104 static int ecx_derive(void *vecxctx, unsigned char *secret, size_t *secretlen,
105 size_t outlen)
106 {
107 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
108
109 if (ecxctx->key == NULL
110 || ecxctx->key->privkey == NULL
111 || ecxctx->peerkey == NULL) {
112 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
113 return 0;
114 }
115
116 if (!ossl_assert(ecxctx->keylen == X25519_KEYLEN
117 || ecxctx->keylen == X448_KEYLEN)) {
118 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
119 return 0;
120 }
121
122 if (secret == NULL) {
123 *secretlen = ecxctx->keylen;
124 return 1;
125 }
126 if (outlen < ecxctx->keylen) {
127 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
128 return 0;
129 }
130
131 if (ecxctx->keylen == X25519_KEYLEN) {
132 #ifdef S390X_EC_ASM
133 if (OPENSSL_s390xcap_P.pcc[1]
134 & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X25519)) {
135 if (s390x_x25519_mul(secret, ecxctx->peerkey->pubkey,
136 ecxctx->key->privkey) == 0) {
137 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
138 return 0;
139 }
140 } else
141 #endif
142 if (X25519(secret, ecxctx->key->privkey, ecxctx->peerkey->pubkey) == 0) {
143 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
144 return 0;
145 }
146 } else {
147 #ifdef S390X_EC_ASM
148 if (OPENSSL_s390xcap_P.pcc[1]
149 & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X448)) {
150 if (s390x_x448_mul(secret, ecxctx->peerkey->pubkey,
151 ecxctx->key->privkey) == 0) {
152 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
153 return 0;
154 }
155 } else
156 #endif
157 if (X448(secret, ecxctx->key->privkey, ecxctx->peerkey->pubkey) == 0) {
158 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
159 return 0;
160 }
161 }
162
163 *secretlen = ecxctx->keylen;
164 return 1;
165 }
166
167 static void ecx_freectx(void *vecxctx)
168 {
169 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
170
171 ecx_key_free(ecxctx->key);
172 ecx_key_free(ecxctx->peerkey);
173
174 OPENSSL_free(ecxctx);
175 }
176
177 static void *ecx_dupctx(void *vecxctx)
178 {
179 PROV_ECX_CTX *srcctx = (PROV_ECX_CTX *)vecxctx;
180 PROV_ECX_CTX *dstctx;
181
182 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
183 if (dstctx == NULL) {
184 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
185 return NULL;
186 }
187
188 *dstctx = *srcctx;
189 if (dstctx->key != NULL && !ecx_key_up_ref(dstctx->key)) {
190 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
191 OPENSSL_free(dstctx);
192 return NULL;
193 }
194
195 if (dstctx->peerkey != NULL && !ecx_key_up_ref(dstctx->peerkey)) {
196 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
197 ecx_key_free(dstctx->key);
198 OPENSSL_free(dstctx);
199 return NULL;
200 }
201
202 return dstctx;
203 }
204
205 const OSSL_DISPATCH x25519_keyexch_functions[] = {
206 { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x25519_newctx },
207 { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init },
208 { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
209 { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
210 { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
211 { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
212 { 0, NULL }
213 };
214
215 const OSSL_DISPATCH x448_keyexch_functions[] = {
216 { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x448_newctx },
217 { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init },
218 { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
219 { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
220 { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
221 { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
222 { 0, NULL }
223 };