You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

des.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict';
  2. var assert = require('minimalistic-assert');
  3. var inherits = require('inherits');
  4. var des = require('../des');
  5. var utils = des.utils;
  6. var Cipher = des.Cipher;
  7. function DESState() {
  8. this.tmp = new Array(2);
  9. this.keys = null;
  10. }
  11. function DES(options) {
  12. Cipher.call(this, options);
  13. var state = new DESState();
  14. this._desState = state;
  15. this.deriveKeys(state, options.key);
  16. }
  17. inherits(DES, Cipher);
  18. module.exports = DES;
  19. DES.create = function create(options) {
  20. return new DES(options);
  21. };
  22. var shiftTable = [
  23. 1, 1, 2, 2, 2, 2, 2, 2,
  24. 1, 2, 2, 2, 2, 2, 2, 1
  25. ];
  26. DES.prototype.deriveKeys = function deriveKeys(state, key) {
  27. state.keys = new Array(16 * 2);
  28. assert.equal(key.length, this.blockSize, 'Invalid key length');
  29. var kL = utils.readUInt32BE(key, 0);
  30. var kR = utils.readUInt32BE(key, 4);
  31. utils.pc1(kL, kR, state.tmp, 0);
  32. kL = state.tmp[0];
  33. kR = state.tmp[1];
  34. for (var i = 0; i < state.keys.length; i += 2) {
  35. var shift = shiftTable[i >>> 1];
  36. kL = utils.r28shl(kL, shift);
  37. kR = utils.r28shl(kR, shift);
  38. utils.pc2(kL, kR, state.keys, i);
  39. }
  40. };
  41. DES.prototype._update = function _update(inp, inOff, out, outOff) {
  42. var state = this._desState;
  43. var l = utils.readUInt32BE(inp, inOff);
  44. var r = utils.readUInt32BE(inp, inOff + 4);
  45. // Initial Permutation
  46. utils.ip(l, r, state.tmp, 0);
  47. l = state.tmp[0];
  48. r = state.tmp[1];
  49. if (this.type === 'encrypt')
  50. this._encrypt(state, l, r, state.tmp, 0);
  51. else
  52. this._decrypt(state, l, r, state.tmp, 0);
  53. l = state.tmp[0];
  54. r = state.tmp[1];
  55. utils.writeUInt32BE(out, l, outOff);
  56. utils.writeUInt32BE(out, r, outOff + 4);
  57. };
  58. DES.prototype._pad = function _pad(buffer, off) {
  59. var value = buffer.length - off;
  60. for (var i = off; i < buffer.length; i++)
  61. buffer[i] = value;
  62. return true;
  63. };
  64. DES.prototype._unpad = function _unpad(buffer) {
  65. var pad = buffer[buffer.length - 1];
  66. for (var i = buffer.length - pad; i < buffer.length; i++)
  67. assert.equal(buffer[i], pad);
  68. return buffer.slice(0, buffer.length - pad);
  69. };
  70. DES.prototype._encrypt = function _encrypt(state, lStart, rStart, out, off) {
  71. var l = lStart;
  72. var r = rStart;
  73. // Apply f() x16 times
  74. for (var i = 0; i < state.keys.length; i += 2) {
  75. var keyL = state.keys[i];
  76. var keyR = state.keys[i + 1];
  77. // f(r, k)
  78. utils.expand(r, state.tmp, 0);
  79. keyL ^= state.tmp[0];
  80. keyR ^= state.tmp[1];
  81. var s = utils.substitute(keyL, keyR);
  82. var f = utils.permute(s);
  83. var t = r;
  84. r = (l ^ f) >>> 0;
  85. l = t;
  86. }
  87. // Reverse Initial Permutation
  88. utils.rip(r, l, out, off);
  89. };
  90. DES.prototype._decrypt = function _decrypt(state, lStart, rStart, out, off) {
  91. var l = rStart;
  92. var r = lStart;
  93. // Apply f() x16 times
  94. for (var i = state.keys.length - 2; i >= 0; i -= 2) {
  95. var keyL = state.keys[i];
  96. var keyR = state.keys[i + 1];
  97. // f(r, k)
  98. utils.expand(l, state.tmp, 0);
  99. keyL ^= state.tmp[0];
  100. keyR ^= state.tmp[1];
  101. var s = utils.substitute(keyL, keyR);
  102. var f = utils.permute(s);
  103. var t = l;
  104. l = (r ^ f) >>> 0;
  105. r = t;
  106. }
  107. // Reverse Initial Permutation
  108. utils.rip(l, r, out, off);
  109. };