Coverage Report

Created: 2026-09-01 13:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/bitcoin/src/crypto/aes.cpp
Line
Count
Source
1
// Copyright (c) 2016-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <crypto/aes.h>
6
#include <support/allocators/secure.h>
7
8
#include <cstring>
9
10
extern "C" {
11
#include <crypto/ctaes/ctaes.c>
12
}
13
14
AES256Encrypt::AES256Encrypt(const unsigned char key[32])
15
10.2k
{
16
10.2k
    ctx = allocator.allocate(1);
17
10.2k
    AES256_init(ctx, key);
18
10.2k
}
19
20
AES256Encrypt::~AES256Encrypt()
21
10.2k
{
22
10.2k
    allocator.deallocate(ctx, 1);
23
10.2k
}
24
25
void AES256Encrypt::Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
26
226k
{
27
226k
    AES256_encrypt(ctx, 1, ciphertext, plaintext);
28
226k
}
29
30
AES256Decrypt::AES256Decrypt(const unsigned char key[32])
31
38.9k
{
32
38.9k
    ctx = allocator.allocate(1);
33
38.9k
    AES256_init(ctx, key);
34
38.9k
}
35
36
AES256Decrypt::~AES256Decrypt()
37
38.9k
{
38
38.9k
    allocator.deallocate(ctx, 1);
39
38.9k
}
40
41
void AES256Decrypt::Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
42
332k
{
43
332k
    AES256_decrypt(ctx, 1, plaintext, ciphertext);
44
332k
}
45
46
47
template <typename T>
48
static int CBCEncrypt(const T& enc, const unsigned char iv[AES_BLOCKSIZE], const unsigned char* data, int size, bool pad, unsigned char* out)
49
110k
{
50
110k
    int written = 0;
51
110k
    int padsize = size % AES_BLOCKSIZE;
52
110k
    unsigned char mixed[AES_BLOCKSIZE];
53
54
110k
    if (!data || !size || !out)
  Branch (54:9): [True: 47.2k, False: 63.5k]
  Branch (54:18): [True: 342, False: 63.1k]
  Branch (54:27): [True: 0, False: 63.1k]
55
47.5k
        return 0;
56
57
63.1k
    if (!pad && padsize != 0)
  Branch (57:9): [True: 4.10k, False: 59.0k]
  Branch (57:17): [True: 3.61k, False: 492]
58
3.61k
        return 0;
59
60
59.5k
    memcpy(mixed, iv, AES_BLOCKSIZE);
61
62
    // Write all but the last block
63
183k
    while (written + AES_BLOCKSIZE <= size) {
  Branch (63:12): [True: 124k, False: 59.5k]
64
2.11M
        for (int i = 0; i != AES_BLOCKSIZE; i++)
  Branch (64:25): [True: 1.98M, False: 124k]
65
1.98M
            mixed[i] ^= *data++;
66
124k
        enc.Encrypt(out + written, mixed);
67
124k
        memcpy(mixed, out + written, AES_BLOCKSIZE);
68
124k
        written += AES_BLOCKSIZE;
69
124k
    }
70
59.5k
    if (pad) {
  Branch (70:9): [True: 59.0k, False: 492]
71
        // For all that remains, pad each byte with the value of the remaining
72
        // space. If there is none, pad by a full block.
73
221k
        for (int i = 0; i != padsize; i++)
  Branch (73:25): [True: 162k, False: 59.0k]
74
162k
            mixed[i] ^= *data++;
75
841k
        for (int i = padsize; i != AES_BLOCKSIZE; i++)
  Branch (75:31): [True: 781k, False: 59.0k]
76
781k
            mixed[i] ^= AES_BLOCKSIZE - padsize;
77
59.0k
        enc.Encrypt(out + written, mixed);
78
59.0k
        written += AES_BLOCKSIZE;
79
59.0k
    }
80
59.5k
    return written;
81
63.1k
}
82
83
template <typename T>
84
static int CBCDecrypt(const T& dec, const unsigned char iv[AES_BLOCKSIZE], const unsigned char* data, int size, bool pad, unsigned char* out)
85
139k
{
86
139k
    int written = 0;
87
139k
    bool fail = false;
88
139k
    const unsigned char* prev = iv;
89
90
139k
    if (!data || !size || !out)
  Branch (90:9): [True: 1.40k, False: 138k]
  Branch (90:18): [True: 50.5k, False: 87.5k]
  Branch (90:27): [True: 0, False: 87.5k]
91
51.9k
        return 0;
92
93
87.5k
    if (size % AES_BLOCKSIZE != 0)
  Branch (93:9): [True: 2.07k, False: 85.4k]
94
2.07k
        return 0;
95
96
    // Decrypt all data. Padding will be checked in the output.
97
375k
    while (written != size) {
  Branch (97:12): [True: 289k, False: 85.4k]
98
289k
        dec.Decrypt(out, data + written);
99
4.92M
        for (int i = 0; i != AES_BLOCKSIZE; i++)
  Branch (99:25): [True: 4.63M, False: 289k]
100
4.63M
            *out++ ^= prev[i];
101
289k
        prev = data + written;
102
289k
        written += AES_BLOCKSIZE;
103
289k
    }
104
105
    // When decrypting padding, attempt to run in constant-time
106
85.4k
    if (pad) {
  Branch (106:9): [True: 84.9k, False: 492]
107
        // If used, padding size is the value of the last decrypted byte. For
108
        // it to be valid, It must be between 1 and AES_BLOCKSIZE.
109
84.9k
        unsigned char padsize = *--out;
110
84.9k
        fail = !padsize | (padsize > AES_BLOCKSIZE);
111
112
        // If not well-formed, treat it as though there's no padding.
113
84.9k
        padsize *= !fail;
114
115
        // All padding must equal the last byte otherwise it's not well-formed
116
1.44M
        for (int i = AES_BLOCKSIZE; i != 0; i--)
  Branch (116:37): [True: 1.35M, False: 84.9k]
117
1.35M
            fail |= ((i > AES_BLOCKSIZE - padsize) & (*out-- != padsize));
118
119
84.9k
        written -= padsize;
120
84.9k
    }
121
85.4k
    return written * !fail;
122
87.5k
}
123
124
AES256CBCEncrypt::AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
125
10.1k
    : enc(key), pad(padIn)
