17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
58047c9fbSmcpowers  * Common Development and Distribution License (the "License").
68047c9fbSmcpowers  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2219193bb6SDina K Nimeh  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Random Number Generation Functions
287c478bd9Sstevel@tonic-gate  * (as defined in PKCS#11 spec section 11.15)
297c478bd9Sstevel@tonic-gate  *
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/stat.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate #include <errno.h>
367c478bd9Sstevel@tonic-gate #include <string.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "metaGlobal.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * meta_SeedRandom
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * Unlike most other metaslot functions, meta_SeedRandom does not distribute
447c478bd9Sstevel@tonic-gate  * the call to a specific provider. Rather, we assume that the /dev/urandom
457c478bd9Sstevel@tonic-gate  * implementation is a kCF consumer, and is pulling randomness from everywhere
467c478bd9Sstevel@tonic-gate  * it can. Thus, by seeding /dev/urandom we let kCF potentially do all the
477c478bd9Sstevel@tonic-gate  * work.
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  * NOTES:
507c478bd9Sstevel@tonic-gate  * 1) /dev/urandom vs. /dev/random... Unfortunately P11 does not allow app
517c478bd9Sstevel@tonic-gate  *    to request a "quality", so we'll just assume urandom is good enough.
527c478bd9Sstevel@tonic-gate  *    Concerned apps can pull hardcore randomness from specific places they
537c478bd9Sstevel@tonic-gate  *    trust (eg by checking for CKF_HW?)..
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate CK_RV
meta_SeedRandom(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pSeed,CK_ULONG ulSeedLen)577c478bd9Sstevel@tonic-gate meta_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed,
587c478bd9Sstevel@tonic-gate     CK_ULONG ulSeedLen)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	CK_RV rv;
617c478bd9Sstevel@tonic-gate 	meta_session_t *session;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	if (pSeed == NULL || ulSeedLen == 0)
647c478bd9Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	/* Just check handle for validity, we don't need it for anything. */
677c478bd9Sstevel@tonic-gate 	rv = meta_handle2session(hSession, &session);
687c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
697c478bd9Sstevel@tonic-gate 		return (rv);
707c478bd9Sstevel@tonic-gate 	REFRELEASE(session);
717c478bd9Sstevel@tonic-gate 
72*7b79d846SDina K Nimeh 	if (pkcs11_seed_urandom(pSeed, ulSeedLen) < 0) {
73*7b79d846SDina K Nimeh 		if (errno == EACCES)
74*7b79d846SDina K Nimeh 			return (CKR_RANDOM_SEED_NOT_SUPPORTED);
757c478bd9Sstevel@tonic-gate 		return (CKR_DEVICE_ERROR);
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 	return (CKR_OK);
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * meta_GenerateRandom
827c478bd9Sstevel@tonic-gate  *
837c478bd9Sstevel@tonic-gate  * Unlike most other metaslot functions, meta_GenerateRandom does not distribute
847c478bd9Sstevel@tonic-gate  * the call to a specific provider. Rather, we assume that the /dev/urandom
857c478bd9Sstevel@tonic-gate  * implementation is a kCF consumer, and is pulling randomness from everywhere
867c478bd9Sstevel@tonic-gate  * it can. Thus, by reading /dev/urandom we let kCF potentially do all the
877c478bd9Sstevel@tonic-gate  * work.
887c478bd9Sstevel@tonic-gate  *
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate CK_RV
meta_GenerateRandom(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pRandomData,CK_ULONG ulRandomLen)917c478bd9Sstevel@tonic-gate meta_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData,
927c478bd9Sstevel@tonic-gate     CK_ULONG ulRandomLen)
937c478bd9Sstevel@tonic-gate {
947c478bd9Sstevel@tonic-gate 	CK_RV rv;
957c478bd9Sstevel@tonic-gate 	meta_session_t *session;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if (pRandomData == NULL || ulRandomLen < 1)
987c478bd9Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/* Just check handle for validity, we don't need it for anything. */
1017c478bd9Sstevel@tonic-gate 	rv = meta_handle2session(hSession, &session);
1027c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
1037c478bd9Sstevel@tonic-gate 		return (rv);
1047c478bd9Sstevel@tonic-gate 	REFRELEASE(session);
1057c478bd9Sstevel@tonic-gate 
106*7b79d846SDina K Nimeh 	if (pkcs11_get_urandom(pRandomData, ulRandomLen) < 0) {
1077c478bd9Sstevel@tonic-gate 		return (CKR_DEVICE_ERROR);
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 	return (CKR_OK);
1107c478bd9Sstevel@tonic-gate }
111