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