Skip to content

Commit f23a7f5

Browse files
committed
pass iv to AES Encrypt/Decrypt directly. aes-test added
1 parent 48b6234 commit f23a7f5

File tree

10 files changed

+112
-50
lines changed

10 files changed

+112
-50
lines changed

libi2pd/Crypto.cpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,8 @@ namespace crypto
442442
// encrypt
443443
CBCEncryption encryption;
444444
encryption.SetKey (shared);
445-
encryption.SetIV (iv);
446445
encrypted[257] = 0;
447-
encryption.Encrypt (m, 256, encrypted + 258);
446+
encryption.Encrypt (m, 256, iv, encrypted + 258);
448447
EC_POINT_free (p);
449448
BN_CTX_end (ctx);
450449
BN_CTX_free (ctx);
@@ -477,8 +476,7 @@ namespace crypto
477476
uint8_t m[256];
478477
CBCDecryption decryption;
479478
decryption.SetKey (shared);
480-
decryption.SetIV (iv);
481-
decryption.Decrypt (encrypted + 258, 256, m);
479+
decryption.Decrypt (encrypted + 258, 256, iv, m);
482480
// verify and copy
483481
uint8_t hash[32];
484482
SHA256 (m + 33, 222, hash);
@@ -560,7 +558,6 @@ namespace crypto
560558
CBCEncryption::CBCEncryption ()
561559
{
562560
m_Ctx = EVP_CIPHER_CTX_new ();
563-
//memset ((uint8_t *)m_LastBlock, 0, 16);
564561
}
565562

566563
CBCEncryption::~CBCEncryption ()
@@ -569,10 +566,10 @@ namespace crypto
569566
EVP_CIPHER_CTX_free (m_Ctx);
570567
}
571568

572-
void CBCEncryption::Encrypt (const uint8_t * in, std::size_t len, uint8_t * out)
569+
void CBCEncryption::Encrypt (const uint8_t * in, size_t len, const uint8_t * iv, uint8_t * out)
573570
{
574571
// len/16
575-
EVP_EncryptInit_ex (m_Ctx, EVP_aes_256_cbc(), NULL, m_Key, m_IV);
572+
EVP_EncryptInit_ex (m_Ctx, EVP_aes_256_cbc(), NULL, m_Key, iv);
576573
EVP_CIPHER_CTX_set_padding (m_Ctx, 0);
577574
int l;
578575
EVP_EncryptUpdate (m_Ctx, out, &l, in, len);
@@ -582,7 +579,6 @@ namespace crypto
582579
CBCDecryption::CBCDecryption ()
583580
{
584581
m_Ctx = EVP_CIPHER_CTX_new ();
585-
//memset ((uint8_t *)m_IV, 0, 16);
586582
}
587583

588584
CBCDecryption::~CBCDecryption ()
@@ -591,10 +587,10 @@ namespace crypto
591587
EVP_CIPHER_CTX_free (m_Ctx);
592588
}
593589

