b9dd7115d22b0ab9156091794862064029fa4a04
[krb5.git] / src / lib / crypto / des / string2key.c
1 /*
2  * lib/crypto/des/string2key.c
3  *
4  * Copyright 1990,1991 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
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.
11  * 
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.
22  */
23
24 #include "k5-int.h"
25 #include "des_int.h"
26
27 /*
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.
33
34         The routine may use "salt" to seed or alter the conversion
35         algorithm.
36
37         If the particular function called does not know how to make a
38         key of type "keytype", an error may be returned.
39
40         returns: errors
41  */
42
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;
50 {
51     register char *str, *copystr;
52     register krb5_octet *key;
53
54     register unsigned temp;
55     register long i;        
56     register int j;
57     register long length;
58     unsigned char *k_p;
59     int forward;
60     register char *p_char;
61     char k_char[64];
62     mit_des_key_schedule key_sked;
63
64 #define min(A, B) ((A) < (B) ? (A): (B))
65
66     if ( keytype != KEYTYPE_DES )
67         return (KRB5_PROG_KEYTYPE_NOSUPP);
68
69     if ( !(keyblock->contents = (krb5_octet *)malloc(sizeof(mit_des_cblock))) )
70         return(ENOMEM);
71
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;
77
78     if (salt)
79         length = data->length + salt->length;
80     else
81         length = data->length;
82
83     copystr = malloc((size_t) length);
84     if (!copystr) {
85         free(keyblock->contents);
86         keyblock->contents = 0;
87         return ENOMEM;
88     }
89
90     memcpy(copystr, (char *) data->data, data->length);
91     if (salt)
92         memcpy(copystr + data->length, (char *)salt->data, salt->length);
93
94     /* convert to des key */
95     forward = 1;
96     p_char = k_char;
97
98     /* init key array for bits */
99     memset(k_char,0,sizeof(k_char));
100
101 #if 0
102     if (mit_des_debug)
103         fprintf(stdout,
104                 "\n\ninput str length = %d  string = %*s\nstring = 0x ",
105                 length,length,str);
106 #endif
107
108     str = copystr;
109
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++;
114 #if 0
115         if (mit_des_debug)
116             fprintf(stdout,"%02x ",temp & 0xff);
117 #endif
118         /* loop through bits within byte, ignore parity */
119         for (j = 0; j <= 6; j++) {
120             if (forward)
121                 *p_char++ ^= (int) temp & 01;
122             else
123                 *--p_char ^= (int) temp & 01;
124             temp = temp >> 1;
125         }
126
127         /* check and flip direction */
128         if ((i%8) == 0)
129             forward = !forward;
130     }
131
132     /* now stuff into the key mit_des_cblock, and force odd parity */
133     p_char = k_char;
134     k_p = (unsigned char *) key;
135
136     for (i = 0; i <= 7; i++) {
137         temp = 0;
138         for (j = 0; j <= 6; j++)
139             temp |= *p_char++ << (1+j);
140         *k_p++ = (unsigned char) temp;
141     }
142
143     /* fix key parity */
144     mit_des_fixup_key_parity(key);
145
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);
149     /* erase key_sked */
150     memset((char *)key_sked, 0, sizeof(key_sked));
151
152     /* clean & free the input string */
153     memset(copystr, 0, (size_t) length);
154     krb5_xfree(copystr);
155
156     /* now fix up key parity again */
157     mit_des_fixup_key_parity(key);
158
159 #if 0
160     if (mit_des_debug)
161         fprintf(stdout,
162                 "\nResulting string_to_key = 0x%x 0x%x\n",
163                 *((unsigned long *) key),
164                 *((unsigned long *) key+1));
165 #endif
166     
167     return 0;
168 }