xref: /illumos-gate/usr/src/lib/pkcs11/libpkcs11/common/pkcs11DualCrypt.c (revision 1a2d662a91cee3bf82f41cd47c7ae6f3825d9db2)
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 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <security/cryptoki.h>
28 #include "pkcs11Global.h"
29 #include "pkcs11Session.h"
30 #include "pkcs11Slot.h"
31 
32 /*
33  * C_DigestEncryptUpdate is a pure wrapper to the underlying provider.
34  * Policy enforcement was done earlier by the mandatory calls to
35  * C_DigestInit and C_EncryptInit.
36  *
37  * The only argument checked is whether or not hSession is valid.
38  */
39 CK_RV
40 C_DigestEncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
41     CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
42     CK_ULONG_PTR pulEncryptedPartLen)
43 {
44 	CK_RV rv;
45 	pkcs11_session_t *sessp;
46 
47 	/* Check for a fastpath */
48 	if (purefastpath || policyfastpath) {
49 		return (fast_funcs->C_DigestEncryptUpdate(hSession, pPart,
50 			    ulPartLen, pEncryptedPart, pulEncryptedPartLen));
51 	}
52 
53 	if (!pkcs11_initialized) {
54 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
55 	}
56 
57 	/* Obtain the session pointer */
58 	HANDLE2SESSION(hSession, sessp, rv);
59 
60 	if (rv != CKR_OK) {
61 		return (rv);
62 	}
63 
64 	/* Pass data to the provider */
65 	rv = FUNCLIST(sessp->se_slotid)->
66 	    C_DigestEncryptUpdate(sessp->se_handle, pPart, ulPartLen,
67 		pEncryptedPart, pulEncryptedPartLen);
68 
69 	/* Present consistent interface to the application */
70 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
71 		return (CKR_FUNCTION_FAILED);
72 	}
73 
74 	return (rv);
75 
76 }
77 
78 /*
79  * C_DecryptDigestUpdate is a pure wrapper to the underlying provider.
80  * Policy enforcement was done earlier by the mandatory calls to
81  * C_DigestInit and C_DecryptInit.
82  *
83  * The only argument checked is whether or not hSession is valid.
84  */
85 CK_RV
86 C_DecryptDigestUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart,
87     CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen)
88 {
89 	CK_RV rv;
90 	pkcs11_session_t *sessp;
91 
92 	/* Check for a fastpath */
93 	if (purefastpath || policyfastpath) {
94 		return (fast_funcs->C_DecryptDigestUpdate(hSession,
95 			    pEncryptedPart, ulEncryptedPartLen, pPart,
96 			    pulPartLen));
97 	}
98 
99 	if (!pkcs11_initialized) {
100 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
101 	}
102 
103 	/* Obtain the session pointer */
104 	HANDLE2SESSION(hSession, sessp, rv);
105 
106 	if (rv != CKR_OK) {
107 		return (rv);
108 	}
109 
110 	/* Pass data to the provider */
111 	rv = FUNCLIST(sessp->se_slotid)->
112 	    C_DecryptDigestUpdate(sessp->se_handle, pEncryptedPart,
113 		ulEncryptedPartLen, pPart, pulPartLen);
114 
115 	/* Present consistent interface to the application */
116 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
117 		return (CKR_FUNCTION_FAILED);
118 	}
119 
120 	return (rv);
121 }
122 
123 /*
124  * C_SignEncryptUpdate is a pure wrapper to the underlying provider.
125  * Policy enforcement was done earlier by the mandatory calls to
126  * C_SignInit and C_EncryptInit.
127  *
128  * The only argument checked is whether or not hSession is valid.
129  */
130 CK_RV
131 C_SignEncryptUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pPart,
132     CK_ULONG ulPartLen, CK_BYTE_PTR pEncryptedPart,
133     CK_ULONG_PTR pulEncryptedPartLen)
134 {
135 	CK_RV rv;
136 	pkcs11_session_t *sessp;
137 
138 	/* Check for a fastpath */
139 	if (purefastpath || policyfastpath) {
140 		return (fast_funcs->C_SignEncryptUpdate(hSession, pPart,
141 			    ulPartLen,  pEncryptedPart, pulEncryptedPartLen));
142 	}
143 
144 	if (!pkcs11_initialized) {
145 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
146 	}
147 
148 	/* Obtain the session pointer */
149 	HANDLE2SESSION(hSession, sessp, rv);
150 
151 	if (rv != CKR_OK) {
152 		return (rv);
153 	}
154 
155 	/* Pass data to the provider */
156 	rv = FUNCLIST(sessp->se_slotid)->
157 	    C_SignEncryptUpdate(sessp->se_handle, pPart, ulPartLen,
158 		pEncryptedPart, pulEncryptedPartLen);
159 
160 	/* Present consistent interface to the application */
161 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
162 		return (CKR_FUNCTION_FAILED);
163 	}
164 
165 	return (rv);
166 }
167 
168 /*
169  * C_DecryptVerifyUpdate is a pure wrapper to the underlying provider.
170  * Policy enforcement was done earlier by the mandatory calls to
171  * C_SignInit and C_EncryptInit.
172  *
173  * The only argument checked is whether or not hSession is valid.
174  */
175 CK_RV
176 C_DecryptVerifyUpdate(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pEncryptedPart,
177     CK_ULONG ulEncryptedPartLen, CK_BYTE_PTR pPart, CK_ULONG_PTR pulPartLen)
178 {
179 	CK_RV rv;
180 	pkcs11_session_t *sessp;
181 
182 	/* Check for a fastpath */
183 	if (purefastpath || policyfastpath) {
184 		return (fast_funcs->C_DecryptVerifyUpdate(hSession,
185 			    pEncryptedPart, ulEncryptedPartLen, pPart,
186 			    pulPartLen));
187 	}
188 
189 	if (!pkcs11_initialized) {
190 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
191 	}
192 
193 	/* Obtain the session pointer */
194 	HANDLE2SESSION(hSession, sessp, rv);
195 
196 	if (rv != CKR_OK) {
197 		return (rv);
198 	}
199 
200 	/* Pass data to the provider */
201 	rv = FUNCLIST(sessp->se_slotid)->
202 	    C_DecryptVerifyUpdate(sessp->se_handle, pEncryptedPart,
203 		ulEncryptedPartLen, pPart, pulPartLen);
204 
205 	/* Present consistent interface to the application */
206 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
207 		return (CKR_FUNCTION_FAILED);
208 	}
209 
210 	return (rv);
211 }
212