tests.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. var __importDefault = (this && this.__importDefault) || function (mod) {
  26. return (mod && mod.__esModule) ? mod : { "default": mod };
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. const assert_1 = __importDefault(require("assert"));
  30. const fs_1 = __importDefault(require("fs"));
  31. const path_1 = require("path");
  32. const aes = __importStar(require("./index.js"));
  33. const root = (function () {
  34. let root = process.cwd();
  35. while (true) {
  36. if (fs_1.default.existsSync((0, path_1.join)(root, "package.json"))) {
  37. return root;
  38. }
  39. const parent = (0, path_1.join)(root, "..");
  40. if (parent === root) {
  41. break;
  42. }
  43. root = parent;
  44. }
  45. throw new Error("could not find root");
  46. })();
  47. describe("Tests Encrypting and Decrypting", function () {
  48. const json = fs_1.default.readFileSync((0, path_1.resolve)(root, "./test/test-vectors.json")).toString();
  49. const tests = JSON.parse(json);
  50. function getCrypter(key, test) {
  51. switch (test.modeOfOperation) {
  52. case "ctr":
  53. return new aes.CTR(key, 0);
  54. case "cbc":
  55. return new aes.CBC(key, Buffer.from(test.iv));
  56. case "cfb":
  57. return new aes.CFB(key, Buffer.from(test.iv), test.segmentSize * 8);
  58. case "ecb":
  59. return new aes.ECB(key);
  60. case "ofb":
  61. return new aes.OFB(key, Buffer.from(test.iv));
  62. }
  63. return null;
  64. }
  65. tests.forEach((test, index) => {
  66. it(`tests encrypting: ${test.modeOfOperation}.${index}`, function () {
  67. const encrypter = getCrypter(Buffer.from(test.key), test);
  68. if (!encrypter) {
  69. this.skip();
  70. }
  71. for (let i = 0; i < test.plaintext.length; i++) {
  72. const plaintext = Buffer.from(test.plaintext[i]);
  73. const ciphertext = Buffer.from(test.encrypted[i]);
  74. const result = Buffer.from(encrypter.encrypt(plaintext));
  75. assert_1.default.ok(ciphertext.equals(result), "encrypting failed");
  76. }
  77. });
  78. it(`tests decrypting: ${test.modeOfOperation}.${index}`, function () {
  79. const decrypter = getCrypter(Buffer.from(test.key), test);
  80. if (!decrypter) {
  81. this.skip();
  82. }
  83. for (let i = 0; i < test.plaintext.length; i++) {
  84. const plaintext = Buffer.from(test.plaintext[i]);
  85. const ciphertext = Buffer.from(test.encrypted[i]);
  86. const result = Buffer.from(decrypter.decrypt(ciphertext));
  87. assert_1.default.ok(plaintext.equals(result), "decrypting failed");
  88. }
  89. });
  90. });
  91. });
  92. describe("Tests Padding", function () {
  93. for (let size = 0; size < 100; size++) {
  94. it(`tests padding: length=${size}`, function () {
  95. // Create a random piece of data
  96. const data = new Uint8Array(size);
  97. data.fill(42);
  98. // Pad it
  99. const padded = aes.pkcs7Pad(data);
  100. assert_1.default.ok((padded.length % 16) === 0, "Failed to pad to block size");
  101. assert_1.default.ok(data.length <= padded.length && padded.length <= data.length + 16, "Padding went awry");
  102. assert_1.default.ok(padded[padded.length - 1] >= 1 && padded[padded.length - 1] <= 16, "Failed to pad to block size");
  103. // Trim it
  104. const trimmed = aes.pkcs7Strip(padded);
  105. assert_1.default.ok(Buffer.from(data).equals(trimmed), "Failed to trim to original data");
  106. });
  107. }
  108. });
  109. //# sourceMappingURL=tests.js.map