Blowfish (cipher)
General  

Designers  Bruce Schneier 
First published  1993 
Successors  Twofish 
Cipher detail  
Key sizes  32–448 bits 
Block sizes  64 bits 
Structure  Feistel network 
Rounds  16 
Best public cryptanalysis  
Four rounds of Blowfish are susceptible to a secondorder differential attack (Rijmen, 1997);^{[1]} for a class of weak keys, 14 rounds of Blowfish can be distinguished from a pseudorandom permutation (Vaudenay, 1996). 
Blowfish is a symmetrickey block cipher, designed in 1993 by Bruce Schneier and included in a large number of cipher suites and encryption products. Blowfish provides a good encryption rate in software and no effective cryptanalysis of it has been found to date. However, the Advanced Encryption Standard (AES) now receives more attention, and Schneier recommends Twofish for modern applications.^{[2]}
Schneier designed Blowfish as a generalpurpose algorithm, intended as an alternative to the aging DES and free of the problems and constraints associated with other algorithms. At the time Blowfish was released, many other designs were proprietary, encumbered by patents or were commercial or government secrets. Schneier has stated that, "Blowfish is unpatented, and will remain so in all countries. The algorithm is hereby placed in the public domain, and can be freely used by anyone."^{[3]}
Notable features of the design include keydependent Sboxes and a highly complex key schedule.
Contents
The algorithm
Blowfish has a 64bit block size and a variable key length from 32 bits up to 448 bits.^{[3]} It is a 16round Feistel cipher and uses large keydependent Sboxes. In structure it resembles CAST128, which uses fixed Sboxes.
The diagram to the left shows Blowfish's encryption routine. Each line represents 32 bits. There are five subkeyarrays: one 18entry Parray (denoted as K in the diagram, to avoid confusion with the Plaintext) and four 256entry Sboxes (S0, S1, S2 and S3).
Every round r consists of 4 actions: First, XOR the left half (L) of the data with the r th Parray entry, second, use the XORed data as input for Blowfish's Ffunction, third, XOR the Ffunction's output with the right half (R) of the data, and last, swap L and R.
The Ffunction splits the 32bit input into four eightbit quarters, and uses the quarters as input to the Sboxes. The Sboxes accept 8bit input and produce 32bit output. The outputs are added modulo 2^{32} and XORed to produce the final 32bit output (see image in the upper right corner).^{[4]}
After the 16th round, undo the last swap, and XOR L with K18 and R with K17 (output whitening).
Decryption is exactly the same as encryption, except that P1, P2, …, P18 are used in the reverse order. This is not so obvious because xor is commutative and associative. A common misconception is to use inverse order of encryption as decryption algorithm (i.e. first XORing P17 and P18 to the ciphertext block, then using the Pentries in reverse order).
Blowfish's key schedule starts by initializing the Parray and Sboxes with values derived from the hexadecimal digits of pi, which contain no obvious pattern (see nothing up my sleeve number). The secret key is then, byte by byte, cycling the key if necessary, XORed with all the Pentries in order. A 64bit allzero block is then encrypted with the algorithm as it stands. The resultant ciphertext replaces P_{1} and P_{2}. The same ciphertext is then encrypted again with the new subkeys, and the new ciphertext replaces P_{3} and P_{4}. This continues, replacing the entire Parray and all the Sbox entries. In all, the Blowfish encryption algorithm will run 521 times to generate all the subkeys  about 4KB of data is processed.
Because the Parray is 576 bits long, and the key bytes are XORed through all these 576 bits during the initialization, many implementations support key sizes up to 576 bits. While this is certainly possible, the 448 bits limit is here to ensure that every bit of every subkey depends on every bit of the key,^{[3]} as the last four values of the Parray don't affect every bit of the ciphertext. This point should be taken in consideration for implementations with a different number of rounds, as even though it increases security against an exhaustive attack, it weakens the security guaranteed by the algorithm. And given the slow initialization of the cipher with each change of key, it is granted a natural protection against bruteforce attacks, which doesn't really justify key sizes longer than 448 bits.
Blowfish in pseudocode
uint32_t P[18];
uint32_t S[4][256];
uint32_t f (uint32_t x) {
uint32_t h = S[0][x >> 24] + S[1][x >> 16 & 0xff];
return ( h ^ S[2][x >> 8 & 0xff] ) + S[3][x & 0xff];
}
void encrypt (uint32_t & L, uint32_t & R) {
for (int i=0 ; i<16 ; i += 2) {
L ^= P[i];
R ^= f(L);
R ^= P[i+1];
L ^= f(R);
}
L ^= P[16];
R ^= P[17];
swap (L, R);
}
void decrypt (uint32_t & L, uint32_t & R) {
for (int i=16 ; i > 0 ; i = 2) {
L ^= P[i+1];
R ^= f(L);
R ^= P[i];
L ^= f(R);
}
L ^= P[1];
R ^= P[0];
swap (L, R);
}
{
// ...
// initializing the Parray and Sboxes with values derived from pi; omitted in the example
// ...
for (int i=0 ; i<18 ; ++i)
P[i] ^= key[i % keylen];
uint32_t L = 0, R = 0;
for (int i=0 ; i<18 ; i+=2) {
encrypt (L, R);
P[i] = L; P[i+1] = R;
}
for (int i=0 ; i<4 ; ++i)
for (int j=0 ; j<256; j+=2) {
encrypt (L, R);
S[i][j] = L; S[i][j+1] = R;
}
}
Blowfish in practice
Blowfish is a fast block cipher, except when changing keys. Each new key requires preprocessing equivalent to encrypting about 4 kilobytes of text, which is very slow compared to other block ciphers. This prevents its use in certain applications, but is not a problem in others.
In one application Blowfish's slow key changing is actually a benefit: the passwordhashing method used in OpenBSD uses an algorithm derived from Blowfish that makes use of the slow key schedule; the idea is that the extra computational effort required gives protection against dictionary attacks. See key stretching.
Blowfish has a memory footprint of just over 4 kilobytes of RAM. This constraint is not a problem even for older desktop and laptop computers, though it does prevent use in the smallest embedded systems such as early smartcards.
Blowfish was one of the first secure block ciphers not subject to any patents and therefore freely available for anyone to use. This benefit has contributed to its popularity in cryptographic software.
bcrypt is a password hashing function which, combined with a variable number of iterations (work "cost"), exploits the expensive key setup phase of Blowfish to increase the workload and duration of hash calculations, further reducing threats from brute force attacks.
bcrypt is also the name of a crossplatform file encryption utility implementing Blowfish developed in 2002.^{[5]}^{[6]}^{[7]}^{[8]}
Weakness and successors
Blowfish's use of a 64bit block size (as opposed to e.g. AES's 128bit block size) makes it vulnerable to birthday attacks, particularly in contexts like HTTPS. In 2016, the SWEET32 attack demonstrated how to leverage birthday attacks to perform plaintext recovery (i.e. decrypting ciphertext) against ciphers with a 64bit block size such as Blowfish.^{[9]}
A reducedround variant of Blowfish is known to be susceptible to knownplaintext attacks on reflectively weak keys. Blowfish implementations use 16 rounds of encryption, and are not susceptible to this attack.^{[10]}^{[11]} Blowfish users are encouraged by Bruce Schneier, Blowfish's creator, to use the more modern and computationally efficient alternative Twofish. He is quoted in 2007 as saying:
At this point, though, I'm amazed it's still being used. If people ask, I recommend Twofish instead.^{[2]}
— Bruce Schneier, Blowfish's creator, in 2007
The FAQ for GnuPG (which features Blowfish as one of its algorithms) recommends that Blowfish should not be used to encrypt files that are larger than 4 Gb because of its small 64bit block size.^{[12]}
See also
References
 ^ Vincent Rijmen (1997). "Cryptanalysis and Design of Iterated Block Ciphers" (PostScript). Ph.D thesis.

