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