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>
26 #include <afs/param.h>
28 #include "des_internal.h"
31 extern int des_debug_print();
32 extern void des_fixup_key_parity();
33 extern afs_uint32 des_cbc_cksum();
36 int des_key_sched(register des_cblock *k, des_key_schedule schedule);
39 * convert an arbitrary length string to a DES key
42 des_string_to_key(str,key)
44 register des_cblock *key;
46 register char *in_str;
47 register unsigned temp,i,j;
48 register afs_int32 length;
51 register char *p_char;
53 des_key_schedule key_sked;
60 /* init key array for bits */
61 bzero(k_char,sizeof(k_char));
66 "\n\ninput str length = %d string = %s\nstring = 0x ",
70 /* get next 8 bytes, strip parity, xor */
71 for (i = 1; i <= length; i++) {
72 /* get next input key byte */
73 temp = (unsigned int) *str++;
76 fprintf(stdout,"%02x ",temp & 0xff);
78 /* loop through bits within byte, ignore parity */
79 for (j = 0; j <= 6; j++) {
81 *p_char++ ^= (int) temp & 01;
83 *--p_char ^= (int) temp & 01;
87 /* check and flip direction */
92 /* now stuff into the key des_cblock, and force odd parity */
94 k_p = (unsigned char *) key;
96 for (i = 0; i <= 7; i++) {
98 for (j = 0; j <= 6; j++)
99 temp |= *p_char++ << (1+j);
100 *k_p++ = (unsigned char) temp;
104 des_fixup_key_parity(key);
106 /* Now one-way encrypt it with the folded key */
107 (void) des_key_sched(key,key_sked);
108 (void) des_cbc_cksum((des_cblock *)in_str,key,length,key_sked,key);
110 bzero((char *)key_sked,sizeof(key_sked));
112 /* now fix up key parity again */
113 des_fixup_key_parity(key);
117 "\nResulting string_to_key = 0x%x 0x%x\n",
118 *((afs_uint32 *) key),
119 *((afs_uint32 *) key+1));