Add X25519/X448 Key Exchange to the default provider
[oweals/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_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"
19
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;
27
28 /*
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.
32  */
33
34 typedef struct {
35     size_t keylen;
36     ECX_KEY *key;
37     ECX_KEY *peerkey;
38 } PROV_ECX_CTX;
39
40 static void *ecx_newctx(void *provctx, size_t keylen)
41 {
42     PROV_ECX_CTX *ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX));
43
44     if (ctx == NULL) {
45         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
46         return NULL;
47     }
48
49     ctx->keylen = keylen;
50
51     return ctx;
52 }
53
54 static void *x25519_newctx(void *provctx)
55 {
56     return ecx_newctx(provctx, X25519_KEYLEN);
57 }
58
59 static void *x448_newctx(void *provctx)
60 {
61     return ecx_newctx(provctx, X448_KEYLEN);
62 }
63
64 static int ecx_init(void *vecxctx, void *vkey)
65 {
66     PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
67     ECX_KEY *key = vkey;
68
69     if (ecxctx == NULL
70             || key == NULL
71             || key->keylen != ecxctx->keylen
72             || !ecx_key_up_ref(key)) {
73         ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
74         return 0;
75     }
76
77     ecx_key_free(ecxctx->key);
78     ecxctx->key = key;
79
80     return 1;
81 }
82
83 static int ecx_set_peer(void *vecxctx, void *vkey)
84 {
85     PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
86     ECX_KEY *key = vkey;
87
88     if (ecxctx == NULL
89             || key == NULL
90             || key->keylen != ecxctx->keylen
91             || !ecx_key_up_ref(key)) {
92         ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
93         return 0;
94     }
95     ecx_key_free(ecxctx->peerkey);
96     ecxctx->peerkey = key;
97
98     return 1;
99 }
100
101 static int ecx_derive(void *vecxctx, unsigned char *secret, size_t *secretlen,
102                       size_t outlen)
103 {
104     PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
105
106     if (ecxctx->key == NULL
107             || ecxctx->key->privkey == NULL
108             || ecxctx->peerkey == NULL) {
109         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
110         return 0;
111     }
112
113     if (!ossl_assert(ecxctx->keylen == X25519_KEYLEN
114             || ecxctx->keylen == X448_KEYLEN)) {
115         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
116         return 0;
117     }
118
119     if (secret == NULL) {
120         *secretlen = ecxctx->keylen;
121         return 1;
122     }
123     if (outlen < ecxctx->keylen) {
124         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
125         return 0;
126     }
127
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);
131             return 0;
132         }
133     } else {
134         if (X448(secret, ecxctx->key->privkey, ecxctx->peerkey->pubkey) == 0) {
135             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
136             return 0;
137         }
138     }
139
140     *secretlen = ecxctx->keylen;
141     return 1;
142 }
143
144 static void ecx_freectx(void *vecxctx)
145 {
146     PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
147
148     ecx_key_free(ecxctx->key);
149     ecx_key_free(ecxctx->peerkey);
150
151     OPENSSL_free(ecxctx);
152 }
153
154 static void *ecx_dupctx(void *vecxctx)
155 {
156     PROV_ECX_CTX *srcctx = (PROV_ECX_CTX *)vecxctx;
157     PROV_ECX_CTX *dstctx;
158
159     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
160     if (dstctx == NULL) {
161         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
162         return NULL;
163     }
164
165     *dstctx = *srcctx;
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);
169         return NULL;
170     }
171
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);
176         return NULL;
177     }
178
179     return dstctx;
180 }
181
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 },
189     { 0, NULL }
190 };
191
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 },
199     { 0, NULL }
200 };