1 /* 2 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 #pragma ident "%Z%%M% %I% %E% SMI" 7 8 /* 9 * lib/crypto/des/string2key.c 10 * 11 * Copyright 1990,1991 by the Massachusetts Institute of Technology. 12 * All Rights Reserved. 13 * 14 * Export of this software from the United States of America may 15 * require a specific license from the United States Government. 16 * It is the responsibility of any person or organization contemplating 17 * export to obtain such a license before exporting. 18 * 19 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 20 * distribute this software and its documentation for any purpose and 21 * without fee is hereby granted, provided that the above copyright 22 * notice appear in all copies and that both that copyright notice and 23 * this permission notice appear in supporting documentation, and that 24 * the name of M.I.T. not be used in advertising or publicity pertaining 25 * to distribution of the software without specific, written prior 26 * permission. M.I.T. makes no representations about the suitability of 27 * this software for any purpose. It is provided "as is" without express 28 * or implied warranty. 29 */ 30 31 #include <k5-int.h> 32 #include <des_int.h> 33 34 /* 35 converts the string pointed to by "data" into an encryption key 36 of type "enctype". *keyblock is filled in with the key info; 37 in particular, keyblock->contents is to be set to allocated storage. 38 It is the responsibility of the caller to release this storage 39 when the generated key no longer needed. 40 41 The routine may use "salt" to seed or alter the conversion 42 algorithm. 43 44 If the particular function called does not know how to make a 45 key of type "enctype", an error may be returned. 46 47 returns: errors 48 */ 49 50 krb5_error_code 51 mit_des_string_to_key_int (krb5_context context, 52 krb5_keyblock *keyblock, 53 const krb5_data *data, 54 const krb5_data *salt) 55 { 56 krb5_error_code retval = KRB5_PROG_ETYPE_NOSUPP; 57 /* EXPORT DELETE START */ 58 register char *str, *copystr; 59 register krb5_octet *key; 60 register unsigned temp; 61 register long i; 62 register int j; 63 register long length; 64 unsigned char *k_p; 65 int forward; 66 register char *p_char; 67 char k_char[64]; 68 69 #ifndef min 70 #define min(A, B) ((A) < (B) ? (A): (B)) 71 #endif 72 73 keyblock->magic = KV5M_KEYBLOCK; 74 keyblock->length = sizeof(mit_des_cblock); 75 key = keyblock->contents; 76 77 if (salt) { 78 if (salt->length == -1) { 79 /* cheat and do AFS string2key instead */ 80 return mit_afs_string_to_key (context, keyblock, data, salt); 81 } else 82 length = data->length + salt->length; 83 } 84 else 85 length = data->length; 86 87 copystr = malloc((size_t) length); 88 if (!copystr) { 89 return ENOMEM; 90 } 91 92 (void) memcpy(copystr, (char *) data->data, data->length); 93 if (salt) 94 (void) 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 (void) 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 if (mit_des_is_weak_key(key)) 148 ((krb5_octet *)key)[7] ^= 0xf0; 149 150 retval = mit_des_cbc_cksum(context, (unsigned char*)copystr, key, 151 length, keyblock, key); 152 153 /* clean & free the input string */ 154 (void) memset(copystr, 0, (size_t) length); 155 krb5_xfree(copystr); 156 157 /* now fix up key parity again */ 158 mit_des_fixup_key_parity(key); 159 if (mit_des_is_weak_key(key)) 160 ((krb5_octet *)key)[7] ^= 0xf0; 161 162 /* 163 * Because this routine actually modifies the original keyblock 164 * in place we cannot use the PKCS#11 key object handle created earlier. 165 * Destroy the existing object handle associated with the key, 166 * a correct handle will get created when the key is actually 167 * used for the first time. 168 */ 169 if (keyblock->hKey != CK_INVALID_HANDLE) { 170 (void)C_DestroyObject(krb_ctx_hSession(context), keyblock->hKey); 171 keyblock->hKey = CK_INVALID_HANDLE; 172 } 173 174 /* EXPORT DELETE END */ 175 return retval; 176 } 177