2 * lib/crypto/des/string2key.c
4 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. M.I.T. makes no representations about the suitability of
20 * this software for any purpose. It is provided "as is" without express
21 * or implied warranty.
28 converts the string pointed to by "data" into an encryption key
29 of type "keytype". *keyblock is filled in with the key info;
30 in particular, keyblock->contents is to be set to allocated storage.
31 It is the responsibility of the caller to release this storage
32 when the generated key no longer needed.
34 The routine may use "salt" to seed or alter the conversion
37 If the particular function called does not know how to make a
38 key of type "keytype", an error may be returned.
43 krb5_error_code INTERFACE
44 mit_des_string_to_key (eblock, keytype, keyblock, data, salt)
45 const krb5_encrypt_block FAR * eblock;
46 const krb5_keytype keytype;
47 krb5_keyblock FAR * keyblock;
48 const krb5_data FAR * data;
49 const krb5_data FAR * salt;
51 register char *str, *copystr;
52 register krb5_octet *key;
54 register unsigned temp;
60 register char *p_char;
62 mit_des_key_schedule key_sked;
64 #define min(A, B) ((A) < (B) ? (A): (B))
66 if ( keytype != KEYTYPE_DES )
67 return (KRB5_PROG_KEYTYPE_NOSUPP);
69 if ( !(keyblock->contents = (krb5_octet *)malloc(sizeof(mit_des_cblock))) )
72 keyblock->magic = KV5M_KEYBLOCK;
73 keyblock->etype = eblock->crypto_entry->proto_enctype;
74 keyblock->keytype = KEYTYPE_DES;
75 keyblock->length = sizeof(mit_des_cblock);
76 key = keyblock->contents;
79 length = data->length + salt->length;
81 length = data->length;
83 copystr = malloc((size_t) length);
85 free(keyblock->contents);
86 keyblock->contents = 0;
90 memcpy(copystr, (char *) data->data, data->length);
92 memcpy(copystr + data->length, (char *)salt->data, salt->length);
94 /* convert to des key */
98 /* init key array for bits */
99 memset(k_char,0,sizeof(k_char));
104 "\n\ninput str length = %d string = %*s\nstring = 0x ",
110 /* get next 8 bytes, strip parity, xor */
111 for (i = 1; i <= length; i++) {
112 /* get next input key byte */
113 temp = (unsigned int) *str++;
116 fprintf(stdout,"%02x ",temp & 0xff);
118 /* loop through bits within byte, ignore parity */
119 for (j = 0; j <= 6; j++) {
121 *p_char++ ^= (int) temp & 01;
123 *--p_char ^= (int) temp & 01;
127 /* check and flip direction */
132 /* now stuff into the key mit_des_cblock, and force odd parity */
134 k_p = (unsigned char *) key;
136 for (i = 0; i <= 7; i++) {
138 for (j = 0; j <= 6; j++)
139 temp |= *p_char++ << (1+j);
140 *k_p++ = (unsigned char) temp;
144 mit_des_fixup_key_parity(key);
146 /* Now one-way encrypt it with the folded key */
147 (void) mit_des_key_sched(key, key_sked);
148 (void) mit_des_cbc_cksum((krb5_octet *)copystr, key, length, key_sked, key);
150 memset((char *)key_sked, 0, sizeof(key_sked));
152 /* clean & free the input string */
153 memset(copystr, 0, (size_t) length);
156 /* now fix up key parity again */
157 mit_des_fixup_key_parity(key);
162 "\nResulting string_to_key = 0x%x 0x%x\n",
163 *((unsigned long *) key),
164 *((unsigned long *) key+1));