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 #include <aes/aes_impl.h>
18 #include <strings.h>
19 #include <stdio.h>
20 #include <sys/debug.h>
21 #include "cryptotest.h"
22 #include "aes_ccm.h"
23 
24 /*
25  * Size of param (in 8-byte chunks for alignment) large enough for both
26  * CK_CCM_PARAMS and CK_AES_CCM_PARAMS.
27  */
28 #define	PARAM_SIZE_64 8
29 
30 static size_t updatelens[] = {
31 	1, AES_BLOCK_LEN, AES_BLOCK_LEN + 1, 2*AES_BLOCK_LEN,
32 	CTEST_UPDATELEN_WHOLE, CTEST_UPDATELEN_END
33 };
34 
35 int
main(void)36 main(void)
37 {
38 	int errs = 0;
39 	int i;
40 	uint8_t N[1024];
41 	uint64_t param[PARAM_SIZE_64];
42 
43 	cryptotest_t args = {
44 		.out = N,
45 		.outlen = sizeof (N),
46 		.mechname = SUN_CKM_AES_CCM,
47 		.updatelens = updatelens
48 	};
49 
50 	args.key = CCM_KEY1;
51 	args.keylen = sizeof (CCM_KEY1);
52 	for (i = 0; i < 12; i++) {
53 		bzero(param, sizeof (param));
54 		ccm_init_params(param, DATALEN[i] - AUTHLEN[i], NONCE[i],
55 		    NONCELEN[i], CCM_DATA1, AUTHLEN[i], MACLEN[i]);
56 
57 		args.param = param;
58 		args.plen = ccm_param_len();
59 
60 		VERIFY3U(args.plen, <=, sizeof (param));
61 
62 		args.in = CCM_DATA1 + AUTHLEN[i];
63 		args.inlen = DATALEN[i] - AUTHLEN[i];
64 
65 		errs += run_test(&args, RES[i] + AUTHLEN[i],
66 		    RESLEN[i] - AUTHLEN[i], ENCR_FG);
67 		(void) fprintf(stderr, "----------\n");
68 	}
69 
70 	args.key = CCM_KEY2;
71 	args.keylen = sizeof (CCM_KEY2);
72 	for (i = 12; i < 24; i++) {
73 		bzero(param, sizeof (param));
74 		ccm_init_params(param, DATALEN[i] - AUTHLEN[i], NONCE[i],
75 		    NONCELEN[i], DATA_2[i-12], AUTHLEN[i], MACLEN[i]);
76 
77 		args.param = param;
78 		args.plen = ccm_param_len();
79 
80 		VERIFY3U(args.plen, <=, sizeof (param));
81 
82 		args.in = DATA_2[i-12] + AUTHLEN[i];
83 		args.inlen = DATALEN[i] - AUTHLEN[i];
84 
85 		errs += run_test(&args, RES[i] + AUTHLEN[i],
86 		    RESLEN[i] - AUTHLEN[i], ENCR_FG);
87 		(void) fprintf(stderr, "----------\n");
88 	}
89 
90 	(void) fprintf(stderr, "\t\t\t=== decrypt ===\n----------\n\n");
91 
92 	args.key = CCM_KEY1;
93 	args.keylen = sizeof (CCM_KEY1);
94 	for (i = 0; i < 12; i++) {
95 		bzero(param, sizeof (param));
96 		ccm_init_params(param, RESLEN[i] - AUTHLEN[i], NONCE[i],
97 		    NONCELEN[i], CCM_DATA1, AUTHLEN[i], MACLEN[i]);
98 
99 		args.param = param;
100 		args.plen = ccm_param_len();
101 
102 		VERIFY3U(args.plen, <=, sizeof (param));
103 
104 		args.in = RES[i] + AUTHLEN[i];
105 		args.inlen = RESLEN[i] - AUTHLEN[i];
106 
107 		errs += run_test(&args, CCM_DATA1 + AUTHLEN[i],
108 		    DATALEN[i] - AUTHLEN[i], DECR_FG);
109 		(void) fprintf(stderr, "----------\n");
110 	}
111 
112 	args.key = CCM_KEY2;
113 	args.keylen = sizeof (CCM_KEY2);
114 	for (i = 12; i < 24; i++) {
115 		bzero(param, sizeof (param));
116 		ccm_init_params(param, RESLEN[i] - AUTHLEN[i], NONCE[i],
117 		    NONCELEN[i], DATA_2[i-12], AUTHLEN[i], MACLEN[i]);
118 
119 		args.param = param;
120 		args.plen = ccm_param_len();
121 
122 		VERIFY3U(args.plen, <=, sizeof (param));
123 
124 		args.in = RES[i] + AUTHLEN[i];
125 		args.inlen = RESLEN[i] - AUTHLEN[i];
126 
127 		errs += run_test(&args, DATA_2[i-12] + AUTHLEN[i],
128 		    DATALEN[i] - AUTHLEN[i], ENCR_FG);
129 		(void) fprintf(stderr, "----------\n");
130 	}
131 
132 	if (errs != 0)
133 		(void) fprintf(stderr, "%d tests failed\n", errs);
134 
135 	return (errs);
136 }
137