mode-cfb.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";
  2. // Cipher Feedback
  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 _CFB_instances, _CFB_iv, _CFB_shiftRegister, _CFB_shift;
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.CFB = void 0;
  17. const mode_js_1 = require("./mode.js");
  18. class CFB extends mode_js_1.ModeOfOperation {
  19. constructor(key, iv, segmentSize = 8) {
  20. super("CFB", key, CFB);
  21. _CFB_instances.add(this);
  22. _CFB_iv.set(this, void 0);
  23. _CFB_shiftRegister.set(this, void 0);
  24. // This library currently only handles byte-aligned segmentSize
  25. if (!Number.isInteger(segmentSize) || (segmentSize % 8)) {
  26. throw new TypeError("invalid segmentSize");
  27. }
  28. Object.defineProperties(this, {
  29. segmentSize: { enumerable: true, value: segmentSize }
  30. });
  31. if (iv) {
  32. if (iv.length % 16) {
  33. throw new TypeError("invalid iv size (must be 16 bytes)");
  34. }
  35. __classPrivateFieldSet(this, _CFB_iv, new Uint8Array(iv), "f");
  36. }
  37. else {
  38. __classPrivateFieldSet(this, _CFB_iv, new Uint8Array(16), "f");
  39. }
  40. __classPrivateFieldSet(this, _CFB_shiftRegister, this.iv, "f");
  41. }
  42. get iv() { return new Uint8Array(__classPrivateFieldGet(this, _CFB_iv, "f")); }
  43. encrypt(plaintext) {
  44. if (8 * plaintext.length % this.segmentSize) {
  45. throw new TypeError("invalid plaintext size (must be multiple of segmentSize bytes)");
  46. }
  47. const segmentSize = this.segmentSize / 8;
  48. const ciphertext = new Uint8Array(plaintext);
  49. for (let i = 0; i < ciphertext.length; i += segmentSize) {
  50. const xorSegment = this.aes.encrypt(__classPrivateFieldGet(this, _CFB_shiftRegister, "f"));
  51. for (let j = 0; j < segmentSize; j++) {
  52. ciphertext[i + j] ^= xorSegment[j];
  53. }
  54. __classPrivateFieldGet(this, _CFB_instances, "m", _CFB_shift).call(this, ciphertext.subarray(i));
  55. }
  56. return ciphertext;
  57. }
  58. decrypt(ciphertext) {
  59. if (8 * ciphertext.length % this.segmentSize) {
  60. throw new TypeError("invalid ciphertext size (must be multiple of segmentSize bytes)");
  61. }
  62. const segmentSize = this.segmentSize / 8;
  63. const plaintext = new Uint8Array(ciphertext);
  64. for (let i = 0; i < plaintext.length; i += segmentSize) {
  65. const xorSegment = this.aes.encrypt(__classPrivateFieldGet(this, _CFB_shiftRegister, "f"));
  66. for (let j = 0; j < segmentSize; j++) {
  67. plaintext[i + j] ^= xorSegment[j];
  68. }
  69. __classPrivateFieldGet(this, _CFB_instances, "m", _CFB_shift).call(this, ciphertext.subarray(i));
  70. }
  71. return plaintext;
  72. }
  73. }
  74. exports.CFB = CFB;
  75. _CFB_iv = new WeakMap(), _CFB_shiftRegister = new WeakMap(), _CFB_instances = new WeakSet(), _CFB_shift = function _CFB_shift(data) {
  76. const segmentSize = this.segmentSize / 8;
  77. // Shift the register
  78. __classPrivateFieldGet(this, _CFB_shiftRegister, "f").set(__classPrivateFieldGet(this, _CFB_shiftRegister, "f").subarray(segmentSize));
  79. __classPrivateFieldGet(this, _CFB_shiftRegister, "f").set(data.subarray(0, segmentSize), 16 - segmentSize);
  80. };
  81. //# sourceMappingURL=mode-cfb.js.map