1*fefcb31aSreyk /* $OpenBSD: dhtest.c,v 1.2 2010/06/29 19:50:16 reyk Exp $ */
271ec8d3aScloder /* $EOM: dhtest.c,v 1.1 1998/07/18 21:14:20 provos Exp $ */
371ec8d3aScloder
471ec8d3aScloder /*
5*fefcb31aSreyk * Copyright (c) 2010 Reyk Floeter <reyk@vantronix.net>
671ec8d3aScloder * Copyright (c) 1998 Niels Provos. All rights reserved.
771ec8d3aScloder *
871ec8d3aScloder * Redistribution and use in source and binary forms, with or without
971ec8d3aScloder * modification, are permitted provided that the following conditions
1071ec8d3aScloder * are met:
1171ec8d3aScloder * 1. Redistributions of source code must retain the above copyright
1271ec8d3aScloder * notice, this list of conditions and the following disclaimer.
1371ec8d3aScloder * 2. Redistributions in binary form must reproduce the above copyright
1471ec8d3aScloder * notice, this list of conditions and the following disclaimer in the
1571ec8d3aScloder * documentation and/or other materials provided with the distribution.
1671ec8d3aScloder *
1771ec8d3aScloder * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1871ec8d3aScloder * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1971ec8d3aScloder * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2071ec8d3aScloder * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2171ec8d3aScloder * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2271ec8d3aScloder * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2371ec8d3aScloder * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2471ec8d3aScloder * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2571ec8d3aScloder * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2671ec8d3aScloder * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2771ec8d3aScloder */
2871ec8d3aScloder
2971ec8d3aScloder /*
3071ec8d3aScloder * This code was written under funding by Ericsson Radio Systems.
3171ec8d3aScloder */
3271ec8d3aScloder
3371ec8d3aScloder /*
3471ec8d3aScloder * This module does a Diffie-Hellman Exchange
3571ec8d3aScloder */
3671ec8d3aScloder
3771ec8d3aScloder #include <stdlib.h>
3871ec8d3aScloder #include <string.h>
3971ec8d3aScloder #include <stdio.h>
4071ec8d3aScloder
4171ec8d3aScloder #include "dh.h"
4271ec8d3aScloder
4371ec8d3aScloder int
main(void)4471ec8d3aScloder main(void)
4571ec8d3aScloder {
46*fefcb31aSreyk int len, id;
47*fefcb31aSreyk char buf[DH_MAXSZ], buf2[DH_MAXSZ];
48*fefcb31aSreyk char sec[DH_MAXSZ], sec2[DH_MAXSZ];
4971ec8d3aScloder struct group *group, *group2;
50*fefcb31aSreyk const char *name[] = { "MODP", "EC2N", "ECP" };
5171ec8d3aScloder
5271ec8d3aScloder group_init();
5371ec8d3aScloder
54*fefcb31aSreyk for (id = 0; id < 0xff; id++) {
55*fefcb31aSreyk if ((group = group_get(id)) == NULL ||
56*fefcb31aSreyk (group2 = group_get(id)) == NULL)
57*fefcb31aSreyk continue;
5871ec8d3aScloder
59*fefcb31aSreyk printf ("Testing group %d (%s%d): ", id,
60*fefcb31aSreyk name[group->spec->type],
61*fefcb31aSreyk group->spec->bits);
62*fefcb31aSreyk
6371ec8d3aScloder len = dh_getlen(group);
64*fefcb31aSreyk
6571ec8d3aScloder dh_create_exchange(group, buf);
6671ec8d3aScloder dh_create_exchange(group2, buf2);
6771ec8d3aScloder
6871ec8d3aScloder dh_create_shared(group, sec, buf2);
6971ec8d3aScloder dh_create_shared(group2, sec2, buf);
7071ec8d3aScloder
71*fefcb31aSreyk if (memcmp (sec, sec2, len)) {
72*fefcb31aSreyk printf("FAILED\n");
73*fefcb31aSreyk return (1);
74*fefcb31aSreyk } else
75*fefcb31aSreyk printf("OKAY\n");
7671ec8d3aScloder
7771ec8d3aScloder group_free(group);
7871ec8d3aScloder group_free(group2);
79*fefcb31aSreyk }
8071ec8d3aScloder
81*fefcb31aSreyk return (0);
8271ec8d3aScloder }
83