diff options
author | Brad Smith <brad@cvs.openbsd.org> | 2013-11-18 18:49:54 +0000 |
---|---|---|
committer | Brad Smith <brad@cvs.openbsd.org> | 2013-11-18 18:49:54 +0000 |
commit | 0452098f994a95e340159a42f8bd629e56e0aefa (patch) | |
tree | 9776fbe37325706c441b8839e75e03bad01808ef | |
parent | 1b8eaab01e0d762a0e87fe1c516ab2bb458ec3f7 (diff) |
ansify some function definitions and tidy up this code style wise. this
ancient code looked pretty crummy.
ok deraadt@
-rw-r--r-- | sys/crypto/ecb3_enc.c | 51 | ||||
-rw-r--r-- | sys/crypto/ecb_enc.c | 174 | ||||
-rw-r--r-- | sys/crypto/set_key.c | 169 |
3 files changed, 188 insertions, 206 deletions
diff --git a/sys/crypto/ecb3_enc.c b/sys/crypto/ecb3_enc.c index 2d52795f18e..6fdb9f9d4d5 100644 --- a/sys/crypto/ecb3_enc.c +++ b/sys/crypto/ecb3_enc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ecb3_enc.c,v 1.2 2002/10/27 13:24:26 miod Exp $ */ +/* $OpenBSD: ecb3_enc.c,v 1.3 2013/11/18 18:49:53 brad Exp $ */ /* lib/des/ecb3_enc.c */ /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au) @@ -49,31 +49,28 @@ #include "des_locl.h" -void des_ecb3_encrypt(input, output, ks1, ks2, ks3, encrypt) -des_cblock (*input); -des_cblock (*output); -des_key_schedule ks1; -des_key_schedule ks2; -des_key_schedule ks3; -int encrypt; - { - register u_int32_t l0,l1; - register unsigned char *in,*out; +void +des_ecb3_encrypt(des_cblock (*input), des_cblock (*output), + des_key_schedule ks1, des_key_schedule ks2, des_key_schedule ks3, + int encrypt) +{ + register u_int32_t l0, l1; + register unsigned char *in, *out; u_int32_t ll[2]; - in=(unsigned char *)input; - out=(unsigned char *)output; - c2l(in,l0); - c2l(in,l1); - IP(l0,l1); - ll[0]=l0; - ll[1]=l1; - des_encrypt2(ll,ks1,encrypt); - des_encrypt2(ll,ks2,!encrypt); - des_encrypt2(ll,ks3,encrypt); - l0=ll[0]; - l1=ll[1]; - FP(l1,l0); - l2c(l0,out); - l2c(l1,out); - } + in = (unsigned char *) input; + out = (unsigned char *) output; + c2l(in, l0); + c2l(in, l1); + IP(l0, l1); + ll[0] = l0; + ll[1] = l1; + des_encrypt2(ll, ks1, encrypt); + des_encrypt2(ll, ks2, !encrypt); + des_encrypt2(ll, ks3, encrypt); + l0 = ll[0]; + l1 = ll[1]; + FP(l1, l0); + l2c(l0, out); + l2c(l1, out); +} diff --git a/sys/crypto/ecb_enc.c b/sys/crypto/ecb_enc.c index b121edc488e..e82c7854f40 100644 --- a/sys/crypto/ecb_enc.c +++ b/sys/crypto/ecb_enc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ecb_enc.c,v 1.3 2005/06/13 10:56:44 hshoexer Exp $ */ +/* $OpenBSD: ecb_enc.c,v 1.4 2013/11/18 18:49:53 brad Exp $ */ /* lib/des/ecb_enc.c */ /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au) @@ -52,100 +52,93 @@ const char *DES_version="libdes v 3.21 - 95/11/21 - eay"; -void des_ecb_encrypt(input, output, ks, encrypt) -des_cblock (*input); -des_cblock (*output); -des_key_schedule ks; -int encrypt; - { - register u_int32_t l0,l1; - register unsigned char *in,*out; +void +des_ecb_encrypt(des_cblock (*input), des_cblock (*output), des_key_schedule ks, + int encrypt) +{ + register u_int32_t l0, l1; + register unsigned char *in, *out; u_int32_t ll[2]; - in=(unsigned char *)input; - out=(unsigned char *)output; - c2l(in,l0); ll[0]=l0; - c2l(in,l1); ll[1]=l1; - des_encrypt(ll,ks,encrypt); - l0=ll[0]; l2c(l0,out); - l1=ll[1]; l2c(l1,out); - l0=l1=ll[0]=ll[1]=0; - } - -void des_encrypt(data, ks, encrypt) -u_int32_t *data; -des_key_schedule ks; -int encrypt; - { - register u_int32_t l,r,t,u; + in = (unsigned char *) input; + out = (unsigned char *) output; + c2l(in, l0); + ll[0] = l0; + c2l(in, l1); + ll[1] = l1; + des_encrypt(ll, ks, encrypt); + l0 = ll[0]; + l2c(l0, out); + l1 = ll[1]; + l2c(l1, out); + l0 = l1 = ll[0] = ll[1] = 0; +} + +void +des_encrypt(u_int32_t *data, des_key_schedule ks, int encrypt) +{ + register u_int32_t l, r, t, u; #ifdef DES_USE_PTR register unsigned char *des_SP=(unsigned char *)des_SPtrans; #endif register int i; register u_int32_t *s; - u=data[0]; - r=data[1]; + u = data[0]; + r = data[1]; - IP(u,r); + IP(u, r); /* Things have been modified so that the initial rotate is * done outside the loop. This required the * des_SPtrans values in sp.h to be rotated 1 bit to the right. * One perl script later and things have a 5% speed up on a sparc2. * Thanks to Richard Outerbridge <71755.204@CompuServe.COM> * for pointing this out. */ - l=(r<<1)|(r>>31); - r=(u<<1)|(u>>31); + l = (r << 1) | (r >> 31); + r = (u << 1) | (u >> 31); /* clear the top bits on machines with 8byte longs */ - l&=0xffffffffL; - r&=0xffffffffL; + l &= 0xffffffffL; + r &= 0xffffffffL; - s=(u_int32_t *)ks; + s = (u_int32_t *) ks; /* I don't know if it is worth the effort of loop unrolling the * inner loop */ - if (encrypt) - { - for (i=0; i<32; i+=4) - { - D_ENCRYPT(l,r,i+0); /* 1 */ - D_ENCRYPT(r,l,i+2); /* 2 */ - } + if (encrypt) { + for (i = 0; i < 32; i += 4) { + D_ENCRYPT(l, r, i + 0); /* 1 */ + D_ENCRYPT(r, l, i + 2); /* 2 */ } - else - { - for (i=30; i>0; i-=4) - { - D_ENCRYPT(l,r,i-0); /* 16 */ - D_ENCRYPT(r,l,i-2); /* 15 */ - } + } else { + for (i = 30; i > 0; i -= 4) { + D_ENCRYPT(l, r, i - 0); /* 16 */ + D_ENCRYPT(r, l, i - 2); /* 15 */ } - l=(l>>1)|(l<<31); - r=(r>>1)|(r<<31); - /* clear the top bits on machines with 8byte longs */ - l&=0xffffffffL; - r&=0xffffffffL; - - FP(r,l); - data[0]=l; - data[1]=r; - l=r=t=u=0; } - -void des_encrypt2(data, ks, encrypt) -u_int32_t *data; -des_key_schedule ks; -int encrypt; - { - register u_int32_t l,r,t,u; + l = (l >> 1) | (l << 31); + r = (r >> 1) | (r << 31); + /* clear the top bits on machines with 8byte longs */ + l &= 0xffffffffL; + r &= 0xffffffffL; + + FP(r, l); + data[0] = l; + data[1] = r; + l = r = t = u = 0; +} + +void +des_encrypt2(u_int32_t *data, des_key_schedule ks, int encrypt) +{ + register u_int32_t l, r, t, u; #ifdef DES_USE_PTR register unsigned char *des_SP=(unsigned char *)des_SPtrans; #endif register int i; register u_int32_t *s; - u=data[0]; - r=data[1]; + u = data[0]; + r = data[1]; /* Things have been modified so that the initial rotate is * done outside the loop. This required the @@ -153,39 +146,34 @@ int encrypt; * One perl script later and things have a 5% speed up on a sparc2. * Thanks to Richard Outerbridge <71755.204@CompuServe.COM> * for pointing this out. */ - l=(r<<1)|(r>>31); - r=(u<<1)|(u>>31); + l = (r << 1) | (r >> 31); + r = (u << 1) | (u >> 31); /* clear the top bits on machines with 8byte longs */ - l&=0xffffffffL; - r&=0xffffffffL; + l &= 0xffffffffL; + r &= 0xffffffffL; - s=(u_int32_t *)ks; + s = (u_int32_t *) ks; /* I don't know if it is worth the effort of loop unrolling the * inner loop */ - if (encrypt) - { - for (i=0; i<32; i+=4) - { - D_ENCRYPT(l,r,i+0); /* 1 */ - D_ENCRYPT(r,l,i+2); /* 2 */ - } + if (encrypt) { + for (i = 0; i < 32; i += 4) { + D_ENCRYPT(l, r, i + 0); /* 1 */ + D_ENCRYPT(r, l, i + 2); /* 2 */ } - else - { - for (i=30; i>0; i-=4) - { - D_ENCRYPT(l,r,i-0); /* 16 */ - D_ENCRYPT(r,l,i-2); /* 15 */ - } + } else { + for (i = 30; i > 0; i -= 4) { + D_ENCRYPT(l, r, i - 0); /* 16 */ + D_ENCRYPT(r, l, i - 2); /* 15 */ } - l=(l>>1)|(l<<31); - r=(r>>1)|(r<<31); + } + l = (l >> 1) | (l << 31); + r = (r >> 1) | (r << 31); /* clear the top bits on machines with 8byte longs */ - l&=0xffffffffL; - r&=0xffffffffL; + l &= 0xffffffffL; + r &= 0xffffffffL; - data[0]=l; - data[1]=r; - l=r=t=u=0; - } + data[0] = l; + data[1] = r; + l = r = t = u = 0; +} diff --git a/sys/crypto/set_key.c b/sys/crypto/set_key.c index 31b9be0f648..cceb3fb5d67 100644 --- a/sys/crypto/set_key.c +++ b/sys/crypto/set_key.c @@ -1,4 +1,4 @@ -/* $OpenBSD: set_key.c,v 1.2 2002/10/27 13:24:26 miod Exp $ */ +/* $OpenBSD: set_key.c,v 1.3 2013/11/18 18:49:53 brad Exp $ */ /* lib/des/set_key.c */ /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au) @@ -66,27 +66,26 @@ static int check_parity(); int des_check_key=0; -void des_set_odd_parity(key) -des_cblock (*key); - { +void +des_set_odd_parity(des_cblock (*key)) +{ int i; - for (i=0; i<DES_KEY_SZ; i++) - (*key)[i]=odd_parity[(*key)[i]]; - } + for (i = 0; i < DES_KEY_SZ; i++) + (*key)[i] = odd_parity[(*key)[i]]; +} -static int check_parity(key) -des_cblock (*key); - { +static int +check_parity(des_cblock (*key)) +{ int i; - for (i=0; i<DES_KEY_SZ; i++) - { + for (i = 0; i < DES_KEY_SZ; i++) { if ((*key)[i] != odd_parity[(*key)[i]]) return(0); - } - return(1); } + return (1); +} /* Weak and semi week keys as take from * %A D.W. Davies @@ -118,119 +117,117 @@ static des_cblock weak_keys[NUM_WEAK_KEY]={ {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE}, {0xFE,0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1}}; -int des_is_weak_key(key) -des_cblock (*key); - { +int +des_is_weak_key(des_cblock (*key)) +{ int i; - for (i=0; i<NUM_WEAK_KEY; i++) + for (i = 0; i < NUM_WEAK_KEY; i++) { /* Added == 0 to comparision, I obviously don't run * this section very often :-(, thanks to * engineering@MorningStar.Com for the fix * eay 93/06/29 */ - if (bcmp(weak_keys[i],key,sizeof(des_cblock)) == 0) return(1); - return(0); + if (bcmp(weak_keys[i], key, sizeof(des_cblock)) == 0) + return (1); } + return (0); +} /* NOW DEFINED IN des_local.h * See ecb_encrypt.c for a pseudo description of these macros. - * #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\ + * #define PERM_OP(a, b, t, n, m) ((t) = ((((a) >> (n))^(b)) & (m)),\ * (b)^=(t),\ - * (a)=((a)^((t)<<(n)))) + * (a) = ((a)^((t) << (n)))) */ -#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\ - (a)=(a)^(t)^(t>>(16-(n)))) +#define HPERM_OP(a, t, n, m) ((t) = ((((a) << (16 - (n)))^(a)) & (m)),\ + (a) = (a)^(t)^(t >> (16 - (n)))) -static int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0}; +static int shifts2[16]={0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0}; /* return 0 if key parity is odd (correct), * return -1 if key parity error, * return -2 if illegal weak key. */ -int des_set_key(key, schedule) -des_cblock (*key); -des_key_schedule schedule; - { - register u_int32_t c,d,t,s; +int +des_set_key(des_cblock (*key), des_key_schedule schedule) +{ + register u_int32_t c, d, t, s; register unsigned char *in; register u_int32_t *k; register int i; - if (des_check_key) - { + if (des_check_key) { if (!check_parity(key)) return(-1); if (des_is_weak_key(key)) return(-2); - } + } - k=(u_int32_t *)schedule; - in=(unsigned char *)key; + k = (u_int32_t *) schedule; + in = (unsigned char *) key; - c2l(in,c); - c2l(in,d); + c2l(in, c); + c2l(in, d); /* do PC1 in 60 simple operations */ -/* PERM_OP(d,c,t,4,0x0f0f0f0fL); - HPERM_OP(c,t,-2, 0xcccc0000L); - HPERM_OP(c,t,-1, 0xaaaa0000L); - HPERM_OP(c,t, 8, 0x00ff0000L); - HPERM_OP(c,t,-1, 0xaaaa0000L); - HPERM_OP(d,t,-8, 0xff000000L); - HPERM_OP(d,t, 8, 0x00ff0000L); - HPERM_OP(d,t, 2, 0x33330000L); - d=((d&0x00aa00aaL)<<7L)|((d&0x55005500L)>>7L)|(d&0xaa55aa55L); - d=(d>>8)|((c&0xf0000000L)>>4); - c&=0x0fffffffL; */ +/* PERM_OP(d, c, t, 4, 0x0f0f0f0fL); + HPERM_OP(c, t, -2, 0xcccc0000L); + HPERM_OP(c, t, -1, 0xaaaa0000L); + HPERM_OP(c, t, 8, 0x00ff0000L); + HPERM_OP(c, t, -1, 0xaaaa0000L); + HPERM_OP(d, t, -8, 0xff000000L); + HPERM_OP(d, t, 8, 0x00ff0000L); + HPERM_OP(d, t, 2, 0x33330000L); + d = ((d & 0x00aa00aaL) << 7L) | ((d & 0x55005500L) >> 7L) | (d & 0xaa55aa55L); + d = (d >> 8) | ((c & 0xf0000000L) >> 4); + c &= 0x0fffffffL; */ /* I now do it in 47 simple operations :-) * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov) * for the inspiration. :-) */ - PERM_OP (d,c,t,4,0x0f0f0f0fL); - HPERM_OP(c,t,-2,0xcccc0000L); - HPERM_OP(d,t,-2,0xcccc0000L); - PERM_OP (d,c,t,1,0x55555555L); - PERM_OP (c,d,t,8,0x00ff00ffL); - PERM_OP (d,c,t,1,0x55555555L); - d= (((d&0x000000ffL)<<16L)| (d&0x0000ff00L) | - ((d&0x00ff0000L)>>16L)|((c&0xf0000000L)>>4L)); - c&=0x0fffffffL; - - for (i=0; i<ITERATIONS; i++) - { + PERM_OP (d, c, t, 4, 0x0f0f0f0fL); + HPERM_OP(c, t, -2, 0xcccc0000L); + HPERM_OP(d, t, -2, 0xcccc0000L); + PERM_OP (d, c, t, 1, 0x55555555L); + PERM_OP (c, d, t, 8, 0x00ff00ffL); + PERM_OP (d, c, t, 1, 0x55555555L); + d = (((d & 0x000000ffL) << 16L) | (d & 0x0000ff00L) | + ((d & 0x00ff0000L) >> 16L) | ((c & 0xf0000000L) >> 4L)); + c &= 0x0fffffffL; + + for (i = 0; i < ITERATIONS; i++) { if (shifts2[i]) - { c=((c>>2L)|(c<<26L)); d=((d>>2L)|(d<<26L)); } + { c = ((c >> 2L) | (c << 26L)); d = ((d >> 2L) | (d << 26L)); } else - { c=((c>>1L)|(c<<27L)); d=((d>>1L)|(d<<27L)); } - c&=0x0fffffffL; - d&=0x0fffffffL; + { c = ((c >> 1L) | (c << 27L)); d = ((d >> 1L) | (d << 27L)); } + c &= 0x0fffffffL; + d &= 0x0fffffffL; /* could be a few less shifts but I am to lazy at this * point in time to investigate */ - s= des_skb[0][ (c )&0x3f ]| - des_skb[1][((c>> 6)&0x03)|((c>> 7L)&0x3c)]| - des_skb[2][((c>>13)&0x0f)|((c>>14L)&0x30)]| - des_skb[3][((c>>20)&0x01)|((c>>21L)&0x06) | - ((c>>22L)&0x38)]; - t= des_skb[4][ (d )&0x3f ]| - des_skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]| - des_skb[6][ (d>>15L)&0x3f ]| - des_skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)]; + s = des_skb[0][ (c ) & 0x3f ]| + des_skb[1][((c >> 6) & 0x03) | ((c >> 7L) & 0x3c)]| + des_skb[2][((c >> 13) & 0x0f) | ((c >> 14L) & 0x30)]| + des_skb[3][((c >> 20) & 0x01) | ((c >> 21L) & 0x06) | + ((c >> 22L) & 0x38)]; + t = des_skb[4][ (d ) & 0x3f ]| + des_skb[5][((d >> 7L) & 0x03) | ((d >> 8L) & 0x3c)]| + des_skb[6][ (d >> 15L) & 0x3f ]| + des_skb[7][((d >> 21L) & 0x0f) | ((d >> 22L) & 0x30)]; /* table contained 0213 4657 */ - *(k++)=((t<<16L)|(s&0x0000ffffL))&0xffffffffL; - s= ((s>>16L)|(t&0xffff0000L)); + *(k++) = ((t << 16L) | (s & 0x0000ffffL)) & 0xffffffffL; + s = ((s >> 16L) | (t & 0xffff0000L)); - s=(s<<4L)|(s>>28L); - *(k++)=s&0xffffffffL; - } - return(0); - } - -int des_key_sched(key, schedule) -des_cblock (*key); -des_key_schedule schedule; - { - return(des_set_key(key,schedule)); + s = (s << 4L) | (s >> 28L); + *(k++) = s & 0xffffffffL; } + return (0); +} + +int +des_key_sched(des_cblock (*key), des_key_schedule schedule) +{ + return (des_set_key(key, schedule)); +} |