2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
7 * lib/crypto/des/string2key.c
9 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
10 * All Rights Reserved.
12 * Export of this software from the United States of America may
13 * require a specific license from the United States Government.
14 * It is the responsibility of any person or organization contemplating
15 * export to obtain such a license before exporting.
17 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
18 * distribute this software and its documentation for any purpose and
19 * without fee is hereby granted, provided that the above copyright
20 * notice appear in all copies and that both that copyright notice and
21 * this permission notice appear in supporting documentation, and that
22 * the name of M.I.T. not be used in advertising or publicity pertaining
23 * to distribution of the software without specific, written prior
24 * permission. M.I.T. makes no representations about the suitability of
25 * this software for any purpose. It is provided "as is" without express
26 * or implied warranty.
33 converts the string pointed to by "data" into an encryption key
34 of type "enctype". *keyblock is filled in with the key info;
35 in particular, keyblock->contents is to be set to allocated storage.
36 It is the responsibility of the caller to release this storage
37 when the generated key no longer needed.
39 The routine may use "salt" to seed or alter the conversion
42 If the particular function called does not know how to make a
43 key of type "enctype", an error may be returned.
49 mit_des_string_to_key_int (krb5_context context
,
50 krb5_keyblock
*keyblock
,
51 const krb5_data
*data
,
52 const krb5_data
*salt
)
54 krb5_error_code retval
= KRB5_PROG_ETYPE_NOSUPP
;
55 register char *str
, *copystr
;
56 register krb5_octet
*key
;
57 register unsigned temp
;
63 register char *p_char
;
67 #define min(A, B) ((A) < (B) ? (A): (B))
70 keyblock
->magic
= KV5M_KEYBLOCK
;
71 keyblock
->length
= sizeof(mit_des_cblock
);
72 key
= keyblock
->contents
;
75 && (salt
->length
== SALT_TYPE_AFS_LENGTH
76 /* XXX Yuck! Aren't we done with this yet? */
77 || salt
->length
== (unsigned) -1)) {
81 afssalt
.data
= salt
->data
;
82 at
= strchr(afssalt
.data
, '@');
85 afssalt
.length
= at
- afssalt
.data
;
87 afssalt
.length
= strlen(afssalt
.data
);
88 return mit_afs_string_to_key(context
, keyblock
, data
, &afssalt
);
91 length
= data
->length
+ (salt
? salt
->length
: 0);
93 copystr
= malloc((size_t) length
);
98 (void) memcpy(copystr
, (char *) data
->data
, data
->length
);
100 (void) memcpy(copystr
+ data
->length
, (char *)salt
->data
, salt
->length
);
102 /* convert to des key */
106 /* init key array for bits */
107 (void) memset(k_char
,0,sizeof(k_char
));
112 "\n\ninput str length = %d string = %*s\nstring = 0x ",
118 /* get next 8 bytes, strip parity, xor */
119 for (i
= 1; i
<= length
; i
++) {
120 /* get next input key byte */
121 temp
= (unsigned int) *str
++;
124 fprintf(stdout
,"%02x ",temp
& 0xff);
126 /* loop through bits within byte, ignore parity */
127 for (j
= 0; j
<= 6; j
++) {
129 *p_char
++ ^= (int) temp
& 01;
131 *--p_char
^= (int) temp
& 01;
135 /* check and flip direction */
140 /* now stuff into the key mit_des_cblock, and force odd parity */
142 k_p
= (unsigned char *) key
;
144 for (i
= 0; i
<= 7; i
++) {
146 for (j
= 0; j
<= 6; j
++)
147 temp
|= *p_char
++ << (1+j
);
148 *k_p
++ = (unsigned char) temp
;
152 mit_des_fixup_key_parity(key
);
153 if (mit_des_is_weak_key(key
))
154 ((krb5_octet
*)key
)[7] ^= 0xf0;
156 retval
= mit_des_cbc_cksum(context
, (unsigned char*)copystr
, key
,
157 length
, keyblock
, key
);
159 /* clean & free the input string */
160 (void) memset(copystr
, 0, (size_t) length
);
163 /* now fix up key parity again */
164 mit_des_fixup_key_parity(key
);
165 if (mit_des_is_weak_key(key
))
166 ((krb5_octet
*)key
)[7] ^= 0xf0;
169 * Because this routine actually modifies the original keyblock
170 * in place we cannot use the PKCS#11 key object handle created earlier.
171 * Destroy the existing object handle associated with the key,
172 * a correct handle will get created when the key is actually
173 * used for the first time.
175 if (keyblock
->hKey
!= CK_INVALID_HANDLE
) {
176 (void)C_DestroyObject(krb_ctx_hSession(context
), keyblock
->hKey
);
177 keyblock
->hKey
= CK_INVALID_HANDLE
;