Coverage Report

Created: 2025-06-13 06:58

/src/openssl31/crypto/ec/ecx_key.c
Line
Count
Source (jump to first uncovered line)
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 <string.h>
11
#include <openssl/err.h>
12
#include "crypto/ecx.h"
13
14
ECX_KEY *ossl_ecx_key_new(OSSL_LIB_CTX *libctx, ECX_KEY_TYPE type, int haspubkey,
15
                          const char *propq)
16
21.2k
{
17
21.2k
    ECX_KEY *ret = OPENSSL_zalloc(sizeof(*ret));
18
19
21.2k
    if (ret == NULL)
20
0
        return NULL;
21
22
21.2k
    ret->libctx = libctx;
23
21.2k
    ret->haspubkey = haspubkey;
24
21.2k
    switch (type) {
25
19.4k
    case ECX_KEY_TYPE_X25519:
26
19.4k
        ret->keylen = X25519_KEYLEN;
27
19.4k
        break;
28
1.32k
    case ECX_KEY_TYPE_X448:
29
1.32k
        ret->keylen = X448_KEYLEN;
30
1.32k
        break;
31
223
    case ECX_KEY_TYPE_ED25519:
32
223
        ret->keylen = ED25519_KEYLEN;
33
223
        break;
34
311
    case ECX_KEY_TYPE_ED448:
35
311
        ret->keylen = ED448_KEYLEN;
36
311
        break;
37
21.2k
    }
38
21.2k
    ret->type = type;
39
21.2k
    ret->references = 1;
40
41
21.2k
    if (propq != NULL) {
42
0
        ret->propq = OPENSSL_strdup(propq);
43
0
        if (ret->propq == NULL)
44
0
            goto err;
45
0
    }
46
47
21.2k
    ret->lock = CRYPTO_THREAD_lock_new();
48
21.2k
    if (ret->lock == NULL)
49
0
        goto err;
50
21.2k
    return ret;
51
0
err:
52
0
    ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
53
0
    OPENSSL_free(ret);
54
0
    return NULL;
55
21.2k
}
56
57
void ossl_ecx_key_free(ECX_KEY *key)
58
601k
{
59
601k
    int i;
60
61
601k
    if (key == NULL)
62
467k
        return;
63
64
133k
    CRYPTO_DOWN_REF(&key->references, &i, key->lock);
65
133k
    REF_PRINT_COUNT("ECX_KEY", key);
66
133k
    if (i > 0)
67
36.3k
        return;
68
97.1k
    REF_ASSERT_ISNT(i < 0);
69
70
97.1k
    OPENSSL_free(key->propq);
71
97.1k
    OPENSSL_secure_clear_free(key->privkey, key->keylen);
72
97.1k
    CRYPTO_THREAD_lock_free(key->lock);
73
97.1k
    OPENSSL_free(key);
74
97.1k
}
75
76
void ossl_ecx_key_set0_libctx(ECX_KEY *key, OSSL_LIB_CTX *libctx)
77
3.74k
{
78
3.74k
    key->libctx = libctx;
79
3.74k
}
80
81
int ossl_ecx_key_up_ref(ECX_KEY *key)
82
36.3k
{
83
36.3k
    int i;
84
85
36.3k
    if (CRYPTO_UP_REF(&key->references, &i, key->lock) <= 0)
86
0
        return 0;
87
88
36.3k
    REF_PRINT_COUNT("ECX_KEY", key);
89
36.3k
    REF_ASSERT_ISNT(i < 2);
90
36.3k
    return ((i > 1) ? 1 : 0);
91
36.3k
}
92
93
unsigned char *ossl_ecx_key_allocate_privkey(ECX_KEY *key)
94
74.3k
{
95
74.3k
    key->privkey = OPENSSL_secure_zalloc(key->keylen);
96
97
74.3k
    return key->privkey;
98
74.3k
}