summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rsa
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/rsa')
-rw-r--r--src/lib/libcrypto/rsa/rsa.h338
-rw-r--r--src/lib/libcrypto/rsa/rsa_chk.c184
-rw-r--r--src/lib/libcrypto/rsa/rsa_eay.c491
-rw-r--r--src/lib/libcrypto/rsa/rsa_err.c148
-rw-r--r--src/lib/libcrypto/rsa/rsa_gen.c197
-rw-r--r--src/lib/libcrypto/rsa/rsa_lib.c333
-rw-r--r--src/lib/libcrypto/rsa/rsa_none.c98
-rw-r--r--src/lib/libcrypto/rsa/rsa_oaep.c163
-rw-r--r--src/lib/libcrypto/rsa/rsa_pk1.c224
-rw-r--r--src/lib/libcrypto/rsa/rsa_saos.c144
-rw-r--r--src/lib/libcrypto/rsa/rsa_sign.c221
-rw-r--r--src/lib/libcrypto/rsa/rsa_ssl.c154
12 files changed, 0 insertions, 2695 deletions
diff --git a/src/lib/libcrypto/rsa/rsa.h b/src/lib/libcrypto/rsa/rsa.h
deleted file mode 100644
index f9f9b5cfe9..0000000000
--- a/src/lib/libcrypto/rsa/rsa.h
+++ /dev/null
@@ -1,338 +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#ifdef __cplusplus
63extern "C" {
64#endif
65
66#include <openssl/bn.h>
67#include <openssl/crypto.h>
68
69#ifdef NO_RSA
70#error RSA is disabled.
71#endif
72
73typedef struct rsa_st RSA;
74
75typedef struct rsa_meth_st
76 {
77 const char *name;
78 int (*rsa_pub_enc)(int flen,unsigned char *from,unsigned char *to,
79 RSA *rsa,int padding);
80 int (*rsa_pub_dec)(int flen,unsigned char *from,unsigned char *to,
81 RSA *rsa,int padding);
82 int (*rsa_priv_enc)(int flen,unsigned char *from,unsigned char *to,
83 RSA *rsa,int padding);
84 int (*rsa_priv_dec)(int flen,unsigned char *from,unsigned char *to,
85 RSA *rsa,int padding);
86 int (*rsa_mod_exp)(BIGNUM *r0,BIGNUM *I,RSA *rsa); /* Can be null */
87 int (*bn_mod_exp)(BIGNUM *r, BIGNUM *a, const BIGNUM *p,
88 const BIGNUM *m, BN_CTX *ctx,
89 BN_MONT_CTX *m_ctx); /* Can be null */
90 int (*init)(RSA *rsa); /* called at new */
91 int (*finish)(RSA *rsa); /* called at free */
92 int flags; /* RSA_METHOD_FLAG_* things */
93 char *app_data; /* may be needed! */
94/* New sign and verify functions: some libraries don't allow arbitrary data
95 * to be signed/verified: this allows them to be used. Note: for this to work
96 * the RSA_public_decrypt() and RSA_private_encrypt() should *NOT* be used
97 * RSA_sign(), RSA_verify() should be used instead. Note: for backwards
98 * compatibility this functionality is only enabled if the RSA_FLAG_SIGN_VER
99 * option is set in 'flags'.
100 */
101 int (*rsa_sign)(int type, unsigned char *m, unsigned int m_len,
102 unsigned char *sigret, unsigned int *siglen, RSA *rsa);
103 int (*rsa_verify)(int dtype, unsigned char *m, unsigned int m_len,
104 unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
105
106 } RSA_METHOD;
107
108struct rsa_st
109 {
110 /* The first parameter is used to pickup errors where
111 * this is passed instead of aEVP_PKEY, it is set to 0 */
112 int pad;
113 int version;
114 RSA_METHOD *meth;
115 BIGNUM *n;
116 BIGNUM *e;
117 BIGNUM *d;
118 BIGNUM *p;
119 BIGNUM *q;
120 BIGNUM *dmp1;
121 BIGNUM *dmq1;
122 BIGNUM *iqmp;
123 /* be careful using this if the RSA structure is shared */
124 CRYPTO_EX_DATA ex_data;
125 int references;
126 int flags;
127
128 /* Used to cache montgomery values */
129 BN_MONT_CTX *_method_mod_n;
130 BN_MONT_CTX *_method_mod_p;
131 BN_MONT_CTX *_method_mod_q;
132
133 /* all BIGNUM values are actually in the following data, if it is not
134 * NULL */
135 char *bignum_data;
136 BN_BLINDING *blinding;
137 };
138
139#define RSA_3 0x3L
140#define RSA_F4 0x10001L
141
142#define RSA_METHOD_FLAG_NO_CHECK 0x01 /* don't check pub/private match */
143
144#define RSA_FLAG_CACHE_PUBLIC 0x02
145#define RSA_FLAG_CACHE_PRIVATE 0x04
146#define RSA_FLAG_BLINDING 0x08
147#define RSA_FLAG_THREAD_SAFE 0x10
148/* This flag means the private key operations will be handled by rsa_mod_exp
149 * and that they do not depend on the private key components being present:
150 * for example a key stored in external hardware. Without this flag bn_mod_exp
151 * gets called when private key components are absent.
152 */
153#define RSA_FLAG_EXT_PKEY 0x20
154
155/* This flag in the RSA_METHOD enables the new rsa_sign, rsa_verify functions.
156 */
157#define RSA_FLAG_SIGN_VER 0x40
158
159#define RSA_PKCS1_PADDING 1
160#define RSA_SSLV23_PADDING 2
161#define RSA_NO_PADDING 3
162#define RSA_PKCS1_OAEP_PADDING 4
163
164#define RSA_set_app_data(s,arg) RSA_set_ex_data(s,0,arg)
165#define RSA_get_app_data(s) RSA_get_ex_data(s,0)
166
167RSA * RSA_new(void);
168RSA * RSA_new_method(RSA_METHOD *method);
169int RSA_size(RSA *);
170RSA * RSA_generate_key(int bits, unsigned long e,void
171 (*callback)(int,int,void *),void *cb_arg);
172int RSA_check_key(RSA *);
173 /* next 4 return -1 on error */
174int RSA_public_encrypt(int flen, unsigned char *from,
175 unsigned char *to, RSA *rsa,int padding);
176int RSA_private_encrypt(int flen, unsigned char *from,
177 unsigned char *to, RSA *rsa,int padding);
178int RSA_public_decrypt(int flen, unsigned char *from,
179 unsigned char *to, RSA *rsa,int padding);
180int RSA_private_decrypt(int flen, unsigned char *from,
181 unsigned char *to, RSA *rsa,int padding);
182void RSA_free (RSA *r);
183
184int RSA_flags(RSA *r);
185
186void RSA_set_default_method(RSA_METHOD *meth);
187RSA_METHOD *RSA_get_default_method(void);
188RSA_METHOD *RSA_get_method(RSA *rsa);
189RSA_METHOD *RSA_set_method(RSA *rsa, RSA_METHOD *meth);
190
191/* This function needs the memory locking malloc callbacks to be installed */
192int RSA_memory_lock(RSA *r);
193
194/* If you have RSAref compiled in. */
195RSA_METHOD *RSA_PKCS1_RSAref(void);
196
197/* these are the actual SSLeay RSA functions */
198RSA_METHOD *RSA_PKCS1_SSLeay(void);
199
200RSA_METHOD *RSA_null_method(void);
201
202void ERR_load_RSA_strings(void );
203
204RSA * d2i_RSAPublicKey(RSA **a, unsigned char **pp, long length);
205int i2d_RSAPublicKey(RSA *a, unsigned char **pp);
206RSA * d2i_RSAPrivateKey(RSA **a, unsigned char **pp, long length);
207int i2d_RSAPrivateKey(RSA *a, unsigned char **pp);
208#ifndef NO_FP_API
209int RSA_print_fp(FILE *fp, RSA *r,int offset);
210#endif
211
212#ifdef HEADER_BIO_H
213int RSA_print(BIO *bp, RSA *r,int offset);
214#endif
215
216int i2d_Netscape_RSA(RSA *a, unsigned char **pp, int (*cb)());
217RSA *d2i_Netscape_RSA(RSA **a, unsigned char **pp, long length, int (*cb)());
218/* Naughty internal function required elsewhere, to handle a MS structure
219 * that is the same as the netscape one :-) */
220RSA *d2i_Netscape_RSA_2(RSA **a, unsigned char **pp, long length, int (*cb)());
221
222/* The following 2 functions sign and verify a X509_SIG ASN1 object
223 * inside PKCS#1 padded RSA encryption */
224int RSA_sign(int type, unsigned char *m, unsigned int m_len,
225 unsigned char *sigret, unsigned int *siglen, RSA *rsa);
226int RSA_verify(int type, unsigned char *m, unsigned int m_len,
227 unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
228
229/* The following 2 function sign and verify a ASN1_OCTET_STRING
230 * object inside PKCS#1 padded RSA encryption */
231int RSA_sign_ASN1_OCTET_STRING(int type, unsigned char *m, unsigned int m_len,
232 unsigned char *sigret, unsigned int *siglen, RSA *rsa);
233int RSA_verify_ASN1_OCTET_STRING(int type, unsigned char *m, unsigned int m_len,
234 unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
235
236int RSA_blinding_on(RSA *rsa, BN_CTX *ctx);
237void RSA_blinding_off(RSA *rsa);
238
239int RSA_padding_add_PKCS1_type_1(unsigned char *to,int tlen,
240 unsigned char *f,int fl);
241int RSA_padding_check_PKCS1_type_1(unsigned char *to,int tlen,
242 unsigned char *f,int fl,int rsa_len);
243int RSA_padding_add_PKCS1_type_2(unsigned char *to,int tlen,
244 unsigned char *f,int fl);
245int RSA_padding_check_PKCS1_type_2(unsigned char *to,int tlen,
246 unsigned char *f,int fl,int rsa_len);
247int RSA_padding_add_PKCS1_OAEP(unsigned char *to,int tlen,
248 unsigned char *f,int fl,unsigned char *p,
249 int pl);
250int RSA_padding_check_PKCS1_OAEP(unsigned char *to,int tlen,
251 unsigned char *f,int fl,int rsa_len,
252 unsigned char *p,int pl);
253int RSA_padding_add_SSLv23(unsigned char *to,int tlen,
254 unsigned char *f,int fl);
255int RSA_padding_check_SSLv23(unsigned char *to,int tlen,
256 unsigned char *f,int fl,int rsa_len);
257int RSA_padding_add_none(unsigned char *to,int tlen,
258 unsigned char *f,int fl);
259int RSA_padding_check_none(unsigned char *to,int tlen,
260 unsigned char *f,int fl,int rsa_len);
261
262int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
263 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
264int RSA_set_ex_data(RSA *r,int idx,void *arg);
265void *RSA_get_ex_data(RSA *r, int idx);
266
267/* BEGIN ERROR CODES */
268/* The following lines are auto generated by the script mkerr.pl. Any changes
269 * made after this point may be overwritten when the script is next run.
270 */
271
272/* Error codes for the RSA functions. */
273
274/* Function codes. */
275#define RSA_F_MEMORY_LOCK 100
276#define RSA_F_RSA_CHECK_KEY 123
277#define RSA_F_RSA_EAY_PRIVATE_DECRYPT 101
278#define RSA_F_RSA_EAY_PRIVATE_ENCRYPT 102
279#define RSA_F_RSA_EAY_PUBLIC_DECRYPT 103
280#define RSA_F_RSA_EAY_PUBLIC_ENCRYPT 104
281#define RSA_F_RSA_GENERATE_KEY 105
282#define RSA_F_RSA_NEW_METHOD 106
283#define RSA_F_RSA_NULL 124
284#define RSA_F_RSA_PADDING_ADD_NONE 107
285#define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP 121
286#define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1 108
287#define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2 109
288#define RSA_F_RSA_PADDING_ADD_SSLV23 110
289#define RSA_F_RSA_PADDING_CHECK_NONE 111
290#define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP 122
291#define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1 112
292#define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2 113
293#define RSA_F_RSA_PADDING_CHECK_SSLV23 114
294#define RSA_F_RSA_PRINT 115
295#define RSA_F_RSA_PRINT_FP 116
296#define RSA_F_RSA_SIGN 117
297#define RSA_F_RSA_SIGN_ASN1_OCTET_STRING 118
298#define RSA_F_RSA_VERIFY 119
299#define RSA_F_RSA_VERIFY_ASN1_OCTET_STRING 120
300
301/* Reason codes. */
302#define RSA_R_ALGORITHM_MISMATCH 100
303#define RSA_R_BAD_E_VALUE 101
304#define RSA_R_BAD_FIXED_HEADER_DECRYPT 102
305#define RSA_R_BAD_PAD_BYTE_COUNT 103
306#define RSA_R_BAD_SIGNATURE 104
307#define RSA_R_BLOCK_TYPE_IS_NOT_01 106
308#define RSA_R_BLOCK_TYPE_IS_NOT_02 107
309#define RSA_R_DATA_GREATER_THAN_MOD_LEN 108
310#define RSA_R_DATA_TOO_LARGE 109
311#define RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 110
312#define RSA_R_DATA_TOO_SMALL 111
313#define RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE 122
314#define RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY 112
315#define RSA_R_DMP1_NOT_CONGRUENT_TO_D 124
316#define RSA_R_DMQ1_NOT_CONGRUENT_TO_D 125
317#define RSA_R_D_E_NOT_CONGRUENT_TO_1 123
318#define RSA_R_INVALID_MESSAGE_LENGTH 131
319#define RSA_R_IQMP_NOT_INVERSE_OF_Q 126
320#define RSA_R_KEY_SIZE_TOO_SMALL 120
321#define RSA_R_NULL_BEFORE_BLOCK_MISSING 113
322#define RSA_R_N_DOES_NOT_EQUAL_P_Q 127
323#define RSA_R_OAEP_DECODING_ERROR 121
324#define RSA_R_PADDING_CHECK_FAILED 114
325#define RSA_R_P_NOT_PRIME 128
326#define RSA_R_Q_NOT_PRIME 129
327#define RSA_R_RSA_OPERATIONS_NOT_SUPPORTED 130
328#define RSA_R_SSLV3_ROLLBACK_ATTACK 115
329#define RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 116
330#define RSA_R_UNKNOWN_ALGORITHM_TYPE 117
331#define RSA_R_UNKNOWN_PADDING_TYPE 118
332#define RSA_R_WRONG_SIGNATURE_LENGTH 119
333
334#ifdef __cplusplus
335}
336#endif
337#endif
338
diff --git a/src/lib/libcrypto/rsa/rsa_chk.c b/src/lib/libcrypto/rsa/rsa_chk.c
deleted file mode 100644
index 91b9115798..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(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 b7d2460754..0000000000
--- a/src/lib/libcrypto/rsa/rsa_eay.c
+++ /dev/null
@@ -1,491 +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
65#ifndef RSA_NULL
66
67static int RSA_eay_public_encrypt(int flen, unsigned char *from,
68 unsigned char *to, RSA *rsa,int padding);
69static int RSA_eay_private_encrypt(int flen, unsigned char *from,
70 unsigned char *to, RSA *rsa,int padding);
71static int RSA_eay_public_decrypt(int flen, unsigned char *from,
72 unsigned char *to, RSA *rsa,int padding);
73static int RSA_eay_private_decrypt(int flen, unsigned char *from,
74 unsigned char *to, RSA *rsa,int padding);
75static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *i, RSA *rsa);
76static int RSA_eay_init(RSA *rsa);
77static int RSA_eay_finish(RSA *rsa);
78static RSA_METHOD rsa_pkcs1_eay_meth={
79 "Eric Young's PKCS#1 RSA",
80 RSA_eay_public_encrypt,
81 RSA_eay_public_decrypt,
82 RSA_eay_private_encrypt,
83 RSA_eay_private_decrypt,
84 RSA_eay_mod_exp,
85 BN_mod_exp_mont,
86 RSA_eay_init,
87 RSA_eay_finish,
88 0,
89 NULL,
90 };
91
92RSA_METHOD *RSA_PKCS1_SSLeay(void)
93 {
94 return(&rsa_pkcs1_eay_meth);
95 }
96
97static int RSA_eay_public_encrypt(int flen, unsigned char *from,
98 unsigned char *to, RSA *rsa, int padding)
99 {
100 BIGNUM f,ret;
101 int i,j,k,num=0,r= -1;
102 unsigned char *buf=NULL;
103 BN_CTX *ctx=NULL;
104
105 BN_init(&f);
106 BN_init(&ret);
107 if ((ctx=BN_CTX_new()) == NULL) goto err;
108 num=BN_num_bytes(rsa->n);
109 if ((buf=(unsigned char *)Malloc(num)) == NULL)
110 {
111 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
112 goto err;
113 }
114
115 switch (padding)
116 {
117 case RSA_PKCS1_PADDING:
118 i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
119 break;
120#ifndef NO_SHA
121 case RSA_PKCS1_OAEP_PADDING:
122 i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);
123 break;
124#endif
125 case RSA_SSLV23_PADDING:
126 i=RSA_padding_add_SSLv23(buf,num,from,flen);
127 break;
128 case RSA_NO_PADDING:
129 i=RSA_padding_add_none(buf,num,from,flen);
130 break;
131 default:
132 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
133 goto err;
134 }
135 if (i <= 0) goto err;
136
137 if (BN_bin2bn(buf,num,&f) == NULL) goto err;
138
139 if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC))
140 {
141 if ((rsa->_method_mod_n=BN_MONT_CTX_new()) != NULL)
142 if (!BN_MONT_CTX_set(rsa->_method_mod_n,rsa->n,ctx))
143 goto err;
144 }
145
146 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx,
147 rsa->_method_mod_n)) goto err;
148
149 /* put in leading 0 bytes if the number is less than the
150 * length of the modulus */
151 j=BN_num_bytes(&ret);
152 i=BN_bn2bin(&ret,&(to[num-j]));
153 for (k=0; k<(num-i); k++)
154 to[k]=0;
155
156 r=num;
157err:
158 if (ctx != NULL) BN_CTX_free(ctx);
159 BN_clear_free(&f);
160 BN_clear_free(&ret);
161 if (buf != NULL)
162 {
163 memset(buf,0,num);
164 Free(buf);
165 }
166 return(r);
167 }
168
169static int RSA_eay_private_encrypt(int flen, unsigned char *from,
170 unsigned char *to, RSA *rsa, int padding)
171 {
172 BIGNUM f,ret;
173 int i,j,k,num=0,r= -1;
174 unsigned char *buf=NULL;
175 BN_CTX *ctx=NULL;
176
177 BN_init(&f);
178 BN_init(&ret);
179
180 if ((ctx=BN_CTX_new()) == NULL) goto err;
181 num=BN_num_bytes(rsa->n);
182 if ((buf=(unsigned char *)Malloc(num)) == NULL)
183 {
184 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
185 goto err;
186 }
187
188 switch (padding)
189 {
190 case RSA_PKCS1_PADDING:
191 i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);
192 break;
193 case RSA_NO_PADDING:
194 i=RSA_padding_add_none(buf,num,from,flen);
195 break;
196 case RSA_SSLV23_PADDING:
197 default:
198 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
199 goto err;
200 }
201 if (i <= 0) goto err;
202
203 if (BN_bin2bn(buf,num,&f) == NULL) goto err;
204
205 if ((rsa->flags & RSA_FLAG_BLINDING) && (rsa->blinding == NULL))
206 RSA_blinding_on(rsa,ctx);
207 if (rsa->flags & RSA_FLAG_BLINDING)
208 if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
209
210 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
211 ((rsa->p != NULL) &&
212 (rsa->q != NULL) &&
213 (rsa->dmp1 != NULL) &&
214 (rsa->dmq1 != NULL) &&
215 (rsa->iqmp != NULL)) )
216 { if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; }
217 else
218 {
219 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) goto err;
220 }
221
222 if (rsa->flags & RSA_FLAG_BLINDING)
223 if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err;
224
225 /* put in leading 0 bytes if the number is less than the
226 * length of the modulus */
227 j=BN_num_bytes(&ret);
228 i=BN_bn2bin(&ret,&(to[num-j]));
229 for (k=0; k<(num-i); k++)
230 to[k]=0;
231
232 r=num;
233err:
234 if (ctx != NULL) BN_CTX_free(ctx);
235 BN_clear_free(&ret);
236 BN_clear_free(&f);
237 if (buf != NULL)
238 {
239 memset(buf,0,num);
240 Free(buf);
241 }
242 return(r);
243 }
244
245static int RSA_eay_private_decrypt(int flen, unsigned char *from,
246 unsigned char *to, RSA *rsa, int padding)
247 {
248 BIGNUM f,ret;
249 int j,num=0,r= -1;
250 unsigned char *p;
251 unsigned char *buf=NULL;
252 BN_CTX *ctx=NULL;
253
254 BN_init(&f);
255 BN_init(&ret);
256 ctx=BN_CTX_new();
257 if (ctx == NULL) goto err;
258
259 num=BN_num_bytes(rsa->n);
260
261 if ((buf=(unsigned char *)Malloc(num)) == NULL)
262 {
263 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
264 goto err;
265 }
266
267 /* This check was for equality but PGP does evil things
268 * and chops off the top '0' bytes */
269 if (flen > num)
270 {
271 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
272 goto err;
273 }
274
275 /* make data into a big number */
276 if (BN_bin2bn(from,(int)flen,&f) == NULL) goto err;
277
278 if ((rsa->flags & RSA_FLAG_BLINDING) && (rsa->blinding == NULL))
279 RSA_blinding_on(rsa,ctx);
280 if (rsa->flags & RSA_FLAG_BLINDING)
281 if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
282
283 /* do the decrypt */
284 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
285 ((rsa->p != NULL) &&
286 (rsa->q != NULL) &&
287 (rsa->dmp1 != NULL) &&
288 (rsa->dmq1 != NULL) &&
289 (rsa->iqmp != NULL)) )
290 { if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; }
291 else
292 {
293 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL))
294 goto err;
295 }
296
297 if (rsa->flags & RSA_FLAG_BLINDING)
298 if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err;
299
300 p=buf;
301 j=BN_bn2bin(&ret,p); /* j is only used with no-padding mode */
302
303 switch (padding)
304 {
305 case RSA_PKCS1_PADDING:
306 r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);
307 break;
308#ifndef NO_SHA
309 case RSA_PKCS1_OAEP_PADDING:
310 r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0);
311 break;
312#endif
313 case RSA_SSLV23_PADDING:
314 r=RSA_padding_check_SSLv23(to,num,buf,j,num);
315 break;
316 case RSA_NO_PADDING:
317 r=RSA_padding_check_none(to,num,buf,j,num);
318 break;
319 default:
320 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
321 goto err;
322 }
323 if (r < 0)
324 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
325
326err:
327 if (ctx != NULL) BN_CTX_free(ctx);
328 BN_clear_free(&f);
329 BN_clear_free(&ret);
330 if (buf != NULL)
331 {
332 memset(buf,0,num);
333 Free(buf);
334 }
335 return(r);
336 }
337
338static int RSA_eay_public_decrypt(int flen, unsigned char *from,
339 unsigned char *to, RSA *rsa, int padding)
340 {
341 BIGNUM f,ret;
342 int i,num=0,r= -1;
343 unsigned char *p;
344 unsigned char *buf=NULL;
345 BN_CTX *ctx=NULL;
346
347 BN_init(&f);
348 BN_init(&ret);
349 ctx=BN_CTX_new();
350 if (ctx == NULL) goto err;
351
352 num=BN_num_bytes(rsa->n);
353 buf=(unsigned char *)Malloc(num);
354 if (buf == NULL)
355 {
356 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
357 goto err;
358 }
359
360 /* This check was for equality but PGP does evil things
361 * and chops off the top '0' bytes */
362 if (flen > num)
363 {
364 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
365 goto err;
366 }
367
368 if (BN_bin2bn(from,flen,&f) == NULL) goto err;
369 /* do the decrypt */
370 if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC))
371 {
372 if ((rsa->_method_mod_n=BN_MONT_CTX_new()) != NULL)
373 if (!BN_MONT_CTX_set(rsa->_method_mod_n,rsa->n,ctx))
374 goto err;
375 }
376
377 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx,
378 rsa->_method_mod_n)) goto err;
379
380 p=buf;
381 i=BN_bn2bin(&ret,p);
382
383 switch (padding)
384 {
385 case RSA_PKCS1_PADDING:
386 r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num);
387 break;
388 case RSA_NO_PADDING:
389 r=RSA_padding_check_none(to,num,buf,i,num);
390 break;
391 default:
392 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
393 goto err;
394 }
395 if (r < 0)
396 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
397
398err:
399 if (ctx != NULL) BN_CTX_free(ctx);
400 BN_clear_free(&f);
401 BN_clear_free(&ret);
402 if (buf != NULL)
403 {
404 memset(buf,0,num);
405 Free(buf);
406 }
407 return(r);
408 }
409
410static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa)
411 {
412 BIGNUM r1,m1;
413 int ret=0;
414 BN_CTX *ctx;
415
416 if ((ctx=BN_CTX_new()) == NULL) goto err;
417 BN_init(&m1);
418 BN_init(&r1);
419
420 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE)
421 {
422 if (rsa->_method_mod_p == NULL)
423 {
424 if ((rsa->_method_mod_p=BN_MONT_CTX_new()) != NULL)
425 if (!BN_MONT_CTX_set(rsa->_method_mod_p,rsa->p,
426 ctx))
427 goto err;
428 }
429 if (rsa->_method_mod_q == NULL)
430 {
431 if ((rsa->_method_mod_q=BN_MONT_CTX_new()) != NULL)
432 if (!BN_MONT_CTX_set(rsa->_method_mod_q,rsa->q,
433 ctx))
434 goto err;
435 }
436 }
437
438 if (!BN_mod(&r1,I,rsa->q,ctx)) goto err;
439 if (!rsa->meth->bn_mod_exp(&m1,&r1,rsa->dmq1,rsa->q,ctx,
440 rsa->_method_mod_q)) goto err;
441
442 if (!BN_mod(&r1,I,rsa->p,ctx)) goto err;
443 if (!rsa->meth->bn_mod_exp(r0,&r1,rsa->dmp1,rsa->p,ctx,
444 rsa->_method_mod_p)) goto err;
445
446 if (!BN_sub(r0,r0,&m1)) goto err;
447 /* This will help stop the size of r0 increasing, which does
448 * affect the multiply if it optimised for a power of 2 size */
449 if (r0->neg)
450 if (!BN_add(r0,r0,rsa->p)) goto err;
451
452 if (!BN_mul(&r1,r0,rsa->iqmp,ctx)) goto err;
453 if (!BN_mod(r0,&r1,rsa->p,ctx)) goto err;
454 /* If p < q it is occasionally possible for the correction of
455 * adding 'p' if r0 is negative above to leave the result still
456 * negative. This can break the private key operations: the following
457 * second correction should *always* correct this rare occurrence.
458 * This will *never* happen with OpenSSL generated keys because
459 * they ensure p > q [steve]
460 */
461 if (r0->neg)
462 if (!BN_add(r0,r0,rsa->p)) goto err;
463 if (!BN_mul(&r1,r0,rsa->q,ctx)) goto err;
464 if (!BN_add(r0,&r1,&m1)) goto err;
465
466 ret=1;
467err:
468 BN_clear_free(&m1);
469 BN_clear_free(&r1);
470 BN_CTX_free(ctx);
471 return(ret);
472 }
473
474static int RSA_eay_init(RSA *rsa)
475 {
476 rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
477 return(1);
478 }
479
480static int RSA_eay_finish(RSA *rsa)
481 {
482 if (rsa->_method_mod_n != NULL)
483 BN_MONT_CTX_free(rsa->_method_mod_n);
484 if (rsa->_method_mod_p != NULL)
485 BN_MONT_CTX_free(rsa->_method_mod_p);
486 if (rsa->_method_mod_q != NULL)
487 BN_MONT_CTX_free(rsa->_method_mod_q);
488 return(1);
489 }
490
491#endif
diff --git a/src/lib/libcrypto/rsa/rsa_err.c b/src/lib/libcrypto/rsa/rsa_err.c
deleted file mode 100644
index 1cde7c0da4..0000000000
--- a/src/lib/libcrypto/rsa/rsa_err.c
+++ /dev/null
@@ -1,148 +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 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_SMALL ,"data too small"},
110{RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE ,"data too small for key size"},
111{RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY ,"digest too big for rsa key"},
112{RSA_R_DMP1_NOT_CONGRUENT_TO_D ,"dmp1 not congruent to d"},
113{RSA_R_DMQ1_NOT_CONGRUENT_TO_D ,"dmq1 not congruent to d"},
114{RSA_R_D_E_NOT_CONGRUENT_TO_1 ,"d e not congruent to 1"},
115{RSA_R_INVALID_MESSAGE_LENGTH ,"invalid message length"},
116{RSA_R_IQMP_NOT_INVERSE_OF_Q ,"iqmp not inverse of q"},
117{RSA_R_KEY_SIZE_TOO_SMALL ,"key size too small"},
118{RSA_R_NULL_BEFORE_BLOCK_MISSING ,"null before block missing"},
119{RSA_R_N_DOES_NOT_EQUAL_P_Q ,"n does not equal p q"},
120{RSA_R_OAEP_DECODING_ERROR ,"oaep decoding error"},
121{RSA_R_PADDING_CHECK_FAILED ,"padding check failed"},
122{RSA_R_P_NOT_PRIME ,"p not prime"},
123{RSA_R_Q_NOT_PRIME ,"q not prime"},
124{RSA_R_RSA_OPERATIONS_NOT_SUPPORTED ,"rsa operations not supported"},
125{RSA_R_SSLV3_ROLLBACK_ATTACK ,"sslv3 rollback attack"},
126{RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD,"the asn1 object identifier is not known for this md"},
127{RSA_R_UNKNOWN_ALGORITHM_TYPE ,"unknown algorithm type"},
128{RSA_R_UNKNOWN_PADDING_TYPE ,"unknown padding type"},
129{RSA_R_WRONG_SIGNATURE_LENGTH ,"wrong signature length"},
130{0,NULL}
131 };
132
133#endif
134
135void ERR_load_RSA_strings(void)
136 {
137 static int init=1;
138
139 if (init)
140 {
141 init=0;
142#ifndef NO_ERR
143 ERR_load_strings(ERR_LIB_RSA,RSA_str_functs);
144 ERR_load_strings(ERR_LIB_RSA,RSA_str_reasons);
145#endif
146
147 }
148 }
diff --git a/src/lib/libcrypto/rsa/rsa_gen.c b/src/lib/libcrypto/rsa/rsa_gen.c
deleted file mode 100644
index 95e636d3f0..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 & (1<<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 074a4f5074..0000000000
--- a/src/lib/libcrypto/rsa/rsa_lib.c
+++ /dev/null
@@ -1,333 +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
66const char *RSA_version="RSA" OPENSSL_VERSION_PTEXT;
67
68static RSA_METHOD *default_RSA_meth=NULL;
69static int rsa_meth_num=0;
70static STACK_OF(CRYPTO_EX_DATA_FUNCS) *rsa_meth=NULL;
71
72RSA *RSA_new(void)
73 {
74 return(RSA_new_method(NULL));
75 }
76
77void RSA_set_default_method(RSA_METHOD *meth)
78 {
79 default_RSA_meth=meth;
80 }
81
82RSA_METHOD *RSA_get_default_method(void)
83{
84 return default_RSA_meth;
85}
86
87RSA_METHOD *RSA_get_method(RSA *rsa)
88{
89 return rsa->meth;
90}
91
92RSA_METHOD *RSA_set_method(RSA *rsa, RSA_METHOD *meth)
93{
94 RSA_METHOD *mtmp;
95 mtmp = rsa->meth;
96 if (mtmp->finish) mtmp->finish(rsa);
97 rsa->meth = meth;
98 if (meth->init) meth->init(rsa);
99 return mtmp;
100}
101
102RSA *RSA_new_method(RSA_METHOD *meth)
103 {
104 RSA *ret;
105
106 if (default_RSA_meth == NULL)
107 {
108#ifdef RSA_NULL
109 default_RSA_meth=RSA_null_method();
110#else
111#ifdef RSAref
112 default_RSA_meth=RSA_PKCS1_RSAref();
113#else
114 default_RSA_meth=RSA_PKCS1_SSLeay();
115#endif
116#endif
117 }
118 ret=(RSA *)Malloc(sizeof(RSA));
119 if (ret == NULL)
120 {
121 RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
122 return(NULL);
123 }
124
125 if (meth == NULL)
126 ret->meth=default_RSA_meth;
127 else
128 ret->meth=meth;
129
130 ret->pad=0;
131 ret->version=0;
132 ret->n=NULL;
133 ret->e=NULL;
134 ret->d=NULL;
135 ret->p=NULL;
136 ret->q=NULL;
137 ret->dmp1=NULL;
138 ret->dmq1=NULL;
139 ret->iqmp=NULL;
140 ret->references=1;
141 ret->_method_mod_n=NULL;
142 ret->_method_mod_p=NULL;
143 ret->_method_mod_q=NULL;
144 ret->blinding=NULL;
145 ret->bignum_data=NULL;
146 ret->flags=ret->meth->flags;
147 if ((ret->meth->init != NULL) && !ret->meth->init(ret))
148 {
149 Free(ret);
150 ret=NULL;
151 }
152 else
153 CRYPTO_new_ex_data(rsa_meth,ret,&ret->ex_data);
154 return(ret);
155 }
156
157void RSA_free(RSA *r)
158 {
159 int i;
160
161 if (r == NULL) return;
162
163 i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_RSA);
164#ifdef REF_PRINT
165 REF_PRINT("RSA",r);
166#endif
167 if (i > 0) return;
168#ifdef REF_CHECK
169 if (i < 0)
170 {
171 fprintf(stderr,"RSA_free, bad reference count\n");
172 abort();
173 }
174#endif
175
176 CRYPTO_free_ex_data(rsa_meth,r,&r->ex_data);
177
178 if (r->meth->finish != NULL)
179 r->meth->finish(r);
180
181 if (r->n != NULL) BN_clear_free(r->n);
182 if (r->e != NULL) BN_clear_free(r->e);
183 if (r->d != NULL) BN_clear_free(r->d);
184 if (r->p != NULL) BN_clear_free(r->p);
185 if (r->q != NULL) BN_clear_free(r->q);
186 if (r->dmp1 != NULL) BN_clear_free(r->dmp1);
187 if (r->dmq1 != NULL) BN_clear_free(r->dmq1);
188 if (r->iqmp != NULL) BN_clear_free(r->iqmp);
189 if (r->blinding != NULL) BN_BLINDING_free(r->blinding);
190 if (r->bignum_data != NULL) Free_locked(r->bignum_data);
191 Free(r);
192 }
193
194int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
195 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
196 {
197 rsa_meth_num++;
198 return(CRYPTO_get_ex_new_index(rsa_meth_num-1,
199 &rsa_meth,argl,argp,new_func,dup_func,free_func));
200 }
201
202int RSA_set_ex_data(RSA *r, int idx, void *arg)
203 {
204 return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
205 }
206
207void *RSA_get_ex_data(RSA *r, int idx)
208 {
209 return(CRYPTO_get_ex_data(&r->ex_data,idx));
210 }
211
212int RSA_size(RSA *r)
213 {
214 return(BN_num_bytes(r->n));
215 }
216
217int RSA_public_encrypt(int flen, unsigned char *from, unsigned char *to,
218 RSA *rsa, int padding)
219 {
220 return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding));
221 }
222
223int RSA_private_encrypt(int flen, unsigned char *from, unsigned char *to,
224 RSA *rsa, int padding)
225 {
226 return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding));
227 }
228
229int RSA_private_decrypt(int flen, unsigned char *from, unsigned char *to,
230 RSA *rsa, int padding)
231 {
232 return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding));
233 }
234
235int RSA_public_decrypt(int flen, unsigned char *from, unsigned char *to,
236 RSA *rsa, int padding)
237 {
238 return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding));
239 }
240
241int RSA_flags(RSA *r)
242 {
243 return((r == NULL)?0:r->meth->flags);
244 }
245
246void RSA_blinding_off(RSA *rsa)
247 {
248 if (rsa->blinding != NULL)
249 {
250 BN_BLINDING_free(rsa->blinding);
251 rsa->blinding=NULL;
252 }
253 rsa->flags&= ~RSA_FLAG_BLINDING;
254 }
255
256int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx)
257 {
258 BIGNUM *A,*Ai;
259 BN_CTX *ctx;
260 int ret=0;
261
262 if (p_ctx == NULL)
263 {
264 if ((ctx=BN_CTX_new()) == NULL) goto err;
265 }
266 else
267 ctx=p_ctx;
268
269 if (rsa->blinding != NULL)
270 BN_BLINDING_free(rsa->blinding);
271
272 BN_CTX_start(ctx);
273 A = BN_CTX_get(ctx);
274 if (!BN_rand(A,BN_num_bits(rsa->n)-1,1,0)) goto err;
275 if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
276
277 if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n))
278 goto err;
279 rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n);
280 rsa->flags|=RSA_FLAG_BLINDING;
281 BN_free(Ai);
282 ret=1;
283err:
284 BN_CTX_end(ctx);
285 if (ctx != p_ctx) BN_CTX_free(ctx);
286 return(ret);
287 }
288
289int RSA_memory_lock(RSA *r)
290 {
291 int i,j,k,off;
292 char *p;
293 BIGNUM *bn,**t[6],*b;
294 BN_ULONG *ul;
295
296 if (r->d == NULL) return(1);
297 t[0]= &r->d;
298 t[1]= &r->p;
299 t[2]= &r->q;
300 t[3]= &r->dmp1;
301 t[4]= &r->dmq1;
302 t[5]= &r->iqmp;
303 k=sizeof(BIGNUM)*6;
304 off=k/sizeof(BN_ULONG)+1;
305 j=1;
306 for (i=0; i<6; i++)
307 j+= (*t[i])->top;
308 if ((p=Malloc_locked((off+j)*sizeof(BN_ULONG))) == NULL)
309 {
310 RSAerr(RSA_F_MEMORY_LOCK,ERR_R_MALLOC_FAILURE);
311 return(0);
312 }
313 bn=(BIGNUM *)p;
314 ul=(BN_ULONG *)&(p[off]);
315 for (i=0; i<6; i++)
316 {
317 b= *(t[i]);
318 *(t[i])= &(bn[i]);
319 memcpy((char *)&(bn[i]),(char *)b,sizeof(BIGNUM));
320 bn[i].flags=BN_FLG_STATIC_DATA;
321 bn[i].d=ul;
322 memcpy((char *)ul,b->d,sizeof(BN_ULONG)*b->top);
323 ul+=b->top;
324 BN_clear_free(b);
325 }
326
327 /* I should fix this so it can still be done */
328 r->flags&= ~(RSA_FLAG_CACHE_PRIVATE|RSA_FLAG_CACHE_PUBLIC);
329
330 r->bignum_data=p;
331 return(1);
332 }
333
diff --git a/src/lib/libcrypto/rsa/rsa_none.c b/src/lib/libcrypto/rsa/rsa_none.c
deleted file mode 100644
index f22fce5016..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, unsigned char *from,
66 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, unsigned char *from,
85 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 1465c01f4f..0000000000
--- a/src/lib/libcrypto/rsa/rsa_oaep.c
+++ /dev/null
@@ -1,163 +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#if !defined(NO_SHA) && !defined(NO_SHA1)
8#include <stdio.h>
9#include "cryptlib.h"
10#include <openssl/bn.h>
11#include <openssl/rsa.h>
12#include <openssl/sha.h>
13#include <openssl/rand.h>
14
15int MGF1(unsigned char *mask, long len, unsigned char *seed, long seedlen);
16
17int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
18 unsigned char *from, int flen, unsigned char *param, int plen)
19 {
20 int i, emlen = tlen - 1;
21 unsigned char *db, *seed;
22 unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH];
23
24 if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1)
25 {
26 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,
27 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
28 return (0);
29 }
30
31 if (emlen < 2 * SHA_DIGEST_LENGTH + 1)
32 {
33 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_KEY_SIZE_TOO_SMALL);
34 return (0);
35 }
36
37 dbmask = Malloc(emlen - SHA_DIGEST_LENGTH);
38 if (dbmask == NULL)
39 {
40 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
41 return (0);
42 }
43
44 to[0] = 0;
45 seed = to + 1;
46 db = to + SHA_DIGEST_LENGTH + 1;
47
48 SHA1(param, plen, db);
49 memset(db + SHA_DIGEST_LENGTH, 0,
50 emlen - flen - 2 * SHA_DIGEST_LENGTH - 1);
51 db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01;
52 memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, (unsigned int) flen);
53 if (RAND_bytes(seed, SHA_DIGEST_LENGTH) <= 0)
54 return (0);
55#ifdef PKCS_TESTVECT
56 memcpy(seed,
57 "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f",
58 20);
59#endif
60
61 MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH);
62 for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++)
63 db[i] ^= dbmask[i];
64
65 MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH);
66 for (i = 0; i < SHA_DIGEST_LENGTH; i++)
67 seed[i] ^= seedmask[i];
68
69 Free(dbmask);
70 return (1);
71 }
72
73int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
74 unsigned char *from, int flen, int num, unsigned char *param,
75 int plen)
76 {
77 int i, dblen, mlen = -1;
78 unsigned char *maskeddb;
79 int lzero;
80 unsigned char *db, seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH];
81
82 if (--num < 2 * SHA_DIGEST_LENGTH + 1)
83 {
84 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR);
85 return (-1);
86 }
87
88 dblen = num - SHA_DIGEST_LENGTH;
89 db = Malloc(dblen);
90 if (db == NULL)
91 {
92 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
93 return (-1);
94 }
95
96 lzero = num - flen;
97 maskeddb = from - lzero + SHA_DIGEST_LENGTH;
98
99 MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen);
100 for (i = lzero; i < SHA_DIGEST_LENGTH; i++)
101 seed[i] ^= from[i - lzero];
102
103 MGF1(db, dblen, seed, SHA_DIGEST_LENGTH);
104 for (i = 0; i < dblen; i++)
105 db[i] ^= maskeddb[i];
106
107 SHA1(param, plen, phash);
108
109 if (memcmp(db, phash, SHA_DIGEST_LENGTH) != 0)
110 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR);
111 else
112 {
113 for (i = SHA_DIGEST_LENGTH; i < dblen; i++)
114 if (db[i] != 0x00)
115 break;
116 if (db[i] != 0x01 || i++ >= dblen)
117 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP,
118 RSA_R_OAEP_DECODING_ERROR);
119 else
120 {
121 mlen = dblen - i;
122 if (tlen < mlen)
123 {
124 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
125 mlen = -1;
126 }
127 else
128 memcpy(to, db + i, mlen);
129 }
130 }
131 Free(db);
132 return (mlen);
133 }
134
135int MGF1(unsigned char *mask, long len, unsigned char *seed, long seedlen)
136 {
137 long i, outlen = 0;
138 unsigned char cnt[4];
139 SHA_CTX c;
140 unsigned char md[SHA_DIGEST_LENGTH];
141
142 for (i = 0; outlen < len; i++)
143 {
144 cnt[0] = (i >> 24) & 255, cnt[1] = (i >> 16) & 255,
145 cnt[2] = (i >> 8) & 255, cnt[3] = i & 255;
146 SHA1_Init(&c);
147 SHA1_Update(&c, seed, seedlen);
148 SHA1_Update(&c, cnt, 4);
149 if (outlen + SHA_DIGEST_LENGTH <= len)
150 {
151 SHA1_Final(mask + outlen, &c);
152 outlen += SHA_DIGEST_LENGTH;
153 }
154 else
155 {
156 SHA1_Final(md, &c);
157 memcpy(mask + outlen, md, len - outlen);
158 outlen = len;
159 }
160 }
161 return (0);
162 }
163#endif
diff --git a/src/lib/libcrypto/rsa/rsa_pk1.c b/src/lib/libcrypto/rsa/rsa_pk1.c
deleted file mode 100644
index 48a32bc264..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 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 unsigned char *from, int flen, int num)
93 {
94 int i,j;
95 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 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 unsigned char *from, int flen, int num)
183 {
184 int i,j;
185 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 61efb0b00f..0000000000
--- a/src/lib/libcrypto/rsa/rsa_saos.c
+++ /dev/null
@@ -1,144 +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, unsigned char *m, unsigned int m_len,
67 unsigned char *sigret, unsigned int *siglen, RSA *rsa)
68 {
69 ASN1_OCTET_STRING sig;
70 int i,j,ret=1;
71 unsigned char *p,*s;
72
73 sig.type=V_ASN1_OCTET_STRING;
74 sig.length=m_len;
75 sig.data=m;
76
77 i=i2d_ASN1_OCTET_STRING(&sig,NULL);
78 j=RSA_size(rsa);
79 if ((i-RSA_PKCS1_PADDING) > j)
80 {
81 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
82 return(0);
83 }
84 s=(unsigned char *)Malloc((unsigned int)j+1);
85 if (s == NULL)
86 {
87 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE);
88 return(0);
89 }
90 p=s;
91 i2d_ASN1_OCTET_STRING(&sig,&p);
92 i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING);
93 if (i <= 0)
94 ret=0;
95 else
96 *siglen=i;
97
98 memset(s,0,(unsigned int)j+1);
99 Free(s);
100 return(ret);
101 }
102
103int RSA_verify_ASN1_OCTET_STRING(int dtype, unsigned char *m,
104 unsigned int m_len, unsigned char *sigbuf, unsigned int siglen,
105 RSA *rsa)
106 {
107 int i,ret=0;
108 unsigned char *p,*s;
109 ASN1_OCTET_STRING *sig=NULL;
110
111 if (siglen != (unsigned int)RSA_size(rsa))
112 {
113 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_WRONG_SIGNATURE_LENGTH);
114 return(0);
115 }
116
117 s=(unsigned char *)Malloc((unsigned int)siglen);
118 if (s == NULL)
119 {
120 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE);
121 goto err;
122 }
123 i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);
124
125 if (i <= 0) goto err;
126
127 p=s;
128 sig=d2i_ASN1_OCTET_STRING(NULL,&p,(long)i);
129 if (sig == NULL) goto err;
130
131 if ( ((unsigned int)sig->length != m_len) ||
132 (memcmp(m,sig->data,m_len) != 0))
133 {
134 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_BAD_SIGNATURE);
135 }
136 else
137 ret=1;
138err:
139 if (sig != NULL) M_ASN1_OCTET_STRING_free(sig);
140 memset(s,0,(unsigned int)siglen);
141 Free(s);
142 return(ret);
143 }
144
diff --git a/src/lib/libcrypto/rsa/rsa_sign.c b/src/lib/libcrypto/rsa/rsa_sign.c
deleted file mode 100644
index 05bb7fb74a..0000000000
--- a/src/lib/libcrypto/rsa/rsa_sign.c
+++ /dev/null
@@ -1,221 +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
66/* Size of an SSL signature: MD5+SHA1 */
67#define SSL_SIG_LENGTH 36
68
69int RSA_sign(int type, unsigned char *m, unsigned int m_len,
70 unsigned char *sigret, unsigned int *siglen, RSA *rsa)
71 {
72 X509_SIG sig;
73 ASN1_TYPE parameter;
74 int i,j,ret=1;
75 unsigned char *p,*s = NULL;
76 X509_ALGOR algor;
77 ASN1_OCTET_STRING digest;
78 if(rsa->flags & RSA_FLAG_SIGN_VER)
79 return rsa->meth->rsa_sign(type, m, m_len, sigret, siglen, rsa);
80 /* Special case: SSL signature, just check the length */
81 if(type == NID_md5_sha1) {
82 if(m_len != SSL_SIG_LENGTH) {
83 RSAerr(RSA_F_RSA_SIGN,RSA_R_INVALID_MESSAGE_LENGTH);
84 return(0);
85 }
86 i = SSL_SIG_LENGTH;
87 s = m;
88 } else {
89 sig.algor= &algor;
90 sig.algor->algorithm=OBJ_nid2obj(type);
91 if (sig.algor->algorithm == NULL)
92 {
93 RSAerr(RSA_F_RSA_SIGN,RSA_R_UNKNOWN_ALGORITHM_TYPE);
94 return(0);
95 }
96 if (sig.algor->algorithm->length == 0)
97 {
98 RSAerr(RSA_F_RSA_SIGN,RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
99 return(0);
100 }
101 parameter.type=V_ASN1_NULL;
102 parameter.value.ptr=NULL;
103 sig.algor->parameter= &parameter;
104
105 sig.digest= &digest;
106 sig.digest->data=m;
107 sig.digest->length=m_len;
108
109 i=i2d_X509_SIG(&sig,NULL);
110 }
111 j=RSA_size(rsa);
112 if ((i-RSA_PKCS1_PADDING) > j)
113 {
114 RSAerr(RSA_F_RSA_SIGN,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
115 return(0);
116 }
117 if(type != NID_md5_sha1) {
118 s=(unsigned char *)Malloc((unsigned int)j+1);
119 if (s == NULL)
120 {
121 RSAerr(RSA_F_RSA_SIGN,ERR_R_MALLOC_FAILURE);
122 return(0);
123 }
124 p=s;
125 i2d_X509_SIG(&sig,&p);
126 }
127 i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING);
128 if (i <= 0)
129 ret=0;
130 else
131 *siglen=i;
132
133 if(type != NID_md5_sha1) {
134 memset(s,0,(unsigned int)j+1);
135 Free(s);
136 }
137 return(ret);
138 }
139
140int RSA_verify(int dtype, unsigned char *m, unsigned int m_len,
141 unsigned char *sigbuf, unsigned int siglen, RSA *rsa)
142 {
143 int i,ret=0,sigtype;
144 unsigned char *p,*s;
145 X509_SIG *sig=NULL;
146
147 if (siglen != (unsigned int)RSA_size(rsa))
148 {
149 RSAerr(RSA_F_RSA_VERIFY,RSA_R_WRONG_SIGNATURE_LENGTH);
150 return(0);
151 }
152
153 if(rsa->flags & RSA_FLAG_SIGN_VER)
154 return rsa->meth->rsa_verify(dtype, m, m_len, sigbuf, siglen, rsa);
155
156 s=(unsigned char *)Malloc((unsigned int)siglen);
157 if (s == NULL)
158 {
159 RSAerr(RSA_F_RSA_VERIFY,ERR_R_MALLOC_FAILURE);
160 goto err;
161 }
162 if((dtype == NID_md5_sha1) && (m_len != SSL_SIG_LENGTH) ) {
163 RSAerr(RSA_F_RSA_VERIFY,RSA_R_INVALID_MESSAGE_LENGTH);
164 return(0);
165 }
166 i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);
167
168 if (i <= 0) goto err;
169
170 /* Special case: SSL signature */
171 if(dtype == NID_md5_sha1) {
172 if((i != SSL_SIG_LENGTH) || memcmp(s, m, SSL_SIG_LENGTH))
173 RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
174 else ret = 1;
175 } else {
176 p=s;
177 sig=d2i_X509_SIG(NULL,&p,(long)i);
178
179 if (sig == NULL) goto err;
180 sigtype=OBJ_obj2nid(sig->algor->algorithm);
181
182
183 #ifdef RSA_DEBUG
184 /* put a backward compatibility flag in EAY */
185 fprintf(stderr,"in(%s) expect(%s)\n",OBJ_nid2ln(sigtype),
186 OBJ_nid2ln(dtype));
187 #endif
188 if (sigtype != dtype)
189 {
190 if (((dtype == NID_md5) &&
191 (sigtype == NID_md5WithRSAEncryption)) ||
192 ((dtype == NID_md2) &&
193 (sigtype == NID_md2WithRSAEncryption)))
194 {
195 /* ok, we will let it through */
196 #if !defined(NO_STDIO) && !defined(WIN16)
197 fprintf(stderr,"signature has problems, re-make with post SSLeay045\n");
198 #endif
199 }
200 else
201 {
202 RSAerr(RSA_F_RSA_VERIFY,
203 RSA_R_ALGORITHM_MISMATCH);
204 goto err;
205 }
206 }
207 if ( ((unsigned int)sig->digest->length != m_len) ||
208 (memcmp(m,sig->digest->data,m_len) != 0))
209 {
210 RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
211 }
212 else
213 ret=1;
214 }
215err:
216 if (sig != NULL) X509_SIG_free(sig);
217 memset(s,0,(unsigned int)siglen);
218 Free(s);
219 return(ret);
220 }
221
diff --git a/src/lib/libcrypto/rsa/rsa_ssl.c b/src/lib/libcrypto/rsa/rsa_ssl.c
deleted file mode 100644
index 81a857c813..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, unsigned char *from,
66 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, unsigned char *from,
106 int flen, int num)
107 {
108 int i,j,k;
109 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 == 0)
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