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>
24 #include <afsconfig.h>
25 #include <afs/param.h>
33 #include "des_internal.h"
34 #include "des_prototypes.h"
45 * convert an arbitrary length string to a DES key
48 des_string_to_key(char *str, register des_cblock * key)
50 register char *in_str;
51 register unsigned temp, i, j;
52 register afs_int32 length;
55 register char *p_char;
57 des_key_schedule key_sked;
64 /* init key array for bits */
65 memset(k_char, 0, sizeof(k_char));
70 "\n\ninput str length = %d string = %s\nstring = 0x ",
74 /* get next 8 bytes, strip parity, xor */
75 for (i = 1; i <= length; i++) {
76 /* get next input key byte */
77 temp = (unsigned int)*str++;
80 fprintf(stdout, "%02x ", temp & 0xff);
82 /* loop through bits within byte, ignore parity */
83 for (j = 0; j <= 6; j++) {
85 *p_char++ ^= (int)temp & 01;
87 *--p_char ^= (int)temp & 01;
91 /* check and flip direction */
96 /* now stuff into the key des_cblock, and force odd parity */
98 k_p = (unsigned char *)key;
100 for (i = 0; i <= 7; i++) {
102 for (j = 0; j <= 6; j++)
103 temp |= *p_char++ << (1 + j);
104 *k_p++ = (unsigned char)temp;
108 des_fixup_key_parity(cblockptr_to_cblock(key));
110 /* Now one-way encrypt it with the folded key */
111 des_key_sched(cblockptr_to_cblock(key), key_sked);
112 des_cbc_cksum(charptr_to_cblockptr(in_str), key, length, key_sked, key);
114 memset((char *)key_sked, 0, sizeof(key_sked));
116 /* now fix up key parity again */
117 des_fixup_key_parity(cblockptr_to_cblock(key));
120 fprintf(stdout, "\nResulting string_to_key = 0x%x 0x%x\n",
121 *((afs_uint32 *) key), *((afs_uint32 *) key + 1));