xref: /titanic_52/usr/src/lib/pkcs11/pkcs11_softtoken/common/softRand.c (revision 7b79d84636ec82b45f00c982cf6810db81852d17)
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 #include <errno.h>
277c478bd9Sstevel@tonic-gate #include <fcntl.h>
287c478bd9Sstevel@tonic-gate #include <sys/stat.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <security/cryptoki.h>
3119193bb6SDina K Nimeh #include <cryptoutil.h>
327c478bd9Sstevel@tonic-gate #include "softGlobal.h"
337c478bd9Sstevel@tonic-gate #include "softSession.h"
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate CK_RV
367c478bd9Sstevel@tonic-gate C_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen)
377c478bd9Sstevel@tonic-gate {
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 	CK_RV	rv;
407c478bd9Sstevel@tonic-gate 	soft_session_t	*session_p;
417c478bd9Sstevel@tonic-gate 	boolean_t	lock_held = B_FALSE;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate 	if (!softtoken_initialized)
447c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer just for validity check. */
477c478bd9Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
487c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
497c478bd9Sstevel@tonic-gate 		return (rv);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	SES_REFRELE(session_p, lock_held);
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate 	if ((pSeed == NULL) || (ulSeedLen == 0)) {
547c478bd9Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
557c478bd9Sstevel@tonic-gate 	}
567c478bd9Sstevel@tonic-gate 
57*7b79d846SDina K Nimeh 	if (pkcs11_seed_urandom(pSeed, ulSeedLen) < 0) {
587c478bd9Sstevel@tonic-gate 		if (errno == EACCES)
5919193bb6SDina K Nimeh 			return (CKR_RANDOM_SEED_NOT_SUPPORTED);
607c478bd9Sstevel@tonic-gate 		return (CKR_DEVICE_ERROR);
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 	return (CKR_OK);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate CK_RV
677c478bd9Sstevel@tonic-gate C_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData,
687c478bd9Sstevel@tonic-gate     CK_ULONG ulRandomLen)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	CK_RV	rv;
727c478bd9Sstevel@tonic-gate 	soft_session_t	*session_p;
737c478bd9Sstevel@tonic-gate 	boolean_t	lock_held = B_FALSE;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	if (!softtoken_initialized)
767c478bd9Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	/* Obtain the session pointer just for validity check. */
797c478bd9Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
807c478bd9Sstevel@tonic-gate 	if (rv != CKR_OK)
817c478bd9Sstevel@tonic-gate 		return (rv);
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	SES_REFRELE(session_p, lock_held);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	if ((pRandomData == NULL) || (ulRandomLen == 0)) {
867c478bd9Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
89*7b79d846SDina K Nimeh 	if (pkcs11_get_urandom(pRandomData, ulRandomLen) < 0)
90*7b79d846SDina K Nimeh 		return (CKR_DEVICE_ERROR);
91*7b79d846SDina K Nimeh 	return (CKR_OK);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate }
94