xref: /titanic_44/usr/src/lib/pkcs11/pkcs11_softtoken/common/softRand.c (revision dfb96a4f56fb431b915bc67e5d9d5c8d4f4f6679)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <security/cryptoki.h>
33 #include "softGlobal.h"
34 #include "softRandom.h"
35 #include "softSession.h"
36 
37 CK_RV
38 C_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen)
39 {
40 
41 	CK_RV	rv;
42 	soft_session_t	*session_p;
43 	boolean_t	lock_held = B_FALSE;
44 	long		nwrite;
45 
46 	if (!softtoken_initialized)
47 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
48 
49 	/* Obtain the session pointer just for validity check. */
50 	rv = handle2session(hSession, &session_p);
51 	if (rv != CKR_OK)
52 		return (rv);
53 
54 	SES_REFRELE(session_p, lock_held);
55 
56 	if ((pSeed == NULL) || (ulSeedLen == 0)) {
57 		return (CKR_ARGUMENTS_BAD);
58 	}
59 
60 	if (soft_urandom_seed_fd < 0) {
61 		(void) pthread_mutex_lock(&soft_giant_mutex);
62 			/* Check again holding the mutex */
63 			if (soft_urandom_seed_fd < 0) {
64 				while ((soft_urandom_seed_fd = open(DEV_URANDOM,
65 				    O_WRONLY)) < 0) {
66 					if (errno != EINTR)
67 						break;
68 				}
69 				if (soft_urandom_seed_fd < 0) {
70 					(void) pthread_mutex_unlock(
71 					    &soft_giant_mutex);
72 					if (errno == EACCES)
73 						return (
74 						CKR_RANDOM_SEED_NOT_SUPPORTED);
75 					return (CKR_DEVICE_ERROR);
76 				}
77 				(void) fcntl(soft_urandom_seed_fd, F_SETFD,
78 				    FD_CLOEXEC);
79 			}
80 		(void) pthread_mutex_unlock(&soft_giant_mutex);
81 	}
82 
83 	nwrite = looping_write(soft_urandom_seed_fd, pSeed, ulSeedLen);
84 	if (nwrite <= 0) {
85 		return (CKR_DEVICE_ERROR);
86 	}
87 
88 	return (CKR_OK);
89 
90 }
91 
92 CK_RV
93 C_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData,
94     CK_ULONG ulRandomLen)
95 {
96 
97 	CK_RV	rv;
98 	soft_session_t	*session_p;
99 	boolean_t	lock_held = B_FALSE;
100 
101 	if (!softtoken_initialized)
102 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
103 
104 	/* Obtain the session pointer just for validity check. */
105 	rv = handle2session(hSession, &session_p);
106 	if (rv != CKR_OK)
107 		return (rv);
108 
109 	SES_REFRELE(session_p, lock_held);
110 
111 	if ((pRandomData == NULL) || (ulRandomLen == 0)) {
112 		return (CKR_ARGUMENTS_BAD);
113 	}
114 
115 	return (soft_random_generator(pRandomData, ulRandomLen, B_FALSE));
116 
117 }
118