xref: /illumos-gate/usr/src/uts/common/gssapi/mechs/krb5/crypto/prng.c (revision 2d6eb4a5e0a47d30189497241345dc5466bb68ab)
17c478bd9Sstevel@tonic-gate /*
2*505d05c7Sgtb  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (C) 1998 by the FundsXpress, INC.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * All rights reserved.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * Export of this software from the United States of America may require
127c478bd9Sstevel@tonic-gate  * a specific license from the United States Government.  It is the
137c478bd9Sstevel@tonic-gate  * responsibility of any person or organization contemplating export to
147c478bd9Sstevel@tonic-gate  * obtain such a license before exporting.
157c478bd9Sstevel@tonic-gate  *
167c478bd9Sstevel@tonic-gate  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
177c478bd9Sstevel@tonic-gate  * distribute this software and its documentation for any purpose and
187c478bd9Sstevel@tonic-gate  * without fee is hereby granted, provided that the above copyright
197c478bd9Sstevel@tonic-gate  * notice appear in all copies and that both that copyright notice and
207c478bd9Sstevel@tonic-gate  * this permission notice appear in supporting documentation, and that
217c478bd9Sstevel@tonic-gate  * the name of FundsXpress. not be used in advertising or publicity pertaining
227c478bd9Sstevel@tonic-gate  * to distribution of the software without specific, written prior
237c478bd9Sstevel@tonic-gate  * permission.  FundsXpress makes no representations about the suitability of
247c478bd9Sstevel@tonic-gate  * this software for any purpose.  It is provided "as is" without express
257c478bd9Sstevel@tonic-gate  * or implied warranty.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <k5-int.h>
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #ifdef _KERNEL
317c478bd9Sstevel@tonic-gate #include <sys/random.h>
327c478bd9Sstevel@tonic-gate #endif
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #ifndef _KERNEL
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * Solaris kerberos:  we don't need a random number generator
387c478bd9Sstevel@tonic-gate  * for the /dev/[u]random, as it uses entropy in the kernel.
397c478bd9Sstevel@tonic-gate  * Keep this function as some apps might call it directly.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*ARGSUSED*/
43*505d05c7Sgtb krb5_error_code KRB5_CALLCONV
krb5_c_random_seed(krb5_context context,krb5_data * data)447c478bd9Sstevel@tonic-gate krb5_c_random_seed(krb5_context context, krb5_data *data)
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	/*
477c478bd9Sstevel@tonic-gate 	 * We can't do much if this fails, so ignore the
487c478bd9Sstevel@tonic-gate 	 * return code.  /dev/urandom has its own entropy
497c478bd9Sstevel@tonic-gate 	 * source, so seeding it from here is of questionable
507c478bd9Sstevel@tonic-gate 	 * value in the first place.
517c478bd9Sstevel@tonic-gate 	 */
527c478bd9Sstevel@tonic-gate 	(void) C_SeedRandom(krb_ctx_hSession(context),
537c478bd9Sstevel@tonic-gate 		(CK_BYTE_PTR)data->data,
547c478bd9Sstevel@tonic-gate 		(CK_ULONG)data->length);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	return(0);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate #endif /* !_KERNEL */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * krb5_get_random_octets
627c478bd9Sstevel@tonic-gate  *   New for Solaris 9.  This routine takes advantage of the new
637c478bd9Sstevel@tonic-gate  * /dev/[u]random interface provided in Solaris 9 for getting random
647c478bd9Sstevel@tonic-gate  * bytes generated from the kernel.  The entropy produced there is generally
657c478bd9Sstevel@tonic-gate  * considered better than the current MIT PRNG code that we are replacing.
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * This func is visible so that it can be used to generate a
687c478bd9Sstevel@tonic-gate  * random confounder.
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #ifndef _KERNEL
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #endif /* ! _KERNEL */
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * We can assume that the memory for data is already malloc'd.
777c478bd9Sstevel@tonic-gate  * Return an error if there is an error, but don't clear the data->length
787c478bd9Sstevel@tonic-gate  * or free data->data.  This will be done by the calling function.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*ARGSUSED*/
82*505d05c7Sgtb krb5_error_code KRB5_CALLCONV
krb5_c_random_make_octets(krb5_context context,krb5_data * data)837c478bd9Sstevel@tonic-gate krb5_c_random_make_octets(krb5_context context, krb5_data *data)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate /*
867c478bd9Sstevel@tonic-gate  * Solaris kerberos uses /dev/[u]random
877c478bd9Sstevel@tonic-gate  */
887c478bd9Sstevel@tonic-gate #ifndef _KERNEL /* User space code */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate     krb5_error_code err = 0;
917c478bd9Sstevel@tonic-gate     CK_RV rv;
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate     KRB5_LOG0(KRB5_INFO, "krb5_c_random_make_octets() start, user space using "
947c478bd9Sstevel@tonic-gate 	"krb5_get_random_octets()\n");
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate     rv = C_GenerateRandom(krb_ctx_hSession(context), (CK_BYTE_PTR)data->data,
977c478bd9Sstevel@tonic-gate 		(CK_ULONG)data->length);
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate     if (rv != CKR_OK) {
1007c478bd9Sstevel@tonic-gate 	KRB5_LOG(KRB5_ERR, "C_GenerateRandom failed in "
1017c478bd9Sstevel@tonic-gate 		"krb5_c_random_make_octets: rv = 0x%x.", rv);
1027c478bd9Sstevel@tonic-gate 	err = PKCS_ERR;
1037c478bd9Sstevel@tonic-gate     }
1047c478bd9Sstevel@tonic-gate     if (err != 0) {
1057c478bd9Sstevel@tonic-gate 	KRB5_LOG0(KRB5_ERR, "krb5_c_random_make_octets() end, error");
1067c478bd9Sstevel@tonic-gate 	return (err);
1077c478bd9Sstevel@tonic-gate     }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #else  /* Kernel code section */
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate     /*
1127c478bd9Sstevel@tonic-gate      * Solaris Kerberos: for kernel code we use the randomness generator native
1137c478bd9Sstevel@tonic-gate      * to Solaris 9.  We avoid global variables and other nastiness this way.
1147c478bd9Sstevel@tonic-gate      *
1157c478bd9Sstevel@tonic-gate      * Using random_get_pseudo_bytes() instead of random_get_bytes() because it
1167c478bd9Sstevel@tonic-gate      * will not return an error code if there isn't enough entropy but will use
1177c478bd9Sstevel@tonic-gate      * a pseudo random algorithm to produce randomness.  Most of the time it
1187c478bd9Sstevel@tonic-gate      * should be as good as random_get_bytes() and we don't have to worry about
1197c478bd9Sstevel@tonic-gate      * dealing with a non-fatal error.
1207c478bd9Sstevel@tonic-gate      */
1217c478bd9Sstevel@tonic-gate     KRB5_LOG0(KRB5_INFO, "krb5_c_random_make_octets() start, kernel using "
1227c478bd9Sstevel@tonic-gate 	    "random_get_pseudo_bytes()\n ");
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate     if(random_get_pseudo_bytes((uint8_t *)data->data, data->length) != 0) {
1257c478bd9Sstevel@tonic-gate 	KRB5_LOG0(KRB5_ERR, "krb5_c_random_make_octets() end, "
1267c478bd9Sstevel@tonic-gate 		"random_get_pseudo_bytes() error.\n");
1277c478bd9Sstevel@tonic-gate 	return(KRB5_CRYPTO_INTERNAL);
1287c478bd9Sstevel@tonic-gate     }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate #endif /* !_KERNEL */
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate     KRB5_LOG0(KRB5_INFO, "krb5_c_random_make_octets() end\n");
1337c478bd9Sstevel@tonic-gate     return(0);
1347c478bd9Sstevel@tonic-gate }
135