summaryrefslogtreecommitdiffstats
path: root/src/lib/des425/str_to_key.c
diff options
context:
space:
mode:
authorJohn Kohl <jtkohl@mit.edu>1991-03-14 10:00:20 +0000
committerJohn Kohl <jtkohl@mit.edu>1991-03-14 10:00:20 +0000
commit0d617fc8545f08c44c2a6aef482097fc7287a65f (patch)
treee5f5b5a0875c186a864258d21adcd462a3cfb8c0 /src/lib/des425/str_to_key.c
parentb79fbdc3e1d570e26053285530894ecf6935685b (diff)
downloadkrb5-0d617fc8545f08c44c2a6aef482097fc7287a65f.tar.gz
krb5-0d617fc8545f08c44c2a6aef482097fc7287a65f.tar.xz
krb5-0d617fc8545f08c44c2a6aef482097fc7287a65f.zip
*** empty log message ***
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@1890 dc483132-0cff-0310-8789-dd5450dbe970
Diffstat (limited to 'src/lib/des425/str_to_key.c')
-rw-r--r--src/lib/des425/str_to_key.c131
1 files changed, 131 insertions, 0 deletions
diff --git a/src/lib/des425/str_to_key.c b/src/lib/des425/str_to_key.c
new file mode 100644
index 0000000000..71bfe83159
--- /dev/null
+++ b/src/lib/des425/str_to_key.c
@@ -0,0 +1,131 @@
+/*
+ * $Source$
+ * $Author$
+ *
+ * Copyright 1985, 1986, 1987, 1988, 1989,1990 by the Massachusetts Institute
+ * of Technology.
+ * All Rights Reserved.
+ *
+ * For copying and distribution information, please see the file
+ * <krb5/copyright.h>.
+ *
+ * These routines perform encryption and decryption using the DES
+ * private key algorithm, or else a subset of it-- fewer inner loops.
+ * (AUTH_DES_ITER defaults to 16, may be less.)
+ *
+ * Under U.S. law, this software may not be exported outside the US
+ * without license from the U.S. Commerce department.
+ *
+ * The key schedule is passed as an arg, as well as the cleartext or
+ * ciphertext. The cleartext and ciphertext should be in host order.
+ *
+ * These routines form the library interface to the DES facilities.
+ *
+ * spm 8/85 MIT project athena
+ */
+
+#ifndef lint
+static char rcsid_string_to_key_c[] =
+"$Id$";
+#endif /* lint */
+
+#include <stdio.h>
+#include <string.h>
+#include "des.h"
+/* #include "des_internal.h" */
+
+extern int mit_des_debug;
+extern int mit_des_debug_print();
+extern void mit_des_fixup_key_parity();
+
+/*
+ * convert an arbitrary length string to a DES key
+ */
+int
+des_string_to_key(str,key)
+ char *str;
+ register mit_des_cblock *key;
+{
+ register char *in_str;
+ register unsigned temp,i;
+ register int j;
+ register long length;
+ static unsigned char *k_p;
+ static int forward;
+ register char *p_char;
+ static char k_char[64];
+ static mit_des_key_schedule key_sked;
+ extern unsigned long des_cbc_cksum();
+
+ in_str = str;
+ forward = 1;
+ p_char = k_char;
+ length = strlen(str);
+
+ /* init key array for bits */
+ bzero(k_char,sizeof(k_char));
+
+#ifdef DEBUG
+ if (mit_des_debug)
+ fprintf(stdout,
+ "\n\ninput str length = %d string = %s\nstring = 0x ",
+ length,str);
+#endif
+
+ /* get next 8 bytes, strip parity, xor */
+ for (i = 1; i <= length; i++) {
+ /* get next input key byte */
+ temp = (unsigned int) *str++;
+#ifdef DEBUG
+ if (mit_des_debug)
+ fprintf(stdout,"%02x ",temp & 0xff);
+#endif
+ /* loop through bits within byte, ignore parity */
+ for (j = 0; j <= 6; j++) {
+ if (forward)
+ *p_char++ ^= (int) temp & 01;
+ else
+ *--p_char ^= (int) temp & 01;
+ temp = temp >> 1;
+ } while (--j > 0);
+
+ /* check and flip direction */
+ if ((i%8) == 0)
+ forward = !forward;
+ }
+
+ /* now stuff into the key des_cblock, and force odd parity */
+ p_char = k_char;
+ k_p = (unsigned char *) key;
+
+ for (i = 0; i <= 7; i++) {
+ temp = 0;
+ for (j = 0; j <= 6; j++)
+ temp |= *p_char++ << (1+j);
+ *k_p++ = (unsigned char) temp;
+ }
+
+ /* fix key parity */
+ des_fixup_key_parity(key);
+
+ /* Now one-way encrypt it with the folded key */
+ (void) des_key_sched(key,key_sked);
+ (void) des_cbc_cksum((des_cblock *)in_str,key,length,key_sked,key);
+ /* erase key_sked */
+ bzero((char *)key_sked,sizeof(key_sked));
+
+ /* now fix up key parity again */
+ des_fixup_key_parity(key);
+
+#ifdef DEBUG
+ if (mit_des_debug)
+ fprintf(stdout,
+ "\nResulting string_to_key = 0x%x 0x%x\n",
+ *((unsigned long *) key),
+ *((unsigned long *) key+1));
+#endif /* DEBUG */
+ return 0; /* Really should be returning void, */
+ /* but the original spec was for it to */
+ /* return an int, and ANSI compilers */
+ /* can do dumb things sometimes */
+}