Skip to content

Commit edc68d4

Browse files
committed
Merge #15663: crypto: Remove unused AES-128 code
f6ee177 Remove unused AES-128 code (practicalswift) Pull request description: Remove unused AES-128 code. As far as I can tell this AES-128 code has never been in use in the project (outside of testing/benchmarking). The AES-256 code is used in `CCrypter::Encrypt`/`CCrypter::Decrypt` (`src/wallet/crypter.cpp`). Trivia: 0.15% of the project's C++ LOC count (excluding dependencies) is trimmed off: ``` $ LOC_BEFORE=$(git grep -I "" HEAD~1 -- "*.cpp" "*.h" ":(exclude)src/leveldb/" ":(exclude)src/secp256k1/" ":(exclude)src/univalue/" | wc -l) $ LOC_AFTER=$(git grep -I "" -- "*.cpp" "*.h" ":(exclude)src/leveldb/" ":(exclude)src/secp256k1/" ":(exclude)src/univalue/" | wc -l) $ bc <<< "scale=4; ${LOC_AFTER}/${LOC_BEFORE}" .9985 ``` :-) Tree-SHA512: 9588a3cd795a89ef658b8ee7323865f57723cb4ed9560c21de793f82d35e2835059e7d6d0705e99e3d16bf6b2a444b4bf19568d50174ff3776caf8a3168f5c85
2 parents 9e7dc68 + f6ee177 commit edc68d4

File tree

3 files changed

+0
-200
lines changed

3 files changed

+0
-200
lines changed

src/crypto/aes.cpp

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,6 @@ extern "C" {
1212
#include <crypto/ctaes/ctaes.c>
1313
}
1414

