ctr_test.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /**
  15. * @fileoverview Unit tests for CTR mode for block ciphers.
  16. */
  17. /** @suppress {extraProvide} */
  18. goog.provide('goog.crypt.CtrTest');
  19. goog.require('goog.crypt');
  20. goog.require('goog.crypt.Aes');
  21. goog.require('goog.crypt.Ctr');
  22. goog.require('goog.testing.jsunit');
  23. goog.setTestOnly('goog.crypt.CtrTest');
  24. /**
  25. * Asserts the given parameters allow encryption and decryption from and to the
  26. * given plaintext/ciphertext.
  27. *
  28. * @param {!Array<number>|!Uint8Array} keyBytes
  29. * @param {!Array<number>|!Uint8Array} initialVectorBytes
  30. * @param {!Array<number>|!Uint8Array} plainTextBytes
  31. * @param {!Array<number>|!Uint8Array} cipherTextBytes
  32. */
  33. function runCtrAesTest(
  34. keyBytes, initialVectorBytes, plainTextBytes, cipherTextBytes) {
  35. var aes = new goog.crypt.Aes(keyBytes);
  36. var Ctr = new goog.crypt.Ctr(aes);
  37. var encryptedBytes = Ctr.encrypt(plainTextBytes, initialVectorBytes);
  38. assertEquals(
  39. 'Encrypted bytes should match cipher text.',
  40. goog.crypt.byteArrayToHex(encryptedBytes),
  41. goog.crypt.byteArrayToHex(cipherTextBytes));
  42. var decryptedBytes = Ctr.decrypt(cipherTextBytes, initialVectorBytes);
  43. assertEquals(
  44. 'Decrypted bytes should match plain text.',
  45. goog.crypt.byteArrayToHex(decryptedBytes),
  46. goog.crypt.byteArrayToHex(plainTextBytes));
  47. }
  48. /**
  49. * Asserts goog.crypt.Ctr.incrementBigEndianCounter turns the first parameter
  50. * into the second.
  51. *
  52. * @param {string} toIncrement
  53. * @param {string} expectedResult
  54. */
  55. function assertIncEquals(toIncrement, expectedResult) {
  56. var counter = goog.crypt.hexToByteArray(toIncrement);
  57. goog.crypt.Ctr.incrementBigEndianCounter_(counter);
  58. assertEquals(expectedResult, goog.crypt.byteArrayToHex(counter));
  59. }
  60. function testIncrementCounter() {
  61. assertIncEquals('', '');
  62. assertIncEquals('00', '01');
  63. assertIncEquals('09', '0a');
  64. assertIncEquals('0e', '0f');
  65. assertIncEquals('0f', '10');
  66. assertIncEquals('1f', '20');
  67. assertIncEquals('ff', '00'); // no length extension
  68. assertIncEquals('0000', '0001');
  69. assertIncEquals('00f0', '00f1');
  70. assertIncEquals('00ff', '0100');
  71. assertIncEquals('0f00', '0f01');
  72. assertIncEquals('0fff', '1000');
  73. assertIncEquals('1000', '1001');
  74. assertIncEquals('ff00', 'ff01');
  75. assertIncEquals('ff0f', 'ff10');
  76. assertIncEquals('ffff', '0000');
  77. assertIncEquals(
  78. 'ffffffffffffffffffffffffffffffffffffffffffffffff',
  79. '000000000000000000000000000000000000000000000000');
  80. }
  81. function testAes128CtrCipherAlgorithm() {
  82. // Test vectors from NIST sp800-38a, p 55
  83. // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
  84. // Case #1, no chaining
  85. runCtrAesTest(
  86. goog.crypt.hexToByteArray('2b7e151628aed2a6abf7158809cf4f3c'),
  87. goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
  88. goog.crypt.hexToByteArray('6bc1bee22e409f96e93d7e117393172a'),
  89. goog.crypt.hexToByteArray('874d6191b620e3261bef6864990db6ce'));
  90. // Case #2, three blocks
  91. runCtrAesTest(
  92. goog.crypt.hexToByteArray('2b7e151628aed2a6abf7158809cf4f3c'),
  93. goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
  94. goog.crypt.hexToByteArray(
  95. '6bc1bee22e409f96e93d7e117393172a' +
  96. 'ae2d8a571e03ac9c9eb76fac45af8e51' +
  97. '30c81c46a35ce411e5fbc1191a0a52ef'),
  98. goog.crypt.hexToByteArray(
  99. '874d6191b620e3261bef6864990db6ce' +
  100. '9806f66b7970fdff8617187bb9fffdff' +
  101. '5ae4df3edbd5d35e5b4f09020db03eab'));
  102. // Case #3, plaintext length not a multiple of blocksize
  103. runCtrAesTest(
  104. goog.crypt.hexToByteArray('2b7e151628aed2a6abf7158809cf4f3c'),
  105. goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
  106. goog.crypt.hexToByteArray(
  107. '6bc1bee22e409f96e93d7e117393172a' +
  108. 'ae2d8a571e03ac9c9eb76fac45af8e51' +
  109. '30c81c'),
  110. goog.crypt.hexToByteArray(
  111. '874d6191b620e3261bef6864990db6ce' +
  112. '9806f66b7970fdff8617187bb9fffdff' +
  113. '5ae4df'));
  114. }
  115. function testAes192CtrCipherAlgorithm() {
  116. // Test vectors from NIST sp800-38a, p 56
  117. // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
  118. // Key block is weird, that's normal: 192 is one block and a half.
  119. // Case #1, no chaining
  120. runCtrAesTest(
  121. goog.crypt.hexToByteArray(
  122. '8e73b0f7da0e6452c810f32b809079e5' +
  123. '62f8ead2522c6b7b'),
  124. goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
  125. goog.crypt.hexToByteArray('6bc1bee22e409f96e93d7e117393172a'),
  126. goog.crypt.hexToByteArray('1abc932417521ca24f2b0459fe7e6e0b'));
  127. // Case #2, three blocks
  128. runCtrAesTest(
  129. goog.crypt.hexToByteArray(
  130. '8e73b0f7da0e6452c810f32b809079e5' +
  131. '62f8ead2522c6b7b'),
  132. goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
  133. goog.crypt.hexToByteArray(
  134. '6bc1bee22e409f96e93d7e117393172a' +
  135. 'ae2d8a571e03ac9c9eb76fac45af8e51' +
  136. '30c81c46a35ce411e5fbc1191a0a52ef'),
  137. goog.crypt.hexToByteArray(
  138. '1abc932417521ca24f2b0459fe7e6e0b' +
  139. '090339ec0aa6faefd5ccc2c6f4ce8e94' +
  140. '1e36b26bd1ebc670d1bd1d665620abf7'));
  141. }
  142. function testAes256CtrCipherAlgorithm() {
  143. // Test vectors from NIST sp800-38a, p 57
  144. // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
  145. // Case #1, no chaining
  146. runCtrAesTest(
  147. goog.crypt.hexToByteArray(
  148. '603deb1015ca71be2b73aef0857d7781' +
  149. '1f352c073b6108d72d9810a30914dff4'),
  150. goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
  151. goog.crypt.hexToByteArray('6bc1bee22e409f96e93d7e117393172a'),
  152. goog.crypt.hexToByteArray('601ec313775789a5b7a7f504bbf3d228'));
  153. // Case #2, three blocks
  154. runCtrAesTest(
  155. goog.crypt.hexToByteArray(
  156. '603deb1015ca71be2b73aef0857d7781' +
  157. '1f352c073b6108d72d9810a30914dff4'),
  158. goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
  159. goog.crypt.hexToByteArray(
  160. '6bc1bee22e409f96e93d7e117393172a' +
  161. 'ae2d8a571e03ac9c9eb76fac45af8e51' +
  162. '30c81c46a35ce411e5fbc1191a0a52ef'),
  163. goog.crypt.hexToByteArray(
  164. '601ec313775789a5b7a7f504bbf3d228' +
  165. 'f443e3ca4d62b59aca84e990cacaf5c5' +
  166. '2b0930daa23de94ce87017ba2d84988d'));
  167. // Case #3, plaintext length not a multiple of blocksize
  168. runCtrAesTest(
  169. goog.crypt.hexToByteArray(
  170. '603deb1015ca71be2b73aef0857d7781' +
  171. '1f352c073b6108d72d9810a30914dff4'),
  172. goog.crypt.hexToByteArray('f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff'),
  173. goog.crypt.hexToByteArray(
  174. '6bc1bee22e409f96e93d7e117393172a' +
  175. 'ae2d8a571e03ac9c9eb76fac45af8e51' +
  176. '30c81c46a35ce411e5fbc1191a0a52'),
  177. goog.crypt.hexToByteArray(
  178. '601ec313775789a5b7a7f504bbf3d228' +
  179. 'f443e3ca4d62b59aca84e990cacaf5c5' +
  180. '2b0930daa23de94ce87017ba2d8498'));
  181. }