xref: /openbsd-src/regress/sbin/iked/dh/dhtest.c (revision d54abb9d690576de9c48c8984482acf8ba43eaee)
1 /*	$OpenBSD: dhtest.c,v 1.5 2021/05/28 21:09:01 tobhe Exp $	*/
2 /*	$EOM: dhtest.c,v 1.1 1998/07/18 21:14:20 provos Exp $	*/
3 
4 /*
5  * Copyright (c) 2020 Tobias Heider <tobhe@openbsd.org>
6  * Copyright (c) 2010 Reyk Floeter <reyk@vantronix.net>
7  * Copyright (c) 1998 Niels Provos.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /*
31  * This code was written under funding by Ericsson Radio Systems.
32  */
33 
34 /*
35  * This module does a Diffie-Hellman Exchange
36  */
37 
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/queue.h>
41 #include <sys/uio.h>
42 #include <event.h>
43 #include <imsg.h>
44 
45 #include <stdlib.h>
46 #include <string.h>
47 #include <stdio.h>
48 
49 #include "dh.h"
50 #include "iked.h"
51 
52 int
main(void)53 main(void)
54 {
55 	int id;
56 	struct ibuf *buf, *buf2;
57 	struct ibuf *sec, *sec2;
58 	uint8_t *raw, *raw2;
59 	struct dh_group *group, *group2;
60 	const char *name[] = { "MODP", "ECP", "CURVE25519" };
61 
62 	group_init();
63 
64 	for (id = 0; id < 0xffff; id++) {
65 		if (((group = group_get(id)) == NULL ||
66 		    (group2 = group_get(id)) == NULL) ||
67 		    group->spec->type == GROUP_SNTRUP761X25519)
68 			continue;
69 
70 		dh_create_exchange(group, &buf, NULL);
71 		dh_create_exchange(group2, &buf2, NULL);
72 
73 		printf ("Testing group %d (%s-%d, length %zu): ", id,
74 		    name[group->spec->type],
75 		    group->spec->bits, ibuf_length(buf) * 8);
76 
77 		dh_create_shared(group, &sec, buf2);
78 		dh_create_shared(group2, &sec2, buf);
79 
80 		raw = ibuf_data(sec);
81 		raw2 = ibuf_data(sec2);
82 
83 		if (memcmp (raw, raw2, ibuf_length(sec))) {
84 			printf("FAILED\n");
85 			return (1);
86 		} else
87 			printf("OKAY\n");
88 
89 		group_free(group);
90 		group_free(group2);
91 	}
92 
93 	return (0);
94 }
95