1*57718be8SEnji Cooper /* $NetBSD: h_xcbcmac.c,v 1.4 2014/01/16 23:56:04 joerg Exp $ */
2*57718be8SEnji Cooper
3*57718be8SEnji Cooper /*-
4*57718be8SEnji Cooper * Copyright (c) 2014 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper * All rights reserved.
6*57718be8SEnji Cooper *
7*57718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
8*57718be8SEnji Cooper * modification, are permitted provided that the following conditions
9*57718be8SEnji Cooper * are met:
10*57718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
11*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
12*57718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
13*57718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
14*57718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
15*57718be8SEnji Cooper *
16*57718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*57718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*57718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*57718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*57718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*57718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*57718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*57718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*57718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*57718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*57718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
27*57718be8SEnji Cooper */
28*57718be8SEnji Cooper
29*57718be8SEnji Cooper #include <err.h>
30*57718be8SEnji Cooper #include <fcntl.h>
31*57718be8SEnji Cooper #include <stdio.h>
32*57718be8SEnji Cooper #include <string.h>
33*57718be8SEnji Cooper
34*57718be8SEnji Cooper #include <sys/ioctl.h>
35*57718be8SEnji Cooper #include <sys/time.h>
36*57718be8SEnji Cooper
37*57718be8SEnji Cooper #include <crypto/cryptodev.h>
38*57718be8SEnji Cooper
39*57718be8SEnji Cooper
40*57718be8SEnji Cooper /* test vectors from RFC3566 */
41*57718be8SEnji Cooper unsigned char key[16] = {
42*57718be8SEnji Cooper 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
43*57718be8SEnji Cooper 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
44*57718be8SEnji Cooper };
45*57718be8SEnji Cooper char plaintx[1000] = {
46*57718be8SEnji Cooper 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
47*57718be8SEnji Cooper 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
48*57718be8SEnji Cooper 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
49*57718be8SEnji Cooper 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
50*57718be8SEnji Cooper 0x20, 0x21
51*57718be8SEnji Cooper };
52*57718be8SEnji Cooper const struct {
53*57718be8SEnji Cooper size_t len;
54*57718be8SEnji Cooper unsigned char mac[12];
55*57718be8SEnji Cooper } tests[] = {
56*57718be8SEnji Cooper { 0, { 0x75, 0xf0, 0x25, 0x1d, 0x52, 0x8a,
57*57718be8SEnji Cooper 0xc0, 0x1c, 0x45, 0x73, 0xdf, 0xd5 } },
58*57718be8SEnji Cooper { 3, { 0x5b, 0x37, 0x65, 0x80, 0xae, 0x2f,
59*57718be8SEnji Cooper 0x19, 0xaf, 0xe7, 0x21, 0x9c, 0xee } },
60*57718be8SEnji Cooper { 16, { 0xd2, 0xa2, 0x46, 0xfa, 0x34, 0x9b,
61*57718be8SEnji Cooper 0x68, 0xa7, 0x99, 0x98, 0xa4, 0x39 } },
62*57718be8SEnji Cooper { 20, { 0x47, 0xf5, 0x1b, 0x45, 0x64, 0x96,
63*57718be8SEnji Cooper 0x62, 0x15, 0xb8, 0x98, 0x5c, 0x63 } },
64*57718be8SEnji Cooper { 32, { 0xf5, 0x4f, 0x0e, 0xc8, 0xd2, 0xb9,
65*57718be8SEnji Cooper 0xf3, 0xd3, 0x68, 0x07, 0x73, 0x4b } },
66*57718be8SEnji Cooper { 34, { 0xbe, 0xcb, 0xb3, 0xbc, 0xcd, 0xb5,
67*57718be8SEnji Cooper 0x18, 0xa3, 0x06, 0x77, 0xd5, 0x48 } },
68*57718be8SEnji Cooper { 1000, { 0xf0, 0xda, 0xfe, 0xe8, 0x95, 0xdb,
69*57718be8SEnji Cooper 0x30, 0x25, 0x37, 0x61, 0x10, 0x3b } },
70*57718be8SEnji Cooper };
71*57718be8SEnji Cooper
72*57718be8SEnji Cooper int
main(void)73*57718be8SEnji Cooper main(void)
74*57718be8SEnji Cooper {
75*57718be8SEnji Cooper int fd, res;
76*57718be8SEnji Cooper size_t i;
77*57718be8SEnji Cooper struct session_op cs;
78*57718be8SEnji Cooper struct crypt_op co;
79*57718be8SEnji Cooper unsigned char buf[16];
80*57718be8SEnji Cooper
81*57718be8SEnji Cooper fd = open("/dev/crypto", O_RDWR, 0);
82*57718be8SEnji Cooper if (fd < 0)
83*57718be8SEnji Cooper err(1, "open");
84*57718be8SEnji Cooper memset(&cs, 0, sizeof(cs));
85*57718be8SEnji Cooper cs.mac = CRYPTO_AES_XCBC_MAC_96;
86*57718be8SEnji Cooper cs.mackeylen = sizeof(key);
87*57718be8SEnji Cooper cs.mackey = key;
88*57718be8SEnji Cooper res = ioctl(fd, CIOCGSESSION, &cs);
89*57718be8SEnji Cooper if (res < 0)
90*57718be8SEnji Cooper err(1, "CIOCGSESSION");
91*57718be8SEnji Cooper
92*57718be8SEnji Cooper for (i = 0; i < __arraycount(tests); i++) {
93*57718be8SEnji Cooper memset(&co, 0, sizeof(co));
94*57718be8SEnji Cooper memset(buf, 0, sizeof(buf));
95*57718be8SEnji Cooper if (tests[i].len == sizeof(plaintx))
96*57718be8SEnji Cooper memset(&plaintx, 0, sizeof(plaintx));
97*57718be8SEnji Cooper co.ses = cs.ses;
98*57718be8SEnji Cooper co.op = COP_ENCRYPT;
99*57718be8SEnji Cooper co.len = tests[i].len;
100*57718be8SEnji Cooper co.src = plaintx;
101*57718be8SEnji Cooper co.mac = buf;
102*57718be8SEnji Cooper res = ioctl(fd, CIOCCRYPT, &co);
103*57718be8SEnji Cooper if (res < 0)
104*57718be8SEnji Cooper err(1, "CIOCCRYPT test %zu", i);
105*57718be8SEnji Cooper if (memcmp(buf, &tests[i].mac, sizeof(tests[i].mac)))
106*57718be8SEnji Cooper errx(1, "verification failed test %zu", i);
107*57718be8SEnji Cooper }
108*57718be8SEnji Cooper return 0;
109*57718be8SEnji Cooper }
110