Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/vczip/tests/tvccrypt.c
1810 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 2003-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Phong Vo <[email protected]> *
18
* Glenn Fowler <[email protected]> *
19
* *
20
***********************************************************************/
21
#include "vctest.h"
22
#include "vccrypto.h"
23
24
/* Data from Appendix B of FIPS-193.pdf */
25
static Vcchar_t Plaindt[] = /* plaintext input */
26
{ 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
27
0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34
28
};
29
static Vcchar_t Plain2dt[] = /* twice above plaintext */
30
{ 0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
31
0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34,
32
0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
33
0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34
34
};
35
static Vcchar_t Cryptdt[] = /* encrypted data */
36
{ 0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb,
37
0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32
38
};
39
static Vcchar_t Cipherkey[] = /* cipher key */
40
{ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
41
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
42
};
43
static Vcchar_t Expandedkey[] = /* expanded key */
44
{ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
45
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
46
47
0xa0, 0xfa, 0xfe, 0x17, 0x88, 0x54, 0x2c, 0xb1,
48
0x23, 0xa3, 0x39, 0x39, 0x2a, 0x6c, 0x76, 0x05,
49
50
0xf2, 0xc2, 0x95, 0xf2, 0x7a, 0x96, 0xb9, 0x43,
51
0x59, 0x35, 0x80, 0x7a, 0x73, 0x59, 0xf6, 0x7f,
52
53
0x3d, 0x80, 0x47, 0x7d, 0x47, 0x16, 0xfe, 0x3e,
54
0x1e, 0x23, 0x7e, 0x44, 0x6d, 0x7a, 0x88, 0x3b,
55
56
0xef, 0x44, 0xa5, 0x41, 0xa8, 0x52, 0x5b, 0x7f,
57
0xb6, 0x71, 0x25, 0x3b, 0xdb, 0x0b, 0xad, 0x00,
58
59
0xd4, 0xd1, 0xc6, 0xf8, 0x7c, 0x83, 0x9d, 0x87,
60
0xca, 0xf2, 0xb8, 0xbc, 0x11, 0xf9, 0x15, 0xbc,
61
62
0x6d, 0x88, 0xa3, 0x7a, 0x11, 0x0b, 0x3e, 0xfd,
63
0xdb, 0xf9, 0x86, 0x41, 0xca, 0x00, 0x93, 0xfd,
64
65
0x4e, 0x54, 0xf7, 0x0e, 0x5f, 0x5f, 0xc9, 0xf3,
66
0x84, 0xa6, 0x4f, 0xb2, 0x4e, 0xa6, 0xdc, 0x4f,
67
68
0xea, 0xd2, 0x73, 0x21, 0xb5, 0x8d, 0xba, 0xd2,
69
0x31, 0x2b, 0xf5, 0x60, 0x7f, 0x8d, 0x29, 0x2f,
70
71
0xac, 0x77, 0x66, 0xf3, 0x19, 0xfa, 0xdc, 0x21,
72
0x28, 0xd1, 0x29, 0x41, 0x57, 0x5c, 0x00, 0x6e,
73
74
0xd0, 0x14, 0xf9, 0xa8, 0xc9, 0xee, 0x25, 0x89,
75
0xe1, 0x3f, 0x0c, 0xc8, 0xb6, 0x63, 0x0c, 0xa6
76
};
77
78
int main()
79
{
80
Vcchar_t *plaindt, *edt, *ddt;
81
ssize_t plainsz, esz, dsz, k;
82
Vcchar_t data[1024];
83
Vcx_t xx;
84
Vcodex_t *evc, *dvc;
85
Vcdisc_t vcdc;
86
87
/* test the subpackage for encryption */
88
if(vcxinit(&xx, Vcxaes128, Cipherkey, -(ssize_t)sizeof(Cipherkey)) < 0)
89
terror("Initializing encryption handle");
90
if((esz = vcxencode(&xx, Plaindt, sizeof(Plaindt), &edt)) < 0)
91
terror("Encrypting test data");
92
93
/* save data in case buffer gets destroyed */
94
memcpy(data, edt, esz); edt = data;
95
96
if((dsz = vcxdecode(&xx, edt, esz, &ddt)) != sizeof(Plaindt))
97
terror("Wrong decrypted size");
98
if(memcmp(ddt, Plaindt, dsz) != 0)
99
terror("Wrong decrypted data");
100
vcxstop(&xx);
101
102
/* test transform */
103
vcdc.data = Cipherkey; vcdc.size = -(ssize_t)sizeof(Cipherkey);
104
vcdc.eventf = 0;
105
if(!(evc = vcopen(&vcdc, Vccrypt, "aes128", 0, VC_ENCODE)) )
106
terror("Can't open encryption handle");
107
if(!(dvc = vcopen(&vcdc, Vccrypt, "aes128", 0, VC_DECODE)) )
108
terror("Can't open decryption handle");
109
for(k = 0; k < 4; ++k)
110
{ if((esz = vcapply(evc, Plain2dt, sizeof(Plain2dt), &edt)) <= 0)
111
terror("Encryption error");
112
if((dsz = vcapply(dvc, edt, esz, &ddt)) <= 0)
113
terror("Decryption error");
114
if(dsz != sizeof(Plain2dt))
115
terror("Wrong decrypted size");
116
if(memcmp(ddt, Plain2dt, sizeof(Plain2dt)) != 0)
117
terror("Wrong decrypted data");
118
}
119
vcclose(evc);
120
vcclose(dvc);
121
122
/* test chained blocks */
123
vcdc.data = Cipherkey; vcdc.size = (ssize_t)sizeof(Cipherkey);
124
vcdc.eventf = 0;
125
if(!(evc = vcopen(&vcdc, Vccrypt, "aes128.chain", 0, VC_ENCODE)) )
126
terror("Can't open encryption handle");
127
if(!(dvc = vcopen(&vcdc, Vccrypt, "aes128.chain", 0, VC_DECODE)) )
128
terror("Can't open decryption handle");
129
for(k = 0; k < 4; ++k)
130
{ if((esz = vcapply(evc, Plain2dt, sizeof(Plain2dt), &edt)) <= 0)
131
terror("Encryption error");
132
if((dsz = vcapply(dvc, edt, esz, &ddt)) <= 0)
133
terror("Decryption error");
134
if(dsz != sizeof(Plain2dt))
135
terror("Wrong decrypted size");
136
if(memcmp(ddt, Plain2dt, sizeof(Plain2dt)) != 0)
137
terror("Wrong decrypted data");
138
}
139
vcclose(evc);
140
vcclose(dvc);
141
142
/* test 128-bit transform with use of password */
143
vcdc.data = "PhongBinhIan"; vcdc.size = 0;
144
vcdc.eventf = 0;
145
if(!(evc = vcopen(&vcdc, Vccrypt, "aes128", 0, VC_ENCODE)) )
146
terror("Can't open encryption handle");
147
if(!(dvc = vcopen(&vcdc, Vccrypt, "aes128", 0, VC_DECODE)) )
148
terror("Can't open decryption handle");
149
plaindt = "NguyenV, we love you!"; plainsz = strlen((char*)plaindt);
150
for(k = 0; k < 4; ++k)
151
{ if((esz = vcapply(evc, plaindt, plainsz, &edt)) <= 0)
152
terror("Encryption error");
153
if((dsz = vcapply(dvc, edt, esz, &ddt)) <= 0)
154
terror("Decryption error");
155
if(dsz != plainsz)
156
terror("Wrong decrypted size");
157
if(memcmp(ddt, plaindt, plainsz) != 0)
158
terror("Wrong decrypted data");
159
}
160
vcclose(evc);
161
vcclose(dvc);
162
163
/* test 192-bit transform with use of password */
164
vcdc.data = "PhongBinhIan"; vcdc.size = 0;
165
vcdc.eventf = 0;
166
if(!(evc = vcopen(&vcdc, Vccrypt, "aes192.chain", 0, VC_ENCODE)) )
167
terror("Can't open encryption handle");
168
if(!(dvc = vcopen(&vcdc, Vccrypt, "aes192.chain", 0, VC_DECODE)) )
169
terror("Can't open decryption handle");
170
plaindt = "NguyenV, we love you!"; plainsz = strlen((char*)plaindt);
171
for(k = 0; k < 4; ++k)
172
{ if((esz = vcapply(evc, plaindt, plainsz, &edt)) <= 0)
173
terror("Encryption error");
174
if((dsz = vcapply(dvc, edt, esz, &ddt)) <= 0)
175
terror("Decryption error");
176
if(dsz != plainsz)
177
terror("Wrong decrypted size");
178
if(memcmp(ddt, plaindt, plainsz) != 0)
179
terror("Wrong decrypted data");
180
}
181
vcclose(evc);
182
vcclose(dvc);
183
184
/* test 256-bit transform with use of password */
185
vcdc.data = "PhongBinhIan"; vcdc.size = 0;
186
vcdc.eventf = 0;
187
if(!(evc = vcopen(&vcdc, Vccrypt, "aes256.chain", 0, VC_ENCODE)) )
188
terror("Can't open encryption handle");
189
if(!(dvc = vcopen(&vcdc, Vccrypt, "aes256.chain", 0, VC_DECODE)) )
190
terror("Can't open decryption handle");
191
plaindt = "NguyenV, we love you!"; plainsz = strlen((char*)plaindt);
192
for(k = 0; k < 4; ++k)
193
{ if((esz = vcapply(evc, plaindt, plainsz, &edt)) <= 0)
194
terror("Encryption error");
195
if((dsz = vcapply(dvc, edt, esz, &ddt)) <= 0)
196
terror("Decryption error");
197
if(dsz != plainsz)
198
terror("Wrong decrypted size");
199
if(memcmp(ddt, plaindt, plainsz) != 0)
200
terror("Wrong decrypted data");
201
}
202
vcclose(evc);
203
vcclose(dvc);
204
205
exit(0);
206
}
207
208