Twofish encryption: how it works and when to use it
How the Twofish cipher works, why it lost the AES competition without being broken, what cryptanalysis has found since, and when AES is the better choice.
By MD-5 Updated 5 min read
Twofish is in an unusual position: one of the most heavily analyzed block ciphers ever published, never broken, and almost nobody’s default. This guide covers how it works, what the research says, where it earns its place, and why AES is still the right choice for most systems.
Where Twofish came from
Twofish was designed in 1998 by Bruce Schneier, John Kelsey, Doug Whiting, David Wagner, Chris Hall and Niels Ferguson as a submission to NIST’s AES competition. It succeeded Schneier’s earlier Blowfish, fixing Blowfish’s main limit, the 64-bit block.
It reached the final five alongside MARS, RC6, Rijndael and Serpent. NIST’s report on the selection (opens in a new tab) (Nechvatal et al., published 2001) judged all five finalists to have adequate security, and rated Twofish’s security margin as high. Rijndael won on its combination of security, performance across platforms from smart cards to servers, and simplicity. Twofish’s complex key schedule and key-dependent tables gave it a large security margin but made it harder to implement efficiently everywhere.
Twofish is unpatented and in the public domain, with free reference implementations.
How it works
- Block size: 128 bits.
- Key size: 128, 192 or 256 bits.
- Structure: a 16-round Feistel network.
- Key-dependent S-boxes: four 8-by-8-bit S-boxes built from the key, rather than fixed tables published in the specification.
- MDS matrix and pseudo-Hadamard transform: mixing steps that spread each input byte’s influence across the block.
- Whitening: the block is XORed with key material before the first round and after the last.
The key-dependent S-boxes are Twofish’s most distinctive feature. In most ciphers, including AES, an analyst knows exactly which substitution each byte goes through. In Twofish, the substitution depends on a key the attacker doesn’t have, which is a large part of why its security margin is considered generous.
Because it’s a Feistel network, encryption and decryption use the same structure with subkeys in reverse order, which keeps code small. The cost is key setup: building the S-boxes takes real work before any data is encrypted. The designers specified several keying options that trade setup time against throughput, depending on whether a key protects gigabytes or a single message.
What the cryptanalysis found
Twofish has been public since 1998 and was attacked intensively during the AES process by the teams behind competing ciphers. Results so far:
- Impossible differential attack (Ferguson, 1999): reaches 6 of 16 rounds, at a cost comparable to brute force for 256-bit keys.
- Truncated differentials (Moriai and Yin, 2000): identified statistical properties of the full cipher, but not an attack that recovers keys or distinguishes it in practice. The designers have noted it doesn’t amount to a break.
No attack threatens full 16-round Twofish. For a cipher more than 25 years old, a flat record like that is the strongest endorsement available.
Blowfish vs Twofish
Both came from Schneier and both are free to use. The meaningful difference is the block: Blowfish’s 64-bit block limits how much data one key can safely protect (the Sweet32 attack (opens in a new tab) demonstrated this against Blowfish in OpenVPN in 2016), while Twofish’s 128-bit block removes that limit. Key length isn’t the difference: Blowfish accepts up to 448 bits, Twofish up to 256, and both are far beyond brute force at sensible lengths.
Where Twofish is used
Twofish lives where one piece of software controls both encryption and decryption, so no algorithm has to be negotiated with a third party. It is largely absent from internet protocols, which standardized on AES.
- Disk encryption: VeraCrypt (opens in a new tab) offers Twofish alone and in cascades such as AES-Twofish and AES-Twofish-Serpent, where data passes through independent ciphers with separate keys. The reasoning is insurance: a break in one algorithm still leaves the others.
- Password managers: KeePassXC offers Twofish as an alternative to AES for database encryption.
- OpenPGP: Twofish is one of the symmetric algorithms defined in the OpenPGP standard (RFC 4880 (opens in a new tab), carried forward in RFC 9580 (opens in a new tab)), so GnuPG can use it when both sides support it.
- Embedded systems: its structure keeps code small and it runs acceptably without a crypto coprocessor.
Why AES is usually the better choice
An unbroken cipher can still be the wrong choice. The case against Twofish has nothing to do with its security:
- No hardware acceleration. Since 2010, x86 processors have AES-NI instructions and 64-bit ARM cores have equivalent extensions. AES runs in silicon; Twofish runs in software, several times slower on the same machine.
- Not approved for regulated use. Twofish is not a NIST-approved algorithm, so it can’t be used in a FIPS 140 validated module. For US federal data, and for anything assessed against standards that list approved algorithms, it’s off the table.
- Thinner library support. Most platform crypto APIs offer AES and little else. A third-party library means more dependencies and less assurance of side-channel hardening.
- No standard authenticated mode. Libraries rarely ship Twofish with an authenticated mode like GCM, so you end up assembling encryption and a MAC yourself, which is where bugs come from.
If you can’t say why you’re not using AES, use AES.
Using Twofish safely
If you do have a reason to use it:
- Use 256-bit keys generated by a cryptographically secure random generator.
- Use a maintained library and check it against the published test vectors. Never write your own implementation.
- Never use ECB mode. Use CTR or CBC with a unique IV per message, and never reuse an IV with the same key.
- Authenticate the ciphertext with a MAC (encrypt-then-MAC) and verify before decrypting.
- Store keys in an HSM or key-management service, never in code or config files.
In practice it’s these mistakes, not the cipher, that testing finds: a key committed to a repository, a reused IV, ciphertext nobody authenticates. A web application penetration test checks how your application actually handles encryption.
Quantum computing
Symmetric ciphers are far less exposed to quantum computers than RSA or elliptic-curve cryptography. Grover’s algorithm would, at best, halve effective key strength, so Twofish with a 256-bit key is in the same position as AES-256: a comfortable margin. See symmetric vs asymmetric encryption for why the two families differ.
Sources
- B. Schneier, J. Kelsey, D. Whiting, D. Wagner, C. Hall, N. Ferguson, Twofish: A 128-bit block cipher (opens in a new tab), 1998.
- J. Nechvatal et al., Report on the development of the Advanced Encryption Standard (AES) (opens in a new tab), Journal of Research of NIST, 2001.
- N. Ferguson, Impossible differentials in Twofish, Twofish Technical Report #5, 1999.
- S. Moriai, Y. L. Yin, Cryptanalysis of Twofish (II), IEICE, 2000.
- IETF, RFC 4880: OpenPGP Message Format (opens in a new tab) and RFC 9580: OpenPGP (opens in a new tab).
- IDRIX, VeraCrypt encryption algorithms (opens in a new tab).
Published by
MD-5 · Certified cybersecurity services
Articles are researched and written in-house and link to their primary sources (standards, advisories, papers and public datasets) so you can check every claim. Spotted an error? Email contact@md-5.com and we’ll correct it.