ctr.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. goog.provide('goog.crypt.Ctr');
  15. goog.require('goog.array');
  16. goog.require('goog.asserts');
  17. goog.require('goog.crypt');
  18. /**
  19. * Implementation of Ctr mode for block ciphers. See
  20. * http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation
  21. * #Cipher-block_chaining_.28Ctr.29. for an overview, and
  22. * http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
  23. * for the spec.
  24. *
  25. * @param {!goog.crypt.BlockCipher} cipher The block cipher to use.
  26. * @constructor
  27. * @final
  28. * @struct
  29. */
  30. goog.crypt.Ctr = function(cipher) {
  31. /**
  32. * Block cipher.
  33. * @type {!goog.crypt.BlockCipher}
  34. * @private
  35. */
  36. this.cipher_ = cipher;
  37. };
  38. /**
  39. * Encrypts a message.
  40. *
  41. * @param {!Array<number>|!Uint8Array} plainText Message to encrypt. An array of
  42. * bytes. The length does not have to be a multiple of the blocksize.
  43. * @param {!Array<number>|!Uint8Array} initialVector Initial vector for the Ctr
  44. * mode. An array of bytes with the same length as the block size, that
  45. * should be not reused when using the same key.
  46. * @return {!Array<number>} Encrypted message.
  47. */
  48. goog.crypt.Ctr.prototype.encrypt = function(plainText, initialVector) {
  49. goog.asserts.assert(
  50. initialVector.length == this.cipher_.BLOCK_SIZE,
  51. 'Initial vector must be size of one block.');
  52. // Copy the IV, so it's not modified.
  53. var counter = goog.array.clone(initialVector);
  54. var keyStreamBlock = [];
  55. var encryptedArray = [];
  56. var plainTextBlock = [];
  57. while (encryptedArray.length < plainText.length) {
  58. keyStreamBlock = this.cipher_.encrypt(counter);
  59. goog.crypt.Ctr.incrementBigEndianCounter_(counter);
  60. plainTextBlock = goog.array.slice(
  61. plainText, encryptedArray.length,
  62. encryptedArray.length + this.cipher_.BLOCK_SIZE);
  63. goog.array.extend(
  64. encryptedArray,
  65. goog.crypt.xorByteArray(
  66. plainTextBlock,
  67. goog.array.slice(keyStreamBlock, 0, plainTextBlock.length)));
  68. }
  69. return encryptedArray;
  70. };
  71. /**
  72. * Decrypts a message. In CTR, this is the same as encrypting.
  73. *
  74. * @param {!Array<number>|!Uint8Array} cipherText Message to decrypt. The length
  75. * does not have to be a multiple of the blocksize.
  76. * @param {!Array<number>|!Uint8Array} initialVector Initial vector for the Ctr
  77. * mode. An array of bytes with the same length as the block size.
  78. * @return {!Array<number>} Decrypted message.
  79. */
  80. goog.crypt.Ctr.prototype.decrypt = goog.crypt.Ctr.prototype.encrypt;
  81. /**
  82. * Increments the big-endian integer represented in counter in-place.
  83. *
  84. * @param {!Array<number>|!Uint8Array} counter The array of bytes to modify.
  85. * @private
  86. */
  87. goog.crypt.Ctr.incrementBigEndianCounter_ = function(counter) {
  88. for (var i = counter.length - 1; i >= 0; i--) {
  89. var currentByte = counter[i];
  90. currentByte = (currentByte + 1) & 0xFF; // Allow wrapping around.
  91. counter[i] = currentByte;
  92. if (currentByte != 0) {
  93. // This iteration hasn't wrapped around, which means there is
  94. // no carry to add to the next byte.
  95. return;
  96. } // else, repeat with next byte.
  97. }
  98. };