1272343Sngie/* $NetBSD: h_md5.c,v 1.5 2014/01/18 20:10:34 pgoyette Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2014 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie
29272343Sngie#include <err.h>
30272343Sngie#include <fcntl.h>
31272343Sngie#include <stdio.h>
32272343Sngie#include <string.h>
33272343Sngie
34272343Sngie#include <sys/ioctl.h>
35272343Sngie#include <sys/time.h>
36272343Sngie
37272343Sngie#include <crypto/cryptodev.h>
38272343Sngie
39272343Sngie/* Test vectors from RFC1321 */
40272343Sngie
41272343Sngieconst struct {
42272343Sngie	size_t len;
43272343Sngie	unsigned char plaintx[80];
44272343Sngie	unsigned char digest[16];
45272343Sngie} tests[] = {
46272343Sngie	{ 0, "",
47272343Sngie	  { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
48272343Sngie	    0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } },
49272343Sngie	{ 1, "a",
50272343Sngie	  { 0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8,
51272343Sngie	    0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61 } },
52272343Sngie	{ 3, "abc",
53272343Sngie	  { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
54272343Sngie	    0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } },
55272343Sngie	{ 14, "message digest",
56272343Sngie	  { 0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d,
57272343Sngie	    0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 } },
58272343Sngie	{ 26, "abcdefghijklmnopqrstuvwxyz",
59272343Sngie	  { 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00,
60272343Sngie	    0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b } },
61272343Sngie	{ 62, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
62272343Sngie	  { 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5,
63272343Sngie	    0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f } },
64272343Sngie	{ 80, "123456789012345678901234567890123456789012345678901234567890"
65272343Sngie		"12345678901234567890",
66272343Sngie	  { 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55,
67272343Sngie	    0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a } },
68272343Sngie};
69272343Sngie
70272343Sngieint
71272343Sngiemain(void)
72272343Sngie{
73272343Sngie	int fd, res;
74272343Sngie	size_t i;
75272343Sngie	struct session_op cs;
76272343Sngie	struct crypt_op co;
77272343Sngie	unsigned char buf[16];
78272343Sngie
79272343Sngie	fd = open("/dev/crypto", O_RDWR, 0);
80272343Sngie	if (fd < 0)
81272343Sngie		err(1, "open");
82272343Sngie	memset(&cs, 0, sizeof(cs));
83272343Sngie	cs.mac = CRYPTO_MD5;
84272343Sngie
85272343Sngie	for (i = 0; i < __arraycount(tests); i++) {
86272343Sngie		res = ioctl(fd, CIOCGSESSION, &cs);
87272343Sngie		if (res < 0)
88272343Sngie			err(1, "CIOCGSESSION test %zu", i);
89272343Sngie
90272343Sngie		memset(&co, 0, sizeof(co));
91272343Sngie		memset(&buf, 0, sizeof(buf));
92272343Sngie		co.ses = cs.ses;
93272343Sngie		co.op = COP_ENCRYPT;
94272343Sngie		co.len = tests[i].len;
95272343Sngie		co.src = __UNCONST(&tests[i].plaintx);
96272343Sngie		co.mac = buf;
97272343Sngie		res = ioctl(fd, CIOCCRYPT, &co);
98272343Sngie		if (res < 0)
99272343Sngie			err(1, "CIOCCRYPT test %zu", i);
100272343Sngie
101272343Sngie		if (memcmp(co.mac, tests[i].digest, sizeof(tests[i].digest)))
102272343Sngie			errx(1, "verification failed test %zu", i);
103272343Sngie
104272343Sngie		res = ioctl(fd, CIOCFSESSION, &cs.ses);
105272343Sngie		if (res < 0)
106272343Sngie			err(1, "CIOCFSESSION test %zu", i);
107272343Sngie	}
108272343Sngie	return 0;
109272343Sngie}
110