DEV Community

PrashikNikumbe
PrashikNikumbe

Posted on

Blowfish Cipher

Blowfish is a symmetric-key block cipher designed by Bruce Schneier in 1993.It is found to be faster than DES and used as a replacement to it. It is unpatented means anyone can freely use it. One major feature is that its S-Boxes are made by using private key, also its subkey generation process is complex. It is more secured as no cryptanalysis of it is found till now.

• Plain text size: 64-bit
• Key Size: It is variable in size and comes in range 32-bits to 448-bit
• Number of rounds: 16
• S-box used: 4
• Subkey used(P-array): 18

There is total three parts in Blowfish algorithm

  1. Key Expansion
  2. Encryption
  3. Decryption

1)Key Expansion

P-array is used which is consist of eighteen 32-bit subkey initialized with hexadecimal digits of pi. Four S-boxes are used which is of 32-bit each and has 256 entries initialized with hex values of pi.

The private key is divided in fourteen 32-bit key for this the key size should be 448-bit but if its less than 448-bit then repetition of a key is done to make it of 488-bit. These values are stored in K-array.

Step1:The first value of P-array i.e. P[0] is XORed with K[0] and result is replaced with P[0].This process is continues till all fourteen values of P-array XORed and replaced with all fourteen values of K-array, now for P[14] to P[17] it is X0Red with K[0] to K[3].

Step2: Now the plaintext of 64-bit initialized to all zero bits and passed through blowfish encryption process which used P-array formed in Step1 and S-boxes which described above. The result which is of 64-bit breaks into two parts of 32-bit each and replaced with P[0] and P[1].

Step3: Process in Step2 is repeated with modified P-array and results in now replaced with P[2] and P[3].

Step4:The above Step3 is repeated until all P-arrays elements are replaced and All S-boxes get replaced in order with the values of generated by blowfish algorithm.

Total 521 iterations are required to generate all sub-keys. They are stored and used in encryption and decryption process.

2)Encryption

Encryption Flowchart

As shown above, plain text of 64-bit is passed through 16 rounds. There is one sub key for each round that comes from P-array, utilization of P[0] to P[15] is done in this block. The output of 16th round is used in whitening process which utilizes the remaining subkey P[16] and P[17]. The final results come out as a cipher text of 64-bit.
Now let’s see what happens in each round.

Rounds

Round flowchart

In first round the plain text is divided into two parts of size 32-bit each. The left part is XORed with 32-bit P[0] for 1st round, also it feed into right part of plain text, passed for next round. The XORed value is passed through function F and the returned 32-bit value is again XORed with right part of plain text. Now it feed into left part of plaintext which is used in next round. The process continues for next fifteen rounds.

Function(F)

Function flowchart

Function F takes input which comes from XORed of left side of plain text and P[i] as shown in round process. The input is of 32-bit and which is divided into four 8-bit blocks. S-boxes take input as an 8-bit and maps within its 256 entries and give output of 32-bit. Output of S-box1 and S-box 2 is feed into modulo add of 2^32 and its result XORed with output of S-box3 which is further modulo add 2^32 with S-box4 output. The resultant of these function is of 32-bit.This function runs for 16 times.

Output Whitening

Output Whitening flowchart

The output that comes from 16th round is break into left and right part. Left part is modulo added 2^32 with P[16],result of it now becomes the right part of Cipher Text. Similarly, right part modulo added 2^32 with P[17] and result is feed into left part of Cipher Text. This process is more like undo the swapping that occurred in 16th round. This block gives a Cipher Text of 64-Bit.

3)Decryption

Alt Text

The decryption process is same as of encryption the difference is that the P-array is used here in a reverse manner. As shown above in 16 rounds the P-array values from P[17] to P[2] is used and remaining two i.e. P[1] and P[0] values are used in Whitening process. P-array and S-boxes are same that used in encryption process. Function F is also same which is used in round process.

Top comments (0)