Performing CFB Operation Manually

This section provides a tutorial example on how to perform CFB operation manually using the standard Blowfish block cipher (or ECB mode) and an XOR function.

If you want to learn how CFB operation mode works, you simulate it manually with the help of the standard block cipher (the ECB operation mode), which performs a pure Blowfish encryption on one block at a time.

First, let's review the CFB operation mode process flow:

IV -----|        -----|        -----| 
      E(K)     /    E(K)     /    E(K)
        |     /       |     /       |
        |    /        |    /        | 
 P[1]--XOR  /  P[2]--XOR  /  P[3]--XOR
        |  /          |  /          | 
        | /           | /           | 
      C[1]          C[2]          C[3]

As you can see the E(K) function can be by the Blowfish-Block-Cipher.php script. For XOR function, you can use the following script:

<?php
#- Blowfish-XOR-Function.php
#- Copyright (c) 2018 HerongYang.com, All Rights Reserved.

   $a = hex2bin('0000000000000000');
   $b = hex2bin('0000000000000000');
   $x = hex2bin('0000000000000000');
   
   if (count($argv)>1) $a = hex2bin($argv[1]);
   if (count($argv)>2) $b = hex2bin($argv[2]);
   if (count($argv)>3) $x = hex2bin($argv[3]);

   $result = $a ^ $b;
   $validation = $result==$x;
   print("   Input A: ".bin2hex($a)."\n");
   print("   Input B: ".bin2hex($b)."\n");
   print("   A XOR B: ".bin2hex($x)."\n");
   print("    Result: ".bin2hex($result)."\n");
   print("Validation: ".$validation."\n");
?>

Now I am ready to simulate the CFB operation "manually" based on the test output from the last tutorial:

Secret Key: 0000000000000000 + IV: 0000000000000000
Plaintext:  e1c030e74c14d261 4ef997456198dd78
Oper. Mode: ncfb
Ciphertext: af39a7a22d8c0f19 ce94e96f7a22ac91
PHP Result: af39a7a22d8c0f19 ce94e96f7a22ac91
Validation: 1

1. Perform E(K,IV) with Blowfish-Block-Cipher.php, I get 4ef997456198dd78:

C:\herong>php Blowfish-Block-Cipher.php \
   0000000000000000 0000000000000000 4ef997456198dd78

key bytes        clear bytes      cipher bytes
0000000000000000 0000000000000000 4ef997456198dd78
Test Result: 4ef997456198dd78
Validation: 1

2. Perform P[1] XOR E(K,IV), I get af39a7a22d8c0f19, which matches the first block of the Blowfish-Block-Chaining-Cipher.php output in "ncfb" mode:

C:\herong>php Blowfish-XOR-Function.php \
   e1c030e74c14d261 4ef997456198dd78 af39a7a22d8c0f19
   
   Input A: e1c030e74c14d261
   Input B: 4ef997456198dd78
   A XOR B: af39a7a22d8c0f19
    Result: af39a7a22d8c0f19
Validation: 1

You can continue the process on the second block.

Table of Contents

 About This Book

 Blowfish Cipher Algorithm

 Perl Crypt::Blowfish Module

 Perl Crypt::ECB Perl Module

 Perl Crypt::CBC Module

 Perl Crypt::CFB Perl Module

 OpenSSL "enc -bf-ecb" for Blowfish/ECB Encryption

 OpenSSL "enc -bf-cbc" for Blowfish/CBC Encryption

 OpenSSL "enc -bf-cfb" for Blowfish/CFB Encryption

 OpenSSL "enc -bf-ofb" for Blowfish/OFB Encryption

PHP Mcrypt Extension for Blowfish

 What is PHP Mcrypt Extension

 PHP Mcrypt Blowfish Block Cipher

 Mycrypt Blowfish Block Chaining Cipher

 "ncfb/nofb" for Block Chaining Ciphers

Performing CFB Operation Manually

 php_blowfish.php - PHP Blowfish Demo

 Blowfish 8-Bit Cipher in PHP

 References

 Full Version in PDF/EPUB