des-test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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', function() {
  9. describe('Key Derivation', function() {
  10. it('should derive proper keys', function() {
  11. var d = des.DES.create({
  12. type: 'encrypt',
  13. key: [ 0x13, 0x34, 0x57, 0x79, 0x9B, 0xBC, 0xDF, 0xF1 ]
  14. });
  15. var expected = [
  16. '000110 110000 001011 101111',
  17. '111111 000111 000001 110010',
  18. '011110 011010 111011 011001',
  19. '110110 111100 100111 100101',
  20. '010101 011111 110010 001010',
  21. '010000 101100 111110 011001',
  22. '011100 101010 110111 010110',
  23. '110110 110011 010100 011101',
  24. '011111 001110 110000 000111',
  25. '111010 110101 001110 101000',
  26. '011000 111010 010100 111110',
  27. '010100 000111 101100 101111',
  28. '111011 001000 010010 110111',
  29. '111101 100001 100010 111100',
  30. '111101 111000 101000 111010',
  31. '110000 010011 101111 111011',
  32. '111000 001101 101111 101011',
  33. '111011 011110 011110 000001',
  34. '101100 011111 001101 000111',
  35. '101110 100100 011001 001111',
  36. '001000 010101 111111 010011',
  37. '110111 101101 001110 000110',
  38. '011101 010111 000111 110101',
  39. '100101 000110 011111 101001',
  40. '100101 111100 010111 010001',
  41. '111110 101011 101001 000001',
  42. '010111 110100 001110 110111',
  43. '111100 101110 011100 111010',
  44. '101111 111001 000110 001101',
  45. '001111 010011 111100 001010',
  46. '110010 110011 110110 001011',
  47. '000011 100001 011111 110101'
  48. ];
  49. expected = expected.map(fixtures.bin);
  50. assert.deepEqual(d._desState.keys, expected);
  51. });
  52. });
  53. describe('encryption/decryption', function() {
  54. var vectors = [
  55. {
  56. key: '133457799bbcdff1',
  57. input: '0123456789abcdef'
  58. },
  59. {
  60. key: '0000000000000000',
  61. input: '0000000000000000'
  62. },
  63. {
  64. key: 'a3a3a3a3b3b3b3b3',
  65. input: 'cccccccccccccccc'
  66. },
  67. {
  68. key: 'deadbeefabbadead',
  69. input: '0102030405060708090a'
  70. },
  71. {
  72. key: 'aabbccddeeff0011',
  73. input: '0102030405060708090a0102030405060708090a0102030405060708090a' +
  74. '0102030405060708090a0102030405060607080a0102030405060708090a'
  75. }
  76. ];
  77. vectors.forEach(function(vec, i) {
  78. it('should encrypt vector ' + i, function() {
  79. var key = new Buffer(vec.key, 'hex');
  80. var input = new Buffer(vec.input, 'hex');
  81. var enc = des.DES.create({
  82. type: 'encrypt',
  83. key: key
  84. });
  85. var dec = des.DES.create({
  86. type: 'decrypt',
  87. key: key
  88. });
  89. var out = new Buffer(enc.update(input).concat(enc.final()));
  90. var cipher = crypto.createCipheriv('des-ecb', key, new Buffer(0));
  91. var expected = Buffer.concat([ cipher.update(input), cipher.final() ]);
  92. assert.deepEqual(out, expected);
  93. assert.deepEqual(new Buffer(dec.update(out).concat(dec.final())),
  94. input);
  95. });
  96. });
  97. it('should buffer during encryption/decryption', function() {
  98. var key = new Buffer('0102030405060708', 'hex');
  99. var chunk = new Buffer('01020304050607', 'hex');
  100. var count = 257;
  101. var expected = new Buffer(
  102. new Array(count + 1).join('01020304050607'), 'hex');
  103. var enc = des.DES.create({
  104. type: 'encrypt',
  105. key: key
  106. });
  107. var cipher = [];
  108. for (var i = 0; i < count; i++)
  109. cipher = cipher.concat(enc.update(chunk));
  110. cipher = cipher.concat(enc.final());
  111. var dec = des.DES.create({
  112. type: 'decrypt',
  113. key: key
  114. });
  115. var out = [];
  116. for (var i = 0; i < count; i++)
  117. out = out.concat(dec.update(cipher.slice(i * 7, (i + 1) * 7)));
  118. out = out.concat(dec.final(cipher.slice(i * 7)));
  119. out = new Buffer(out);
  120. assert.deepEqual(out, expected);
  121. });
  122. });
  123. });