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/providercommon.h"
19 #include "prov/providercommonerr.h"
21 # include "s390x_arch.h"
24 static OSSL_FUNC_keyexch_newctx_fn x25519_newctx
;
25 static OSSL_FUNC_keyexch_newctx_fn x448_newctx
;
26 static OSSL_FUNC_keyexch_init_fn ecx_init
;
27 static OSSL_FUNC_keyexch_set_peer_fn ecx_set_peer
;
28 static OSSL_FUNC_keyexch_derive_fn ecx_derive
;
29 static OSSL_FUNC_keyexch_freectx_fn ecx_freectx
;
30 static OSSL_FUNC_keyexch_dupctx_fn ecx_dupctx
;
33 * What's passed as an actual key is defined by the KEYMGMT interface.
34 * We happen to know that our KEYMGMT simply passes ECX_KEY structures, so
35 * we use that here too.
44 static void *ecx_newctx(void *provctx
, size_t keylen
)
48 if (!ossl_prov_is_running())
51 ctx
= OPENSSL_zalloc(sizeof(PROV_ECX_CTX
));
53 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
62 static void *x25519_newctx(void *provctx
)
64 return ecx_newctx(provctx
, X25519_KEYLEN
);
67 static void *x448_newctx(void *provctx
)
69 return ecx_newctx(provctx
, X448_KEYLEN
);
72 static int ecx_init(void *vecxctx
, void *vkey
)
74 PROV_ECX_CTX
*ecxctx
= (PROV_ECX_CTX
*)vecxctx
;
77 if (!ossl_prov_is_running())
82 || key
->keylen
!= ecxctx
->keylen
83 || !ecx_key_up_ref(key
)) {
84 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
88 ecx_key_free(ecxctx
->key
);
94 static int ecx_set_peer(void *vecxctx
, void *vkey
)
96 PROV_ECX_CTX
*ecxctx
= (PROV_ECX_CTX
*)vecxctx
;
99 if (!ossl_prov_is_running())
104 || key
->keylen
!= ecxctx
->keylen
105 || !ecx_key_up_ref(key
)) {
106 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
109 ecx_key_free(ecxctx
->peerkey
);
110 ecxctx
->peerkey
= key
;
115 static int ecx_derive(void *vecxctx
, unsigned char *secret
, size_t *secretlen
,
118 PROV_ECX_CTX
*ecxctx
= (PROV_ECX_CTX
*)vecxctx
;
120 if (!ossl_prov_is_running())
123 if (ecxctx
->key
== NULL
124 || ecxctx
->key
->privkey
== NULL
125 || ecxctx
->peerkey
== NULL
) {
126 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY
);
130 if (!ossl_assert(ecxctx
->keylen
== X25519_KEYLEN
131 || ecxctx
->keylen
== X448_KEYLEN
)) {
132 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY_LENGTH
);
136 if (secret
== NULL
) {
137 *secretlen
= ecxctx
->keylen
;
140 if (outlen
< ecxctx
->keylen
) {
141 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY_LENGTH
);
145 if (ecxctx
->keylen
== X25519_KEYLEN
) {
147 if (OPENSSL_s390xcap_P
.pcc
[1]
148 & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X25519
)) {
149 if (s390x_x25519_mul(secret
, ecxctx
->peerkey
->pubkey
,
150 ecxctx
->key
->privkey
) == 0) {
151 ERR_raise(ERR_LIB_PROV
, PROV_R_FAILED_DURING_DERIVATION
);
156 if (X25519(secret
, ecxctx
->key
->privkey
, ecxctx
->peerkey
->pubkey
) == 0) {
157 ERR_raise(ERR_LIB_PROV
, PROV_R_FAILED_DURING_DERIVATION
);
162 if (OPENSSL_s390xcap_P
.pcc
[1]
163 & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X448
)) {
164 if (s390x_x448_mul(secret
, ecxctx
->peerkey
->pubkey
,
165 ecxctx
->key
->privkey
) == 0) {
166 ERR_raise(ERR_LIB_PROV
, PROV_R_FAILED_DURING_DERIVATION
);
171 if (X448(secret
, ecxctx
->key
->privkey
, ecxctx
->peerkey
->pubkey
) == 0) {
172 ERR_raise(ERR_LIB_PROV
, PROV_R_FAILED_DURING_DERIVATION
);
177 *secretlen
= ecxctx
->keylen
;
181 static void ecx_freectx(void *vecxctx
)
183 PROV_ECX_CTX
*ecxctx
= (PROV_ECX_CTX
*)vecxctx
;
185 ecx_key_free(ecxctx
->key
);
186 ecx_key_free(ecxctx
->peerkey
);
188 OPENSSL_free(ecxctx
);
191 static void *ecx_dupctx(void *vecxctx
)
193 PROV_ECX_CTX
*srcctx
= (PROV_ECX_CTX
*)vecxctx
;
194 PROV_ECX_CTX
*dstctx
;
196 if (!ossl_prov_is_running())
199 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
200 if (dstctx
== NULL
) {
201 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
206 if (dstctx
->key
!= NULL
&& !ecx_key_up_ref(dstctx
->key
)) {
207 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
208 OPENSSL_free(dstctx
);
212 if (dstctx
->peerkey
!= NULL
&& !ecx_key_up_ref(dstctx
->peerkey
)) {
213 ERR_raise(ERR_LIB_PROV
, ERR_R_INTERNAL_ERROR
);
214 ecx_key_free(dstctx
->key
);
215 OPENSSL_free(dstctx
);
222 const OSSL_DISPATCH ossl_x25519_keyexch_functions
[] = {
223 { OSSL_FUNC_KEYEXCH_NEWCTX
, (void (*)(void))x25519_newctx
},
224 { OSSL_FUNC_KEYEXCH_INIT
, (void (*)(void))ecx_init
},
225 { OSSL_FUNC_KEYEXCH_DERIVE
, (void (*)(void))ecx_derive
},
226 { OSSL_FUNC_KEYEXCH_SET_PEER
, (void (*)(void))ecx_set_peer
},
227 { OSSL_FUNC_KEYEXCH_FREECTX
, (void (*)(void))ecx_freectx
},
228 { OSSL_FUNC_KEYEXCH_DUPCTX
, (void (*)(void))ecx_dupctx
},
232 const OSSL_DISPATCH ossl_x448_keyexch_functions
[] = {
233 { OSSL_FUNC_KEYEXCH_NEWCTX
, (void (*)(void))x448_newctx
},
234 { OSSL_FUNC_KEYEXCH_INIT
, (void (*)(void))ecx_init
},
235 { OSSL_FUNC_KEYEXCH_DERIVE
, (void (*)(void))ecx_derive
},
236 { OSSL_FUNC_KEYEXCH_SET_PEER
, (void (*)(void))ecx_set_peer
},
237 { OSSL_FUNC_KEYEXCH_FREECTX
, (void (*)(void))ecx_freectx
},
238 { OSSL_FUNC_KEYEXCH_DUPCTX
, (void (*)(void))ecx_dupctx
},