mode-ctr.js 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. // Counter Mode
  3. var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  4. if (kind === "m") throw new TypeError("Private method is not writable");
  5. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
  6. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
  7. return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
  8. };
  9. var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  10. if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
  11. if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
  12. return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
  13. };
  14. var _CTR_remaining, _CTR_remainingIndex, _CTR_counter;
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.CTR = void 0;
  17. const mode_js_1 = require("./mode.js");
  18. class CTR extends mode_js_1.ModeOfOperation {
  19. constructor(key, initialValue) {
  20. super("CTR", key, CTR);
  21. // Remaining bytes for the one-time pad
  22. _CTR_remaining.set(this, void 0);
  23. _CTR_remainingIndex.set(this, void 0);
  24. // The current counter
  25. _CTR_counter.set(this, void 0);
  26. __classPrivateFieldSet(this, _CTR_counter, new Uint8Array(16), "f");
  27. __classPrivateFieldGet(this, _CTR_counter, "f").fill(0);
  28. __classPrivateFieldSet(this, _CTR_remaining, __classPrivateFieldGet(this, _CTR_counter, "f"), "f"); // This will be discarded immediately
  29. __classPrivateFieldSet(this, _CTR_remainingIndex, 16, "f");
  30. if (initialValue == null) {
  31. initialValue = 1;
  32. }
  33. if (typeof (initialValue) === "number") {
  34. this.setCounterValue(initialValue);
  35. }
  36. else {
  37. this.setCounterBytes(initialValue);
  38. }
  39. }
  40. get counter() { return new Uint8Array(__classPrivateFieldGet(this, _CTR_counter, "f")); }
  41. setCounterValue(value) {
  42. if (!Number.isInteger(value) || value < 0 || value > Number.MAX_SAFE_INTEGER) {
  43. throw new TypeError("invalid counter initial integer value");
  44. }
  45. for (let index = 15; index >= 0; --index) {
  46. __classPrivateFieldGet(this, _CTR_counter, "f")[index] = value % 256;
  47. value = Math.floor(value / 256);
  48. }
  49. }
  50. setCounterBytes(value) {
  51. if (value.length !== 16) {
  52. throw new TypeError("invalid counter initial Uint8Array value length");
  53. }
  54. __classPrivateFieldGet(this, _CTR_counter, "f").set(value);
  55. }
  56. increment() {
  57. for (let i = 15; i >= 0; i--) {
  58. if (__classPrivateFieldGet(this, _CTR_counter, "f")[i] === 255) {
  59. __classPrivateFieldGet(this, _CTR_counter, "f")[i] = 0;
  60. }
  61. else {
  62. __classPrivateFieldGet(this, _CTR_counter, "f")[i]++;
  63. break;
  64. }
  65. }
  66. }
  67. encrypt(plaintext) {
  68. var _a, _b;
  69. const crypttext = new Uint8Array(plaintext);
  70. for (let i = 0; i < crypttext.length; i++) {
  71. if (__classPrivateFieldGet(this, _CTR_remainingIndex, "f") === 16) {
  72. __classPrivateFieldSet(this, _CTR_remaining, this.aes.encrypt(__classPrivateFieldGet(this, _CTR_counter, "f")), "f");
  73. __classPrivateFieldSet(this, _CTR_remainingIndex, 0, "f");
  74. this.increment();
  75. }
  76. crypttext[i] ^= __classPrivateFieldGet(this, _CTR_remaining, "f")[__classPrivateFieldSet(this, _CTR_remainingIndex, (_b = __classPrivateFieldGet(this, _CTR_remainingIndex, "f"), _a = _b++, _b), "f"), _a];
  77. }
  78. return crypttext;
  79. }
  80. decrypt(ciphertext) {
  81. return this.encrypt(ciphertext);
  82. }
  83. }
  84. exports.CTR = CTR;
  85. _CTR_remaining = new WeakMap(), _CTR_remainingIndex = new WeakMap(), _CTR_counter = new WeakMap();
  86. //# sourceMappingURL=mode-ctr.js.map