Stefan Seyfried <seife+obs@b1-systems.com>
[vpnc.git] / dh.c
blob4b69a84fd24066049e25af1fbccf5d0e8a200e4a
1 /* borrowed from isakmpd-20030718 (-; */
3 /* $OpenBSD: dh.c,v 1.8 2003/06/03 14:28:16 ho Exp $ */
4 /* $EOM: dh.c,v 1.5 1999/04/17 23:20:22 niklas Exp $ */
6 /*
7 * Copyright (c) 1998 Niels Provos. All rights reserved.
8 * Copyright (c) 1999 Niklas Hallqvist. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * This code was written under funding by Ericsson Radio Systems.
35 #include "math_group.h"
36 #include "dh.h"
39 * Returns the length of our exchange value.
42 int dh_getlen(struct group *group)
44 return group->getlen(group);
48 * Creates the exchange value we are offering to the other party.
49 * Each time this function is called a new value is created, that
50 * means the application has to save the exchange value itself,
51 * dh_create_exchange should only be called once.
53 int dh_create_exchange(struct group *group, unsigned char *buf)
55 if (group->setrandom(group, group->c))
56 return -1;
57 if (group->operation(group, group->a, group->gen, group->c))
58 return -1;
59 group->getraw(group, group->a, buf);
60 return 0;
64 * Creates the Diffie-Hellman shared secret in 'secret', where 'exchange'
65 * is the exchange value offered by the other party. No length verification
66 * is done for the value, the application has to do that.
68 int dh_create_shared(struct group *group, unsigned char *secret, unsigned char *exchange)
70 if (group->setraw(group, group->b, exchange, group->getlen(group)))
71 return -1;
72 if (group->operation(group, group->a, group->b, group->c))
73 return -1;
74 group->getraw(group, group->a, secret);
75 return 0;