xref: /titanic_52/usr/src/lib/pkcs11/libpkcs11/common/pkcs11Rand.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
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 2004 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 <security/cryptoki.h>
30 #include "pkcs11Global.h"
31 #include "pkcs11Session.h"
32 #include "pkcs11Slot.h"
33 
34 /*
35  * C_SeedRandom will verify that the session handle is valid within
36  * the framework, that random numbers are not disabled for the slot
37  * associated with this session, and then redirect to the underlying
38  * provider.
39  */
40 CK_RV
41 C_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen)
42 {
43 	CK_RV rv;
44 	pkcs11_session_t *sessp;
45 	CK_SLOT_ID slotid;
46 
47 	/* Check for a fastpath */
48 	if (purefastpath || policyfastpath) {
49 		/* Check if random number functions are allowed */
50 		if (policyfastpath &&
51 		    slottable->st_slots[fast_slot]->sl_norandom) {
52 			return (CKR_FUNCTION_FAILED);
53 		}
54 		return (fast_funcs->C_SeedRandom(hSession, pSeed, ulSeedLen));
55 	}
56 
57 	if (!pkcs11_initialized) {
58 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
59 	}
60 
61 	/* Obtain the session pointer */
62 	HANDLE2SESSION(hSession, sessp, rv);
63 
64 	if (rv != CKR_OK) {
65 		return (rv);
66 	}
67 
68 	slotid = sessp->se_slotid;
69 
70 	/* Check if random number functions are allowed */
71 	if (slottable->st_slots[slotid]->sl_norandom)
72 		return (CKR_FUNCTION_FAILED);
73 
74 	/* Pass data to the provider */
75 	rv = FUNCLIST(slotid)->C_SeedRandom(sessp->se_handle, pSeed,
76 	    ulSeedLen);
77 
78 	/* Present consistent interface to the application */
79 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
80 		return (CKR_FUNCTION_FAILED);
81 	}
82 
83 	return (rv);
84 }
85 
86 /*
87  * C_GenerateRandom will verify that the session handle is valid within
88  * the framework, that random numbers are not disabled for the slot
89  * associated with this session, and then redirect to the underlying
90  * provider.
91  */
92 CK_RV
93 C_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData,
94     CK_ULONG ulRandomLen)
95 {
96 	CK_RV rv;
97 	pkcs11_session_t *sessp;
98 	CK_SLOT_ID slotid;
99 
100 	/* Check for a fastpath */
101 	if (purefastpath || policyfastpath) {
102 		/* Check if random number functions are allowed */
103 		if (policyfastpath &&
104 		    slottable->st_slots[fast_slot]->sl_norandom) {
105 			return (CKR_FUNCTION_FAILED);
106 		}
107 		return (fast_funcs->C_GenerateRandom(hSession, pRandomData,
108 			    ulRandomLen));
109 	}
110 
111 	if (!pkcs11_initialized) {
112 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
113 	}
114 
115 	/* Obtain the session pointer */
116 	HANDLE2SESSION(hSession, sessp, rv);
117 
118 	if (rv != CKR_OK) {
119 		return (rv);
120 	}
121 
122 	slotid = sessp->se_slotid;
123 
124 	/* Check if random number functions are allowed */
125 	if (slottable->st_slots[slotid]->sl_norandom)
126 		return (CKR_FUNCTION_FAILED);
127 
128 	/* Pass data to the provider */
129 	rv = FUNCLIST(sessp->se_slotid)->C_GenerateRandom(sessp->se_handle,
130 	    pRandomData, ulRandomLen);
131 
132 	/* Present consistent interface to the application */
133 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
134 		return (CKR_FUNCTION_FAILED);
135 	}
136 
137 	return (rv);
138 }
139