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