2 * Copyright 1985, 1986, 1987, 1988, 1989 by the Massachusetts Institute
5 * For copying and distribution information, please see the file
8 * These routines perform encryption and decryption using the DES
9 * private key algorithm, or else a subset of it-- fewer inner loops.
10 * (AUTH_DES_ITER defaults to 16, may be less.)
12 * Under U.S. law, this software may not be exported outside the US
13 * without license from the U.S. Commerce department.
15 * The key schedule is passed as an arg, as well as the cleartext or
16 * ciphertext. The cleartext and ciphertext should be in host order.
18 * These routines form the library interface to the DES facilities.
20 * spm 8/85 MIT project athena
23 #include <mit-cpyright.h>
25 #include <afs/param.h>
26 #include <afsconfig.h>
31 #include "des_internal.h"
32 #if defined(HAVE_STRINGS_H)
35 #if defined(HAVE_STRING_H)
40 extern int des_debug_print();
41 extern void des_fixup_key_parity();
42 extern afs_uint32 des_cbc_cksum();
45 int des_key_sched(register des_cblock *k, des_key_schedule schedule);
48 * convert an arbitrary length string to a DES key
51 des_string_to_key(str,key)
53 register des_cblock *key;
55 register char *in_str;
56 register unsigned temp,i,j;
57 register afs_int32 length;
60 register char *p_char;
62 des_key_schedule key_sked;
69 /* init key array for bits */
70 bzero(k_char,sizeof(k_char));
75 "\n\ninput str length = %d string = %s\nstring = 0x ",
79 /* get next 8 bytes, strip parity, xor */
80 for (i = 1; i <= length; i++) {
81 /* get next input key byte */
82 temp = (unsigned int) *str++;
85 fprintf(stdout,"%02x ",temp & 0xff);
87 /* loop through bits within byte, ignore parity */
88 for (j = 0; j <= 6; j++) {
90 *p_char++ ^= (int) temp & 01;
92 *--p_char ^= (int) temp & 01;
96 /* check and flip direction */
101 /* now stuff into the key des_cblock, and force odd parity */
103 k_p = (unsigned char *) key;
105 for (i = 0; i <= 7; i++) {
107 for (j = 0; j <= 6; j++)
108 temp |= *p_char++ << (1+j);
109 *k_p++ = (unsigned char) temp;
113 des_fixup_key_parity(key);
115 /* Now one-way encrypt it with the folded key */
116 (void) des_key_sched(key,key_sked);
117 (void) des_cbc_cksum((des_cblock *)in_str,key,length,key_sked,key);
119 bzero((char *)key_sked,sizeof(key_sked));
121 /* now fix up key parity again */
122 des_fixup_key_parity(key);
126 "\nResulting string_to_key = 0x%lx 0x%lx\n",
127 *((afs_uint32 *) key),
128 *((afs_uint32 *) key+1));