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>
27 #include "des_internal.h"
30 extern int des_debug_print();
31 extern void des_fixup_key_parity();
32 extern afs_uint32 des_cbc_cksum();
35 * convert an arbitrary length string to a DES key
38 des_string_to_key(str,key)
40 register des_cblock *key;
42 register char *in_str;
43 register unsigned temp,i,j;
44 register afs_int32 length;
47 register char *p_char;
49 des_key_schedule key_sked;
56 /* init key array for bits */
57 bzero(k_char,sizeof(k_char));
62 "\n\ninput str length = %d string = %s\nstring = 0x ",
66 /* get next 8 bytes, strip parity, xor */
67 for (i = 1; i <= length; i++) {
68 /* get next input key byte */
69 temp = (unsigned int) *str++;
72 fprintf(stdout,"%02x ",temp & 0xff);
74 /* loop through bits within byte, ignore parity */
75 for (j = 0; j <= 6; j++) {
77 *p_char++ ^= (int) temp & 01;
79 *--p_char ^= (int) temp & 01;
83 /* check and flip direction */
88 /* now stuff into the key des_cblock, and force odd parity */
90 k_p = (unsigned char *) key;
92 for (i = 0; i <= 7; i++) {
94 for (j = 0; j <= 6; j++)
95 temp |= *p_char++ << (1+j);
96 *k_p++ = (unsigned char) temp;
100 des_fixup_key_parity(key);
102 /* Now one-way encrypt it with the folded key */
103 (void) des_key_sched(key,key_sked);
104 (void) des_cbc_cksum((des_cblock *)in_str,key,length,key_sked,key);
106 bzero((char *)key_sked,sizeof(key_sked));
108 /* now fix up key parity again */
109 des_fixup_key_parity(key);
113 "\nResulting string_to_key = 0x%x 0x%x\n",
114 *((afs_uint32 *) key),
115 *((afs_uint32 *) key+1));