Move the PROV_R reason codes to a public header
[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_dispatch.h>
12 #include <openssl/core_names.h>
13 #include <openssl/params.h>
14 #include <openssl/err.h>
15 #include <openssl/proverr.h>
16 #include "internal/cryptlib.h"
17 #include "crypto/ecx.h"
18 #include "prov/implementations.h"
19 #include "prov/providercommon.h"
20 #ifdef S390X_EC_ASM
21 # include "s390x_arch.h"
22 #endif
23
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;
31
32 /*
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.
36  */
37
38 typedef struct {
39     size_t keylen;
40     ECX_KEY *key;
41     ECX_KEY *peerkey;
42 } PROV_ECX_CTX;
43
44 static void *ecx_newctx(void *provctx, size_t keylen)
45 {
46     PROV_ECX_CTX *ctx;
47
48     if (!ossl_prov_is_running())
49         return NULL;
50
51     ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX));
52     if (ctx == NULL) {
53         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
54         return NULL;
55     }
56
57     ctx->keylen = keylen;
58
59     return ctx;
60 }
61
62 static void *x25519_newctx(void *provctx)
63 {
64     return ecx_newctx(provctx, X25519_KEYLEN);
65 }
66
67 static void *x448_newctx(void *provctx)
68 {
69     return ecx_newctx(provctx, X448_KEYLEN);
70 }
71
72 static int ecx_init(void *vecxctx, void *vkey)
73 {
74     PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
75     ECX_KEY *key = vkey;
76
77     if (!ossl_prov_is_running())
78         return 0;
79
80     if (ecxctx == NULL
81             || key == NULL
82             || key->keylen != ecxctx->keylen
83             || !ecx_key_up_ref(key)) {
84         ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
85         return 0;
86     }
87
88     ecx_key_free(ecxctx->key);
89     ecxctx->key = key;
90
91     return 1;
92 }
93
94 static int ecx_set_peer(void *vecxctx, void *vkey)
95 {
96     PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
97     ECX_KEY *key = vkey;
98
99     if (!ossl_prov_is_running())
100         return 0;
101
102     if (ecxctx == NULL
103             || key == NULL
104             || key->keylen != ecxctx->keylen
105             || !ecx_key_up_ref(key)) {
106         ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
107         return 0;
108     }
109     ecx_key_free(ecxctx->peerkey);
110     ecxctx->peerkey = key;
111
112     return 1;
113 }
114
115 static int ecx_derive(void *vecxctx, unsigned char *secret, size_t *secretlen,
116                       size_t outlen)
117 {
118     PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
119
120     if (!ossl_prov_is_running())
121         return 0;
122
123     if (ecxctx->key == NULL
124             || ecxctx->key->privkey == NULL
125             || ecxctx->peerkey == NULL) {
126         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
127         return 0;
128     }
129
130     if (!ossl_assert(ecxctx->keylen == X25519_KEYLEN
131             || ecxctx->keylen == X448_KEYLEN)) {
132         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
133         return 0;
134     }
135
136     if (secret == NULL) {
137         *secretlen = ecxctx->keylen;
138         return 1;
139     }
140     if (outlen < ecxctx->keylen) {
141         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
142         return 0;
143     }
144
145     if (ecxctx->keylen == X25519_KEYLEN) {
146 #ifdef S390X_EC_ASM
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);
152                 return 0;
153             }
154         } else
155 #endif
156         if (X25519(secret, ecxctx->key->privkey, ecxctx->peerkey->pubkey) == 0) {
157             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
158             return 0;
159         }
160     } else {
161 #ifdef S390X_EC_ASM
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);
167                 return 0;
168             }
169         } else
170 #endif
171         if (X448(secret, ecxctx->key->privkey, ecxctx->peerkey->pubkey) == 0) {
172             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
173             return 0;
174         }
175     }
176
177     *secretlen = ecxctx->keylen;
178     return 1;
179 }
180
181 static void ecx_freectx(void *vecxctx)
182 {
183     PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
184
185     ecx_key_free(ecxctx->key);
186     ecx_key_free(ecxctx->peerkey);
187
188     OPENSSL_free(ecxctx);
189 }
190
191 static void *ecx_dupctx(void *vecxctx)
192 {
193     PROV_ECX_CTX *srcctx = (PROV_ECX_CTX *)vecxctx;
194     PROV_ECX_CTX *dstctx;
195
196     if (!ossl_prov_is_running())
197         return NULL;
198
199     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
200     if (dstctx == NULL) {
201         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
202         return NULL;
203     }
204
205     *dstctx = *srcctx;
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);
209         return NULL;
210     }
211
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);
216         return NULL;
217     }
218
219     return dstctx;
220 }
221
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 },
229     { 0, NULL }
230 };
231
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 },
239     { 0, NULL }
240 };