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