Aim: To Implement Blowfish Algorithm. Theory
Aim: To Implement Blowfish Algorithm. Theory
Algorithm:
1. Form P-array initials as many as 18 units (P1, P2, P18 ..............) Missing each 32-bit value.
Array P consists of eighteen 32-bit subkey:
P1,P2,.......,P18
2. Form
S-box
by
4
pieces
each
worth
32-bit
with
input
256.
Four
32-bit
S-boxes
each
having
256
entries:
S1,
0,
S1,
1,
....................,
S1,
255
S2,
0,
S2,
1,
....................,
S2,
255
S3,
0,
S3,
1,
....................,
S3,
255
S4,
0,
S4,
1,
....................,
S4,
255
3. Plaintext to be encrypted is assumed as input, Plaintext is taken as 64-bit, and if less than
64-bit then we add bitnya, so that in later operations in accordance with the data.
4. Results decision was divided by 2, the first 32-bit called XL, 32-bit the second is called
XR.
5. Next do the operation xor XL = XL and XR Pi = F (XL) xor XR
6. The results of the above operation be exchanged XL XR and XR to XL.
Page no.
fig.Blowfish algorithm
Applications:
1. Blowfish is a fast block cipher, except when changing keys. Each new key requires
pre-processing 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.
2. 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.
3. 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.
Page no.
4. 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.
Program
#include<stdio.h>
#define N
16
Page no.
Page no.
Page no.
Page no.
Page no.
Page no.
Page no.
y = y ^ ctx->S[2][c];
y = y + ctx->S[3][d];
return y;
}
void Blowfish_Encrypt(BLOWFISH_CTX *ctx, unsigned long *xl, unsigned long *xr){
unsigned long Xl;
unsigned long Xr;
unsigned long temp;
short
i;
Xl = *xl;
Xr = *xr;
for (i = 0; i < N; ++i) {
Xl = Xl ^ ctx->P[i];
Xr = F(ctx, Xl) ^ Xr;
temp = Xl;
Xl = Xr;
Xr = temp;
}
temp = Xl;
Xl = Xr;
Xr = temp;
Xr = Xr ^ ctx->P[N];
Xl = Xl ^ ctx->P[N + 1];
*xl = Xl;
*xr = Xr;
}
void Blowfish_Decrypt(BLOWFISH_CTX *ctx, unsigned long *xl, unsigned long *xr){
unsigned long Xl;
unsigned long Xr;
unsigned long temp;
short
i;
Xl = *xl;
Xr = *xr;
for (i = N + 1; i > 1; --i) {
Xl = Xl ^ ctx->P[i];
Xr = F(ctx, Xl) ^ Xr;
/* Exchange Xl and Xr */
temp = Xl;
Xl = Xr;
Xr = temp;
}
Page no.
/* Exchange Xl and Xr */
temp = Xl;
Xl = Xr;
Xr = temp;
Xr = Xr ^ ctx->P[1];
Xl = Xl ^ ctx->P[0];
*xl = Xl;
*xr = Xr;
}
void Blowfish_Init(BLOWFISH_CTX *ctx, unsigned char *key, int keyLen) {
int i, j, k;
unsigned long data, datal, datar;
for (i = 0; i < 4; i++) {
for (j = 0; j < 256; j++)
ctx->S[i][j] = ORIG_S[i][j];
}
j = 0;
for (i = 0; i < N + 2; ++i) {
data = 0x00000000;
for (k = 0; k < 4; ++k) {
data = (data << 8) | key[j];
j = j + 1;
if (j >= keyLen)
j = 0;
}
ctx->P[i] = ORIG_P[i] ^ data;
}
datal = 0x00000000;
datar = 0x00000000;
for (i = 0; i < N + 2; i += 2) {
Blowfish_Encrypt(ctx, &datal, &datar);
ctx->P[i] = datal;
ctx->P[i + 1] = datar;
}
for (i = 0; i < 4; ++i) {
for (j = 0; j < 256; j += 2) {
Blowfish_Encrypt(ctx, &datal, &datar);
ctx->S[i][j] = datal;
Page no.
ctx->S[i][j + 1] = datar;
}
}
}
void main(void) {
unsigned long L = 1, R = 2;
BLOWFISH_CTX ctx;
Blowfish_Init (&ctx, (unsigned char*)"TESTKEY", 7);
Blowfish_Encrypt(&ctx, &L, &R);
printf("%08lX %08lX\n", L, R);
if (L == 0xDF333FD2L && R == 0x30A71BB4L)
printf("Test encryption OK.\n");
else
printf("Test encryption failed.\n");
Blowfish_Decrypt(&ctx, &L, &R);
if (L == 1 && R == 2)
printf("Test decryption OK.\n");
else
printf("Test decryption failed.\n");
}
Output:
DF333FD2 30A71BB4
Test encryption OK.
Test decryption OK.