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