xref: /freebsd/contrib/netbsd-tests/crypto/opencrypto/h_gcm.c (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
1 /* $NetBSD: h_gcm.c,v 1.2 2014/01/17 14:16:08 pgoyette Exp $ */
2 
3 /*-
4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <err.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <string.h>
33 
34 #include <sys/ioctl.h>
35 #include <sys/time.h>
36 
37 #include <crypto/cryptodev.h>
38 
39 unsigned char key[20] = { 0 };
40 char plaintx[16] = { 0 };
41 unsigned char iv[16] = { 0 };
42 const unsigned char ciphertx[16] = {
43 	0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92,
44 	0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe, 0x78
45 };
46 const unsigned char hash[16] = {
47 	0xab, 0x6e, 0x47, 0xd4, 0x2c, 0xec, 0x13, 0xbd,
48 	0xf5, 0x3a, 0x67, 0xb2, 0x12, 0x57, 0xbd, 0xdf
49 };
50 
51 int
52 main(void)
53 {
54 	int fd, res;
55 	struct session_op cs;
56 	struct crypt_op co;
57 	unsigned char databuf[16];
58 	unsigned char macbuf[16];
59 	unsigned char databuf2[16];
60 
61 	fd = open("/dev/crypto", O_RDWR, 0);
62 	if (fd < 0)
63 		err(1, "open");
64 	memset(&cs, 0, sizeof(cs));
65 	cs.mac = CRYPTO_AES_128_GMAC;
66 	cs.mackeylen = sizeof(key);
67 	cs.mackey = key;
68 	cs.cipher = CRYPTO_AES_GCM_16;
69 	cs.key = key;
70 	cs.keylen = sizeof(key);
71 	res = ioctl(fd, CIOCGSESSION, &cs);
72 	if (res < 0)
73 		err(1, "CIOCGSESSION");
74 
75 	memset(&co, 0, sizeof(co));
76 	memset(databuf, 0, sizeof(databuf));
77 	memset(macbuf, 0, sizeof(macbuf));
78 	co.ses = cs.ses;
79 	co.op = COP_ENCRYPT;
80 	co.len = sizeof(plaintx);
81 	co.src = plaintx;
82 	co.dst = databuf;
83 	co.mac = macbuf;
84 	co.iv = iv;
85 	res = ioctl(fd, CIOCCRYPT, &co);
86 	if (res < 0)
87 		err(1, "CIOCCRYPT");
88 #if 1
89 	if (memcmp(co.dst, ciphertx, sizeof(ciphertx)))
90 		errx(1, "verification failed");
91 	if (memcmp(macbuf, hash, sizeof(hash)))
92 		errx(1, "hash failed");
93 #else
94 	{
95 		int i;
96 		for (i = 0; i < sizeof(databuf); i++)
97 			printf("%02x ", databuf[i]);
98 		printf("\n");
99 	}
100 	{
101 		int i;
102 		for (i = 0; i < sizeof(macbuf); i++)
103 			printf("%02x ", macbuf[i]);
104 		printf("\n");
105 	}
106 #endif
107 	memset(databuf2, 0, sizeof(databuf2));
108 	memset(macbuf, 0, sizeof(macbuf));
109 	co.ses = cs.ses;
110 	co.op = COP_DECRYPT;
111 	co.len = sizeof(databuf);
112 	co.src = databuf;
113 	co.dst = databuf2;
114 	co.mac = macbuf;
115 	co.iv = iv;
116 	res = ioctl(fd, CIOCCRYPT, &co);
117 	if (res < 0)
118 		err(1, "CIOCCRYPT");
119 
120 	if (memcmp(co.dst, plaintx, sizeof(plaintx)))
121 		errx(1, "verification failed");
122 	if (memcmp(macbuf, hash, sizeof(hash)))
123 		errx(1, "hash failed");
124 
125 	return 0;
126 }
127