| 1 |
// Copyright 2016 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 |
/** |
| 16 |
* @fileoverview Unit tests for CTR mode for block ciphers. |
| 17 |
*/ |
| 18 |
|
| 19 |
/** @suppress {extraProvide} */ |
| 20 |
goog.provide('goog.crypt.CtrTest'); |
| 21 |
|
| 22 |
goog.require('goog.crypt'); |
| 23 |
goog.require('goog.crypt.Aes'); |
| 24 |
goog.require('goog.crypt.Ctr'); |
| 25 |
goog.require('goog.testing.jsunit'); |
| 26 |
|
| 27 |
goog.setTestOnly('goog.crypt.CtrTest'); |
| 28 |
|
| 29 |
/** |
| 30 |
* Asserts the given parameters allow encryption and decryption from and to the |
| 31 |
* given plaintext/ciphertext. |
| 32 |
* |
| 33 |
* @param {!Array<number>|!Uint8Array} keyBytes |
| 34 |
* @param {!Array<number>|!Uint8Array} initialVectorBytes |
| 35 |
* @param {!Array<number>|!Uint8Array} plainTextBytes |
| 36 |
* @param {!Array<number>|!Uint8Array} cipherTextBytes |
| 37 |
*/ |
| 38 |
function runCtrAesTest( |
| 39 |
keyBytes, initialVectorBytes, plainTextBytes, cipherTextBytes) { |
| 40 |
var aes = new goog.crypt.Aes(keyBytes); |
| 41 |
var Ctr = new goog.crypt.Ctr(aes); |
| 42 |
|
| 43 |
var encryptedBytes = Ctr.encrypt(plainTextBytes, initialVectorBytes); |
| 44 |
assertEquals( |
| 45 |
'Encrypted bytes should match cipher text.', |
| 46 |
goog.crypt.byteArrayToHex(encryptedBytes), |
| 47 |
goog.crypt.byteArrayToHex(cipherTextBytes)); |
| 48 |
|
| 49 |
var decryptedBytes = Ctr.decrypt(cipherTextBytes, initialVectorBytes); |
| 50 |
assertEquals( |
| 51 |
'Decrypted bytes should match plain text.', |
| 52 |
goog.crypt.byteArrayToHex(decryptedBytes), |
| 53 |
goog.crypt.byteArrayToHex(plainTextBytes)); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Asserts goog.crypt.Ctr.incrementBigEndianCounter turns the first parameter |
| 58 |
* into the second. |
| 59 |
* |
| 60 |
* @param {string} toIncrement |
| 61 |
* @param {string} expectedResult |
| 62 |
*/ |
| 63 |
function assertIncEquals(toIncrement, expectedResult) { |
| 64 |
var counter = goog.crypt.hexToByteArray(toIncrement); |
| 65 |
goog.crypt.Ctr.incrementBigEndianCounter_(counter); |
| 66 |
assertEquals(expectedResult, goog.crypt.byteArrayToHex(counter)); |
| 67 |
} |
| 68 |
|
| 69 |
function testIncrementCounter() { |
| 70 |
assertIncEquals('', ''); |
| 71 |
|
| 72 |
assertIncEquals('00', '01'); |
| 73 |
assertIncEquals('09', '0a'); |
| 74 |
assertIncEquals('0e', '0f'); |
| 75 |
assertIncEquals('0f', '10'); |
| 76 |
assertIncEquals('1f', '20'); |
| 77 |
assertIncEquals('ff', '00'); // no length extension |
| 78 |
|
| 79 |
assertIncEquals('0000', '0001'); |
| 80 |
assertIncEquals('00f0', '00f1'); |
| 81 |
assertIncEquals('00ff', '0100'); |
| 82 |
assertIncEquals('0f00', '0f01'); |
| 83 |
assertIncEquals('0fff', '1000'); |
| 84 |
assertIncEquals('1000', '1001'); |
| 85 |
assertIncEquals('ff00', 'ff01'); |
| 86 |
assertIncEquals('ff0f', 'ff10'); |
| 87 |
assertIncEquals('ffff', '0000'); |
| 88 |
|
| 89 |
assertIncEquals( |
| 90 |
'ffffffffffffffffffffffffffffffffffffffffffffffff', |
| 91 |
'000000000000000000000000000000000000000000000000'); |
| 92 |
} |
| 93 |
|
| 94 |
function testAes128CtrCipherAlgorithm() { |
| 95 |
// Test vectors from NIST sp800-38a, p 55 |
| 96 |
// http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
| 97 |
|
| 98 |
// Case #1, no chaining |
| 99 |
runCtrAesTest( |
| 100 |
goog.crypt.hexToByteArray('2b7e151628aed2a6abf7158809cf4f3c'), |
| 101 |
goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'), |
| 102 |
goog.crypt.hexToByteArray('6bc1bee22e409f96e93d7e117393172a'), |
| 103 |
goog.crypt.hexToByteArray('874d6191b620e3261bef6864990db6ce')); |
| 104 |
|
| 105 |
// Case #2, three blocks |
| 106 |
runCtrAesTest( |
| 107 |
goog.crypt.hexToByteArray('2b7e151628aed2a6abf7158809cf4f3c'), |
| 108 |
goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'), |
| 109 |
goog.crypt.hexToByteArray( |
| 110 |
'6bc1bee22e409f96e93d7e117393172a' + |
| 111 |
'ae2d8a571e03ac9c9eb76fac45af8e51' + |
| 112 |
'30c81c46a35ce411e5fbc1191a0a52ef'), |
| 113 |
goog.crypt.hexToByteArray( |
| 114 |
'874d6191b620e3261bef6864990db6ce' + |
| 115 |
'9806f66b7970fdff8617187bb9fffdff' + |
| 116 |
'5ae4df3edbd5d35e5b4f09020db03eab')); |
| 117 |
|
| 118 |
// Case #3, plaintext length not a multiple of blocksize |
| 119 |
runCtrAesTest( |
| 120 |
goog.crypt.hexToByteArray('2b7e151628aed2a6abf7158809cf4f3c'), |
| 121 |
goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'), |
| 122 |
goog.crypt.hexToByteArray( |
| 123 |
'6bc1bee22e409f96e93d7e117393172a' + |
| 124 |
'ae2d8a571e03ac9c9eb76fac45af8e51' + |
| 125 |
'30c81c'), |
| 126 |
goog.crypt.hexToByteArray( |
| 127 |
'874d6191b620e3261bef6864990db6ce' + |
| 128 |
'9806f66b7970fdff8617187bb9fffdff' + |
| 129 |
'5ae4df')); |
| 130 |
} |
| 131 |
|
| 132 |
function testAes192CtrCipherAlgorithm() { |
| 133 |
// Test vectors from NIST sp800-38a, p 56 |
| 134 |
// http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
| 135 |
// Key block is weird, that's normal: 192 is one block and a half. |
| 136 |
|
| 137 |
|
| 138 |
// Case #1, no chaining |
| 139 |
runCtrAesTest( |
| 140 |
goog.crypt.hexToByteArray( |
| 141 |
'8e73b0f7da0e6452c810f32b809079e5' + |
| 142 |
'62f8ead2522c6b7b'), |
| 143 |
goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'), |
| 144 |
goog.crypt.hexToByteArray('6bc1bee22e409f96e93d7e117393172a'), |
| 145 |
goog.crypt.hexToByteArray('1abc932417521ca24f2b0459fe7e6e0b')); |
| 146 |
|
| 147 |
// Case #2, three blocks |
| 148 |
runCtrAesTest( |
| 149 |
goog.crypt.hexToByteArray( |
| 150 |
'8e73b0f7da0e6452c810f32b809079e5' + |
| 151 |
'62f8ead2522c6b7b'), |
| 152 |
goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'), |
| 153 |
goog.crypt.hexToByteArray( |
| 154 |
'6bc1bee22e409f96e93d7e117393172a' + |
| 155 |
'ae2d8a571e03ac9c9eb76fac45af8e51' + |
| 156 |
'30c81c46a35ce411e5fbc1191a0a52ef'), |
| 157 |
goog.crypt.hexToByteArray( |
| 158 |
'1abc932417521ca24f2b0459fe7e6e0b' + |
| 159 |
'090339ec0aa6faefd5ccc2c6f4ce8e94' + |
| 160 |
'1e36b26bd1ebc670d1bd1d665620abf7')); |
| 161 |
} |
| 162 |
|
| 163 |
function testAes256CtrCipherAlgorithm() { |
| 164 |
// Test vectors from NIST sp800-38a, p 57 |
| 165 |
// http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf |
| 166 |
|
| 167 |
// Case #1, no chaining |
| 168 |
runCtrAesTest( |
| 169 |
goog.crypt.hexToByteArray( |
| 170 |
'603deb1015ca71be2b73aef0857d7781' + |
| 171 |
'1f352c073b6108d72d9810a30914dff4'), |
| 172 |
goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'), |
| 173 |
goog.crypt.hexToByteArray('6bc1bee22e409f96e93d7e117393172a'), |
| 174 |
goog.crypt.hexToByteArray('601ec313775789a5b7a7f504bbf3d228')); |
| 175 |
|
| 176 |
|
| 177 |
// Case #2, three blocks |
| 178 |
runCtrAesTest( |
| 179 |
goog.crypt.hexToByteArray( |
| 180 |
'603deb1015ca71be2b73aef0857d7781' + |
| 181 |
'1f352c073b6108d72d9810a30914dff4'), |
| 182 |
goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'), |
| 183 |
goog.crypt.hexToByteArray( |
| 184 |
'6bc1bee22e409f96e93d7e117393172a' + |
| 185 |
'ae2d8a571e03ac9c9eb76fac45af8e51' + |
| 186 |
'30c81c46a35ce411e5fbc1191a0a52ef'), |
| 187 |
goog.crypt.hexToByteArray( |
| 188 |
'601ec313775789a5b7a7f504bbf3d228' + |
| 189 |
'f443e3ca4d62b59aca84e990cacaf5c5' + |
| 190 |
'2b0930daa23de94ce87017ba2d84988d')); |
| 191 |
|
| 192 |
// Case #3, plaintext length not a multiple of blocksize |
| 193 |
runCtrAesTest( |
| 194 |
goog.crypt.hexToByteArray( |
| 195 |
'603deb1015ca71be2b73aef0857d7781' + |
| 196 |
'1f352c073b6108d72d9810a30914dff4'), |
| 197 |
goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'), |
| 198 |
goog.crypt.hexToByteArray( |
| 199 |
'6bc1bee22e409f96e93d7e117393172a' + |
| 200 |
'ae2d8a571e03ac9c9eb76fac45af8e51' + |
| 201 |
'30c81c46a35ce411e5fbc1191a0a52'), |
| 202 |
goog.crypt.hexToByteArray( |
| 203 |
'601ec313775789a5b7a7f504bbf3d228' + |
| 204 |
'f443e3ca4d62b59aca84e990cacaf5c5' + |
| 205 |
'2b0930daa23de94ce87017ba2d8498')); |
| 206 |
} |