xref: /illumos-gate/usr/src/test/crypto-tests/tests/modes/aes/gcm/aes_gcm.c (revision bf5d9f18edeb77c14df996d367853599bdd43fd1)
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 #include "cryptotest.h"
19 #include "aes_gcm.h"
20 
21 const size_t GCM_SPEC_TAG_LEN = 16;
22 int
23 main(void)
24 {
25 	int errs = 0;
26 	int i;
27 	uint8_t N[1024];
28 	size_t taglen = GCM_SPEC_TAG_LEN;
29 
30 	CK_AES_GCM_PARAMS param;
31 	cryptotest_t args;
32 
33 	bzero(&param, sizeof (param));
34 	param.ulTagBits = taglen*8;
35 
36 	args.out = N;
37 	args.param = &param;
38 
39 	args.outlen = sizeof (N);
40 	args.plen = sizeof (param);
41 
42 	args.mechname = SUN_CKM_AES_GCM;
43 	args.updatelen = 1;
44 
45 	for (i = 0; i < sizeof (DATA) / sizeof (DATA[0]); i++) {
46 		args.in = DATA[i];
47 		args.key = KEY[i];
48 
49 		args.inlen = DATALEN[i];
50 		args.keylen = KEYLEN[i];
51 
52 		param.pIv = IV[i];
53 		param.ulIvLen = IVLEN[i];
54 		param.ulIvBits = IVLEN[i]*8;
55 		param.pAAD = AUTH[i];
56 		param.ulAADLen = AUTHLEN[i];
57 
58 
59 		errs += run_test(&args, RES[i], RESLEN[i], ENCR_FG);
60 		(void) fprintf(stderr, "----------\n");
61 	}
62 
63 	(void) fprintf(stderr, "\t\t\t=== decrypt ===\n----------\n\n");
64 
65 	for (i = 0; i < sizeof (DATA) / sizeof (DATA[0]); i++) {
66 		args.in = RES[i];
67 		args.key = KEY[i];
68 
69 		args.inlen = RESLEN[i];
70 		args.keylen = KEYLEN[i];
71 
72 		param.pIv = IV[i];
73 		param.ulIvLen = IVLEN[i];
74 		param.ulIvBits = IVLEN[i]*8;
75 		param.pAAD = AUTH[i];
76 		param.ulAADLen = AUTHLEN[i];
77 
78 
79 		errs += run_test(&args, DATA[i], DATALEN[i], DECR_FG);
80 		(void) fprintf(stderr, "----------\n");
81 	}
82 
83 	if (errs != 0)
84 		(void) fprintf(stderr, "%d tests failed\n", errs);
85 
86 	return (errs);
87 }
88