|
|
1.1 root 1: /*
2: * $Source: /afs/athena.mit.edu/astaff/project/kerberos/src/lib/des/RCS/string_to_key.c,v $
3: * $Author: jtkohl $
4: *
5: * Copyright 1985, 1986, 1987, 1988, 1989 by the Massachusetts Institute
6: * of Technology.
7: *
8: * For copying and distribution information, please see the file
9: * <mit-copyright.h>.
10: *
11: * These routines perform encryption and decryption using the DES
12: * private key algorithm, or else a subset of it-- fewer inner loops.
13: * (AUTH_DES_ITER defaults to 16, may be less.)
14: *
15: * Under U.S. law, this software may not be exported outside the US
16: * without license from the U.S. Commerce department.
17: *
18: * The key schedule is passed as an arg, as well as the cleartext or
19: * ciphertext. The cleartext and ciphertext should be in host order.
20: *
21: * These routines form the library interface to the DES facilities.
22: *
23: * spm 8/85 MIT project athena
24: */
25:
26: #ifndef lint
27: static char rcsid_string_to_key_c[] =
28: "$Id: string_to_key.c,v 4.11 90/01/02 13:46:38 jtkohl Exp $";
29: #endif lint
30:
31: #include <mit-copyright.h>
32: #include <stdio.h>
33: #include <des.h>
34: #include "des_internal.h"
35:
36: extern int des_debug;
37: extern int des_debug_print();
38: extern void des_fixup_key_parity();
39:
40: /*
41: * convert an arbitrary length string to a DES key
42: */
43: int
44: des_string_to_key(str,key)
45: char *str;
46: register des_cblock *key;
47: {
48: register char *in_str;
49: register unsigned temp,i;
50: register int j;
51: register long length;
52: static unsigned char *k_p;
53: static int forward;
54: register char *p_char;
55: static char k_char[64];
56: static des_key_schedule key_sked;
57: extern unsigned long des_cbc_cksum();
58:
59: in_str = str;
60: forward = 1;
61: p_char = k_char;
62: length = strlen(str);
63:
64: /* init key array for bits */
65: bzero(k_char,sizeof(k_char));
66:
67: #ifdef DEBUG
68: if (des_debug)
69: fprintf(stdout,
70: "\n\ninput str length = %d string = %s\nstring = 0x ",
71: length,str);
72: #endif
73:
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++;
78: #ifdef DEBUG
79: if (des_debug)
80: fprintf(stdout,"%02x ",temp & 0xff);
81: #endif
82: /* loop through bits within byte, ignore parity */
83: for (j = 0; j <= 6; j++) {
84: if (forward)
85: *p_char++ ^= (int) temp & 01;
86: else
87: *--p_char ^= (int) temp & 01;
88: temp = temp >> 1;
89: } while (--j > 0);
90:
91: /* check and flip direction */
92: if ((i%8) == 0)
93: forward = !forward;
94: }
95:
96: /* now stuff into the key des_cblock, and force odd parity */
97: p_char = k_char;
98: k_p = (unsigned char *) key;
99:
100: for (i = 0; i <= 7; i++) {
101: temp = 0;
102: for (j = 0; j <= 6; j++)
103: temp |= *p_char++ << (1+j);
104: *k_p++ = (unsigned char) temp;
105: }
106:
107: /* fix key parity */
108: des_fixup_key_parity(key);
109:
110: /* Now one-way encrypt it with the folded key */
111: (void) des_key_sched(key,key_sked);
112: (void) des_cbc_cksum((des_cblock *)in_str,key,length,key_sked,key);
113: /* erase key_sked */
114: bzero((char *)key_sked,sizeof(key_sked));
115:
116: /* now fix up key parity again */
117: des_fixup_key_parity(key);
118:
119: if (des_debug)
120: fprintf(stdout,
121: "\nResulting string_to_key = 0x%x 0x%x\n",
122: *((unsigned long *) key),
123: *((unsigned long *) key+1));
124: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.