26 |
7u83 |
1 |
#include <u.h>
|
|
|
2 |
#include <libc.h>
|
|
|
3 |
#include <libsec.h>
|
|
|
4 |
|
|
|
5 |
static void
|
|
|
6 |
printblock(uchar *b, usize n)
|
|
|
7 |
{
|
|
|
8 |
int i;
|
|
|
9 |
|
|
|
10 |
for(i=0; i+8<=n; i+=8){
|
|
|
11 |
print("%#.2ux %#.2ux %#.2ux %#.2ux %#.2ux %#.2ux %#.2ux %#.2ux\n",
|
|
|
12 |
b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
|
|
|
13 |
b += 8;
|
|
|
14 |
}
|
|
|
15 |
if(i < n){
|
|
|
16 |
print("%#.2ux", *b++);
|
|
|
17 |
while(++i < n)
|
|
|
18 |
print(" %#.2ux", *b++);
|
|
|
19 |
print("\n");
|
|
|
20 |
}
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
/* test vector from RFC7539 */
|
|
|
24 |
uchar rfckey[] = {
|
|
|
25 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
|
|
26 |
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
|
|
27 |
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
|
|
28 |
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f};
|
|
|
29 |
uchar rfcnonce[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00};
|
|
|
30 |
u32int rfccount = 1;
|
|
|
31 |
char rfctext[] = "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, "
|
|
|
32 |
"sunscreen would be it.";
|
|
|
33 |
uchar rfcout[3*ChachaBsize];
|
|
|
34 |
uchar rfcref[] = {
|
|
|
35 |
0x6e, 0x2e, 0x35, 0x9a, 0x25, 0x68, 0xf9, 0x80, 0x41, 0xba, 0x07, 0x28, 0xdd, 0x0d, 0x69, 0x81,
|
|
|
36 |
0xe9, 0x7e, 0x7a, 0xec, 0x1d, 0x43, 0x60, 0xc2, 0x0a, 0x27, 0xaf, 0xcc, 0xfd, 0x9f, 0xae, 0x0b,
|
|
|
37 |
0xf9, 0x1b, 0x65, 0xc5, 0x52, 0x47, 0x33, 0xab, 0x8f, 0x59, 0x3d, 0xab, 0xcd, 0x62, 0xb3, 0x57,
|
|
|
38 |
0x16, 0x39, 0xd6, 0x24, 0xe6, 0x51, 0x52, 0xab, 0x8f, 0x53, 0x0c, 0x35, 0x9f, 0x08, 0x61, 0xd8,
|
|
|
39 |
0x07, 0xca, 0x0d, 0xbf, 0x50, 0x0d, 0x6a, 0x61, 0x56, 0xa3, 0x8e, 0x08, 0x8a, 0x22, 0xb6, 0x5e,
|
|
|
40 |
0x52, 0xbc, 0x51, 0x4d, 0x16, 0xcc, 0xf8, 0x06, 0x81, 0x8c, 0xe9, 0x1a, 0xb7, 0x79, 0x37, 0x36,
|
|
|
41 |
0x5a, 0xf9, 0x0b, 0xbf, 0x74, 0xa3, 0x5b, 0xe6, 0xb4, 0x0b, 0x8e, 0xed, 0xf2, 0x78, 0x5e, 0x42,
|
|
|
42 |
0x87, 0x4d
|
|
|
43 |
};
|
|
|
44 |
|
|
|
45 |
uchar xcckey[] = {
|
|
|
46 |
0x1b, 0x27, 0x55, 0x64, 0x73, 0xe9, 0x85, 0xd4, 0x62, 0xcd, 0x51, 0x19, 0x7a, 0x9a, 0x46, 0xc7,
|
|
|
47 |
0x60, 0x09, 0x54, 0x9e, 0xac, 0x64, 0x74, 0xf2, 0x06, 0xc4, 0xee, 0x08, 0x44, 0xf6, 0x83, 0x89,
|
|
|
48 |
};
|
|
|
49 |
uchar xcciv[] = {
|
|
|
50 |
0x69, 0x69, 0x6e, 0xe9, 0x55, 0xb6, 0x2b, 0x73, 0xcd, 0x62, 0xbd, 0xa8, 0x75, 0xfc, 0x73, 0xd6,
|
|
|
51 |
0x82, 0x19, 0xe0, 0x03, 0x6b, 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0xc6, 0xc7,
|
|
|
67 |
};
|
|
|
68 |
uchar ccpkey[] = {
|
|
|
69 |
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
|
|
|
70 |
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
|
|
|
71 |
};
|
|
|
72 |
uchar ccpiv[] = {
|
|
|
73 |
0x07, 0x00, 0x00, 0x00,
|
|
|
74 |
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
|
|
|
75 |
};
|
|
|
76 |
uchar ccptag[] = {
|
|
|
77 |
0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a, 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91,
|
|
|
78 |
};
|
|
|
79 |
|
|
|
80 |
uchar ccp64aad[] = {
|
|
|
81 |
0x87, 0xe2, 0x29, 0xd4, 0x50, 0x08, 0x45, 0xa0, 0x79, 0xc0,
|
|
|
82 |
};
|
|
|
83 |
uchar ccp64key[] = {
|
|
|
84 |
0x42, 0x90, 0xbc, 0xb1, 0x54, 0x17, 0x35, 0x31, 0xf3, 0x14, 0xaf, 0x57, 0xf3, 0xbe, 0x3b, 0x50,
|
|
|
85 |
0x06, 0xda, 0x37, 0x1e, 0xce, 0x27, 0x2a, 0xfa, 0x1b, 0x5d, 0xbd, 0xd1, 0x10, 0x0a, 0x10, 0x07,
|
|
|
86 |
};
|
|
|
87 |
uchar ccp64iv[] = {
|
|
|
88 |
0xcd, 0x7c, 0xf6, 0x7b, 0xe3, 0x9c, 0x79, 0x4a,
|
|
|
89 |
};
|
|
|
90 |
uchar ccp64inp[] = {
|
|
|
91 |
0x86, 0xd0, 0x99, 0x74, 0x84, 0x0b, 0xde, 0xd2, 0xa5, 0xca,
|
|
|
92 |
};
|
|
|
93 |
uchar ccp64out[] = {
|
|
|
94 |
0xe3, 0xe4, 0x46, 0xf7, 0xed, 0xe9, 0xa1, 0x9b, 0x62, 0xa4,
|
|
|
95 |
};
|
|
|
96 |
uchar ccp64tag[] = {
|
|
|
97 |
0x67, 0x7d, 0xab, 0xf4, 0xe3, 0xd2, 0x4b, 0x87, 0x6b, 0xb2, 0x84, 0x75, 0x38, 0x96, 0xe1, 0xd6,
|
|
|
98 |
};
|
|
|
99 |
|
|
|
100 |
void
|
|
|
101 |
main(int argc, char **argv)
|
|
|
102 |
{
|
|
|
103 |
Chachastate s;
|
|
|
104 |
uchar tag[16];
|
|
|
105 |
int n;
|
|
|
106 |
|
|
|
107 |
ARGBEGIN{
|
|
|
108 |
}ARGEND
|
|
|
109 |
print("rfc7539:\n");
|
|
|
110 |
print("key:\n");
|
|
|
111 |
printblock(rfckey, sizeof(rfckey));
|
|
|
112 |
n = strlen(rfctext);
|
|
|
113 |
setupChachastate(&s, rfckey, sizeof(rfckey), rfcnonce, sizeof(rfcnonce), 0);
|
|
|
114 |
chacha_setblock(&s, rfccount);
|
|
|
115 |
print("rfc in:\n");
|
|
|
116 |
printblock((uchar*)rfctext, n);
|
|
|
117 |
chacha_encrypt2((uchar*)rfctext, rfcout, n, &s);
|
|
|
118 |
print("rfc out:\n");
|
|
|
119 |
printblock(rfcout, n);
|
|
|
120 |
if(memcmp(rfcout, rfcref, sizeof(rfcref)) != 0){
|
|
|
121 |
print("failure of vision\n");
|
|
|
122 |
exits("wrong");
|
|
|
123 |
}
|
|
|
124 |
print("\n");
|
|
|
125 |
|
|
|
126 |
|
|
|
127 |
print("xchacha key:\n");
|
|
|
128 |
printblock(xcckey, sizeof(xcckey));
|
|
|
129 |
|
|
|
130 |
print("xchacha iv:\n");
|
|
|
131 |
printblock(xcciv, sizeof(xcciv));
|
|
|
132 |
|
|
|
133 |
setupChachastate(&s, xcckey, sizeof(xcckey), xcciv, sizeof(xcciv), 20);
|
|
|
134 |
memset(rfcout, 0, sizeof(xccref));
|
|
|
135 |
chacha_encrypt(rfcout, sizeof(xccref), &s);
|
|
|
136 |
|
|
|
137 |
print("xchacha out:\n");
|
|
|
138 |
printblock(rfcout, sizeof(xccref));
|
|
|
139 |
if(memcmp(rfcout, xccref, sizeof(xccref)) != 0){
|
|
|
140 |
print("failure of vision\n");
|
|
|
141 |
exits("wrong");
|
|
|
142 |
}
|
|
|
143 |
print("\n");
|
|
|
144 |
|
|
|
145 |
|
|
|
146 |
print("ccpoly key:\n");
|
|
|
147 |
printblock(ccpkey, sizeof(ccpkey));
|
|
|
148 |
|
|
|
149 |
print("ccpoly iv:\n");
|
|
|
150 |
printblock(ccpiv, sizeof(ccpiv));
|
|
|
151 |
|
|
|
152 |
setupChachastate(&s, ccpkey, sizeof(ccpkey), ccpiv, sizeof(ccpiv), 20);
|
|
|
153 |
|
|
|
154 |
memmove(rfcout, rfctext, sizeof(rfctext)-1);
|
|
|
155 |
ccpoly_encrypt(rfcout, sizeof(rfctext)-1, ccpaad, sizeof(ccpaad), tag, &s);
|
|
|
156 |
|
|
|
157 |
print("ccpoly cipher:\n");
|
|
|
158 |
printblock(rfcout, sizeof(rfctext)-1);
|
|
|
159 |
|
|
|
160 |
print("ccpoly tag:\n");
|
|
|
161 |
printblock(tag, sizeof(tag));
|
|
|
162 |
|
|
|
163 |
if(memcmp(tag, ccptag, sizeof(tag)) != 0){
|
|
|
164 |
print("bad ccpoly tag\n");
|
|
|
165 |
exits("wrong");
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
if(ccpoly_decrypt(rfcout, sizeof(rfctext)-1, ccpaad, sizeof(ccpaad), tag, &s) != 0){
|
|
|
169 |
print("ccpoly decryption failed\n");
|
|
|
170 |
exits("wrong");
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
if(memcmp(rfcout, rfctext, sizeof(rfctext)-1) != 0){
|
|
|
174 |
print("ccpoly bad decryption\n");
|
|
|
175 |
exits("wrong");
|
|
|
176 |
}
|
|
|
177 |
print("\n");
|
|
|
178 |
|
|
|
179 |
|
|
|
180 |
print("ccpoly64 key:\n");
|
|
|
181 |
printblock(ccp64key, sizeof(ccp64key));
|
|
|
182 |
|
|
|
183 |
print("ccpoly64 iv:\n");
|
|
|
184 |
printblock(ccp64iv, sizeof(ccp64iv));
|
|
|
185 |
|
|
|
186 |
setupChachastate(&s, ccp64key, sizeof(ccp64key), ccp64iv, sizeof(ccp64iv), 20);
|
|
|
187 |
|
|
|
188 |
memmove(rfcout, ccp64inp, sizeof(ccp64inp));
|
|
|
189 |
ccpoly_encrypt(rfcout, sizeof(ccp64inp), ccp64aad, sizeof(ccp64aad), tag, &s);
|
|
|
190 |
|
|
|
191 |
print("ccpoly64 cipher:\n");
|
|
|
192 |
printblock(rfcout, sizeof(ccp64inp));
|
|
|
193 |
|
|
|
194 |
print("ccpoly64 tag:\n");
|
|
|
195 |
printblock(tag, sizeof(tag));
|
|
|
196 |
|
|
|
197 |
if(memcmp(rfcout, ccp64out, sizeof(ccp64out)) != 0){
|
|
|
198 |
print("ccpoly64 bad ciphertext\n");
|
|
|
199 |
exits("wrong");
|
|
|
200 |
}
|
|
|
201 |
if(memcmp(tag, ccp64tag, sizeof(ccp64tag)) != 0){
|
|
|
202 |
print("ccpoly64 bad encryption tag\n");
|
|
|
203 |
exits("wrong");
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
if(ccpoly_decrypt(rfcout, sizeof(ccp64inp), ccp64aad, sizeof(ccp64aad), tag, &s) != 0){
|
|
|
207 |
print("ccpoly64 decryption failed\n");
|
|
|
208 |
exits("wrong");
|
|
|
209 |
}
|
|
|
210 |
if(memcmp(rfcout, ccp64inp, sizeof(ccp64inp)) != 0){
|
|
|
211 |
print("ccpoly64 bad decryption\n");
|
|
|
212 |
exits("wrong");
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
print("passed\n");
|
|
|
216 |
exits(nil);
|
|
|
217 |
}
|