summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rsa
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/rsa')
-rw-r--r--src/lib/libcrypto/rsa/Makefile77
-rw-r--r--src/lib/libcrypto/rsa/rsa_eng.c348
-rw-r--r--src/lib/libcrypto/rsa/rsa_null.c2
-rw-r--r--src/lib/libcrypto/rsa/rsa_x931g.c255
4 files changed, 658 insertions, 24 deletions
diff --git a/src/lib/libcrypto/rsa/Makefile b/src/lib/libcrypto/rsa/Makefile
index 13900812ac..7b1fd6428c 100644
--- a/src/lib/libcrypto/rsa/Makefile
+++ b/src/lib/libcrypto/rsa/Makefile
@@ -19,10 +19,10 @@ APPS=
19LIB=$(TOP)/libcrypto.a 19LIB=$(TOP)/libcrypto.a
20LIBSRC= rsa_eay.c rsa_gen.c rsa_lib.c rsa_sign.c rsa_saos.c rsa_err.c \ 20LIBSRC= rsa_eay.c rsa_gen.c rsa_lib.c rsa_sign.c rsa_saos.c rsa_err.c \
21 rsa_pk1.c rsa_ssl.c rsa_none.c rsa_oaep.c rsa_chk.c rsa_null.c \ 21 rsa_pk1.c rsa_ssl.c rsa_none.c rsa_oaep.c rsa_chk.c rsa_null.c \
22 rsa_pss.c rsa_x931.c rsa_asn1.c rsa_depr.c 22 rsa_pss.c rsa_x931.c rsa_x931g.c rsa_asn1.c rsa_depr.c rsa_eng.c
23LIBOBJ= rsa_eay.o rsa_gen.o rsa_lib.o rsa_sign.o rsa_saos.o rsa_err.o \ 23LIBOBJ= rsa_eay.o rsa_gen.o rsa_lib.o rsa_sign.o rsa_saos.o rsa_err.o \
24 rsa_pk1.o rsa_ssl.o rsa_none.o rsa_oaep.o rsa_chk.o rsa_null.o \ 24 rsa_pk1.o rsa_ssl.o rsa_none.o rsa_oaep.o rsa_chk.o rsa_null.o \
25 rsa_pss.o rsa_x931.o rsa_asn1.o rsa_depr.o 25 rsa_pss.o rsa_x931.o rsa_x931g.o rsa_asn1.o rsa_depr.o rsa_eng.o
26 26
27SRC= $(LIBSRC) 27SRC= $(LIBSRC)
28 28
@@ -37,7 +37,7 @@ top:
37all: lib 37all: lib
38 38
39lib: $(LIBOBJ) 39lib: $(LIBOBJ)
40 $(AR) $(LIB) $(LIBOBJ) 40 $(ARX) $(LIB) $(LIBOBJ)
41 $(RANLIB) $(LIB) || echo Never mind. 41 $(RANLIB) $(LIB) || echo Never mind.
42 @touch lib 42 @touch lib
43 43
@@ -114,6 +114,21 @@ rsa_eay.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
114rsa_eay.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h 114rsa_eay.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h
115rsa_eay.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h 115rsa_eay.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
116rsa_eay.o: ../../include/openssl/symhacks.h ../cryptlib.h rsa_eay.c 116rsa_eay.o: ../../include/openssl/symhacks.h ../cryptlib.h rsa_eay.c
117rsa_eng.o: ../../e_os.h ../../include/openssl/asn1.h
118rsa_eng.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
119rsa_eng.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
120rsa_eng.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h
121rsa_eng.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h
122rsa_eng.o: ../../include/openssl/engine.h ../../include/openssl/err.h
123rsa_eng.o: ../../include/openssl/evp.h ../../include/openssl/fips.h
124rsa_eng.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
125rsa_eng.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
126rsa_eng.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
127rsa_eng.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h
128rsa_eng.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h
129rsa_eng.o: ../../include/openssl/sha.h ../../include/openssl/stack.h
130rsa_eng.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h
131rsa_eng.o: ../../include/openssl/x509_vfy.h ../cryptlib.h rsa_eng.c
117rsa_err.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h 132rsa_err.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
118rsa_err.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h 133rsa_err.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h
119rsa_err.o: ../../include/openssl/err.h ../../include/openssl/lhash.h 134rsa_err.o: ../../include/openssl/err.h ../../include/openssl/lhash.h
@@ -133,13 +148,18 @@ rsa_gen.o: ../cryptlib.h rsa_gen.c
133rsa_lib.o: ../../e_os.h ../../include/openssl/asn1.h 148rsa_lib.o: ../../e_os.h ../../include/openssl/asn1.h
134rsa_lib.o: ../../include/openssl/bio.h ../../include/openssl/bn.h 149rsa_lib.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
135rsa_lib.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h 150rsa_lib.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
136rsa_lib.o: ../../include/openssl/e_os2.h ../../include/openssl/engine.h 151rsa_lib.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h
137rsa_lib.o: ../../include/openssl/err.h ../../include/openssl/lhash.h 152rsa_lib.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h
138rsa_lib.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h 153rsa_lib.o: ../../include/openssl/engine.h ../../include/openssl/err.h
139rsa_lib.o: ../../include/openssl/ossl_typ.h ../../include/openssl/rand.h 154rsa_lib.o: ../../include/openssl/evp.h ../../include/openssl/fips.h
155rsa_lib.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
156rsa_lib.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
157rsa_lib.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
158rsa_lib.o: ../../include/openssl/pkcs7.h ../../include/openssl/rand.h
140rsa_lib.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h 159rsa_lib.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h
141rsa_lib.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h 160rsa_lib.o: ../../include/openssl/sha.h ../../include/openssl/stack.h
142rsa_lib.o: ../cryptlib.h rsa_lib.c 161rsa_lib.o: ../../include/openssl/symhacks.h ../../include/openssl/x509.h
162rsa_lib.o: ../../include/openssl/x509_vfy.h ../cryptlib.h rsa_lib.c
143rsa_none.o: ../../e_os.h ../../include/openssl/asn1.h 163rsa_none.o: ../../e_os.h ../../include/openssl/asn1.h
144rsa_none.o: ../../include/openssl/bio.h ../../include/openssl/bn.h 164rsa_none.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
145rsa_none.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h 165rsa_none.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
@@ -162,9 +182,9 @@ rsa_oaep.o: ../../e_os.h ../../include/openssl/asn1.h
162rsa_oaep.o: ../../include/openssl/bio.h ../../include/openssl/bn.h 182rsa_oaep.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
163rsa_oaep.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h 183rsa_oaep.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
164rsa_oaep.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h 184rsa_oaep.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
165rsa_oaep.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h 185rsa_oaep.o: ../../include/openssl/evp.h ../../include/openssl/fips.h
166rsa_oaep.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h 186rsa_oaep.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
167rsa_oaep.o: ../../include/openssl/opensslconf.h 187rsa_oaep.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
168rsa_oaep.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h 188rsa_oaep.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
169rsa_oaep.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h 189rsa_oaep.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h
170rsa_oaep.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h 190rsa_oaep.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
@@ -183,21 +203,23 @@ rsa_pss.o: ../../e_os.h ../../include/openssl/asn1.h
183rsa_pss.o: ../../include/openssl/bio.h ../../include/openssl/bn.h 203rsa_pss.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
184rsa_pss.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h 204rsa_pss.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
185rsa_pss.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h 205rsa_pss.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
186rsa_pss.o: ../../include/openssl/evp.h ../../include/openssl/lhash.h 206rsa_pss.o: ../../include/openssl/evp.h ../../include/openssl/fips.h
187rsa_pss.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h 207rsa_pss.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h
188rsa_pss.o: ../../include/openssl/opensslconf.h ../../include/openssl/opensslv.h 208rsa_pss.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h
189rsa_pss.o: ../../include/openssl/ossl_typ.h ../../include/openssl/rand.h 209rsa_pss.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
190rsa_pss.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h 210rsa_pss.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h
191rsa_pss.o: ../../include/openssl/sha.h ../../include/openssl/stack.h 211rsa_pss.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
192rsa_pss.o: ../../include/openssl/symhacks.h ../cryptlib.h rsa_pss.c 212rsa_pss.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
213rsa_pss.o: ../cryptlib.h rsa_pss.c
193rsa_saos.o: ../../e_os.h ../../include/openssl/asn1.h 214rsa_saos.o: ../../e_os.h ../../include/openssl/asn1.h
194rsa_saos.o: ../../include/openssl/bio.h ../../include/openssl/bn.h 215rsa_saos.o: ../../include/openssl/bio.h ../../include/openssl/bn.h
195rsa_saos.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h 216rsa_saos.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
196rsa_saos.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h 217rsa_saos.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h
197rsa_saos.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h 218rsa_saos.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h
198rsa_saos.o: ../../include/openssl/err.h ../../include/openssl/evp.h 219rsa_saos.o: ../../include/openssl/err.h ../../include/openssl/evp.h
199rsa_saos.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h 220rsa_saos.o: ../../include/openssl/fips.h ../../include/openssl/lhash.h
200rsa_saos.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h 221rsa_saos.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
222rsa_saos.o: ../../include/openssl/opensslconf.h
201rsa_saos.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h 223rsa_saos.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
202rsa_saos.o: ../../include/openssl/pkcs7.h ../../include/openssl/rsa.h 224rsa_saos.o: ../../include/openssl/pkcs7.h ../../include/openssl/rsa.h
203rsa_saos.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h 225rsa_saos.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
@@ -210,8 +232,9 @@ rsa_sign.o: ../../include/openssl/buffer.h ../../include/openssl/crypto.h
210rsa_sign.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h 232rsa_sign.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h
211rsa_sign.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h 233rsa_sign.o: ../../include/openssl/ecdh.h ../../include/openssl/ecdsa.h
212rsa_sign.o: ../../include/openssl/err.h ../../include/openssl/evp.h 234rsa_sign.o: ../../include/openssl/err.h ../../include/openssl/evp.h
213rsa_sign.o: ../../include/openssl/lhash.h ../../include/openssl/obj_mac.h 235rsa_sign.o: ../../include/openssl/fips.h ../../include/openssl/lhash.h
214rsa_sign.o: ../../include/openssl/objects.h ../../include/openssl/opensslconf.h 236rsa_sign.o: ../../include/openssl/obj_mac.h ../../include/openssl/objects.h
237rsa_sign.o: ../../include/openssl/opensslconf.h
215rsa_sign.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h 238rsa_sign.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
216rsa_sign.o: ../../include/openssl/pkcs7.h ../../include/openssl/rsa.h 239rsa_sign.o: ../../include/openssl/pkcs7.h ../../include/openssl/rsa.h
217rsa_sign.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h 240rsa_sign.o: ../../include/openssl/safestack.h ../../include/openssl/sha.h
@@ -237,3 +260,11 @@ rsa_x931.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
237rsa_x931.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h 260rsa_x931.o: ../../include/openssl/rand.h ../../include/openssl/rsa.h
238rsa_x931.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h 261rsa_x931.o: ../../include/openssl/safestack.h ../../include/openssl/stack.h
239rsa_x931.o: ../../include/openssl/symhacks.h ../cryptlib.h rsa_x931.c 262rsa_x931.o: ../../include/openssl/symhacks.h ../cryptlib.h rsa_x931.c
263rsa_x931g.o: ../../include/openssl/asn1.h ../../include/openssl/bio.h
264rsa_x931g.o: ../../include/openssl/bn.h ../../include/openssl/crypto.h
265rsa_x931g.o: ../../include/openssl/e_os2.h ../../include/openssl/err.h
266rsa_x931g.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h
267rsa_x931g.o: ../../include/openssl/opensslv.h ../../include/openssl/ossl_typ.h
268rsa_x931g.o: ../../include/openssl/rsa.h ../../include/openssl/safestack.h
269rsa_x931g.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h
270rsa_x931g.o: rsa_x931g.c
diff --git a/src/lib/libcrypto/rsa/rsa_eng.c b/src/lib/libcrypto/rsa/rsa_eng.c
new file mode 100644
index 0000000000..383a7045b2
--- /dev/null
+++ b/src/lib/libcrypto/rsa/rsa_eng.c
@@ -0,0 +1,348 @@
1/* crypto/rsa/rsa_lib.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
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
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <openssl/crypto.h>
61#include "cryptlib.h"
62#include <openssl/lhash.h>
63#include <openssl/bn.h>
64#include <openssl/rsa.h>
65#include <openssl/rand.h>
66#ifndef OPENSSL_NO_ENGINE
67#include <openssl/engine.h>
68#endif
69
70const char RSA_version[]="RSA" OPENSSL_VERSION_PTEXT;
71
72static const RSA_METHOD *default_RSA_meth=NULL;
73
74RSA *RSA_new(void)
75 {
76 RSA *r=RSA_new_method(NULL);
77
78 return r;
79 }
80
81void RSA_set_default_method(const RSA_METHOD *meth)
82 {
83#ifdef OPENSSL_FIPS
84 if (FIPS_mode() && !(meth->flags & RSA_FLAG_FIPS_METHOD))
85 {
86 RSAerr(RSA_F_RSA_SET_DEFAULT_METHOD, RSA_R_NON_FIPS_METHOD);
87 return;
88 }
89#endif
90 default_RSA_meth = meth;
91 }
92
93const RSA_METHOD *RSA_get_default_method(void)
94 {
95 if (default_RSA_meth == NULL)
96 {
97#ifdef RSA_NULL
98 default_RSA_meth=RSA_null_method();
99#else
100#if 0 /* was: #ifdef RSAref */
101 default_RSA_meth=RSA_PKCS1_RSAref();
102#else
103 default_RSA_meth=RSA_PKCS1_SSLeay();
104#endif
105#endif
106 }
107
108 return default_RSA_meth;
109 }
110
111const RSA_METHOD *RSA_get_method(const RSA *rsa)
112 {
113 return rsa->meth;
114 }
115
116int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
117 {
118 /* NB: The caller is specifically setting a method, so it's not up to us
119 * to deal with which ENGINE it comes from. */
120 const RSA_METHOD *mtmp;
121#ifdef OPENSSL_FIPS
122 if (FIPS_mode() && !(meth->flags & RSA_FLAG_FIPS_METHOD))
123 {
124 RSAerr(RSA_F_RSA_SET_METHOD, RSA_R_NON_FIPS_METHOD);
125 return 0;
126 }
127#endif
128 mtmp = rsa->meth;
129 if (mtmp->finish) mtmp->finish(rsa);
130#ifndef OPENSSL_NO_ENGINE
131 if (rsa->engine)
132 {
133 ENGINE_finish(rsa->engine);
134 rsa->engine = NULL;
135 }
136#endif
137 rsa->meth = meth;
138 if (meth->init) meth->init(rsa);
139 return 1;
140 }
141
142RSA *RSA_new_method(ENGINE *engine)
143 {
144 RSA *ret;
145
146 ret=(RSA *)OPENSSL_malloc(sizeof(RSA));
147 if (ret == NULL)
148 {
149 RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
150 return NULL;
151 }
152
153 ret->meth = RSA_get_default_method();
154#ifndef OPENSSL_NO_ENGINE
155 if (engine)
156 {
157 if (!ENGINE_init(engine))
158 {
159 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
160 OPENSSL_free(ret);
161 return NULL;
162 }
163 ret->engine = engine;
164 }
165 else
166 ret->engine = ENGINE_get_default_RSA();
167 if(ret->engine)
168 {
169 ret->meth = ENGINE_get_RSA(ret->engine);
170 if(!ret->meth)
171 {
172 RSAerr(RSA_F_RSA_NEW_METHOD,
173 ERR_R_ENGINE_LIB);
174 ENGINE_finish(ret->engine);
175 OPENSSL_free(ret);
176 return NULL;
177 }
178 }
179#endif
180#ifdef OPENSSL_FIPS
181 if (FIPS_mode() && !(ret->meth->flags & RSA_FLAG_FIPS_METHOD))
182 {
183 RSAerr(RSA_F_RSA_NEW_METHOD, RSA_R_NON_FIPS_METHOD);
184#ifndef OPENSSL_NO_ENGINE
185 if (ret->engine)
186 ENGINE_finish(ret->engine);
187#endif
188 OPENSSL_free(ret);
189 return NULL;
190 }
191#endif
192
193 ret->pad=0;
194 ret->version=0;
195 ret->n=NULL;
196 ret->e=NULL;
197 ret->d=NULL;
198 ret->p=NULL;
199 ret->q=NULL;
200 ret->dmp1=NULL;
201 ret->dmq1=NULL;
202 ret->iqmp=NULL;
203 ret->references=1;
204 ret->_method_mod_n=NULL;
205 ret->_method_mod_p=NULL;
206 ret->_method_mod_q=NULL;
207 ret->blinding=NULL;
208 ret->mt_blinding=NULL;
209 ret->bignum_data=NULL;
210 ret->flags=ret->meth->flags;
211 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
212 if ((ret->meth->init != NULL) && !ret->meth->init(ret))
213 {
214#ifndef OPENSSL_NO_ENGINE
215 if (ret->engine)
216 ENGINE_finish(ret->engine);
217#endif
218 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
219 OPENSSL_free(ret);
220 ret=NULL;
221 }
222 return(ret);
223 }
224
225void RSA_free(RSA *r)
226 {
227 int i;
228
229 if (r == NULL) return;
230
231 i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_RSA);
232#ifdef REF_PRINT
233 REF_PRINT("RSA",r);
234#endif
235 if (i > 0) return;
236#ifdef REF_CHECK
237 if (i < 0)
238 {
239 fprintf(stderr,"RSA_free, bad reference count\n");
240 abort();
241 }
242#endif
243
244 if (r->meth->finish)
245 r->meth->finish(r);
246#ifndef OPENSSL_NO_ENGINE
247 if (r->engine)
248 ENGINE_finish(r->engine);
249#endif
250
251 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
252
253 if (r->n != NULL) BN_clear_free(r->n);
254 if (r->e != NULL) BN_clear_free(r->e);
255 if (r->d != NULL) BN_clear_free(r->d);
256 if (r->p != NULL) BN_clear_free(r->p);
257 if (r->q != NULL) BN_clear_free(r->q);
258 if (r->dmp1 != NULL) BN_clear_free(r->dmp1);
259 if (r->dmq1 != NULL) BN_clear_free(r->dmq1);
260 if (r->iqmp != NULL) BN_clear_free(r->iqmp);
261 if (r->blinding != NULL) BN_BLINDING_free(r->blinding);
262 if (r->mt_blinding != NULL) BN_BLINDING_free(r->mt_blinding);
263 if (r->bignum_data != NULL) OPENSSL_free_locked(r->bignum_data);
264 OPENSSL_free(r);
265 }
266
267int RSA_up_ref(RSA *r)
268 {
269 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
270#ifdef REF_PRINT
271 REF_PRINT("RSA",r);
272#endif
273#ifdef REF_CHECK
274 if (i < 2)
275 {
276 fprintf(stderr, "RSA_up_ref, bad reference count\n");
277 abort();
278 }
279#endif
280 return ((i > 1) ? 1 : 0);
281 }
282
283int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
284 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
285 {
286 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
287 new_func, dup_func, free_func);
288 }
289
290int RSA_set_ex_data(RSA *r, int idx, void *arg)
291 {
292 return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
293 }
294
295void *RSA_get_ex_data(const RSA *r, int idx)
296 {
297 return(CRYPTO_get_ex_data(&r->ex_data,idx));
298 }
299
300int RSA_flags(const RSA *r)
301 {
302 return((r == NULL)?0:r->meth->flags);
303 }
304
305int RSA_memory_lock(RSA *r)
306 {
307 int i,j,k,off;
308 char *p;
309 BIGNUM *bn,**t[6],*b;
310 BN_ULONG *ul;
311
312 if (r->d == NULL) return(1);
313 t[0]= &r->d;
314 t[1]= &r->p;
315 t[2]= &r->q;
316 t[3]= &r->dmp1;
317 t[4]= &r->dmq1;
318 t[5]= &r->iqmp;
319 k=sizeof(BIGNUM)*6;
320 off=k/sizeof(BN_ULONG)+1;
321 j=1;
322 for (i=0; i<6; i++)
323 j+= (*t[i])->top;
324 if ((p=OPENSSL_malloc_locked((off+j)*sizeof(BN_ULONG))) == NULL)
325 {
326 RSAerr(RSA_F_RSA_MEMORY_LOCK,ERR_R_MALLOC_FAILURE);
327 return(0);
328 }
329 bn=(BIGNUM *)p;
330 ul=(BN_ULONG *)&(p[off]);
331 for (i=0; i<6; i++)
332 {
333 b= *(t[i]);
334 *(t[i])= &(bn[i]);
335 memcpy((char *)&(bn[i]),(char *)b,sizeof(BIGNUM));
336 bn[i].flags=BN_FLG_STATIC_DATA;
337 bn[i].d=ul;
338 memcpy((char *)ul,b->d,sizeof(BN_ULONG)*b->top);
339 ul+=b->top;
340 BN_clear_free(b);
341 }
342
343 /* I should fix this so it can still be done */
344 r->flags&= ~(RSA_FLAG_CACHE_PRIVATE|RSA_FLAG_CACHE_PUBLIC);
345
346 r->bignum_data=p;
347 return(1);
348 }
diff --git a/src/lib/libcrypto/rsa/rsa_null.c b/src/lib/libcrypto/rsa/rsa_null.c
index 491572c82b..2f2202f142 100644
--- a/src/lib/libcrypto/rsa/rsa_null.c
+++ b/src/lib/libcrypto/rsa/rsa_null.c
@@ -1,5 +1,5 @@
1/* rsa_null.c */ 1/* rsa_null.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999. 3 * project 1999.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
diff --git a/src/lib/libcrypto/rsa/rsa_x931g.c b/src/lib/libcrypto/rsa/rsa_x931g.c
new file mode 100644
index 0000000000..c640cc2ec9
--- /dev/null
+++ b/src/lib/libcrypto/rsa/rsa_x931g.c
@@ -0,0 +1,255 @@
1/* crypto/rsa/rsa_gen.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
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
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <string.h>
61#include <time.h>
62#include <openssl/err.h>
63#include <openssl/bn.h>
64#include <openssl/rsa.h>
65
66#ifndef OPENSSL_FIPS
67
68/* X9.31 RSA key derivation and generation */
69
70int RSA_X931_derive_ex(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1, BIGNUM *q2,
71 const BIGNUM *Xp1, const BIGNUM *Xp2, const BIGNUM *Xp,
72 const BIGNUM *Xq1, const BIGNUM *Xq2, const BIGNUM *Xq,
73 const BIGNUM *e, BN_GENCB *cb)
74 {
75 BIGNUM *r0=NULL,*r1=NULL,*r2=NULL,*r3=NULL;
76 BN_CTX *ctx=NULL,*ctx2=NULL;
77
78 if (!rsa)
79 goto err;
80
81 ctx = BN_CTX_new();
82 BN_CTX_start(ctx);
83 if (!ctx)
84 goto err;
85
86 r0 = BN_CTX_get(ctx);
87 r1 = BN_CTX_get(ctx);
88 r2 = BN_CTX_get(ctx);
89 r3 = BN_CTX_get(ctx);
90
91 if (r3 == NULL)
92 goto err;
93 if (!rsa->e)
94 {
95 rsa->e = BN_dup(e);
96 if (!rsa->e)
97 goto err;
98 }
99 else
100 e = rsa->e;
101
102 /* If not all parameters present only calculate what we can.
103 * This allows test programs to output selective parameters.
104 */
105
106 if (Xp && !rsa->p)
107 {
108 rsa->p = BN_new();
109 if (!rsa->p)
110 goto err;
111
112 if (!BN_X931_derive_prime_ex(rsa->p, p1, p2,
113 Xp, Xp1, Xp2, e, ctx, cb))
114 goto err;
115 }
116
117 if (Xq && !rsa->q)
118 {
119 rsa->q = BN_new();
120 if (!rsa->q)
121 goto err;
122 if (!BN_X931_derive_prime_ex(rsa->q, q1, q2,
123 Xq, Xq1, Xq2, e, ctx, cb))
124 goto err;
125 }
126
127 if (!rsa->p || !rsa->q)
128 {
129 BN_CTX_end(ctx);
130 BN_CTX_free(ctx);
131 return 2;
132 }
133
134 /* Since both primes are set we can now calculate all remaining
135 * components.
136 */
137
138 /* calculate n */
139 rsa->n=BN_new();
140 if (rsa->n == NULL)
141 goto err;
142 if (!BN_mul(rsa->n,rsa->p,rsa->q,ctx))
143 goto err;
144
145 /* calculate d */
146 if (!BN_sub(r1,rsa->p,BN_value_one()))
147 goto err; /* p-1 */
148 if (!BN_sub(r2,rsa->q,BN_value_one()))
149 goto err; /* q-1 */
150 if (!BN_mul(r0,r1,r2,ctx))
151 goto err; /* (p-1)(q-1) */
152
153 if (!BN_gcd(r3, r1, r2, ctx))
154 goto err;
155
156 if (!BN_div(r0, NULL, r0, r3, ctx))
157 goto err; /* LCM((p-1)(q-1)) */
158
159 ctx2 = BN_CTX_new();
160 if (!ctx2)
161 goto err;
162
163 rsa->d=BN_mod_inverse(NULL,rsa->e,r0,ctx2); /* d */
164 if (rsa->d == NULL)
165 goto err;
166
167 /* calculate d mod (p-1) */
168 rsa->dmp1=BN_new();
169 if (rsa->dmp1 == NULL)
170 goto err;
171 if (!BN_mod(rsa->dmp1,rsa->d,r1,ctx))
172 goto err;
173
174 /* calculate d mod (q-1) */
175 rsa->dmq1=BN_new();
176 if (rsa->dmq1 == NULL)
177 goto err;
178 if (!BN_mod(rsa->dmq1,rsa->d,r2,ctx))
179 goto err;
180
181 /* calculate inverse of q mod p */
182 rsa->iqmp=BN_mod_inverse(NULL,rsa->q,rsa->p,ctx2);
183
184 err:
185 if (ctx)
186 {
187 BN_CTX_end(ctx);
188 BN_CTX_free(ctx);
189 }
190 if (ctx2)
191 BN_CTX_free(ctx2);
192 /* If this is set all calls successful */
193 if (rsa->iqmp != NULL)
194 return 1;
195
196 return 0;
197
198 }
199
200int RSA_X931_generate_key_ex(RSA *rsa, int bits, const BIGNUM *e, BN_GENCB *cb)
201 {
202 int ok = 0;
203 BIGNUM *Xp = NULL, *Xq = NULL;
204 BN_CTX *ctx = NULL;
205
206 ctx = BN_CTX_new();
207 if (!ctx)
208 goto error;
209
210 BN_CTX_start(ctx);
211 Xp = BN_CTX_get(ctx);
212 Xq = BN_CTX_get(ctx);
213 if (!BN_X931_generate_Xpq(Xp, Xq, bits, ctx))
214 goto error;
215
216 rsa->p = BN_new();
217 rsa->q = BN_new();
218 if (!rsa->p || !rsa->q)
219 goto error;
220
221 /* Generate two primes from Xp, Xq */
222
223 if (!BN_X931_generate_prime_ex(rsa->p, NULL, NULL, NULL, NULL, Xp,
224 e, ctx, cb))
225 goto error;
226
227 if (!BN_X931_generate_prime_ex(rsa->q, NULL, NULL, NULL, NULL, Xq,
228 e, ctx, cb))
229 goto error;
230
231 /* Since rsa->p and rsa->q are valid this call will just derive
232 * remaining RSA components.
233 */
234
235 if (!RSA_X931_derive_ex(rsa, NULL, NULL, NULL, NULL,
236 NULL, NULL, NULL, NULL, NULL, NULL, e, cb))
237 goto error;
238
239 ok = 1;
240
241 error:
242 if (ctx)
243 {
244 BN_CTX_end(ctx);
245 BN_CTX_free(ctx);
246 }
247
248 if (ok)
249 return 1;
250
251 return 0;
252
253 }
254
255#endif