2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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
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"
20 # include "s390x_arch.h"
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
;
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.
43 static void *ecx_newctx(void *provctx
, size_t keylen
)
45 PROV_ECX_CTX
*ctx
= OPENSSL_zalloc(sizeof(PROV_ECX_CTX
));
48 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
57 static void *x25519_newctx(void *provctx
)
59 return ecx_newctx(provctx
, X25519_KEYLEN
);
62 static void *x448_newctx(void *provctx
)
64 return ecx_newctx(provctx
, X448_KEYLEN
);
67 static int ecx_init(void *vecxctx
, void *vkey
)
69 PROV_ECX_CTX
*ecxctx
= (PROV_ECX_CTX
*)vecxctx
;
74 || key
->keylen
!= ecxctx
->keylen
75 || !ecx_key_up_ref(key
)) {
76 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
80 ecx_key_free(ecxctx
->key
);
86 static int ecx_set_peer(void *vecxctx
, void *vkey
)
88 PROV_ECX_CTX
*ecxctx
= (PROV_ECX_CTX
*)vecxctx
;
93 || key
->keylen
!= ecxctx
->keylen
94 || !ecx_key_up_ref(key
)) {
95 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
98 ecx_key_free(ecxctx
->peerkey
);
99 ecxctx
->peerkey
= key
;
104 static int ecx_derive(void *vecxctx
, unsigned char *secret
, size_t *secretlen
,
107 PROV_ECX_CTX
*ecxctx
= (PROV_ECX_CTX
*)vecxctx
;
109 if (ecxctx
->key
== NULL
110 || ecxctx
->key
->privkey
== NULL
111 || ecxctx
->peerkey
== NULL
) {
112 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY
);
116 if (!ossl_assert(ecxctx
->keylen
== X25519_KEYLEN
117 || ecxctx
->keylen
== X448_KEYLEN
)) {
118 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY_LENGTH
);
122 if (secret
== NULL
) {
123 *secretlen
= ecxctx
->keylen
;
126 if (outlen
< ecxctx
->keylen
) {
127 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY_LENGTH
);
131 if (ecxctx
->keylen
== X25519_KEYLEN
) {
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
);
142 if (X25519(secret
, ecxctx
->key
->privkey
, ecxctx
->peerkey
->pubkey
) == 0) {
143 ERR_raise(ERR_LIB_PROV
, PROV_R_FAILED_DURING_DERIVATION
);
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
);
157 if (X448(secret
, ecxctx
->key
->privkey
, ecxctx
->peerkey
->pubkey
) == 0) {
158 ERR_raise(ERR_LIB_PROV
, PROV_R_FAILED_DURING_DERIVATION
);
163 *secretlen
= ecxctx
->keylen
;
167 static void ecx_freectx(void *vecxctx
)
169 PROV_ECX_CTX
*ecxctx
= (PROV_ECX_CTX
*)vecxctx
;
171 ecx_key_free(ecxctx
->key
);
172 ecx_key_free(ecxctx
->peerkey
);
174 OPENSSL_free(ecxctx
);
177 static void *ecx_dupctx(void *vecxctx
)
179 PROV_ECX_CTX
*srcctx
= (PROV_ECX_CTX
*)vecxctx
;
180 PROV_ECX_CTX
*dstctx
;
182 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
183 if (dstctx
== NULL
) {
184 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
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
);
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
);
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
},
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
},