Browse Source

improve performance of blowfish encryption, also speeds up blowfish passwords.

OPENBSD_2_3
provos 27 years ago
parent
commit
6640fe10d3
1 changed files with 34 additions and 77 deletions
  1. +34
    -77
      src/lib/libc/crypt/blowfish.c

+ 34
- 77
src/lib/libc/crypt/blowfish.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: blowfish.c,v 1.6 1997/07/25 20:30:00 mickey Exp $ */
/* $OpenBSD: blowfish.c,v 1.7 1998/03/02 14:11:50 provos Exp $ */
/*
* Blowfish block cipher for OpenBSD
* Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de>
@ -39,7 +39,7 @@
* Bruce Schneier.
*/
#ifdef TEST
#if 0
#include <stdio.h> /* used for debugging */
#include <string.h>
#endif
@ -54,35 +54,14 @@
#define inline
#endif /* !__GNUC__ */
static inline u_int32_t F __P((blf_ctx * bc, u_int32_t x));
/* Function for Feistel Networks */
static inline u_int32_t
F(bc, x)
blf_ctx *bc;
u_int32_t x;
{
u_int8_t a;
u_int8_t b;
u_int8_t c;
u_int8_t d;
u_int32_t y;
d = (u_int8_t) (x & 0xFF);
x >>= 8;
c = (u_int8_t) (x & 0xFF);
x >>= 8;
b = (u_int8_t) (x & 0xFF);
x >>= 8;
a = (u_int8_t) (x & 0xFF);
y = bc->S[0][a] + bc->S[1][b];
y = y ^ bc->S[2][c];
y = y + bc->S[3][d];
return y;
}
#define F(bc, x) ((((bc)->S[0][((x) & 0xFF000000) >> 24] \
+ (bc)->S[1][((x) &0xFF0000 ) >> 16]) \
^ (bc)->S[2][((x) & 0xFF00) >> 8]) \
+ (bc)->S[3][(x) & 0x00FF])
#define BLFRND(bc,i,j,n) (i ^= F(bc,j) ^ (bc)->P[n])
void
Blowfish_encipher(c, xl, xr)
@ -92,33 +71,22 @@ Blowfish_encipher(c, xl, xr)
{
u_int32_t Xl;
u_int32_t Xr;
u_int32_t temp;
u_int16_t i;
Xl = *xl;
Xr = *xr;
for (i = 0; i < BLF_N; i++) {
/* One round of a Feistel network */
Xl = Xl ^ c->P[i];
Xr = F(c, Xl) ^ Xr;
/* Swap Xl and Xr */
temp = Xl;
Xl = Xr;
Xr = temp;
}
/* End of Feistel Network, swap again */
temp = Xl;
Xl = Xr;
Xr = temp;
Xr = Xr ^ c->P[BLF_N];
Xl = Xl ^ c->P[BLF_N + 1];
*xl = Xl;
*xr = Xr;
Xl ^= c->P[0];
BLFRND(c, Xr, Xl, 1); BLFRND(c, Xl, Xr, 2);
BLFRND(c, Xr, Xl, 3); BLFRND(c, Xl, Xr, 4);
BLFRND(c, Xr, Xl, 5); BLFRND(c, Xl, Xr, 6);
BLFRND(c, Xr, Xl, 7); BLFRND(c, Xl, Xr, 8);
BLFRND(c, Xr, Xl, 9); BLFRND(c, Xl, Xr, 10);
BLFRND(c, Xr, Xl, 11); BLFRND(c, Xl, Xr, 12);
BLFRND(c, Xr, Xl, 13); BLFRND(c, Xl, Xr, 14);
BLFRND(c, Xr, Xl, 15); BLFRND(c, Xl, Xr, 16);
*xl = Xr ^ c->P[17];
*xr = Xl;
}
void
@ -129,33 +97,22 @@ Blowfish_decipher(c, xl, xr)
{
u_int32_t Xl;
u_int32_t Xr;
u_int32_t temp;
u_int16_t i;
Xl = *xl;
Xr = *xr;
for (i = BLF_N + 1; i > 1; i--) {
/* One round of a Feistel network */
Xl = Xl ^ c->P[i];
Xr = F(c, Xl) ^ Xr;
/* Swap Xl and Xr */
temp = Xl;
Xl = Xr;
Xr = temp;
}
/* End of Feistel Network, swap again */
temp = Xl;
Xl = Xr;
Xr = temp;
Xr = Xr ^ c->P[1];
Xl = Xl ^ c->P[0];
*xr = Xr;
*xl = Xl;
Xl ^= c->P[17];
BLFRND(c, Xr, Xl, 16); BLFRND(c, Xl, Xr, 15);
BLFRND(c, Xr, Xl, 14); BLFRND(c, Xl, Xr, 13);
BLFRND(c, Xr, Xl, 12); BLFRND(c, Xl, Xr, 11);
BLFRND(c, Xr, Xl, 10); BLFRND(c, Xl, Xr, 9);
BLFRND(c, Xr, Xl, 8); BLFRND(c, Xl, Xr, 7);
BLFRND(c, Xr, Xl, 6); BLFRND(c, Xl, Xr, 5);
BLFRND(c, Xr, Xl, 4); BLFRND(c, Xl, Xr, 3);
BLFRND(c, Xr, Xl, 2); BLFRND(c, Xl, Xr, 1);
*xl = Xr ^ c->P[0];
*xr = Xl;
}
void
@ -627,7 +584,7 @@ blf_dec(c, data, blocks)
d += 2;
}
}
#ifdef TEST
#ifdef 0
void
report(u_int32_t data[], u_int16_t len)
{
@ -669,4 +626,4 @@ main(void)
blf_dec(&c, data2, 1);
report(data2, 2);
}
#endif /* TEST */
#endif

Loading…
Cancel
Save