-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
158 lines (126 loc) · 3.14 KB
/
test.c
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Advanced Encryption Standard
* @author Dani Huertas
* @email [email protected]
*
* Based on the document FIPS PUB 197
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aes.h"
int main()
{
uint8_t i;
/*
* Appendix A - Key Expansion Examples
*/
/* 128 bits */
/* uint8_t key[] = {
0x2b, 0x7e, 0x15, 0x16,
0x28, 0xae, 0xd2, 0xa6,
0xab, 0xf7, 0x15, 0x88,
0x09, 0xcf, 0x4f, 0x3c}; */
/* 192 bits */
/* uint8_t key[] = {
0x8e, 0x73, 0xb0, 0xf7,
0xda, 0x0e, 0x64, 0x52,
0xc8, 0x10, 0xf3, 0x2b,
0x80, 0x90, 0x79, 0xe5,
0x62, 0xf8, 0xea, 0xd2,
0x52, 0x2c, 0x6b, 0x7b}; */
/* 256 bits */
/* uint8_t key[] = {
0x60, 0x3d, 0xeb, 0x10,
0x15, 0xca, 0x71, 0xbe,
0x2b, 0x73, 0xae, 0xf0,
0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07,
0x3b, 0x61, 0x08, 0xd7,
0x2d, 0x98, 0x10, 0xa3,
0x09, 0x14, 0xdf, 0xf4};
*/
/* uint8_t in[] = {
0x32, 0x43, 0xf6, 0xa8,
0x88, 0x5a, 0x30, 0x8d,
0x31, 0x31, 0x98, 0xa2,
0xe0, 0x37, 0x07, 0x34}; // 128
*/
/*
* Appendix C - Example Vectors
*/
/* 128 bit key */
/* uint8_t key[] = {
0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f}; */
/* 192 bit key */
/* uint8_t key[] = {
0x00, 0x01, 0x02, 0x03,
0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b,
0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17}; */
/* 256 bit key */
uint8_t key[] =
{
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
};
uint8_t in[] =
{
0x00, 0x11, 0x22, 0x33,
0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb,
0xcc, 0xdd, 0xee, 0xff
};
uint8_t out[16];
uint8_t *w; // expanded key
w = aes_init(sizeof(key));
aes_key_expansion(key, w);
printf("Plaintext message:\n");
for (i = 0; i < 4; i++) {
printf("%02x %02x %02x %02x ", in[4*i+0], in[4*i+1], in[4*i+2], in[4*i+3]);
}
printf("\n");
aes_cipher(in /* in */, out /* out */, w /* expanded key */);
printf("Ciphered message:\n");
for (i = 0; i < 4; i++) {
printf("%02x %02x %02x %02x ", out[4*i+0], out[4*i+1], out[4*i+2], out[4*i+3]);
}
printf("\n");
aes_inv_cipher(out, in, w);
printf("Original message (after inv cipher):\n");
for (i = 0; i < 4; i++) {
printf("%02x %02x %02x %02x ", in[4*i+0], in[4*i+1], in[4*i+2], in[4*i+3]);
}
printf("\n\n");
uint8_t data[96];
uint8_t buf[96];
uint8_t key_exp[32] = { 0 };
char phrase[10] = { 0 };
strcpy(phrase, "any");
printf("phrase = \"%s\"\n", phrase);
md5_hash(phrase, key_exp, 32);
aes_key_expansion(key_exp, w);
printf("key:\n");
print_hex(key_exp, 32);
//print_hex(w, 240);
strcpy((char*)data, "dfuyg832g g872387g387gg7g;[a][pDKJFIJPIJ MXBNH =k45g34j234jbeiubwebfibiuhg213864593267g_____111");
size_t length = strlen((char*)data);
aes_encode(data, buf, length, w);
printf("DATA length = %zu\n%s\n", length, data);
print_hex(buf, length);
aes_decode(buf, data, length, w);
printf("%s\n\n", (char*)data);
free(w);
return 0;
}