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.
26 #pragma ident "%Z%%M% %I% %E% SMI"
31 #include <sys/types.h>
32 #include <security/cryptoki.h>
33 #include "softObject.h"
35 #include "softSession.h"
40 #include "softCrypt.h"
46 * session_p: pointer to soft_session_t struct
47 * pMechanism: pointer to CK_MECHANISM struct provided by application
48 * key_p: pointer to key soft_object_t struct
51 * called by C_VerifyInit(). This function calls the corresponding
52 * verify init routine based on the mechanism.
56 soft_verify_init(soft_session_t
*session_p
, CK_MECHANISM_PTR pMechanism
,
60 switch (pMechanism
->mechanism
) {
62 case CKM_SSL3_MD5_MAC
:
63 case CKM_SSL3_SHA1_MAC
:
64 case CKM_MD5_HMAC_GENERAL
:
66 case CKM_SHA_1_HMAC_GENERAL
:
68 case CKM_SHA256_HMAC_GENERAL
:
70 case CKM_SHA384_HMAC_GENERAL
:
72 case CKM_SHA512_HMAC_GENERAL
:
75 return (soft_hmac_sign_verify_init_common(session_p
,
76 pMechanism
, key_p
, B_FALSE
));
80 case CKM_MD5_RSA_PKCS
:
81 case CKM_SHA1_RSA_PKCS
:
82 case CKM_SHA256_RSA_PKCS
:
83 case CKM_SHA384_RSA_PKCS
:
84 case CKM_SHA512_RSA_PKCS
:
86 return (soft_rsa_sign_verify_init_common(session_p
, pMechanism
,
92 return (soft_dsa_sign_verify_init_common(session_p
, pMechanism
,
98 return (soft_ecc_sign_verify_init_common(session_p
, pMechanism
,
101 case CKM_DES_MAC_GENERAL
:
104 return (soft_des_sign_verify_init_common(session_p
, pMechanism
,
108 return (CKR_MECHANISM_INVALID
);
118 * session_p: pointer to soft_session_t struct
119 * pData: pointer to the input data
120 * ulDataLen: length of the input data
121 * pSignature: pointer to the signature
122 * ulSignatureLen: length of the signature
125 * called by C_Verify(). This function calls the corresponding
126 * verify routine based on the mechanism.
130 soft_verify(soft_session_t
*session_p
, CK_BYTE_PTR pData
,
131 CK_ULONG ulDataLen
, CK_BYTE_PTR pSignature
,
132 CK_ULONG ulSignatureLen
)
135 CK_MECHANISM_TYPE mechanism
= session_p
->verify
.mech
.mechanism
;
140 case CKM_SSL3_MD5_MAC
:
141 case CKM_SSL3_SHA1_MAC
:
142 case CKM_MD5_HMAC_GENERAL
:
144 case CKM_SHA_1_HMAC_GENERAL
:
146 case CKM_SHA256_HMAC_GENERAL
:
147 case CKM_SHA256_HMAC
:
148 case CKM_SHA384_HMAC_GENERAL
:
149 case CKM_SHA384_HMAC
:
150 case CKM_SHA512_HMAC_GENERAL
:
151 case CKM_SHA512_HMAC
:
154 CK_BYTE hmac
[SHA512_DIGEST_LENGTH
]; /* use the maximum size */
155 soft_hmac_ctx_t
*hmac_ctx
;
157 hmac_ctx
= (soft_hmac_ctx_t
*)session_p
->verify
.context
;
158 len
= hmac_ctx
->hmac_len
;
160 rv
= soft_hmac_sign_verify_common(session_p
, pData
,
161 ulDataLen
, hmac
, &len
, B_FALSE
);
164 if (len
!= ulSignatureLen
) {
165 rv
= CKR_SIGNATURE_LEN_RANGE
;
168 if (memcmp(hmac
, pSignature
, len
) != 0) {
169 rv
= CKR_SIGNATURE_INVALID
;
175 case CKM_DES_MAC_GENERAL
:
179 CK_BYTE signature
[DES_BLOCK_LEN
]; /* use the maximum size */
180 soft_des_ctx_t
*des_ctx
;
182 des_ctx
= (soft_des_ctx_t
*)session_p
->verify
.context
;
183 len
= des_ctx
->mac_len
;
185 /* Pass local buffer to avoid overflow. */
186 rv
= soft_des_sign_verify_common(session_p
, pData
,
187 ulDataLen
, signature
, &len
, B_FALSE
, B_FALSE
);
190 if (len
!= ulSignatureLen
) {
191 rv
= CKR_SIGNATURE_LEN_RANGE
;
194 if (memcmp(signature
, pSignature
, len
) != 0) {
195 rv
= CKR_SIGNATURE_INVALID
;
204 return (soft_rsa_verify_common(session_p
, pData
, ulDataLen
,
205 pSignature
, ulSignatureLen
, mechanism
));
207 case CKM_MD5_RSA_PKCS
:
208 case CKM_SHA1_RSA_PKCS
:
209 case CKM_SHA256_RSA_PKCS
:
210 case CKM_SHA384_RSA_PKCS
:
211 case CKM_SHA512_RSA_PKCS
:
213 return (soft_rsa_digest_verify_common(session_p
, pData
,
214 ulDataLen
, pSignature
, ulSignatureLen
, mechanism
, B_FALSE
));
218 return (soft_dsa_verify(session_p
, pData
, ulDataLen
,
219 pSignature
, ulSignatureLen
));
223 return (soft_dsa_digest_verify_common(session_p
, pData
,
224 ulDataLen
, pSignature
, ulSignatureLen
, B_FALSE
));
228 return (soft_ecc_verify(session_p
, pData
, ulDataLen
,
229 pSignature
, ulSignatureLen
));
233 return (soft_ecc_digest_verify_common(session_p
, pData
,
234 ulDataLen
, pSignature
, ulSignatureLen
, B_FALSE
));
237 return (CKR_MECHANISM_INVALID
);
243 * soft_verify_update()
246 * session_p: pointer to soft_session_t struct
247 * pPart: pointer to the input data
248 * ulPartLen: length of the input data
251 * called by C_VerifyUpdate(). This function calls the corresponding
252 * verify update routine based on the mechanism.
256 soft_verify_update(soft_session_t
*session_p
, CK_BYTE_PTR pPart
,
259 CK_MECHANISM_TYPE mechanism
= session_p
->verify
.mech
.mechanism
;
263 case CKM_SSL3_MD5_MAC
:
264 case CKM_SSL3_SHA1_MAC
:
265 case CKM_MD5_HMAC_GENERAL
:
267 case CKM_SHA_1_HMAC_GENERAL
:
269 case CKM_SHA256_HMAC_GENERAL
:
270 case CKM_SHA256_HMAC
:
271 case CKM_SHA384_HMAC_GENERAL
:
272 case CKM_SHA384_HMAC
:
273 case CKM_SHA512_HMAC_GENERAL
:
274 case CKM_SHA512_HMAC
:
276 return (soft_hmac_sign_verify_update(session_p
, pPart
,
277 ulPartLen
, B_FALSE
));
279 case CKM_DES_MAC_GENERAL
:
282 return (soft_des_mac_sign_verify_update(session_p
, pPart
,
285 case CKM_MD5_RSA_PKCS
:
286 case CKM_SHA1_RSA_PKCS
:
287 case CKM_SHA256_RSA_PKCS
:
288 case CKM_SHA384_RSA_PKCS
:
289 case CKM_SHA512_RSA_PKCS
:
291 * The MD5/SHA1 digest value is accumulated in the context
292 * of the multiple-part digesting operation. In the final
293 * operation, the digest is encoded and then perform RSA
299 return (soft_digest_update(session_p
, pPart
, ulPartLen
));
302 /* PKCS11: The mechanism only supports single-part operation. */
303 return (CKR_MECHANISM_INVALID
);
309 * soft_verify_final()
312 * session_p: pointer to soft_session_t struct
313 * pSignature: pointer to the signature
314 * ulSignatureLen: length of the signature
317 * called by C_VerifyFinal(). This function calls the corresponding
318 * verify final routine based on the mechanism.
322 soft_verify_final(soft_session_t
*session_p
, CK_BYTE_PTR pSignature
,
323 CK_ULONG ulSignatureLen
)
326 CK_MECHANISM_TYPE mechanism
= session_p
->verify
.mech
.mechanism
;
331 case CKM_SSL3_MD5_MAC
:
332 case CKM_SSL3_SHA1_MAC
:
333 case CKM_MD5_HMAC_GENERAL
:
335 case CKM_SHA_1_HMAC_GENERAL
:
337 case CKM_SHA256_HMAC_GENERAL
:
338 case CKM_SHA256_HMAC
:
339 case CKM_SHA384_HMAC_GENERAL
:
340 case CKM_SHA384_HMAC
:
341 case CKM_SHA512_HMAC_GENERAL
:
342 case CKM_SHA512_HMAC
:
345 CK_BYTE hmac
[SHA512_DIGEST_LENGTH
];
346 soft_hmac_ctx_t
*hmac_ctx
;
348 hmac_ctx
= (soft_hmac_ctx_t
*)session_p
->verify
.context
;
349 len
= hmac_ctx
->hmac_len
;
351 rv
= soft_hmac_sign_verify_common(session_p
, NULL
, 0,
352 hmac
, &len
, B_FALSE
);
355 if (len
!= ulSignatureLen
) {
356 rv
= CKR_SIGNATURE_LEN_RANGE
;
359 if (memcmp(hmac
, pSignature
, len
) != 0) {
360 rv
= CKR_SIGNATURE_INVALID
;
366 case CKM_DES_MAC_GENERAL
:
370 CK_BYTE signature
[DES_BLOCK_LEN
]; /* use the maximum size */
371 soft_des_ctx_t
*des_ctx
;
373 des_ctx
= (soft_des_ctx_t
*)session_p
->verify
.context
;
374 len
= des_ctx
->mac_len
;
376 /* Pass local buffer to avoid overflow. */
377 rv
= soft_des_sign_verify_common(session_p
, NULL
, 0,
378 signature
, &len
, B_FALSE
, B_TRUE
);
381 if (len
!= ulSignatureLen
) {
382 rv
= CKR_SIGNATURE_LEN_RANGE
;
385 if (memcmp(signature
, pSignature
, len
) != 0) {
386 rv
= CKR_SIGNATURE_INVALID
;
392 case CKM_MD5_RSA_PKCS
:
393 case CKM_SHA1_RSA_PKCS
:
394 case CKM_SHA256_RSA_PKCS
:
395 case CKM_SHA384_RSA_PKCS
:
396 case CKM_SHA512_RSA_PKCS
:
398 return (soft_rsa_digest_verify_common(session_p
, NULL
, 0,
399 pSignature
, ulSignatureLen
, mechanism
, B_TRUE
));
403 return (soft_dsa_digest_verify_common(session_p
, NULL
, 0,
404 pSignature
, ulSignatureLen
, B_TRUE
));
408 return (soft_ecc_digest_verify_common(session_p
, NULL
, 0,
409 pSignature
, ulSignatureLen
, B_TRUE
));
412 /* PKCS11: The mechanism only supports single-part operation. */
413 return (CKR_MECHANISM_INVALID
);
420 soft_verify_recover_init(soft_session_t
*session_p
, CK_MECHANISM_PTR pMechanism
,
421 soft_object_t
*key_p
)
424 switch (pMechanism
->mechanism
) {
429 return (soft_rsa_sign_verify_init_common(session_p
, pMechanism
,
433 return (CKR_MECHANISM_INVALID
);
439 soft_verify_recover(soft_session_t
*session_p
, CK_BYTE_PTR pSignature
,
440 CK_ULONG ulSignatureLen
, CK_BYTE_PTR pData
, CK_ULONG_PTR pulDataLen
)
443 CK_MECHANISM_TYPE mechanism
= session_p
->verify
.mech
.mechanism
;
450 return (soft_rsa_verify_recover(session_p
, pSignature
,
451 ulSignatureLen
, pData
, pulDataLen
));
454 return (CKR_MECHANISM_INVALID
);