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_numbers.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 static OSSL_OP_keyexch_newctx_fn x25519_newctx;
21 static OSSL_OP_keyexch_newctx_fn x448_newctx;
22 static OSSL_OP_keyexch_init_fn ecx_init;
23 static OSSL_OP_keyexch_set_peer_fn ecx_set_peer;
24 static OSSL_OP_keyexch_derive_fn ecx_derive;
25 static OSSL_OP_keyexch_freectx_fn ecx_freectx;
26 static OSSL_OP_keyexch_dupctx_fn ecx_dupctx;
29 * What's passed as an actual key is defined by the KEYMGMT interface.
30 * We happen to know that our KEYMGMT simply passes ECX_KEY structures, so
31 * we use that here too.
40 static void *ecx_newctx(void *provctx, size_t keylen)
42 PROV_ECX_CTX *ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX));
45 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
54 static void *x25519_newctx(void *provctx)
56 return ecx_newctx(provctx, X25519_KEYLEN);
59 static void *x448_newctx(void *provctx)
61 return ecx_newctx(provctx, X448_KEYLEN);
64 static int ecx_init(void *vecxctx, void *vkey)
66 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
71 || key->keylen != ecxctx->keylen
72 || !ecx_key_up_ref(key)) {
73 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
77 ecx_key_free(ecxctx->key);
83 static int ecx_set_peer(void *vecxctx, void *vkey)
85 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
90 || key->keylen != ecxctx->keylen
91 || !ecx_key_up_ref(key)) {
92 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
95 ecx_key_free(ecxctx->peerkey);
96 ecxctx->peerkey = key;
101 static int ecx_derive(void *vecxctx, unsigned char *secret, size_t *secretlen,
104 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
106 if (ecxctx->key == NULL
107 || ecxctx->key->privkey == NULL
108 || ecxctx->peerkey == NULL) {
109 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
113 if (!ossl_assert(ecxctx->keylen == X25519_KEYLEN
114 || ecxctx->keylen == X448_KEYLEN)) {
115 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
119 if (secret == NULL) {
120 *secretlen = ecxctx->keylen;
123 if (outlen < ecxctx->keylen) {
124 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
128 if (ecxctx->keylen == X25519_KEYLEN) {
129 if (X25519(secret, ecxctx->key->privkey, ecxctx->peerkey->pubkey) == 0) {
130 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
134 if (X448(secret, ecxctx->key->privkey, ecxctx->peerkey->pubkey) == 0) {
135 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
140 *secretlen = ecxctx->keylen;
144 static void ecx_freectx(void *vecxctx)
146 PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
148 ecx_key_free(ecxctx->key);
149 ecx_key_free(ecxctx->peerkey);
151 OPENSSL_free(ecxctx);
154 static void *ecx_dupctx(void *vecxctx)
156 PROV_ECX_CTX *srcctx = (PROV_ECX_CTX *)vecxctx;
157 PROV_ECX_CTX *dstctx;
159 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
160 if (dstctx == NULL) {
161 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
166 if (dstctx->key != NULL && !ecx_key_up_ref(dstctx->key)) {
167 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
168 OPENSSL_free(dstctx);
172 if (dstctx->peerkey != NULL && !ecx_key_up_ref(dstctx->peerkey)) {
173 ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
174 ecx_key_free(dstctx->key);
175 OPENSSL_free(dstctx);
182 const OSSL_DISPATCH x25519_keyexch_functions[] = {
183 { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x25519_newctx },
184 { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init },
185 { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
186 { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
187 { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
188 { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
192 const OSSL_DISPATCH x448_keyexch_functions[] = {
193 { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x448_newctx },
194 { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init },
195 { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
196 { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
197 { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
198 { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },