xref: /openbsd-src/regress/sbin/iked/dh/dhtest.c (revision d54abb9d690576de9c48c8984482acf8ba43eaee)
1*d54abb9dStobhe /*	$OpenBSD: dhtest.c,v 1.5 2021/05/28 21:09:01 tobhe Exp $	*/
21d6a7cacSreyk /*	$EOM: dhtest.c,v 1.1 1998/07/18 21:14:20 provos Exp $	*/
31d6a7cacSreyk 
41d6a7cacSreyk /*
56943705dStobhe  * Copyright (c) 2020 Tobias Heider <tobhe@openbsd.org>
61d6a7cacSreyk  * Copyright (c) 2010 Reyk Floeter <reyk@vantronix.net>
71d6a7cacSreyk  * Copyright (c) 1998 Niels Provos.  All rights reserved.
81d6a7cacSreyk  *
91d6a7cacSreyk  * Redistribution and use in source and binary forms, with or without
101d6a7cacSreyk  * modification, are permitted provided that the following conditions
111d6a7cacSreyk  * are met:
121d6a7cacSreyk  * 1. Redistributions of source code must retain the above copyright
131d6a7cacSreyk  *    notice, this list of conditions and the following disclaimer.
141d6a7cacSreyk  * 2. Redistributions in binary form must reproduce the above copyright
151d6a7cacSreyk  *    notice, this list of conditions and the following disclaimer in the
161d6a7cacSreyk  *    documentation and/or other materials provided with the distribution.
171d6a7cacSreyk  *
181d6a7cacSreyk  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
191d6a7cacSreyk  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
201d6a7cacSreyk  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
211d6a7cacSreyk  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
221d6a7cacSreyk  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
231d6a7cacSreyk  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
241d6a7cacSreyk  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
251d6a7cacSreyk  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
261d6a7cacSreyk  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
271d6a7cacSreyk  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
281d6a7cacSreyk  */
291d6a7cacSreyk 
301d6a7cacSreyk /*
311d6a7cacSreyk  * This code was written under funding by Ericsson Radio Systems.
321d6a7cacSreyk  */
331d6a7cacSreyk 
341d6a7cacSreyk /*
351d6a7cacSreyk  * This module does a Diffie-Hellman Exchange
361d6a7cacSreyk  */
371d6a7cacSreyk 
386943705dStobhe #include <sys/types.h>
396943705dStobhe #include <sys/socket.h>
406943705dStobhe #include <sys/queue.h>
416943705dStobhe #include <sys/uio.h>
426943705dStobhe #include <event.h>
436943705dStobhe #include <imsg.h>
446943705dStobhe 
451d6a7cacSreyk #include <stdlib.h>
461d6a7cacSreyk #include <string.h>
471d6a7cacSreyk #include <stdio.h>
481d6a7cacSreyk 
491d6a7cacSreyk #include "dh.h"
506943705dStobhe #include "iked.h"
511d6a7cacSreyk 
521d6a7cacSreyk int
main(void)531d6a7cacSreyk main(void)
541d6a7cacSreyk {
556943705dStobhe 	int id;
566943705dStobhe 	struct ibuf *buf, *buf2;
576943705dStobhe 	struct ibuf *sec, *sec2;
586943705dStobhe 	uint8_t *raw, *raw2;
591d59cccaStobhe 	struct dh_group *group, *group2;
606943705dStobhe 	const char *name[] = { "MODP", "ECP", "CURVE25519" };
611d6a7cacSreyk 
621d6a7cacSreyk 	group_init();
631d6a7cacSreyk 
6445135ebcSreyk 	for (id = 0; id < 0xffff; id++) {
65*d54abb9dStobhe 		if (((group = group_get(id)) == NULL ||
66*d54abb9dStobhe 		    (group2 = group_get(id)) == NULL) ||
67*d54abb9dStobhe 		    group->spec->type == GROUP_SNTRUP761X25519)
681d6a7cacSreyk 			continue;
691d6a7cacSreyk 
706943705dStobhe 		dh_create_exchange(group, &buf, NULL);
716943705dStobhe 		dh_create_exchange(group2, &buf2, NULL);
721d6a7cacSreyk 
736943705dStobhe 		printf ("Testing group %d (%s-%d, length %zu): ", id,
7445135ebcSreyk 		    name[group->spec->type],
756943705dStobhe 		    group->spec->bits, ibuf_length(buf) * 8);
7645135ebcSreyk 
776943705dStobhe 		dh_create_shared(group, &sec, buf2);
786943705dStobhe 		dh_create_shared(group2, &sec2, buf);
791d6a7cacSreyk 
806943705dStobhe 		raw = ibuf_data(sec);
816943705dStobhe 		raw2 = ibuf_data(sec2);
821d6a7cacSreyk 
836943705dStobhe 		if (memcmp (raw, raw2, ibuf_length(sec))) {
841d6a7cacSreyk 			printf("FAILED\n");
851d6a7cacSreyk 			return (1);
861d6a7cacSreyk 		} else
871d6a7cacSreyk 			printf("OKAY\n");
881d6a7cacSreyk 
891d6a7cacSreyk 		group_free(group);
901d6a7cacSreyk 		group_free(group2);
911d6a7cacSreyk 	}
921d6a7cacSreyk 
931d6a7cacSreyk 	return (0);
941d6a7cacSreyk }
95