126
10.1k
{
127
10.1k
    iv = allocator.allocate(AES_BLOCKSIZE);
128
10.1k
    memcpy(iv, ivIn, AES_BLOCKSIZE);
129
10.1k
}
130
131
int AES256CBCEncrypt::Encrypt(const unsigned char* data, int size, unsigned char* out) const
132
110k
{
133
110k
    return CBCEncrypt(enc, iv, data, size, pad, out);
134
110k
}
135
136
AES256CBCEncrypt::~AES256CBCEncrypt()
137
10.1k
{
138
10.1k
    allocator.deallocate(iv, AES_BLOCKSIZE);
139
10.1k
}
140
141
AES256CBCDecrypt::AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
142
38.9k
    : dec(key), pad(padIn)
143
38.9k
{
144
38.9k
    iv = allocator.allocate(AES_BLOCKSIZE);
145
38.9k
    memcpy(iv, ivIn, AES_BLOCKSIZE);
146
38.9k
}
147
148
149
int AES256CBCDecrypt::Decrypt(const unsigned char* data, int size, unsigned char* out) const
150
139k
{
151
139k
    return CBCDecrypt(dec, iv, data, size, pad, out);
152
139k
}
153
154
AES256CBCDecrypt::~AES256CBCDecrypt()
155
38.9k
{
156
38.9k
    allocator.deallocate(iv, AES_BLOCKSIZE);
157
38.9k
}