h_md5.c revision 1.5.4.2 1 1.5.4.2 yamt /* $NetBSD: h_md5.c,v 1.5.4.2 2014/05/22 11:42:17 yamt Exp $ */
2 1.5.4.2 yamt
3 1.5.4.2 yamt /*-
4 1.5.4.2 yamt * Copyright (c) 2014 The NetBSD Foundation, Inc.
5 1.5.4.2 yamt * All rights reserved.
6 1.5.4.2 yamt *
7 1.5.4.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.5.4.2 yamt * modification, are permitted provided that the following conditions
9 1.5.4.2 yamt * are met:
10 1.5.4.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.5.4.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.5.4.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.5.4.2 yamt * notice, this list of conditions and the following disclaimer in the
14 1.5.4.2 yamt * documentation and/or other materials provided with the distribution.
15 1.5.4.2 yamt *
16 1.5.4.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.5.4.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.5.4.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.5.4.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.5.4.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.5.4.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.5.4.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.5.4.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.5.4.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.5.4.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.5.4.2 yamt * POSSIBILITY OF SUCH DAMAGE.
27 1.5.4.2 yamt */
28 1.5.4.2 yamt
29 1.5.4.2 yamt #include <err.h>
30 1.5.4.2 yamt #include <fcntl.h>
31 1.5.4.2 yamt #include <stdio.h>
32 1.5.4.2 yamt #include <string.h>
33 1.5.4.2 yamt
34 1.5.4.2 yamt #include <sys/ioctl.h>
35 1.5.4.2 yamt #include <sys/time.h>
36 1.5.4.2 yamt
37 1.5.4.2 yamt #include <crypto/cryptodev.h>
38 1.5.4.2 yamt
39 1.5.4.2 yamt /* Test vectors from RFC1321 */
40 1.5.4.2 yamt
41 1.5.4.2 yamt const struct {
42 1.5.4.2 yamt size_t len;
43 1.5.4.2 yamt unsigned char plaintx[80];
44 1.5.4.2 yamt unsigned char digest[16];
45 1.5.4.2 yamt } tests[] = {
46 1.5.4.2 yamt { 0, "",
47 1.5.4.2 yamt { 0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
48 1.5.4.2 yamt 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e } },
49 1.5.4.2 yamt { 1, "a",
50 1.5.4.2 yamt { 0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8,
51 1.5.4.2 yamt 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61 } },
52 1.5.4.2 yamt { 3, "abc",
53 1.5.4.2 yamt { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0,
54 1.5.4.2 yamt 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 } },
55 1.5.4.2 yamt { 14, "message digest",
56 1.5.4.2 yamt { 0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d,
57 1.5.4.2 yamt 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0 } },
58 1.5.4.2 yamt { 26, "abcdefghijklmnopqrstuvwxyz",
59 1.5.4.2 yamt { 0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00,
60 1.5.4.2 yamt 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b } },
61 1.5.4.2 yamt { 62, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
62 1.5.4.2 yamt { 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5,
63 1.5.4.2 yamt 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f } },
64 1.5.4.2 yamt { 80, "123456789012345678901234567890123456789012345678901234567890"
65 1.5.4.2 yamt "12345678901234567890",
66 1.5.4.2 yamt { 0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55,
67 1.5.4.2 yamt 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a } },
68 1.5.4.2 yamt };
69 1.5.4.2 yamt
70 1.5.4.2 yamt int
71 1.5.4.2 yamt main(void)
72 1.5.4.2 yamt {
73 1.5.4.2 yamt int fd, res;
74 1.5.4.2 yamt size_t i;
75 1.5.4.2 yamt struct session_op cs;
76 1.5.4.2 yamt struct crypt_op co;
77 1.5.4.2 yamt unsigned char buf[16];
78 1.5.4.2 yamt
79 1.5.4.2 yamt fd = open("/dev/crypto", O_RDWR, 0);
80 1.5.4.2 yamt if (fd < 0)
81 1.5.4.2 yamt err(1, "open");
82 1.5.4.2 yamt memset(&cs, 0, sizeof(cs));
83 1.5.4.2 yamt cs.mac = CRYPTO_MD5;
84 1.5.4.2 yamt
85 1.5.4.2 yamt for (i = 0; i < __arraycount(tests); i++) {
86 1.5.4.2 yamt res = ioctl(fd, CIOCGSESSION, &cs);
87 1.5.4.2 yamt if (res < 0)
88 1.5.4.2 yamt err(1, "CIOCGSESSION test %zu", i);
89 1.5.4.2 yamt
90 1.5.4.2 yamt memset(&co, 0, sizeof(co));
91 1.5.4.2 yamt memset(&buf, 0, sizeof(buf));
92 1.5.4.2 yamt co.ses = cs.ses;
93 1.5.4.2 yamt co.op = COP_ENCRYPT;
94 1.5.4.2 yamt co.len = tests[i].len;
95 1.5.4.2 yamt co.src = __UNCONST(&tests[i].plaintx);
96 1.5.4.2 yamt co.mac = buf;
97 1.5.4.2 yamt res = ioctl(fd, CIOCCRYPT, &co);
98 1.5.4.2 yamt if (res < 0)
99 1.5.4.2 yamt err(1, "CIOCCRYPT test %zu", i);
100 1.5.4.2 yamt
101 1.5.4.2 yamt if (memcmp(co.mac, tests[i].digest, sizeof(tests[i].digest)))
102 1.5.4.2 yamt errx(1, "verification failed test %zu", i);
103 1.5.4.2 yamt
104 1.5.4.2 yamt res = ioctl(fd, CIOCFSESSION, &cs.ses);
105 1.5.4.2 yamt if (res < 0)
106 1.5.4.2 yamt err(1, "CIOCFSESSION test %zu", i);
107 1.5.4.2 yamt }
108 1.5.4.2 yamt return 0;
109 1.5.4.2 yamt }
110