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