2 * Stream cipher testing support.
3 * Copyright (C) 2014 Ketmar Dark // Invisible Vector (ketmar@ketmar.no-ip.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * Get a copy of the GNU GPL from <http://www.gnu.org/licenses/>.
16 module iv
.stc.testing
/*is aliced*/;
23 public uint count
= 0;
25 public void test(alias T
) (const(ubyte)[] key
, const(ubyte)[] iv
, const(ubyte)[] output
, bool big
=false) {
26 ubyte[] res
= new ubyte[!big ?
512 : 131072];
27 auto ctx
= T(key
, iv
);
28 ctx
.process(res
, res
);
30 assert(res
[0..64] == output
[0..64]);
31 assert(res
[192..256] == output
[64..128]);
32 assert(res
[256..320] == output
[128..192]);
33 assert(res
[448..512] == output
[192..256]);
35 assert(res
[0..64] == output
[0..64]);
36 assert(res
[65472..65536] == output
[64..128]);
37 assert(res
[65536..65600] == output
[128..192]);
38 assert(res
[131008..131072] == output
[192..256]);
44 public void processTVFile(alias T
) (string tvs
) {
49 ubyte[256] output
= void;
53 while (pos
< tvs
.length
&& (tvs
[pos
] == ' ' || tvs
[pos
] == '\t' || tvs
[pos
] == '\n' || tvs
[pos
] == '\r')) ++pos
;
57 usize
h2x (ubyte[] arr
, int len
=16) {
60 if (tvs
[0] < '0' ||
(tvs
[0] > '9' && tvs
[0] < 'A') || tvs
[0] > 'F') break;
62 if (n0
> 9) n0
-= 'A'-'9'-1;
64 if (n1
> 9) n1
-= 'A'-'9'-1;
65 arr
[pos
++] = cast(ubyte)(n0
*16+n1
);
72 assert(len
== 666 || len
== 0);
77 static immutable string
[3][2] ranges
= [
78 ["stream[192..255] = ",
79 "stream[256..319] = ",
80 "stream[448..511] = "],
81 ["stream[65472..65535] = ",
82 "stream[65536..65599] = ",
83 "stream[131008..131071] = "]
86 auto xpos
= tvs
.indexOf("\nSet ");
91 xpos
= tvs
.indexOf("key = ");
93 keylen
= h2x(key
, 666);
94 if (!tvs
.startsWith("IV = ")) {
96 keylen
+= h2x(key
[keylen
..$], 666);
99 assert(tvs
.skipOver("IV = "));
100 ivlen
= h2x(iv
, 666);
102 assert(tvs
.skipOver("stream[0..63] = "));
103 h2x(output
[0*16..$]);
104 h2x(output
[1*16..$]);
105 h2x(output
[2*16..$]);
106 h2x(output
[3*16..$]);
107 rng
= (tvs
.startsWith(ranges
[0][0]) ?
0 : 1);
108 for (usize f
= 1; f
< 4; ++f
) {
109 assert(tvs
.skipOver(ranges
[rng
][f
-1]));
110 h2x(output
[64*f
+0*16..$]);
111 h2x(output
[64*f
+1*16..$]);
112 h2x(output
[64*f
+2*16..$]);
113 h2x(output
[64*f
+3*16..$]);
115 test!T(key
[0..keylen
], iv
[0..ivlen
], output
, (rng
> 0));
116 xpos
= tvs
.indexOf("\nSet ");