mode-ofb.js 756 B

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * Output Feedback block mode.
  3. */
  4. import {
  5. BlockCipherMode,
  6. } from './cipher-core.js';
  7. export class OFB extends BlockCipherMode {
  8. }
  9. OFB.Encryptor = class extends OFB {
  10. processBlock(words, offset) {
  11. const _words = words;
  12. // Shortcuts
  13. const cipher = this._cipher;
  14. const { blockSize } = cipher;
  15. const iv = this._iv;
  16. let keystream = this._keystream;
  17. // Generate keystream
  18. if (iv) {
  19. this._keystream = iv.slice(0);
  20. keystream = this._keystream;
  21. // Remove IV for subsequent blocks
  22. this._iv = undefined;
  23. }
  24. cipher.encryptBlock(keystream, 0);
  25. // Encrypt
  26. for (let i = 0; i < blockSize; i += 1) {
  27. _words[offset + i] ^= keystream[i];
  28. }
  29. }
  30. };
  31. OFB.Decryptor = OFB.Encryptor;