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 | |
5 | use Test; |
6 | use strict; |
7 | use integer; |
8 | use File::Basename qw(dirname); |
9 | use File::Spec; |
10 | use Digest::SHA; |
11 | |
41c686de |
12 | BEGIN { |
77d2a621 |
13 | if ($ENV{PERL_CORE}) { |
14 | chdir 't' if -d 't'; |
15 | @INC = '../lib'; |
16 | } |
41c686de |
17 | } |
18 | |
6bc89f92 |
19 | # SHA-1 Test Vectors |
20 | # |
21 | # In the following we use the notation bitstring#n to mean a bitstring |
22 | # repeated n (in decimal) times, and we use | for concatenation. |
23 | # Therefore 110#3|1 is 1101101101. |
24 | # |
25 | # Here is a set near 2^32 bits to test the roll-over in the length |
26 | # field from one to two 32-bit words: |
27 | # |
28 | # 110#1431655764|11 1eef5a18 969255a3 b1793a2a 955c7ec2 8cd221a5 |
29 | # 110#1431655765| 7a1045b9 14672afa ce8d90e6 d19b3a6a da3cb879 |
30 | # 110#1431655765|1 d5e09777 a94f1ea9 240874c4 8d9fecb6 b634256b |
31 | # 110#1431655765|11 eb256904 3c3014e5 1b2862ae 6eb5fb4e 0b851d99 |
32 | # |
33 | # 011#1431655764|01 4CB0C4EF 69143D5B F34FC35F 1D4B19F6 ECCAE0F2 |
34 | # 011#1431655765 47D92F91 1FC7BB74 DE00ADFC 4E981A81 05556D52 |
35 | # 011#1431655765|0 A3D7438C 589B0B93 2AA91CC2 446F06DF 9ABC73F0 |
36 | # 011#1431655765|01 3EEE3E1E 28DEDE2C A444D68D A5675B2F AAAB3203 |
37 | |
38 | my(@vec110, @vec011); |
39 | |
77d2a621 |
40 | BEGIN { |
6bc89f92 |
41 | @vec110 = ( # 110 rep 1431655764 |
42 | "11", "1eef5a18969255a3b1793a2a955c7ec28cd221a5", |
43 | "110", "7a1045b914672aface8d90e6d19b3a6ada3cb879", |
44 | "1101", "d5e09777a94f1ea9240874c48d9fecb6b634256b", |
45 | "11011", "eb2569043c3014e51b2862ae6eb5fb4e0b851d99" |
46 | ); |
47 | |
48 | @vec011 = ( # 011 rep 1431655764 |
49 | "01", "4cb0c4ef69143d5bf34fc35f1d4b19f6eccae0f2", |
50 | "011", "47d92f911fc7bb74de00adfc4e981a8105556d52", |
51 | "0110", "a3d7438c589b0b932aa91cc2446f06df9abc73f0", |
52 | "01101", "3eee3e1e28dede2ca444d68da5675b2faaab3203" |
53 | ); |
77d2a621 |
54 | plan tests => scalar(@vec110) / 2 + scalar(@vec011) / 2; |
6bc89f92 |
55 | } |
56 | |
cccd5831 |
57 | my $STATE110 = File::Spec->catfile(dirname($0), "gillogly", "state.110"); |
58 | my $STATE011 = File::Spec->catfile(dirname($0), "gillogly", "state.011"); |
6bc89f92 |
59 | |
60 | my $reps = 1 << 14; |
61 | my $loops = int(1431655764 / $reps); |
62 | my $rest = 3 * (1431655764 - $loops * $reps); |
63 | |
64 | sub state110 { |
65 | my $state; |
66 | my $bitstr; |
67 | |
68 | $state = Digest::SHA->new(1); |
69 | if (-r $STATE110) { |
70 | if ($state->load($STATE110)) { |
71 | return($state); |
72 | } |
73 | } |
74 | $bitstr = pack("B*", "110" x $reps); |
75 | $state->reset; |
76 | for (my $i = 0; $i < $loops; $i++) { |
77 | $state->add_bits($bitstr, 3 * $reps); |
78 | } |
79 | $state->add_bits($bitstr, $rest); |
80 | $state->dump($STATE110); |
81 | return($state); |
82 | } |
83 | |
84 | sub state011 { |
85 | my $state; |
86 | my $bitstr; |
87 | |
88 | $state = Digest::SHA->new(1); |
89 | if (-r $STATE011) { |
90 | if ($state->load($STATE011)) { |
91 | return($state); |
92 | } |
93 | } |
94 | $bitstr = pack("B*", "011" x $reps); |
95 | $state->reset; |
96 | for (my $i = 0; $i < $loops; $i++) { |
97 | $state->add_bits($bitstr, 3 * $reps); |
98 | } |
99 | $state->add_bits($bitstr, $rest); |
100 | $state->dump($STATE011); |
101 | return($state); |
102 | } |
103 | |
104 | my $i; |
105 | |
106 | my $state110 = state110(); |
107 | for ($i = 0; $i < @vec110/2; $i++) { |
108 | my $state = $state110->clone; |
109 | $state->add_bits($vec110[2*$i]); |
110 | ok($state->hexdigest, $vec110[2*$i+1]); |
111 | } |
112 | |
113 | my $state011 = state011(); |
114 | for ($i = 0; $i < @vec011/2; $i++) { |
115 | my $state = $state011->clone; |
116 | $state->add_bits($vec011[2*$i]); |
117 | ok($state->hexdigest, $vec011[2*$i+1]); |
118 | } |