mode-ctr.js 4.1 KB

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