summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rsa
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/rsa/rsa.h354
-rw-r--r--src/lib/libcrypto/rsa/rsa_asn1.c121
-rw-r--r--src/lib/libcrypto/rsa/rsa_chk.c184
-rw-r--r--src/lib/libcrypto/rsa/rsa_eay.c628
-rw-r--r--src/lib/libcrypto/rsa/rsa_err.c149
-rw-r--r--src/lib/libcrypto/rsa/rsa_gen.c197
-rw-r--r--src/lib/libcrypto/rsa/rsa_lib.c385
-rw-r--r--src/lib/libcrypto/rsa/rsa_none.c98
-rw-r--r--src/lib/libcrypto/rsa/rsa_oaep.c206
-rw-r--r--src/lib/libcrypto/rsa/rsa_pk1.c224
-rw-r--r--src/lib/libcrypto/rsa/rsa_saos.c146
-rw-r--r--src/lib/libcrypto/rsa/rsa_sign.c228
-rw-r--r--src/lib/libcrypto/rsa/rsa_ssl.c154
13 files changed, 0 insertions, 3074 deletions
diff --git a/src/lib/libcrypto/rsa/rsa.h b/src/lib/libcrypto/rsa/rsa.h
deleted file mode 100644
index 98b3bd7cc5..0000000000
--- a/src/lib/libcrypto/rsa/rsa.h
+++ /dev/null
@@ -1,354 +0,0 @@
1/* crypto/rsa/rsa.h */
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#ifndef HEADER_RSA_H
60#define HEADER_RSA_H
61
62#include <openssl/asn1.h>
63
64#ifndef OPENSSL_NO_BIO
65#include <openssl/bio.h>
66#endif
67#include <openssl/bn.h>
68#include <openssl/crypto.h>
69#include <openssl/ossl_typ.h>
70
71#ifdef OPENSSL_NO_RSA
72#error RSA is disabled.
73#endif
74
75#ifdef __cplusplus
76extern "C" {
77#endif
78
79typedef struct rsa_st RSA;
80
81typedef struct rsa_meth_st
82 {
83 const char *name;
84 int (*rsa_pub_enc)(int flen,const unsigned char *from,
85 unsigned char *to,
86 RSA *rsa,int padding);
87 int (*rsa_pub_dec)(int flen,const unsigned char *from,
88 unsigned char *to,
89 RSA *rsa,int padding);
90 int (*rsa_priv_enc)(int flen,const unsigned char *from,
91 unsigned char *to,
92 RSA *rsa,int padding);
93 int (*rsa_priv_dec)(int flen,const unsigned char *from,
94 unsigned char *to,
95 RSA *rsa,int padding);
96 int (*rsa_mod_exp)(BIGNUM *r0,const BIGNUM *I,RSA *rsa); /* Can be null */
97 int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
98 const BIGNUM *m, BN_CTX *ctx,
99 BN_MONT_CTX *m_ctx); /* Can be null */
100 int (*init)(RSA *rsa); /* called at new */
101 int (*finish)(RSA *rsa); /* called at free */
102 int flags; /* RSA_METHOD_FLAG_* things */
103 char *app_data; /* may be needed! */
104/* New sign and verify functions: some libraries don't allow arbitrary data
105 * to be signed/verified: this allows them to be used. Note: for this to work
106 * the RSA_public_decrypt() and RSA_private_encrypt() should *NOT* be used
107 * RSA_sign(), RSA_verify() should be used instead. Note: for backwards
108 * compatibility this functionality is only enabled if the RSA_FLAG_SIGN_VER
109 * option is set in 'flags'.
110 */
111 int (*rsa_sign)(int type,
112 const unsigned char *m, unsigned int m_length,
113 unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
114 int (*rsa_verify)(int dtype,
115 const unsigned char *m, unsigned int m_length,
116 unsigned char *sigbuf, unsigned int siglen, const RSA *rsa);
117
118 } RSA_METHOD;
119
120struct rsa_st
121 {
122 /* The first parameter is used to pickup errors where
123 * this is passed instead of aEVP_PKEY, it is set to 0 */
124 int pad;
125 long version;
126 const RSA_METHOD *meth;
127 /* functional reference if 'meth' is ENGINE-provided */
128 ENGINE *engine;
129 BIGNUM *n;
130 BIGNUM *e;
131 BIGNUM *d;
132 BIGNUM *p;
133 BIGNUM *q;
134 BIGNUM *dmp1;
135 BIGNUM *dmq1;
136 BIGNUM *iqmp;
137 /* be careful using this if the RSA structure is shared */
138 CRYPTO_EX_DATA ex_data;
139 int references;
140 int flags;
141
142 /* Used to cache montgomery values */
143 BN_MONT_CTX *_method_mod_n;
144 BN_MONT_CTX *_method_mod_p;
145 BN_MONT_CTX *_method_mod_q;
146
147 /* all BIGNUM values are actually in the following data, if it is not
148 * NULL */
149 char *bignum_data;
150 BN_BLINDING *blinding;
151 };
152
153#define RSA_3 0x3L
154#define RSA_F4 0x10001L
155
156#define RSA_METHOD_FLAG_NO_CHECK 0x01 /* don't check pub/private match */
157
158#define RSA_FLAG_CACHE_PUBLIC 0x02
159#define RSA_FLAG_CACHE_PRIVATE 0x04
160#define RSA_FLAG_BLINDING 0x08
161#define RSA_FLAG_THREAD_SAFE 0x10
162/* This flag means the private key operations will be handled by rsa_mod_exp
163 * and that they do not depend on the private key components being present:
164 * for example a key stored in external hardware. Without this flag bn_mod_exp
165 * gets called when private key components are absent.
166 */
167#define RSA_FLAG_EXT_PKEY 0x20
168
169/* This flag in the RSA_METHOD enables the new rsa_sign, rsa_verify functions.
170 */
171#define RSA_FLAG_SIGN_VER 0x40
172
173#define RSA_PKCS1_PADDING 1
174#define RSA_SSLV23_PADDING 2
175#define RSA_NO_PADDING 3
176#define RSA_PKCS1_OAEP_PADDING 4
177
178#define RSA_set_app_data(s,arg) RSA_set_ex_data(s,0,arg)
179#define RSA_get_app_data(s) RSA_get_ex_data(s,0)
180
181RSA * RSA_new(void);
182RSA * RSA_new_method(ENGINE *engine);
183int RSA_size(const RSA *);
184RSA * RSA_generate_key(int bits, unsigned long e,void
185 (*callback)(int,int,void *),void *cb_arg);
186int RSA_check_key(const RSA *);
187 /* next 4 return -1 on error */
188int RSA_public_encrypt(int flen, const unsigned char *from,
189 unsigned char *to, RSA *rsa,int padding);
190int RSA_private_encrypt(int flen, const unsigned char *from,
191 unsigned char *to, RSA *rsa,int padding);
192int RSA_public_decrypt(int flen, const unsigned char *from,
193 unsigned char *to, RSA *rsa,int padding);
194int RSA_private_decrypt(int flen, const unsigned char *from,
195 unsigned char *to, RSA *rsa,int padding);
196void RSA_free (RSA *r);
197/* "up" the RSA object's reference count */
198int RSA_up_ref(RSA *r);
199
200int RSA_flags(const RSA *r);
201
202void RSA_set_default_method(const RSA_METHOD *meth);
203const RSA_METHOD *RSA_get_default_method(void);
204const RSA_METHOD *RSA_get_method(const RSA *rsa);
205int RSA_set_method(RSA *rsa, const RSA_METHOD *meth);
206
207/* This function needs the memory locking malloc callbacks to be installed */
208int RSA_memory_lock(RSA *r);
209
210/* these are the actual SSLeay RSA functions */
211const RSA_METHOD *RSA_PKCS1_SSLeay(void);
212
213const RSA_METHOD *RSA_null_method(void);
214
215DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPublicKey)
216DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPrivateKey)
217
218#ifndef OPENSSL_NO_FP_API
219int RSA_print_fp(FILE *fp, const RSA *r,int offset);
220#endif
221
222#ifndef OPENSSL_NO_BIO
223int RSA_print(BIO *bp, const RSA *r,int offset);
224#endif
225
226int i2d_RSA_NET(const RSA *a, unsigned char **pp, int (*cb)(), int sgckey);
227RSA *d2i_RSA_NET(RSA **a, const unsigned char **pp, long length, int (*cb)(), int sgckey);
228
229int i2d_Netscape_RSA(const RSA *a, unsigned char **pp, int (*cb)());
230RSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length, int (*cb)());
231
232/* The following 2 functions sign and verify a X509_SIG ASN1 object
233 * inside PKCS#1 padded RSA encryption */
234int RSA_sign(int type, const unsigned char *m, unsigned int m_length,
235 unsigned char *sigret, unsigned int *siglen, RSA *rsa);
236int RSA_verify(int type, const unsigned char *m, unsigned int m_length,
237 unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
238
239/* The following 2 function sign and verify a ASN1_OCTET_STRING
240 * object inside PKCS#1 padded RSA encryption */
241int RSA_sign_ASN1_OCTET_STRING(int type,
242 const unsigned char *m, unsigned int m_length,
243 unsigned char *sigret, unsigned int *siglen, RSA *rsa);
244int RSA_verify_ASN1_OCTET_STRING(int type,
245 const unsigned char *m, unsigned int m_length,
246 unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
247
248int RSA_blinding_on(RSA *rsa, BN_CTX *ctx);
249void RSA_blinding_off(RSA *rsa);
250
251int RSA_padding_add_PKCS1_type_1(unsigned char *to,int tlen,
252 const unsigned char *f,int fl);
253int RSA_padding_check_PKCS1_type_1(unsigned char *to,int tlen,
254 const unsigned char *f,int fl,int rsa_len);
255int RSA_padding_add_PKCS1_type_2(unsigned char *to,int tlen,
256 const unsigned char *f,int fl);
257int RSA_padding_check_PKCS1_type_2(unsigned char *to,int tlen,
258 const unsigned char *f,int fl,int rsa_len);
259int RSA_padding_add_PKCS1_OAEP(unsigned char *to,int tlen,
260 const unsigned char *f,int fl,
261 const unsigned char *p,int pl);
262int RSA_padding_check_PKCS1_OAEP(unsigned char *to,int tlen,
263 const unsigned char *f,int fl,int rsa_len,
264 const unsigned char *p,int pl);
265int RSA_padding_add_SSLv23(unsigned char *to,int tlen,
266 const unsigned char *f,int fl);
267int RSA_padding_check_SSLv23(unsigned char *to,int tlen,
268 const unsigned char *f,int fl,int rsa_len);
269int RSA_padding_add_none(unsigned char *to,int tlen,
270 const unsigned char *f,int fl);
271int RSA_padding_check_none(unsigned char *to,int tlen,
272 const unsigned char *f,int fl,int rsa_len);
273
274int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
275 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
276int RSA_set_ex_data(RSA *r,int idx,void *arg);
277void *RSA_get_ex_data(const RSA *r, int idx);
278
279RSA *RSAPublicKey_dup(RSA *rsa);
280RSA *RSAPrivateKey_dup(RSA *rsa);
281
282/* BEGIN ERROR CODES */
283/* The following lines are auto generated by the script mkerr.pl. Any changes
284 * made after this point may be overwritten when the script is next run.
285 */
286void ERR_load_RSA_strings(void);
287
288/* Error codes for the RSA functions. */
289
290/* Function codes. */
291#define RSA_F_MEMORY_LOCK 100
292#define RSA_F_RSA_CHECK_KEY 123
293#define RSA_F_RSA_EAY_PRIVATE_DECRYPT 101
294#define RSA_F_RSA_EAY_PRIVATE_ENCRYPT 102
295#define RSA_F_RSA_EAY_PUBLIC_DECRYPT 103
296#define RSA_F_RSA_EAY_PUBLIC_ENCRYPT 104
297#define RSA_F_RSA_GENERATE_KEY 105
298#define RSA_F_RSA_NEW_METHOD 106
299#define RSA_F_RSA_NULL 124
300#define RSA_F_RSA_PADDING_ADD_NONE 107
301#define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP 121
302#define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1 108
303#define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2 109
304#define RSA_F_RSA_PADDING_ADD_SSLV23 110
305#define RSA_F_RSA_PADDING_CHECK_NONE 111
306#define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP 122
307#define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1 112
308#define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2 113
309#define RSA_F_RSA_PADDING_CHECK_SSLV23 114
310#define RSA_F_RSA_PRINT 115
311#define RSA_F_RSA_PRINT_FP 116
312#define RSA_F_RSA_SIGN 117
313#define RSA_F_RSA_SIGN_ASN1_OCTET_STRING 118
314#define RSA_F_RSA_VERIFY 119
315#define RSA_F_RSA_VERIFY_ASN1_OCTET_STRING 120
316
317/* Reason codes. */
318#define RSA_R_ALGORITHM_MISMATCH 100
319#define RSA_R_BAD_E_VALUE 101
320#define RSA_R_BAD_FIXED_HEADER_DECRYPT 102
321#define RSA_R_BAD_PAD_BYTE_COUNT 103
322#define RSA_R_BAD_SIGNATURE 104
323#define RSA_R_BLOCK_TYPE_IS_NOT_01 106
324#define RSA_R_BLOCK_TYPE_IS_NOT_02 107
325#define RSA_R_DATA_GREATER_THAN_MOD_LEN 108
326#define RSA_R_DATA_TOO_LARGE 109
327#define RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 110
328#define RSA_R_DATA_TOO_LARGE_FOR_MODULUS 132
329#define RSA_R_DATA_TOO_SMALL 111
330#define RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE 122
331#define RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY 112
332#define RSA_R_DMP1_NOT_CONGRUENT_TO_D 124
333#define RSA_R_DMQ1_NOT_CONGRUENT_TO_D 125
334#define RSA_R_D_E_NOT_CONGRUENT_TO_1 123
335#define RSA_R_INVALID_MESSAGE_LENGTH 131
336#define RSA_R_IQMP_NOT_INVERSE_OF_Q 126
337#define RSA_R_KEY_SIZE_TOO_SMALL 120
338#define RSA_R_NULL_BEFORE_BLOCK_MISSING 113
339#define RSA_R_N_DOES_NOT_EQUAL_P_Q 127
340#define RSA_R_OAEP_DECODING_ERROR 121
341#define RSA_R_PADDING_CHECK_FAILED 114
342#define RSA_R_P_NOT_PRIME 128
343#define RSA_R_Q_NOT_PRIME 129
344#define RSA_R_RSA_OPERATIONS_NOT_SUPPORTED 130
345#define RSA_R_SSLV3_ROLLBACK_ATTACK 115
346#define RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 116
347#define RSA_R_UNKNOWN_ALGORITHM_TYPE 117
348#define RSA_R_UNKNOWN_PADDING_TYPE 118
349#define RSA_R_WRONG_SIGNATURE_LENGTH 119
350
351#ifdef __cplusplus
352}
353#endif
354#endif
diff --git a/src/lib/libcrypto/rsa/rsa_asn1.c b/src/lib/libcrypto/rsa/rsa_asn1.c
deleted file mode 100644
index 1455a7e0e4..0000000000
--- a/src/lib/libcrypto/rsa/rsa_asn1.c
+++ /dev/null
@@ -1,121 +0,0 @@
1/* rsa_asn1.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. 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 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/rsa.h>
63#include <openssl/asn1t.h>
64
65static ASN1_METHOD method={
66 (int (*)()) i2d_RSAPrivateKey,
67 (char *(*)())d2i_RSAPrivateKey,
68 (char *(*)())RSA_new,
69 (void (*)()) RSA_free};
70
71ASN1_METHOD *RSAPrivateKey_asn1_meth(void)
72 {
73 return(&method);
74 }
75
76/* Override the default free and new methods */
77static int rsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it)
78{
79 if(operation == ASN1_OP_NEW_PRE) {
80 *pval = (ASN1_VALUE *)RSA_new();
81 if(*pval) return 2;
82 return 0;
83 } else if(operation == ASN1_OP_FREE_PRE) {
84 RSA_free((RSA *)*pval);
85 *pval = NULL;
86 return 2;
87 }
88 return 1;
89}
90
91ASN1_SEQUENCE_cb(RSAPrivateKey, rsa_cb) = {
92 ASN1_SIMPLE(RSA, version, LONG),
93 ASN1_SIMPLE(RSA, n, BIGNUM),
94 ASN1_SIMPLE(RSA, e, BIGNUM),
95 ASN1_SIMPLE(RSA, d, BIGNUM),
96 ASN1_SIMPLE(RSA, p, BIGNUM),
97 ASN1_SIMPLE(RSA, q, BIGNUM),
98 ASN1_SIMPLE(RSA, dmp1, BIGNUM),
99 ASN1_SIMPLE(RSA, dmq1, BIGNUM),
100 ASN1_SIMPLE(RSA, iqmp, BIGNUM)
101} ASN1_SEQUENCE_END_cb(RSA, RSAPrivateKey)
102
103
104ASN1_SEQUENCE_cb(RSAPublicKey, rsa_cb) = {
105 ASN1_SIMPLE(RSA, n, BIGNUM),
106 ASN1_SIMPLE(RSA, e, BIGNUM),
107} ASN1_SEQUENCE_END_cb(RSA, RSAPublicKey)
108
109IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPrivateKey, RSAPrivateKey)
110
111IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPublicKey, RSAPublicKey)
112
113RSA *RSAPublicKey_dup(RSA *rsa)
114 {
115 return ASN1_item_dup(ASN1_ITEM_rptr(RSAPublicKey), rsa);
116 }
117
118RSA *RSAPrivateKey_dup(RSA *rsa)
119 {
120 return ASN1_item_dup(ASN1_ITEM_rptr(RSAPrivateKey), rsa);
121 }
diff --git a/src/lib/libcrypto/rsa/rsa_chk.c b/src/lib/libcrypto/rsa/rsa_chk.c
deleted file mode 100644
index 002f2cb487..0000000000
--- a/src/lib/libcrypto/rsa/rsa_chk.c
+++ /dev/null
@@ -1,184 +0,0 @@
1/* crypto/rsa/rsa_chk.c -*- Mode: C; c-file-style: "eay" -*- */
2/* ====================================================================
3 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
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
51#include <openssl/bn.h>
52#include <openssl/err.h>
53#include <openssl/rsa.h>
54
55
56int RSA_check_key(const RSA *key)
57 {
58 BIGNUM *i, *j, *k, *l, *m;
59 BN_CTX *ctx;
60 int r;
61 int ret=1;
62
63 i = BN_new();
64 j = BN_new();
65 k = BN_new();
66 l = BN_new();
67 m = BN_new();
68 ctx = BN_CTX_new();
69 if (i == NULL || j == NULL || k == NULL || l == NULL ||
70 m == NULL || ctx == NULL)
71 {
72 ret = -1;
73 RSAerr(RSA_F_RSA_CHECK_KEY, ERR_R_MALLOC_FAILURE);
74 goto err;
75 }
76
77 /* p prime? */
78 r = BN_is_prime(key->p, BN_prime_checks, NULL, NULL, NULL);
79 if (r != 1)
80 {
81 ret = r;
82 if (r != 0)
83 goto err;
84 RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_P_NOT_PRIME);
85 }
86
87 /* q prime? */
88 r = BN_is_prime(key->q, BN_prime_checks, NULL, NULL, NULL);
89 if (r != 1)
90 {
91 ret = r;
92 if (r != 0)
93 goto err;
94 RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_Q_NOT_PRIME);
95 }
96
97 /* n = p*q? */
98 r = BN_mul(i, key->p, key->q, ctx);
99 if (!r) { ret = -1; goto err; }
100
101 if (BN_cmp(i, key->n) != 0)
102 {
103 ret = 0;
104 RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_N_DOES_NOT_EQUAL_P_Q);
105 }
106
107 /* d*e = 1 mod lcm(p-1,q-1)? */
108
109 r = BN_sub(i, key->p, BN_value_one());
110 if (!r) { ret = -1; goto err; }
111 r = BN_sub(j, key->q, BN_value_one());
112 if (!r) { ret = -1; goto err; }
113
114 /* now compute k = lcm(i,j) */
115 r = BN_mul(l, i, j, ctx);
116 if (!r) { ret = -1; goto err; }
117 r = BN_gcd(m, i, j, ctx);
118 if (!r) { ret = -1; goto err; }
119 r = BN_div(k, NULL, l, m, ctx); /* remainder is 0 */
120 if (!r) { ret = -1; goto err; }
121
122 r = BN_mod_mul(i, key->d, key->e, k, ctx);
123 if (!r) { ret = -1; goto err; }
124
125 if (!BN_is_one(i))
126 {
127 ret = 0;
128 RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_D_E_NOT_CONGRUENT_TO_1);
129 }
130
131 if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL)
132 {
133 /* dmp1 = d mod (p-1)? */
134 r = BN_sub(i, key->p, BN_value_one());
135 if (!r) { ret = -1; goto err; }
136
137 r = BN_mod(j, key->d, i, ctx);
138 if (!r) { ret = -1; goto err; }
139
140 if (BN_cmp(j, key->dmp1) != 0)
141 {
142 ret = 0;
143 RSAerr(RSA_F_RSA_CHECK_KEY,
144 RSA_R_DMP1_NOT_CONGRUENT_TO_D);
145 }
146
147 /* dmq1 = d mod (q-1)? */
148 r = BN_sub(i, key->q, BN_value_one());
149 if (!r) { ret = -1; goto err; }
150
151 r = BN_mod(j, key->d, i, ctx);
152 if (!r) { ret = -1; goto err; }
153
154 if (BN_cmp(j, key->dmq1) != 0)
155 {
156 ret = 0;
157 RSAerr(RSA_F_RSA_CHECK_KEY,
158 RSA_R_DMQ1_NOT_CONGRUENT_TO_D);
159 }
160
161 /* iqmp = q^-1 mod p? */
162 if(!BN_mod_inverse(i, key->q, key->p, ctx))
163 {
164 ret = -1;
165 goto err;
166 }
167
168 if (BN_cmp(i, key->iqmp) != 0)
169 {
170 ret = 0;
171 RSAerr(RSA_F_RSA_CHECK_KEY,
172 RSA_R_IQMP_NOT_INVERSE_OF_Q);
173 }
174 }
175
176 err:
177 if (i != NULL) BN_free(i);
178 if (j != NULL) BN_free(j);
179 if (k != NULL) BN_free(k);
180 if (l != NULL) BN_free(l);
181 if (m != NULL) BN_free(m);
182 if (ctx != NULL) BN_CTX_free(ctx);
183 return (ret);
184 }
diff --git a/src/lib/libcrypto/rsa/rsa_eay.c b/src/lib/libcrypto/rsa/rsa_eay.c
deleted file mode 100644
index a3f549d8e6..0000000000
--- a/src/lib/libcrypto/rsa/rsa_eay.c
+++ /dev/null
@@ -1,628 +0,0 @@
1/* crypto/rsa/rsa_eay.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 "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/rsa.h>
63#include <openssl/rand.h>
64#include <openssl/engine.h>
65
66#ifndef RSA_NULL
67
68static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
69 unsigned char *to, RSA *rsa,int padding);
70static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
71 unsigned char *to, RSA *rsa,int padding);
72static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
73 unsigned char *to, RSA *rsa,int padding);
74static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
75 unsigned char *to, RSA *rsa,int padding);
76static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa);
77static int RSA_eay_init(RSA *rsa);
78static int RSA_eay_finish(RSA *rsa);
79static RSA_METHOD rsa_pkcs1_eay_meth={
80 "Eric Young's PKCS#1 RSA",
81 RSA_eay_public_encrypt,
82 RSA_eay_public_decrypt, /* signature verification */
83 RSA_eay_private_encrypt, /* signing */
84 RSA_eay_private_decrypt,
85 RSA_eay_mod_exp,
86 BN_mod_exp_mont, /* XXX probably we should not use Montgomery if e == 3 */
87 RSA_eay_init,
88 RSA_eay_finish,
89 0, /* flags */
90 NULL,
91 0, /* rsa_sign */
92 0 /* rsa_verify */
93 };
94
95const RSA_METHOD *RSA_PKCS1_SSLeay(void)
96 {
97 return(&rsa_pkcs1_eay_meth);
98 }
99
100static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
101 unsigned char *to, RSA *rsa, int padding)
102 {
103 BIGNUM f,ret;
104 int i,j,k,num=0,r= -1;
105 unsigned char *buf=NULL;
106 BN_CTX *ctx=NULL;
107
108 BN_init(&f);
109 BN_init(&ret);
110 if ((ctx=BN_CTX_new()) == NULL) goto err;
111 num=BN_num_bytes(rsa->n);
112 if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
113 {
114 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
115 goto err;
116 }
117
118 switch (padding)
119 {
120 case RSA_PKCS1_PADDING:
121 i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
122 break;
123#ifndef OPENSSL_NO_SHA
124 case RSA_PKCS1_OAEP_PADDING:
125 i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);
126 break;
127#endif
128 case RSA_SSLV23_PADDING:
129 i=RSA_padding_add_SSLv23(buf,num,from,flen);
130 break;
131 case RSA_NO_PADDING:
132 i=RSA_padding_add_none(buf,num,from,flen);
133 break;
134 default:
135 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
136 goto err;
137 }
138 if (i <= 0) goto err;
139
140 if (BN_bin2bn(buf,num,&f) == NULL) goto err;
141
142 if (BN_ucmp(&f, rsa->n) >= 0)
143 {
144 /* usually the padding functions would catch this */
145 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
146 goto err;
147 }
148
149 if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC))
150 {
151 BN_MONT_CTX* bn_mont_ctx;
152 if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
153 goto err;
154 if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->n,ctx))
155 {
156 BN_MONT_CTX_free(bn_mont_ctx);
157 goto err;
158 }
159 if (rsa->_method_mod_n == NULL) /* other thread may have finished first */
160 {
161 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
162 if (rsa->_method_mod_n == NULL)
163 {
164 rsa->_method_mod_n = bn_mont_ctx;
165 bn_mont_ctx = NULL;
166 }
167 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
168 }
169 if (bn_mont_ctx)
170 BN_MONT_CTX_free(bn_mont_ctx);
171 }
172
173 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx,
174 rsa->_method_mod_n)) goto err;
175
176 /* put in leading 0 bytes if the number is less than the
177 * length of the modulus */
178 j=BN_num_bytes(&ret);
179 i=BN_bn2bin(&ret,&(to[num-j]));
180 for (k=0; k<(num-i); k++)
181 to[k]=0;
182
183 r=num;
184err:
185 if (ctx != NULL) BN_CTX_free(ctx);
186 BN_clear_free(&f);
187 BN_clear_free(&ret);
188 if (buf != NULL)
189 {
190 memset(buf,0,num);
191 OPENSSL_free(buf);
192 }
193 return(r);
194 }
195
196static int rsa_eay_blinding(RSA *rsa, BN_CTX *ctx)
197 {
198 int ret = 1;
199 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
200 /* Check again inside the lock - the macro's check is racey */
201 if(rsa->blinding == NULL)
202 ret = RSA_blinding_on(rsa, ctx);
203 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
204 return ret;
205 }
206
207#define BLINDING_HELPER(rsa, ctx, err_instr) \
208 do { \
209 if(((rsa)->flags & RSA_FLAG_BLINDING) && \
210 ((rsa)->blinding == NULL) && \
211 !rsa_eay_blinding(rsa, ctx)) \
212 err_instr \
213 } while(0)
214
215/* signing */
216static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
217 unsigned char *to, RSA *rsa, int padding)
218 {
219 BIGNUM f,ret;
220 int i,j,k,num=0,r= -1;
221 unsigned char *buf=NULL;
222 BN_CTX *ctx=NULL;
223
224 BN_init(&f);
225 BN_init(&ret);
226
227 if ((ctx=BN_CTX_new()) == NULL) goto err;
228 num=BN_num_bytes(rsa->n);
229 if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
230 {
231 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
232 goto err;
233 }
234
235 switch (padding)
236 {
237 case RSA_PKCS1_PADDING:
238 i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);
239 break;
240 case RSA_NO_PADDING:
241 i=RSA_padding_add_none(buf,num,from,flen);
242 break;
243 case RSA_SSLV23_PADDING:
244 default:
245 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
246 goto err;
247 }
248 if (i <= 0) goto err;
249
250 if (BN_bin2bn(buf,num,&f) == NULL) goto err;
251
252 if (BN_ucmp(&f, rsa->n) >= 0)
253 {
254 /* usually the padding functions would catch this */
255 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
256 goto err;
257 }
258
259 BLINDING_HELPER(rsa, ctx, goto err;);
260
261 if (rsa->flags & RSA_FLAG_BLINDING)
262 if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
263
264 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
265 ((rsa->p != NULL) &&
266 (rsa->q != NULL) &&
267 (rsa->dmp1 != NULL) &&
268 (rsa->dmq1 != NULL) &&
269 (rsa->iqmp != NULL)) )
270 { if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; }
271 else
272 {
273 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) goto err;
274 }
275
276 if (rsa->flags & RSA_FLAG_BLINDING)
277 if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err;
278
279 /* put in leading 0 bytes if the number is less than the
280 * length of the modulus */
281 j=BN_num_bytes(&ret);
282 i=BN_bn2bin(&ret,&(to[num-j]));
283 for (k=0; k<(num-i); k++)
284 to[k]=0;
285
286 r=num;
287err:
288 if (ctx != NULL) BN_CTX_free(ctx);
289 BN_clear_free(&ret);
290 BN_clear_free(&f);
291 if (buf != NULL)
292 {
293 memset(buf,0,num);
294 OPENSSL_free(buf);
295 }
296 return(r);
297 }
298
299static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
300 unsigned char *to, RSA *rsa, int padding)
301 {
302 BIGNUM f,ret;
303 int j,num=0,r= -1;
304 unsigned char *p;
305 unsigned char *buf=NULL;
306 BN_CTX *ctx=NULL;
307
308 BN_init(&f);
309 BN_init(&ret);
310 ctx=BN_CTX_new();
311 if (ctx == NULL) goto err;
312
313 num=BN_num_bytes(rsa->n);
314
315 if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
316 {
317 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
318 goto err;
319 }
320
321 /* This check was for equality but PGP does evil things
322 * and chops off the top '0' bytes */
323 if (flen > num)
324 {
325 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
326 goto err;
327 }
328
329 /* make data into a big number */
330 if (BN_bin2bn(from,(int)flen,&f) == NULL) goto err;
331
332 if (BN_ucmp(&f, rsa->n) >= 0)
333 {
334 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
335 goto err;
336 }
337
338 BLINDING_HELPER(rsa, ctx, goto err;);
339
340 if (rsa->flags & RSA_FLAG_BLINDING)
341 if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
342
343 /* do the decrypt */
344 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
345 ((rsa->p != NULL) &&
346 (rsa->q != NULL) &&
347 (rsa->dmp1 != NULL) &&
348 (rsa->dmq1 != NULL) &&
349 (rsa->iqmp != NULL)) )
350 { if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; }
351 else
352 {
353 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL))
354 goto err;
355 }
356
357 if (rsa->flags & RSA_FLAG_BLINDING)
358 if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err;
359
360 p=buf;
361 j=BN_bn2bin(&ret,p); /* j is only used with no-padding mode */
362
363 switch (padding)
364 {
365 case RSA_PKCS1_PADDING:
366 r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);
367 break;
368#ifndef OPENSSL_NO_SHA
369 case RSA_PKCS1_OAEP_PADDING:
370 r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0);
371 break;
372#endif
373 case RSA_SSLV23_PADDING:
374 r=RSA_padding_check_SSLv23(to,num,buf,j,num);
375 break;
376 case RSA_NO_PADDING:
377 r=RSA_padding_check_none(to,num,buf,j,num);
378 break;
379 default:
380 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
381 goto err;
382 }
383 if (r < 0)
384 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
385
386err:
387 if (ctx != NULL) BN_CTX_free(ctx);
388 BN_clear_free(&f);
389 BN_clear_free(&ret);
390 if (buf != NULL)
391 {
392 memset(buf,0,num);
393 OPENSSL_free(buf);
394 }
395 return(r);
396 }
397
398/* signature verification */
399static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
400 unsigned char *to, RSA *rsa, int padding)
401 {
402 BIGNUM f,ret;
403 int i,num=0,r= -1;
404 unsigned char *p;
405 unsigned char *buf=NULL;
406 BN_CTX *ctx=NULL;
407
408 BN_init(&f);
409 BN_init(&ret);
410 ctx=BN_CTX_new();
411 if (ctx == NULL) goto err;
412
413 num=BN_num_bytes(rsa->n);
414 buf=(unsigned char *)OPENSSL_malloc(num);
415 if (buf == NULL)
416 {
417 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
418 goto err;
419 }
420
421 /* This check was for equality but PGP does evil things
422 * and chops off the top '0' bytes */
423 if (flen > num)
424 {
425 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
426 goto err;
427 }
428
429 if (BN_bin2bn(from,flen,&f) == NULL) goto err;
430
431 if (BN_ucmp(&f, rsa->n) >= 0)
432 {
433 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
434 goto err;
435 }
436
437 /* do the decrypt */
438 if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC))
439 {
440 BN_MONT_CTX* bn_mont_ctx;
441 if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
442 goto err;
443 if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->n,ctx))
444 {
445 BN_MONT_CTX_free(bn_mont_ctx);
446 goto err;
447 }
448 if (rsa->_method_mod_n == NULL) /* other thread may have finished first */
449 {
450 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
451 if (rsa->_method_mod_n == NULL)
452 {
453 rsa->_method_mod_n = bn_mont_ctx;
454 bn_mont_ctx = NULL;
455 }
456 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
457 }
458 if (bn_mont_ctx)
459 BN_MONT_CTX_free(bn_mont_ctx);
460 }
461
462 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx,
463 rsa->_method_mod_n)) goto err;
464
465 p=buf;
466 i=BN_bn2bin(&ret,p);
467
468 switch (padding)
469 {
470 case RSA_PKCS1_PADDING:
471 r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num);
472 break;
473 case RSA_NO_PADDING:
474 r=RSA_padding_check_none(to,num,buf,i,num);
475 break;
476 default:
477 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
478 goto err;
479 }
480 if (r < 0)
481 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
482
483err:
484 if (ctx != NULL) BN_CTX_free(ctx);
485 BN_clear_free(&f);
486 BN_clear_free(&ret);
487 if (buf != NULL)
488 {
489 memset(buf,0,num);
490 OPENSSL_free(buf);
491 }
492 return(r);
493 }
494
495static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
496 {
497 BIGNUM r1,m1,vrfy;
498 int ret=0;
499 BN_CTX *ctx;
500
501 BN_init(&m1);
502 BN_init(&r1);
503 BN_init(&vrfy);
504 if ((ctx=BN_CTX_new()) == NULL) goto err;
505
506 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE)
507 {
508 if (rsa->_method_mod_p == NULL)
509 {
510 BN_MONT_CTX* bn_mont_ctx;
511 if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
512 goto err;
513 if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->p,ctx))
514 {
515 BN_MONT_CTX_free(bn_mont_ctx);
516 goto err;
517 }
518 if (rsa->_method_mod_p == NULL) /* other thread may have finished first */
519 {
520 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
521 if (rsa->_method_mod_p == NULL)
522 {
523 rsa->_method_mod_p = bn_mont_ctx;
524 bn_mont_ctx = NULL;
525 }
526 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
527 }
528 if (bn_mont_ctx)
529 BN_MONT_CTX_free(bn_mont_ctx);
530 }
531
532 if (rsa->_method_mod_q == NULL)
533 {
534 BN_MONT_CTX* bn_mont_ctx;
535 if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
536 goto err;
537 if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->q,ctx))
538 {
539 BN_MONT_CTX_free(bn_mont_ctx);
540 goto err;
541 }
542 if (rsa->_method_mod_q == NULL) /* other thread may have finished first */
543 {
544 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
545 if (rsa->_method_mod_q == NULL)
546 {
547 rsa->_method_mod_q = bn_mont_ctx;
548 bn_mont_ctx = NULL;
549 }
550 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
551 }
552 if (bn_mont_ctx)
553 BN_MONT_CTX_free(bn_mont_ctx);
554 }
555 }
556
557 if (!BN_mod(&r1,I,rsa->q,ctx)) goto err;
558 if (!rsa->meth->bn_mod_exp(&m1,&r1,rsa->dmq1,rsa->q,ctx,
559 rsa->_method_mod_q)) goto err;
560
561 if (!BN_mod(&r1,I,rsa->p,ctx)) goto err;
562 if (!rsa->meth->bn_mod_exp(r0,&r1,rsa->dmp1,rsa->p,ctx,
563 rsa->_method_mod_p)) goto err;
564
565 if (!BN_sub(r0,r0,&m1)) goto err;
566 /* This will help stop the size of r0 increasing, which does
567 * affect the multiply if it optimised for a power of 2 size */
568 if (r0->neg)
569 if (!BN_add(r0,r0,rsa->p)) goto err;
570
571 if (!BN_mul(&r1,r0,rsa->iqmp,ctx)) goto err;
572 if (!BN_mod(r0,&r1,rsa->p,ctx)) goto err;
573 /* If p < q it is occasionally possible for the correction of
574 * adding 'p' if r0 is negative above to leave the result still
575 * negative. This can break the private key operations: the following
576 * second correction should *always* correct this rare occurrence.
577 * This will *never* happen with OpenSSL generated keys because
578 * they ensure p > q [steve]
579 */
580 if (r0->neg)
581 if (!BN_add(r0,r0,rsa->p)) goto err;
582 if (!BN_mul(&r1,r0,rsa->q,ctx)) goto err;
583 if (!BN_add(r0,&r1,&m1)) goto err;
584
585 if (rsa->e && rsa->n)
586 {
587 if (!rsa->meth->bn_mod_exp(&vrfy,r0,rsa->e,rsa->n,ctx,NULL)) goto err;
588 /* If 'I' was greater than (or equal to) rsa->n, the operation
589 * will be equivalent to using 'I mod n'. However, the result of
590 * the verify will *always* be less than 'n' so we don't check
591 * for absolute equality, just congruency. */
592 if (!BN_sub(&vrfy, &vrfy, I)) goto err;
593 if (!BN_mod(&vrfy, &vrfy, rsa->n, ctx)) goto err;
594 if (vrfy.neg)
595 if (!BN_add(&vrfy, &vrfy, rsa->n)) goto err;
596 if (!BN_is_zero(&vrfy))
597 /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
598 * miscalculated CRT output, just do a raw (slower)
599 * mod_exp and return that instead. */
600 if (!rsa->meth->bn_mod_exp(r0,I,rsa->d,rsa->n,ctx,NULL)) goto err;
601 }
602 ret=1;
603err:
604 BN_clear_free(&m1);
605 BN_clear_free(&r1);
606 BN_clear_free(&vrfy);
607 BN_CTX_free(ctx);
608 return(ret);
609 }
610
611static int RSA_eay_init(RSA *rsa)
612 {
613 rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
614 return(1);
615 }
616
617static int RSA_eay_finish(RSA *rsa)
618 {
619 if (rsa->_method_mod_n != NULL)
620 BN_MONT_CTX_free(rsa->_method_mod_n);
621 if (rsa->_method_mod_p != NULL)
622 BN_MONT_CTX_free(rsa->_method_mod_p);
623 if (rsa->_method_mod_q != NULL)
624 BN_MONT_CTX_free(rsa->_method_mod_q);
625 return(1);
626 }
627
628#endif
diff --git a/src/lib/libcrypto/rsa/rsa_err.c b/src/lib/libcrypto/rsa/rsa_err.c
deleted file mode 100644
index a7766c3b76..0000000000
--- a/src/lib/libcrypto/rsa/rsa_err.c
+++ /dev/null
@@ -1,149 +0,0 @@
1/* crypto/rsa/rsa_err.c */
2/* ====================================================================
3 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
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 *
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
61#include <stdio.h>
62#include <openssl/err.h>
63#include <openssl/rsa.h>
64
65/* BEGIN ERROR CODES */
66#ifndef OPENSSL_NO_ERR
67static ERR_STRING_DATA RSA_str_functs[]=
68 {
69{ERR_PACK(0,RSA_F_MEMORY_LOCK,0), "MEMORY_LOCK"},
70{ERR_PACK(0,RSA_F_RSA_CHECK_KEY,0), "RSA_check_key"},
71{ERR_PACK(0,RSA_F_RSA_EAY_PRIVATE_DECRYPT,0), "RSA_EAY_PRIVATE_DECRYPT"},
72{ERR_PACK(0,RSA_F_RSA_EAY_PRIVATE_ENCRYPT,0), "RSA_EAY_PRIVATE_ENCRYPT"},
73{ERR_PACK(0,RSA_F_RSA_EAY_PUBLIC_DECRYPT,0), "RSA_EAY_PUBLIC_DECRYPT"},
74{ERR_PACK(0,RSA_F_RSA_EAY_PUBLIC_ENCRYPT,0), "RSA_EAY_PUBLIC_ENCRYPT"},
75{ERR_PACK(0,RSA_F_RSA_GENERATE_KEY,0), "RSA_generate_key"},
76{ERR_PACK(0,RSA_F_RSA_NEW_METHOD,0), "RSA_new_method"},
77{ERR_PACK(0,RSA_F_RSA_NULL,0), "RSA_NULL"},
78{ERR_PACK(0,RSA_F_RSA_PADDING_ADD_NONE,0), "RSA_padding_add_none"},
79{ERR_PACK(0,RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,0), "RSA_padding_add_PKCS1_OAEP"},
80{ERR_PACK(0,RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,0), "RSA_padding_add_PKCS1_type_1"},
81{ERR_PACK(0,RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,0), "RSA_padding_add_PKCS1_type_2"},
82{ERR_PACK(0,RSA_F_RSA_PADDING_ADD_SSLV23,0), "RSA_padding_add_SSLv23"},
83{ERR_PACK(0,RSA_F_RSA_PADDING_CHECK_NONE,0), "RSA_padding_check_none"},
84{ERR_PACK(0,RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP,0), "RSA_padding_check_PKCS1_OAEP"},
85{ERR_PACK(0,RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,0), "RSA_padding_check_PKCS1_type_1"},
86{ERR_PACK(0,RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,0), "RSA_padding_check_PKCS1_type_2"},
87{ERR_PACK(0,RSA_F_RSA_PADDING_CHECK_SSLV23,0), "RSA_padding_check_SSLv23"},
88{ERR_PACK(0,RSA_F_RSA_PRINT,0), "RSA_print"},
89{ERR_PACK(0,RSA_F_RSA_PRINT_FP,0), "RSA_print_fp"},
90{ERR_PACK(0,RSA_F_RSA_SIGN,0), "RSA_sign"},
91{ERR_PACK(0,RSA_F_RSA_SIGN_ASN1_OCTET_STRING,0), "RSA_sign_ASN1_OCTET_STRING"},
92{ERR_PACK(0,RSA_F_RSA_VERIFY,0), "RSA_verify"},
93{ERR_PACK(0,RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,0), "RSA_verify_ASN1_OCTET_STRING"},
94{0,NULL}
95 };
96
97static ERR_STRING_DATA RSA_str_reasons[]=
98 {
99{RSA_R_ALGORITHM_MISMATCH ,"algorithm mismatch"},
100{RSA_R_BAD_E_VALUE ,"bad e value"},
101{RSA_R_BAD_FIXED_HEADER_DECRYPT ,"bad fixed header decrypt"},
102{RSA_R_BAD_PAD_BYTE_COUNT ,"bad pad byte count"},
103{RSA_R_BAD_SIGNATURE ,"bad signature"},
104{RSA_R_BLOCK_TYPE_IS_NOT_01 ,"block type is not 01"},
105{RSA_R_BLOCK_TYPE_IS_NOT_02 ,"block type is not 02"},
106{RSA_R_DATA_GREATER_THAN_MOD_LEN ,"data greater than mod len"},
107{RSA_R_DATA_TOO_LARGE ,"data too large"},
108{RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE ,"data too large for key size"},
109{RSA_R_DATA_TOO_LARGE_FOR_MODULUS ,"data too large for modulus"},
110{RSA_R_DATA_TOO_SMALL ,"data too small"},
111{RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE ,"data too small for key size"},
112{RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY ,"digest too big for rsa key"},
113{RSA_R_DMP1_NOT_CONGRUENT_TO_D ,"dmp1 not congruent to d"},
114{RSA_R_DMQ1_NOT_CONGRUENT_TO_D ,"dmq1 not congruent to d"},
115{RSA_R_D_E_NOT_CONGRUENT_TO_1 ,"d e not congruent to 1"},
116{RSA_R_INVALID_MESSAGE_LENGTH ,"invalid message length"},
117{RSA_R_IQMP_NOT_INVERSE_OF_Q ,"iqmp not inverse of q"},
118{RSA_R_KEY_SIZE_TOO_SMALL ,"key size too small"},
119{RSA_R_NULL_BEFORE_BLOCK_MISSING ,"null before block missing"},
120{RSA_R_N_DOES_NOT_EQUAL_P_Q ,"n does not equal p q"},
121{RSA_R_OAEP_DECODING_ERROR ,"oaep decoding error"},
122{RSA_R_PADDING_CHECK_FAILED ,"padding check failed"},
123{RSA_R_P_NOT_PRIME ,"p not prime"},
124{RSA_R_Q_NOT_PRIME ,"q not prime"},
125{RSA_R_RSA_OPERATIONS_NOT_SUPPORTED ,"rsa operations not supported"},
126{RSA_R_SSLV3_ROLLBACK_ATTACK ,"sslv3 rollback attack"},
127{RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD,"the asn1 object identifier is not known for this md"},
128{RSA_R_UNKNOWN_ALGORITHM_TYPE ,"unknown algorithm type"},
129{RSA_R_UNKNOWN_PADDING_TYPE ,"unknown padding type"},
130{RSA_R_WRONG_SIGNATURE_LENGTH ,"wrong signature length"},
131{0,NULL}
132 };
133
134#endif
135
136void ERR_load_RSA_strings(void)
137 {
138 static int init=1;
139
140 if (init)
141 {
142 init=0;
143#ifndef OPENSSL_NO_ERR
144 ERR_load_strings(ERR_LIB_RSA,RSA_str_functs);
145 ERR_load_strings(ERR_LIB_RSA,RSA_str_reasons);
146#endif
147
148 }
149 }
diff --git a/src/lib/libcrypto/rsa/rsa_gen.c b/src/lib/libcrypto/rsa/rsa_gen.c
deleted file mode 100644
index 00c25adbc5..0000000000
--- a/src/lib/libcrypto/rsa/rsa_gen.c
+++ /dev/null
@@ -1,197 +0,0 @@
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 <time.h>
61#include "cryptlib.h"
62#include <openssl/bn.h>
63#include <openssl/rsa.h>
64
65RSA *RSA_generate_key(int bits, unsigned long e_value,
66 void (*callback)(int,int,void *), void *cb_arg)
67 {
68 RSA *rsa=NULL;
69 BIGNUM *r0=NULL,*r1=NULL,*r2=NULL,*r3=NULL,*tmp;
70 int bitsp,bitsq,ok= -1,n=0,i;
71 BN_CTX *ctx=NULL,*ctx2=NULL;
72
73 ctx=BN_CTX_new();
74 if (ctx == NULL) goto err;
75 ctx2=BN_CTX_new();
76 if (ctx2 == NULL) goto err;
77 BN_CTX_start(ctx);
78 r0 = BN_CTX_get(ctx);
79 r1 = BN_CTX_get(ctx);
80 r2 = BN_CTX_get(ctx);
81 r3 = BN_CTX_get(ctx);
82 if (r3 == NULL) goto err;
83
84 bitsp=(bits+1)/2;
85 bitsq=bits-bitsp;
86 rsa=RSA_new();
87 if (rsa == NULL) goto err;
88
89 /* set e */
90 rsa->e=BN_new();
91 if (rsa->e == NULL) goto err;
92
93#if 1
94 /* The problem is when building with 8, 16, or 32 BN_ULONG,
95 * unsigned long can be larger */
96 for (i=0; i<sizeof(unsigned long)*8; i++)
97 {
98 if (e_value & (1UL<<i))
99 BN_set_bit(rsa->e,i);
100 }
101#else
102 if (!BN_set_word(rsa->e,e_value)) goto err;
103#endif
104
105 /* generate p and q */
106 for (;;)
107 {
108 rsa->p=BN_generate_prime(NULL,bitsp,0,NULL,NULL,callback,cb_arg);
109 if (rsa->p == NULL) goto err;
110 if (!BN_sub(r2,rsa->p,BN_value_one())) goto err;
111 if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err;
112 if (BN_is_one(r1)) break;
113 if (callback != NULL) callback(2,n++,cb_arg);
114 BN_free(rsa->p);
115 }
116 if (callback != NULL) callback(3,0,cb_arg);
117 for (;;)
118 {
119 rsa->q=BN_generate_prime(NULL,bitsq,0,NULL,NULL,callback,cb_arg);
120 if (rsa->q == NULL) goto err;
121 if (!BN_sub(r2,rsa->q,BN_value_one())) goto err;
122 if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err;
123 if (BN_is_one(r1) && (BN_cmp(rsa->p,rsa->q) != 0))
124 break;
125 if (callback != NULL) callback(2,n++,cb_arg);
126 BN_free(rsa->q);
127 }
128 if (callback != NULL) callback(3,1,cb_arg);
129 if (BN_cmp(rsa->p,rsa->q) < 0)
130 {
131 tmp=rsa->p;
132 rsa->p=rsa->q;
133 rsa->q=tmp;
134 }
135
136 /* calculate n */
137 rsa->n=BN_new();
138 if (rsa->n == NULL) goto err;
139 if (!BN_mul(rsa->n,rsa->p,rsa->q,ctx)) goto err;
140
141 /* calculate d */
142 if (!BN_sub(r1,rsa->p,BN_value_one())) goto err; /* p-1 */
143 if (!BN_sub(r2,rsa->q,BN_value_one())) goto err; /* q-1 */
144 if (!BN_mul(r0,r1,r2,ctx)) goto err; /* (p-1)(q-1) */
145
146/* should not be needed, since gcd(p-1,e) == 1 and gcd(q-1,e) == 1 */
147/* for (;;)
148 {
149 if (!BN_gcd(r3,r0,rsa->e,ctx)) goto err;
150 if (BN_is_one(r3)) break;
151
152 if (1)
153 {
154 if (!BN_add_word(rsa->e,2L)) goto err;
155 continue;
156 }
157 RSAerr(RSA_F_RSA_GENERATE_KEY,RSA_R_BAD_E_VALUE);
158 goto err;
159 }
160*/
161 rsa->d=BN_mod_inverse(NULL,rsa->e,r0,ctx2); /* d */
162 if (rsa->d == NULL) goto err;
163
164 /* calculate d mod (p-1) */
165 rsa->dmp1=BN_new();
166 if (rsa->dmp1 == NULL) goto err;
167 if (!BN_mod(rsa->dmp1,rsa->d,r1,ctx)) goto err;
168
169 /* calculate d mod (q-1) */
170 rsa->dmq1=BN_new();
171 if (rsa->dmq1 == NULL) goto err;
172 if (!BN_mod(rsa->dmq1,rsa->d,r2,ctx)) goto err;
173
174 /* calculate inverse of q mod p */
175 rsa->iqmp=BN_mod_inverse(NULL,rsa->q,rsa->p,ctx2);
176 if (rsa->iqmp == NULL) goto err;
177
178 ok=1;
179err:
180 if (ok == -1)
181 {
182 RSAerr(RSA_F_RSA_GENERATE_KEY,ERR_LIB_BN);
183 ok=0;
184 }
185 BN_CTX_end(ctx);
186 BN_CTX_free(ctx);
187 BN_CTX_free(ctx2);
188
189 if (!ok)
190 {
191 if (rsa != NULL) RSA_free(rsa);
192 return(NULL);
193 }
194 else
195 return(rsa);
196 }
197
diff --git a/src/lib/libcrypto/rsa/rsa_lib.c b/src/lib/libcrypto/rsa/rsa_lib.c
deleted file mode 100644
index 37fff8bce3..0000000000
--- a/src/lib/libcrypto/rsa/rsa_lib.c
+++ /dev/null
@@ -1,385 +0,0 @@
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/engine.h>
66
67const char *RSA_version="RSA" OPENSSL_VERSION_PTEXT;
68
69static const RSA_METHOD *default_RSA_meth=NULL;
70
71RSA *RSA_new(void)
72 {
73 RSA *r=RSA_new_method(NULL);
74
75#ifndef OPENSSL_NO_FORCE_RSA_BLINDING
76 r->flags|=RSA_FLAG_BLINDING;
77#endif
78
79 return r;
80 }
81
82void RSA_set_default_method(const RSA_METHOD *meth)
83 {
84 default_RSA_meth = meth;
85 }
86
87const RSA_METHOD *RSA_get_default_method(void)
88 {
89 if (default_RSA_meth == NULL)
90 {
91#ifdef RSA_NULL
92 default_RSA_meth=RSA_null_method();
93#else
94#if 0 /* was: #ifdef RSAref */
95 default_RSA_meth=RSA_PKCS1_RSAref();
96#else
97 default_RSA_meth=RSA_PKCS1_SSLeay();
98#endif
99#endif
100 }
101
102 return default_RSA_meth;
103 }
104
105const RSA_METHOD *RSA_get_method(const RSA *rsa)
106 {
107 return rsa->meth;
108 }
109
110int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
111 {
112 /* NB: The caller is specifically setting a method, so it's not up to us
113 * to deal with which ENGINE it comes from. */
114 const RSA_METHOD *mtmp;
115 mtmp = rsa->meth;
116 if (mtmp->finish) mtmp->finish(rsa);
117 if (rsa->engine)
118 {
119 ENGINE_finish(rsa->engine);
120 rsa->engine = NULL;
121 }
122 rsa->meth = meth;
123 if (meth->init) meth->init(rsa);
124 return 1;
125 }
126
127RSA *RSA_new_method(ENGINE *engine)
128 {
129 RSA *ret;
130
131 ret=(RSA *)OPENSSL_malloc(sizeof(RSA));
132 if (ret == NULL)
133 {
134 RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
135 return NULL;
136 }
137
138 ret->meth = RSA_get_default_method();
139 if (engine)
140 {
141 if (!ENGINE_init(engine))
142 {
143 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
144 OPENSSL_free(ret);
145 return NULL;
146 }
147 ret->engine = engine;
148 }
149 else
150 ret->engine = ENGINE_get_default_RSA();
151 if(ret->engine)
152 {
153 ret->meth = ENGINE_get_RSA(ret->engine);
154 if(!ret->meth)
155 {
156 RSAerr(RSA_F_RSA_NEW_METHOD,
157 ERR_R_ENGINE_LIB);
158 ENGINE_finish(ret->engine);
159 OPENSSL_free(ret);
160 return NULL;
161 }
162 }
163
164 ret->pad=0;
165 ret->version=0;
166 ret->n=NULL;
167 ret->e=NULL;
168 ret->d=NULL;
169 ret->p=NULL;
170 ret->q=NULL;
171 ret->dmp1=NULL;
172 ret->dmq1=NULL;
173 ret->iqmp=NULL;
174 ret->references=1;
175 ret->_method_mod_n=NULL;
176 ret->_method_mod_p=NULL;
177 ret->_method_mod_q=NULL;
178 ret->blinding=NULL;
179 ret->bignum_data=NULL;
180 ret->flags=ret->meth->flags;
181 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
182 if ((ret->meth->init != NULL) && !ret->meth->init(ret))
183 {
184 if (ret->engine)
185 ENGINE_finish(ret->engine);
186 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
187 OPENSSL_free(ret);
188 ret=NULL;
189 }
190 return(ret);
191 }
192
193void RSA_free(RSA *r)
194 {
195 int i;
196
197 if (r == NULL) return;
198
199 i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_RSA);
200#ifdef REF_PRINT
201 REF_PRINT("RSA",r);
202#endif
203 if (i > 0) return;
204#ifdef REF_CHECK
205 if (i < 0)
206 {
207 fprintf(stderr,"RSA_free, bad reference count\n");
208 abort();
209 }
210#endif
211
212 if (r->meth->finish)
213 r->meth->finish(r);
214 if (r->engine)
215 ENGINE_finish(r->engine);
216
217 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
218
219 if (r->n != NULL) BN_clear_free(r->n);
220 if (r->e != NULL) BN_clear_free(r->e);
221 if (r->d != NULL) BN_clear_free(r->d);
222 if (r->p != NULL) BN_clear_free(r->p);
223 if (r->q != NULL) BN_clear_free(r->q);
224 if (r->dmp1 != NULL) BN_clear_free(r->dmp1);
225 if (r->dmq1 != NULL) BN_clear_free(r->dmq1);
226 if (r->iqmp != NULL) BN_clear_free(r->iqmp);
227 if (r->blinding != NULL) BN_BLINDING_free(r->blinding);
228 if (r->bignum_data != NULL) OPENSSL_free_locked(r->bignum_data);
229 OPENSSL_free(r);
230 }
231
232int RSA_up_ref(RSA *r)
233 {
234 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
235#ifdef REF_PRINT
236 REF_PRINT("RSA",r);
237#endif
238#ifdef REF_CHECK
239 if (i < 2)
240 {
241 fprintf(stderr, "RSA_up_ref, bad reference count\n");
242 abort();
243 }
244#endif
245 return ((i > 1) ? 1 : 0);
246 }
247
248int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
249 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
250 {
251 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
252 new_func, dup_func, free_func);
253 }
254
255int RSA_set_ex_data(RSA *r, int idx, void *arg)
256 {
257 return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
258 }
259
260void *RSA_get_ex_data(const RSA *r, int idx)
261 {
262 return(CRYPTO_get_ex_data(&r->ex_data,idx));
263 }
264
265int RSA_size(const RSA *r)
266 {
267 return(BN_num_bytes(r->n));
268 }
269
270int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
271 RSA *rsa, int padding)
272 {
273 return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding));
274 }
275
276int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to,
277 RSA *rsa, int padding)
278 {
279 return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding));
280 }
281
282int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to,
283 RSA *rsa, int padding)
284 {
285 return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding));
286 }
287
288int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
289 RSA *rsa, int padding)
290 {
291 return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding));
292 }
293
294int RSA_flags(const RSA *r)
295 {
296 return((r == NULL)?0:r->meth->flags);
297 }
298
299void RSA_blinding_off(RSA *rsa)
300 {
301 if (rsa->blinding != NULL)
302 {
303 BN_BLINDING_free(rsa->blinding);
304 rsa->blinding=NULL;
305 }
306 rsa->flags&= ~RSA_FLAG_BLINDING;
307 }
308
309int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx)
310 {
311 BIGNUM *A,*Ai;
312 BN_CTX *ctx;
313 int ret=0;
314
315 if (p_ctx == NULL)
316 {
317 if ((ctx=BN_CTX_new()) == NULL) goto err;
318 }
319 else
320 ctx=p_ctx;
321
322 if (rsa->blinding != NULL)
323 BN_BLINDING_free(rsa->blinding);
324
325 BN_CTX_start(ctx);
326 A = BN_CTX_get(ctx);
327 if (!BN_rand_range(A,rsa->n)) goto err;
328 if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
329
330 if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n))
331 goto err;
332 rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n);
333 rsa->flags|=RSA_FLAG_BLINDING;
334 BN_free(Ai);
335 ret=1;
336err:
337 BN_CTX_end(ctx);
338 if (ctx != p_ctx) BN_CTX_free(ctx);
339 return(ret);
340 }
341
342int RSA_memory_lock(RSA *r)
343 {
344 int i,j,k,off;
345 char *p;
346 BIGNUM *bn,**t[6],*b;
347 BN_ULONG *ul;
348
349 if (r->d == NULL) return(1);
350 t[0]= &r->d;
351 t[1]= &r->p;
352 t[2]= &r->q;
353 t[3]= &r->dmp1;
354 t[4]= &r->dmq1;
355 t[5]= &r->iqmp;
356 k=sizeof(BIGNUM)*6;
357 off=k/sizeof(BN_ULONG)+1;
358 j=1;
359 for (i=0; i<6; i++)
360 j+= (*t[i])->top;
361 if ((p=OPENSSL_malloc_locked((off+j)*sizeof(BN_ULONG))) == NULL)
362 {
363 RSAerr(RSA_F_MEMORY_LOCK,ERR_R_MALLOC_FAILURE);
364 return(0);
365 }
366 bn=(BIGNUM *)p;
367 ul=(BN_ULONG *)&(p[off]);
368 for (i=0; i<6; i++)
369 {
370 b= *(t[i]);
371 *(t[i])= &(bn[i]);
372 memcpy((char *)&(bn[i]),(char *)b,sizeof(BIGNUM));
373 bn[i].flags=BN_FLG_STATIC_DATA;
374 bn[i].d=ul;
375 memcpy((char *)ul,b->d,sizeof(BN_ULONG)*b->top);
376 ul+=b->top;
377 BN_clear_free(b);
378 }
379
380 /* I should fix this so it can still be done */
381 r->flags&= ~(RSA_FLAG_CACHE_PRIVATE|RSA_FLAG_CACHE_PUBLIC);
382
383 r->bignum_data=p;
384 return(1);
385 }
diff --git a/src/lib/libcrypto/rsa/rsa_none.c b/src/lib/libcrypto/rsa/rsa_none.c
deleted file mode 100644
index e6f3e627ca..0000000000
--- a/src/lib/libcrypto/rsa/rsa_none.c
+++ /dev/null
@@ -1,98 +0,0 @@
1/* crypto/rsa/rsa_none.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 "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/rsa.h>
63#include <openssl/rand.h>
64
65int RSA_padding_add_none(unsigned char *to, int tlen,
66 const unsigned char *from, int flen)
67 {
68 if (flen > tlen)
69 {
70 RSAerr(RSA_F_RSA_PADDING_ADD_NONE,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
71 return(0);
72 }
73
74 if (flen < tlen)
75 {
76 RSAerr(RSA_F_RSA_PADDING_ADD_NONE,RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE);
77 return(0);
78 }
79
80 memcpy(to,from,(unsigned int)flen);
81 return(1);
82 }
83
84int RSA_padding_check_none(unsigned char *to, int tlen,
85 const unsigned char *from, int flen, int num)
86 {
87
88 if (flen > tlen)
89 {
90 RSAerr(RSA_F_RSA_PADDING_CHECK_NONE,RSA_R_DATA_TOO_LARGE);
91 return(-1);
92 }
93
94 memset(to,0,tlen-flen);
95 memcpy(to+tlen-flen,from,flen);
96 return(tlen);
97 }
98
diff --git a/src/lib/libcrypto/rsa/rsa_oaep.c b/src/lib/libcrypto/rsa/rsa_oaep.c
deleted file mode 100644
index e3f7c608ec..0000000000
--- a/src/lib/libcrypto/rsa/rsa_oaep.c
+++ /dev/null
@@ -1,206 +0,0 @@
1/* crypto/rsa/rsa_oaep.c */
2/* Written by Ulf Moeller. This software is distributed on an "AS IS"
3 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. */
4
5/* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
6
7/* See Victor Shoup, "OAEP reconsidered," Nov. 2000,
8 * <URL: http://www.shoup.net/papers/oaep.ps.Z>
9 * for problems with the security proof for the
10 * original OAEP scheme, which EME-OAEP is based on.
11 *
12 * A new proof can be found in E. Fujisaki, T. Okamoto,
13 * D. Pointcheval, J. Stern, "RSA-OEAP is Still Alive!",
14 * Dec. 2000, <URL: http://eprint.iacr.org/2000/061/>.
15 * The new proof has stronger requirements for the
16 * underlying permutation: "partial-one-wayness" instead
17 * of one-wayness. For the RSA function, this is
18 * an equivalent notion.
19 */
20
21
22#if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
23#include <stdio.h>
24#include "cryptlib.h"
25#include <openssl/bn.h>
26#include <openssl/rsa.h>
27#include <openssl/evp.h>
28#include <openssl/rand.h>
29#include <openssl/sha.h>
30
31int MGF1(unsigned char *mask, long len,
32 const unsigned char *seed, long seedlen);
33
34int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
35 const unsigned char *from, int flen,
36 const unsigned char *param, int plen)
37 {
38 int i, emlen = tlen - 1;
39 unsigned char *db, *seed;
40 unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH];
41
42 if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1)
43 {
44 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,
45 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
46 return 0;
47 }
48
49 if (emlen < 2 * SHA_DIGEST_LENGTH + 1)
50 {
51 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_KEY_SIZE_TOO_SMALL);
52 return 0;
53 }
54
55 dbmask = OPENSSL_malloc(emlen - SHA_DIGEST_LENGTH);
56 if (dbmask == NULL)
57 {
58 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
59 return 0;
60 }
61
62 to[0] = 0;
63 seed = to + 1;
64 db = to + SHA_DIGEST_LENGTH + 1;
65
66 EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL);
67 memset(db + SHA_DIGEST_LENGTH, 0,
68 emlen - flen - 2 * SHA_DIGEST_LENGTH - 1);
69 db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01;
70 memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, (unsigned int) flen);
71 if (RAND_bytes(seed, SHA_DIGEST_LENGTH) <= 0)
72 return 0;
73#ifdef PKCS_TESTVECT
74 memcpy(seed,
75 "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f",
76 20);
77#endif
78
79 MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH);
80 for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++)
81 db[i] ^= dbmask[i];
82
83 MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH);
84 for (i = 0; i < SHA_DIGEST_LENGTH; i++)
85 seed[i] ^= seedmask[i];
86
87 OPENSSL_free(dbmask);
88 return 1;
89 }
90
91int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
92 const unsigned char *from, int flen, int num,
93 const unsigned char *param, int plen)
94 {
95 int i, dblen, mlen = -1;
96 const unsigned char *maskeddb;
97 int lzero;
98 unsigned char *db = NULL, seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH];
99 int bad = 0;
100
101 if (--num < 2 * SHA_DIGEST_LENGTH + 1)
102 /* 'num' is the length of the modulus, i.e. does not depend on the
103 * particular ciphertext. */
104 goto decoding_err;
105
106 lzero = num - flen;
107 if (lzero < 0)
108 {
109 /* lzero == -1 */
110
111 /* signalling this error immediately after detection might allow
112 * for side-channel attacks (e.g. timing if 'plen' is huge
113 * -- cf. James H. Manger, "A Chosen Ciphertext Attack on RSA Optimal
114 * Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001),
115 * so we use a 'bad' flag */
116 bad = 1;
117 lzero = 0;
118 }
119 maskeddb = from - lzero + SHA_DIGEST_LENGTH;
120
121 dblen = num - SHA_DIGEST_LENGTH;
122 db = OPENSSL_malloc(dblen);
123 if (db == NULL)
124 {
125 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
126 return -1;
127 }
128
129 MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen);
130 for (i = lzero; i < SHA_DIGEST_LENGTH; i++)
131 seed[i] ^= from[i - lzero];
132
133 MGF1(db, dblen, seed, SHA_DIGEST_LENGTH);
134 for (i = 0; i < dblen; i++)
135 db[i] ^= maskeddb[i];
136
137 EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL);
138
139 if (memcmp(db, phash, SHA_DIGEST_LENGTH) != 0 || bad)
140 goto decoding_err;
141 else
142 {
143 for (i = SHA_DIGEST_LENGTH; i < dblen; i++)
144 if (db[i] != 0x00)
145 break;
146 if (db[i] != 0x01 || i++ >= dblen)
147 goto decoding_err;
148 else
149 {
150 /* everything looks OK */
151
152 mlen = dblen - i;
153 if (tlen < mlen)
154 {
155 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
156 mlen = -1;
157 }
158 else
159 memcpy(to, db + i, mlen);
160 }
161 }
162 OPENSSL_free(db);
163 return mlen;
164
165decoding_err:
166 /* to avoid chosen ciphertext attacks, the error message should not reveal
167 * which kind of decoding error happened */
168 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR);
169 if (db != NULL) OPENSSL_free(db);
170 return -1;
171 }
172
173int MGF1(unsigned char *mask, long len,
174 const unsigned char *seed, long seedlen)
175 {
176 long i, outlen = 0;
177 unsigned char cnt[4];
178 EVP_MD_CTX c;
179 unsigned char md[SHA_DIGEST_LENGTH];
180
181 EVP_MD_CTX_init(&c);
182 for (i = 0; outlen < len; i++)
183 {
184 cnt[0] = (unsigned char)((i >> 24) & 255);
185 cnt[1] = (unsigned char)((i >> 16) & 255);
186 cnt[2] = (unsigned char)((i >> 8)) & 255;
187 cnt[3] = (unsigned char)(i & 255);
188 EVP_DigestInit_ex(&c,EVP_sha1(), NULL);
189 EVP_DigestUpdate(&c, seed, seedlen);
190 EVP_DigestUpdate(&c, cnt, 4);
191 if (outlen + SHA_DIGEST_LENGTH <= len)
192 {
193 EVP_DigestFinal_ex(&c, mask + outlen, NULL);
194 outlen += SHA_DIGEST_LENGTH;
195 }
196 else
197 {
198 EVP_DigestFinal_ex(&c, md, NULL);
199 memcpy(mask + outlen, md, len - outlen);
200 outlen = len;
201 }
202 }
203 EVP_MD_CTX_cleanup(&c);
204 return 0;
205 }
206#endif
diff --git a/src/lib/libcrypto/rsa/rsa_pk1.c b/src/lib/libcrypto/rsa/rsa_pk1.c
deleted file mode 100644
index c1edd6764f..0000000000
--- a/src/lib/libcrypto/rsa/rsa_pk1.c
+++ /dev/null
@@ -1,224 +0,0 @@
1/* crypto/rsa/rsa_pk1.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 "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/rsa.h>
63#include <openssl/rand.h>
64
65int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen,
66 const unsigned char *from, int flen)
67 {
68 int j;
69 unsigned char *p;
70
71 if (flen > (tlen-11))
72 {
73 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
74 return(0);
75 }
76
77 p=(unsigned char *)to;
78
79 *(p++)=0;
80 *(p++)=1; /* Private Key BT (Block Type) */
81
82 /* pad out with 0xff data */
83 j=tlen-3-flen;
84 memset(p,0xff,j);
85 p+=j;
86 *(p++)='\0';
87 memcpy(p,from,(unsigned int)flen);
88 return(1);
89 }
90
91int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen,
92 const unsigned char *from, int flen, int num)
93 {
94 int i,j;
95 const unsigned char *p;
96
97 p=from;
98 if ((num != (flen+1)) || (*(p++) != 01))
99 {
100 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BLOCK_TYPE_IS_NOT_01);
101 return(-1);
102 }
103
104 /* scan over padding data */
105 j=flen-1; /* one for type. */
106 for (i=0; i<j; i++)
107 {
108 if (*p != 0xff) /* should decrypt to 0xff */
109 {
110 if (*p == 0)
111 { p++; break; }
112 else {
113 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BAD_FIXED_HEADER_DECRYPT);
114 return(-1);
115 }
116 }
117 p++;
118 }
119
120 if (i == j)
121 {
122 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_NULL_BEFORE_BLOCK_MISSING);
123 return(-1);
124 }
125
126 if (i < 8)
127 {
128 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BAD_PAD_BYTE_COUNT);
129 return(-1);
130 }
131 i++; /* Skip over the '\0' */
132 j-=i;
133 if (j > tlen)
134 {
135 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_DATA_TOO_LARGE);
136 return(-1);
137 }
138 memcpy(to,p,(unsigned int)j);
139
140 return(j);
141 }
142
143int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen,
144 const unsigned char *from, int flen)
145 {
146 int i,j;
147 unsigned char *p;
148
149 if (flen > (tlen-11))
150 {
151 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
152 return(0);
153 }
154
155 p=(unsigned char *)to;
156
157 *(p++)=0;
158 *(p++)=2; /* Public Key BT (Block Type) */
159
160 /* pad out with non-zero random data */
161 j=tlen-3-flen;
162
163 if (RAND_bytes(p,j) <= 0)
164 return(0);
165 for (i=0; i<j; i++)
166 {
167 if (*p == '\0')
168 do {
169 if (RAND_bytes(p,1) <= 0)
170 return(0);
171 } while (*p == '\0');
172 p++;
173 }
174
175 *(p++)='\0';
176
177 memcpy(p,from,(unsigned int)flen);
178 return(1);
179 }
180
181int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen,
182 const unsigned char *from, int flen, int num)
183 {
184 int i,j;
185 const unsigned char *p;
186
187 p=from;
188 if ((num != (flen+1)) || (*(p++) != 02))
189 {
190 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_BLOCK_TYPE_IS_NOT_02);
191 return(-1);
192 }
193#ifdef PKCS1_CHECK
194 return(num-11);
195#endif
196
197 /* scan over padding data */
198 j=flen-1; /* one for type. */
199 for (i=0; i<j; i++)
200 if (*(p++) == 0) break;
201
202 if (i == j)
203 {
204 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_NULL_BEFORE_BLOCK_MISSING);
205 return(-1);
206 }
207
208 if (i < 8)
209 {
210 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_BAD_PAD_BYTE_COUNT);
211 return(-1);
212 }
213 i++; /* Skip over the '\0' */
214 j-=i;
215 if (j > tlen)
216 {
217 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_DATA_TOO_LARGE);
218 return(-1);
219 }
220 memcpy(to,p,(unsigned int)j);
221
222 return(j);
223 }
224
diff --git a/src/lib/libcrypto/rsa/rsa_saos.c b/src/lib/libcrypto/rsa/rsa_saos.c
deleted file mode 100644
index 85adacc08f..0000000000
--- a/src/lib/libcrypto/rsa/rsa_saos.c
+++ /dev/null
@@ -1,146 +0,0 @@
1/* crypto/rsa/rsa_saos.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 "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/rsa.h>
63#include <openssl/objects.h>
64#include <openssl/x509.h>
65
66int RSA_sign_ASN1_OCTET_STRING(int type,
67 const unsigned char *m, unsigned int m_len,
68 unsigned char *sigret, unsigned int *siglen, RSA *rsa)
69 {
70 ASN1_OCTET_STRING sig;
71 int i,j,ret=1;
72 unsigned char *p,*s;
73
74 sig.type=V_ASN1_OCTET_STRING;
75 sig.length=m_len;
76 sig.data=(unsigned char *)m;
77
78 i=i2d_ASN1_OCTET_STRING(&sig,NULL);
79 j=RSA_size(rsa);
80 if ((i-RSA_PKCS1_PADDING) > j)
81 {
82 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
83 return(0);
84 }
85 s=(unsigned char *)OPENSSL_malloc((unsigned int)j+1);
86 if (s == NULL)
87 {
88 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE);
89 return(0);
90 }
91 p=s;
92 i2d_ASN1_OCTET_STRING(&sig,&p);
93 i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING);
94 if (i <= 0)
95 ret=0;
96 else
97 *siglen=i;
98
99 memset(s,0,(unsigned int)j+1);
100 OPENSSL_free(s);
101 return(ret);
102 }
103
104int RSA_verify_ASN1_OCTET_STRING(int dtype,
105 const unsigned char *m,
106 unsigned int m_len, unsigned char *sigbuf, unsigned int siglen,
107 RSA *rsa)
108 {
109 int i,ret=0;
110 unsigned char *p,*s;
111 ASN1_OCTET_STRING *sig=NULL;
112
113 if (siglen != (unsigned int)RSA_size(rsa))
114 {
115 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_WRONG_SIGNATURE_LENGTH);
116 return(0);
117 }
118
119 s=(unsigned char *)OPENSSL_malloc((unsigned int)siglen);
120 if (s == NULL)
121 {
122 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE);
123 goto err;
124 }
125 i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);
126
127 if (i <= 0) goto err;
128
129 p=s;
130 sig=d2i_ASN1_OCTET_STRING(NULL,&p,(long)i);
131 if (sig == NULL) goto err;
132
133 if ( ((unsigned int)sig->length != m_len) ||
134 (memcmp(m,sig->data,m_len) != 0))
135 {
136 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_BAD_SIGNATURE);
137 }
138 else
139 ret=1;
140err:
141 if (sig != NULL) M_ASN1_OCTET_STRING_free(sig);
142 memset(s,0,(unsigned int)siglen);
143 OPENSSL_free(s);
144 return(ret);
145 }
146
diff --git a/src/lib/libcrypto/rsa/rsa_sign.c b/src/lib/libcrypto/rsa/rsa_sign.c
deleted file mode 100644
index 2a440901de..0000000000
--- a/src/lib/libcrypto/rsa/rsa_sign.c
+++ /dev/null
@@ -1,228 +0,0 @@
1/* crypto/rsa/rsa_sign.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 "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/rsa.h>
63#include <openssl/objects.h>
64#include <openssl/x509.h>
65#include <openssl/engine.h>
66
67/* Size of an SSL signature: MD5+SHA1 */
68#define SSL_SIG_LENGTH 36
69
70int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
71 unsigned char *sigret, unsigned int *siglen, RSA *rsa)
72 {
73 X509_SIG sig;
74 ASN1_TYPE parameter;
75 int i,j,ret=1;
76 unsigned char *p, *tmps = NULL;
77 const unsigned char *s = NULL;
78 X509_ALGOR algor;
79 ASN1_OCTET_STRING digest;
80 if((rsa->flags & RSA_FLAG_SIGN_VER)
81 && ENGINE_get_RSA(rsa->engine)->rsa_sign)
82 return ENGINE_get_RSA(rsa->engine)->rsa_sign(type,
83 m, m_len, sigret, siglen, rsa);
84 /* Special case: SSL signature, just check the length */
85 if(type == NID_md5_sha1) {
86 if(m_len != SSL_SIG_LENGTH) {
87 RSAerr(RSA_F_RSA_SIGN,RSA_R_INVALID_MESSAGE_LENGTH);
88 return(0);
89 }
90 i = SSL_SIG_LENGTH;
91 s = m;
92 } else {
93 sig.algor= &algor;
94 sig.algor->algorithm=OBJ_nid2obj(type);
95 if (sig.algor->algorithm == NULL)
96 {
97 RSAerr(RSA_F_RSA_SIGN,RSA_R_UNKNOWN_ALGORITHM_TYPE);
98 return(0);
99 }
100 if (sig.algor->algorithm->length == 0)
101 {
102 RSAerr(RSA_F_RSA_SIGN,RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
103 return(0);
104 }
105 parameter.type=V_ASN1_NULL;
106 parameter.value.ptr=NULL;
107 sig.algor->parameter= &parameter;
108
109 sig.digest= &digest;
110 sig.digest->data=(unsigned char *)m; /* TMP UGLY CAST */
111 sig.digest->length=m_len;
112
113 i=i2d_X509_SIG(&sig,NULL);
114 }
115 j=RSA_size(rsa);
116 if ((i-RSA_PKCS1_PADDING) > j)
117 {
118 RSAerr(RSA_F_RSA_SIGN,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
119 return(0);
120 }
121 if(type != NID_md5_sha1) {
122 tmps=(unsigned char *)OPENSSL_malloc((unsigned int)j+1);
123 if (tmps == NULL)
124 {
125 RSAerr(RSA_F_RSA_SIGN,ERR_R_MALLOC_FAILURE);
126 return(0);
127 }
128 p=tmps;
129 i2d_X509_SIG(&sig,&p);
130 s=tmps;
131 }
132 i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING);
133 if (i <= 0)
134 ret=0;
135 else
136 *siglen=i;
137
138 if(type != NID_md5_sha1) {
139 memset(tmps,0,(unsigned int)j+1);
140 OPENSSL_free(tmps);
141 }
142 return(ret);
143 }
144
145int RSA_verify(int dtype, const unsigned char *m, unsigned int m_len,
146 unsigned char *sigbuf, unsigned int siglen, RSA *rsa)
147 {
148 int i,ret=0,sigtype;
149 unsigned char *p,*s;
150 X509_SIG *sig=NULL;
151
152 if (siglen != (unsigned int)RSA_size(rsa))
153 {
154 RSAerr(RSA_F_RSA_VERIFY,RSA_R_WRONG_SIGNATURE_LENGTH);
155 return(0);
156 }
157
158 if((rsa->flags & RSA_FLAG_SIGN_VER)
159 && ENGINE_get_RSA(rsa->engine)->rsa_verify)
160 return ENGINE_get_RSA(rsa->engine)->rsa_verify(dtype,
161 m, m_len, sigbuf, siglen, rsa);
162
163 s=(unsigned char *)OPENSSL_malloc((unsigned int)siglen);
164 if (s == NULL)
165 {
166 RSAerr(RSA_F_RSA_VERIFY,ERR_R_MALLOC_FAILURE);
167 goto err;
168 }
169 if((dtype == NID_md5_sha1) && (m_len != SSL_SIG_LENGTH) ) {
170 RSAerr(RSA_F_RSA_VERIFY,RSA_R_INVALID_MESSAGE_LENGTH);
171 return(0);
172 }
173 i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);
174
175 if (i <= 0) goto err;
176
177 /* Special case: SSL signature */
178 if(dtype == NID_md5_sha1) {
179 if((i != SSL_SIG_LENGTH) || memcmp(s, m, SSL_SIG_LENGTH))
180 RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
181 else ret = 1;
182 } else {
183 p=s;
184 sig=d2i_X509_SIG(NULL,&p,(long)i);
185
186 if (sig == NULL) goto err;
187 sigtype=OBJ_obj2nid(sig->algor->algorithm);
188
189
190 #ifdef RSA_DEBUG
191 /* put a backward compatibility flag in EAY */
192 fprintf(stderr,"in(%s) expect(%s)\n",OBJ_nid2ln(sigtype),
193 OBJ_nid2ln(dtype));
194 #endif
195 if (sigtype != dtype)
196 {
197 if (((dtype == NID_md5) &&
198 (sigtype == NID_md5WithRSAEncryption)) ||
199 ((dtype == NID_md2) &&
200 (sigtype == NID_md2WithRSAEncryption)))
201 {
202 /* ok, we will let it through */
203#if !defined(OPENSSL_NO_STDIO) && !defined(OPENSSL_SYS_WIN16)
204 fprintf(stderr,"signature has problems, re-make with post SSLeay045\n");
205#endif
206 }
207 else
208 {
209 RSAerr(RSA_F_RSA_VERIFY,
210 RSA_R_ALGORITHM_MISMATCH);
211 goto err;
212 }
213 }
214 if ( ((unsigned int)sig->digest->length != m_len) ||
215 (memcmp(m,sig->digest->data,m_len) != 0))
216 {
217 RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
218 }
219 else
220 ret=1;
221 }
222err:
223 if (sig != NULL) X509_SIG_free(sig);
224 memset(s,0,(unsigned int)siglen);
225 OPENSSL_free(s);
226 return(ret);
227 }
228
diff --git a/src/lib/libcrypto/rsa/rsa_ssl.c b/src/lib/libcrypto/rsa/rsa_ssl.c
deleted file mode 100644
index ea72629494..0000000000
--- a/src/lib/libcrypto/rsa/rsa_ssl.c
+++ /dev/null
@@ -1,154 +0,0 @@
1/* crypto/rsa/rsa_ssl.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 "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/rsa.h>
63#include <openssl/rand.h>
64
65int RSA_padding_add_SSLv23(unsigned char *to, int tlen,
66 const unsigned char *from, int flen)
67 {
68 int i,j;
69 unsigned char *p;
70
71 if (flen > (tlen-11))
72 {
73 RSAerr(RSA_F_RSA_PADDING_ADD_SSLV23,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
74 return(0);
75 }
76
77 p=(unsigned char *)to;
78
79 *(p++)=0;
80 *(p++)=2; /* Public Key BT (Block Type) */
81
82 /* pad out with non-zero random data */
83 j=tlen-3-8-flen;
84
85 if (RAND_bytes(p,j) <= 0)
86 return(0);
87 for (i=0; i<j; i++)
88 {
89 if (*p == '\0')
90 do {
91 if (RAND_bytes(p,1) <= 0)
92 return(0);
93 } while (*p == '\0');
94 p++;
95 }
96
97 memset(p,3,8);
98 p+=8;
99 *(p++)='\0';
100
101 memcpy(p,from,(unsigned int)flen);
102 return(1);
103 }
104
105int RSA_padding_check_SSLv23(unsigned char *to, int tlen,
106 const unsigned char *from, int flen, int num)
107 {
108 int i,j,k;
109 const unsigned char *p;
110
111 p=from;
112 if (flen < 10)
113 {
114 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_DATA_TOO_SMALL);
115 return(-1);
116 }
117 if ((num != (flen+1)) || (*(p++) != 02))
118 {
119 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_BLOCK_TYPE_IS_NOT_02);
120 return(-1);
121 }
122
123 /* scan over padding data */
124 j=flen-1; /* one for type */
125 for (i=0; i<j; i++)
126 if (*(p++) == 0) break;
127
128 if ((i == j) || (i < 8))
129 {
130 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_NULL_BEFORE_BLOCK_MISSING);
131 return(-1);
132 }
133 for (k= -8; k<0; k++)
134 {
135 if (p[k] != 0x03) break;
136 }
137 if (k == -1)
138 {
139 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_SSLV3_ROLLBACK_ATTACK);
140 return(-1);
141 }
142
143 i++; /* Skip over the '\0' */
144 j-=i;
145 if (j > tlen)
146 {
147 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_DATA_TOO_LARGE);
148 return(-1);
149 }
150 memcpy(to,p,(unsigned int)j);
151
152 return(j);
153 }
154