^ ^{a} ^{b} Dahna, McConnachie (20071227). "Bruce Almighty: Schneier preaches security to Linux faithful". Computerworld. p. 3. Archived from the original on 20120603.
At this point, though, I'm amazed it's still being used. If people ask, I recommend Twofish instead.
 ^ ^{a} ^{b} ^{c} Bruce Schneier (1993). "Description of a New VariableLength Key, 64Bit Block Cipher (Blowfish)". Fast Software Encryption, Cambridge Security Workshop Proceedings. SpringerVerlag: 191–204.
 ^ "Cryptography: Description of a New VariableLength Key, 64Bit Block Cipher (Blowfish)  Schneier on Security". www.schneier.com. Retrieved 20151231.
 ^ http://bcrypt.sourceforge.net bcrypt file encryption program homepage
 ^ http://bcrypt463065.android.informer.com/
 ^ http://www.t2project.org/packages/bcrypt.html
 ^ https://docs.oracle.com/cd/E51849_01/ggwinux/OGGLC/ogglc_licenses.htm
 ^ Karthikeyan Bhargavan; Gaëtan Leurent (August 2016). "On the Practical (In)Security of 64bit Block Ciphers — Collision Attacks on HTTP over TLS and OpenVPN". ACM CCS 2016.
 ^ Tom Gonzalez (January 2007). "A Reflection Attack on Blowfish" (PDF). Journal of LATEX Class Files.
 ^ Orhun Kara & Cevat Manap (March 2007). "A New Class of Weak Keys for Blowfish" (PDF). FSE 2007.
 ^ "GnuPG Frequently Asked Questions". www.gnupg.org.
External links
Wikimedia Commons has media related to Blowfish (cipher). 
 Bruce Schneier. "The Blowfish Encryption Algorithm".
 Bruce Schneier. "Products that Use Blowfish".
 "Standard Cryptographic Algorithm Naming: Blowfish".