xref: /illumos-gate/usr/src/test/crypto-tests/tests/modes/aes/ccm/aes_ccm.c (revision e153cda9f9660e385e8f468253f80e59f5d454d7)
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 <strings.h>
17 #include <stdio.h>
18 
19 #include "cryptotest.h"
20 #include "aes_ccm.h"
21 
22 int
23 main(void)
24 {
25 	int errs = 0;
26 	int i;
27 	uint8_t N[1024];
28 	CK_AES_CCM_PARAMS param;
29 	cryptotest_t args;
30 
31 	bzero(&param, sizeof (param));
32 
33 	args.out = N;
34 	args.param = &param;
35 
36 	args.outlen = sizeof (N);
37 	args.plen = sizeof (param);
38 
39 	args.mechname = SUN_CKM_AES_CCM;
40 	args.updatelen = 1;
41 
42 	param.authData = CCM_DATA1;
43 	args.key = CCM_KEY1;
44 	args.keylen = sizeof (CCM_KEY1);
45 	for (i = 0; i < 12; i++) {
46 		param.ulMACSize = MACLEN[i];
47 		param.ulNonceSize = NONCELEN[i];
48 		param.ulAuthDataSize = AUTHLEN[i];
49 		param.ulDataSize = DATALEN[i] - AUTHLEN[i];
50 		param.nonce = NONCE[i];
51 
52 		args.in = CCM_DATA1 + AUTHLEN[i];
53 		args.inlen = DATALEN[i] - AUTHLEN[i];
54 
55 		errs += run_test(&args, RES[i] + AUTHLEN[i],
56 		    RESLEN[i] - AUTHLEN[i], ENCR_FG);
57 		(void) fprintf(stderr, "----------\n");
58 	}
59 
60 	args.key = CCM_KEY2;
61 	args.keylen = sizeof (CCM_KEY2);
62 	for (i = 12; i < 24; i++) {
63 		param.ulMACSize = MACLEN[i];
64 		param.ulNonceSize = NONCELEN[i];
65 		param.ulAuthDataSize = AUTHLEN[i];
66 		param.ulDataSize = DATALEN[i] - AUTHLEN[i];
67 		param.nonce = NONCE[i];
68 		param.authData = DATA_2[i-12];
69 
70 		args.in = DATA_2[i-12] + AUTHLEN[i];
71 		args.inlen = DATALEN[i] - AUTHLEN[i];
72 
73 		errs += run_test(&args, RES[i] + AUTHLEN[i],
74 		    RESLEN[i] - AUTHLEN[i], ENCR_FG);
75 		(void) fprintf(stderr, "----------\n");
76 	}
77 
78 	(void) fprintf(stderr, "\t\t\t=== decrypt ===\n----------\n\n");
79 
80 	param.authData = CCM_DATA1;
81 	args.key = CCM_KEY1;
82 	args.keylen = sizeof (CCM_KEY1);
83 	for (i = 0; i < 12; i++) {
84 		param.ulMACSize = MACLEN[i];
85 		param.ulNonceSize = NONCELEN[i];
86 		param.ulAuthDataSize = AUTHLEN[i];
87 		param.ulDataSize = RESLEN[i] - AUTHLEN[i];
88 		param.nonce = NONCE[i];
89 
90 		args.in = RES[i] + AUTHLEN[i];
91 		args.inlen = RESLEN[i] - AUTHLEN[i];
92 
93 		errs += run_test(&args, CCM_DATA1 + AUTHLEN[i],
94 		    DATALEN[i] - AUTHLEN[i], DECR_FG);
95 		(void) fprintf(stderr, "----------\n");
96 	}
97 
98 	args.key = CCM_KEY2;
99 	args.keylen = sizeof (CCM_KEY2);
100 	for (i = 12; i < 24; i++) {
101 		param.ulMACSize = MACLEN[i];
102 		param.ulNonceSize = NONCELEN[i];
103 		param.ulAuthDataSize = AUTHLEN[i];
104 		param.ulDataSize = RESLEN[i] - AUTHLEN[i];
105 		param.nonce = NONCE[i];
106 		param.authData = DATA_2[i-12];
107 
108 		args.in = RES[i] + AUTHLEN[i];
109 		args.inlen = RESLEN[i] - AUTHLEN[i];
110 
111 		errs += run_test(&args, DATA_2[i-12] + AUTHLEN[i],
112 		    DATALEN[i] - AUTHLEN[i], ENCR_FG);
113 		(void) fprintf(stderr, "----------\n");
114 	}
115 
116 	if (errs != 0)
117 		(void) fprintf(stderr, "%d tests failed\n", errs);
118 
119 	return (errs);
120 }
121