liveBook · Manning

State

As explained in Hacking Cryptography

The internal state of an LFSR is the mutable sequence of register bits that determines its subsequent output and evolution.

State is the mutable data a deterministic system carries from one step to the next. A PRNG initializes state from a seed, then repeatedly transforms it; the resulting state determines later outputs and when the sequence eventually repeats. Keeping state distinct from output is a core security boundary: when output reveals state, an observer can reproduce future behavior. section 2.2.2 section 2.2.3

For a linear-feedback shift register (LFSR), state is the ordered sequence of register bits. The register also stores tap positions, which select the state bits XORed to form the next shift-in bit. Each generated bit is the old rightmost state bit; the remaining bits shift right and the computed feedback bit enters on the left. Thus, one state transition both emits a bit and replaces the current state. section 4.2.1

Listing 4.2 impl_lfsr.go
func (lfsr *LFSR) GenerateBit() byte {
  outputBit := lfsr.state[lfsr.length-1]

  newShiftBit := byte(0x00)
  for i := 0; i < lfsr.length; i++ {
    if lfsr.taps[i] == 1 {
      newShiftBit = newShiftBit ^ lfsr.state[i]   #1
    }
  }

  for i := lfsr.length - 1; i > 0; i-- {
    lfsr.state[i] = lfsr.state[i-1]  #1
  }

  lfsr.state[0] = newShiftBit

  return outputBit
}

Trace the transition in order: save the rightmost bit, XOR tapped state bits, shift the register, and insert the feedback bit. section 4.2.1

An LFSR can generate a keystream for XOR encryption, making its initial state the encryption key in this construction. The implementation first generates and discards as many bits as the register length so that plaintext is not XORed with the initial key bits directly. It then assembles eight generated bits into each keystream byte. section 4.2.1

Recovering a stream-cipher state

Known plaintext and its ciphertext reveal the corresponding keystream through XOR. For an LFSR, linear algebra can recover the internal state from that keystream; an attacker can then clone the register and generate later keystream bits. Discarding initial output does not address recovery from sufficient later keystream. section 10.1.2 section 4.2.1

The same distinction between state and output explains failures in other generators. An LCG outputs its state directly, allowing observed values to reveal its configuration and support cloning. MT19937 transforms each state word before output, yet observing and reversing 624 transformed outputs recovers its full 624-word state and permits a clone to match future values. section 2.2.2 section 3.1.2

State exposure can also create attacks outside random-number generation. A Merkle–Damgård digest exposes the hash function's internal state, allowing an attacker to resume hashing from a known digest and append data in a length-extension attack. Sponge constructions instead absorb input into part of their state and squeeze output while repeatedly permuting that state. section 7.3.2 section 6.3.2

Treat mutable cryptographic state as security-sensitive data:

  • Do not treat a generator as cryptographically secure merely because its outputs look random; exposed or reversible state can enable prediction and cloning. section 2.2.3 section 10.1.1
  • Avoid LFSRs as standalone stream-cipher generators when an attacker may obtain keystream, because recovered state yields subsequent keystream bits. section 10.1.2
  • Avoid secret-prefix MACs built from Merkle–Damgård hashes when a digest lets an attacker resume the internal state. section 7.3.2

How Hacking Cryptography covers it

How deep it goes
Explains LFSR state transitions in executable detail and connects state recovery to concrete cloning attacks.
Examples
Uses LCG and MT19937 cloning, LFSR keystream encryption, and secret-prefix MAC length extension.
What it assumes
Introduces LFSR state procedurally through bits, taps, shifts, and XOR-based keystream generation.
What you can do after
Evaluate/understand how observable or recoverable state enables prediction, cloning, and continuation attacks.
How it connects
Connects state to seeds, PRNGs, CSPRNGs, LFSRs, keystreams, XOR encryption, hash functions, and length-extension attacks.

Also explained in

Compare how 39 books explain state →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hacking Cryptography. The section links point to the parts of the book it draws on.