4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 * Copyright 2018, Joyent, Inc.
28 * Encryption and Decryption Functions
29 * (as defined in PKCS#11 spec sections 11.8 and 11.9)
32 #include "metaGlobal.h"
40 meta_EncryptInit(CK_SESSION_HANDLE hSession
, CK_MECHANISM_PTR pMechanism
,
41 CK_OBJECT_HANDLE hKey
)
44 meta_session_t
*session
;
47 if (pMechanism
== NULL
)
48 return (CKR_ARGUMENTS_BAD
);
50 rv
= meta_handle2session(hSession
, &session
);
54 rv
= meta_handle2object(hKey
, &key
);
60 rv
= meta_operation_init_defer(CKF_ENCRYPT
, session
, pMechanism
, key
);
74 meta_Encrypt(CK_SESSION_HANDLE hSession
,
75 CK_BYTE_PTR pData
, CK_ULONG ulDataLen
,
76 CK_BYTE_PTR pEncryptedData
, CK_ULONG_PTR pulEncryptedDataLen
)
79 meta_session_t
*session
;
81 rv
= meta_handle2session(hSession
, &session
);
85 if (pulEncryptedDataLen
== NULL
) {
86 meta_operation_cleanup(session
, CKF_ENCRYPT
, FALSE
);
88 return (CKR_ARGUMENTS_BAD
);
92 * Allow pData to be NULL as long as the length is 0 in order to
93 * support ciphers that permit 0 byte inputs (e.g. combined mode
94 * ciphers), otherwise consider pData being NULL as invalid.
96 if (pData
== NULL
&& ulDataLen
!= 0) {
97 meta_operation_cleanup(session
, CKF_ENCRYPT
, FALSE
);
99 return (CKR_ARGUMENTS_BAD
);
102 rv
= meta_do_operation(CKF_ENCRYPT
, MODE_SINGLE
, session
, NULL
,
103 pData
, ulDataLen
, pEncryptedData
, pulEncryptedDataLen
);
116 meta_EncryptUpdate(CK_SESSION_HANDLE hSession
,
117 CK_BYTE_PTR pPart
, CK_ULONG ulPartLen
,
118 CK_BYTE_PTR pEncryptedPart
, CK_ULONG_PTR pulEncryptedPartLen
)
121 meta_session_t
*session
;
123 rv
= meta_handle2session(hSession
, &session
);
127 if (pPart
== NULL
|| pulEncryptedPartLen
== NULL
) {
128 meta_operation_cleanup(session
, CKF_ENCRYPT
, FALSE
);
130 return (CKR_ARGUMENTS_BAD
);
133 rv
= meta_do_operation(CKF_ENCRYPT
, MODE_UPDATE
, session
, NULL
,
134 pPart
, ulPartLen
, pEncryptedPart
, pulEncryptedPartLen
);
147 meta_EncryptFinal(CK_SESSION_HANDLE hSession
,
148 CK_BYTE_PTR pLastEncryptedPart
, CK_ULONG_PTR pulLastEncryptedPartLen
)
151 meta_session_t
*session
;
153 rv
= meta_handle2session(hSession
, &session
);
157 if (pulLastEncryptedPartLen
== NULL
) {
158 meta_operation_cleanup(session
, CKF_ENCRYPT
, FALSE
);
160 return (CKR_ARGUMENTS_BAD
);
163 rv
= meta_do_operation(CKF_ENCRYPT
, MODE_FINAL
, session
, NULL
,
164 NULL
, 0, pLastEncryptedPart
, pulLastEncryptedPartLen
);
177 meta_DecryptInit(CK_SESSION_HANDLE hSession
, CK_MECHANISM_PTR pMechanism
,
178 CK_OBJECT_HANDLE hKey
)
181 meta_session_t
*session
;
184 if (pMechanism
== NULL
)
185 return (CKR_ARGUMENTS_BAD
);
187 rv
= meta_handle2session(hSession
, &session
);
191 rv
= meta_handle2object(hKey
, &key
);
197 rv
= meta_operation_init_defer(CKF_DECRYPT
, session
, pMechanism
, key
);
211 meta_Decrypt(CK_SESSION_HANDLE hSession
,
212 CK_BYTE_PTR pEncryptedData
, CK_ULONG ulEncryptedDataLen
,
213 CK_BYTE_PTR pData
, CK_ULONG_PTR pulDataLen
)
216 meta_session_t
*session
;
218 rv
= meta_handle2session(hSession
, &session
);
222 if (pEncryptedData
== NULL
|| pulDataLen
== NULL
) {
223 meta_operation_cleanup(session
, CKF_DECRYPT
, FALSE
);
225 return (CKR_ARGUMENTS_BAD
);
228 rv
= meta_do_operation(CKF_DECRYPT
, MODE_SINGLE
, session
, NULL
,
229 pEncryptedData
, ulEncryptedDataLen
, pData
, pulDataLen
);
242 meta_DecryptUpdate(CK_SESSION_HANDLE hSession
,
243 CK_BYTE_PTR pEncryptedPart
, CK_ULONG ulEncryptedPartLen
,
244 CK_BYTE_PTR pPart
, CK_ULONG_PTR pulPartLen
)
247 meta_session_t
*session
;
249 rv
= meta_handle2session(hSession
, &session
);
253 if (pEncryptedPart
== NULL
|| pulPartLen
== NULL
) {
254 meta_operation_cleanup(session
, CKF_DECRYPT
, FALSE
);
256 return (CKR_ARGUMENTS_BAD
);
259 rv
= meta_do_operation(CKF_DECRYPT
, MODE_UPDATE
, session
, NULL
,
260 pEncryptedPart
, ulEncryptedPartLen
, pPart
, pulPartLen
);
273 meta_DecryptFinal(CK_SESSION_HANDLE hSession
,
274 CK_BYTE_PTR pLastPart
, CK_ULONG_PTR pulLastPartLen
)
277 meta_session_t
*session
;
279 rv
= meta_handle2session(hSession
, &session
);
283 if (pulLastPartLen
== NULL
) {
284 meta_operation_cleanup(session
, CKF_DECRYPT
, FALSE
);
286 return (CKR_ARGUMENTS_BAD
);
289 rv
= meta_do_operation(CKF_DECRYPT
, MODE_FINAL
, session
, NULL
,
290 NULL
, 0, pLastPart
, pulLastPartLen
);