Print this page
3882 remove xmod & friends
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/gss_mechs/mech_krb5/crypto/des/string2key.c
+++ new/usr/src/lib/gss_mechs/mech_krb5/crypto/des/string2key.c
1 1 /*
2 2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 3 * Use is subject to license terms.
4 4 */
5 5
6 6 #pragma ident "%Z%%M% %I% %E% SMI"
7 7
8 8 /*
9 9 * lib/crypto/des/string2key.c
10 10 *
11 11 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
12 12 * All Rights Reserved.
13 13 *
14 14 * Export of this software from the United States of America may
15 15 * require a specific license from the United States Government.
16 16 * It is the responsibility of any person or organization contemplating
17 17 * export to obtain such a license before exporting.
18 18 *
19 19 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
20 20 * distribute this software and its documentation for any purpose and
21 21 * without fee is hereby granted, provided that the above copyright
22 22 * notice appear in all copies and that both that copyright notice and
23 23 * this permission notice appear in supporting documentation, and that
24 24 * the name of M.I.T. not be used in advertising or publicity pertaining
25 25 * to distribution of the software without specific, written prior
26 26 * permission. M.I.T. makes no representations about the suitability of
27 27 * this software for any purpose. It is provided "as is" without express
28 28 * or implied warranty.
29 29 */
30 30
31 31 #include <k5-int.h>
32 32 #include <des_int.h>
33 33
34 34 /*
35 35 converts the string pointed to by "data" into an encryption key
36 36 of type "enctype". *keyblock is filled in with the key info;
37 37 in particular, keyblock->contents is to be set to allocated storage.
38 38 It is the responsibility of the caller to release this storage
39 39 when the generated key no longer needed.
40 40
41 41 The routine may use "salt" to seed or alter the conversion
42 42 algorithm.
43 43
44 44 If the particular function called does not know how to make a
45 45 key of type "enctype", an error may be returned.
46 46
↓ open down ↓ |
46 lines elided |
↑ open up ↑ |
47 47 returns: errors
48 48 */
49 49
50 50 krb5_error_code
51 51 mit_des_string_to_key_int (krb5_context context,
52 52 krb5_keyblock *keyblock,
53 53 const krb5_data *data,
54 54 const krb5_data *salt)
55 55 {
56 56 krb5_error_code retval = KRB5_PROG_ETYPE_NOSUPP;
57 -/* EXPORT DELETE START */
58 57 register char *str, *copystr;
59 58 register krb5_octet *key;
60 59 register unsigned temp;
61 60 register long i;
62 61 register int j;
63 62 register long length;
64 63 unsigned char *k_p;
65 64 int forward;
66 65 register char *p_char;
67 66 char k_char[64];
68 67
69 68 #ifndef min
70 69 #define min(A, B) ((A) < (B) ? (A): (B))
71 70 #endif
72 71
73 72 keyblock->magic = KV5M_KEYBLOCK;
74 73 keyblock->length = sizeof(mit_des_cblock);
75 74 key = keyblock->contents;
76 75
77 76 if (salt
78 77 && (salt->length == SALT_TYPE_AFS_LENGTH
79 78 /* XXX Yuck! Aren't we done with this yet? */
80 79 || salt->length == (unsigned) -1)) {
81 80 krb5_data afssalt;
82 81 char *at;
83 82
84 83 afssalt.data = salt->data;
85 84 at = strchr(afssalt.data, '@');
86 85 if (at) {
87 86 *at = 0;
88 87 afssalt.length = at - afssalt.data;
89 88 } else
90 89 afssalt.length = strlen(afssalt.data);
91 90 return mit_afs_string_to_key(context, keyblock, data, &afssalt);
92 91 }
93 92
94 93 length = data->length + (salt ? salt->length : 0);
95 94
96 95 copystr = malloc((size_t) length);
97 96 if (!copystr) {
98 97 return ENOMEM;
99 98 }
100 99
101 100 (void) memcpy(copystr, (char *) data->data, data->length);
102 101 if (salt)
103 102 (void) memcpy(copystr + data->length, (char *)salt->data, salt->length);
104 103
105 104 /* convert to des key */
106 105 forward = 1;
107 106 p_char = k_char;
108 107
109 108 /* init key array for bits */
110 109 (void) memset(k_char,0,sizeof(k_char));
111 110
112 111 #if 0
113 112 if (mit_des_debug)
114 113 fprintf(stdout,
115 114 "\n\ninput str length = %d string = %*s\nstring = 0x ",
116 115 length,length,str);
117 116 #endif
118 117
119 118 str = copystr;
120 119
121 120 /* get next 8 bytes, strip parity, xor */
122 121 for (i = 1; i <= length; i++) {
123 122 /* get next input key byte */
124 123 temp = (unsigned int) *str++;
125 124 #if 0
126 125 if (mit_des_debug)
127 126 fprintf(stdout,"%02x ",temp & 0xff);
128 127 #endif
129 128 /* loop through bits within byte, ignore parity */
130 129 for (j = 0; j <= 6; j++) {
131 130 if (forward)
132 131 *p_char++ ^= (int) temp & 01;
133 132 else
134 133 *--p_char ^= (int) temp & 01;
135 134 temp = temp >> 1;
136 135 }
137 136
138 137 /* check and flip direction */
139 138 if ((i%8) == 0)
140 139 forward = !forward;
141 140 }
142 141
143 142 /* now stuff into the key mit_des_cblock, and force odd parity */
144 143 p_char = k_char;
145 144 k_p = (unsigned char *) key;
146 145
147 146 for (i = 0; i <= 7; i++) {
148 147 temp = 0;
149 148 for (j = 0; j <= 6; j++)
150 149 temp |= *p_char++ << (1+j);
151 150 *k_p++ = (unsigned char) temp;
152 151 }
153 152
154 153 /* fix key parity */
155 154 mit_des_fixup_key_parity(key);
156 155 if (mit_des_is_weak_key(key))
157 156 ((krb5_octet *)key)[7] ^= 0xf0;
158 157
159 158 retval = mit_des_cbc_cksum(context, (unsigned char*)copystr, key,
160 159 length, keyblock, key);
161 160
162 161 /* clean & free the input string */
163 162 (void) memset(copystr, 0, (size_t) length);
164 163 krb5_xfree(copystr);
165 164
166 165 /* now fix up key parity again */
167 166 mit_des_fixup_key_parity(key);
168 167 if (mit_des_is_weak_key(key))
169 168 ((krb5_octet *)key)[7] ^= 0xf0;
170 169
171 170 /*
172 171 * Because this routine actually modifies the original keyblock
↓ open down ↓ |
105 lines elided |
↑ open up ↑ |
173 172 * in place we cannot use the PKCS#11 key object handle created earlier.
174 173 * Destroy the existing object handle associated with the key,
175 174 * a correct handle will get created when the key is actually
176 175 * used for the first time.
177 176 */
178 177 if (keyblock->hKey != CK_INVALID_HANDLE) {
179 178 (void)C_DestroyObject(krb_ctx_hSession(context), keyblock->hKey);
180 179 keyblock->hKey = CK_INVALID_HANDLE;
181 180 }
182 181
183 -/* EXPORT DELETE END */
184 182 return retval;
185 183 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX