Commit | Line | Data |
6bc89f92 |
1 | # Test against long bitwise vectors from Jim Gillogly and Francois Grieu |
2 | # |
3 | # http://www.chiark.greenend.org.uk/pipermail/ukcrypto/1999-February/003538.html |
4 | |
6bc89f92 |
5 | use strict; |
747da336 |
6 | use FileHandle; |
7 | |
8 | my $MODULE; |
9 | |
10 | BEGIN { |
72aa8e26 |
11 | $MODULE = ($ENV{PERL_CORE} || -e "SHA.pm") ? "Digest::SHA" : "Digest::SHA::PurePerl"; |
747da336 |
12 | eval "require $MODULE" || die $@; |
13 | $MODULE->import(qw()); |
14 | } |
6bc89f92 |
15 | |
41c686de |
16 | BEGIN { |
77d2a621 |
17 | if ($ENV{PERL_CORE}) { |
18 | chdir 't' if -d 't'; |
19 | @INC = '../lib'; |
20 | } |
41c686de |
21 | } |
22 | |
6bc89f92 |
23 | # SHA-1 Test Vectors |
24 | # |
25 | # In the following we use the notation bitstring#n to mean a bitstring |
26 | # repeated n (in decimal) times, and we use | for concatenation. |
27 | # Therefore 110#3|1 is 1101101101. |
28 | # |
29 | # Here is a set near 2^32 bits to test the roll-over in the length |
30 | # field from one to two 32-bit words: |
31 | # |
32 | # 110#1431655764|11 1eef5a18 969255a3 b1793a2a 955c7ec2 8cd221a5 |
33 | # 110#1431655765| 7a1045b9 14672afa ce8d90e6 d19b3a6a da3cb879 |
34 | # 110#1431655765|1 d5e09777 a94f1ea9 240874c4 8d9fecb6 b634256b |
35 | # 110#1431655765|11 eb256904 3c3014e5 1b2862ae 6eb5fb4e 0b851d99 |
36 | # |
37 | # 011#1431655764|01 4CB0C4EF 69143D5B F34FC35F 1D4B19F6 ECCAE0F2 |
38 | # 011#1431655765 47D92F91 1FC7BB74 DE00ADFC 4E981A81 05556D52 |
39 | # 011#1431655765|0 A3D7438C 589B0B93 2AA91CC2 446F06DF 9ABC73F0 |
40 | # 011#1431655765|01 3EEE3E1E 28DEDE2C A444D68D A5675B2F AAAB3203 |
41 | |
747da336 |
42 | my @vec110 = ( # 110 rep 1431655764 |
43 | "11", "1eef5a18969255a3b1793a2a955c7ec28cd221a5", |
44 | "110", "7a1045b914672aface8d90e6d19b3a6ada3cb879", |
45 | "1101", "d5e09777a94f1ea9240874c48d9fecb6b634256b", |
46 | "11011", "eb2569043c3014e51b2862ae6eb5fb4e0b851d99" |
47 | ); |
6bc89f92 |
48 | |
747da336 |
49 | my @vec011 = ( # 011 rep 1431655764 |
50 | "01", "4cb0c4ef69143d5bf34fc35f1d4b19f6eccae0f2", |
51 | "011", "47d92f911fc7bb74de00adfc4e981a8105556d52", |
52 | "0110", "a3d7438c589b0b932aa91cc2446f06df9abc73f0", |
53 | "01101", "3eee3e1e28dede2ca444d68da5675b2faaab3203" |
54 | ); |
55 | print "1..", scalar(@vec110) / 2 + scalar(@vec011) / 2, "\n"; |
6bc89f92 |
56 | |
747da336 |
57 | my $STATE110 = "gglong0.tmp"; |
58 | my $STATE011 = "gglong1.tmp"; |
59 | |
60 | END { 1 while unlink $STATE110, $STATE011 } |
61 | |
62 | for ($STATE011, $STATE110) { |
63 | my $fh = FileHandle->new($_, "w"); |
64 | for (1 .. 8) { my $line = <DATA>; print $fh $line } |
65 | $fh->close; |
66 | } |
6bc89f92 |
67 | |
68 | my $reps = 1 << 14; |
69 | my $loops = int(1431655764 / $reps); |
70 | my $rest = 3 * (1431655764 - $loops * $reps); |
71 | |
72 | sub state110 { |
747da336 |
73 | my $i; |
6bc89f92 |
74 | my $state; |
75 | my $bitstr; |
76 | |
747da336 |
77 | $state = $MODULE->new(1); |
6bc89f92 |
78 | if (-r $STATE110) { |
79 | if ($state->load($STATE110)) { |
80 | return($state); |
81 | } |
82 | } |
83 | $bitstr = pack("B*", "110" x $reps); |
84 | $state->reset; |
747da336 |
85 | for ($i = 0; $i < $loops; $i++) { |
6bc89f92 |
86 | $state->add_bits($bitstr, 3 * $reps); |
87 | } |
88 | $state->add_bits($bitstr, $rest); |
89 | $state->dump($STATE110); |
90 | return($state); |
91 | } |
92 | |
93 | sub state011 { |
747da336 |
94 | my $i; |
6bc89f92 |
95 | my $state; |
96 | my $bitstr; |
97 | |
747da336 |
98 | $state = $MODULE->new(1); |
6bc89f92 |
99 | if (-r $STATE011) { |
100 | if ($state->load($STATE011)) { |
101 | return($state); |
102 | } |
103 | } |
104 | $bitstr = pack("B*", "011" x $reps); |
105 | $state->reset; |
747da336 |
106 | for ($i = 0; $i < $loops; $i++) { |
6bc89f92 |
107 | $state->add_bits($bitstr, 3 * $reps); |
108 | } |
109 | $state->add_bits($bitstr, $rest); |
110 | $state->dump($STATE011); |
111 | return($state); |
112 | } |
113 | |
114 | my $i; |
115 | |
747da336 |
116 | my $testnum = 1; |
117 | |
6bc89f92 |
118 | my $state110 = state110(); |
119 | for ($i = 0; $i < @vec110/2; $i++) { |
120 | my $state = $state110->clone; |
121 | $state->add_bits($vec110[2*$i]); |
747da336 |
122 | print "not " unless $state->hexdigest eq $vec110[2*$i+1]; |
123 | print "ok ", $testnum++, "\n"; |
6bc89f92 |
124 | } |
125 | |
126 | my $state011 = state011(); |
127 | for ($i = 0; $i < @vec011/2; $i++) { |
128 | my $state = $state011->clone; |
129 | $state->add_bits($vec011[2*$i]); |
747da336 |
130 | print "not " unless $state->hexdigest eq $vec011[2*$i+1]; |
131 | print "ok ", $testnum++, "\n"; |
6bc89f92 |
132 | } |
747da336 |
133 | |
134 | __DATA__ |
135 | alg:1 |
136 | H:7950cbe2:86a45aa0:91ff7dff:29015b42:3912e764:00000000:00000000:00000000 |
137 | block:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6 |
138 | blockcnt:508 |
139 | lenhh:0 |
140 | lenhl:0 |
141 | lenlh:0 |
142 | lenll:4294967292 |
143 | alg:1 |
144 | H:dfc51a14:87b4a4b7:ecf19acd:8cbbe40e:03a435f8:00000000:00000000:00000000 |
145 | block:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d:b6:db:6d |
146 | blockcnt:508 |
147 | lenhh:0 |
148 | lenhl:0 |
149 | lenlh:0 |
150 | lenll:4294967292 |