15-
AES128Encrypt::AES128Encrypt(const unsigned char key[16])
16-
{
17-
AES128_init(&ctx, key);
18-
}
19-
20-
AES128Encrypt::~AES128Encrypt()
21-
{
22-
memset(&ctx, 0, sizeof(ctx));
23-
}
24-
25-
void AES128Encrypt::Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
26-
{
27-
AES128_encrypt(&ctx, 1, ciphertext, plaintext);
28-
}
29-
30-
AES128Decrypt::AES128Decrypt(const unsigned char key[16])
31-
{
32-
AES128_init(&ctx, key);
33-
}
34-
35-
AES128Decrypt::~AES128Decrypt()
36-
{
37-
memset(&ctx, 0, sizeof(ctx));
38-
}
39-
40-
void AES128Decrypt::Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
41-
{
42-
AES128_decrypt(&ctx, 1, plaintext, ciphertext);
43-
}
44-
4515
AES256Encrypt::AES256Encrypt(const unsigned char key[32])
4616
{
4717
AES256_init(&ctx, key);
@@ -182,35 +152,3 @@ AES256CBCDecrypt::~AES256CBCDecrypt()
182152
{
183153
memset(iv, 0, sizeof(iv));
184154
}
185-
186-
AES128CBCEncrypt::AES128CBCEncrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
187-
: enc(key), pad(padIn)
188-
{
189-
memcpy(iv, ivIn, AES_BLOCKSIZE);
190-
}
191-
192-
AES128CBCEncrypt::~AES128CBCEncrypt()
193-
{
194-
memset(iv, 0, AES_BLOCKSIZE);
195-
}
196-
197-
int AES128CBCEncrypt::Encrypt(const unsigned char* data, int size, unsigned char* out) const
198-
{
199-
return CBCEncrypt(enc, iv, data, size, pad, out);
200-
}
201-
202-
AES128CBCDecrypt::AES128CBCDecrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
203-
: dec(key), pad(padIn)
204-
{
205-
memcpy(iv, ivIn, AES_BLOCKSIZE);
206-
}
207-
208-
AES128CBCDecrypt::~AES128CBCDecrypt()
209-
{
210-
memset(iv, 0, AES_BLOCKSIZE);
211-
}
212-
213-
int AES128CBCDecrypt::Decrypt(const unsigned char* data, int size, unsigned char* out) const
214-
{
215-
return CBCDecrypt(dec, iv, data, size, pad, out);
216-
}

src/crypto/aes.h

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,8 @@ extern "C" {
1212
}
1313

1414
static const int AES_BLOCKSIZE = 16;
15-
static const int AES128_KEYSIZE = 16;
1615
static const int AES256_KEYSIZE = 32;
1716

18-
/** An encryption class for AES-128. */
19-
class AES128Encrypt
20-
{
21-
private:
22-
AES128_ctx ctx;
23-
24-
public:
25-
explicit AES128Encrypt(const unsigned char key[16]);
26-
~AES128Encrypt();
27-
void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const;
28-
};
29-
30-
/** A decryption class for AES-128. */
31-
class AES128Decrypt
32-
{
33-
private:
34-
AES128_ctx ctx;
35-
36-
public:
37-
explicit AES128Decrypt(const unsigned char key[16]);
38-
~AES128Decrypt();
39-
void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const;
40-
};
41-
4217
/** An encryption class for AES-256. */
4318
class AES256Encrypt
4419
{
@@ -89,30 +64,4 @@ class AES256CBCDecrypt
8964
unsigned char iv[AES_BLOCKSIZE];
9065
};
9166

92-
class AES128CBCEncrypt
93-
{
94-
public:
95-
AES128CBCEncrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
96-
~AES128CBCEncrypt();
97-
int Encrypt(const unsigned char* data, int size, unsigned char* out) const;
98-
99-
private:
100-
const AES128Encrypt enc;
101-
const bool pad;
102-
unsigned char iv[AES_BLOCKSIZE];
103-
};
104-
105-
class AES128CBCDecrypt
106-
{
107-
public:
108-
AES128CBCDecrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
109-
~AES128CBCDecrypt();
110-
int Decrypt(const unsigned char* data, int size, unsigned char* out) const;
111-
112-
private:
113-
const AES128Decrypt dec;
114-
const bool pad;
115-
unsigned char iv[AES_BLOCKSIZE];
116-
};
117-
11867
#endif // BITCOIN_CRYPTO_AES_H

src/test/crypto_tests.cpp

Lines changed: 0 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,6 @@ static void TestHMACSHA512(const std::string &hexkey, const std::string &hexin,
6767
TestVector(CHMAC_SHA512(key.data(), key.size()), ParseHex(hexin), ParseHex(hexout));
6868
}
6969

70-
static void TestAES128(const std::string &hexkey, const std::string &hexin, const std::string &hexout)
71-
{
72-
std::vector<unsigned char> key = ParseHex(hexkey);
73-
std::vector<unsigned char> in = ParseHex(hexin);
74-
std::vector<unsigned char> correctout = ParseHex(hexout);
75-
std::vector<unsigned char> buf, buf2;
76-
77-
assert(key.size() == 16);
78-
assert(in.size() == 16);
79-
assert(correctout.size() == 16);
80-
AES128Encrypt enc(key.data());
81-
buf.resize(correctout.size());
82-
buf2.resize(correctout.size());
83-
enc.Encrypt(buf.data(), in.data());
84-
BOOST_CHECK_EQUAL(HexStr(buf), HexStr(correctout));
85-
AES128Decrypt dec(key.data());
86-
dec.Decrypt(buf2.data(), buf.data());
87-
BOOST_CHECK_EQUAL(HexStr(buf2), HexStr(in));
88-
}
89-
9070
static void TestAES256(const std::string &hexkey, const std::string &hexin, const std::string &hexout)
9171
{
9272
std::vector<unsigned char> key = ParseHex(hexkey);
@@ -106,47 +86,6 @@ static void TestAES256(const std::string &hexkey, const std::string &hexin, cons
10686
BOOST_CHECK(buf == in);
10787
}
10888

109-
static void TestAES128CBC(const std::string &hexkey, const std::string &hexiv, bool pad, const std::string &hexin, const std::string &hexout)
110-
{
111-
std::vector<unsigned char> key = ParseHex(hexkey);
112-
std::vector<unsigned char> iv = ParseHex(hexiv);
113-
std::vector<unsigned char> in = ParseHex(hexin);
114-
std::vector<unsigned char> correctout = ParseHex(hexout);
115-
std::vector<unsigned char> realout(in.size() + AES_BLOCKSIZE);
116-
117-
// Encrypt the plaintext and verify that it equals the cipher
118-
AES128CBCEncrypt enc(key.data(), iv.data(), pad);
119-
int size = enc.Encrypt(in.data(), in.size(), realout.data());
120-
realout.resize(size);
121-
BOOST_CHECK(realout.size() == correctout.size());
122-
BOOST_CHECK_MESSAGE(realout == correctout, HexStr(realout) + std::string(" != ") + hexout);
123-
124-
// Decrypt the cipher and verify that it equals the plaintext
125-
std::vector<unsigned char> decrypted(correctout.size());
126-
AES128CBCDecrypt dec(key.data(), iv.data(), pad);
127-
size = dec.Decrypt(correctout.data(), correctout.size(), decrypted.data());
128-
decrypted.resize(size);
129-
BOOST_CHECK(decrypted.size() == in.size());
130-
BOOST_CHECK_MESSAGE(decrypted == in, HexStr(decrypted) + std::string(" != ") + hexin);
131-
132-
// Encrypt and re-decrypt substrings of the plaintext and verify that they equal each-other
133-
for(std::vector<unsigned char>::iterator i(in.begin()); i != in.end(); ++i)
134-
{
135-
std::vector<unsigned char> sub(i, in.end());
136-
std::vector<unsigned char> subout(sub.size() + AES_BLOCKSIZE);
137-
int _size = enc.Encrypt(sub.data(), sub.size(), subout.data());
138-
if (_size != 0)
139-
{
140-
subout.resize(_size);
141-
std::vector<unsigned char> subdecrypted(subout.size());
142-
_size = dec.Decrypt(subout.data(), subout.size(), subdecrypted.data());
143-
subdecrypted.resize(_size);
144-
BOOST_CHECK(decrypted.size() == in.size());
145-
BOOST_CHECK_MESSAGE(subdecrypted == sub, HexStr(subdecrypted) + std::string(" != ") + HexStr(sub));
146-
}
147-
}
148-
}
149-
15089
static void TestAES256CBC(const std::string &hexkey, const std::string &hexiv, bool pad, const std::string &hexin, const std::string &hexout)
15190
{
15291
std::vector<unsigned char> key = ParseHex(hexkey);
@@ -440,42 +379,16 @@ BOOST_AUTO_TEST_CASE(hmac_sha512_testvectors) {
440379

441380
BOOST_AUTO_TEST_CASE(aes_testvectors) {
442381
// AES test vectors from FIPS 197.
443-
TestAES128("000102030405060708090a0b0c0d0e0f", "00112233445566778899aabbccddeeff", "69c4e0d86a7b0430d8cdb78070b4c55a");
444382
TestAES256("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f", "00112233445566778899aabbccddeeff", "8ea2b7ca516745bfeafc49904b496089");
445383

446384
// AES-ECB test vectors from NIST sp800-38a.
447-
TestAES128("2b7e151628aed2a6abf7158809cf4f3c", "6bc1bee22e409f96e93d7e117393172a", "3ad77bb40d7a3660a89ecaf32466ef97");
448-
TestAES128("2b7e151628aed2a6abf7158809cf4f3c", "ae2d8a571e03ac9c9eb76fac45af8e51", "f5d3d58503b9699de785895a96fdbaaf");
449-
TestAES128("2b7e151628aed2a6abf7158809cf4f3c", "30c81c46a35ce411e5fbc1191a0a52ef", "43b1cd7f598ece23881b00e3ed030688");
450-
TestAES128("2b7e151628aed2a6abf7158809cf4f3c", "f69f2445df4f9b17ad2b417be66c3710", "7b0c785e27e8ad3f8223207104725dd4");
451385
TestAES256("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "6bc1bee22e409f96e93d7e117393172a", "f3eed1bdb5d2a03c064b5a7e3db181f8");
452386
TestAES256("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "ae2d8a571e03ac9c9eb76fac45af8e51", "591ccb10d410ed26dc5ba74a31362870");
453387
TestAES256("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "30c81c46a35ce411e5fbc1191a0a52ef", "b6ed21b99ca6f4f9f153e7b1beafed1d");
454388
TestAES256("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", "f69f2445df4f9b17ad2b417be66c3710", "23304b7a39f9f3ff067d8d8f9e24ecc7");
455389
}
456390

457391
BOOST_AUTO_TEST_CASE(aes_cbc_testvectors) {
458-
459-
// NIST AES CBC 128-bit encryption test-vectors
460-
TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "000102030405060708090A0B0C0D0E0F", false, \
461-
"6bc1bee22e409f96e93d7e117393172a", "7649abac8119b246cee98e9b12e9197d");
462-
TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "7649ABAC8119B246CEE98E9B12E9197D", false, \
463-
"ae2d8a571e03ac9c9eb76fac45af8e51", "5086cb9b507219ee95db113a917678b2");
464-
TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "5086cb9b507219ee95db113a917678b2", false, \
465-
"30c81c46a35ce411e5fbc1191a0a52ef", "73bed6b8e3c1743b7116e69e22229516");
466-
TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "73bed6b8e3c1743b7116e69e22229516", false, \
467-
"f69f2445df4f9b17ad2b417be66c3710", "3ff1caa1681fac09120eca307586e1a7");
468-
469-
// The same vectors with padding enabled
470-
TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "000102030405060708090A0B0C0D0E0F", true, \
471-
"6bc1bee22e409f96e93d7e117393172a", "7649abac8119b246cee98e9b12e9197d8964e0b149c10b7b682e6e39aaeb731c");
472-
TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "7649ABAC8119B246CEE98E9B12E9197D", true, \
473-
"ae2d8a571e03ac9c9eb76fac45af8e51", "5086cb9b507219ee95db113a917678b255e21d7100b988ffec32feeafaf23538");
474-
TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "5086cb9b507219ee95db113a917678b2", true, \
475-
"30c81c46a35ce411e5fbc1191a0a52ef", "73bed6b8e3c1743b7116e69e22229516f6eccda327bf8e5ec43718b0039adceb");
476-
TestAES128CBC("2b7e151628aed2a6abf7158809cf4f3c", "73bed6b8e3c1743b7116e69e22229516", true, \
477-
"f69f2445df4f9b17ad2b417be66c3710", "3ff1caa1681fac09120eca307586e1a78cb82807230e1321d3fae00d18cc2012");
478-
479392
// NIST AES CBC 256-bit encryption test-vectors
480393
TestAES256CBC("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4", \
481394
"000102030405060708090A0B0C0D0E0F", false, "6bc1bee22e409f96e93d7e117393172a", \

0 commit comments

Comments
 (0)