forked from csumathdyj/libdigest-hmac-sha256-cxx11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cipher-aes-test.cpp
122 lines (114 loc) · 3.26 KB
/
cipher-aes-test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <cstdlib>
#include <cstdint>
#include "cipher-aes.hpp"
#include "taptests.hpp"
namespace {
using AES_BLOCK = typename cipher::AES::BLOCK;
// FIPS 197 Advanced Encryption Standard (AES)
// Appendix C - Example Vectors
// C.1 AES-128
void
test_key128 (test::simple& t)
{
std::array<std::uint8_t,16> const key128 {
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
};
AES_BLOCK const plain {
{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}
};
AES_BLOCK const cipher {
{0x69, 0xc4, 0xe0, 0xd8, 0x6a, 0x7b, 0x04, 0x30,
0xd8, 0xcd, 0xb7, 0x80, 0x70, 0xb4, 0xc5, 0x5a},
};
{
cipher::AES aes;
AES_BLOCK got_cipher;
aes.set_encrypt_key128 (key128);
aes.encrypt (plain, got_cipher);
t.ok (cipher == got_cipher, "encrypt key128");
}
{
cipher::AES aes;
AES_BLOCK got_plain;
aes.set_decrypt_key128 (key128);
aes.decrypt (cipher, got_plain);
t.ok (plain == got_plain, "decrypt key128");
}
}
// C.2 AES-192
void
test_key192 (test::simple& t)
{
std::array<std::uint8_t,24> const key192 {
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17},
};
AES_BLOCK const plain {
{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}
};
AES_BLOCK const cipher {
{0xdd, 0xa9, 0x7c, 0xa4, 0x86, 0x4c, 0xdf, 0xe0,
0x6e, 0xaf, 0x70, 0xa0, 0xec, 0x0d, 0x71, 0x91},
};
{
cipher::AES aes;
AES_BLOCK got_cipher;
aes.set_encrypt_key192 (key192);
aes.encrypt (plain, got_cipher);
t.ok (cipher == got_cipher, "encrypt key192");
}
{
cipher::AES aes;
AES_BLOCK got_plain;
aes.set_decrypt_key192 (key192);
aes.decrypt (cipher, got_plain);
t.ok (plain == got_plain, "decrypt key192");
}
}
// C.3 AES-256
void
test_key256 (test::simple& t)
{
std::array<std::uint8_t,32> const key256 {
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f},
};
AES_BLOCK const plain {
{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}
};
AES_BLOCK const cipher {
{0x8e, 0xa2, 0xb7, 0xca, 0x51, 0x67, 0x45, 0xbf,
0xea, 0xfc, 0x49, 0x90, 0x4b, 0x49, 0x60, 0x89},
};
{
cipher::AES aes;
AES_BLOCK got_cipher;
aes.set_encrypt_key256 (key256);
aes.encrypt (plain, got_cipher);
t.ok (cipher == got_cipher, "encrypt key256");
}
{
cipher::AES aes;
AES_BLOCK got_plain;
aes.set_decrypt_key256 (key256);
aes.decrypt (cipher, got_plain);
t.ok (plain == got_plain, "decrypt key256");
}
}
}//namespace
int
main ()
{
test::simple t (6);
test_key128 (t);
test_key192 (t);
test_key256 (t);
return t.done_testing ();
}