594-
void CBCDecryption::Decrypt (const uint8_t * in, std::size_t len, uint8_t * out)
590+
void CBCDecryption::Decrypt (const uint8_t * in, size_t len, const uint8_t * iv, uint8_t * out)
595591
{
596592
// len/16
597-
EVP_DecryptInit_ex (m_Ctx, EVP_aes_256_cbc(), NULL, m_Key, m_IV);
593+
EVP_DecryptInit_ex (m_Ctx, EVP_aes_256_cbc(), NULL, m_Key, iv);
598594
EVP_CIPHER_CTX_set_padding (m_Ctx, 0);
599595
int l;
600596
EVP_DecryptUpdate (m_Ctx, out, &l, in, len);
@@ -603,18 +599,18 @@ namespace crypto
603599

604600
void TunnelEncryption::Encrypt (const uint8_t * in, uint8_t * out)
605601
{
606-
m_IVEncryption.Encrypt (in, out); // iv
607-
m_LayerEncryption.SetIV (out);
608-
m_LayerEncryption.Encrypt (in + 16, i2p::tunnel::TUNNEL_DATA_ENCRYPTED_SIZE, out + 16); // data
609-
m_IVEncryption.Encrypt (out, out); // double iv
602+
uint8_t iv[16];
603+
m_IVEncryption.Encrypt (in, iv); // iv
604+
m_LayerEncryption.Encrypt (in + 16, i2p::tunnel::TUNNEL_DATA_ENCRYPTED_SIZE, iv, out + 16); // data
605+
m_IVEncryption.Encrypt (iv, out); // double iv
610606
}
611607

612608
void TunnelDecryption::Decrypt (const uint8_t * in, uint8_t * out)
613609
{
614-
m_IVDecryption.Decrypt (in, out); // iv
615-
m_LayerDecryption.SetIV (out);
616-
m_LayerDecryption.Decrypt (in + 16, i2p::tunnel::TUNNEL_DATA_ENCRYPTED_SIZE, out + 16); // data
617-
m_IVDecryption.Decrypt (out, out); // double iv
610+
uint8_t iv[16];
611+
m_IVDecryption.Decrypt (in, iv); // iv
612+
m_LayerDecryption.Decrypt (in + 16, i2p::tunnel::TUNNEL_DATA_ENCRYPTED_SIZE, iv, out + 16); // data
613+
m_IVDecryption.Decrypt (iv, out); // double iv
618614
}
619615

620616
// AEAD/ChaCha20/Poly1305

libi2pd/Crypto.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ namespace crypto
9393
ECBEncryption ();
9494
~ECBEncryption ();
9595

96-
void SetKey (const AESKey& key) { m_Key = key; };
96+
void SetKey (const uint8_t * key) { m_Key = key; };
9797
void Encrypt(const uint8_t * in, uint8_t * out);
9898

9999
private:
@@ -109,7 +109,7 @@ namespace crypto
109109
ECBDecryption ();
110110
~ECBDecryption ();
111111

112-
void SetKey (const AESKey& key) { m_Key = key; };
112+
void SetKey (const uint8_t * key) { m_Key = key; };
113113
void Decrypt (const uint8_t * in, uint8_t * out);
114114

115115
private:
@@ -125,15 +125,12 @@ namespace crypto
125125
CBCEncryption ();
126126
~CBCEncryption ();
127127

128-
void SetKey (const AESKey& key) { m_Key = key; }; // 32 bytes
129-
void SetIV (const uint8_t * iv) { m_IV = iv; }; // 16 bytes
130-
131-
void Encrypt (const uint8_t * in, std::size_t len, uint8_t * out);
128+
void SetKey (const uint8_t * key) { m_Key = key; }; // 32 bytes
129+
void Encrypt (const uint8_t * in, size_t len, const uint8_t * iv, uint8_t * out);
132130

133131
private:
134132

135133
AESKey m_Key;
136-
i2p::data::Tag<16> m_IV;
137134
EVP_CIPHER_CTX * m_Ctx;
138135
};
139136

@@ -144,15 +141,12 @@ namespace crypto
144141
CBCDecryption ();
145142
~CBCDecryption ();
146143

147-
void SetKey (const AESKey& key) { m_Key = key; }; // 32 bytes
148-
void SetIV (const uint8_t * iv) { m_IV = iv; }; // 16 bytes
149-
150-
void Decrypt (const uint8_t * in, std::size_t len, uint8_t * out);
144+
void SetKey (const uint8_t * key) { m_Key = key; }; // 32 bytes
145+
void Decrypt (const uint8_t * in, size_t len, const uint8_t * iv, uint8_t * out);
151146

152147
private:
153148

154149
AESKey m_Key;
155-
i2p::data::Tag<16> m_IV;
156150
EVP_CIPHER_CTX * m_Ctx;
157151
};
158152

