summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/dh
diff options
context:
space:
mode:
authormarkus <>2002-09-05 12:51:50 +0000
committermarkus <>2002-09-05 12:51:50 +0000
commit15b5d84f9da2ce4bfae8580e56e34a859f74ad71 (patch)
treebf939e82d7fd73cc8a01cf6959002209972091bc /src/lib/libcrypto/dh
parent027351f729b9e837200dae6e1520cda6577ab930 (diff)
downloadopenbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.gz
openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.bz2
openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.zip
import openssl-0.9.7-beta1
Diffstat (limited to 'src/lib/libcrypto/dh')
-rw-r--r--src/lib/libcrypto/dh/dh.h131
-rw-r--r--src/lib/libcrypto/dh/dh_check.c18
-rw-r--r--src/lib/libcrypto/dh/dh_err.c126
-rw-r--r--src/lib/libcrypto/dh/dh_gen.c63
-rw-r--r--src/lib/libcrypto/dh/dh_key.c131
-rw-r--r--src/lib/libcrypto/dh/dh_lib.c159
6 files changed, 457 insertions, 171 deletions
diff --git a/src/lib/libcrypto/dh/dh.h b/src/lib/libcrypto/dh/dh.h
index 4cc1df2650..05851f8429 100644
--- a/src/lib/libcrypto/dh/dh.h
+++ b/src/lib/libcrypto/dh/dh.h
@@ -59,15 +59,41 @@
59#ifndef HEADER_DH_H 59#ifndef HEADER_DH_H
60#define HEADER_DH_H 60#define HEADER_DH_H
61 61
62#ifdef OPENSSL_NO_DH
63#error DH is disabled.
64#endif
65
66#ifndef OPENSSL_NO_BIO
67#include <openssl/bio.h>
68#endif
69#include <openssl/bn.h>
70#include <openssl/crypto.h>
71#include <openssl/ossl_typ.h>
72
73#define DH_FLAG_CACHE_MONT_P 0x01
74
62#ifdef __cplusplus 75#ifdef __cplusplus
63extern "C" { 76extern "C" {
64#endif 77#endif
65 78
66#ifndef HEADER_BN_H 79typedef struct dh_st DH;
67#define BIGNUM char 80
68#endif 81typedef struct dh_method {
82 const char *name;
83 /* Methods here */
84 int (*generate_key)(DH *dh);
85 int (*compute_key)(unsigned char *key,const BIGNUM *pub_key,DH *dh);
86 int (*bn_mod_exp)(const DH *dh, BIGNUM *r, const BIGNUM *a,
87 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
88 BN_MONT_CTX *m_ctx); /* Can be null */
89
90 int (*init)(DH *dh);
91 int (*finish)(DH *dh);
92 int flags;
93 char *app_data;
94} DH_METHOD;
69 95
70typedef struct dh_st 96struct dh_st
71 { 97 {
72 /* This first argument is used to pick up errors when 98 /* This first argument is used to pick up errors when
73 * a DH is passed instead of a EVP_PKEY */ 99 * a DH is passed instead of a EVP_PKEY */
@@ -75,10 +101,24 @@ typedef struct dh_st
75 int version; 101 int version;
76 BIGNUM *p; 102 BIGNUM *p;
77 BIGNUM *g; 103 BIGNUM *g;
78 int length; /* optional */ 104 long length; /* optional */
79 BIGNUM *pub_key; /* y */ 105 BIGNUM *pub_key; /* g^x */
80 BIGNUM *priv_key; /* x */ 106 BIGNUM *priv_key; /* x */
81 } DH; 107
108 int flags;
109 char *method_mont_p;
110 /* Place holders if we want to do X9.42 DH */
111 BIGNUM *q;
112 BIGNUM *j;
113 unsigned char *seed;
114 int seedlen;
115 BIGNUM *counter;
116
117 int references;
118 CRYPTO_EX_DATA ex_data;
119 const DH_METHOD *meth;
120 ENGINE *engine;
121 };
82 122
83#define DH_GENERATOR_2 2 123#define DH_GENERATOR_2 2
84/* #define DH_GENERATOR_3 3 */ 124/* #define DH_GENERATOR_3 3 */
@@ -86,10 +126,14 @@ typedef struct dh_st
86 126
87/* DH_check error codes */ 127/* DH_check error codes */
88#define DH_CHECK_P_NOT_PRIME 0x01 128#define DH_CHECK_P_NOT_PRIME 0x01
89#define DH_CHECK_P_NOT_STRONG_PRIME 0x02 129#define DH_CHECK_P_NOT_SAFE_PRIME 0x02
90#define DH_UNABLE_TO_CHECK_GENERATOR 0x04 130#define DH_UNABLE_TO_CHECK_GENERATOR 0x04
91#define DH_NOT_SUITABLE_GENERATOR 0x08 131#define DH_NOT_SUITABLE_GENERATOR 0x08
92 132
133/* primes p where (p-1)/2 is prime too are called "safe"; we define
134 this for backward compatibility: */
135#define DH_CHECK_P_NOT_STRONG_PRIME DH_CHECK_P_NOT_SAFE_PRIME
136
93#define DHparams_dup(x) (DH *)ASN1_dup((int (*)())i2d_DHparams, \ 137#define DHparams_dup(x) (DH *)ASN1_dup((int (*)())i2d_DHparams, \
94 (char *(*)())d2i_DHparams,(char *)(x)) 138 (char *(*)())d2i_DHparams,(char *)(x))
95#define d2i_DHparams_fp(fp,x) (DH *)ASN1_d2i_fp((char *(*)())DH_new, \ 139#define d2i_DHparams_fp(fp,x) (DH *)ASN1_d2i_fp((char *(*)())DH_new, \
@@ -98,50 +142,51 @@ typedef struct dh_st
98 (unsigned char *)(x)) 142 (unsigned char *)(x))
99#define d2i_DHparams_bio(bp,x) (DH *)ASN1_d2i_bio((char *(*)())DH_new, \ 143#define d2i_DHparams_bio(bp,x) (DH *)ASN1_d2i_bio((char *(*)())DH_new, \
100 (char *(*)())d2i_DHparams,(bp),(unsigned char **)(x)) 144 (char *(*)())d2i_DHparams,(bp),(unsigned char **)(x))
145#ifdef __cplusplus
146#define i2d_DHparams_bio(bp,x) ASN1_i2d_bio((int (*)())i2d_DHparams,(bp), \
147 (unsigned char *)(x))
148#else
101#define i2d_DHparams_bio(bp,x) ASN1_i2d_bio(i2d_DHparams,(bp), \ 149#define i2d_DHparams_bio(bp,x) ASN1_i2d_bio(i2d_DHparams,(bp), \
102 (unsigned char *)(x)) 150 (unsigned char *)(x))
151#endif
152
153const DH_METHOD *DH_OpenSSL(void);
154
155void DH_set_default_method(const DH_METHOD *meth);
156const DH_METHOD *DH_get_default_method(void);
157int DH_set_method(DH *dh, const DH_METHOD *meth);
158DH *DH_new_method(ENGINE *engine);
103 159
104#ifndef NOPROTO
105DH * DH_new(void); 160DH * DH_new(void);
106void DH_free(DH *dh); 161void DH_free(DH *dh);
107int DH_size(DH *dh); 162int DH_up_ref(DH *dh);
163int DH_size(const DH *dh);
164int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
165 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
166int DH_set_ex_data(DH *d, int idx, void *arg);
167void *DH_get_ex_data(DH *d, int idx);
108DH * DH_generate_parameters(int prime_len,int generator, 168DH * DH_generate_parameters(int prime_len,int generator,
109 void (*callback)(int,int,char *),char *cb_arg); 169 void (*callback)(int,int,void *),void *cb_arg);
110int DH_check(DH *dh,int *codes); 170int DH_check(const DH *dh,int *codes);
111int DH_generate_key(DH *dh); 171int DH_generate_key(DH *dh);
112int DH_compute_key(unsigned char *key,BIGNUM *pub_key,DH *dh); 172int DH_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh);
113DH * d2i_DHparams(DH **a,unsigned char **pp, long length); 173DH * d2i_DHparams(DH **a,const unsigned char **pp, long length);
114int i2d_DHparams(DH *a,unsigned char **pp); 174int i2d_DHparams(const DH *a,unsigned char **pp);
115#ifndef NO_FP_API 175#ifndef OPENSSL_NO_FP_API
116int DHparams_print_fp(FILE *fp, DH *x); 176int DHparams_print_fp(FILE *fp, const DH *x);
117#endif 177#endif
118#ifdef HEADER_BIO_H 178#ifndef OPENSSL_NO_BIO
119int DHparams_print(BIO *bp, DH *x); 179int DHparams_print(BIO *bp, const DH *x);
120#else 180#else
121int DHparams_print(char *bp, DH *x); 181int DHparams_print(char *bp, const DH *x);
122#endif
123void ERR_load_DH_strings(void );
124
125#else
126
127DH * DH_new();
128void DH_free();
129int DH_size();
130DH * DH_generate_parameters();
131int DH_check();
132int DH_generate_key();
133int DH_compute_key();
134DH * d2i_DHparams();
135int i2d_DHparams();
136#ifndef NO_FP_API
137int DHparams_print_fp();
138#endif
139int DHparams_print();
140void ERR_load_DH_strings();
141
142#endif 182#endif
143 183
144/* BEGIN ERROR CODES */ 184/* BEGIN ERROR CODES */
185/* The following lines are auto generated by the script mkerr.pl. Any changes
186 * made after this point may be overwritten when the script is next run.
187 */
188void ERR_load_DH_strings(void);
189
145/* Error codes for the DH functions. */ 190/* Error codes for the DH functions. */
146 191
147/* Function codes. */ 192/* Function codes. */
@@ -150,13 +195,13 @@ void ERR_load_DH_strings();
150#define DH_F_DH_COMPUTE_KEY 102 195#define DH_F_DH_COMPUTE_KEY 102
151#define DH_F_DH_GENERATE_KEY 103 196#define DH_F_DH_GENERATE_KEY 103
152#define DH_F_DH_GENERATE_PARAMETERS 104 197#define DH_F_DH_GENERATE_PARAMETERS 104
153#define DH_F_DH_NEW 105 198#define DH_F_DH_NEW_METHOD 105
154 199
155/* Reason codes. */ 200/* Reason codes. */
201#define DH_R_BAD_GENERATOR 101
156#define DH_R_NO_PRIVATE_VALUE 100 202#define DH_R_NO_PRIVATE_VALUE 100
157 203
158#ifdef __cplusplus 204#ifdef __cplusplus
159} 205}
160#endif 206#endif
161#endif 207#endif
162
diff --git a/src/lib/libcrypto/dh/dh_check.c b/src/lib/libcrypto/dh/dh_check.c
index 65602e494f..f0373f7d68 100644
--- a/src/lib/libcrypto/dh/dh_check.c
+++ b/src/lib/libcrypto/dh/dh_check.c
@@ -58,10 +58,10 @@
58 58
59#include <stdio.h> 59#include <stdio.h>
60#include "cryptlib.h" 60#include "cryptlib.h"
61#include "bn.h" 61#include <openssl/bn.h>
62#include "dh.h" 62#include <openssl/dh.h>
63 63
64/* Check that p is a strong prime and 64/* Check that p is a safe prime and
65 * if g is 2, 3 or 5, check that is is a suitable generator 65 * if g is 2, 3 or 5, check that is is a suitable generator
66 * where 66 * where
67 * for 2, p mod 24 == 11 67 * for 2, p mod 24 == 11
@@ -70,9 +70,7 @@
70 * should hold. 70 * should hold.
71 */ 71 */
72 72
73int DH_check(dh,ret) 73int DH_check(const DH *dh, int *ret)
74DH *dh;
75int *ret;
76 { 74 {
77 int ok=0; 75 int ok=0;
78 BN_CTX *ctx=NULL; 76 BN_CTX *ctx=NULL;
@@ -90,11 +88,13 @@ int *ret;
90 l=BN_mod_word(dh->p,24); 88 l=BN_mod_word(dh->p,24);
91 if (l != 11) *ret|=DH_NOT_SUITABLE_GENERATOR; 89 if (l != 11) *ret|=DH_NOT_SUITABLE_GENERATOR;
92 } 90 }
93/* else if (BN_is_word(dh->g,DH_GENERATOR_3)) 91#if 0
92 else if (BN_is_word(dh->g,DH_GENERATOR_3))
94 { 93 {
95 l=BN_mod_word(dh->p,12); 94 l=BN_mod_word(dh->p,12);
96 if (l != 5) *ret|=DH_NOT_SUITABLE_GENERATOR; 95 if (l != 5) *ret|=DH_NOT_SUITABLE_GENERATOR;
97 }*/ 96 }
97#endif
98 else if (BN_is_word(dh->g,DH_GENERATOR_5)) 98 else if (BN_is_word(dh->g,DH_GENERATOR_5))
99 { 99 {
100 l=BN_mod_word(dh->p,10); 100 l=BN_mod_word(dh->p,10);
@@ -110,7 +110,7 @@ int *ret;
110 { 110 {
111 if (!BN_rshift1(q,dh->p)) goto err; 111 if (!BN_rshift1(q,dh->p)) goto err;
112 if (!BN_is_prime(q,BN_prime_checks,NULL,ctx,NULL)) 112 if (!BN_is_prime(q,BN_prime_checks,NULL,ctx,NULL))
113 *ret|=DH_CHECK_P_NOT_STRONG_PRIME; 113 *ret|=DH_CHECK_P_NOT_SAFE_PRIME;
114 } 114 }
115 ok=1; 115 ok=1;
116err: 116err:
diff --git a/src/lib/libcrypto/dh/dh_err.c b/src/lib/libcrypto/dh/dh_err.c
index 9d5c06ac24..d837950aec 100644
--- a/src/lib/libcrypto/dh/dh_err.c
+++ b/src/lib/libcrypto/dh/dh_err.c
@@ -1,66 +1,69 @@
1/* lib/dh/dh_err.c */ 1/* crypto/dh/dh_err.c */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) 2/* ====================================================================
3 * All rights reserved. 3 * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved.
4 * 4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
25 * are met: 7 * are met:
26 * 1. Redistributions of source code must retain the copyright 8 *
27 * notice, this list of conditions and the following disclaimer. 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
28 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in
30 * documentation and/or other materials provided with the distribution. 14 * the documentation and/or other materials provided with the
31 * 3. All advertising materials mentioning features or use of this software 15 * distribution.
32 * must display the following acknowledgement: 16 *
33 * "This product includes cryptographic software written by 17 * 3. All advertising materials mentioning features or use of this
34 * Eric Young (eay@cryptsoft.com)" 18 * software must display the following acknowledgment:
35 * The word 'cryptographic' can be left out if the rouines from the library 19 * "This product includes software developed by the OpenSSL Project
36 * being used are not cryptographic related :-). 20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
37 * 4. If you include any Windows specific code (or a derivative thereof) from 21 *
38 * the apps directory (application code) you must include an acknowledgement: 22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 23 * endorse or promote products derived from this software without
40 * 24 * prior written permission. For written permission, please contact
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 25 * openssl-core@OpenSSL.org.
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 *
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * 5. Products derived from this software may not be called "OpenSSL"
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 28 * nor may "OpenSSL" appear in their names without prior written
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * permission of the OpenSSL Project.
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 *
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * 6. Redistributions of any form whatsoever must retain the following
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * acknowledgment:
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * "This product includes software developed by the OpenSSL Project
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
51 * SUCH DAMAGE. 35 *
52 * 36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
53 * The licence and distribution terms for any publically available version or 37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * derivative of this code cannot be changed. i.e. this code cannot simply be 38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55 * copied and put under another distribution licence 39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
56 * [including the GNU Public Licence.] 40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
57 */ 54 */
55
56/* NOTE: this file was auto generated by the mkerr.pl script: any changes
57 * made to it will be overwritten when the script next updates this file,
58 * only reason strings will be preserved.
59 */
60
58#include <stdio.h> 61#include <stdio.h>
59#include "err.h" 62#include <openssl/err.h>
60#include "dh.h" 63#include <openssl/dh.h>
61 64
62/* BEGIN ERROR CODES */ 65/* BEGIN ERROR CODES */
63#ifndef NO_ERR 66#ifndef OPENSSL_NO_ERR
64static ERR_STRING_DATA DH_str_functs[]= 67static ERR_STRING_DATA DH_str_functs[]=
65 { 68 {
66{ERR_PACK(0,DH_F_DHPARAMS_PRINT,0), "DHparams_print"}, 69{ERR_PACK(0,DH_F_DHPARAMS_PRINT,0), "DHparams_print"},
@@ -68,26 +71,27 @@ static ERR_STRING_DATA DH_str_functs[]=
68{ERR_PACK(0,DH_F_DH_COMPUTE_KEY,0), "DH_compute_key"}, 71{ERR_PACK(0,DH_F_DH_COMPUTE_KEY,0), "DH_compute_key"},
69{ERR_PACK(0,DH_F_DH_GENERATE_KEY,0), "DH_generate_key"}, 72{ERR_PACK(0,DH_F_DH_GENERATE_KEY,0), "DH_generate_key"},
70{ERR_PACK(0,DH_F_DH_GENERATE_PARAMETERS,0), "DH_generate_parameters"}, 73{ERR_PACK(0,DH_F_DH_GENERATE_PARAMETERS,0), "DH_generate_parameters"},
71{ERR_PACK(0,DH_F_DH_NEW,0), "DH_new"}, 74{ERR_PACK(0,DH_F_DH_NEW_METHOD,0), "DH_new_method"},
72{0,NULL}, 75{0,NULL}
73 }; 76 };
74 77
75static ERR_STRING_DATA DH_str_reasons[]= 78static ERR_STRING_DATA DH_str_reasons[]=
76 { 79 {
80{DH_R_BAD_GENERATOR ,"bad generator"},
77{DH_R_NO_PRIVATE_VALUE ,"no private value"}, 81{DH_R_NO_PRIVATE_VALUE ,"no private value"},
78{0,NULL}, 82{0,NULL}
79 }; 83 };
80 84
81#endif 85#endif
82 86
83void ERR_load_DH_strings() 87void ERR_load_DH_strings(void)
84 { 88 {
85 static int init=1; 89 static int init=1;
86 90
87 if (init); 91 if (init)
88 {; 92 {
89 init=0; 93 init=0;
90#ifndef NO_ERR 94#ifndef OPENSSL_NO_ERR
91 ERR_load_strings(ERR_LIB_DH,DH_str_functs); 95 ERR_load_strings(ERR_LIB_DH,DH_str_functs);
92 ERR_load_strings(ERR_LIB_DH,DH_str_reasons); 96 ERR_load_strings(ERR_LIB_DH,DH_str_reasons);
93#endif 97#endif
diff --git a/src/lib/libcrypto/dh/dh_gen.c b/src/lib/libcrypto/dh/dh_gen.c
index 04c7046a7b..06f78b35ab 100644
--- a/src/lib/libcrypto/dh/dh_gen.c
+++ b/src/lib/libcrypto/dh/dh_gen.c
@@ -58,8 +58,8 @@
58 58
59#include <stdio.h> 59#include <stdio.h>
60#include "cryptlib.h" 60#include "cryptlib.h"
61#include "bn.h" 61#include <openssl/bn.h>
62#include "dh.h" 62#include <openssl/dh.h>
63 63
64/* We generate DH parameters as follows 64/* We generate DH parameters as follows
65 * find a prime q which is prime_len/2 bits long. 65 * find a prime q which is prime_len/2 bits long.
@@ -72,22 +72,22 @@
72 * Having said all that, 72 * Having said all that,
73 * there is another special case method for the generators 2, 3 and 5. 73 * there is another special case method for the generators 2, 3 and 5.
74 * for 2, p mod 24 == 11 74 * for 2, p mod 24 == 11
75 * for 3, p mod 12 == 5 <<<<< does not work for strong primes. 75 * for 3, p mod 12 == 5 <<<<< does not work for safe primes.
76 * for 5, p mod 10 == 3 or 7 76 * for 5, p mod 10 == 3 or 7
77 * 77 *
78 * Thanks to Phil Karn <karn@qualcomm.com> for the pointers about the 78 * Thanks to Phil Karn <karn@qualcomm.com> for the pointers about the
79 * special generators and for answering some of my questions. 79 * special generators and for answering some of my questions.
80 * 80 *
81 * I've implemented the second simple method :-). 81 * I've implemented the second simple method :-).
82 * Since DH should be using a strong prime (both p and q are prime), 82 * Since DH should be using a safe prime (both p and q are prime),
83 * this generator function can take a very very long time to run. 83 * this generator function can take a very very long time to run.
84 */ 84 */
85 85/* Actually there is no reason to insist that 'generator' be a generator.
86DH *DH_generate_parameters(prime_len,generator,callback,cb_arg) 86 * It's just as OK (and in some sense better) to use a generator of the
87int prime_len; 87 * order-q subgroup.
88int generator; 88 */
89void (*callback)(P_I_I_P); 89DH *DH_generate_parameters(int prime_len, int generator,
90char *cb_arg; 90 void (*callback)(int,int,void *), void *cb_arg)
91 { 91 {
92 BIGNUM *p=NULL,*t1,*t2; 92 BIGNUM *p=NULL,*t1,*t2;
93 DH *ret=NULL; 93 DH *ret=NULL;
@@ -95,38 +95,53 @@ char *cb_arg;
95 BN_CTX *ctx=NULL; 95 BN_CTX *ctx=NULL;
96 96
97 ret=DH_new(); 97 ret=DH_new();
98 if (ret == NULL) goto err;
98 ctx=BN_CTX_new(); 99 ctx=BN_CTX_new();
99 if (ctx == NULL) goto err; 100 if (ctx == NULL) goto err;
100 t1=ctx->bn[0]; 101 BN_CTX_start(ctx);
101 t2=ctx->bn[1]; 102 t1 = BN_CTX_get(ctx);
102 ctx->tos=2; 103 t2 = BN_CTX_get(ctx);
104 if (t1 == NULL || t2 == NULL) goto err;
103 105
106 if (generator <= 1)
107 {
108 DHerr(DH_F_DH_GENERATE_PARAMETERS, DH_R_BAD_GENERATOR);
109 goto err;
110 }
104 if (generator == DH_GENERATOR_2) 111 if (generator == DH_GENERATOR_2)
105 { 112 {
106 BN_set_word(t1,24); 113 if (!BN_set_word(t1,24)) goto err;
107 BN_set_word(t2,11); 114 if (!BN_set_word(t2,11)) goto err;
108 g=2; 115 g=2;
109 } 116 }
110#ifdef undef /* does not work for strong primes */ 117#if 0 /* does not work for safe primes */
111 else if (generator == DH_GENERATOR_3) 118 else if (generator == DH_GENERATOR_3)
112 { 119 {
113 BN_set_word(t1,12); 120 if (!BN_set_word(t1,12)) goto err;
114 BN_set_word(t2,5); 121 if (!BN_set_word(t2,5)) goto err;
115 g=3; 122 g=3;
116 } 123 }
117#endif 124#endif
118 else if (generator == DH_GENERATOR_5) 125 else if (generator == DH_GENERATOR_5)
119 { 126 {
120 BN_set_word(t1,10); 127 if (!BN_set_word(t1,10)) goto err;
121 BN_set_word(t2,3); 128 if (!BN_set_word(t2,3)) goto err;
122 /* BN_set_word(t3,7); just have to miss 129 /* BN_set_word(t3,7); just have to miss
123 * out on these ones :-( */ 130 * out on these ones :-( */
124 g=5; 131 g=5;
125 } 132 }
126 else 133 else
134 {
135 /* in the general case, don't worry if 'generator' is a
136 * generator or not: since we are using safe primes,
137 * it will generate either an order-q or an order-2q group,
138 * which both is OK */
139 if (!BN_set_word(t1,2)) goto err;
140 if (!BN_set_word(t2,1)) goto err;
127 g=generator; 141 g=generator;
142 }
128 143
129 p=BN_generate_prime(prime_len,1,t1,t2,callback,cb_arg); 144 p=BN_generate_prime(NULL,prime_len,1,t1,t2,callback,cb_arg);
130 if (p == NULL) goto err; 145 if (p == NULL) goto err;
131 if (callback != NULL) callback(3,0,cb_arg); 146 if (callback != NULL) callback(3,0,cb_arg);
132 ret->p=p; 147 ret->p=p;
@@ -140,7 +155,11 @@ err:
140 ok=0; 155 ok=0;
141 } 156 }
142 157
143 if (ctx != NULL) BN_CTX_free(ctx); 158 if (ctx != NULL)
159 {
160 BN_CTX_end(ctx);
161 BN_CTX_free(ctx);
162 }
144 if (!ok && (ret != NULL)) 163 if (!ok && (ret != NULL))
145 { 164 {
146 DH_free(ret); 165 DH_free(ret);
diff --git a/src/lib/libcrypto/dh/dh_key.c b/src/lib/libcrypto/dh/dh_key.c
index 7576772bcd..1a0efca2c4 100644
--- a/src/lib/libcrypto/dh/dh_key.c
+++ b/src/lib/libcrypto/dh/dh_key.c
@@ -58,32 +58,63 @@
58 58
59#include <stdio.h> 59#include <stdio.h>
60#include "cryptlib.h" 60#include "cryptlib.h"
61#include "bn.h" 61#include <openssl/bn.h>
62#include "rand.h" 62#include <openssl/rand.h>
63#include "dh.h" 63#include <openssl/dh.h>
64#include <openssl/engine.h>
64 65
65int DH_generate_key(dh) 66static int generate_key(DH *dh);
66DH *dh; 67static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh);
68static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
69 const BIGNUM *a, const BIGNUM *p,
70 const BIGNUM *m, BN_CTX *ctx,
71 BN_MONT_CTX *m_ctx);
72static int dh_init(DH *dh);
73static int dh_finish(DH *dh);
74
75int DH_generate_key(DH *dh)
76 {
77 return dh->meth->generate_key(dh);
78 }
79
80int DH_compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
81 {
82 return dh->meth->compute_key(key, pub_key, dh);
83 }
84
85static DH_METHOD dh_ossl = {
86"OpenSSL DH Method",
87generate_key,
88compute_key,
89dh_bn_mod_exp,
90dh_init,
91dh_finish,
920,
93NULL
94};
95
96const DH_METHOD *DH_OpenSSL(void)
97{
98 return &dh_ossl;
99}
100
101static int generate_key(DH *dh)
67 { 102 {
68 int ok=0; 103 int ok=0;
69 unsigned int i; 104 int generate_new_key=0;
70 BN_CTX *ctx=NULL; 105 unsigned l;
106 BN_CTX *ctx;
107 BN_MONT_CTX *mont;
71 BIGNUM *pub_key=NULL,*priv_key=NULL; 108 BIGNUM *pub_key=NULL,*priv_key=NULL;
72 109
73 ctx=BN_CTX_new(); 110 ctx = BN_CTX_new();
74 if (ctx == NULL) goto err; 111 if (ctx == NULL) goto err;
75 112
76 if (dh->priv_key == NULL) 113 if (dh->priv_key == NULL)
77 { 114 {
78 i=dh->length;
79 if (i == 0)
80 {
81 /* Make the number p-1 bits long */
82 i=BN_num_bits(dh->p)-1;
83 }
84 priv_key=BN_new(); 115 priv_key=BN_new();
85 if (priv_key == NULL) goto err; 116 if (priv_key == NULL) goto err;
86 if (!BN_rand(priv_key,i,0,0)) goto err; 117 generate_new_key=1;
87 } 118 }
88 else 119 else
89 priv_key=dh->priv_key; 120 priv_key=dh->priv_key;
@@ -96,7 +127,21 @@ DH *dh;
96 else 127 else
97 pub_key=dh->pub_key; 128 pub_key=dh->pub_key;
98 129
99 if (!BN_mod_exp(pub_key,dh->g,priv_key,dh->p,ctx)) goto err; 130 if ((dh->method_mont_p == NULL) && (dh->flags & DH_FLAG_CACHE_MONT_P))
131 {
132 if ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)
133 if (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p,
134 dh->p,ctx)) goto err;
135 }
136 mont=(BN_MONT_CTX *)dh->method_mont_p;
137
138 if (generate_new_key)
139 {
140 l = dh->length ? dh->length : BN_num_bits(dh->p)-1; /* secret exponent length */
141 if (!BN_rand(priv_key, l, 0, 0)) goto err;
142 }
143 if (!dh->meth->bn_mod_exp(dh, pub_key, dh->g, priv_key,dh->p,ctx,mont))
144 goto err;
100 145
101 dh->pub_key=pub_key; 146 dh->pub_key=pub_key;
102 dh->priv_key=priv_key; 147 dh->priv_key=priv_key;
@@ -107,29 +152,36 @@ err:
107 152
108 if ((pub_key != NULL) && (dh->pub_key == NULL)) BN_free(pub_key); 153 if ((pub_key != NULL) && (dh->pub_key == NULL)) BN_free(pub_key);
109 if ((priv_key != NULL) && (dh->priv_key == NULL)) BN_free(priv_key); 154 if ((priv_key != NULL) && (dh->priv_key == NULL)) BN_free(priv_key);
110 if (ctx != NULL) BN_CTX_free(ctx); 155 BN_CTX_free(ctx);
111 return(ok); 156 return(ok);
112 } 157 }
113 158
114int DH_compute_key(key,pub_key,dh) 159static int compute_key(unsigned char *key, const BIGNUM *pub_key, DH *dh)
115unsigned char *key;
116BIGNUM *pub_key;
117DH *dh;
118 { 160 {
119 BN_CTX *ctx; 161 BN_CTX *ctx;
162 BN_MONT_CTX *mont;
120 BIGNUM *tmp; 163 BIGNUM *tmp;
121 int ret= -1; 164 int ret= -1;
122 165
123 ctx=BN_CTX_new(); 166 ctx = BN_CTX_new();
124 if (ctx == NULL) goto err; 167 if (ctx == NULL) goto err;
125 tmp=ctx->bn[ctx->tos++]; 168 BN_CTX_start(ctx);
169 tmp = BN_CTX_get(ctx);
126 170
127 if (dh->priv_key == NULL) 171 if (dh->priv_key == NULL)
128 { 172 {
129 DHerr(DH_F_DH_COMPUTE_KEY,DH_R_NO_PRIVATE_VALUE); 173 DHerr(DH_F_DH_COMPUTE_KEY,DH_R_NO_PRIVATE_VALUE);
130 goto err; 174 goto err;
131 } 175 }
132 if (!BN_mod_exp(tmp,pub_key,dh->priv_key,dh->p,ctx)) 176 if ((dh->method_mont_p == NULL) && (dh->flags & DH_FLAG_CACHE_MONT_P))
177 {
178 if ((dh->method_mont_p=(char *)BN_MONT_CTX_new()) != NULL)
179 if (!BN_MONT_CTX_set((BN_MONT_CTX *)dh->method_mont_p,
180 dh->p,ctx)) goto err;
181 }
182
183 mont=(BN_MONT_CTX *)dh->method_mont_p;
184 if (!dh->meth->bn_mod_exp(dh, tmp, pub_key, dh->priv_key,dh->p,ctx,mont))
133 { 185 {
134 DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB); 186 DHerr(DH_F_DH_COMPUTE_KEY,ERR_R_BN_LIB);
135 goto err; 187 goto err;
@@ -137,6 +189,35 @@ DH *dh;
137 189
138 ret=BN_bn2bin(tmp,key); 190 ret=BN_bn2bin(tmp,key);
139err: 191err:
140 if (ctx != NULL) BN_CTX_free(ctx); 192 BN_CTX_end(ctx);
193 BN_CTX_free(ctx);
141 return(ret); 194 return(ret);
142 } 195 }
196
197static int dh_bn_mod_exp(const DH *dh, BIGNUM *r,
198 const BIGNUM *a, const BIGNUM *p,
199 const BIGNUM *m, BN_CTX *ctx,
200 BN_MONT_CTX *m_ctx)
201 {
202 if (a->top == 1)
203 {
204 BN_ULONG A = a->d[0];
205 return BN_mod_exp_mont_word(r,A,p,m,ctx,m_ctx);
206 }
207 else
208 return BN_mod_exp_mont(r,a,p,m,ctx,m_ctx);
209 }
210
211
212static int dh_init(DH *dh)
213 {
214 dh->flags |= DH_FLAG_CACHE_MONT_P;
215 return(1);
216 }
217
218static int dh_finish(DH *dh)
219 {
220 if(dh->method_mont_p)
221 BN_MONT_CTX_free((BN_MONT_CTX *)dh->method_mont_p);
222 return(1);
223 }
diff --git a/src/lib/libcrypto/dh/dh_lib.c b/src/lib/libcrypto/dh/dh_lib.c
index a300b38396..ba5fd41057 100644
--- a/src/lib/libcrypto/dh/dh_lib.c
+++ b/src/lib/libcrypto/dh/dh_lib.c
@@ -58,21 +58,84 @@
58 58
59#include <stdio.h> 59#include <stdio.h>
60#include "cryptlib.h" 60#include "cryptlib.h"
61#include "bn.h" 61#include <openssl/bn.h>
62#include "dh.h" 62#include <openssl/dh.h>
63#include <openssl/engine.h>
63 64
64char *DH_version="Diffie-Hellman part of SSLeay 0.9.0b 29-Jun-1998"; 65const char *DH_version="Diffie-Hellman" OPENSSL_VERSION_PTEXT;
65 66
66DH *DH_new() 67static const DH_METHOD *default_DH_method = NULL;
68
69void DH_set_default_method(const DH_METHOD *meth)
70 {
71 default_DH_method = meth;
72 }
73
74const DH_METHOD *DH_get_default_method(void)
75 {
76 if(!default_DH_method)
77 default_DH_method = DH_OpenSSL();
78 return default_DH_method;
79 }
80
81int DH_set_method(DH *dh, const DH_METHOD *meth)
82 {
83 /* NB: The caller is specifically setting a method, so it's not up to us
84 * to deal with which ENGINE it comes from. */
85 const DH_METHOD *mtmp;
86 mtmp = dh->meth;
87 if (mtmp->finish) mtmp->finish(dh);
88 if (dh->engine)
89 {
90 ENGINE_finish(dh->engine);
91 dh->engine = NULL;
92 }
93 dh->meth = meth;
94 if (meth->init) meth->init(dh);
95 return 1;
96 }
97
98DH *DH_new(void)
99 {
100 return DH_new_method(NULL);
101 }
102
103DH *DH_new_method(ENGINE *engine)
67 { 104 {
68 DH *ret; 105 DH *ret;
69 106
70 ret=(DH *)Malloc(sizeof(DH)); 107 ret=(DH *)OPENSSL_malloc(sizeof(DH));
71 if (ret == NULL) 108 if (ret == NULL)
72 { 109 {
73 DHerr(DH_F_DH_NEW,ERR_R_MALLOC_FAILURE); 110 DHerr(DH_F_DH_NEW_METHOD,ERR_R_MALLOC_FAILURE);
74 return(NULL); 111 return(NULL);
75 } 112 }
113
114 ret->meth = DH_get_default_method();
115 if (engine)
116 {
117 if (!ENGINE_init(engine))
118 {
119 DHerr(DH_F_DH_NEW_METHOD, ERR_R_ENGINE_LIB);
120 OPENSSL_free(ret);
121 return NULL;
122 }
123 ret->engine = engine;
124 }
125 else
126 ret->engine = ENGINE_get_default_DH();
127 if(ret->engine)
128 {
129 ret->meth = ENGINE_get_DH(ret->engine);
130 if(!ret->meth)
131 {
132 DHerr(DH_F_DH_NEW_METHOD,ERR_R_ENGINE_LIB);
133 ENGINE_finish(ret->engine);
134 OPENSSL_free(ret);
135 return NULL;
136 }
137 }
138
76 ret->pad=0; 139 ret->pad=0;
77 ret->version=0; 140 ret->version=0;
78 ret->p=NULL; 141 ret->p=NULL;
@@ -80,21 +143,95 @@ DH *DH_new()
80 ret->length=0; 143 ret->length=0;
81 ret->pub_key=NULL; 144 ret->pub_key=NULL;
82 ret->priv_key=NULL; 145 ret->priv_key=NULL;
146 ret->q=NULL;
147 ret->j=NULL;
148 ret->seed = NULL;
149 ret->seedlen = 0;
150 ret->counter = NULL;
151 ret->method_mont_p=NULL;
152 ret->references = 1;
153 ret->flags=ret->meth->flags;
154 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
155 if ((ret->meth->init != NULL) && !ret->meth->init(ret))
156 {
157 if (ret->engine)
158 ENGINE_finish(ret->engine);
159 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, ret, &ret->ex_data);
160 OPENSSL_free(ret);
161 ret=NULL;
162 }
83 return(ret); 163 return(ret);
84 } 164 }
85 165
86void DH_free(r) 166void DH_free(DH *r)
87DH *r;
88 { 167 {
168 int i;
169 if(r == NULL) return;
170 i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_DH);
171#ifdef REF_PRINT
172 REF_PRINT("DH",r);
173#endif
174 if (i > 0) return;
175#ifdef REF_CHECK
176 if (i < 0)
177 {
178 fprintf(stderr,"DH_free, bad reference count\n");
179 abort();
180 }
181#endif
182
183 if (r->meth->finish)
184 r->meth->finish(r);
185 if (r->engine)
186 ENGINE_finish(r->engine);
187
188 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DH, r, &r->ex_data);
189
89 if (r->p != NULL) BN_clear_free(r->p); 190 if (r->p != NULL) BN_clear_free(r->p);
90 if (r->g != NULL) BN_clear_free(r->g); 191 if (r->g != NULL) BN_clear_free(r->g);
192 if (r->q != NULL) BN_clear_free(r->q);
193 if (r->j != NULL) BN_clear_free(r->j);
194 if (r->seed) OPENSSL_free(r->seed);
195 if (r->counter != NULL) BN_clear_free(r->counter);
91 if (r->pub_key != NULL) BN_clear_free(r->pub_key); 196 if (r->pub_key != NULL) BN_clear_free(r->pub_key);
92 if (r->priv_key != NULL) BN_clear_free(r->priv_key); 197 if (r->priv_key != NULL) BN_clear_free(r->priv_key);
93 Free(r); 198 OPENSSL_free(r);
199 }
200
201int DH_up_ref(DH *r)
202 {
203 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_DH);
204#ifdef REF_PRINT
205 REF_PRINT("DH",r);
206#endif
207#ifdef REF_CHECK
208 if (i < 2)
209 {
210 fprintf(stderr, "DH_up, bad reference count\n");
211 abort();
212 }
213#endif
214 return ((i > 1) ? 1 : 0);
215 }
216
217int DH_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
218 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
219 {
220 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_DH, argl, argp,
221 new_func, dup_func, free_func);
222 }
223
224int DH_set_ex_data(DH *d, int idx, void *arg)
225 {
226 return(CRYPTO_set_ex_data(&d->ex_data,idx,arg));
227 }
228
229void *DH_get_ex_data(DH *d, int idx)
230 {
231 return(CRYPTO_get_ex_data(&d->ex_data,idx));
94 } 232 }
95 233
96int DH_size(dh) 234int DH_size(const DH *dh)
97DH *dh;
98 { 235 {
99 return(BN_num_bytes(dh->p)); 236 return(BN_num_bytes(dh->p));
100 } 237 }