Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/providers/implementations/exchange/ecx_exch.c
Line
Count
Source
1
/*
2
 * Copyright 2020-2021 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
32.2k
{
46
32.2k
    PROV_ECX_CTX *ctx;
47
48
32.2k
    if (!ossl_prov_is_running())
49
0
        return NULL;
50
51
32.2k
    ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX));
52
32.2k
    if (ctx == NULL) {
53
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
54
0
        return NULL;
55
0
    }
56
57
32.2k
    ctx->keylen = keylen;
58
59
32.2k
    return ctx;
60
32.2k
}
61
62
static void *x25519_newctx(void *provctx)
63
32.2k
{
64
32.2k
    return ecx_newctx(provctx, X25519_KEYLEN);
65
32.2k
}
66
67
static void *x448_newctx(void *provctx)
68
33
{
69
33
    return ecx_newctx(provctx, X448_KEYLEN);
70
33
}
71
72
static int ecx_init(void *vecxctx, void *vkey,
73
    ossl_unused const OSSL_PARAM params[])
74
32.2k
{
75
32.2k
    PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
76
32.2k
    ECX_KEY *key = vkey;
77
78
32.2k
    if (!ossl_prov_is_running())
79
0
        return 0;
80
81
32.2k
    if (ecxctx == NULL
82
32.2k
        || key == NULL
83
32.2k
        || key->keylen != ecxctx->keylen
84
32.2k
        || !ossl_ecx_key_up_ref(key)) {
85
0
        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
86
0
        return 0;
87
0
    }
88
89
32.2k
    ossl_ecx_key_free(ecxctx->key);
90
32.2k
    ecxctx->key = key;
91
92
32.2k
    return 1;
93
32.2k
}
94
95
static int ecx_set_peer(void *vecxctx, void *vkey)
96
32.2k
{
97
32.2k
    PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
98
32.2k
    ECX_KEY *key = vkey;
99
100
32.2k
    if (!ossl_prov_is_running())
101
0
        return 0;
102
103
32.2k
    if (ecxctx == NULL
104
32.2k
        || key == NULL
105
32.2k
        || key->keylen != ecxctx->keylen
106
32.2k
        || !ossl_ecx_key_up_ref(key)) {
107
0
        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
108
0
        return 0;
109
0
    }
110
32.2k
    ossl_ecx_key_free(ecxctx->peerkey);
111
32.2k
    ecxctx->peerkey = key;
112
113
32.2k
    return 1;
114
32.2k
}
115
116
static int ecx_derive(void *vecxctx, unsigned char *secret, size_t *secretlen,
117
    size_t outlen)
118
1.87k
{
119
1.87k
    PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
120
121
1.87k
    if (!ossl_prov_is_running())
122
0
        return 0;
123
124
1.87k
    if (ecxctx->key == NULL
125
1.87k
        || ecxctx->key->privkey == NULL
126
1.87k
        || ecxctx->peerkey == NULL) {
127
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
128
0
        return 0;
129
0
    }
130
131
1.87k
    if (!ossl_assert(ecxctx->keylen == X25519_KEYLEN
132
1.87k
            || ecxctx->keylen == X448_KEYLEN)) {
133
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
134
0
        return 0;
135
0
    }
136
137
1.87k
    if (secret == NULL) {
138
935
        *secretlen = ecxctx->keylen;
139
935
        return 1;
140
935
    }
141
935
    if (outlen < ecxctx->keylen) {
142
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
143
0
        return 0;
144
0
    }
145
146
935
    if (ecxctx->keylen == X25519_KEYLEN) {
147
#ifdef S390X_EC_ASM
148
        if (OPENSSL_s390xcap_P.pcc[1]
149
            & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X25519)) {
150
            if (s390x_x25519_mul(secret, ecxctx->peerkey->pubkey,
151
                    ecxctx->key->privkey)
152
                == 0) {
153
                ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
154
                return 0;
155
            }
156
        } else
157
#endif
158
930
            if (ossl_x25519(secret, ecxctx->key->privkey,
159
930
                    ecxctx->peerkey->pubkey)
160
930
                == 0) {
161
8
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
162
8
            return 0;
163
8
        }
164
930
    } else {
165
#ifdef S390X_EC_ASM
166
        if (OPENSSL_s390xcap_P.pcc[1]
167
            & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X448)) {
168
            if (s390x_x448_mul(secret, ecxctx->peerkey->pubkey,
169
                    ecxctx->key->privkey)
170
                == 0) {
171
                ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
172
                return 0;
173
            }
174
        } else
175
#endif
176
5
            if (ossl_x448(secret, ecxctx->key->privkey,
177
5
                    ecxctx->peerkey->pubkey)
178
5
                == 0) {
179
1
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
180
1
            return 0;
181
1
        }
182
5
    }
183
184
926
    *secretlen = ecxctx->keylen;
185
926
    return 1;
186
935
}
187
188
static void ecx_freectx(void *vecxctx)
189
32.2k
{
190
32.2k
    PROV_ECX_CTX *ecxctx = (PROV_ECX_CTX *)vecxctx;
191
192
32.2k
    ossl_ecx_key_free(ecxctx->key);
193
32.2k
    ossl_ecx_key_free(ecxctx->peerkey);
194
195
32.2k
    OPENSSL_free(ecxctx);
196
32.2k
}
197
198
static void *ecx_dupctx(void *vecxctx)
199
0
{
200
0
    PROV_ECX_CTX *srcctx = (PROV_ECX_CTX *)vecxctx;
201
0
    PROV_ECX_CTX *dstctx;
202
203
0
    if (!ossl_prov_is_running())
204
0
        return NULL;
205
206
0
    dstctx = OPENSSL_zalloc(sizeof(*srcctx));
207
0
    if (dstctx == NULL) {
208
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
209
0
        return NULL;
210
0
    }
211
212
0
    *dstctx = *srcctx;
213
0
    if (dstctx->key != NULL && !ossl_ecx_key_up_ref(dstctx->key)) {
214
0
        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
215
0
        OPENSSL_free(dstctx);
216
0
        return NULL;
217
0
    }
218
219
0
    if (dstctx->peerkey != NULL && !ossl_ecx_key_up_ref(dstctx->peerkey)) {
220
0
        ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR);
221
0
        ossl_ecx_key_free(dstctx->key);
222
0
        OPENSSL_free(dstctx);
223
0
        return NULL;
224
0
    }
225
226
0
    return dstctx;
227
0
}
228
229
const OSSL_DISPATCH ossl_x25519_keyexch_functions[] = {
230
    { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x25519_newctx },
231
    { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init },
232
    { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
233
    { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
234
    { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
235
    { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
236
    { 0, NULL }
237
};
238
239
const OSSL_DISPATCH ossl_x448_keyexch_functions[] = {
240
    { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))x448_newctx },
241
    { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))ecx_init },
242
    { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))ecx_derive },
243
    { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))ecx_set_peer },
244
    { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))ecx_freectx },
245
    { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))ecx_dupctx },
246
    { 0, NULL }
247
};