libi2pd/Garlic.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ namespace garlic
160160
uint8_t iv[32]; // IV is first 16 bytes
161161
SHA256(elGamal.preIV, 32, iv);
162162
m_Destination->Encrypt ((uint8_t *)&elGamal, buf);
163-
m_Encryption.SetIV (iv);
163+
m_IV = iv;
164164
buf += 514;
165165
len += 514;
166166
}
@@ -170,7 +170,7 @@ namespace garlic
170170
memcpy (buf, tag, 32);
171171
uint8_t iv[32]; // IV is first 16 bytes
172172
SHA256(tag, 32, iv);
173-
m_Encryption.SetIV (iv);
173+
m_IV = iv;
174174
buf += 32;
175175
len += 32;
176176
}
@@ -210,7 +210,7 @@ namespace garlic
210210
size_t rem = blockSize % 16;
211211
if (rem)
212212
blockSize += (16-rem); //padding
213-
m_Encryption.Encrypt(buf, blockSize, buf);
213+
m_Encryption.Encrypt(buf, blockSize, m_IV, buf);
214214
return blockSize;
215215
}
216216

@@ -514,8 +514,7 @@ namespace garlic
514514
{
515515
uint8_t iv[32]; // IV is first 16 bytes
516516
SHA256(buf, 32, iv);
517-
decryption->SetIV (iv);
518-
decryption->Decrypt (buf + 32, length - 32, buf + 32);
517+
decryption->Decrypt (buf + 32, length - 32, iv, buf + 32);
519518
HandleAESBlock (buf + 32, length - 32, decryption, msg->from);
520519
found = true;
521520
}
@@ -533,8 +532,7 @@ namespace garlic
533532
auto decryption = std::make_shared<AESDecryption>(elGamal.sessionKey);
534533
uint8_t iv[32]; // IV is first 16 bytes
535534
SHA256(elGamal.preIV, 32, iv);
536-
decryption->SetIV (iv);
537-
decryption->Decrypt(buf + 514, length - 514, buf + 514);
535+
decryption->Decrypt(buf + 514, length - 514, iv, buf + 514);
538536
HandleAESBlock (buf + 514, length - 514, decryption, msg->from);
539537
}
540538
else if (SupportsEncryptionType (i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD))

libi2pd/Garlic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ namespace garlic
205205
std::map<uint32_t, std::unique_ptr<UnconfirmedTags> > m_UnconfirmedTagsMsgs; // msgID->tags
206206

207207
i2p::crypto::CBCEncryption m_Encryption;
208+
i2p::data::Tag<16> m_IV;
208209

209210
public:
210211

libi2pd/NTCP2.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ namespace transport
120120
// encrypt X
121121
i2p::crypto::CBCEncryption encryption;
122122
encryption.SetKey (m_RemoteIdentHash);
123-
encryption.SetIV (m_IV);
124-
encryption.Encrypt (GetPub (), 32, m_SessionRequestBuffer); // X
123+
encryption.Encrypt (GetPub (), 32, m_IV, m_SessionRequestBuffer); // X
125124
memcpy (m_IV, m_SessionRequestBuffer + 16, 16); // save last block as IV for SessionCreated
126125
// encryption key for next block
127126
if (!KDF1Alice ()) return false;
@@ -161,8 +160,7 @@ namespace transport
161160
// encrypt Y
162161
i2p::crypto::CBCEncryption encryption;
163162
encryption.SetKey (i2p::context.GetIdentHash ());
164-
encryption.SetIV (m_IV);
165-
encryption.Encrypt (GetPub (), 32, m_SessionCreatedBuffer); // Y
163+
encryption.Encrypt (GetPub (), 32, m_IV, m_SessionCreatedBuffer); // Y
166164
// encryption key for next block (m_K)
167165
if (!KDF2Bob ()) return false;
168166
uint8_t options[16];
@@ -208,8 +206,7 @@ namespace transport
208206
// decrypt X
209207
i2p::crypto::CBCDecryption decryption;
210208
decryption.SetKey (i2p::context.GetIdentHash ());
211-
decryption.SetIV (i2p::context.GetNTCP2IV ());
212-
decryption.Decrypt (m_SessionRequestBuffer, 32, GetRemotePub ());
209+
decryption.Decrypt (m_SessionRequestBuffer, 32, i2p::context.GetNTCP2IV (), GetRemotePub ());
213210
memcpy (m_IV, m_SessionRequestBuffer + 16, 16); // save last block as IV for SessionCreated
214211
// decryption key for next block
215212
if (!KDF1Bob ())
@@ -268,8 +265,7 @@ namespace transport
268265
// decrypt Y
269266
i2p::crypto::CBCDecryption decryption;
270267
decryption.SetKey (m_RemoteIdentHash);
271-
decryption.SetIV (m_IV);
272-
decryption.Decrypt (m_SessionCreatedBuffer, 32, GetRemotePub ());
268+
decryption.Decrypt (m_SessionCreatedBuffer, 32, m_IV, GetRemotePub ());
273269
// decryption key for next block (m_K)
274270
if (!KDF2Alice ())
275271
{

libi2pd/TransitTunnel.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,7 @@ namespace tunnel
434434
else
435435
{
436436
encryption.SetKey (clearText + ECIES_BUILD_REQUEST_RECORD_REPLY_KEY_OFFSET);
437-
encryption.SetIV (clearText + ECIES_BUILD_REQUEST_RECORD_REPLY_IV_OFFSET);
438-
encryption.Encrypt(reply, TUNNEL_BUILD_RECORD_SIZE, reply);
437+
encryption.Encrypt(reply, TUNNEL_BUILD_RECORD_SIZE, clearText + ECIES_BUILD_REQUEST_RECORD_REPLY_IV_OFFSET, reply);
439438
}
440439
}
441440
return true;

