7
memzero_p (const uint8_t *p, size_t n)
10
for (i = 0; i < n; i++)
16
/* The ecrypt testcases encrypt 512 zero bytes (8 blocks), then give
17
the xor of all blocks, and the data for block 0 (0-43), 3,4
18
(192-319), 7 (448-511) */
20
#define STREAM_LENGTH 512
22
test_salsa20_stream(unsigned key_length,
25
const uint8_t *ciphertext,
26
const uint8_t *xor_ref)
28
struct salsa20_ctx ctx;
29
uint8_t data[STREAM_LENGTH + 1];
30
uint8_t stream[STREAM_LENGTH + 1];
31
uint8_t xor[SALSA20_BLOCK_SIZE];
34
salsa20_set_key(&ctx, key_length, key);
35
salsa20_set_iv(&ctx, iv);
36
memset(stream, 0, STREAM_LENGTH + 1);
37
salsa20_crypt(&ctx, STREAM_LENGTH, stream, stream);
38
if (stream[STREAM_LENGTH])
40
fprintf(stderr, "Stream of %d bytes wrote too much!\n", STREAM_LENGTH);
43
if (!MEMEQ (64, stream, ciphertext))
45
fprintf(stderr, "Error failed, offset 0:\n");
46
fprintf(stderr, "\nOutput: ");
47
print_hex(64, stream);
48
fprintf(stderr, "\nExpected:");
49
print_hex(64, ciphertext);
50
fprintf(stderr, "\n");
53
if (!MEMEQ (128, stream + 192, ciphertext + 64))
55
fprintf(stderr, "Error failed, offset 192:\n");
56
fprintf(stderr, "\nOutput: ");
57
print_hex(128, stream + 192);
58
fprintf(stderr, "\nExpected:");
59
print_hex(64, ciphertext + 64);
60
fprintf(stderr, "\n");
63
if (!MEMEQ (64, stream + 448, ciphertext + 192))
65
fprintf(stderr, "Error failed, offset 448:\n");
66
fprintf(stderr, "\nOutput: ");
67
print_hex(64, stream + 448);
68
fprintf(stderr, "\nExpected:");
69
print_hex(64, ciphertext + 192);
70
fprintf(stderr, "\n");
74
memxor3 (xor, stream, stream + SALSA20_BLOCK_SIZE, SALSA20_BLOCK_SIZE);
75
for (j = 2*SALSA20_BLOCK_SIZE; j < STREAM_LENGTH; j += SALSA20_BLOCK_SIZE)
76
memxor (xor, stream + j, SALSA20_BLOCK_SIZE);
78
if (!MEMEQ (SALSA20_BLOCK_SIZE, xor, xor_ref))
80
fprintf(stderr, "Error failed, bad xor 448:\n");
81
fprintf(stderr, "\nOutput: ");
82
print_hex(SALSA20_BLOCK_SIZE, xor);
83
fprintf(stderr, "\nExpected:");
84
print_hex(SALSA20_BLOCK_SIZE, xor_ref);
85
fprintf(stderr, "\n");
89
for (j = 1; j <= STREAM_LENGTH; j++)
91
memset(data, 0, STREAM_LENGTH + 1);
92
salsa20_set_iv(&ctx, iv);
93
salsa20_crypt(&ctx, j, data, data);
95
if (!MEMEQ(j, data, stream))
97
fprintf(stderr, "Encrypt failed for length %u:\n", j);
98
fprintf(stderr, "\nOutput: ");
100
fprintf(stderr, "\nExpected:");
101
print_hex(j, stream);
102
fprintf(stderr, "\n");
105
if (!memzero_p (data + j, STREAM_LENGTH + 1 - j))
107
fprintf(stderr, "Encrypt failed for length %u, wrote too much:\n", j);
108
fprintf(stderr, "\nOutput: ");
109
print_hex(STREAM_LENGTH + 1 - j, data + j);
110
fprintf(stderr, "\n");
117
test_salsa20(unsigned key_length,
121
const uint8_t *cleartext,
122
const uint8_t *ciphertext)
124
struct salsa20_ctx ctx;
125
uint8_t *data = xalloc(length + 1);
127
salsa20_set_key(&ctx, key_length, key);
128
salsa20_set_iv(&ctx, iv);
130
salsa20_crypt(&ctx, length, data, cleartext);
131
if (data[length] != 17)
133
fprintf(stderr, "Encrypt of %u bytes wrote too much!\nInput:", length);
134
print_hex(length, cleartext);
135
fprintf(stderr, "\n");
138
if (!MEMEQ(length, data, ciphertext))
140
fprintf(stderr, "Encrypt failed:\nInput:");
141
print_hex(length, cleartext);
142
fprintf(stderr, "\nOutput: ");
143
print_hex(length, data);
144
fprintf(stderr, "\nExpected:");
145
print_hex(length, ciphertext);
146
fprintf(stderr, "\n");
149
salsa20_set_key(&ctx, key_length, key);
150
salsa20_set_iv(&ctx, iv);
151
salsa20_crypt(&ctx, length, data, data);
153
if (!MEMEQ(length, data, cleartext))
155
fprintf(stderr, "Decrypt failed:\nInput:");
156
print_hex(length, ciphertext);
157
fprintf(stderr, "\nOutput: ");
158
print_hex(length, data);
159
fprintf(stderr, "\nExpected:");
160
print_hex(length, cleartext);
161
fprintf(stderr, "\n");
171
/* http://www.ecrypt.eu.org/stream/svn/viewcvs.cgi/ecrypt/trunk/submissions/salsa20/full/verified.test-vectors?logsort=rev&rev=210&view=markup */
173
test_salsa20(HL("80000000 00000000 00000000 00000000"),
174
H("00000000 00000000"),
175
HL("00000000 00000000"),
176
H("4DFA5E48 1DA23EA0"));
178
test_salsa20(HL("00000000 00000000 00000000 00000000"),
179
H("80000000 00000000"),
180
HL("00000000 00000000"),
181
H("B66C1E44 46DD9557"));
183
test_salsa20(HL("0053A6F94C9FF24598EB3E91E4378ADD"),
184
H("0D74DB42A91077DE"),
185
HL("00000000 00000000"),
186
H("05E1E7BE B697D999"));
188
test_salsa20(HL("80000000 00000000 00000000 00000000"
189
"00000000 00000000 00000000 00000000"),
190
H("00000000 00000000"),
191
HL("00000000 00000000"),
192
H("E3BE8FDD 8BECA2E3"));
194
test_salsa20(HL("00000000 00000000 00000000 00000000"
195
"00000000 00000000 00000000 00000000"),
196
H("80000000 00000000"),
197
HL("00000000 00000000"),
198
H("2ABA3DC45B494700"));
200
test_salsa20(HL("0053A6F94C9FF24598EB3E91E4378ADD"
201
"3083D6297CCF2275C81B6EC11467BA0D"),
202
H("0D74DB42A91077DE"),
203
HL("00000000 00000000"),
204
H("F5FAD53F 79F9DF58"));
206
test_salsa20_stream(HL("80000000000000000000000000000000"),
207
H("00000000 00000000"),
208
H("4DFA5E481DA23EA09A31022050859936"
209
"DA52FCEE218005164F267CB65F5CFD7F"
210
"2B4F97E0FF16924A52DF269515110A07"
211
"F9E460BC65EF95DA58F740B7D1DBB0AA"
212
"DA9C1581F429E0A00F7D67E23B730676"
213
"783B262E8EB43A25F55FB90B3E753AEF"
214
"8C6713EC66C51881111593CCB3E8CB8F"
215
"8DE124080501EEEB389C4BCB6977CF95"
216
"7D5789631EB4554400E1E025935DFA7B"
217
"3E9039D61BDC58A8697D36815BF1985C"
218
"EFDF7AE112E5BB81E37ECF0616CE7147"
219
"FC08A93A367E08631F23C03B00A8DA2F"
220
"B375703739DACED4DD4059FD71C3C47F"
221
"C2F9939670FAD4A46066ADCC6A564578"
222
"3308B90FFB72BE04A6B147CBE38CC0C3"
223
"B9267C296A92A7C69873F9F263BE9703"),
224
H("F7A274D268316790A67EC058F45C0F2A"
225
"067A99FCDE6236C0CEF8E056349FE54C"
226
"5F13AC74D2539570FD34FEAB06C57205"
227
"3949B59585742181A5A760223AFA22D4"));
229
test_salsa20_stream(HL("48494A4B4C4D4E4F5051525354555657"
230
"58595A5B5C5D5E5F6061626364656667"),
231
H("0000000000000000"),
232
H("53AD3698A011F779AD71030F3EFBEBA0"
233
"A7EE3C55789681B1591EF33A7BE521ED"
234
"68FC36E58F53FFD6E1369B00E390E973"
235
"F656ACB097E0D603BE59A0B8F7975B98"
236
"A04698274C6AC6EC03F66ED3F94C08B7"
237
"9FFDBF2A1610E6F5814905E73AD6D0D2"
238
"8164EEB8450D8ED0BB4B644761B43512"
239
"52DD5DDF00C31E3DABA0BC17691CCFDC"
240
"B826C7F071E796D34E3BFFB3C96E76A1"
241
"209388392806947C7F19B86D379FA3AE"
242
"DFCD19EBF49803DACC6E577E5B97B0F6"
243
"D2036B6624D8196C96FCF02C865D30C1"
244
"B505D41E2C207FA1C0A0E93413DDCFFC"
245
"9BECA8030AFFAC2466E56482DA0EF428"
246
"E63880B5021D3051F18679505A2B9D4F"
247
"9B2C5A2D271D276DE3F51DBEBA934436"),
248
H("7849651A820B1CDFE36D5D6632716534"
249
"E0635EDEFD538122D80870B60FB055DB"
250
"637C7CA2B78B116F83AFF46E40F8F71D"
251
"4CD6D2E1B750D5E011D1DF2E80F7210A"));