dh.c revision 1.1
1/*	$Id: dh.c,v 1.1 1998/11/15 00:03:48 niklas Exp $	*/
2
3/*
4 * Copyright (c) 1998 Niels Provos.  All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed by Ericsson Radio Systems.
17 * 4. The name of the author may not be used to endorse or promote products
18 *    derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * This code was written under funding by Ericsson Radio Systems.
34 */
35
36#include <sys/param.h>
37
38#include "math_group.h"
39#include "dh.h"
40#include "log.h"
41
42/*
43 * Returns the length of our exchange value.
44 */
45
46int
47dh_getlen (struct group *group)
48{
49  return group->getlen (group);
50}
51
52/*
53 * Creates the exchange value we are offering to the other party.
54 * Each time this function is called a new value is created, that
55 * means the application has to save the exchange value itself,
56 * dh_create_exchange should only be called once.
57 */
58
59void
60dh_create_exchange (struct group *group, u_int8_t *buf)
61{
62  group->setrandom (group, group->c);
63  group->operation (group, group->a, group->gen, group->c);
64  group->getraw (group, group->a, buf);
65}
66
67/*
68 * Creates the Diffie-Hellman shared secret in 'secret', where 'exchange'
69 * is the exchange value offered by the other party. No length verification
70 * is done for the value, the application has to do that.
71 */
72
73void
74dh_create_shared (struct group *group, u_int8_t *secret, u_int8_t *exchange)
75{
76  group->setraw (group, group->b, exchange, group->getlen(group));
77  group->operation (group, group->a, group->b, group->c);
78  group->getraw (group, group->a, secret);
79}
80