Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/crypto/ec/ecx_key.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020-2023 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 <string.h>
11
#include <openssl/err.h>
12
#include <openssl/proverr.h>
13
#include "crypto/ecx.h"
14
#include "internal/common.h" /* for ossl_assert() */
15
16
#ifdef S390X_EC_ASM
17
# include "s390x_arch.h"
18
#endif
19
20
ECX_KEY *ossl_ecx_key_new(OSSL_LIB_CTX *libctx, ECX_KEY_TYPE type, int haspubkey,
21
                          const char *propq)
22
63.0k
{
23
63.0k
    ECX_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
24
25
63.0k
    if (ret == NULL)
26
0
        return NULL;
27
28
63.0k
    ret->libctx = libctx;
29
63.0k
    ret->haspubkey = haspubkey;
30
63.0k
    switch (type) {
31
61.2k
    case ECX_KEY_TYPE_X25519:
32
61.2k
        ret->keylen = X25519_KEYLEN;
33
61.2k
        break;
34
1.42k
    case ECX_KEY_TYPE_X448:
35
1.42k
        ret->keylen = X448_KEYLEN;
36
1.42k
        break;
37
83
    case ECX_KEY_TYPE_ED25519:
38
83
        ret->keylen = ED25519_KEYLEN;
39
83
        break;
40
213
    case ECX_KEY_TYPE_ED448:
41
213
        ret->keylen = ED448_KEYLEN;
42
213
        break;
43
63.0k
    }
44
63.0k
    ret->type = type;
45
46
63.0k
    if (!CRYPTO_NEW_REF(&ret->references, 1))
47
0
        goto err;
48
49
63.0k
    if (propq != NULL) {
50
24
        ret->propq = OPENSSL_strdup(propq);
51
24
        if (ret->propq == NULL)
52
0
            goto err;
53
24
    }
54
63.0k
    return ret;
55
0
err:
56
0
    if (ret != NULL) {
57
0
        OPENSSL_free(ret->propq);
58
0
        CRYPTO_FREE_REF(&ret->references);
59
0
    }
60
0
    OPENSSL_free(ret);
61
0
    return NULL;
62
63.0k
}
63
64
void ossl_ecx_key_free(ECX_KEY *key)
65
601k
{
66
601k
    int i;
67
68
601k
    if (key == NULL)
69
467k
        return;
70
71
133k
    CRYPTO_DOWN_REF(&key->references, &i);
72
133k
    REF_PRINT_COUNT("ECX_KEY", i, key);
73
133k
    if (i > 0)
74
36.3k
        return;
75
97.1k
    REF_ASSERT_ISNT(i < 0);
76
77
97.1k
    OPENSSL_free(key->propq);
78
97.1k
    OPENSSL_secure_clear_free(key->privkey, key->keylen);
79
97.1k
    CRYPTO_FREE_REF(&key->references);
80
97.1k
    OPENSSL_free(key);
81
97.1k
}
82
83
void ossl_ecx_key_set0_libctx(ECX_KEY *key, OSSL_LIB_CTX *libctx)
84
3.74k
{
85
3.74k
    key->libctx = libctx;
86
3.74k
}
87
88
int ossl_ecx_key_up_ref(ECX_KEY *key)
89
36.3k
{
90
36.3k
    int i;
91
92
36.3k
    if (CRYPTO_UP_REF(&key->references, &i) <= 0)
93
0
        return 0;
94
95
36.3k
    REF_PRINT_COUNT("ECX_KEY", i, key);
96
36.3k
    REF_ASSERT_ISNT(i < 2);
97
36.3k
    return ((i > 1) ? 1 : 0);
98
36.3k
}
99
100
unsigned char *ossl_ecx_key_allocate_privkey(ECX_KEY *key)
101
74.3k
{
102
74.3k
    key->privkey = OPENSSL_secure_zalloc(key->keylen);
103
104
74.3k
    return key->privkey;
105
74.3k
}
106
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)
109
28.5k
{
110
28.5k
    if (priv == NULL
111
28.5k
            || priv->privkey == NULL
112
28.5k
            || peer == NULL) {
113
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
114
0
        return 0;
115
0
    }
116
117
28.5k
    if (!ossl_assert(keylen == X25519_KEYLEN
118
28.5k
            || keylen == X448_KEYLEN)) {
119
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
120
0
        return 0;
121
0
    }
122
123
28.5k
    if (secret == NULL) {
124
14.2k
        *secretlen = keylen;
125
14.2k
        return 1;
126
14.2k
    }
127
14.3k
    if (outlen < keylen) {
128
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
129
0
        return 0;
130
0
    }
131
132
14.3k
    if (keylen == X25519_KEYLEN) {
133
#ifdef S390X_EC_ASM
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);
138
                return 0;
139
            }
140
        } else
141
#endif
142
14.2k
        if (ossl_x25519(secret, priv->privkey, peer->pubkey) == 0) {
143
26
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
144
26
            return 0;
145
26
        }
146
14.2k
    } else {
147
#ifdef S390X_EC_ASM
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);
152
                return 0;
153
            }
154
        } else
155
#endif
156
7
        if (ossl_x448(secret, priv->privkey, peer->pubkey) == 0) {
157
2
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
158
2
            return 0;
159
2
        }
160
7
    }
161
14.2k
    *secretlen = keylen;
162
14.2k
    return 1;
163
14.3k
}