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 2016 Nexenta Systems, Inc.  All rights reserved.
14  * Copyright 2019 Joyent, Inc.
15  */
16 
17 #include <strings.h>
18 #include <stdio.h>
19 /* used for CK_*_PARAMS */
20 #include <security/cryptoki.h>
21 
22 #include "cryptotest.h"
23 #include "aes_ctr.h"
24 
25 /*
26  * CTR is a stream cipher, and it runs ctr_mode_final every time
27  * it has a remainder, so the result is different
28  * if len == 0 mod block_size vs len != 0 mod block_size
29  */
30 static size_t updatelens[] = { 16, 15, 17, CTEST_UPDATELEN_END };
31 
32 int
main(void)33 main(void)
34 {
35 	int errs = 0;
36 	int i;
37 	uint8_t N[1024];
38 	size_t cblen = sizeof (CTR_CB0);
39 	CK_AES_CTR_PARAMS param = {
40 		.ulCounterBits = 128 - cblen * 8,
41 		.cb[15] = 0x01
42 	};
43 	cryptotest_t args = {
44 		.out = N,
45 		.outlen = sizeof (N),
46 		.param = &param,
47 		.plen = sizeof (param),
48 		.mechname = SUN_CKM_AES_CTR,
49 		.updatelens = updatelens
50 	};
51 
52 	for (i = 0; i < sizeof (DATA) / sizeof (DATA[0]); i++) {
53 		bcopy(CB[i], param.cb, cblen);
54 
55 		args.in = DATA[i];
56 		args.key = KEY[i];
57 
58 		args.inlen = DATALEN[i];
59 		args.keylen = KEYLEN[i];
60 
61 		errs += run_test(&args, RES[i], RESLEN[i], ENCR_FG);
62 		(void) fprintf(stderr, "----------\n");
63 	}
64 
65 	(void) fprintf(stderr, "\t\t\t=== decrypt ===\n----------\n\n");
66 
67 	for (i = 0; i < sizeof (DATA) / sizeof (DATA[0]); i++) {
68 		bcopy(CB[i], param.cb, cblen);
69 
70 		args.in = RES[i];
71 		args.key = KEY[i];
72 
73 		args.inlen = RESLEN[i];
74 		args.keylen = KEYLEN[i];
75 
76 		errs += run_test(&args, DATA[i], DATALEN[i], ENCR_FG);
77 		(void) fprintf(stderr, "----------\n");
78 	}
79 
80 	if (errs != 0)
81 		(void) fprintf(stderr, "%d tests failed\n", errs);
82 
83 	return (errs);
84 }
85