| 1 |
// Copyright 2012 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 CBC mode for block ciphers. |
| 17 |
* |
| 18 |
* @author [email protected] (Nathan Naze) |
| 19 |
*/ |
| 20 |
|
| 21 |
/** @suppress {extraProvide} */ |
| 22 |
goog.provide('goog.crypt.CbcTest'); |
| 23 |
|
| 24 |
goog.require('goog.crypt'); |
| 25 |
goog.require('goog.crypt.Aes'); |
| 26 |
goog.require('goog.crypt.Cbc'); |
| 27 |
goog.require('goog.testing.jsunit'); |
| 28 |
|
| 29 |
goog.setTestOnly('goog.crypt.CbcTest'); |
| 30 |
|
| 31 |
function stringToBytes(s) { |
| 32 |
var bytes = new Array(s.length); |
| 33 |
for (var i = 0; i < s.length; ++i) bytes[i] = s.charCodeAt(i) & 255; |
| 34 |
return bytes; |
| 35 |
} |
| 36 |
|
| 37 |
function runCbcAesTest( |
| 38 |
keyBytes, initialVectorBytes, plainTextBytes, cipherTextBytes) { |
| 39 |
var aes = new goog.crypt.Aes(keyBytes); |
| 40 |
var cbc = new goog.crypt.Cbc(aes); |
| 41 |
|
| 42 |
var encryptedBytes = cbc.encrypt(plainTextBytes, initialVectorBytes); |
| 43 |
assertEquals( |
| 44 |
'Encrypted bytes should match cipher text.', |
| 45 |
goog.crypt.byteArrayToHex(cipherTextBytes), |
| 46 |
goog.crypt.byteArrayToHex(encryptedBytes)); |
| 47 |
|
| 48 |
var decryptedBytes = cbc.decrypt(cipherTextBytes, initialVectorBytes); |
| 49 |
assertEquals( |
| 50 |
'Decrypted bytes should match plain text.', |
| 51 |
goog.crypt.byteArrayToHex(plainTextBytes), |
| 52 |
goog.crypt.byteArrayToHex(decryptedBytes)); |
| 53 |
} |
| 54 |
|
| 55 |
function testAesCbcCipherAlgorithm() { |
| 56 |
// Test values from http://www.ietf.org/rfc/rfc3602.txt |
| 57 |
|
| 58 |
// Case #1 |
| 59 |
runCbcAesTest( |
| 60 |
goog.crypt.hexToByteArray('06a9214036b8a15b512e03d534120006'), |
| 61 |
goog.crypt.hexToByteArray('3dafba429d9eb430b422da802c9fac41'), |
| 62 |
stringToBytes('Single block msg'), |
| 63 |
goog.crypt.hexToByteArray('e353779c1079aeb82708942dbe77181a')); |
| 64 |
|
| 65 |
// Case #2 |
| 66 |
runCbcAesTest( |
| 67 |
goog.crypt.hexToByteArray('c286696d887c9aa0611bbb3e2025a45a'), |
| 68 |
goog.crypt.hexToByteArray('562e17996d093d28ddb3ba695a2e6f58'), |
| 69 |
goog.crypt.hexToByteArray( |
| 70 |
'000102030405060708090a0b0c0d0e0f' + |
| 71 |
'101112131415161718191a1b1c1d1e1f'), |
| 72 |
goog.crypt.hexToByteArray( |
| 73 |
'd296cd94c2cccf8a3a863028b5e1dc0a' + |
| 74 |
'7586602d253cfff91b8266bea6d61ab1')); |
| 75 |
|
| 76 |
// Case #3 |
| 77 |
runCbcAesTest( |
| 78 |
goog.crypt.hexToByteArray('6c3ea0477630ce21a2ce334aa746c2cd'), |
| 79 |
goog.crypt.hexToByteArray('c782dc4c098c66cbd9cd27d825682c81'), |
| 80 |
stringToBytes('This is a 48-byte message (exactly 3 AES blocks)'), |
| 81 |
goog.crypt.hexToByteArray( |
| 82 |
'd0a02b3836451753d493665d33f0e886' + |
| 83 |
'2dea54cdb293abc7506939276772f8d5' + |
| 84 |
'021c19216bad525c8579695d83ba2684')); |
| 85 |
|
| 86 |
// Case #4 |
| 87 |
runCbcAesTest( |
| 88 |
goog.crypt.hexToByteArray('56e47a38c5598974bc46903dba290349'), |
| 89 |
goog.crypt.hexToByteArray('8ce82eefbea0da3c44699ed7db51b7d9'), |
| 90 |
goog.crypt.hexToByteArray( |
| 91 |
'a0a1a2a3a4a5a6a7a8a9aaabacadaeaf' + |
| 92 |
'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' + |
| 93 |
'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf' + |
| 94 |
'd0d1d2d3d4d5d6d7d8d9dadbdcdddedf'), |
| 95 |
goog.crypt.hexToByteArray( |
| 96 |
'c30e32ffedc0774e6aff6af0869f71aa' + |
| 97 |
'0f3af07a9a31a9c684db207eb0ef8e4e' + |
| 98 |
'35907aa632c3ffdf868bb7b29d3d46ad' + |
| 99 |
'83ce9f9a102ee99d49a53e87f4c3da55')); |
| 100 |
} |