sha2.t
1 #! /usr/bin/env perl 2 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # See the NOTICE file distributed with this work for additional 6 # information regarding copyright ownership. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 use strict; 18 use warnings; 19 20 use FindBin; 21 use lib "$FindBin::Bin"; 22 use driver; 23 use cmd_helpers; 24 25 my $abc = unpack('H*', "abc"); 26 my $abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq 27 = unpack('H*', "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"); 28 my $abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu 29 = unpack('H*', "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"); 30 my $digits_30_times = "1234567890" x 30; 31 runtests( 32 { 33 title => 'For fun, a couple of SHA-256 hashquine tests', 34 cases => [ 35 hash => { 36 input => 'input', 37 output => 'output', 38 cmd => mk_cmd('lesec-tool', 'digest', 39 '-plugin' => 'ltc', 40 'sha-256') 41 } 42 ], 43 vectors => [ 44 { 45 tests => [ 46 { 47 # https://susam.net/sha-256-0573e7473.html 48 input => unpack('H*', "The SHA-256 hash of this sentence begins with 0573e7473."), 49 output => "0573e74731e90fed80059d65263d300d58c4a452012a69c56f0a58fcae0605ad" 50 }, 51 { 52 # Similar, look it up yourself ;-) 53 input => unpack('H*', "The SHA256 for this sentence begins with: one, eight, two, a, seven, c and nine."), 54 output => "182a7c930b0e5227ff8d24b5f4500ff2fa3ee1a57bd35e52d98c6e24c2749ae0" 55 } 56 ] 57 } 58 ] 59 }, 60 { 61 title => 'Sha-224 with test vectors from libtomcrypt', 62 cases => [ 63 hash => { 64 input => 'input', 65 output => 'output', 66 cmd => mk_cmd('lesec-tool', 'digest', 67 '-plugin' => 'ltc', 68 'sha-224') 69 } 70 ], 71 vectors => [ 72 { 73 tests => [ 74 { 75 input => $abc, 76 output => "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7", 77 }, 78 { 79 input => $abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq, 80 output => "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525", 81 } 82 ] 83 } 84 ] 85 }, 86 { 87 title => 'Sha-256 with test vectors from libtomcrypt', 88 cases => [ 89 hash => { 90 input => 'input', 91 output => 'output', 92 cmd => mk_cmd('lesec-tool', 'digest', 93 '-plugin' => 'ltc', 94 'sha-256') 95 } 96 ], 97 vectors => [ 98 { 99 tests => [ 100 { 101 input => $abc, 102 output => "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", 103 }, 104 { 105 input => $abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq, 106 output => "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", 107 } 108 ] 109 } 110 ] 111 }, 112 { 113 title => 'Sha-384 with test vectors from libtomcrypt', 114 cases => [ 115 hash => { 116 input => 'input', 117 output => 'output', 118 cmd => mk_cmd('lesec-tool', 'digest', 119 '-plugin' => 'ltc', 120 'sha-384') 121 } 122 ], 123 vectors => [ 124 { 125 tests => [ 126 { 127 input => $abc, 128 output => "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7", 129 }, 130 { 131 input => $abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu, 132 output => "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039", 133 } 134 ] 135 } 136 ] 137 }, 138 { 139 title => 'Sha-512 with test vectors from libtomcrypt', 140 cases => [ 141 hash => { 142 input => 'input', 143 output => 'output', 144 cmd => mk_cmd('lesec-tool', 'digest', 145 '-plugin' => 'ltc', 146 'sha-512') 147 } 148 ], 149 vectors => [ 150 { 151 tests => [ 152 { 153 input => $abc, 154 output => "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f", 155 }, 156 { 157 intut => $abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu, 158 output => "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909", 159 } 160 ] 161 } 162 ] 163 }, 164 { 165 title => 'Sha-512/224 with test vectors from libtomcrypt', 166 cases => [ 167 hash => { 168 input => 'input', 169 output => 'output', 170 cmd => mk_cmd('lesec-tool', 'digest', 171 '-plugin' => 'ltc', 172 'sha-512/224') 173 } 174 ], 175 vectors => [ 176 { 177 tests => [ 178 { 179 input => $abc, 180 output => "4634270F707B6A54DAAE7530460842E20E37ED265CEEE9A43E8924AA", 181 }, 182 { 183 input => $abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu, 184 output => "23FEC5BB94D60B23308192640B0C453335D664734FE40E7268674AF9", 185 }, 186 ] 187 } 188 ] 189 }, 190 { 191 cases => [ 192 hash => { 193 input => 'input', 194 output => 'output', 195 cmd => mk_cmd('lesec-tool', 'digest', 196 '-plugin' => 'ltc', 197 'sha-512/256') 198 } 199 ], 200 vectors => [ 201 { 202 tests => [ 203 { 204 input => $abc, 205 output => "53048E2681941EF99B2E29B76B4C7DABE4C2D0C634FC6D46E0E2F13107E7AF23", 206 }, 207 { 208 input => $abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu, 209 output => "3928E184FB8690F840DA3988121D31BE65CB9D3EF83EE6146FEAC861E19B563A", 210 }, 211 ] 212 } 213 ] 214 }, 215 );