2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
17 #include <cryptoutil.h>
19 #include "cryptotest.h"
36 CK_MECHANISM_TYPE mech
;
37 CK_OBJECT_HANDLE keyt
;
38 CK_SESSION_HANDLE hsession
;
43 cryptotest_error(char *name
, CK_RV rv
)
45 (void) fprintf(stderr
, "%s: Error = 0x%.8lX '%s'\n",
46 name
, rv
, pkcs11_strerror(rv
));
50 cryptotest_init(cryptotest_t
*arg
, size_t fg
)
52 crypto_op_t
*op
= malloc(sizeof (*op
));
54 op
->in
= (CK_BYTE_PTR
)arg
->in
;
55 op
->out
= (CK_BYTE_PTR
)arg
->out
;
56 op
->key
= (CK_BYTE_PTR
)arg
->key
;
57 op
->param
= (CK_BYTE_PTR
)arg
->param
;
59 op
->inlen
= arg
->inlen
;
60 op
->outlen
= arg
->outlen
;
61 op
->keylen
= arg
->keylen
;
62 op
->paramlen
= arg
->plen
;
63 op
->updatelen
= arg
->updatelen
;
65 op
->mechname
= arg
->mechname
;
67 op
->hsession
= CRYPTO_INVALID_SESSION
;
71 op
->outlen
= op
->inlen
;
76 cryptotest_close_session(CK_SESSION_HANDLE hsession
)
79 rv
= C_CloseSession(hsession
);
81 cryptotest_error("cryptotest_close_session", rv
);
87 cryptotest_close(crypto_op_t
*op
)
89 (void) C_DestroyObject(op
->hsession
, op
->keyt
);
90 if (op
->hsession
!= CRYPTO_INVALID_SESSION
)
91 (void) cryptotest_close_session(op
->hsession
);
93 return (C_Finalize(NULL
));
97 get_mech_info(crypto_op_t
*op
)
100 rv
= pkcs11_str2mech(op
->mechname
, &op
->mech
);
102 cryptotest_error("get_mech_info", rv
);
103 (void) fprintf(stderr
, "failed to resolve mechanism name %s\n",
105 (void) cryptotest_close(op
);
106 return (CTEST_NAME_RESOLVE_FAILED
);
113 get_hsession_by_mech(crypto_op_t
*op
)
116 rv
= SUNW_C_GetMechSession(op
->mech
, &op
->hsession
);
118 cryptotest_error("get_hsession_by_mech", rv
);
119 (void) fprintf(stderr
,
120 "could not find provider for mechanism %lu\n",
122 (void) cryptotest_close(op
);
123 return (CTEST_MECH_NO_PROVIDER
);
132 sign_init(crypto_op_t
*op
)
137 mech
.mechanism
= op
->mech
;
138 mech
.pParameter
= NULL
;
139 mech
.ulParameterLen
= 0;
141 rv
= SUNW_C_KeyToObject(op
->hsession
, op
->mech
,
142 op
->key
, op
->keylen
, &op
->keyt
);
145 cryptotest_error("SUNW_C_KeyToObject", rv
);
147 rv
= C_SignInit(op
->hsession
, &mech
, op
->keyt
);
150 cryptotest_error("C_SignInit", rv
);
156 sign_single(crypto_op_t
*op
)
160 rv
= C_Sign(op
->hsession
, op
->in
, op
->inlen
,
161 op
->out
, (CK_ULONG_PTR
)&op
->outlen
);
163 cryptotest_error("C_Sign", rv
);
168 sign_update(crypto_op_t
*op
, int offset
)
171 rv
= C_SignUpdate(op
->hsession
, op
->in
+ offset
, op
->updatelen
);
173 cryptotest_error("C_SignUpdate", rv
);
179 sign_final(crypto_op_t
*op
)
182 rv
= C_SignFinal(op
->hsession
, op
->out
, (CK_ULONG_PTR
)&op
->outlen
);
184 cryptotest_error("C_SignFinal", rv
);
192 mac_init(crypto_op_t
*op
)
194 return (sign_init(op
));
198 mac_single(crypto_op_t
*op
)
200 return (sign_single(op
));
204 mac_update(crypto_op_t
*op
, int offset
)
206 return (sign_update(op
, offset
));
210 mac_final(crypto_op_t
*op
)
212 return (sign_final(op
));
219 verify_init(crypto_op_t
*op
)
224 mech
.mechanism
= op
->mech
;
225 mech
.pParameter
= NULL
;
226 mech
.ulParameterLen
= 0;
228 rv
= SUNW_C_KeyToObject(op
->hsession
, op
->mech
,
229 op
->key
, op
->keylen
, &op
->keyt
);
232 cryptotest_error("SUNW_C_KeyToObject", rv
);
234 rv
= C_VerifyInit(op
->hsession
, &mech
, op
->keyt
);
237 cryptotest_error("C_VerifyInit", rv
);
243 verify_single(crypto_op_t
*op
)
247 rv
= C_Verify(op
->hsession
, op
->in
, op
->inlen
, op
->out
, op
->outlen
);
248 if (rv
!= CKR_OK
&& rv
!= CKR_SIGNATURE_INVALID
&&
249 rv
!= CKR_SIGNATURE_LEN_RANGE
)
250 cryptotest_error("C_Verify", rv
);
255 verify_update(crypto_op_t
*op
, int offset
)
258 rv
= C_VerifyUpdate(op
->hsession
, op
->in
+ offset
, op
->updatelen
);
260 cryptotest_error("C_VerifyUpdate", rv
);
265 verify_final(crypto_op_t
*op
)
268 rv
= C_VerifyFinal(op
->hsession
, op
->out
, op
->outlen
);
269 if (rv
!= CKR_OK
&& rv
!= CKR_SIGNATURE_INVALID
&&
270 rv
!= CKR_SIGNATURE_LEN_RANGE
)
271 cryptotest_error("C_VerifyFinal", rv
);
276 * ENCRYPT_* functions
279 encrypt_init(crypto_op_t
*op
)
284 mech
.mechanism
= op
->mech
;
285 mech
.pParameter
= op
->param
;
286 mech
.ulParameterLen
= op
->paramlen
;
288 rv
= SUNW_C_KeyToObject(op
->hsession
, op
->mech
,
289 op
->key
, op
->keylen
, &op
->keyt
);
292 cryptotest_error("SUNW_C_KeyToObject", rv
);
294 rv
= C_EncryptInit(op
->hsession
, &mech
, op
->keyt
);
297 cryptotest_error("C_EncryptInit", rv
);
303 encrypt_single(crypto_op_t
*op
)
307 rv
= C_Encrypt(op
->hsession
, op
->in
, op
->inlen
,
308 op
->out
, (CK_ULONG_PTR
)&op
->outlen
);
310 cryptotest_error("C_Encrypt", rv
);
315 encrypt_update(crypto_op_t
*op
, int offset
, size_t *encrlen
)
318 CK_ULONG outlen
= op
->outlen
- *encrlen
;
319 rv
= C_EncryptUpdate(op
->hsession
, op
->in
+ offset
, op
->updatelen
,
320 op
->out
+ *encrlen
, &outlen
);
322 cryptotest_error("C_EncryptUpdate", rv
);
329 encrypt_final(crypto_op_t
*op
, size_t encrlen
)
332 CK_ULONG outlen
= op
->outlen
- encrlen
;
333 rv
= C_EncryptFinal(op
->hsession
, op
->out
+ encrlen
, &outlen
);
335 cryptotest_error("C_EncryptFinal", rv
);
340 * DECRYPT_* functions
343 decrypt_init(crypto_op_t
*op
)
348 mech
.mechanism
= op
->mech
;
349 mech
.pParameter
= op
->param
;
350 mech
.ulParameterLen
= op
->paramlen
;
352 rv
= SUNW_C_KeyToObject(op
->hsession
, op
->mech
,
353 op
->key
, op
->keylen
, &op
->keyt
);
356 cryptotest_error("SUNW_C_KeyToObject", rv
);
358 rv
= C_DecryptInit(op
->hsession
, &mech
, op
->keyt
);
361 cryptotest_error("C_DecryptInit", rv
);
367 decrypt_single(crypto_op_t
*op
)
371 rv
= C_Decrypt(op
->hsession
, op
->in
, op
->inlen
,
372 op
->out
, (CK_ULONG_PTR
)&op
->outlen
);
374 cryptotest_error("C_Decrypt", rv
);
379 decrypt_update(crypto_op_t
*op
, int offset
, size_t *encrlen
)
382 CK_ULONG outlen
= op
->outlen
- *encrlen
;
383 rv
= C_DecryptUpdate(op
->hsession
, op
->in
+ offset
, op
->updatelen
,
384 op
->out
+ *encrlen
, &outlen
);
386 cryptotest_error("C_DecryptUpdate", rv
);
393 decrypt_final(crypto_op_t
*op
, size_t encrlen
)
396 CK_ULONG outlen
= op
->outlen
- encrlen
;
397 rv
= C_DecryptFinal(op
->hsession
, op
->out
+ encrlen
, &outlen
);
399 cryptotest_error("C_DecryptFinal", rv
);