Line data Source code
1 : // Copyright (c) 2016-2019 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 :
7 : #include <string.h>
8 :
9 : extern "C" {
10 : #include <crypto/ctaes/ctaes.c>
11 : }
12 :
13 8440 : AES256Encrypt::AES256Encrypt(const unsigned char key[32])
14 4220 : {
15 4220 : AES256_init(&ctx, key);
16 8440 : }
17 :
18 8440 : AES256Encrypt::~AES256Encrypt()
19 4220 : {
20 4220 : memset(&ctx, 0, sizeof(ctx));
21 8440 : }
22 :
23 8064 : void AES256Encrypt::Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
24 : {
25 8064 : AES256_encrypt(&ctx, 1, ciphertext, plaintext);
26 8064 : }
27 :
28 11872 : AES256Decrypt::AES256Decrypt(const unsigned char key[32])
29 5936 : {
30 5936 : AES256_init(&ctx, key);
31 11872 : }
32 :
33 11872 : AES256Decrypt::~AES256Decrypt()
34 5936 : {
35 5936 : memset(&ctx, 0, sizeof(ctx));
36 11872 : }
37 :
38 13106 : void AES256Decrypt::Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
39 : {
40 13106 : AES256_decrypt(&ctx, 1, plaintext, ciphertext);
41 13106 : }
42 :
43 :
44 : template <typename T>
45 4343 : static int CBCEncrypt(const T& enc, const unsigned char iv[AES_BLOCKSIZE], const unsigned char* data, int size, bool pad, unsigned char* out)
46 : {
47 : int written = 0;
48 4343 : int padsize = size % AES_BLOCKSIZE;
49 4343 : unsigned char mixed[AES_BLOCKSIZE];
50 :
51 4343 : if (!data || !size || !out)
52 0 : return 0;
53 :
54 4343 : if (!pad && padsize != 0)
55 60 : return 0;
56 :
57 4283 : memcpy(mixed, iv, AES_BLOCKSIZE);
58 :
59 : // Write all but the last block
60 8067 : while (written + AES_BLOCKSIZE <= size) {
61 64328 : for (int i = 0; i != AES_BLOCKSIZE; i++)
62 60544 : mixed[i] ^= *data++;
63 3784 : enc.Encrypt(out + written, mixed);
64 3784 : memcpy(mixed, out + written, AES_BLOCKSIZE);
65 : written += AES_BLOCKSIZE;
66 : }
67 4283 : if (pad) {
68 : // For all that remains, pad each byte with the value of the remaining
69 : // space. If there is none, pad by a full block.
70 28995 : for (int i = 0; i != padsize; i++)
71 24720 : mixed[i] ^= *data++;
72 47955 : for (int i = padsize; i != AES_BLOCKSIZE; i++)
73 43680 : mixed[i] ^= AES_BLOCKSIZE - padsize;
74 4275 : enc.Encrypt(out + written, mixed);
75 : written += AES_BLOCKSIZE;
76 4275 : }
77 4283 : return written;
78 4343 : }
79 :
80 : template <typename T>
81 5999 : static int CBCDecrypt(const T& dec, const unsigned char iv[AES_BLOCKSIZE], const unsigned char* data, int size, bool pad, unsigned char* out)
82 : {
83 : int written = 0;
84 : bool fail = false;
85 : const unsigned char* prev = iv;
86 :
87 5999 : if (!data || !size || !out)
88 0 : return 0;
89 :
90 5999 : if (size % AES_BLOCKSIZE != 0)
91 0 : return 0;
92 :
93 : // Decrypt all data. Padding will be checked in the output.
94 19100 : while (written != size) {
95 13101 : dec.Decrypt(out, data + written);
96 222717 : for (int i = 0; i != AES_BLOCKSIZE; i++)
97 209616 : *out++ ^= prev[i];
98 : prev = data + written;
99 13101 : written += AES_BLOCKSIZE;
100 : }
101 :
102 : // When decrypting padding, attempt to run in constant-time
103 5999 : if (pad) {
104 : // If used, padding size is the value of the last decrypted byte. For
105 : // it to be valid, It must be between 1 and AES_BLOCKSIZE.
106 5991 : unsigned char padsize = *--out;
107 5991 : fail = !padsize | (padsize > AES_BLOCKSIZE);
108 :
109 : // If not well-formed, treat it as though there's no padding.
110 5991 : padsize *= !fail;
111 :
112 : // All padding must equal the last byte otherwise it's not well-formed
113 101847 : for (int i = AES_BLOCKSIZE; i != 0; i--)
114 95856 : fail |= ((i > AES_BLOCKSIZE - padsize) & (*out-- != padsize));
115 :
116 5991 : written -= padsize;
117 5991 : }
118 5999 : return written * !fail;
119 5999 : }
120 :
121 8430 : AES256CBCEncrypt::AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
122 4215 : : enc(key), pad(padIn)
123 4215 : {
124 4215 : memcpy(iv, ivIn, AES_BLOCKSIZE);
125 8430 : }
126 :
127 4343 : int AES256CBCEncrypt::Encrypt(const unsigned char* data, int size, unsigned char* out) const
128 : {
129 4343 : return CBCEncrypt(enc, iv, data, size, pad, out);
130 : }
131 :
132 8430 : AES256CBCEncrypt::~AES256CBCEncrypt()
133 4215 : {
134 4215 : memset(iv, 0, sizeof(iv));
135 8430 : }
136 :
137 11862 : AES256CBCDecrypt::AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
138 5931 : : dec(key), pad(padIn)
139 5931 : {
140 5931 : memcpy(iv, ivIn, AES_BLOCKSIZE);
141 11862 : }
142 :
143 :
144 5999 : int AES256CBCDecrypt::Decrypt(const unsigned char* data, int size, unsigned char* out) const
145 : {
146 5999 : return CBCDecrypt(dec, iv, data, size, pad, out);
147 : }
148 :
149 11862 : AES256CBCDecrypt::~AES256CBCDecrypt()
150 5931 : {
151 5931 : memset(iv, 0, sizeof(iv));
152 11862 : }
|