| 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 Implementation of CBC mode for block ciphers. See |
| 17 |
* http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation |
| 18 |
* #Cipher-block_chaining_.28CBC.29. for description. |
| 19 |
* |
| 20 |
* @author [email protected] (Nathan Naze) |
| 21 |
*/ |
| 22 |
|
| 23 |
goog.provide('goog.crypt.Cbc'); |
| 24 |
|
| 25 |
goog.require('goog.array'); |
| 26 |
goog.require('goog.asserts'); |
| 27 |
goog.require('goog.crypt'); |
| 28 |
goog.require('goog.crypt.BlockCipher'); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* Implements the CBC mode for block ciphers. See |
| 34 |
* http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation |
| 35 |
* #Cipher-block_chaining_.28CBC.29 |
| 36 |
* |
| 37 |
* @param {!goog.crypt.BlockCipher} cipher The block cipher to use. |
| 38 |
* @constructor |
| 39 |
* @final |
| 40 |
* @struct |
| 41 |
*/ |
| 42 |
goog.crypt.Cbc = function(cipher) { |
| 43 |
|
| 44 |
/** |
| 45 |
* Block cipher. |
| 46 |
* @type {!goog.crypt.BlockCipher} |
| 47 |
* @private |
| 48 |
*/ |
| 49 |
this.cipher_ = cipher; |
| 50 |
}; |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* Encrypt a message. |
| 55 |
* |
| 56 |
* @param {!Array<number>|!Uint8Array} plainText Message to encrypt. An array of |
| 57 |
* bytes. The length should be a multiple of the block size. |
| 58 |
* @param {!Array<number>|!Uint8Array} initialVector Initial vector for the CBC |
| 59 |
* mode. An array of bytes with the same length as the block size. |
| 60 |
* @return {!Array<number>} Encrypted message. |
| 61 |
*/ |
| 62 |
goog.crypt.Cbc.prototype.encrypt = function(plainText, initialVector) { |
| 63 |
|
| 64 |
goog.asserts.assert( |
| 65 |
plainText.length % this.cipher_.BLOCK_SIZE == 0, |
| 66 |
'Data\'s length must be multiple of block size.'); |
| 67 |
|
| 68 |
goog.asserts.assert( |
| 69 |
initialVector.length == this.cipher_.BLOCK_SIZE, |
| 70 |
'Initial vector must be size of one block.'); |
| 71 |
|
| 72 |
// Implementation of |
| 73 |
// http://en.wikipedia.org/wiki/File:Cbc_encryption.png |
| 74 |
|
| 75 |
var cipherText = []; |
| 76 |
var vector = initialVector; |
| 77 |
|
| 78 |
// Generate each block of the encrypted cypher text. |
| 79 |
for (var blockStartIndex = 0; blockStartIndex < plainText.length; |
| 80 |
blockStartIndex += this.cipher_.BLOCK_SIZE) { |
| 81 |
// Takes one block from the input message. |
| 82 |
var plainTextBlock = goog.array.slice( |
| 83 |
plainText, blockStartIndex, blockStartIndex + this.cipher_.BLOCK_SIZE); |
| 84 |
|
| 85 |
var input = goog.crypt.xorByteArray(plainTextBlock, vector); |
| 86 |
var resultBlock = this.cipher_.encrypt(input); |
| 87 |
|
| 88 |
goog.array.extend(cipherText, resultBlock); |
| 89 |
vector = resultBlock; |
| 90 |
} |
| 91 |
|
| 92 |
return cipherText; |
| 93 |
}; |
| 94 |
|
| 95 |
|
| 96 |
/** |
| 97 |
* Decrypt a message. |
| 98 |
* |
| 99 |
* @param {!Array<number>|!Uint8Array} cipherText Message to decrypt. An array |
| 100 |
* of bytes. The length should be a multiple of the block size. |
| 101 |
* @param {!Array<number>|!Uint8Array} initialVector Initial vector for the CBC |
| 102 |
* mode. An array of bytes with the same length as the block size. |
| 103 |
* @return {!Array<number>} Decrypted message. |
| 104 |
*/ |
| 105 |
goog.crypt.Cbc.prototype.decrypt = function(cipherText, initialVector) { |
| 106 |
|
| 107 |
goog.asserts.assert( |
| 108 |
cipherText.length % this.cipher_.BLOCK_SIZE == 0, |
| 109 |
'Data\'s length must be multiple of block size.'); |
| 110 |
|
| 111 |
goog.asserts.assert( |
| 112 |
initialVector.length == this.cipher_.BLOCK_SIZE, |
| 113 |
'Initial vector must be size of one block.'); |
| 114 |
|
| 115 |
// Implementation of |
| 116 |
// http://en.wikipedia.org/wiki/File:Cbc_decryption.png |
| 117 |
|
| 118 |
var plainText = []; |
| 119 |
var blockStartIndex = 0; |
| 120 |
var vector = initialVector; |
| 121 |
|
| 122 |
// Generate each block of the decrypted plain text. |
| 123 |
while (blockStartIndex < cipherText.length) { |
| 124 |
// Takes one block. |
| 125 |
var cipherTextBlock = goog.array.slice( |
| 126 |
cipherText, blockStartIndex, blockStartIndex + this.cipher_.BLOCK_SIZE); |
| 127 |
|
| 128 |
var resultBlock = this.cipher_.decrypt(cipherTextBlock); |
| 129 |
var plainTextBlock = goog.crypt.xorByteArray(vector, resultBlock); |
| 130 |
|
| 131 |
goog.array.extend(plainText, plainTextBlock); |
| 132 |
vector = cipherTextBlock; |
| 133 |
|
| 134 |
blockStartIndex += this.cipher_.BLOCK_SIZE; |
| 135 |
} |
| 136 |
|
| 137 |
return plainText; |
| 138 |
}; |