cbc_test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Unit tests for CBC mode for block ciphers.
  16. *
  17. * @author nnaze@google.com (Nathan Naze)
  18. */
  19. /** @suppress {extraProvide} */
  20. goog.provide('goog.crypt.CbcTest');
  21. goog.require('goog.crypt');
  22. goog.require('goog.crypt.Aes');
  23. goog.require('goog.crypt.Cbc');
  24. goog.require('goog.testing.jsunit');
  25. goog.setTestOnly('goog.crypt.CbcTest');
  26. function stringToBytes(s) {
  27. var bytes = new Array(s.length);
  28. for (var i = 0; i < s.length; ++i) bytes[i] = s.charCodeAt(i) & 255;
  29. return bytes;
  30. }
  31. function runCbcAesTest(
  32. keyBytes, initialVectorBytes, plainTextBytes, cipherTextBytes) {
  33. var aes = new goog.crypt.Aes(keyBytes);
  34. var cbc = new goog.crypt.Cbc(aes);
  35. var encryptedBytes = cbc.encrypt(plainTextBytes, initialVectorBytes);
  36. assertEquals(
  37. 'Encrypted bytes should match cipher text.',
  38. goog.crypt.byteArrayToHex(cipherTextBytes),
  39. goog.crypt.byteArrayToHex(encryptedBytes));
  40. var decryptedBytes = cbc.decrypt(cipherTextBytes, initialVectorBytes);
  41. assertEquals(
  42. 'Decrypted bytes should match plain text.',
  43. goog.crypt.byteArrayToHex(plainTextBytes),
  44. goog.crypt.byteArrayToHex(decryptedBytes));
  45. }
  46. function testAesCbcCipherAlgorithm() {
  47. // Test values from http://www.ietf.org/rfc/rfc3602.txt
  48. // Case #1
  49. runCbcAesTest(
  50. goog.crypt.hexToByteArray('06a9214036b8a15b512e03d534120006'),
  51. goog.crypt.hexToByteArray('3dafba429d9eb430b422da802c9fac41'),
  52. stringToBytes('Single block msg'),
  53. goog.crypt.hexToByteArray('e353779c1079aeb82708942dbe77181a'));
  54. // Case #2
  55. runCbcAesTest(
  56. goog.crypt.hexToByteArray('c286696d887c9aa0611bbb3e2025a45a'),
  57. goog.crypt.hexToByteArray('562e17996d093d28ddb3ba695a2e6f58'),
  58. goog.crypt.hexToByteArray(
  59. '000102030405060708090a0b0c0d0e0f' +
  60. '101112131415161718191a1b1c1d1e1f'),
  61. goog.crypt.hexToByteArray(
  62. 'd296cd94c2cccf8a3a863028b5e1dc0a' +
  63. '7586602d253cfff91b8266bea6d61ab1'));
  64. // Case #3
  65. runCbcAesTest(
  66. goog.crypt.hexToByteArray('6c3ea0477630ce21a2ce334aa746c2cd'),
  67. goog.crypt.hexToByteArray('c782dc4c098c66cbd9cd27d825682c81'),
  68. stringToBytes('This is a 48-byte message (exactly 3 AES blocks)'),
  69. goog.crypt.hexToByteArray(
  70. 'd0a02b3836451753d493665d33f0e886' +
  71. '2dea54cdb293abc7506939276772f8d5' +
  72. '021c19216bad525c8579695d83ba2684'));
  73. // Case #4
  74. runCbcAesTest(
  75. goog.crypt.hexToByteArray('56e47a38c5598974bc46903dba290349'),
  76. goog.crypt.hexToByteArray('8ce82eefbea0da3c44699ed7db51b7d9'),
  77. goog.crypt.hexToByteArray(
  78. 'a0a1a2a3a4a5a6a7a8a9aaabacadaeaf' +
  79. 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' +
  80. 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf' +
  81. 'd0d1d2d3d4d5d6d7d8d9dadbdcdddedf'),
  82. goog.crypt.hexToByteArray(
  83. 'c30e32ffedc0774e6aff6af0869f71aa' +
  84. '0f3af07a9a31a9c684db207eb0ef8e4e' +
  85. '35907aa632c3ffdf868bb7b29d3d46ad' +
  86. '83ce9f9a102ee99d49a53e87f4c3da55'));
  87. }