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