libi2pd/TunnelConfig.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ namespace tunnel
7979
uint8_t * record = records + index*TUNNEL_BUILD_RECORD_SIZE;
8080
i2p::crypto::CBCDecryption decryption;
8181
decryption.SetKey (replyKey);
82-
decryption.SetIV (replyIV);
83-
decryption.Decrypt(record, TUNNEL_BUILD_RECORD_SIZE, record);
82+
decryption.Decrypt(record, TUNNEL_BUILD_RECORD_SIZE, replyIV, record);
8483
}
8584

8685
void ECIESTunnelHopConfig::EncryptECIES (const uint8_t * plainText, size_t len, uint8_t * encrypted)

tests/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ set(test-eddsa_SRCS
6565
test-eddsa.cpp
6666
)
6767

68+
set(test-aes_SRCS
69+
test-aes.cpp
70+
)
71+
6872
add_executable(test-http-merge_chunked ${test-http-merge_chunked_SRCS})
6973
add_executable(test-http-req ${test-http-req_SRCS})
7074
add_executable(test-http-res ${test-http-res_SRCS})
@@ -77,6 +81,7 @@ add_executable(test-aeadchacha20poly1305 ${test-aeadchacha20poly1305_SRCS})
7781
add_executable(test-blinding ${test-blinding_SRCS})
7882
add_executable(test-elligator ${test-elligator_SRCS})
7983
add_executable(test-eddsa ${test-eddsa_SRCS})
84+
add_executable(test-aes ${test-aes_SRCS})
8085

