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