zipcrypto.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. "use strict";
  2. // node crypt, we use it for generate salt
  3. // eslint-disable-next-line node/no-unsupported-features/node-builtins
  4. const { randomFillSync } = require("crypto");
  5. const Errors = require("../util/errors");
  6. // generate CRC32 lookup table
  7. const crctable = new Uint32Array(256).map((t, crc) => {
  8. for (let j = 0; j < 8; j++) {
  9. if (0 !== (crc & 1)) {
  10. crc = (crc >>> 1) ^ 0xedb88320;
  11. } else {
  12. crc >>>= 1;
  13. }
  14. }
  15. return crc >>> 0;
  16. });
  17. // C-style uInt32 Multiply (discards higher bits, when JS multiply discards lower bits)
  18. const uMul = (a, b) => Math.imul(a, b) >>> 0;
  19. // crc32 byte single update (actually same function is part of utils.crc32 function :) )
  20. const crc32update = (pCrc32, bval) => {
  21. return crctable[(pCrc32 ^ bval) & 0xff] ^ (pCrc32 >>> 8);
  22. };
  23. // function for generating salt for encrytion header
  24. const genSalt = () => {
  25. if ("function" === typeof randomFillSync) {
  26. return randomFillSync(Buffer.alloc(12));
  27. } else {
  28. // fallback if function is not defined
  29. return genSalt.node();
  30. }
  31. };
  32. // salt generation with node random function (mainly as fallback)
  33. genSalt.node = () => {
  34. const salt = Buffer.alloc(12);
  35. const len = salt.length;
  36. for (let i = 0; i < len; i++) salt[i] = (Math.random() * 256) & 0xff;
  37. return salt;
  38. };
  39. // general config
  40. const config = {
  41. genSalt
  42. };
  43. // Class Initkeys handles same basic ops with keys
  44. function Initkeys(pw) {
  45. const pass = Buffer.isBuffer(pw) ? pw : Buffer.from(pw);
  46. this.keys = new Uint32Array([0x12345678, 0x23456789, 0x34567890]);
  47. for (let i = 0; i < pass.length; i++) {
  48. this.updateKeys(pass[i]);
  49. }
  50. }
  51. Initkeys.prototype.updateKeys = function (byteValue) {
  52. const keys = this.keys;
  53. keys[0] = crc32update(keys[0], byteValue);
  54. keys[1] += keys[0] & 0xff;
  55. keys[1] = uMul(keys[1], 134775813) + 1;
  56. keys[2] = crc32update(keys[2], keys[1] >>> 24);
  57. return byteValue;
  58. };
  59. Initkeys.prototype.next = function () {
  60. const k = (this.keys[2] | 2) >>> 0; // key
  61. return (uMul(k, k ^ 1) >> 8) & 0xff; // decode
  62. };
  63. function make_decrypter(/*Buffer*/ pwd) {
  64. // 1. Stage initialize key
  65. const keys = new Initkeys(pwd);
  66. // return decrypter function
  67. return function (/*Buffer*/ data) {
  68. // result - we create new Buffer for results
  69. const result = Buffer.alloc(data.length);
  70. let pos = 0;
  71. // process input data
  72. for (let c of data) {
  73. //c ^= keys.next();
  74. //result[pos++] = c; // decode & Save Value
  75. result[pos++] = keys.updateKeys(c ^ keys.next()); // update keys with decoded byte
  76. }
  77. return result;
  78. };
  79. }
  80. function make_encrypter(/*Buffer*/ pwd) {
  81. // 1. Stage initialize key
  82. const keys = new Initkeys(pwd);
  83. // return encrypting function, result and pos is here so we dont have to merge buffers later
  84. return function (/*Buffer*/ data, /*Buffer*/ result, /* Number */ pos = 0) {
  85. // result - we create new Buffer for results
  86. if (!result) result = Buffer.alloc(data.length);
  87. // process input data
  88. for (let c of data) {
  89. const k = keys.next(); // save key byte
  90. result[pos++] = c ^ k; // save val
  91. keys.updateKeys(c); // update keys with decoded byte
  92. }
  93. return result;
  94. };
  95. }
  96. function decrypt(/*Buffer*/ data, /*Object*/ header, /*String, Buffer*/ pwd) {
  97. if (!data || !Buffer.isBuffer(data) || data.length < 12) {
  98. return Buffer.alloc(0);
  99. }
  100. // 1. We Initialize and generate decrypting function
  101. const decrypter = make_decrypter(pwd);
  102. // 2. decrypt salt what is always 12 bytes and is a part of file content
  103. const salt = decrypter(data.slice(0, 12));
  104. // if bit 3 (0x08) of the general-purpose flags field is set, check salt[11] with the high byte of the header time
  105. // 2 byte data block (as per Info-Zip spec), otherwise check with the high byte of the header entry
  106. const verifyByte = (header.flags & 0x8) === 0x8 ? header.timeHighByte : header.crc >>> 24;
  107. //3. does password meet expectations
  108. if (salt[11] !== verifyByte) {
  109. throw Errors.WRONG_PASSWORD();
  110. }
  111. // 4. decode content
  112. return decrypter(data.slice(12));
  113. }
  114. // lets add way to populate salt, NOT RECOMMENDED for production but maybe useful for testing general functionality
  115. function _salter(data) {
  116. if (Buffer.isBuffer(data) && data.length >= 12) {
  117. // be aware - currently salting buffer data is modified
  118. config.genSalt = function () {
  119. return data.slice(0, 12);
  120. };
  121. } else if (data === "node") {
  122. // test salt generation with node random function
  123. config.genSalt = genSalt.node;
  124. } else {
  125. // if value is not acceptable config gets reset.
  126. config.genSalt = genSalt;
  127. }
  128. }
  129. function encrypt(/*Buffer*/ data, /*Object*/ header, /*String, Buffer*/ pwd, /*Boolean*/ oldlike = false) {
  130. // 1. test data if data is not Buffer we make buffer from it
  131. if (data == null) data = Buffer.alloc(0);
  132. // if data is not buffer be make buffer from it
  133. if (!Buffer.isBuffer(data)) data = Buffer.from(data.toString());
  134. // 2. We Initialize and generate encrypting function
  135. const encrypter = make_encrypter(pwd);
  136. // 3. generate salt (12-bytes of random data)
  137. const salt = config.genSalt();
  138. salt[11] = (header.crc >>> 24) & 0xff;
  139. // old implementations (before PKZip 2.04g) used two byte check
  140. if (oldlike) salt[10] = (header.crc >>> 16) & 0xff;
  141. // 4. create output
  142. const result = Buffer.alloc(data.length + 12);
  143. encrypter(salt, result);
  144. // finally encode content
  145. return encrypter(data, result, 12);
  146. }
  147. module.exports = { decrypt, encrypt, _salter };