1 /* 2 * Copyright 2005 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 * Copyright (C) 1998 by the FundsXpress, INC. 10 * 11 * All rights reserved. 12 * 13 * Export of this software from the United States of America may require 14 * a specific license from the United States Government. It is the 15 * responsibility of any person or organization contemplating export to 16 * obtain such a license before exporting. 17 * 18 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 19 * distribute this software and its documentation for any purpose and 20 * without fee is hereby granted, provided that the above copyright 21 * notice appear in all copies and that both that copyright notice and 22 * this permission notice appear in supporting documentation, and that 23 * the name of FundsXpress. not be used in advertising or publicity pertaining 24 * to distribution of the software without specific, written prior 25 * permission. FundsXpress makes no representations about the suitability of 26 * this software for any purpose. It is provided "as is" without express 27 * or implied warranty. 28 * 29 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 30 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 31 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 32 */ 33 34 #include <k5-int.h> 35 #include <etypes.h> 36 37 krb5_error_code KRB5_CALLCONV 38 krb5_c_make_random_key(krb5_context context, krb5_enctype enctype, 39 krb5_keyblock *random_key) 40 { 41 int i; 42 krb5_error_code ret; 43 const struct krb5_enc_provider *enc; 44 size_t keybytes, keylength; 45 krb5_data random_data; 46 unsigned char *bytes; 47 48 for (i=0; i<krb5_enctypes_length; i++) { 49 if (krb5_enctypes_list[i].etype == enctype) 50 break; 51 } 52 53 if (i == krb5_enctypes_length) 54 return(KRB5_BAD_ENCTYPE); 55 56 enc = krb5_enctypes_list[i].enc; 57 58 keybytes = enc->keybytes; 59 keylength = enc->keylength; 60 61 if ((bytes = (unsigned char *) malloc(keybytes)) == NULL) 62 return(ENOMEM); 63 if ((random_key->contents = (krb5_octet *) malloc(keylength)) == NULL) { 64 free(bytes); 65 return(ENOMEM); 66 } 67 68 random_data.data = (char *) bytes; 69 random_data.length = keybytes; 70 71 if ((ret = krb5_c_random_make_octets(context, &random_data))) 72 goto cleanup; 73 74 random_key->magic = KV5M_KEYBLOCK; 75 random_key->enctype = enctype; 76 random_key->length = keylength; 77 random_key->dk_list = NULL; 78 #ifdef _KERNEL 79 random_key->kef_key = NULL; 80 #else 81 random_key->hKey = CK_INVALID_HANDLE; 82 #endif 83 84 ret = ((*(enc->make_key))(context, &random_data, random_key)); 85 86 cleanup: 87 memset(bytes, 0, keybytes); 88 free(bytes); 89 90 if (ret) { 91 memset(random_key->contents, 0, keylength); 92 free(random_key->contents); 93 random_key->contents = NULL; 94 } 95 96 return(ret); 97 } 98