1 /*
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
5 * 1.0 of the CDDL.
6 *
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.
10 */
11
12 /*
13 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
14 */
15
16 #include <stdio.h>
17 #include <cryptoutil.h>
18
19 #include "cryptotest.h"
20
21 struct crypto_op {
22 CK_BYTE_PTR in;
23 CK_BYTE_PTR out;
24 CK_BYTE_PTR key;
25 CK_BYTE_PTR param;
26
27 size_t inlen;
28 size_t outlen;
29 size_t keylen;
30 size_t paramlen;
31 size_t updatelen;
32
33 char *mechname;
34
35 /* internal */
36 CK_MECHANISM_TYPE mech;
37 CK_OBJECT_HANDLE keyt;
47 }
48
49 crypto_op_t *
50 cryptotest_init(cryptotest_t *arg, size_t fg)
51 {
52 crypto_op_t *op = malloc(sizeof (*op));
53
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;
58
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;
64
65 op->mechname = arg->mechname;
66
67 op->hsession = CRYPTO_INVALID_SESSION;
68 op->fg = fg;
69
70 if (op->out == NULL)
71 op->outlen = op->inlen;
72 return (op);
73 }
74
75 int
76 cryptotest_close_session(CK_SESSION_HANDLE hsession)
77 {
78 CK_RV rv;
79 rv = C_CloseSession(hsession);
80 if (rv != CKR_OK)
81 cryptotest_error("cryptotest_close_session", rv);
82
83 return (rv);
84 }
85
86 int
87 cryptotest_close(crypto_op_t *op)
88 {
89 (void) C_DestroyObject(op->hsession, op->keyt);
90 if (op->hsession != CRYPTO_INVALID_SESSION)
91 (void) cryptotest_close_session(op->hsession);
92 free(op);
93 return (C_Finalize(NULL));
94 }
95
96 int
97 get_mech_info(crypto_op_t *op)
98 {
99 CK_RV rv;
100 rv = pkcs11_str2mech(op->mechname, &op->mech);
101 if (rv != CKR_OK) {
102 cryptotest_error("get_mech_info", rv);
103 (void) fprintf(stderr, "failed to resolve mechanism name %s\n",
104 op->mechname);
105 (void) cryptotest_close(op);
106 return (CTEST_NAME_RESOLVE_FAILED);
107 }
108 return (rv);
109 }
110
|
1 /*
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
5 * 1.0 of the CDDL.
6 *
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.
10 */
11
12 /*
13 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
14 * Copyright 2018, Joyent, Inc.
15 */
16
17 #include <stdio.h>
18 #include <cryptoutil.h>
19 #include <security/cryptoki.h>
20
21 #include "cryptotest.h"
22
23 struct crypto_op {
24 CK_BYTE_PTR in;
25 CK_BYTE_PTR out;
26 CK_BYTE_PTR key;
27 CK_BYTE_PTR param;
28
29 size_t inlen;
30 size_t outlen;
31 size_t keylen;
32 size_t paramlen;
33 size_t updatelen;
34
35 char *mechname;
36
37 /* internal */
38 CK_MECHANISM_TYPE mech;
39 CK_OBJECT_HANDLE keyt;
49 }
50
51 crypto_op_t *
52 cryptotest_init(cryptotest_t *arg, size_t fg)
53 {
54 crypto_op_t *op = malloc(sizeof (*op));
55
56 op->in = (CK_BYTE_PTR)arg->in;
57 op->out = (CK_BYTE_PTR)arg->out;
58 op->key = (CK_BYTE_PTR)arg->key;
59 op->param = (CK_BYTE_PTR)arg->param;
60
61 op->inlen = arg->inlen;
62 op->outlen = arg->outlen;
63 op->keylen = arg->keylen;
64 op->paramlen = arg->plen;
65 op->updatelen = arg->updatelen;
66
67 op->mechname = arg->mechname;
68
69 op->hsession = CK_INVALID_HANDLE;
70 op->fg = fg;
71
72 if (op->out == NULL)
73 op->outlen = op->inlen;
74 return (op);
75 }
76
77 int
78 cryptotest_close_session(CK_SESSION_HANDLE hsession)
79 {
80 CK_RV rv;
81 rv = C_CloseSession(hsession);
82 if (rv != CKR_OK)
83 cryptotest_error("cryptotest_close_session", rv);
84
85 return (rv);
86 }
87
88 int
89 cryptotest_close(crypto_op_t *op)
90 {
91 (void) C_DestroyObject(op->hsession, op->keyt);
92 if (op->hsession != CK_INVALID_HANDLE)
93 (void) cryptotest_close_session(op->hsession);
94 free(op);
95 return (C_Finalize(NULL));
96 }
97
98 int
99 get_mech_info(crypto_op_t *op)
100 {
101 CK_RV rv;
102 rv = pkcs11_str2mech(op->mechname, &op->mech);
103 if (rv != CKR_OK) {
104 cryptotest_error("get_mech_info", rv);
105 (void) fprintf(stderr, "failed to resolve mechanism name %s\n",
106 op->mechname);
107 (void) cryptotest_close(op);
108 return (CTEST_NAME_RESOLVE_FAILED);
109 }
110 return (rv);
111 }
112
|