Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/crypto/ec/ecx_key.c
Line
Count
Source
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
150k
{
23
150k
    ECX_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
24
25
150k
    if (ret == NULL)
26
0
        return NULL;
27
28
150k
    ret->libctx = libctx;
29
150k
    ret->haspubkey = haspubkey;
30
150k
    switch (type) {
31
149k
    case ECX_KEY_TYPE_X25519:
32
149k
        ret->keylen = X25519_KEYLEN;
33
149k
        break;
34
436
    case ECX_KEY_TYPE_X448:
35
436
        ret->keylen = X448_KEYLEN;
36
436
        break;
37
166
    case ECX_KEY_TYPE_ED25519:
38
166
        ret->keylen = ED25519_KEYLEN;
39
166
        break;
40
310
    case ECX_KEY_TYPE_ED448:
41
310
        ret->keylen = ED448_KEYLEN;
42
310
        break;
43
150k
    }
44
150k
    ret->type = type;
45
46
150k
    if (!CRYPTO_NEW_REF(&ret->references, 1))
47
0
        goto err;
48
49
150k
    if (propq != NULL) {
50
48
        ret->propq = OPENSSL_strdup(propq);
51
48
        if (ret->propq == NULL)
52
0
            goto err;
53
48
    }
54
150k
    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
150k
}
63
64
void ossl_ecx_key_free(ECX_KEY *key)
65
976k
{
66
976k
    int i;
67
68
976k
    if (key == NULL)
69
719k
        return;
70
71
257k
    CRYPTO_DOWN_REF(&key->references, &i);
72
257k
    REF_PRINT_COUNT("ECX_KEY", i, key);
73
257k
    if (i > 0)
74
68.5k
        return;
75
188k
    REF_ASSERT_ISNT(i < 0);
76
77
188k
    OPENSSL_free(key->propq);
78
#ifdef OPENSSL_PEDANTIC_ZEROIZATION
79
    OPENSSL_cleanse(&key->pubkey, sizeof(key->pubkey));
80
#endif
81
188k
    OPENSSL_secure_clear_free(key->privkey, key->keylen);
82
188k
    CRYPTO_FREE_REF(&key->references);
83
188k
    OPENSSL_free(key);
84
188k
}
85
86
void ossl_ecx_key_set0_libctx(ECX_KEY *key, OSSL_LIB_CTX *libctx)
87
2.90k
{
88
2.90k
    key->libctx = libctx;
89
2.90k
}
90
91
int ossl_ecx_key_up_ref(ECX_KEY *key)
92
68.5k
{
93
68.5k
    int i;
94
95
68.5k
    if (CRYPTO_UP_REF(&key->references, &i) <= 0)
96
0
        return 0;
97
98
68.5k
    REF_PRINT_COUNT("ECX_KEY", i, key);
99
68.5k
    REF_ASSERT_ISNT(i < 2);
100
68.5k
    return ((i > 1) ? 1 : 0);
101
68.5k
}
102
103
unsigned char *ossl_ecx_key_allocate_privkey(ECX_KEY *key)
104
151k
{
105
151k
    key->privkey = OPENSSL_secure_zalloc(key->keylen);
106
107
151k
    return key->privkey;
108
151k
}
109
110
int ossl_ecx_compute_key(ECX_KEY *peer, ECX_KEY *priv, size_t keylen,
111
    unsigned char *secret, size_t *secretlen, size_t outlen)
112
63.7k
{
113
63.7k
    if (priv == NULL
114
63.7k
        || priv->privkey == NULL
115
63.7k
        || peer == NULL) {
116
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY);
117
0
        return 0;
118
0
    }
119
120
63.7k
    if (!ossl_assert(keylen == X25519_KEYLEN
121
63.7k
            || keylen == X448_KEYLEN)) {
122
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
123
0
        return 0;
124
0
    }
125
126
63.7k
    if (secret == NULL) {
127
31.8k
        *secretlen = keylen;
128
31.8k
        return 1;
129
31.8k
    }
130
31.8k
    if (outlen < keylen) {
131
0
        ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
132
0
        return 0;
133
0
    }
134
135
31.8k
    if (keylen == X25519_KEYLEN) {
136
#ifdef S390X_EC_ASM
137
        if (OPENSSL_s390xcap_P.pcc[1]
138
            & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X25519)) {
139
            if (s390x_x25519_mul(secret, peer->pubkey, priv->privkey) == 0) {
140
                ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
141
                return 0;
142
            }
143
        } else
144
#endif
145
31.8k
            if (ossl_x25519(secret, priv->privkey, peer->pubkey) == 0) {
146
53
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
147
53
            return 0;
148
53
        }
149
31.8k
    } else {
150
#ifdef S390X_EC_ASM
151
        if (OPENSSL_s390xcap_P.pcc[1]
152
            & S390X_CAPBIT(S390X_SCALAR_MULTIPLY_X448)) {
153
            if (s390x_x448_mul(secret, peer->pubkey, priv->privkey) == 0) {
154
                ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
155
                return 0;
156
            }
157
        } else
158
#endif
159
26
            if (ossl_x448(secret, priv->privkey, peer->pubkey) == 0) {
160
5
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_DURING_DERIVATION);
161
5
            return 0;
162
5
        }
163
26
    }
164
31.8k
    *secretlen = keylen;
165
31.8k
    return 1;
166
31.8k
}