1/* 2 * dh.h: Diffie-Hellman header file. 3 * 4 * Code copied from openssl distribution and 5 * Modified just enough so that compiles and runs standalone 6 * 7 * Copyright (C) 2010, Broadcom Corporation. All Rights Reserved. 8 * 9 * Permission to use, copy, modify, and/or distribute this software for any 10 * purpose with or without fee is hereby granted, provided that the above 11 * copyright notice and this permission notice appear in all copies. 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 16 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 18 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 19 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 20 * 21 * $Id: dh.h,v 1.3.622.1 2010/08/13 18:00:16 Exp $ 22 */ 23/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 24 * All rights reserved. 25 * 26 * This package is an SSL implementation written 27 * by Eric Young (eay@cryptsoft.com). 28 * The implementation was written so as to conform with Netscapes SSL. 29 * 30 * This library is free for commercial and non-commercial use as long as 31 * the following conditions are aheared to. The following conditions 32 * apply to all code found in this distribution, be it the RC4, RSA, 33 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 34 * included with this distribution is covered by the same copyright terms 35 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 36 * 37 * Copyright remains Eric Young's, and as such any Copyright notices in 38 * the code are not to be removed. 39 * If this package is used in a product, Eric Young should be given attribution 40 * as the author of the parts of the library used. 41 * This can be in the form of a textual message at program startup or 42 * in documentation (online or textual) provided with the package. 43 * 44 * Redistribution and use in source and binary forms, with or without 45 * modification, are permitted provided that the following conditions 46 * are met: 47 * 1. Redistributions of source code must retain the copyright 48 * notice, this list of conditions and the following disclaimer. 49 * 2. Redistributions in binary form must reproduce the above copyright 50 * notice, this list of conditions and the following disclaimer in the 51 * documentation and/or other materials provided with the distribution. 52 * 3. All advertising materials mentioning features or use of this software 53 * must display the following acknowledgement: 54 * "This product includes cryptographic software written by 55 * Eric Young (eay@cryptsoft.com)" 56 * The word 'cryptographic' can be left out if the rouines from the library 57 * being used are not cryptographic related :-). 58 * 4. If you include any Windows specific code (or a derivative thereof) from 59 * the apps directory (application code) you must include an acknowledgement: 60 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 61 * 62 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 65 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 72 * SUCH DAMAGE. 73 * 74 * The licence and distribution terms for any publically available version or 75 * derivative of this code cannot be changed. i.e. this code cannot simply be 76 * copied and put under another distribution licence 77 * [including the GNU Public Licence.] 78 */ 79 80#ifndef HEADER_DH_H 81#define HEADER_DH_H 82 83#include <bcmcrypto/bn.h> 84 85#define DH_FLAG_CACHE_MONT_P 0x01 86 87typedef struct dh_st 88 { 89 BIGNUM *p; 90 BIGNUM *g; 91 long length; /* optional */ 92 BIGNUM *pub_key; /* g^x */ 93 BIGNUM *priv_key; /* x */ 94 95 int flags; 96 BN_MONT_CTX *method_mont_p; 97 /* Place holders if we want to do X9.42 DH */ 98 BIGNUM *q; 99 BIGNUM *j; 100 unsigned char *seed; 101 int seedlen; 102 BIGNUM *counter; 103 } DH; 104 105#define DH_GENERATOR_2 2 106/* #define DH_GENERATOR_3 3 */ 107#define DH_GENERATOR_5 5 108 109/* DH_check error codes */ 110#define DH_CHECK_P_NOT_PRIME 0x01 111#define DH_CHECK_P_NOT_SAFE_PRIME 0x02 112#define DH_UNABLE_TO_CHECK_GENERATOR 0x04 113#define DH_NOT_SUITABLE_GENERATOR 0x08 114 115/* primes p where (p-1)/2 is prime too are called "safe"; we define 116 * this for backward compatibility: 117 */ 118#define DH_CHECK_P_NOT_STRONG_PRIME DH_CHECK_P_NOT_SAFE_PRIME 119 120 121DH * DH_new(void); 122void DH_free(DH *dh); 123int DH_generate_key(unsigned char *key, DH *dh); 124int DH_compute_key(unsigned char *key, unsigned char *pubbuf, int buflen, DH *dh); 125int DH_compute_key_bn(unsigned char *key, BIGNUM *peer_key, DH *dh); 126DH *DH_init(unsigned char *pbuf, int plen, int g); 127 128/* for standalone test purposes */ 129int DH_size(const DH *dh); 130DH * DH_generate_parameters(DH *dh, int prime_len, int generator, 131 void (*callback)(int, int, void *), void *cb_arg); 132int DH_check(const DH *dh, int *codes); 133 134#endif /* HEADER_DH_H */ 135