1 /* $NetBSD: h_gcm.c,v 1.2 2014/01/17 14:16:08 pgoyette Exp $ */
4 * Copyright (c) 2014 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
34 #include <sys/ioctl.h>
37 #include <crypto/cryptodev.h>
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
46 const unsigned char hash[16] = {
47 0xab, 0x6e, 0x47, 0xd4, 0x2c, 0xec, 0x13, 0xbd,
48 0xf5, 0x3a, 0x67, 0xb2, 0x12, 0x57, 0xbd, 0xdf
57 unsigned char databuf[16];
58 unsigned char macbuf[16];
59 unsigned char databuf2[16];
61 fd = open("/dev/crypto", O_RDWR, 0);
64 memset(&cs, 0, sizeof(cs));
65 cs.mac = CRYPTO_AES_128_GMAC;
66 cs.mackeylen = sizeof(key);
68 cs.cipher = CRYPTO_AES_GCM_16;
70 cs.keylen = sizeof(key);
71 res = ioctl(fd, CIOCGSESSION, &cs);
73 err(1, "CIOCGSESSION");
75 memset(&co, 0, sizeof(co));
76 memset(databuf, 0, sizeof(databuf));
77 memset(macbuf, 0, sizeof(macbuf));
80 co.len = sizeof(plaintx);
85 res = ioctl(fd, CIOCCRYPT, &co);
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");
96 for (i = 0; i < sizeof(databuf); i++)
97 printf("%02x ", databuf[i]);
102 for (i = 0; i < sizeof(macbuf); i++)
103 printf("%02x ", macbuf[i]);
107 memset(databuf2, 0, sizeof(databuf2));
108 memset(macbuf, 0, sizeof(macbuf));
111 co.len = sizeof(databuf);
116 res = ioctl(fd, CIOCCRYPT, &co);
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");