8186
set(LIBS
8287
libi2pd
@@ -101,6 +106,7 @@ target_link_libraries(test-aeadchacha20poly1305 ${LIBS})
101106
target_link_libraries(test-blinding ${LIBS})
102107
target_link_libraries(test-elligator ${LIBS})
103108
target_link_libraries(test-eddsa ${LIBS})
109+
target_link_libraries(test-aes ${LIBS})
104110

105111
add_test(test-http-merge_chunked ${TEST_PATH}/test-http-merge_chunked)
106112
add_test(test-http-req ${TEST_PATH}/test-http-req)
@@ -114,3 +120,4 @@ add_test(test-aeadchacha20poly1305 ${TEST_PATH}/test-aeadchacha20poly1305)
114120
add_test(test-blinding ${TEST_PATH}/test-blinding)
115121
add_test(test-elligator ${TEST_PATH}/test-elligator)
116122
add_test(test-eddsa ${TEST_PATH}/test-eddsa)
123+
add_test(test-aes ${TEST_PATH}/test-aes)

tests/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LIBI2PD = ../libi2pd.a
88
TESTS = \
99
test-http-merge_chunked test-http-req test-http-res test-http-url test-http-url_decode \
1010
test-gost test-gost-sig test-base-64 test-aeadchacha20poly1305 test-blinding \
11-
test-elligator test-eddsa
11+
test-elligator test-eddsa test-aes
1212

1313
ifneq (, $(findstring mingw, $(SYS))$(findstring windows-gnu, $(SYS))$(findstring cygwin, $(SYS)))
1414
CXXFLAGS += -DWIN32_LEAN_AND_MEAN
@@ -56,6 +56,9 @@ test-elligator: test-elligator.cpp $(LIBI2PD)
5656
test-eddsa: test-eddsa.cpp $(LIBI2PD)
5757
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
5858

59+
test-aes: test-aes.cpp $(LIBI2PD)
60+
$(CXX) $(CXXFLAGS) $(NEEDED_CXXFLAGS) $(INCFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
61+
5962
run: $(TESTS)
6063
@for TEST in $(TESTS); do echo Running $$TEST; ./$$TEST ; done
6164

tests/test-aes.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <cassert>
2+
#include <inttypes.h>
3+
#include <string.h>
4+
5+
#include "Crypto.h"
6+
7+
uint8_t ecb_key1[32] =
8+
{
9+
0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
10+
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
11+
};
12+
13+
uint8_t ecb_plain1[16] =
14+
{
15+
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
16+
};
17+
18+
uint8_t ecb_cipher1[16] =
19+
{
20+
0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c, 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8
21+
};
22+
23+
uint8_t cbc_key1[32] =
24+
{
25+
0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
26+
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
27+
};
28+
29+
uint8_t cbc_iv1[16] =
30+
{
31+
0xF5, 0x8C, 0x4C, 0x04, 0xD6, 0xE5, 0xF1, 0xBA, 0x77, 0x9E, 0xAB, 0xFB, 0x5F, 0x7B, 0xFB, 0xD6
32+
};
33+
34+
uint8_t cbc_plain1[16] =
35+
{
36+
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51
37+
};
38+
39+
uint8_t cbc_cipher1[16] =
40+
{
41+
0x9c, 0xfc, 0x4e, 0x96, 0x7e, 0xdb, 0x80, 0x8d, 0x67, 0x9f, 0x77, 0x7b, 0xc6, 0x70, 0x2c, 0x7d
42+
};
43+
44+
int main ()
45+
{
46+
// ECB encrypt test1
47+
i2p::crypto::ECBEncryption ecbencryption;
48+
ecbencryption.SetKey (ecb_key1);
49+
uint8_t out[16];
50+
ecbencryption.Encrypt (ecb_plain1, out);
51+
assert (memcmp (ecb_cipher1, out, 16) == 0);
52+
53+
// ECB decrypt test1
54+
i2p::crypto::ECBDecryption ecbdecryption;
55+
ecbdecryption.SetKey (ecb_key1);
56+
ecbdecryption.Decrypt (ecb_cipher1, out);
57+
assert (memcmp (ecb_plain1, out, 16) == 0);
58+
// CBC encrypt test
59+
i2p::crypto::CBCEncryption cbcencryption;
60+
cbcencryption.SetKey (cbc_key1);
61+
cbcencryption.Encrypt (cbc_plain1, 16, cbc_iv1, out);
62+
assert (memcmp (cbc_cipher1, out, 16) == 0);
63+
// CBC decrypt test
64+
i2p::crypto::CBCDecryption cbcdecryption;
65+
cbcdecryption.SetKey (cbc_key1);
66+
cbcdecryption.Decrypt (cbc_cipher1, 16, cbc_iv1, out);
67+
assert (memcmp (cbc_plain1, out, 16) == 0);
68+
}
69+

0 commit comments

Comments
 (0)