xref: /illumos-gate/usr/src/test/crypto-tests/tests/common/cryptotest.h (revision 8a2b682e57a046b828f37bcde1776f131ef4629f)
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 2019 Joyent, Inc.
15  */
16 
17 #ifndef _CRYPTOTEST_H
18 #define	_CRYPTOTEST_H
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #include <inttypes.h>
25 #include <sys/crypto/ioctl.h>
26 
27 #define	CTEST_INIT_FAILED (-1)
28 #define	CTEST_NAME_RESOLVE_FAILED (-2)
29 #define	CTEST_MECH_NO_PROVIDER (-3)
30 
31 #define	CTEST_UPDATELEN_WHOLE	SIZE_MAX
32 #define	CTEST_UPDATELEN_END	0
33 
34 typedef struct cryptotest {
35 	uint8_t *in;
36 	uint8_t *out;
37 	uint8_t *key;
38 	void *param;
39 
40 	size_t inlen;
41 	size_t outlen;
42 	size_t keylen;
43 	size_t plen;
44 
45 	char *mechname;
46 	size_t *updatelens;
47 } cryptotest_t;
48 
49 typedef struct crypto_op crypto_op_t;
50 
51 typedef struct test_fg {
52 	crypto_func_group_t tf_fg;
53 	int (*tf_init)(crypto_op_t *);
54 	int (*tf_single)(crypto_op_t *);
55 	int (*tf_update)(crypto_op_t *, size_t, size_t, size_t *);
56 	int (*tf_final)(crypto_op_t *, size_t);
57 } test_fg_t;
58 
59 #define	CRYPTO_INVALID_SESSION ((crypto_session_id_t)-1)
60 
61 int run_test(cryptotest_t *args, uint8_t *cmp, size_t cmplen, test_fg_t *funcs);
62 
63 const char *cryptotest_errstr(int e, char *buf, size_t buflen);
64 
65 /* utils */
66 crypto_op_t *cryptotest_init(cryptotest_t *args, crypto_func_group_t fg);
67 void cryptotest_close(crypto_op_t *op);
68 int get_mech_info(crypto_op_t *op);
69 int get_hsession_by_mech(crypto_op_t *op);
70 
71 /* CRYPTO_MAC */
72 int mac_init(crypto_op_t *op);
73 int mac_single(crypto_op_t *op);
74 int mac_update(crypto_op_t *op, size_t offset, size_t len, size_t *dummy);
75 int mac_final(crypto_op_t *op, size_t dummy);
76 
77 /* CRYPTO_ENCRYPT */
78 int encrypt_init(crypto_op_t *op);
79 int encrypt_single(crypto_op_t *op);
80 int encrypt_update(crypto_op_t *op, size_t offset, size_t plainlen,
81     size_t *encrlen);
82 int encrypt_final(crypto_op_t *op, size_t encrlen);
83 
84 /* CRYPTO_DECRYPT */
85 int decrypt_init(crypto_op_t *op);
86 int decrypt_single(crypto_op_t *op);
87 int decrypt_update(crypto_op_t *op, size_t offset, size_t cipherlen,
88     size_t *encrlen);
89 int decrypt_final(crypto_op_t *op, size_t encrlen);
90 
91 /* CRYPTO_DIGEST */
92 int digest_init(crypto_op_t *op);
93 int digest_single(crypto_op_t *op);
94 int digest_update(crypto_op_t *op, size_t offset, size_t len, size_t *dummy);
95 int digest_final(crypto_op_t *op, size_t dummy);
96 
97 extern test_fg_t cryptotest_decr_fg;
98 extern test_fg_t cryptotest_encr_fg;
99 extern test_fg_t cryptotest_mac_fg;
100 extern test_fg_t cryptotest_digest_fg;
101 
102 #define	MAC_FG (&cryptotest_mac_fg)
103 #define	ENCR_FG (&cryptotest_encr_fg)
104 #define	DECR_FG (&cryptotest_decr_fg)
105 #define	DIGEST_FG (&cryptotest_digest_fg)
106 
107 /*
108  * KCF and PKCS11 use different structures for the CCM params (CK_AES_CCM_PARAMS
109  * and CK_CCM_PARAMS respectively.  Each cryptotest_*.c file implements this
110  * for their respective structs.
111  */
112 void ccm_init_params(void *, ulong_t, uchar_t *, ulong_t, uchar_t *, ulong_t,
113     ulong_t);
114 size_t ccm_param_len(void);
115 
116 #ifdef __cplusplus
117 }
118 #endif
119 
120 #endif /* _CRYPTOTEST_H */
121