Windows global stuff:
[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
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 #ifndef min
65 #define min(A, B) ((A) < (B) ? (A): (B))
66 #endif
67
68     if ( keytype != KEYTYPE_DES )
69         return (KRB5_PROG_KEYTYPE_NOSUPP);
70
71     if ( !(keyblock->contents = (krb5_octet *)malloc(sizeof(mit_des_cblock))) )
72         return(ENOMEM);
73
74     keyblock->magic = KV5M_KEYBLOCK;
75     keyblock->etype = eblock->crypto_entry->proto_enctype;
76     keyblock->keytype = KEYTYPE_DES;
77     keyblock->length = sizeof(mit_des_cblock);
78     key = keyblock->contents;
79
80     if (salt)
81         length = data->length + salt->length;
82     else
83         length = data->length;
84
85     copystr = malloc((size_t) length);
86     if (!copystr) {
87         free(keyblock->contents);
88         keyblock->contents = 0;
89         return ENOMEM;
90     }
91
92     memcpy(copystr, (char *) data->data, data->length);
93     if (salt)
94         memcpy(copystr + data->length, (char *)salt->data, salt->length);
95
96     /* convert to des key */
97     forward = 1;
98     p_char = k_char;
99
100     /* init key array for bits */
101     memset(k_char,0,sizeof(k_char));
102
103 #if 0
104     if (mit_des_debug)
105         fprintf(stdout,
106                 "\n\ninput str length = %d  string = %*s\nstring = 0x ",
107                 length,length,str);
108 #endif
109
110     str = copystr;
111
112     /* get next 8 bytes, strip parity, xor */
113     for (i = 1; i <= length; i++) {
114         /* get next input key byte */
115         temp = (unsigned int) *str++;
116 #if 0
117         if (mit_des_debug)
118             fprintf(stdout,"%02x ",temp & 0xff);
119 #endif
120         /* loop through bits within byte, ignore parity */
121         for (j = 0; j <= 6; j++) {
122             if (forward)
123                 *p_char++ ^= (int) temp & 01;
124             else
125                 *--p_char ^= (int) temp & 01;
126             temp = temp >> 1;
127         }
128
129         /* check and flip direction */
130         if ((i%8) == 0)
131             forward = !forward;
132     }
133
134     /* now stuff into the key mit_des_cblock, and force odd parity */
135     p_char = k_char;
136     k_p = (unsigned char *) key;
137
138     for (i = 0; i <= 7; i++) {
139         temp = 0;
140         for (j = 0; j <= 6; j++)
141             temp |= *p_char++ << (1+j);
142         *k_p++ = (unsigned char) temp;
143     }
144
145     /* fix key parity */
146     mit_des_fixup_key_parity(key);
147
148     /* Now one-way encrypt it with the folded key */
149     (void) mit_des_key_sched(key, key_sked);
150     (void) mit_des_cbc_cksum((krb5_octet *)copystr, key, length, key_sked, key);
151     /* erase key_sked */
152     memset((char *)key_sked, 0, sizeof(key_sked));
153
154     /* clean & free the input string */
155     memset(copystr, 0, (size_t) length);
156     krb5_xfree(copystr);
157
158     /* now fix up key parity again */
159     mit_des_fixup_key_parity(key);
160
161 #if 0
162     if (mit_des_debug)
163         fprintf(stdout,
164                 "\nResulting string_to_key = 0x%x 0x%x\n",
165                 *((unsigned long *) key),
166                 *((unsigned long *) key+1));
167 #endif
168     
169     return 0;
170 }