2 * Copyright 2020-2021 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
11 #include <openssl/err.h>
12 #include <openssl/proverr.h>
13 #include "crypto/ecx.h"
14 #include "internal/common.h" /* for ossl_assert() */
17 # include "s390x_arch.h"
20 ECX_KEY
*ossl_ecx_key_new(OSSL_LIB_CTX
*libctx
, ECX_KEY_TYPE type
, int haspubkey
,
23 ECX_KEY
*ret
= OPENSSL_zalloc(sizeof(*ret
));
29 ret
->haspubkey
= haspubkey
;
31 case ECX_KEY_TYPE_X25519
:
32 ret
->keylen
= X25519_KEYLEN
;
34 case ECX_KEY_TYPE_X448
:
35 ret
->keylen
= X448_KEYLEN
;
37 case ECX_KEY_TYPE_ED25519
:
38 ret
->keylen
= ED25519_KEYLEN
;
40 case ECX_KEY_TYPE_ED448
:
41 ret
->keylen
= ED448_KEYLEN
;
46 if (!CRYPTO_NEW_REF(&ret
->references
, 1))
50 ret
->propq
= OPENSSL_strdup(propq
);
51 if (ret
->propq
== NULL
)
57 OPENSSL_free(ret
->propq
);
58 CRYPTO_FREE_REF(&ret
->references
);
64 void ossl_ecx_key_free(ECX_KEY
*key
)
71 CRYPTO_DOWN_REF(&key
->references
, &i
);
72 REF_PRINT_COUNT("ECX_KEY", key
);
75 REF_ASSERT_ISNT(i
< 0);
77 OPENSSL_free(key
->propq
);
78 OPENSSL_secure_clear_free(key
->privkey
, key
->keylen
);
79 CRYPTO_FREE_REF(&key
->references
);
83 void ossl_ecx_key_set0_libctx(ECX_KEY
*key
, OSSL_LIB_CTX
*libctx
)
88 int ossl_ecx_key_up_ref(ECX_KEY
*key
)
92 if (CRYPTO_UP_REF(&key
->references
, &i
) <= 0)
95 REF_PRINT_COUNT("ECX_KEY", key
);
96 REF_ASSERT_ISNT(i
< 2);
97 return ((i
> 1) ? 1 : 0);
100 unsigned char *ossl_ecx_key_allocate_privkey(ECX_KEY
*key
)
102 key
->privkey
= OPENSSL_secure_zalloc(key
->keylen
);
107 int ossl_ecx_compute_key(ECX_KEY
*peer
, ECX_KEY
*priv
, size_t keylen
,
108 unsigned char *secret
, size_t *secretlen
, size_t outlen
)
111 || priv
->privkey
== NULL
113 ERR_raise(ERR_LIB_PROV
, PROV_R_MISSING_KEY
);
117 if (!ossl_assert(keylen
== X25519_KEYLEN
118 || keylen
== X448_KEYLEN
)) {
119 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_KEY_LENGTH
);
123 if (secret
== NULL
) {
127 if (outlen
< keylen
) {
128 ERR_raise(ERR_LIB_PROV
, PROV_R_OUTPUT_BUFFER_TOO_SMALL
);
132 if (keylen
== X25519_KEYLEN
) {
134 if (OPENSSL_s390xcap_P
.pcc
[1]
135 & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X25519
)) {
136 if (s390x_x25519_mul(secret
, peer
->pubkey
, priv
->privkey
) == 0) {
137 ERR_raise(ERR_LIB_PROV
, PROV_R_FAILED_DURING_DERIVATION
);
142 if (ossl_x25519(secret
, priv
->privkey
, peer
->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
, peer
->pubkey
, priv
->privkey
) == 0) {
151 ERR_raise(ERR_LIB_PROV
, PROV_R_FAILED_DURING_DERIVATION
);
156 if (ossl_x448(secret
, priv
->privkey
, peer
->pubkey
) == 0) {
157 ERR_raise(ERR_LIB_PROV
, PROV_R_FAILED_DURING_DERIVATION
);