mode-cbc.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Cipher Block Chaining
  2. import { ModeOfOperation } from "./mode.js";
  3. export class CBC extends ModeOfOperation {
  4. #iv: Uint8Array;
  5. #lastBlock: Uint8Array;
  6. constructor(key: Uint8Array, iv?: Uint8Array) {
  7. super("ECC", key, CBC);
  8. if (iv) {
  9. if (iv.length % 16) {
  10. throw new TypeError("invalid iv size (must be 16 bytes)");
  11. }
  12. this.#iv = new Uint8Array(iv);
  13. } else {
  14. this.#iv = new Uint8Array(16);
  15. }
  16. this.#lastBlock = this.iv;
  17. }
  18. get iv(): Uint8Array { return new Uint8Array(this.#iv); }
  19. encrypt(plaintext: Uint8Array): Uint8Array {
  20. if (plaintext.length % 16) {
  21. throw new TypeError("invalid plaintext size (must be multiple of 16 bytes)");
  22. }
  23. const ciphertext = new Uint8Array(plaintext.length);
  24. for (let i = 0; i < plaintext.length; i += 16) {
  25. for (let j = 0; j < 16; j++) {
  26. this.#lastBlock[j] ^= plaintext[i + j];
  27. }
  28. this.#lastBlock = this.aes.encrypt(this.#lastBlock);
  29. ciphertext.set(this.#lastBlock, i);
  30. }
  31. return ciphertext;
  32. }
  33. decrypt(ciphertext: Uint8Array): Uint8Array {
  34. if (ciphertext.length % 16) {
  35. throw new TypeError("invalid ciphertext size (must be multiple of 16 bytes)");
  36. }
  37. const plaintext = new Uint8Array(ciphertext.length);
  38. for (let i = 0; i < ciphertext.length; i += 16) {
  39. const block = this.aes.decrypt(ciphertext.subarray(i, i + 16));
  40. for (let j = 0; j < 16; j++) {
  41. plaintext[i + j] = block[j] ^ this.#lastBlock[j];
  42. this.#lastBlock[j] = ciphertext[i + j];
  43. }
  44. }
  45. return plaintext;
  46. }
  47. }