aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/crypt/cbc.js
blob: b4c9efb1c81ec2139be11dae0a923c6d165fbda8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2012 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview Implementation of CBC mode for block ciphers.  See
 *     http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation
 *     #Cipher-block_chaining_.28CBC.29. for description.
 *
 * @author nnaze@google.com (Nathan Naze)
 */

goog.provide('goog.crypt.Cbc');

goog.require('goog.array');



/**
 * Implements the CBC mode for block ciphers. See
 * http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation
 * #Cipher-block_chaining_.28CBC.29
 *
 * @param {!goog.crypt.BlockCipher} cipher The block cipher to use.
 * @param {number=} opt_blockSize The block size of the cipher in bytes.
 *     Defaults to 16 bytes.
 * @constructor
 */
goog.crypt.Cbc = function(cipher, opt_blockSize) {

  /**
   * Block cipher.
   * @type {!goog.crypt.BlockCipher}
   * @private
   */
  this.cipher_ = cipher;

  /**
   * Block size in bytes.
   * @type {number}
   * @private
   */
  this.blockSize_ = opt_blockSize || 16;
};


/**
 * Encrypt a message.
 *
 * @param {!Array.<number>} plainText Message to encrypt. An array of bytes.
 *     The length should be a multiple of the block size.
 * @param {!Array.<number>} initialVector Initial vector for the CBC mode.
 *     An array of bytes with the same length as the block size.
 * @return {!Array.<number>} Encrypted message.
 */
goog.crypt.Cbc.prototype.encrypt = function(plainText, initialVector) {

  goog.asserts.assert(
      plainText.length % this.blockSize_ == 0,
      'Data\'s length must be multiple of block size.');

  goog.asserts.assert(
      initialVector.length == this.blockSize_,
      'Initial vector must be size of one block.');

  // Implementation of
  // http://en.wikipedia.org/wiki/File:Cbc_encryption.png

  var cipherText = [];
  var vector = initialVector;

  // Generate each block of the encrypted cypher text.
  for (var blockStartIndex = 0;
       blockStartIndex < plainText.length;
       blockStartIndex += this.blockSize_) {

    // Takes one block from the input message.
    var plainTextBlock = goog.array.slice(
        plainText,
        blockStartIndex,
        blockStartIndex + this.blockSize_);

    var input = goog.crypt.Cbc.xorByteArray_(plainTextBlock, vector);
    var resultBlock = this.cipher_.encrypt(input);

    goog.array.extend(cipherText, resultBlock);
    vector = resultBlock;
  }

  return cipherText;
};


/**
 * Decrypt a message.
 *
 * @param {!Array.<number>} cipherText Message to decrypt. An array of bytes.
 *     The length should be a multiple of the block size.
 * @param {!Array.<number>} initialVector Initial vector for the CBC mode.
 *     An array of bytes with the same length as the block size.
 * @return {!Array.<number>} Decrypted message.
 */
goog.crypt.Cbc.prototype.decrypt = function(cipherText, initialVector) {

  goog.asserts.assert(
      cipherText.length % this.blockSize_ == 0,
      'Data\'s length must be multiple of block size.');

  goog.asserts.assert(
      initialVector.length == this.blockSize_,
      'Initial vector must be size of one block.');

  // Implementation of
  // http://en.wikipedia.org/wiki/File:Cbc_decryption.png

  var plainText = [];
  var blockStartIndex = 0;
  var vector = initialVector;

  // Generate each block of the decrypted plain text.
  while (blockStartIndex < cipherText.length) {

    // Takes one block.
    var cipherTextBlock = goog.array.slice(
        cipherText,
        blockStartIndex,
        blockStartIndex + this.blockSize_);

    var resultBlock = this.cipher_.decrypt(cipherTextBlock);
    var plainTextBlock = goog.crypt.Cbc.xorByteArray_(vector, resultBlock);

    goog.array.extend(plainText, plainTextBlock);
    vector = cipherTextBlock;

    blockStartIndex += this.blockSize_;
  }

  return plainText;
};


/**
 * XOR two byte arrays.
 * @param {!Array.<number>} arr1 Byte array.
 * @param {!Array.<number>} arr2 Byte array.
 * @return {!Array.<number>} Resulting XOR of the two byte arrays.
 * @private
 */
goog.crypt.Cbc.xorByteArray_ = function(arr1, arr2) {
  goog.asserts.assert(arr1.length == arr2.length,
                      'XOR array lengths must match');
  var result = [];
  for (var i = 0; i < arr1.length; i++) {
    result.push(arr1[i] ^ arr2[i]);
  }
  return result;
};