xref: /onnv-gate/usr/src/lib/pkcs11/pkcs11_softtoken/common/softRand.c (revision 9127:39de79f2e5d5)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51920Smcpowers  * Common Development and Distribution License (the "License").
61920Smcpowers  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
228932SDina.Nimeh@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <errno.h>
270Sstevel@tonic-gate #include <fcntl.h>
280Sstevel@tonic-gate #include <sys/stat.h>
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <security/cryptoki.h>
318932SDina.Nimeh@Sun.COM #include <cryptoutil.h>
320Sstevel@tonic-gate #include "softGlobal.h"
330Sstevel@tonic-gate #include "softSession.h"
340Sstevel@tonic-gate 
350Sstevel@tonic-gate CK_RV
C_SeedRandom(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pSeed,CK_ULONG ulSeedLen)360Sstevel@tonic-gate C_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen)
370Sstevel@tonic-gate {
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 	CK_RV	rv;
400Sstevel@tonic-gate 	soft_session_t	*session_p;
410Sstevel@tonic-gate 	boolean_t	lock_held = B_FALSE;
420Sstevel@tonic-gate 
430Sstevel@tonic-gate 	if (!softtoken_initialized)
440Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	/* Obtain the session pointer just for validity check. */
470Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
480Sstevel@tonic-gate 	if (rv != CKR_OK)
490Sstevel@tonic-gate 		return (rv);
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 	SES_REFRELE(session_p, lock_held);
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	if ((pSeed == NULL) || (ulSeedLen == 0)) {
540Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
550Sstevel@tonic-gate 	}
560Sstevel@tonic-gate 
57*9127SDina.Nimeh@Sun.COM 	if (pkcs11_seed_urandom(pSeed, ulSeedLen) < 0) {
58*9127SDina.Nimeh@Sun.COM 		if (errno == EACCES)
59*9127SDina.Nimeh@Sun.COM 			return (CKR_RANDOM_SEED_NOT_SUPPORTED);
600Sstevel@tonic-gate 		return (CKR_DEVICE_ERROR);
610Sstevel@tonic-gate 	}
620Sstevel@tonic-gate 	return (CKR_OK);
630Sstevel@tonic-gate 
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
660Sstevel@tonic-gate CK_RV
C_GenerateRandom(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pRandomData,CK_ULONG ulRandomLen)670Sstevel@tonic-gate C_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData,
680Sstevel@tonic-gate     CK_ULONG ulRandomLen)
690Sstevel@tonic-gate {
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 	CK_RV	rv;
720Sstevel@tonic-gate 	soft_session_t	*session_p;
730Sstevel@tonic-gate 	boolean_t	lock_held = B_FALSE;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	if (!softtoken_initialized)
760Sstevel@tonic-gate 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	/* Obtain the session pointer just for validity check. */
790Sstevel@tonic-gate 	rv = handle2session(hSession, &session_p);
800Sstevel@tonic-gate 	if (rv != CKR_OK)
810Sstevel@tonic-gate 		return (rv);
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	SES_REFRELE(session_p, lock_held);
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	if ((pRandomData == NULL) || (ulRandomLen == 0)) {
860Sstevel@tonic-gate 		return (CKR_ARGUMENTS_BAD);
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate 
89*9127SDina.Nimeh@Sun.COM 	if (pkcs11_get_urandom(pRandomData, ulRandomLen) < 0)
90*9127SDina.Nimeh@Sun.COM 		return (CKR_DEVICE_ERROR);
91*9127SDina.Nimeh@Sun.COM 	return (CKR_OK);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate }
94