tests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import assert from "assert";
  2. import fs from "fs";
  3. import { join, resolve } from "path";
  4. import * as aes from "./index.js";
  5. const root = (function () {
  6. let root = process.cwd();
  7. while (true) {
  8. if (fs.existsSync(join(root, "package.json"))) {
  9. return root;
  10. }
  11. const parent = join(root, "..");
  12. if (parent === root) {
  13. break;
  14. }
  15. root = parent;
  16. }
  17. throw new Error("could not find root");
  18. })();
  19. describe("Tests Encrypting and Decrypting", function () {
  20. const json = fs.readFileSync(resolve(root, "./test/test-vectors.json")).toString();
  21. const tests = JSON.parse(json);
  22. function getCrypter(key, test) {
  23. switch (test.modeOfOperation) {
  24. case "ctr":
  25. return new aes.CTR(key, 0);
  26. case "cbc":
  27. return new aes.CBC(key, Buffer.from(test.iv));
  28. case "cfb":
  29. return new aes.CFB(key, Buffer.from(test.iv), test.segmentSize * 8);
  30. case "ecb":
  31. return new aes.ECB(key);
  32. case "ofb":
  33. return new aes.OFB(key, Buffer.from(test.iv));
  34. }
  35. return null;
  36. }
  37. tests.forEach((test, index) => {
  38. it(`tests encrypting: ${test.modeOfOperation}.${index}`, function () {
  39. const encrypter = getCrypter(Buffer.from(test.key), test);
  40. if (!encrypter) {
  41. this.skip();
  42. }
  43. for (let i = 0; i < test.plaintext.length; i++) {
  44. const plaintext = Buffer.from(test.plaintext[i]);
  45. const ciphertext = Buffer.from(test.encrypted[i]);
  46. const result = Buffer.from(encrypter.encrypt(plaintext));
  47. assert.ok(ciphertext.equals(result), "encrypting failed");
  48. }
  49. });
  50. it(`tests decrypting: ${test.modeOfOperation}.${index}`, function () {
  51. const decrypter = getCrypter(Buffer.from(test.key), test);
  52. if (!decrypter) {
  53. this.skip();
  54. }
  55. for (let i = 0; i < test.plaintext.length; i++) {
  56. const plaintext = Buffer.from(test.plaintext[i]);
  57. const ciphertext = Buffer.from(test.encrypted[i]);
  58. const result = Buffer.from(decrypter.decrypt(ciphertext));
  59. assert.ok(plaintext.equals(result), "decrypting failed");
  60. }
  61. });
  62. });
  63. });
  64. describe("Tests Padding", function () {
  65. for (let size = 0; size < 100; size++) {
  66. it(`tests padding: length=${size}`, function () {
  67. // Create a random piece of data
  68. const data = new Uint8Array(size);
  69. data.fill(42);
  70. // Pad it
  71. const padded = aes.pkcs7Pad(data);
  72. assert.ok((padded.length % 16) === 0, "Failed to pad to block size");
  73. assert.ok(data.length <= padded.length && padded.length <= data.length + 16, "Padding went awry");
  74. assert.ok(padded[padded.length - 1] >= 1 && padded[padded.length - 1] <= 16, "Failed to pad to block size");
  75. // Trim it
  76. const trimmed = aes.pkcs7Strip(padded);
  77. assert.ok(Buffer.from(data).equals(trimmed), "Failed to trim to original data");
  78. });
  79. }
  80. });
  81. //# sourceMappingURL=tests.js.map