xref: /freebsd/contrib/netbsd-tests/crypto/opencrypto/h_md5hmac.c (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1*57718be8SEnji Cooper /* $NetBSD: h_md5hmac.c,v 1.4 2014/01/18 02:31:14 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 #define	MD5_HMAC_KEYLEN		16	/* Fixed key length supported */
40*57718be8SEnji Cooper 
41*57718be8SEnji Cooper /* Test data from RFC2202 */
42*57718be8SEnji Cooper const struct {
43*57718be8SEnji Cooper 	int num;
44*57718be8SEnji Cooper         size_t key_len;
45*57718be8SEnji Cooper         size_t len;
46*57718be8SEnji Cooper 	unsigned char key[80];
47*57718be8SEnji Cooper 	unsigned char data[80];
48*57718be8SEnji Cooper         unsigned char mac[16];
49*57718be8SEnji Cooper } tests[] = {
50*57718be8SEnji Cooper 	/* Test #1 */
51*57718be8SEnji Cooper 	{ 1, 16, 8,
52*57718be8SEnji Cooper 	  { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
53*57718be8SEnji Cooper 	    0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b },
54*57718be8SEnji Cooper 	  "Hi There",
55*57718be8SEnji Cooper 	  { 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c,
56*57718be8SEnji Cooper 	    0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d }
57*57718be8SEnji Cooper 	},
58*57718be8SEnji Cooper 	/* Test #2 */
59*57718be8SEnji Cooper 	{ 2, 4, 28,
60*57718be8SEnji Cooper 	  "Jefe",
61*57718be8SEnji Cooper 	  "what do ya want for nothing?",
62*57718be8SEnji Cooper 	  { 0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03,
63*57718be8SEnji Cooper 	    0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38 }
64*57718be8SEnji Cooper 	},
65*57718be8SEnji Cooper 	/* Test #3 */
66*57718be8SEnji Cooper 	{ 3, 16, 50,
67*57718be8SEnji Cooper 	  { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
68*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa },
69*57718be8SEnji Cooper 	  { 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
70*57718be8SEnji Cooper 	    0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
71*57718be8SEnji Cooper 	    0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
72*57718be8SEnji Cooper 	    0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
73*57718be8SEnji Cooper 	    0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
74*57718be8SEnji Cooper 	    0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd,
75*57718be8SEnji Cooper 	    0xdd, 0xdd },
76*57718be8SEnji Cooper 	  { 0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88,
77*57718be8SEnji Cooper 	    0xdb, 0xb8, 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6 }
78*57718be8SEnji Cooper 	},
79*57718be8SEnji Cooper 	/* Test #4 */
80*57718be8SEnji Cooper 	{ 4, 25, 50,
81*57718be8SEnji Cooper 	  { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
82*57718be8SEnji Cooper 	    0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
83*57718be8SEnji Cooper 	    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
84*57718be8SEnji Cooper 	    0x19 },
85*57718be8SEnji Cooper 	  { 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
86*57718be8SEnji Cooper 	    0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
87*57718be8SEnji Cooper 	    0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
88*57718be8SEnji Cooper 	    0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
89*57718be8SEnji Cooper 	    0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
90*57718be8SEnji Cooper 	    0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd, 0xcd,
91*57718be8SEnji Cooper 	    0xcd, 0xcd },
92*57718be8SEnji Cooper 	  { 0x69, 0x7e, 0xaf, 0x0a, 0xca, 0x3a, 0x3a, 0xea,
93*57718be8SEnji Cooper 	    0x3a, 0x75, 0x16, 0x47, 0x46, 0xff, 0xaa, 0x79 }
94*57718be8SEnji Cooper 	},
95*57718be8SEnji Cooper 	/* Test #5 */
96*57718be8SEnji Cooper 	{ 5, 16, 20,
97*57718be8SEnji Cooper 	  { 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,
98*57718be8SEnji Cooper 	    0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c },
99*57718be8SEnji Cooper 	  "Test With Truncation",
100*57718be8SEnji Cooper 	  { 0x56, 0x46, 0x1e, 0xf2, 0x34, 0x2e, 0xdc, 0x00,
101*57718be8SEnji Cooper 	    0xf9, 0xba, 0xb9, 0x95, 0x69, 0x0e, 0xfd, 0x4c }
102*57718be8SEnji Cooper 	},
103*57718be8SEnji Cooper 	/* Test #6 */
104*57718be8SEnji Cooper 	{ 6, 80, 54,
105*57718be8SEnji Cooper 	  { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
106*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
107*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
108*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
109*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
110*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
111*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
112*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
113*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
114*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa },
115*57718be8SEnji Cooper 	  "Test Using Larger Than Block-Size Key - Hash Key First",
116*57718be8SEnji Cooper 	  { 0x6b, 0x1a, 0xb7, 0xfe, 0x4b, 0xd7, 0xbf, 0x8f,
117*57718be8SEnji Cooper 	    0x0b, 0x62, 0xe6, 0xce, 0x61, 0xb9, 0xd0, 0xcd }
118*57718be8SEnji Cooper 	},
119*57718be8SEnji Cooper 	/* Test #7 */
120*57718be8SEnji Cooper 	{ 7, 80, 73,
121*57718be8SEnji Cooper 	  { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
122*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
123*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
124*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
125*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
126*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
127*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
128*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
129*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
130*57718be8SEnji Cooper 	    0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa },
131*57718be8SEnji Cooper 	  "Test Using Larger Than Block-Size Key and Larger "
132*57718be8SEnji Cooper 		"Than One Block-Size Data",
133*57718be8SEnji Cooper 	{ 0x6f, 0x63, 0x0f, 0xad, 0x67, 0xcd, 0xa0, 0xee,
134*57718be8SEnji Cooper 	  0x1f, 0xb1, 0xf5, 0x62, 0xdb, 0x3a, 0xa5, 0x3e }
135*57718be8SEnji Cooper 	},
136*57718be8SEnji Cooper };
137*57718be8SEnji Cooper 
138*57718be8SEnji Cooper int
main(void)139*57718be8SEnji Cooper main(void)
140*57718be8SEnji Cooper {
141*57718be8SEnji Cooper 	size_t i;
142*57718be8SEnji Cooper 	int fd, res;
143*57718be8SEnji Cooper 	struct session_op cs;
144*57718be8SEnji Cooper 	struct crypt_op co;
145*57718be8SEnji Cooper 	unsigned char buf[16];
146*57718be8SEnji Cooper 
147*57718be8SEnji Cooper 	fd = open("/dev/crypto", O_RDWR, 0);
148*57718be8SEnji Cooper 	if (fd < 0)
149*57718be8SEnji Cooper 		err(1, "open");
150*57718be8SEnji Cooper 	for (i = 0; i < __arraycount(tests); i++) {
151*57718be8SEnji Cooper 		if (tests[i].key_len != MD5_HMAC_KEYLEN)
152*57718be8SEnji Cooper 			continue;
153*57718be8SEnji Cooper 
154*57718be8SEnji Cooper 		memset(&cs, 0, sizeof(cs));
155*57718be8SEnji Cooper 		cs.mac = CRYPTO_MD5_HMAC;
156*57718be8SEnji Cooper 		cs.mackeylen = tests[i].key_len;
157*57718be8SEnji Cooper 		cs.mackey = __UNCONST(&tests[i].key);
158*57718be8SEnji Cooper 		res = ioctl(fd, CIOCGSESSION, &cs);
159*57718be8SEnji Cooper 		if (res < 0)
160*57718be8SEnji Cooper 			err(1, "CIOCGSESSION test %d", tests[i].num);
161*57718be8SEnji Cooper 
162*57718be8SEnji Cooper 		memset(&co, 0, sizeof(co));
163*57718be8SEnji Cooper 		memset(buf, 0, sizeof(buf));
164*57718be8SEnji Cooper 		co.ses = cs.ses;
165*57718be8SEnji Cooper 		co.op = COP_ENCRYPT;
166*57718be8SEnji Cooper 		co.len = tests[i].len;
167*57718be8SEnji Cooper 		co.src = __UNCONST(&tests[i].data);
168*57718be8SEnji Cooper 		co.mac = buf;
169*57718be8SEnji Cooper 		res = ioctl(fd, CIOCCRYPT, &co);
170*57718be8SEnji Cooper 		if (res < 0)
171*57718be8SEnji Cooper 			err(1, "CIOCCRYPT test %d", tests[i].num);
172*57718be8SEnji Cooper 
173*57718be8SEnji Cooper 		if (memcmp(co.mac, tests[i].mac, sizeof(tests[i].mac)))
174*57718be8SEnji Cooper 			errx(1, "verification failed test %d", tests[i].num);
175*57718be8SEnji Cooper 
176*57718be8SEnji Cooper 		res = ioctl(fd, CIOCFSESSION, &cs.ses);
177*57718be8SEnji Cooper 		if (res < 0)
178*57718be8SEnji Cooper 			err(1, "CIOCFSESSION test %d", tests[i].num);
179*57718be8SEnji Cooper 	}
180*57718be8SEnji Cooper 	return 0;
181*57718be8SEnji Cooper }
182