cbc.js 4.2 KB

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