xref: /titanic_44/usr/src/lib/pkcs11/pkcs11_kernel/common/kernelRand.c (revision 03831d35f7499c87d51205817c93e9a8d42c4bae)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "kernelGlobal.h"
30 #include <errno.h>
31 #include <security/cryptoki.h>
32 #include <sys/crypto/common.h>
33 #include <sys/crypto/ioctl.h>
34 
35 CK_RV
36 C_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen)
37 {
38 	kernel_session_t *session_p;
39 	crypto_seed_random_t seed_random;
40 	boolean_t ses_lock_held = B_FALSE;
41 	CK_RV rv;
42 	int r;
43 
44 	if (!kernel_initialized)
45 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
46 
47 	/* Obtain the session pointer. */
48 	rv = handle2session(hSession, &session_p);
49 	if (rv != CKR_OK)
50 		return (rv);
51 
52 	if ((pSeed == NULL) || (ulSeedLen == 0)) {
53 		REFRELE(session_p, ses_lock_held);
54 		return (CKR_ARGUMENTS_BAD);
55 	}
56 
57 	seed_random.sr_session = session_p->k_session;
58 	seed_random.sr_seedbuf = (caddr_t)pSeed;
59 	seed_random.sr_seedlen = ulSeedLen;
60 
61 	while ((r = ioctl(kernel_fd, CRYPTO_SEED_RANDOM, &seed_random)) < 0) {
62 		if (errno != EINTR)
63 			break;
64 	}
65 	if (r < 0) {
66 		rv = CKR_FUNCTION_FAILED;
67 	} else {
68 		if (seed_random.sr_return_value != CRYPTO_SUCCESS) {
69 			rv = crypto2pkcs11_error_number(
70 			    seed_random.sr_return_value);
71 		} else {
72 			rv = CKR_OK;
73 		}
74 	}
75 
76 	REFRELE(session_p, ses_lock_held);
77 	return (rv);
78 }
79 
80 CK_RV
81 C_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData,
82     CK_ULONG ulRandomLen)
83 {
84 	kernel_session_t *session_p;
85 	crypto_generate_random_t generate_random;
86 	boolean_t ses_lock_held = B_FALSE;
87 	CK_RV rv;
88 	int r;
89 
90 	if (!kernel_initialized)
91 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
92 
93 	/* Obtain the session pointer. */
94 	rv = handle2session(hSession, &session_p);
95 	if (rv != CKR_OK)
96 		return (rv);
97 
98 	if ((pRandomData == NULL) || (ulRandomLen == 0)) {
99 		REFRELE(session_p, ses_lock_held);
100 		return (CKR_ARGUMENTS_BAD);
101 	}
102 
103 	generate_random.gr_session = session_p->k_session;
104 	generate_random.gr_buf = (caddr_t)pRandomData;
105 	generate_random.gr_buflen = ulRandomLen;
106 
107 	while ((r = ioctl(kernel_fd, CRYPTO_GENERATE_RANDOM,
108 	    &generate_random)) < 0) {
109 		if (errno != EINTR)
110 			break;
111 	}
112 	if (r < 0) {
113 		rv = CKR_FUNCTION_FAILED;
114 	} else {
115 		if (generate_random.gr_return_value != CRYPTO_SUCCESS) {
116 			rv = crypto2pkcs11_error_number(
117 			    generate_random.gr_return_value);
118 		} else {
119 			rv = CKR_OK;
120 		}
121 	}
122 
123 	REFRELE(session_p, ses_lock_held);
124 	return (rv);
125 }
126