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