cbc-test.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. var assert = require('assert');
  3. var crypto = require('crypto');
  4. var Buffer = require('buffer').Buffer;
  5. var des = require('../');
  6. var fixtures = require('./fixtures');
  7. var bin = fixtures.bin;
  8. describe('DES-CBC', function() {
  9. var CBC = des.CBC.instantiate(des.DES);
  10. describe('encryption/decryption', function() {
  11. var vectors = [
  12. {
  13. key: '133457799bbcdff1',
  14. iv: '0102030405060708',
  15. input: '0123456789abcdef'
  16. },
  17. {
  18. key: '0000000000000000',
  19. iv: 'ffffffffffffffff',
  20. input: '0000000000000000'
  21. },
  22. {
  23. key: 'a3a3a3a3b3b3b3b3',
  24. iv: 'cdcdcdcdcdcdcdcd',
  25. input: 'cccccccccccccccc'
  26. },
  27. {
  28. key: 'deadbeefabbadead',
  29. iv: 'a0da0da0da0da0da',
  30. input: '0102030405060708090a'
  31. },
  32. {
  33. key: 'aabbccddeeff0011',
  34. iv: 'fefefefefefefefe',
  35. input: '0102030405060708090a0102030405060708090a0102030405060708090a' +
  36. '0102030405060708090a0102030405060607080a0102030405060708090a'
  37. }
  38. ];
  39. vectors.forEach(function(vec, i) {
  40. it('should encrypt vector ' + i, function() {
  41. var key = new Buffer(vec.key, 'hex');
  42. var iv = new Buffer(vec.iv, 'hex');
  43. var input = new Buffer(vec.input, 'hex');
  44. var enc = CBC.create({
  45. type: 'encrypt',
  46. key: key,
  47. iv: iv
  48. });
  49. var out = new Buffer(enc.update(input).concat(enc.final()));
  50. var cipher = crypto.createCipheriv('des-cbc', key, iv);
  51. var expected = Buffer.concat([ cipher.update(input), cipher.final() ]);
  52. assert.deepEqual(out, expected);
  53. var dec = CBC.create({
  54. type: 'decrypt',
  55. key: key,
  56. iv: iv
  57. });
  58. assert.deepEqual(new Buffer(dec.update(out).concat(dec.final())),
  59. input);
  60. });
  61. });
  62. });
  63. });