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 #include "kernelGlobal.h"
28 #include <errno.h>
29 #include <security/cryptoki.h>
30 #include <sys/crypto/common.h>
31 #include <sys/crypto/ioctl.h>
32
33 CK_RV
C_SeedRandom(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pSeed,CK_ULONG ulSeedLen)34 C_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen)
35 {
36 kernel_session_t *session_p;
37 crypto_seed_random_t seed_random;
38 boolean_t ses_lock_held = B_FALSE;
39 CK_RV rv;
40 int r;
41
42 if (!kernel_initialized)
43 return (CKR_CRYPTOKI_NOT_INITIALIZED);
44
45 /* Obtain the session pointer. */
46 rv = handle2session(hSession, &session_p);
47 if (rv != CKR_OK)
48 return (rv);
49
50 if ((pSeed == NULL) || (ulSeedLen == 0)) {
51 REFRELE(session_p, ses_lock_held);
52 return (CKR_ARGUMENTS_BAD);
53 }
54
55 seed_random.sr_session = session_p->k_session;
56 seed_random.sr_seedbuf = (caddr_t)pSeed;
57 seed_random.sr_seedlen = ulSeedLen;
58
59 while ((r = ioctl(kernel_fd, CRYPTO_SEED_RANDOM, &seed_random)) < 0) {
60 if (errno != EINTR)
61 break;
62 }
63 if (r < 0) {
64 rv = CKR_FUNCTION_FAILED;
65 } else {
66 if (seed_random.sr_return_value != CRYPTO_SUCCESS) {
67 rv = crypto2pkcs11_error_number(
68 seed_random.sr_return_value);
69 } else {
70 rv = CKR_OK;
71 }
72 }
73
74 REFRELE(session_p, ses_lock_held);
75 return (rv);
76 }
77
78 CK_RV
C_GenerateRandom(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pRandomData,CK_ULONG ulRandomLen)79 C_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData,
80 CK_ULONG ulRandomLen)
81 {
82 kernel_session_t *session_p;
83 crypto_generate_random_t generate_random;
84 boolean_t ses_lock_held = B_FALSE;
85 CK_RV rv;
86 int r;
87
88 if (!kernel_initialized)
89 return (CKR_CRYPTOKI_NOT_INITIALIZED);
90
91 /* Obtain the session pointer. */
92 rv = handle2session(hSession, &session_p);
93 if (rv != CKR_OK)
94 return (rv);
95
96 if ((pRandomData == NULL) || (ulRandomLen == 0)) {
97 REFRELE(session_p, ses_lock_held);
98 return (CKR_ARGUMENTS_BAD);
99 }
100
101 generate_random.gr_session = session_p->k_session;
102 generate_random.gr_buf = (caddr_t)pRandomData;
103 generate_random.gr_buflen = ulRandomLen;
104
105 while ((r = ioctl(kernel_fd, CRYPTO_GENERATE_RANDOM,
106 &generate_random)) < 0) {
107 if (errno != EINTR)
108 break;
109 }
110 if (r < 0) {
111 rv = CKR_FUNCTION_FAILED;
112 } else {
113 if (generate_random.gr_return_value != CRYPTO_SUCCESS) {
114 rv = crypto2pkcs11_error_number(
115 generate_random.gr_return_value);
116 } else {
117 rv = CKR_OK;
118 }
119 }
120
121 REFRELE(session_p, ses_lock_held);
122 return (rv);
123 }
124