From ee5f8e41f37418b5bceab927ff99c451f8302548 Mon Sep 17 00:00:00 2001 From: Ken Raeburn Date: Tue, 10 Apr 2001 07:40:02 +0000 Subject: [PATCH] use const for some des inputs and reduce unnecessary casts git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@13166 dc483132-0cff-0310-8789-dd5450dbe970 --- src/include/kerberosIV/ChangeLog | 3 +++ src/include/kerberosIV/des.h | 18 +++++++++++------- src/lib/des425/ChangeLog | 15 +++++++++++++++ src/lib/des425/cksum.c | 8 ++++---- src/lib/des425/des.c | 4 ++-- src/lib/des425/enc_dec.c | 6 +++--- src/lib/des425/pcbc_encrypt.c | 21 +++++++++++---------- src/lib/des425/quad_cksum.c | 12 ++++++------ src/lib/des425/str_to_key.c | 16 ++++++++-------- src/lib/des425/t_pcbc.c | 4 ++-- src/lib/des425/util.c | 2 +- 11 files changed, 66 insertions(+), 43 deletions(-) diff --git a/src/include/kerberosIV/ChangeLog b/src/include/kerberosIV/ChangeLog index aef698dbb..b9bd0d751 100644 --- a/src/include/kerberosIV/ChangeLog +++ b/src/include/kerberosIV/ChangeLog @@ -1,5 +1,8 @@ 2001-04-10 Ken Raeburn + * des.h (des_pcbc_encrypt, des_quad_cksum, des_cbc_cksum, + des_cbc_encrypt, des_ecb_encrypt): Use const for some input args. + * krb.h (krb_get_admhst, krb_get_krbhst, krb_get_lrealm): Don't use "index" as argument name. diff --git a/src/include/kerberosIV/des.h b/src/include/kerberosIV/des.h index 67eca8ee6..529b33c82 100644 --- a/src/include/kerberosIV/des.h +++ b/src/include/kerberosIV/des.h @@ -135,25 +135,29 @@ KRB5_DLLIMP int KRB5_CALLCONV des_key_sched (C_Block, Key_schedule); KRB5_DLLIMP int KRB5_CALLCONV des_pcbc_encrypt (C_Block FAR *in, C_Block FAR *out, long length, - Key_schedule schedule, C_Block FAR *ivec, int encrypt); + const des_key_schedule schedule, C_Block FAR *ivec, + int encrypt); KRB5_DLLIMP unsigned long KRB5_CALLCONV -des_quad_cksum (unsigned char FAR *in, unsigned KRB4_32 FAR *out, +des_quad_cksum (const unsigned char FAR *in, unsigned KRB4_32 FAR *out, long length, int out_count, C_Block FAR *seed); -KRB5_DLLIMP int KRB5_CALLCONV des_string_to_key (char FAR *, C_Block); +KRB5_DLLIMP int KRB5_CALLCONV des_string_to_key (const char FAR *, C_Block); /* new */ #ifdef KRB5_GENERAL__ +/* Why are we using krb5 types as part of this API? */ KRB5_DLLIMP void KRB5_CALLCONV -des_cbc_cksum(krb5_octet *, krb5_octet *, unsigned long, - des_key_schedule, krb5_octet *); +des_cbc_cksum(const krb5_octet *, krb5_octet *, unsigned long, + const des_key_schedule, const krb5_octet *); int des_cbc_encrypt(krb5_octet *, krb5_octet *, unsigned long, - des_key_schedule, krb5_octet *, int); + const des_key_schedule, const krb5_octet *, int); krb5_error_code des_read_password(des_cblock *, char *, int); #endif +/* The unsigned long pointers are indicative of the desired alignment; + the values there aren't really treated as long values. */ KRB5_DLLIMP int KRB5_CALLCONV des_ecb_encrypt(unsigned long *, unsigned long *, - des_key_schedule, int); + const des_key_schedule, int); void des_fixup_key_parity(des_cblock); int des_check_key_parity(des_cblock); KRB5_DLLIMP int KRB5_CALLCONV des_new_random_key(des_cblock); diff --git a/src/lib/des425/ChangeLog b/src/lib/des425/ChangeLog index 412dc6484..58722e848 100644 --- a/src/lib/des425/ChangeLog +++ b/src/lib/des425/ChangeLog @@ -1,5 +1,20 @@ 2001-04-10 Ken Raeburn + * cksum.c (des_cbc_cksum): Arguments IN, KEY, and IV now const. + * des.c (des_ecb_encrypt): Change type of arg "schedule" to + const des_key_schedule and drop register decl. Make local + variable "iv" const. + * enc_dec.c (des_cbc_encrypt): Arguments KEY and IV now const. + * pcbc_encrypt.c (des_pcbc_encrypt): Argument SCHEDULE now const. + Drop some unnecessary casts. + * quad_cksum.c (vaxtohl, vaxtohs): Cast to pointer to const. + (des_quad_cksum): Argument IN now points to const. + * str_to_key.c (des_string_to_key): String argument now const. + Delete local declaration of des_cbc_cksum. Delete or fix some + casts. + * t_pcbc.c (main): Pass address of ivec to des_pcbc_encrypt. + * util.c (des_cblock_print_file): Delete unnecessary cast. + * Makefile.in (STLIBOBJS, OBJS, SRCS): Delete references to k4_glue.c etc. (clean): Remove t_quad and t_pcbc object files and test programs. diff --git a/src/lib/des425/cksum.c b/src/lib/des425/cksum.c index deff03c65..9758a054c 100644 --- a/src/lib/des425/cksum.c +++ b/src/lib/des425/cksum.c @@ -57,11 +57,11 @@ void des_cbc_cksum(in,out,length,key,iv) - krb5_octet *in; /* >= length bytes of inputtext */ - krb5_octet *out; /* >= length bytes of outputtext */ + const krb5_octet *in; /* >= length bytes of inputtext */ + krb5_octet *out; /* >= length bytes of outputtext */ register unsigned long length; /* in bytes */ - mit_des_key_schedule key; /* precomputed key schedule */ - krb5_octet *iv; /* 8 bytes of ivec */ + const mit_des_key_schedule key; /* precomputed key schedule */ + const krb5_octet *iv; /* 8 bytes of ivec */ { mit_des_cbc_cksum(in, out, length, key, iv); } diff --git a/src/lib/des425/des.c b/src/lib/des425/des.c index 9dfdce3e3..6ba03730d 100644 --- a/src/lib/des425/des.c +++ b/src/lib/des425/des.c @@ -58,10 +58,10 @@ KRB5_DLLIMP int KRB5_CALLCONV des_ecb_encrypt(clear, cipher, schedule, encrypt) unsigned long *clear; unsigned long *cipher; - register mit_des_key_schedule schedule; /* r11 */ + const mit_des_key_schedule schedule; int encrypt; /* 0 ==> decrypt, else encrypt */ { - static des_cblock iv; + const static des_cblock iv; return (mit_des_cbc_encrypt((const des_cblock *) clear, (des_cblock *) cipher, diff --git a/src/lib/des425/enc_dec.c b/src/lib/des425/enc_dec.c index f0e0daa50..c46341392 100644 --- a/src/lib/des425/enc_dec.c +++ b/src/lib/des425/enc_dec.c @@ -62,11 +62,11 @@ int des_cbc_encrypt(in,out,length,key,iv,encrypt) - krb5_octet *in; /* >= length bytes of input text */ + krb5_octet *in; /* >= length bytes of input text */ krb5_octet *out; /* >= length bytes of output text */ register unsigned long length; /* in bytes */ - mit_des_key_schedule key; /* precomputed key schedule */ - krb5_octet *iv; /* 8 bytes of ivec */ + const mit_des_key_schedule key; /* precomputed key schedule */ + const krb5_octet *iv; /* 8 bytes of ivec */ int encrypt; /* 0 ==> decrypt, else encrypt */ { return (mit_des_cbc_encrypt((const des_cblock *) in, diff --git a/src/lib/des425/pcbc_encrypt.c b/src/lib/des425/pcbc_encrypt.c index e47817061..d150af51a 100644 --- a/src/lib/des425/pcbc_encrypt.c +++ b/src/lib/des425/pcbc_encrypt.c @@ -27,19 +27,20 @@ des_pcbc_encrypt(in, out, length, schedule, ivec, encrypt) des_cblock *in; des_cblock *out; long length; - des_key_schedule schedule; + const des_key_schedule schedule; des_cblock *ivec; int encrypt; { register unsigned DES_INT32 left, right; register unsigned DES_INT32 temp; - register unsigned DES_INT32 *kp; - register unsigned char *ip, *op; + const unsigned DES_INT32 *kp; + const unsigned char *ip; + unsigned char *op; /* * Copy the key pointer, just once */ - kp = (unsigned DES_INT32 *)schedule; + kp = (const unsigned DES_INT32 *)schedule; /* * Deal with encryption and decryption separately. @@ -57,7 +58,7 @@ des_pcbc_encrypt(in, out, length, schedule, ivec, encrypt) * Initialize left and right with the contents of the initial * vector. */ - ip = (unsigned char *)ivec; + ip = *ivec; GET_HALF_BLOCK(left, ip); GET_HALF_BLOCK(right, ip); @@ -65,8 +66,8 @@ des_pcbc_encrypt(in, out, length, schedule, ivec, encrypt) * Suitably initialized, now work the length down 8 bytes * at a time. */ - ip = (unsigned char *)in; - op = (unsigned char *)out; + ip = *in; + op = *out; while (length > 0) { /* * Get block of input. If the length is @@ -142,15 +143,15 @@ des_pcbc_encrypt(in, out, length, schedule, ivec, encrypt) /* * Prime the old cipher with ivec. */ - ip = (unsigned char *)ivec; + ip = *ivec; GET_HALF_BLOCK(ocipherl, ip); GET_HALF_BLOCK(ocipherr, ip); /* * Now do this in earnest until we run out of length. */ - ip = (unsigned char *)in; - op = (unsigned char *)out; + ip = *in; + op = *out; for (;;) { /* check done inside loop */ /* * Read a block from the input into left and diff --git a/src/lib/des425/quad_cksum.c b/src/lib/des425/quad_cksum.c index cdf81c3f7..213e6a6e2 100644 --- a/src/lib/des425/quad_cksum.c +++ b/src/lib/des425/quad_cksum.c @@ -114,9 +114,9 @@ /* vax byte order is LSB first. This is not performance critical, and is far more readable this way. */ #define four_bytes_vax_to_nets(x) ((((((x[3]<<8)|x[2])<<8)|x[1])<<8)|x[0]) -#define vaxtohl(x) four_bytes_vax_to_nets(((unsigned char *)(x))) +#define vaxtohl(x) four_bytes_vax_to_nets(((const unsigned char *)(x))) #define two_bytes_vax_to_nets(x) ((x[1]<<8)|x[0]) -#define vaxtohs(x) two_bytes_vax_to_nets(((unsigned char *)(x))) +#define vaxtohs(x) two_bytes_vax_to_nets(((const unsigned char *)(x))) /* Externals */ extern char *errmsg(); @@ -129,7 +129,7 @@ extern int des_debug; KRB5_DLLIMP unsigned long KRB5_CALLCONV des_quad_cksum(in,out,length,out_count,c_seed) - unsigned char FAR *in; /* input block */ + const unsigned char FAR *in; /* input block */ unsigned DES_INT32 FAR *out; /* optional longer output */ long length; /* original length in bytes */ int out_count; /* number of iterations */ @@ -147,14 +147,14 @@ des_quad_cksum(in,out,length,out_count,c_seed) register unsigned DES_INT32 z2; register unsigned DES_INT32 x; register unsigned DES_INT32 x2; - register unsigned char *p; + const unsigned char *p; register DES_INT32 len; register int i; /* use all 8 bytes of seed */ z = vaxtohl(c_seed); - z2 = vaxtohl((char *)c_seed+4); + z2 = vaxtohl((const char *)c_seed+4); if (out == NULL) out_count = 1; /* default */ @@ -173,7 +173,7 @@ des_quad_cksum(in,out,length,out_count,c_seed) len -= 2; } else { - x = (z + *(unsigned char *)p++); + x = (z + *(const unsigned char *)p++); len = 0; } x2 = z2; diff --git a/src/lib/des425/str_to_key.c b/src/lib/des425/str_to_key.c index 448947125..139b10272 100644 --- a/src/lib/des425/str_to_key.c +++ b/src/lib/des425/str_to_key.c @@ -63,19 +63,18 @@ extern int mit_des_debug; */ KRB5_DLLIMP int KRB5_CALLCONV des_string_to_key(str,key) - char *str; + const char *str; register mit_des_cblock key; { - register char *in_str; + const char *in_str; register unsigned temp; register int j; - register long i, length; + unsigned long i, length; unsigned char *k_p; int forward; register char *p_char; char k_char[64]; mit_des_key_schedule key_sked; - extern void des_cbc_cksum(); in_str = str; forward = 1; @@ -88,7 +87,7 @@ des_string_to_key(str,key) #ifdef DEBUG if (mit_des_debug) fprintf(stdout, - "\n\ninput str length = %d string = %s\nstring = 0x ", + "\n\ninput str length = %ld string = %s\nstring = 0x ", length,str); #endif @@ -129,10 +128,11 @@ des_string_to_key(str,key) des_fixup_key_parity(key); /* Now one-way encrypt it with the folded key */ - (void) des_key_sched(key, *(Key_schedule *)&key_sked); - (void) des_cbc_cksum((des_cblock *)in_str,key,length,key_sked,key); + (void) des_key_sched(key, key_sked); + (void) des_cbc_cksum((const unsigned char *) in_str, key, length, + key_sked, key); /* erase key_sked */ - memset((char *)key_sked, 0,sizeof(key_sked)); + memset(key_sked, 0,sizeof(key_sked)); /* now fix up key parity again */ des_fixup_key_parity(key); diff --git a/src/lib/des425/t_pcbc.c b/src/lib/des425/t_pcbc.c index c6b63d44d..8bd6a08bc 100644 --- a/src/lib/des425/t_pcbc.c +++ b/src/lib/des425/t_pcbc.c @@ -96,7 +96,7 @@ main(argc,argv) des_key_sched (default_key, sked); /* This could lose on alignment... */ des_pcbc_encrypt ((des_cblock *)&tests[i].text, out, - strlen(tests[i].text) + 1, sked, ivec, 1); + strlen(tests[i].text) + 1, sked, &ivec, 1); printf ("pcbc_encrypt(\"%s\") = {", tests[i].text); jmax = (strlen (tests[i].text) + 8) & ~7U; for (j = 0; j < jmax; j++) { @@ -109,7 +109,7 @@ main(argc,argv) printf ("\n}\n"); /* reverse it */ - des_pcbc_encrypt (out, out2, jmax, sked, ivec, 0); + des_pcbc_encrypt (out, out2, jmax, sked, &ivec, 0); if (strcmp ((char *)out2, tests[i].text)) { printf ("decrypt failed\n"); wrong = 1; diff --git a/src/lib/des425/util.c b/src/lib/des425/util.c index 54545fcf0..2c5ef9216 100644 --- a/src/lib/des425/util.c +++ b/src/lib/des425/util.c @@ -20,7 +20,7 @@ void des_cblock_print_file(x, fp) des_cblock *x; FILE *fp; { - unsigned char *y = (unsigned char *) x; + unsigned char *y = *x; register int i = 0; fprintf(fp," 0x { "); -- 2.26.2