Coverage Report

Created: 2026-09-01 13:33

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
6.31k
{
16
6.31k
    ctx = allocator.allocate(1);
17
6.31k
    AES256_init(ctx, key);
18
6.31k
}
19
20
AES256Encrypt::~AES256Encrypt()
21
6.31k
{
22
6.31k
    allocator.deallocate(ctx, 1);
23
6.31k
}
24
25
void AES256Encrypt::Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
26
149k
{
27
149k
    AES256_encrypt(ctx, 1, ciphertext, plaintext);
28
149k
}
29
30
AES256Decrypt::AES256Decrypt(const unsigned char key[32])
31
29.2k
{
32
29.2k
    ctx = allocator.allocate(1);
33
29.2k
    AES256_init(ctx, key);
34
29.2k
}
35
36
AES256Decrypt::~AES256Decrypt()
37
29.2k
{
38
29.2k
    allocator.deallocate(ctx, 1);
39
29.2k
}
40
41
void AES256Decrypt::Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
42
230k
{
43
230k
    AES256_decrypt(ctx, 1, plaintext, ciphertext);
44
230k
}
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
58.3k
{
50
58.3k
    int written = 0;
51
58.3k
    int padsize = size % AES_BLOCKSIZE;
52
58.3k
    unsigned char mixed[AES_BLOCKSIZE];
53
54
58.3k
    if (!data || !size || !out)
  Branch (54:9): [True: 27.6k, False: 30.7k]
  Branch (54:18): [True: 166, False: 30.5k]
  Branch (54:27): [True: 0, False: 30.5k]
55
27.8k
        return 0;
56
57
30.5k
    if (!pad && padsize != 0)
  Branch (57:9): [True: 1.67k, False: 28.8k]
  Branch (57:17): [True: 1.42k, False: 247]
58
1.42k
        return 0;
59
60
29.1k
    memcpy(mixed, iv, AES_BLOCKSIZE);
61
62
    // Write all but the last block
63
117k
    while (written + AES_BLOCKSIZE <= size) {
  Branch (63:12): [True: 88.0k, False: 29.1k]
64
1.49M
        for (int i = 0; i != AES_BLOCKSIZE; i++)
  Branch (64:25): [True: 1.40M, False: 88.0k]
65
1.40M
            mixed[i] ^= *data++;
66
88.0k
        enc.Encrypt(out + written, mixed);
67
88.0k
        memcpy(mixed, out + written, AES_BLOCKSIZE);
68
88.0k
        written += AES_BLOCKSIZE;
69
88.0k
    }
70
29.1k
    if (pad) {
  Branch (70:9): [True: 28.8k, False: 247]
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
105k
        for (int i = 0; i != padsize; i++)
  Branch (73:25): [True: 76.4k, False: 28.8k]
74
76.4k
            mixed[i] ^= *data++;
75
414k
        for (int i = padsize; i != AES_BLOCKSIZE; i++)
  Branch (75:31): [True: 385k, False: 28.8k]
76
385k
            mixed[i] ^= AES_BLOCKSIZE - padsize;
77
28.8k
        enc.Encrypt(out + written, mixed);
78
28.8k
        written += AES_BLOCKSIZE;
79
28.8k
    }
80
29.1k
    return written;
81
30.5k
}
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
81.2k
{
86
81.2k
    int written = 0;
87
81.2k
    bool fail = false;
88
81.2k
    const unsigned char* prev = iv;
89
90
81.2k
    if (!data || !size || !out)
  Branch (90:9): [True: 733, False: 80.5k]
  Branch (90:18): [True: 28.8k, False: 51.7k]
  Branch (90:27): [True: 0, False: 51.7k]
91
29.5k
        return 0;
92
93
51.7k
    if (size % AES_BLOCKSIZE != 0)
  Branch (93:9): [True: 1.03k, False: 50.7k]
94
1.03k
        return 0;
95
96
    // Decrypt all data. Padding will be checked in the output.
97
248k
    while (written != size) {
  Branch (97:12): [True: 198k, False: 50.7k]
98
198k
        dec.Decrypt(out, data + written);
99
3.37M
        for (int i = 0; i != AES_BLOCKSIZE; i++)
  Branch (99:25): [True: 3.17M, False: 198k]
100
3.17M
            *out++ ^= prev[i];
101
198k
        prev = data + written;
102
198k
        written += AES_BLOCKSIZE;
103
198k
    }
104
105
    // When decrypting padding, attempt to run in constant-time
106
50.7k
    if (pad) {
  Branch (106:9): [True: 50.4k, False: 247]
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
50.4k
        unsigned char padsize = *--out;
110
50.4k
        fail = !padsize | (padsize > AES_BLOCKSIZE);
111
112
        // If not well-formed, treat it as though there's no padding.
113
50.4k
        padsize *= !fail;
114
115
        // All padding must equal the last byte otherwise it's not well-formed
116
857k
        for (int i = AES_BLOCKSIZE; i != 0; i--)
  Branch (116:37): [True: 807k, False: 50.4k]
117
807k
            fail |= ((i > AES_BLOCKSIZE - padsize) & (*out-- != padsize));
118
119
50.4k
        written -= padsize;
120
50.4k
    }
121
50.7k
    return written * !fail;
122
51.7k
}
123
124
AES256CBCEncrypt::AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
125
6.25k
    : enc(key), pad(padIn)
126
6.25k
{
127
6.25k
    iv = allocator.allocate(AES_BLOCKSIZE);
128
6.25k
    memcpy(iv, ivIn, AES_BLOCKSIZE);
129
6.25k
}
130
131
int AES256CBCEncrypt::Encrypt(const unsigned char* data, int size, unsigned char* out) const
132
58.3k
{
133
58.3k
    return CBCEncrypt(enc, iv, data, size, pad, out);
134
58.3k
}
135
136
AES256CBCEncrypt::~AES256CBCEncrypt()
137
6.25k
{
138
6.25k
    allocator.deallocate(iv, AES_BLOCKSIZE);
139
6.25k
}
140
141
AES256CBCDecrypt::AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
142
29.1k
    : dec(key), pad(padIn)
143
29.1k
{
144
29.1k
    iv = allocator.allocate(AES_BLOCKSIZE);
145
29.1k
    memcpy(iv, ivIn, AES_BLOCKSIZE);
146
29.1k
}
147
148
149
int AES256CBCDecrypt::Decrypt(const unsigned char* data, int size, unsigned char* out) const
150
81.2k
{
151
81.2k
    return CBCDecrypt(dec, iv, data, size, pad, out);
152
81.2k
}
153
154
AES256CBCDecrypt::~AES256CBCDecrypt()
155
29.1k
{
156
29.1k
    allocator.deallocate(iv, AES_BLOCKSIZE);
157
29.1k
}