| 1 |
// Copyright 2014 The Closure Library Authors. All Rights Reserved. |
| 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 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.crypt.Sha2_64bit_test'); |
| 16 |
goog.setTestOnly('goog.crypt.Sha2_64bit_test'); |
| 17 |
|
| 18 |
goog.require('goog.array'); |
| 19 |
goog.require('goog.crypt'); |
| 20 |
goog.require('goog.crypt.Sha384'); |
| 21 |
goog.require('goog.crypt.Sha512'); |
| 22 |
goog.require('goog.crypt.Sha512_256'); |
| 23 |
goog.require('goog.crypt.hashTester'); |
| 24 |
goog.require('goog.testing.jsunit'); |
| 25 |
goog.require('goog.userAgent'); |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* Each object in the test vector array is a source text and one or more |
| 30 |
* hashes of that source text. The source text is either a string or a |
| 31 |
* byte array. |
| 32 |
* <p> |
| 33 |
* All hash values, except for the empty string, are from public sources: |
| 34 |
* csrc.nist.gov/publications/fips/fips180-2/fips180-2withchangenotice.pdf |
| 35 |
* csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA384.pdf |
| 36 |
* csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA512_256.pdf |
| 37 |
* csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA2_Additional.pdf |
| 38 |
* en.wikipedia.org/wiki/SHA-2#Examples_of_SHA-2_variants |
| 39 |
*/ |
| 40 |
var TEST_VECTOR = [ |
| 41 |
{ |
| 42 |
// Make sure the algorithm correctly handles the empty string |
| 43 |
source: '', |
| 44 |
512: 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce' + |
| 45 |
'47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e' |
| 46 |
}, |
| 47 |
{ |
| 48 |
source: 'abc', |
| 49 |
512: 'ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a' + |
| 50 |
'2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f', |
| 51 |
384: 'cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163' + |
| 52 |
'1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7', |
| 53 |
256: '53048e2681941ef99b2e29b76b4c7dabe4c2d0c634fc6d46e0e2f13107e7af23' |
| 54 |
}, |
| 55 |
{ |
| 56 |
source: 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn' + |
| 57 |
'hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu', |
| 58 |
512: '8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018' + |
| 59 |
'501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909', |
| 60 |
384: '09330c33f71147e83d192fc782cd1b4753111b173b3b05d2' + |
| 61 |
'2fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039', |
| 62 |
256: '3928e184fb8690f840da3988121d31be65cb9d3ef83ee6146feac861e19b563a' |
| 63 |
}, |
| 64 |
{ |
| 65 |
source: 'The quick brown fox jumps over the lazy dog', |
| 66 |
512: '07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb64' + |
| 67 |
'2e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6', |
| 68 |
384: 'ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c49' + |
| 69 |
'4011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1', |
| 70 |
256: 'dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d' |
| 71 |
} |
| 72 |
]; |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* For each integer key N, the value is the SHA-512 value of a string |
| 77 |
* consisting of N repetitions of the character 'a'. |
| 78 |
*/ |
| 79 |
var TEST_FENCEPOST_VECTOR = { |
| 80 |
110: 'c825949632e509824543f7eaf159fb6041722fce3c1cdcbb613b3d37ff107c51' + |
| 81 |
'9417baac32f8e74fe29d7f4823bf6886956603dca5354a6ed6e4a542e06b7d28', |
| 82 |
111: 'fa9121c7b32b9e01733d034cfc78cbf67f926c7ed83e82200ef8681819692176' + |
| 83 |
'0b4beff48404df811b953828274461673c68d04e297b0eb7b2b4d60fc6b566a2', |
| 84 |
112: 'c01d080efd492776a1c43bd23dd99d0a2e626d481e16782e75d54c2503b5dc32' + |
| 85 |
'bd05f0f1ba33e568b88fd2d970929b719ecbb152f58f130a407c8830604b70ca', |
| 86 |
113: '55ddd8ac210a6e18ba1ee055af84c966e0dbff091c43580ae1be703bdb85da31' + |
| 87 |
'acf6948cf5bd90c55a20e5450f22fb89bd8d0085e39f85a86cc46abbca75e24d' |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* Simple sanity tests for hash functions. |
| 93 |
*/ |
| 94 |
function testBasicOperations() { |
| 95 |
var sha512 = new goog.crypt.Sha512(); |
| 96 |
goog.crypt.hashTester.runBasicTests(sha512); |
| 97 |
|
| 98 |
var sha384 = new goog.crypt.Sha384(); |
| 99 |
goog.crypt.hashTester.runBasicTests(sha384); |
| 100 |
|
| 101 |
var sha256 = new goog.crypt.Sha512_256(); |
| 102 |
goog.crypt.hashTester.runBasicTests(sha256); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* Function called by the actual testers to ensure that specific strings |
| 108 |
* hash to specific published values. |
| 109 |
* |
| 110 |
* Each item in the vector has a "source" and one or more additional keys. |
| 111 |
* If the item has a key matching the key argument passed to this |
| 112 |
* function, it is the expected value of the hash function. |
| 113 |
* |
| 114 |
* @param {!goog.crypt.Sha2_64bit} hasher The hasher to test |
| 115 |
* @param {number} length The length of the resulting hash, in bits. |
| 116 |
* Also the key to use in TEST_VECTOR for the expected hash value |
| 117 |
*/ |
| 118 |
function hashGoldenTester(hasher, length) { |
| 119 |
goog.array.forEach(TEST_VECTOR, function(data) { |
| 120 |
hasher.update(data.source); |
| 121 |
var digest = hasher.digest(); |
| 122 |
assertEquals('Hash digest has the wrong length', length, digest.length * 8); |
| 123 |
if (data[length]) { |
| 124 |
// We're given an expected value |
| 125 |
var expected = goog.crypt.hexToByteArray(data[length]); |
| 126 |
assertElementsEquals( |
| 127 |
'Wrong result for hash' + length + '(\'' + data.source + '\')', |
| 128 |
expected, digest); |
| 129 |
} |
| 130 |
hasher.reset(); |
| 131 |
}); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
/** Test that Sha512() returns the published values */ |
| 136 |
function testHashing512() { |
| 137 |
hashGoldenTester(new goog.crypt.Sha512(), 512); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
/** Test that Sha384 returns the published values */ |
| 142 |
function testHashing384() { |
| 143 |
hashGoldenTester(new goog.crypt.Sha384(), 384); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
/** Test that Sha512_256 returns the published values */ |
| 148 |
function testHashing256() { |
| 149 |
hashGoldenTester(new goog.crypt.Sha512_256(), 256); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
/** Test that the opt_length works */ |
| 154 |
function testHashing_optLength() { |
| 155 |
var hasher = new goog.crypt.Sha512(); |
| 156 |
hasher.update('1234567890'); |
| 157 |
var digest1 = hasher.digest(); |
| 158 |
hasher.reset(); |
| 159 |
hasher.update('12345678901234567890', 10); |
| 160 |
var digest2 = hasher.digest(); |
| 161 |
assertElementsEquals(digest1, digest2); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
/** |
| 166 |
* Make sure that we correctly handle strings whose length is 110-113. |
| 167 |
* This is the area where we are likely to hit fencepost errors in the padding |
| 168 |
* code. |
| 169 |
*/ |
| 170 |
function testFencepostErrors() { |
| 171 |
var hasher = new goog.crypt.Sha512(); |
| 172 |
A64 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; |
| 173 |
A128 = A64 + A64; |
| 174 |
for (var i = 110; i <= 113; i++) { |
| 175 |
hasher.update(A128, i); |
| 176 |
var digest = hasher.digest(); |
| 177 |
var expected = goog.crypt.hexToByteArray(TEST_FENCEPOST_VECTOR[i]); |
| 178 |
assertElementsEquals('Fencepost ' + i, expected, digest); |
| 179 |
hasher.reset(); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
/** Test one really large string using SHA512 */ |
| 185 |
function testHashing512Large() { |
| 186 |
// This test tends to time out on IE7 and IE8. See b/22873770. |
| 187 |
if (!goog.userAgent.IE || goog.userAgent.isVersionOrHigher('9')) { |
| 188 |
var hasher = new goog.crypt.Sha512(); |
| 189 |
hasher.update(goog.array.repeat(0, 1000000)); |
| 190 |
var digest = hasher.digest(); |
| 191 |
var expected = goog.crypt.hexToByteArray( |
| 192 |
'ce044bc9fd43269d5bbc946cbebc3bb711341115cc4abdf2edbc3ff2c57ad4b1' + |
| 193 |
'5deb699bda257fea5aef9c6e55fcf4cf9dc25a8c3ce25f2efe90908379bff7ed'); |
| 194 |
assertElementsEquals(expected, digest); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
/** Check that the code throws an error for bad input */ |
| 200 |
function testBadInput_nullNotAllowed() { |
| 201 |
var hasher = new goog.crypt.Sha512(); |
| 202 |
assertThrows('Null input not allowed', function() { hasher.update({}); }); |
| 203 |
} |
| 204 |
|
| 205 |
function testBadInput_noFloatingPoint() { |
| 206 |
var hasher = new goog.crypt.Sha512(); |
| 207 |
assertThrows('Floating point not allows', function() { |
| 208 |
hasher.update([1, 2, 3, 4, 4.5]); |
| 209 |
}); |
| 210 |
} |
| 211 |
|
| 212 |
function testBadInput_negativeNotAllowed() { |
| 213 |
var hasher = new goog.crypt.Sha512(); |
| 214 |
assertThrows('Negative not allowed', function() { |
| 215 |
hasher.update([1, 2, 3, 4, -10]); |
| 216 |
}); |
| 217 |
} |
| 218 |
|
| 219 |
function testBadInput_mustBeByteArray() { |
| 220 |
var hasher = new goog.crypt.Sha512(); |
| 221 |
assertThrows('Must be byte array', function() { |
| 222 |
hasher.update([1, 2, 3, 4, {}]); |
| 223 |
}); |
| 224 |
} |
| 225 |
|
| 226 |
function testBadInput_byteTooLarge() { |
| 227 |
var hasher = new goog.crypt.Sha512(); |
| 228 |
assertThrows('>255 not allowed', function() { |
| 229 |
hasher.update([1, 2, 3, 4, 256]); |
| 230 |
}); |
| 231 |
} |
| 232 |
|
| 233 |
function testBadInput_characterTooLarge() { |
| 234 |
var hasher = new goog.crypt.Sha512(); |
| 235 |
assertThrows('>255 not allowed', function() { |
| 236 |
hasher.update('abc' + String.fromCharCode(256)); |
| 237 |
}); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
function testHasherNeedsReset_beforeDigest() { |
| 242 |
var hasher = new goog.crypt.Sha512(); |
| 243 |
hasher.update('abc'); |
| 244 |
hasher.digest(); |
| 245 |
assertThrows('Need reset after digest', function() { hasher.digest(); }); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
function testHasherNeedsReset_beforeUpdate() { |
| 250 |
var hasher = new goog.crypt.Sha512(); |
| 251 |
hasher.update('abc'); |
| 252 |
hasher.digest(); |
| 253 |
assertThrows('Need reset after digest', function() { hasher.update('abc'); }); |
| 254 |
} |
| 255 |
|
| 256 |
function testHashingArrayLike() { |
| 257 |
// Create array-like object |
| 258 |
var obj = {}; |
| 259 |
obj.length = 26; |
| 260 |
for (var i = 0; i < 26; i++) { |
| 261 |
obj[i] = 97 + i; |
| 262 |
} |
| 263 |
|
| 264 |
// Check hashing |
| 265 |
var hasher = new goog.crypt.Sha512(); |
| 266 |
hasher.update(obj); |
| 267 |
var digest1 = hasher.digest(); |
| 268 |
|
| 269 |
hasher.reset(); |
| 270 |
|
| 271 |
hasher.update('abcdefghijklmnopqrstuvwxyz', 26); |
| 272 |
var digest2 = hasher.digest(); |
| 273 |
|
| 274 |
assertElementsEquals(digest1, digest2); |
| 275 |
} |