Following is related to a version of BLOWFISH, written by me. You can find it at: http://www.s-direktnet.de/homepages/neumann/crypt/en/index.html. See category Algorithms and then BLOWFISH and download Blowfish.cpp. For any comments or questions, please contact me: neumann@s-direktnet.de.
The number of rounds is normally 16 and the block-size is 64-bit. But there are other version of BLOWFISH (e.g. Mini-Blowfish), where the block-size and the number of rounds are different.
If you want to change the number of rounds you have to do:
don't #define FAST - don't use the macros
change the source to the following (green are the changes):
void Blowfish_encryptBlock(ULONG& xl, ULONG& xr) {
ULONG temp;
for(int i=0; i<NUMBER_OF_ROUNDS; ++i) {
xl ^= PArray[i];
xr ^= F(xl);
temp = xl; xl = xr; xr = temp;
}
temp = xl; xl = xr; xr = temp;
xr ^= PArray[NUMBER_OF_ROUNDS];
xl ^= PArray[NUMBER_OF_ROUNDS+1];
}
void Blowfish_decryptBlock(ULONG& xl, ULONG& xr) {
ULONG temp;
for(int i=NUMBER_OF_ROUNDS+1; i>1; --i) {
xl ^= PArray[i];
xr ^= F(xl);
temp = xl; xl = xr; xr = temp;
}
temp = xl; xl = xr; xr = temp;
xr ^= PArray[1];
xl ^= PArray[0];
}
ULONG PArray[NUMBER_OF_ROUNDS+2], initial_PArray[NUMBER_OF_ROUNDS+2] = { /* see below */ }
|
The array "initial_PArray" must be filled!!
"initial_SBoxes" and "initial_PArray" are
filled with the digits of PI, but you can also fill them with
random numbers, or any non-periodical number.
Then in "Blowfish_init" you have to change all occurences of "18" (the size of the initial_PArray with 16 round) to NUMBER_OF_ROUND+2:
for(i=0; i<NUMBER_OF_ROUNDS+2; ++i) {
// generate from string "key" 32-bit keys
val = 0;
for(j=0; j<4; ++j) {
val = (val << 8) | key[keypos++];
if(keypos >= keylen) keypos=0;
}
PArray[i] = initial_PArray[i] ^ val;
}
data.l = data.r = 0;
for(i=0; i<NUMBER_OF_ROUNDS+2; i+=2) {
Blowfish_encryptBlock(data.l,data.r);
PArray[i] = data.l;
PArray[i+1] = data.r;
}
|
That's all!
Normally the blocksize is 64 bits!
But you can change this to 32-bit or 128-bit (or even more).
You have to change ULONG to what you want, for example to "unsigned short" or a type which has 128-bits. But then you have to change the "initial_PArray" and "intitial_SBoxes". Fill them with "unsigned short"'s (or 128-bit values) of PI or anything else (see above).
Michael Neumann, 24.05.1999, Germany