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.h407
-rw-r--r--src/lib/libcrypto/rsa/rsa_asn1.c121
-rw-r--r--src/lib/libcrypto/rsa/rsa_chk.c184
-rw-r--r--src/lib/libcrypto/rsa/rsa_eay.c774
-rw-r--r--src/lib/libcrypto/rsa/rsa_err.c164
-rw-r--r--src/lib/libcrypto/rsa/rsa_gen.c201
-rw-r--r--src/lib/libcrypto/rsa/rsa_lib.c414
-rw-r--r--src/lib/libcrypto/rsa/rsa_none.c98
-rw-r--r--src/lib/libcrypto/rsa/rsa_oaep.c212
-rw-r--r--src/lib/libcrypto/rsa/rsa_pk1.c224
-rw-r--r--src/lib/libcrypto/rsa/rsa_pss.c261
-rw-r--r--src/lib/libcrypto/rsa/rsa_saos.c149
-rw-r--r--src/lib/libcrypto/rsa/rsa_sign.c249
-rw-r--r--src/lib/libcrypto/rsa/rsa_ssl.c154
-rw-r--r--src/lib/libcrypto/rsa/rsa_x931.c177
15 files changed, 0 insertions, 3789 deletions
diff --git a/src/lib/libcrypto/rsa/rsa.h b/src/lib/libcrypto/rsa/rsa.h
deleted file mode 100644
index 0b639cd37f..0000000000
--- a/src/lib/libcrypto/rsa/rsa.h
+++ /dev/null
@@ -1,407 +0,0 @@
1/* crypto/rsa/rsa.h */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_RSA_H
60#define HEADER_RSA_H
61
62#include <openssl/asn1.h>
63
64#ifndef OPENSSL_NO_BIO
65#include <openssl/bio.h>
66#endif
67#include <openssl/bn.h>
68#include <openssl/crypto.h>
69#include <openssl/ossl_typ.h>
70
71#ifdef OPENSSL_NO_RSA
72#error RSA is disabled.
73#endif
74
75#if defined(OPENSSL_FIPS)
76#define FIPS_RSA_SIZE_T int
77#endif
78
79#ifdef __cplusplus
80extern "C" {
81#endif
82
83typedef struct rsa_st RSA;
84
85typedef struct rsa_meth_st
86 {
87 const char *name;
88 int (*rsa_pub_enc)(int flen,const unsigned char *from,
89 unsigned char *to,
90 RSA *rsa,int padding);
91 int (*rsa_pub_dec)(int flen,const unsigned char *from,
92 unsigned char *to,
93 RSA *rsa,int padding);
94 int (*rsa_priv_enc)(int flen,const unsigned char *from,
95 unsigned char *to,
96 RSA *rsa,int padding);
97 int (*rsa_priv_dec)(int flen,const unsigned char *from,
98 unsigned char *to,
99 RSA *rsa,int padding);
100 int (*rsa_mod_exp)(BIGNUM *r0,const BIGNUM *I,RSA *rsa); /* Can be null */
101 int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
102 const BIGNUM *m, BN_CTX *ctx,
103 BN_MONT_CTX *m_ctx); /* Can be null */
104 int (*init)(RSA *rsa); /* called at new */
105 int (*finish)(RSA *rsa); /* called at free */
106 int flags; /* RSA_METHOD_FLAG_* things */
107 char *app_data; /* may be needed! */
108/* New sign and verify functions: some libraries don't allow arbitrary data
109 * to be signed/verified: this allows them to be used. Note: for this to work
110 * the RSA_public_decrypt() and RSA_private_encrypt() should *NOT* be used
111 * RSA_sign(), RSA_verify() should be used instead. Note: for backwards
112 * compatibility this functionality is only enabled if the RSA_FLAG_SIGN_VER
113 * option is set in 'flags'.
114 */
115 int (*rsa_sign)(int type,
116 const unsigned char *m, unsigned int m_length,
117 unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
118 int (*rsa_verify)(int dtype,
119 const unsigned char *m, unsigned int m_length,
120 unsigned char *sigbuf, unsigned int siglen, const RSA *rsa);
121
122 } RSA_METHOD;
123
124struct rsa_st
125 {
126 /* The first parameter is used to pickup errors where
127 * this is passed instead of aEVP_PKEY, it is set to 0 */
128 int pad;
129 long version;
130 const RSA_METHOD *meth;
131 /* functional reference if 'meth' is ENGINE-provided */
132 ENGINE *engine;
133 BIGNUM *n;
134 BIGNUM *e;
135 BIGNUM *d;
136 BIGNUM *p;
137 BIGNUM *q;
138 BIGNUM *dmp1;
139 BIGNUM *dmq1;
140 BIGNUM *iqmp;
141 /* be careful using this if the RSA structure is shared */
142 CRYPTO_EX_DATA ex_data;
143 int references;
144 int flags;
145
146 /* Used to cache montgomery values */
147 BN_MONT_CTX *_method_mod_n;
148 BN_MONT_CTX *_method_mod_p;
149 BN_MONT_CTX *_method_mod_q;
150
151 /* all BIGNUM values are actually in the following data, if it is not
152 * NULL */
153 char *bignum_data;
154 BN_BLINDING *blinding;
155 };
156
157#define RSA_3 0x3L
158#define RSA_F4 0x10001L
159
160#define RSA_METHOD_FLAG_NO_CHECK 0x0001 /* don't check pub/private match */
161
162#define RSA_FLAG_CACHE_PUBLIC 0x0002
163#define RSA_FLAG_CACHE_PRIVATE 0x0004
164#define RSA_FLAG_BLINDING 0x0008
165#define RSA_FLAG_THREAD_SAFE 0x0010
166/* This flag means the private key operations will be handled by rsa_mod_exp
167 * and that they do not depend on the private key components being present:
168 * for example a key stored in external hardware. Without this flag bn_mod_exp
169 * gets called when private key components are absent.
170 */
171#define RSA_FLAG_EXT_PKEY 0x0020
172
173/* This flag in the RSA_METHOD enables the new rsa_sign, rsa_verify functions.
174 */
175#define RSA_FLAG_SIGN_VER 0x0040
176
177#define RSA_FLAG_NO_BLINDING 0x0080 /* new with 0.9.6j and 0.9.7b; the built-in
178 * RSA implementation now uses blinding by
179 * default (ignoring RSA_FLAG_BLINDING),
180 * but other engines might not need it
181 */
182#define RSA_FLAG_NO_EXP_CONSTTIME 0x0100 /* new with 0.9.7h; the built-in RSA
183 * implementation now uses constant time
184 * modular exponentiation for secret exponents
185 * by default. This flag causes the
186 * faster variable sliding window method to
187 * be used for all exponents.
188 */
189
190#define RSA_PKCS1_PADDING 1
191#define RSA_SSLV23_PADDING 2
192#define RSA_NO_PADDING 3
193#define RSA_PKCS1_OAEP_PADDING 4
194#define RSA_X931_PADDING 5
195
196#define RSA_PKCS1_PADDING_SIZE 11
197
198#define RSA_set_app_data(s,arg) RSA_set_ex_data(s,0,arg)
199#define RSA_get_app_data(s) RSA_get_ex_data(s,0)
200
201RSA * RSA_new(void);
202RSA * RSA_new_method(ENGINE *engine);
203int RSA_size(const RSA *);
204RSA * RSA_generate_key(int bits, unsigned long e,void
205 (*callback)(int,int,void *),void *cb_arg);
206int RSA_check_key(const RSA *);
207#ifdef OPENSSL_FIPS
208int RSA_X931_derive(RSA *rsa, BIGNUM *p1, BIGNUM *p2, BIGNUM *q1, BIGNUM *q2,
209 void (*cb)(int, int, void *), void *cb_arg,
210 const BIGNUM *Xp1, const BIGNUM *Xp2, const BIGNUM *Xp,
211 const BIGNUM *Xq1, const BIGNUM *Xq2, const BIGNUM *Xq,
212 const BIGNUM *e);
213RSA *RSA_X931_generate_key(int bits, const BIGNUM *e,
214 void (*cb)(int,int,void *), void *cb_arg);
215#endif
216 /* next 4 return -1 on error */
217int RSA_public_encrypt(int flen, const unsigned char *from,
218 unsigned char *to, RSA *rsa,int padding);
219int RSA_private_encrypt(int flen, const unsigned char *from,
220 unsigned char *to, RSA *rsa,int padding);
221int RSA_public_decrypt(int flen, const unsigned char *from,
222 unsigned char *to, RSA *rsa,int padding);
223int RSA_private_decrypt(int flen, const unsigned char *from,
224 unsigned char *to, RSA *rsa,int padding);
225void RSA_free (RSA *r);
226/* "up" the RSA object's reference count */
227int RSA_up_ref(RSA *r);
228
229int RSA_flags(const RSA *r);
230
231void RSA_set_default_method(const RSA_METHOD *meth);
232const RSA_METHOD *RSA_get_default_method(void);
233const RSA_METHOD *RSA_get_method(const RSA *rsa);
234int RSA_set_method(RSA *rsa, const RSA_METHOD *meth);
235
236/* This function needs the memory locking malloc callbacks to be installed */
237int RSA_memory_lock(RSA *r);
238
239/* these are the actual SSLeay RSA functions */
240const RSA_METHOD *RSA_PKCS1_SSLeay(void);
241
242const RSA_METHOD *RSA_null_method(void);
243
244DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPublicKey)
245DECLARE_ASN1_ENCODE_FUNCTIONS_const(RSA, RSAPrivateKey)
246
247#ifndef OPENSSL_NO_FP_API
248int RSA_print_fp(FILE *fp, const RSA *r,int offset);
249#endif
250
251#ifndef OPENSSL_NO_BIO
252int RSA_print(BIO *bp, const RSA *r,int offset);
253#endif
254
255int i2d_RSA_NET(const RSA *a, unsigned char **pp, int (*cb)(), int sgckey);
256RSA *d2i_RSA_NET(RSA **a, const unsigned char **pp, long length, int (*cb)(), int sgckey);
257
258int i2d_Netscape_RSA(const RSA *a, unsigned char **pp, int (*cb)());
259RSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length, int (*cb)());
260
261/* The following 2 functions sign and verify a X509_SIG ASN1 object
262 * inside PKCS#1 padded RSA encryption */
263int RSA_sign(int type, const unsigned char *m, unsigned int m_length,
264 unsigned char *sigret, unsigned int *siglen, RSA *rsa);
265int RSA_verify(int type, const unsigned char *m, unsigned int m_length,
266 unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
267
268/* The following 2 function sign and verify a ASN1_OCTET_STRING
269 * object inside PKCS#1 padded RSA encryption */
270int RSA_sign_ASN1_OCTET_STRING(int type,
271 const unsigned char *m, unsigned int m_length,
272 unsigned char *sigret, unsigned int *siglen, RSA *rsa);
273int RSA_verify_ASN1_OCTET_STRING(int type,
274 const unsigned char *m, unsigned int m_length,
275 unsigned char *sigbuf, unsigned int siglen, RSA *rsa);
276
277int RSA_blinding_on(RSA *rsa, BN_CTX *ctx);
278void RSA_blinding_off(RSA *rsa);
279
280int RSA_padding_add_PKCS1_type_1(unsigned char *to,int tlen,
281 const unsigned char *f,int fl);
282int RSA_padding_check_PKCS1_type_1(unsigned char *to,int tlen,
283 const unsigned char *f,int fl,int rsa_len);
284int RSA_padding_add_PKCS1_type_2(unsigned char *to,int tlen,
285 const unsigned char *f,int fl);
286int RSA_padding_check_PKCS1_type_2(unsigned char *to,int tlen,
287 const unsigned char *f,int fl,int rsa_len);
288int PKCS1_MGF1(unsigned char *mask, long len,
289 const unsigned char *seed, long seedlen, const EVP_MD *dgst);
290int RSA_padding_add_PKCS1_OAEP(unsigned char *to,int tlen,
291 const unsigned char *f,int fl,
292 const unsigned char *p,int pl);
293int RSA_padding_check_PKCS1_OAEP(unsigned char *to,int tlen,
294 const unsigned char *f,int fl,int rsa_len,
295 const unsigned char *p,int pl);
296int RSA_padding_add_SSLv23(unsigned char *to,int tlen,
297 const unsigned char *f,int fl);
298int RSA_padding_check_SSLv23(unsigned char *to,int tlen,
299 const unsigned char *f,int fl,int rsa_len);
300int RSA_padding_add_none(unsigned char *to,int tlen,
301 const unsigned char *f,int fl);
302int RSA_padding_check_none(unsigned char *to,int tlen,
303 const unsigned char *f,int fl,int rsa_len);
304int RSA_padding_add_X931(unsigned char *to,int tlen,
305 const unsigned char *f,int fl);
306int RSA_padding_check_X931(unsigned char *to,int tlen,
307 const unsigned char *f,int fl,int rsa_len);
308int RSA_X931_hash_id(int nid);
309
310int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
311 const EVP_MD *Hash, const unsigned char *EM, int sLen);
312int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
313 const unsigned char *mHash,
314 const EVP_MD *Hash, int sLen);
315
316int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
317 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func);
318int RSA_set_ex_data(RSA *r,int idx,void *arg);
319void *RSA_get_ex_data(const RSA *r, int idx);
320
321RSA *RSAPublicKey_dup(RSA *rsa);
322RSA *RSAPrivateKey_dup(RSA *rsa);
323
324/* BEGIN ERROR CODES */
325/* The following lines are auto generated by the script mkerr.pl. Any changes
326 * made after this point may be overwritten when the script is next run.
327 */
328void ERR_load_RSA_strings(void);
329
330/* Error codes for the RSA functions. */
331
332/* Function codes. */
333#define RSA_F_MEMORY_LOCK 100
334#define RSA_F_RSA_CHECK_KEY 123
335#define RSA_F_RSA_EAY_PRIVATE_DECRYPT 101
336#define RSA_F_RSA_EAY_PRIVATE_ENCRYPT 102
337#define RSA_F_RSA_EAY_PUBLIC_DECRYPT 103
338#define RSA_F_RSA_EAY_PUBLIC_ENCRYPT 104
339#define RSA_F_RSA_GENERATE_KEY 105
340#define RSA_F_RSA_NEW_METHOD 106
341#define RSA_F_RSA_NULL 124
342#define RSA_F_RSA_PADDING_ADD_NONE 107
343#define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP 121
344#define RSA_F_RSA_PADDING_ADD_PKCS1_PSS 125
345#define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1 108
346#define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2 109
347#define RSA_F_RSA_PADDING_ADD_SSLV23 110
348#define RSA_F_RSA_PADDING_ADD_X931 127
349#define RSA_F_RSA_PADDING_CHECK_NONE 111
350#define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP 122
351#define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1 112
352#define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2 113
353#define RSA_F_RSA_PADDING_CHECK_SSLV23 114
354#define RSA_F_RSA_PADDING_CHECK_X931 128
355#define RSA_F_RSA_PRINT 115
356#define RSA_F_RSA_PRINT_FP 116
357#define RSA_F_RSA_SIGN 117
358#define RSA_F_RSA_SIGN_ASN1_OCTET_STRING 118
359#define RSA_F_RSA_VERIFY 119
360#define RSA_F_RSA_VERIFY_ASN1_OCTET_STRING 120
361#define RSA_F_RSA_VERIFY_PKCS1_PSS 126
362
363/* Reason codes. */
364#define RSA_R_ALGORITHM_MISMATCH 100
365#define RSA_R_BAD_E_VALUE 101
366#define RSA_R_BAD_FIXED_HEADER_DECRYPT 102
367#define RSA_R_BAD_PAD_BYTE_COUNT 103
368#define RSA_R_BAD_SIGNATURE 104
369#define RSA_R_BLOCK_TYPE_IS_NOT_01 106
370#define RSA_R_BLOCK_TYPE_IS_NOT_02 107
371#define RSA_R_DATA_GREATER_THAN_MOD_LEN 108
372#define RSA_R_DATA_TOO_LARGE 109
373#define RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 110
374#define RSA_R_DATA_TOO_LARGE_FOR_MODULUS 132
375#define RSA_R_DATA_TOO_SMALL 111
376#define RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE 122
377#define RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY 112
378#define RSA_R_DMP1_NOT_CONGRUENT_TO_D 124
379#define RSA_R_DMQ1_NOT_CONGRUENT_TO_D 125
380#define RSA_R_D_E_NOT_CONGRUENT_TO_1 123
381#define RSA_R_FIRST_OCTET_INVALID 133
382#define RSA_R_INVALID_HEADER 137
383#define RSA_R_INVALID_MESSAGE_LENGTH 131
384#define RSA_R_INVALID_PADDING 138
385#define RSA_R_INVALID_TRAILER 139
386#define RSA_R_IQMP_NOT_INVERSE_OF_Q 126
387#define RSA_R_KEY_SIZE_TOO_SMALL 120
388#define RSA_R_LAST_OCTET_INVALID 134
389#define RSA_R_NULL_BEFORE_BLOCK_MISSING 113
390#define RSA_R_N_DOES_NOT_EQUAL_P_Q 127
391#define RSA_R_OAEP_DECODING_ERROR 121
392#define RSA_R_PADDING_CHECK_FAILED 114
393#define RSA_R_P_NOT_PRIME 128
394#define RSA_R_Q_NOT_PRIME 129
395#define RSA_R_RSA_OPERATIONS_NOT_SUPPORTED 130
396#define RSA_R_SLEN_CHECK_FAILED 136
397#define RSA_R_SLEN_RECOVERY_FAILED 135
398#define RSA_R_SSLV3_ROLLBACK_ATTACK 115
399#define RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD 116
400#define RSA_R_UNKNOWN_ALGORITHM_TYPE 117
401#define RSA_R_UNKNOWN_PADDING_TYPE 118
402#define RSA_R_WRONG_SIGNATURE_LENGTH 119
403
404#ifdef __cplusplus
405}
406#endif
407#endif
diff --git a/src/lib/libcrypto/rsa/rsa_asn1.c b/src/lib/libcrypto/rsa/rsa_asn1.c
deleted file mode 100644
index 1455a7e0e4..0000000000
--- a/src/lib/libcrypto/rsa/rsa_asn1.c
+++ /dev/null
@@ -1,121 +0,0 @@
1/* rsa_asn1.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/rsa.h>
63#include <openssl/asn1t.h>
64
65static ASN1_METHOD method={
66 (int (*)()) i2d_RSAPrivateKey,
67 (char *(*)())d2i_RSAPrivateKey,
68 (char *(*)())RSA_new,
69 (void (*)()) RSA_free};
70
71ASN1_METHOD *RSAPrivateKey_asn1_meth(void)
72 {
73 return(&method);
74 }
75
76/* Override the default free and new methods */
77static int rsa_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it)
78{
79 if(operation == ASN1_OP_NEW_PRE) {
80 *pval = (ASN1_VALUE *)RSA_new();
81 if(*pval) return 2;
82 return 0;
83 } else if(operation == ASN1_OP_FREE_PRE) {
84 RSA_free((RSA *)*pval);
85 *pval = NULL;
86 return 2;
87 }
88 return 1;
89}
90
91ASN1_SEQUENCE_cb(RSAPrivateKey, rsa_cb) = {
92 ASN1_SIMPLE(RSA, version, LONG),
93 ASN1_SIMPLE(RSA, n, BIGNUM),
94 ASN1_SIMPLE(RSA, e, BIGNUM),
95 ASN1_SIMPLE(RSA, d, BIGNUM),
96 ASN1_SIMPLE(RSA, p, BIGNUM),
97 ASN1_SIMPLE(RSA, q, BIGNUM),
98 ASN1_SIMPLE(RSA, dmp1, BIGNUM),
99 ASN1_SIMPLE(RSA, dmq1, BIGNUM),
100 ASN1_SIMPLE(RSA, iqmp, BIGNUM)
101} ASN1_SEQUENCE_END_cb(RSA, RSAPrivateKey)
102
103
104ASN1_SEQUENCE_cb(RSAPublicKey, rsa_cb) = {
105 ASN1_SIMPLE(RSA, n, BIGNUM),
106 ASN1_SIMPLE(RSA, e, BIGNUM),
107} ASN1_SEQUENCE_END_cb(RSA, RSAPublicKey)
108
109IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPrivateKey, RSAPrivateKey)
110
111IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(RSA, RSAPublicKey, RSAPublicKey)
112
113RSA *RSAPublicKey_dup(RSA *rsa)
114 {
115 return ASN1_item_dup(ASN1_ITEM_rptr(RSAPublicKey), rsa);
116 }
117
118RSA *RSAPrivateKey_dup(RSA *rsa)
119 {
120 return ASN1_item_dup(ASN1_ITEM_rptr(RSAPrivateKey), rsa);
121 }
diff --git a/src/lib/libcrypto/rsa/rsa_chk.c b/src/lib/libcrypto/rsa/rsa_chk.c
deleted file mode 100644
index 002f2cb487..0000000000
--- a/src/lib/libcrypto/rsa/rsa_chk.c
+++ /dev/null
@@ -1,184 +0,0 @@
1/* crypto/rsa/rsa_chk.c -*- Mode: C; c-file-style: "eay" -*- */
2/* ====================================================================
3 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 */
50
51#include <openssl/bn.h>
52#include <openssl/err.h>
53#include <openssl/rsa.h>
54
55
56int RSA_check_key(const RSA *key)
57 {
58 BIGNUM *i, *j, *k, *l, *m;
59 BN_CTX *ctx;
60 int r;
61 int ret=1;
62
63 i = BN_new();
64 j = BN_new();
65 k = BN_new();
66 l = BN_new();
67 m = BN_new();
68 ctx = BN_CTX_new();
69 if (i == NULL || j == NULL || k == NULL || l == NULL ||
70 m == NULL || ctx == NULL)
71 {
72 ret = -1;
73 RSAerr(RSA_F_RSA_CHECK_KEY, ERR_R_MALLOC_FAILURE);
74 goto err;
75 }
76
77 /* p prime? */
78 r = BN_is_prime(key->p, BN_prime_checks, NULL, NULL, NULL);
79 if (r != 1)
80 {
81 ret = r;
82 if (r != 0)
83 goto err;
84 RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_P_NOT_PRIME);
85 }
86
87 /* q prime? */
88 r = BN_is_prime(key->q, BN_prime_checks, NULL, NULL, NULL);
89 if (r != 1)
90 {
91 ret = r;
92 if (r != 0)
93 goto err;
94 RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_Q_NOT_PRIME);
95 }
96
97 /* n = p*q? */
98 r = BN_mul(i, key->p, key->q, ctx);
99 if (!r) { ret = -1; goto err; }
100
101 if (BN_cmp(i, key->n) != 0)
102 {
103 ret = 0;
104 RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_N_DOES_NOT_EQUAL_P_Q);
105 }
106
107 /* d*e = 1 mod lcm(p-1,q-1)? */
108
109 r = BN_sub(i, key->p, BN_value_one());
110 if (!r) { ret = -1; goto err; }
111 r = BN_sub(j, key->q, BN_value_one());
112 if (!r) { ret = -1; goto err; }
113
114 /* now compute k = lcm(i,j) */
115 r = BN_mul(l, i, j, ctx);
116 if (!r) { ret = -1; goto err; }
117 r = BN_gcd(m, i, j, ctx);
118 if (!r) { ret = -1; goto err; }
119 r = BN_div(k, NULL, l, m, ctx); /* remainder is 0 */
120 if (!r) { ret = -1; goto err; }
121
122 r = BN_mod_mul(i, key->d, key->e, k, ctx);
123 if (!r) { ret = -1; goto err; }
124
125 if (!BN_is_one(i))
126 {
127 ret = 0;
128 RSAerr(RSA_F_RSA_CHECK_KEY, RSA_R_D_E_NOT_CONGRUENT_TO_1);
129 }
130
131 if (key->dmp1 != NULL && key->dmq1 != NULL && key->iqmp != NULL)
132 {
133 /* dmp1 = d mod (p-1)? */
134 r = BN_sub(i, key->p, BN_value_one());
135 if (!r) { ret = -1; goto err; }
136
137 r = BN_mod(j, key->d, i, ctx);
138 if (!r) { ret = -1; goto err; }
139
140 if (BN_cmp(j, key->dmp1) != 0)
141 {
142 ret = 0;
143 RSAerr(RSA_F_RSA_CHECK_KEY,
144 RSA_R_DMP1_NOT_CONGRUENT_TO_D);
145 }
146
147 /* dmq1 = d mod (q-1)? */
148 r = BN_sub(i, key->q, BN_value_one());
149 if (!r) { ret = -1; goto err; }
150
151 r = BN_mod(j, key->d, i, ctx);
152 if (!r) { ret = -1; goto err; }
153
154 if (BN_cmp(j, key->dmq1) != 0)
155 {
156 ret = 0;
157 RSAerr(RSA_F_RSA_CHECK_KEY,
158 RSA_R_DMQ1_NOT_CONGRUENT_TO_D);
159 }
160
161 /* iqmp = q^-1 mod p? */
162 if(!BN_mod_inverse(i, key->q, key->p, ctx))
163 {
164 ret = -1;
165 goto err;
166 }
167
168 if (BN_cmp(i, key->iqmp) != 0)
169 {
170 ret = 0;
171 RSAerr(RSA_F_RSA_CHECK_KEY,
172 RSA_R_IQMP_NOT_INVERSE_OF_Q);
173 }
174 }
175
176 err:
177 if (i != NULL) BN_free(i);
178 if (j != NULL) BN_free(j);
179 if (k != NULL) BN_free(k);
180 if (l != NULL) BN_free(l);
181 if (m != NULL) BN_free(m);
182 if (ctx != NULL) BN_CTX_free(ctx);
183 return (ret);
184 }
diff --git a/src/lib/libcrypto/rsa/rsa_eay.c b/src/lib/libcrypto/rsa/rsa_eay.c
deleted file mode 100644
index be4ac96ce3..0000000000
--- a/src/lib/libcrypto/rsa/rsa_eay.c
+++ /dev/null
@@ -1,774 +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 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
60 *
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
64 *
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 *
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 *
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
82 *
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
86 *
87 * 6. Redistributions of any form whatsoever must retain the following
88 * acknowledgment:
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 *
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
105 *
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
109 *
110 */
111
112#include <stdio.h>
113#include "cryptlib.h"
114#include <openssl/bn.h>
115#include <openssl/rsa.h>
116#include <openssl/rand.h>
117
118#if !defined(RSA_NULL) && !defined(OPENSSL_FIPS)
119
120static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
121 unsigned char *to, RSA *rsa,int padding);
122static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
123 unsigned char *to, RSA *rsa,int padding);
124static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
125 unsigned char *to, RSA *rsa,int padding);
126static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
127 unsigned char *to, RSA *rsa,int padding);
128static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa);
129static int RSA_eay_init(RSA *rsa);
130static int RSA_eay_finish(RSA *rsa);
131static RSA_METHOD rsa_pkcs1_eay_meth={
132 "Eric Young's PKCS#1 RSA",
133 RSA_eay_public_encrypt,
134 RSA_eay_public_decrypt, /* signature verification */
135 RSA_eay_private_encrypt, /* signing */
136 RSA_eay_private_decrypt,
137 RSA_eay_mod_exp,
138 BN_mod_exp_mont, /* XXX probably we should not use Montgomery if e == 3 */
139 RSA_eay_init,
140 RSA_eay_finish,
141 0, /* flags */
142 NULL,
143 0, /* rsa_sign */
144 0 /* rsa_verify */
145 };
146
147const RSA_METHOD *RSA_PKCS1_SSLeay(void)
148 {
149 return(&rsa_pkcs1_eay_meth);
150 }
151
152static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
153 unsigned char *to, RSA *rsa, int padding)
154 {
155 BIGNUM f,ret;
156 int i,j,k,num=0,r= -1;
157 unsigned char *buf=NULL;
158 BN_CTX *ctx=NULL;
159
160 BN_init(&f);
161 BN_init(&ret);
162 if ((ctx=BN_CTX_new()) == NULL) goto err;
163 num=BN_num_bytes(rsa->n);
164 if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
165 {
166 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
167 goto err;
168 }
169
170 switch (padding)
171 {
172 case RSA_PKCS1_PADDING:
173 i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
174 break;
175#ifndef OPENSSL_NO_SHA
176 case RSA_PKCS1_OAEP_PADDING:
177 i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);
178 break;
179#endif
180 case RSA_SSLV23_PADDING:
181 i=RSA_padding_add_SSLv23(buf,num,from,flen);
182 break;
183 case RSA_NO_PADDING:
184 i=RSA_padding_add_none(buf,num,from,flen);
185 break;
186 default:
187 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
188 goto err;
189 }
190 if (i <= 0) goto err;
191
192 if (BN_bin2bn(buf,num,&f) == NULL) goto err;
193
194 if (BN_ucmp(&f, rsa->n) >= 0)
195 {
196 /* usually the padding functions would catch this */
197 RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
198 goto err;
199 }
200
201 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
202 {
203 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
204 CRYPTO_LOCK_RSA, rsa->n, ctx))
205 goto err;
206 }
207
208 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx,
209 rsa->_method_mod_n)) goto err;
210
211 /* put in leading 0 bytes if the number is less than the
212 * length of the modulus */
213 j=BN_num_bytes(&ret);
214 i=BN_bn2bin(&ret,&(to[num-j]));
215 for (k=0; k<(num-i); k++)
216 to[k]=0;
217
218 r=num;
219err:
220 if (ctx != NULL) BN_CTX_free(ctx);
221 BN_clear_free(&f);
222 BN_clear_free(&ret);
223 if (buf != NULL)
224 {
225 OPENSSL_cleanse(buf,num);
226 OPENSSL_free(buf);
227 }
228 return(r);
229 }
230
231static int rsa_eay_blinding(RSA *rsa, BN_CTX *ctx)
232 {
233 int ret = 1;
234 CRYPTO_w_lock(CRYPTO_LOCK_RSA);
235 /* Check again inside the lock - the macro's check is racey */
236 if(rsa->blinding == NULL)
237 ret = RSA_blinding_on(rsa, ctx);
238 CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
239 return ret;
240 }
241
242#define BLINDING_HELPER(rsa, ctx, err_instr) \
243 do { \
244 if((!((rsa)->flags & RSA_FLAG_NO_BLINDING)) && \
245 ((rsa)->blinding == NULL) && \
246 !rsa_eay_blinding(rsa, ctx)) \
247 err_instr \
248 } while(0)
249
250static BN_BLINDING *setup_blinding(RSA *rsa, BN_CTX *ctx)
251 {
252 BIGNUM *A, *Ai;
253 BN_BLINDING *ret = NULL;
254
255 /* added in OpenSSL 0.9.6j and 0.9.7b */
256
257 /* NB: similar code appears in RSA_blinding_on (rsa_lib.c);
258 * this should be placed in a new function of its own, but for reasons
259 * of binary compatibility can't */
260
261 BN_CTX_start(ctx);
262 A = BN_CTX_get(ctx);
263 if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL)
264 {
265 /* if PRNG is not properly seeded, resort to secret exponent as unpredictable seed */
266 RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0);
267 if (!BN_pseudo_rand_range(A,rsa->n)) goto err;
268 }
269 else
270 {
271 if (!BN_rand_range(A,rsa->n)) goto err;
272 }
273 if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
274
275 if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n))
276 goto err;
277 ret = BN_BLINDING_new(A,Ai,rsa->n);
278 BN_free(Ai);
279err:
280 BN_CTX_end(ctx);
281 return ret;
282 }
283
284/* signing */
285static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
286 unsigned char *to, RSA *rsa, int padding)
287 {
288 BIGNUM f,ret, *res;
289 int i,j,k,num=0,r= -1;
290 unsigned char *buf=NULL;
291 BN_CTX *ctx=NULL;
292 int local_blinding = 0;
293 BN_BLINDING *blinding = NULL;
294
295 BN_init(&f);
296 BN_init(&ret);
297
298 if ((ctx=BN_CTX_new()) == NULL) goto err;
299 num=BN_num_bytes(rsa->n);
300 if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
301 {
302 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
303 goto err;
304 }
305
306 switch (padding)
307 {
308 case RSA_PKCS1_PADDING:
309 i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);
310 break;
311 case RSA_NO_PADDING:
312 i=RSA_padding_add_none(buf,num,from,flen);
313 break;
314 case RSA_SSLV23_PADDING:
315 default:
316 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
317 goto err;
318 }
319 if (i <= 0) goto err;
320
321 if (BN_bin2bn(buf,num,&f) == NULL) goto err;
322
323 if (BN_ucmp(&f, rsa->n) >= 0)
324 {
325 /* usually the padding functions would catch this */
326 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
327 goto err;
328 }
329
330 BLINDING_HELPER(rsa, ctx, goto err;);
331 blinding = rsa->blinding;
332
333 /* Now unless blinding is disabled, 'blinding' is non-NULL.
334 * But the BN_BLINDING object may be owned by some other thread
335 * (we don't want to keep it constant and we don't want to use
336 * lots of locking to avoid race conditions, so only a single
337 * thread can use it; other threads have to use local blinding
338 * factors) */
339 if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
340 {
341 if (blinding == NULL)
342 {
343 RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
344 goto err;
345 }
346 }
347
348 if (blinding != NULL)
349 {
350 if (blinding->thread_id != CRYPTO_thread_id())
351 {
352 /* we need a local one-time blinding factor */
353
354 blinding = setup_blinding(rsa, ctx);
355 if (blinding == NULL)
356 goto err;
357 local_blinding = 1;
358 }
359 }
360
361 if (blinding)
362 if (!BN_BLINDING_convert(&f, blinding, ctx)) goto err;
363
364 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
365 ((rsa->p != NULL) &&
366 (rsa->q != NULL) &&
367 (rsa->dmp1 != NULL) &&
368 (rsa->dmq1 != NULL) &&
369 (rsa->iqmp != NULL)) )
370 {
371 if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err;
372 }
373 else
374 {
375 BIGNUM local_d;
376 BIGNUM *d = NULL;
377
378 if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME))
379 {
380 BN_init(&local_d);
381 d = &local_d;
382 BN_with_flags(d, rsa->d, BN_FLG_EXP_CONSTTIME);
383 }
384 else
385 d = rsa->d;
386 if (!rsa->meth->bn_mod_exp(&ret,&f,d,rsa->n,ctx,NULL)) goto err;
387 }
388
389 if (blinding)
390 if (!BN_BLINDING_invert(&ret, blinding, ctx)) goto err;
391
392 if (padding == RSA_X931_PADDING)
393 {
394 BN_sub(&f, rsa->n, &ret);
395 if (BN_cmp(&ret, &f))
396 res = &f;
397 else
398 res = &ret;
399 }
400 else
401 res = &ret;
402
403 /* put in leading 0 bytes if the number is less than the
404 * length of the modulus */
405 j=BN_num_bytes(res);
406 i=BN_bn2bin(res,&(to[num-j]));
407 for (k=0; k<(num-i); k++)
408 to[k]=0;
409
410 r=num;
411err:
412 if (ctx != NULL) BN_CTX_free(ctx);
413 BN_clear_free(&ret);
414 BN_clear_free(&f);
415 if (local_blinding)
416 BN_BLINDING_free(blinding);
417 if (buf != NULL)
418 {
419 OPENSSL_cleanse(buf,num);
420 OPENSSL_free(buf);
421 }
422 return(r);
423 }
424
425static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
426 unsigned char *to, RSA *rsa, int padding)
427 {
428 BIGNUM f,ret;
429 int j,num=0,r= -1;
430 unsigned char *p;
431 unsigned char *buf=NULL;
432 BN_CTX *ctx=NULL;
433 int local_blinding = 0;
434 BN_BLINDING *blinding = NULL;
435
436 BN_init(&f);
437 BN_init(&ret);
438 ctx=BN_CTX_new();
439 if (ctx == NULL) goto err;
440
441 num=BN_num_bytes(rsa->n);
442
443 if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
444 {
445 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
446 goto err;
447 }
448
449 /* This check was for equality but PGP does evil things
450 * and chops off the top '0' bytes */
451 if (flen > num)
452 {
453 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
454 goto err;
455 }
456
457 /* make data into a big number */
458 if (BN_bin2bn(from,(int)flen,&f) == NULL) goto err;
459
460 if (BN_ucmp(&f, rsa->n) >= 0)
461 {
462 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
463 goto err;
464 }
465
466 BLINDING_HELPER(rsa, ctx, goto err;);
467 blinding = rsa->blinding;
468
469 /* Now unless blinding is disabled, 'blinding' is non-NULL.
470 * But the BN_BLINDING object may be owned by some other thread
471 * (we don't want to keep it constant and we don't want to use
472 * lots of locking to avoid race conditions, so only a single
473 * thread can use it; other threads have to use local blinding
474 * factors) */
475 if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
476 {
477 if (blinding == NULL)
478 {
479 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
480 goto err;
481 }
482 }
483
484 if (blinding != NULL)
485 {
486 if (blinding->thread_id != CRYPTO_thread_id())
487 {
488 /* we need a local one-time blinding factor */
489
490 blinding = setup_blinding(rsa, ctx);
491 if (blinding == NULL)
492 goto err;
493 local_blinding = 1;
494 }
495 }
496
497 if (blinding)
498 if (!BN_BLINDING_convert(&f, blinding, ctx)) goto err;
499
500 /* do the decrypt */
501 if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
502 ((rsa->p != NULL) &&
503 (rsa->q != NULL) &&
504 (rsa->dmp1 != NULL) &&
505 (rsa->dmq1 != NULL) &&
506 (rsa->iqmp != NULL)) )
507 {
508 if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err;
509 }
510 else
511 {
512 BIGNUM local_d;
513 BIGNUM *d = NULL;
514
515 if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME))
516 {
517 d = &local_d;
518 BN_with_flags(d, rsa->d, BN_FLG_EXP_CONSTTIME);
519 }
520 else
521 d = rsa->d;
522 if (!rsa->meth->bn_mod_exp(&ret,&f,d,rsa->n,ctx,NULL))
523 goto err;
524 }
525
526 if (blinding)
527 if (!BN_BLINDING_invert(&ret, blinding, ctx)) goto err;
528
529 p=buf;
530 j=BN_bn2bin(&ret,p); /* j is only used with no-padding mode */
531
532 switch (padding)
533 {
534 case RSA_PKCS1_PADDING:
535 r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);
536 break;
537#ifndef OPENSSL_NO_SHA
538 case RSA_PKCS1_OAEP_PADDING:
539 r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0);
540 break;
541#endif
542 case RSA_SSLV23_PADDING:
543 r=RSA_padding_check_SSLv23(to,num,buf,j,num);
544 break;
545 case RSA_NO_PADDING:
546 r=RSA_padding_check_none(to,num,buf,j,num);
547 break;
548 default:
549 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
550 goto err;
551 }
552 if (r < 0)
553 RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
554
555err:
556 if (ctx != NULL) BN_CTX_free(ctx);
557 BN_clear_free(&f);
558 BN_clear_free(&ret);
559 if (local_blinding)
560 BN_BLINDING_free(blinding);
561 if (buf != NULL)
562 {
563 OPENSSL_cleanse(buf,num);
564 OPENSSL_free(buf);
565 }
566 return(r);
567 }
568
569/* signature verification */
570static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
571 unsigned char *to, RSA *rsa, int padding)
572 {
573 BIGNUM f,ret;
574 int i,num=0,r= -1;
575 unsigned char *p;
576 unsigned char *buf=NULL;
577 BN_CTX *ctx=NULL;
578
579 BN_init(&f);
580 BN_init(&ret);
581 ctx=BN_CTX_new();
582 if (ctx == NULL) goto err;
583
584 num=BN_num_bytes(rsa->n);
585 buf=(unsigned char *)OPENSSL_malloc(num);
586 if (buf == NULL)
587 {
588 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
589 goto err;
590 }
591
592 /* This check was for equality but PGP does evil things
593 * and chops off the top '0' bytes */
594 if (flen > num)
595 {
596 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
597 goto err;
598 }
599
600 if (BN_bin2bn(from,flen,&f) == NULL) goto err;
601
602 if (BN_ucmp(&f, rsa->n) >= 0)
603 {
604 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
605 goto err;
606 }
607
608 /* do the decrypt */
609
610 if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
611 {
612 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n,
613 CRYPTO_LOCK_RSA, rsa->n, ctx))
614 goto err;
615 }
616
617 if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx,
618 rsa->_method_mod_n)) goto err;
619
620 if ((padding == RSA_X931_PADDING) && ((ret.d[0] & 0xf) != 12))
621 BN_sub(&ret, rsa->n, &ret);
622
623 p=buf;
624 i=BN_bn2bin(&ret,p);
625
626 switch (padding)
627 {
628 case RSA_PKCS1_PADDING:
629 r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num);
630 break;
631 case RSA_NO_PADDING:
632 r=RSA_padding_check_none(to,num,buf,i,num);
633 break;
634 default:
635 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
636 goto err;
637 }
638 if (r < 0)
639 RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
640
641err:
642 if (ctx != NULL) BN_CTX_free(ctx);
643 BN_clear_free(&f);
644 BN_clear_free(&ret);
645 if (buf != NULL)
646 {
647 OPENSSL_cleanse(buf,num);
648 OPENSSL_free(buf);
649 }
650 return(r);
651 }
652
653static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
654 {
655 BIGNUM r1,m1,vrfy;
656 BIGNUM local_dmp1, local_dmq1;
657 BIGNUM *dmp1, *dmq1;
658 int ret=0;
659 BN_CTX *ctx;
660
661 BN_init(&m1);
662 BN_init(&r1);
663 BN_init(&vrfy);
664 if ((ctx=BN_CTX_new()) == NULL) goto err;
665
666 if (rsa->flags & RSA_FLAG_CACHE_PRIVATE)
667 {
668 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p,
669 CRYPTO_LOCK_RSA, rsa->p, ctx))
670 goto err;
671 if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_q,
672 CRYPTO_LOCK_RSA, rsa->q, ctx))
673 goto err;
674 }
675
676 if (!BN_mod(&r1,I,rsa->q,ctx)) goto err;
677 if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME))
678 {
679 dmq1 = &local_dmq1;
680 BN_with_flags(dmq1, rsa->dmq1, BN_FLG_EXP_CONSTTIME);
681 }
682 else
683 dmq1 = rsa->dmq1;
684 if (!rsa->meth->bn_mod_exp(&m1,&r1,dmq1,rsa->q,ctx,
685 rsa->_method_mod_q)) goto err;
686
687 if (!BN_mod(&r1,I,rsa->p,ctx)) goto err;
688 if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME))
689 {
690 dmp1 = &local_dmp1;
691 BN_with_flags(dmp1, rsa->dmp1, BN_FLG_EXP_CONSTTIME);
692 }
693 else
694 dmp1 = rsa->dmp1;
695 if (!rsa->meth->bn_mod_exp(r0,&r1,dmp1,rsa->p,ctx,
696 rsa->_method_mod_p)) goto err;
697
698 if (!BN_sub(r0,r0,&m1)) goto err;
699 /* This will help stop the size of r0 increasing, which does
700 * affect the multiply if it optimised for a power of 2 size */
701 if (r0->neg)
702 if (!BN_add(r0,r0,rsa->p)) goto err;
703
704 if (!BN_mul(&r1,r0,rsa->iqmp,ctx)) goto err;
705 if (!BN_mod(r0,&r1,rsa->p,ctx)) goto err;
706 /* If p < q it is occasionally possible for the correction of
707 * adding 'p' if r0 is negative above to leave the result still
708 * negative. This can break the private key operations: the following
709 * second correction should *always* correct this rare occurrence.
710 * This will *never* happen with OpenSSL generated keys because
711 * they ensure p > q [steve]
712 */
713 if (r0->neg)
714 if (!BN_add(r0,r0,rsa->p)) goto err;
715 if (!BN_mul(&r1,r0,rsa->q,ctx)) goto err;
716 if (!BN_add(r0,&r1,&m1)) goto err;
717
718 if (rsa->e && rsa->n)
719 {
720 if (!rsa->meth->bn_mod_exp(&vrfy,r0,rsa->e,rsa->n,ctx,NULL)) goto err;
721 /* If 'I' was greater than (or equal to) rsa->n, the operation
722 * will be equivalent to using 'I mod n'. However, the result of
723 * the verify will *always* be less than 'n' so we don't check
724 * for absolute equality, just congruency. */
725 if (!BN_sub(&vrfy, &vrfy, I)) goto err;
726 if (!BN_mod(&vrfy, &vrfy, rsa->n, ctx)) goto err;
727 if (vrfy.neg)
728 if (!BN_add(&vrfy, &vrfy, rsa->n)) goto err;
729 if (!BN_is_zero(&vrfy))
730 {
731 /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
732 * miscalculated CRT output, just do a raw (slower)
733 * mod_exp and return that instead. */
734
735 BIGNUM local_d;
736 BIGNUM *d = NULL;
737
738 if (!(rsa->flags & RSA_FLAG_NO_EXP_CONSTTIME))
739 {
740 d = &local_d;
741 BN_with_flags(d, rsa->d, BN_FLG_EXP_CONSTTIME);
742 }
743 else
744 d = rsa->d;
745 if (!rsa->meth->bn_mod_exp(r0,I,d,rsa->n,ctx,NULL)) goto err;
746 }
747 }
748 ret=1;
749err:
750 BN_clear_free(&m1);
751 BN_clear_free(&r1);
752 BN_clear_free(&vrfy);
753 BN_CTX_free(ctx);
754 return(ret);
755 }
756
757static int RSA_eay_init(RSA *rsa)
758 {
759 rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
760 return(1);
761 }
762
763static int RSA_eay_finish(RSA *rsa)
764 {
765 if (rsa->_method_mod_n != NULL)
766 BN_MONT_CTX_free(rsa->_method_mod_n);
767 if (rsa->_method_mod_p != NULL)
768 BN_MONT_CTX_free(rsa->_method_mod_p);
769 if (rsa->_method_mod_q != NULL)
770 BN_MONT_CTX_free(rsa->_method_mod_q);
771 return(1);
772 }
773
774#endif
diff --git a/src/lib/libcrypto/rsa/rsa_err.c b/src/lib/libcrypto/rsa/rsa_err.c
deleted file mode 100644
index 2ec4b30ff7..0000000000
--- a/src/lib/libcrypto/rsa/rsa_err.c
+++ /dev/null
@@ -1,164 +0,0 @@
1/* crypto/rsa/rsa_err.c */
2/* ====================================================================
3 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21 *
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@OpenSSL.org.
26 *
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
30 *
31 * 6. Redistributions of any form whatsoever must retain the following
32 * acknowledgment:
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35 *
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
49 *
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
53 *
54 */
55
56/* NOTE: this file was auto generated by the mkerr.pl script: any changes
57 * made to it will be overwritten when the script next updates this file,
58 * only reason strings will be preserved.
59 */
60
61#include <stdio.h>
62#include <openssl/err.h>
63#include <openssl/rsa.h>
64
65/* BEGIN ERROR CODES */
66#ifndef OPENSSL_NO_ERR
67
68#define ERR_FUNC(func) ERR_PACK(ERR_LIB_RSA,func,0)
69#define ERR_REASON(reason) ERR_PACK(ERR_LIB_RSA,0,reason)
70
71static ERR_STRING_DATA RSA_str_functs[]=
72 {
73{ERR_FUNC(RSA_F_MEMORY_LOCK), "MEMORY_LOCK"},
74{ERR_FUNC(RSA_F_RSA_CHECK_KEY), "RSA_check_key"},
75{ERR_FUNC(RSA_F_RSA_EAY_PRIVATE_DECRYPT), "RSA_EAY_PRIVATE_DECRYPT"},
76{ERR_FUNC(RSA_F_RSA_EAY_PRIVATE_ENCRYPT), "RSA_EAY_PRIVATE_ENCRYPT"},
77{ERR_FUNC(RSA_F_RSA_EAY_PUBLIC_DECRYPT), "RSA_EAY_PUBLIC_DECRYPT"},
78{ERR_FUNC(RSA_F_RSA_EAY_PUBLIC_ENCRYPT), "RSA_EAY_PUBLIC_ENCRYPT"},
79{ERR_FUNC(RSA_F_RSA_GENERATE_KEY), "RSA_generate_key"},
80{ERR_FUNC(RSA_F_RSA_NEW_METHOD), "RSA_new_method"},
81{ERR_FUNC(RSA_F_RSA_NULL), "RSA_NULL"},
82{ERR_FUNC(RSA_F_RSA_PADDING_ADD_NONE), "RSA_padding_add_none"},
83{ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP), "RSA_padding_add_PKCS1_OAEP"},
84{ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_PSS), "RSA_padding_add_PKCS1_PSS"},
85{ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1), "RSA_padding_add_PKCS1_type_1"},
86{ERR_FUNC(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2), "RSA_padding_add_PKCS1_type_2"},
87{ERR_FUNC(RSA_F_RSA_PADDING_ADD_SSLV23), "RSA_padding_add_SSLv23"},
88{ERR_FUNC(RSA_F_RSA_PADDING_ADD_X931), "RSA_padding_add_X931"},
89{ERR_FUNC(RSA_F_RSA_PADDING_CHECK_NONE), "RSA_padding_check_none"},
90{ERR_FUNC(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP), "RSA_padding_check_PKCS1_OAEP"},
91{ERR_FUNC(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1), "RSA_padding_check_PKCS1_type_1"},
92{ERR_FUNC(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2), "RSA_padding_check_PKCS1_type_2"},
93{ERR_FUNC(RSA_F_RSA_PADDING_CHECK_SSLV23), "RSA_padding_check_SSLv23"},
94{ERR_FUNC(RSA_F_RSA_PADDING_CHECK_X931), "RSA_padding_check_X931"},
95{ERR_FUNC(RSA_F_RSA_PRINT), "RSA_print"},
96{ERR_FUNC(RSA_F_RSA_PRINT_FP), "RSA_print_fp"},
97{ERR_FUNC(RSA_F_RSA_SIGN), "RSA_sign"},
98{ERR_FUNC(RSA_F_RSA_SIGN_ASN1_OCTET_STRING), "RSA_sign_ASN1_OCTET_STRING"},
99{ERR_FUNC(RSA_F_RSA_VERIFY), "RSA_verify"},
100{ERR_FUNC(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING), "RSA_verify_ASN1_OCTET_STRING"},
101{ERR_FUNC(RSA_F_RSA_VERIFY_PKCS1_PSS), "RSA_verify_PKCS1_PSS"},
102{0,NULL}
103 };
104
105static ERR_STRING_DATA RSA_str_reasons[]=
106 {
107{ERR_REASON(RSA_R_ALGORITHM_MISMATCH) ,"algorithm mismatch"},
108{ERR_REASON(RSA_R_BAD_E_VALUE) ,"bad e value"},
109{ERR_REASON(RSA_R_BAD_FIXED_HEADER_DECRYPT),"bad fixed header decrypt"},
110{ERR_REASON(RSA_R_BAD_PAD_BYTE_COUNT) ,"bad pad byte count"},
111{ERR_REASON(RSA_R_BAD_SIGNATURE) ,"bad signature"},
112{ERR_REASON(RSA_R_BLOCK_TYPE_IS_NOT_01) ,"block type is not 01"},
113{ERR_REASON(RSA_R_BLOCK_TYPE_IS_NOT_02) ,"block type is not 02"},
114{ERR_REASON(RSA_R_DATA_GREATER_THAN_MOD_LEN),"data greater than mod len"},
115{ERR_REASON(RSA_R_DATA_TOO_LARGE) ,"data too large"},
116{ERR_REASON(RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE),"data too large for key size"},
117{ERR_REASON(RSA_R_DATA_TOO_LARGE_FOR_MODULUS),"data too large for modulus"},
118{ERR_REASON(RSA_R_DATA_TOO_SMALL) ,"data too small"},
119{ERR_REASON(RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE),"data too small for key size"},
120{ERR_REASON(RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY),"digest too big for rsa key"},
121{ERR_REASON(RSA_R_DMP1_NOT_CONGRUENT_TO_D),"dmp1 not congruent to d"},
122{ERR_REASON(RSA_R_DMQ1_NOT_CONGRUENT_TO_D),"dmq1 not congruent to d"},
123{ERR_REASON(RSA_R_D_E_NOT_CONGRUENT_TO_1),"d e not congruent to 1"},
124{ERR_REASON(RSA_R_FIRST_OCTET_INVALID) ,"first octet invalid"},
125{ERR_REASON(RSA_R_INVALID_HEADER) ,"invalid header"},
126{ERR_REASON(RSA_R_INVALID_MESSAGE_LENGTH),"invalid message length"},
127{ERR_REASON(RSA_R_INVALID_PADDING) ,"invalid padding"},
128{ERR_REASON(RSA_R_INVALID_TRAILER) ,"invalid trailer"},
129{ERR_REASON(RSA_R_IQMP_NOT_INVERSE_OF_Q) ,"iqmp not inverse of q"},
130{ERR_REASON(RSA_R_KEY_SIZE_TOO_SMALL) ,"key size too small"},
131{ERR_REASON(RSA_R_LAST_OCTET_INVALID) ,"last octet invalid"},
132{ERR_REASON(RSA_R_NULL_BEFORE_BLOCK_MISSING),"null before block missing"},
133{ERR_REASON(RSA_R_N_DOES_NOT_EQUAL_P_Q) ,"n does not equal p q"},
134{ERR_REASON(RSA_R_OAEP_DECODING_ERROR) ,"oaep decoding error"},
135{ERR_REASON(RSA_R_SLEN_RECOVERY_FAILED) ,"salt length recovery failed"},
136{ERR_REASON(RSA_R_PADDING_CHECK_FAILED) ,"padding check failed"},
137{ERR_REASON(RSA_R_P_NOT_PRIME) ,"p not prime"},
138{ERR_REASON(RSA_R_Q_NOT_PRIME) ,"q not prime"},
139{ERR_REASON(RSA_R_RSA_OPERATIONS_NOT_SUPPORTED),"rsa operations not supported"},
140{ERR_REASON(RSA_R_SSLV3_ROLLBACK_ATTACK) ,"sslv3 rollback attack"},
141{ERR_REASON(RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD),"the asn1 object identifier is not known for this md"},
142{ERR_REASON(RSA_R_UNKNOWN_ALGORITHM_TYPE),"unknown algorithm type"},
143{ERR_REASON(RSA_R_UNKNOWN_PADDING_TYPE) ,"unknown padding type"},
144{ERR_REASON(RSA_R_WRONG_SIGNATURE_LENGTH),"wrong signature length"},
145{ERR_REASON(RSA_R_SLEN_CHECK_FAILED) ,"salt length check failed"},
146{0,NULL}
147 };
148
149#endif
150
151void ERR_load_RSA_strings(void)
152 {
153 static int init=1;
154
155 if (init)
156 {
157 init=0;
158#ifndef OPENSSL_NO_ERR
159 ERR_load_strings(0,RSA_str_functs);
160 ERR_load_strings(0,RSA_str_reasons);
161#endif
162
163 }
164 }
diff --git a/src/lib/libcrypto/rsa/rsa_gen.c b/src/lib/libcrypto/rsa/rsa_gen.c
deleted file mode 100644
index dd1422cc98..0000000000
--- a/src/lib/libcrypto/rsa/rsa_gen.c
+++ /dev/null
@@ -1,201 +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
65#ifndef OPENSSL_FIPS
66
67RSA *RSA_generate_key(int bits, unsigned long e_value,
68 void (*callback)(int,int,void *), void *cb_arg)
69 {
70 RSA *rsa=NULL;
71 BIGNUM *r0=NULL,*r1=NULL,*r2=NULL,*r3=NULL,*tmp;
72 int bitsp,bitsq,ok= -1,n=0,i;
73 BN_CTX *ctx=NULL,*ctx2=NULL;
74
75 ctx=BN_CTX_new();
76 if (ctx == NULL) goto err;
77 ctx2=BN_CTX_new();
78 if (ctx2 == NULL) goto err;
79 BN_CTX_start(ctx);
80 r0 = BN_CTX_get(ctx);
81 r1 = BN_CTX_get(ctx);
82 r2 = BN_CTX_get(ctx);
83 r3 = BN_CTX_get(ctx);
84 if (r3 == NULL) goto err;
85
86 bitsp=(bits+1)/2;
87 bitsq=bits-bitsp;
88 rsa=RSA_new();
89 if (rsa == NULL) goto err;
90
91 /* set e */
92 rsa->e=BN_new();
93 if (rsa->e == NULL) goto err;
94
95#if 1
96 /* The problem is when building with 8, 16, or 32 BN_ULONG,
97 * unsigned long can be larger */
98 for (i=0; i<sizeof(unsigned long)*8; i++)
99 {
100 if (e_value & (1UL<<i))
101 BN_set_bit(rsa->e,i);
102 }
103#else
104 if (!BN_set_word(rsa->e,e_value)) goto err;
105#endif
106
107 /* generate p and q */
108 for (;;)
109 {
110 rsa->p=BN_generate_prime(NULL,bitsp,0,NULL,NULL,callback,cb_arg);
111 if (rsa->p == NULL) goto err;
112 if (!BN_sub(r2,rsa->p,BN_value_one())) goto err;
113 if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err;
114 if (BN_is_one(r1)) break;
115 if (callback != NULL) callback(2,n++,cb_arg);
116 BN_free(rsa->p);
117 }
118 if (callback != NULL) callback(3,0,cb_arg);
119 for (;;)
120 {
121 rsa->q=BN_generate_prime(NULL,bitsq,0,NULL,NULL,callback,cb_arg);
122 if (rsa->q == NULL) goto err;
123 if (!BN_sub(r2,rsa->q,BN_value_one())) goto err;
124 if (!BN_gcd(r1,r2,rsa->e,ctx)) goto err;
125 if (BN_is_one(r1) && (BN_cmp(rsa->p,rsa->q) != 0))
126 break;
127 if (callback != NULL) callback(2,n++,cb_arg);
128 BN_free(rsa->q);
129 }
130 if (callback != NULL) callback(3,1,cb_arg);
131 if (BN_cmp(rsa->p,rsa->q) < 0)
132 {
133 tmp=rsa->p;
134 rsa->p=rsa->q;
135 rsa->q=tmp;
136 }
137
138 /* calculate n */
139 rsa->n=BN_new();
140 if (rsa->n == NULL) goto err;
141 if (!BN_mul(rsa->n,rsa->p,rsa->q,ctx)) goto err;
142
143 /* calculate d */
144 if (!BN_sub(r1,rsa->p,BN_value_one())) goto err; /* p-1 */
145 if (!BN_sub(r2,rsa->q,BN_value_one())) goto err; /* q-1 */
146 if (!BN_mul(r0,r1,r2,ctx)) goto err; /* (p-1)(q-1) */
147
148/* should not be needed, since gcd(p-1,e) == 1 and gcd(q-1,e) == 1 */
149/* for (;;)
150 {
151 if (!BN_gcd(r3,r0,rsa->e,ctx)) goto err;
152 if (BN_is_one(r3)) break;
153
154 if (1)
155 {
156 if (!BN_add_word(rsa->e,2L)) goto err;
157 continue;
158 }
159 RSAerr(RSA_F_RSA_GENERATE_KEY,RSA_R_BAD_E_VALUE);
160 goto err;
161 }
162*/
163 rsa->d=BN_mod_inverse(NULL,rsa->e,r0,ctx2); /* d */
164 if (rsa->d == NULL) goto err;
165
166 /* calculate d mod (p-1) */
167 rsa->dmp1=BN_new();
168 if (rsa->dmp1 == NULL) goto err;
169 if (!BN_mod(rsa->dmp1,rsa->d,r1,ctx)) goto err;
170
171 /* calculate d mod (q-1) */
172 rsa->dmq1=BN_new();
173 if (rsa->dmq1 == NULL) goto err;
174 if (!BN_mod(rsa->dmq1,rsa->d,r2,ctx)) goto err;
175
176 /* calculate inverse of q mod p */
177 rsa->iqmp=BN_mod_inverse(NULL,rsa->q,rsa->p,ctx2);
178 if (rsa->iqmp == NULL) goto err;
179
180 ok=1;
181err:
182 if (ok == -1)
183 {
184 RSAerr(RSA_F_RSA_GENERATE_KEY,ERR_LIB_BN);
185 ok=0;
186 }
187 if (ctx != NULL)
188 BN_CTX_end(ctx);
189 BN_CTX_free(ctx);
190 BN_CTX_free(ctx2);
191
192 if (!ok)
193 {
194 if (rsa != NULL) RSA_free(rsa);
195 return(NULL);
196 }
197 else
198 return(rsa);
199 }
200
201#endif
diff --git a/src/lib/libcrypto/rsa/rsa_lib.c b/src/lib/libcrypto/rsa/rsa_lib.c
deleted file mode 100644
index e4d622851e..0000000000
--- a/src/lib/libcrypto/rsa/rsa_lib.c
+++ /dev/null
@@ -1,414 +0,0 @@
1/* crypto/rsa/rsa_lib.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include <openssl/crypto.h>
61#include "cryptlib.h"
62#include <openssl/lhash.h>
63#include <openssl/bn.h>
64#include <openssl/rsa.h>
65#include <openssl/rand.h>
66#ifndef OPENSSL_NO_ENGINE
67#include <openssl/engine.h>
68#endif
69
70const char *RSA_version="RSA" OPENSSL_VERSION_PTEXT;
71
72static const RSA_METHOD *default_RSA_meth=NULL;
73
74RSA *RSA_new(void)
75 {
76 RSA *r=RSA_new_method(NULL);
77
78 return r;
79 }
80
81void RSA_set_default_method(const RSA_METHOD *meth)
82 {
83 default_RSA_meth = meth;
84 }
85
86const RSA_METHOD *RSA_get_default_method(void)
87 {
88 if (default_RSA_meth == NULL)
89 {
90#ifdef RSA_NULL
91 default_RSA_meth=RSA_null_method();
92#else
93#if 0 /* was: #ifdef RSAref */
94 default_RSA_meth=RSA_PKCS1_RSAref();
95#else
96 default_RSA_meth=RSA_PKCS1_SSLeay();
97#endif
98#endif
99 }
100
101 return default_RSA_meth;
102 }
103
104const RSA_METHOD *RSA_get_method(const RSA *rsa)
105 {
106 return rsa->meth;
107 }
108
109int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
110 {
111 /* NB: The caller is specifically setting a method, so it's not up to us
112 * to deal with which ENGINE it comes from. */
113 const RSA_METHOD *mtmp;
114 mtmp = rsa->meth;
115 if (mtmp->finish) mtmp->finish(rsa);
116#ifndef OPENSSL_NO_ENGINE
117 if (rsa->engine)
118 {
119 ENGINE_finish(rsa->engine);
120 rsa->engine = NULL;
121 }
122#endif
123 rsa->meth = meth;
124 if (meth->init) meth->init(rsa);
125 return 1;
126 }
127
128RSA *RSA_new_method(ENGINE *engine)
129 {
130 RSA *ret;
131
132 ret=(RSA *)OPENSSL_malloc(sizeof(RSA));
133 if (ret == NULL)
134 {
135 RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
136 return NULL;
137 }
138
139 ret->meth = RSA_get_default_method();
140#ifndef OPENSSL_NO_ENGINE
141 if (engine)
142 {
143 if (!ENGINE_init(engine))
144 {
145 RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
146 OPENSSL_free(ret);
147 return NULL;
148 }
149 ret->engine = engine;
150 }
151 else
152 ret->engine = ENGINE_get_default_RSA();
153 if(ret->engine)
154 {
155 ret->meth = ENGINE_get_RSA(ret->engine);
156 if(!ret->meth)
157 {
158 RSAerr(RSA_F_RSA_NEW_METHOD,
159 ERR_R_ENGINE_LIB);
160 ENGINE_finish(ret->engine);
161 OPENSSL_free(ret);
162 return NULL;
163 }
164 }
165#endif
166
167 ret->pad=0;
168 ret->version=0;
169 ret->n=NULL;
170 ret->e=NULL;
171 ret->d=NULL;
172 ret->p=NULL;
173 ret->q=NULL;
174 ret->dmp1=NULL;
175 ret->dmq1=NULL;
176 ret->iqmp=NULL;
177 ret->references=1;
178 ret->_method_mod_n=NULL;
179 ret->_method_mod_p=NULL;
180 ret->_method_mod_q=NULL;
181 ret->blinding=NULL;
182 ret->bignum_data=NULL;
183 ret->flags=ret->meth->flags;
184 CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
185 if ((ret->meth->init != NULL) && !ret->meth->init(ret))
186 {
187#ifndef OPENSSL_NO_ENGINE
188 if (ret->engine)
189 ENGINE_finish(ret->engine);
190#endif
191 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
192 OPENSSL_free(ret);
193 ret=NULL;
194 }
195 return(ret);
196 }
197
198void RSA_free(RSA *r)
199 {
200 int i;
201
202 if (r == NULL) return;
203
204 i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_RSA);
205#ifdef REF_PRINT
206 REF_PRINT("RSA",r);
207#endif
208 if (i > 0) return;
209#ifdef REF_CHECK
210 if (i < 0)
211 {
212 fprintf(stderr,"RSA_free, bad reference count\n");
213 abort();
214 }
215#endif
216
217 if (r->meth->finish)
218 r->meth->finish(r);
219#ifndef OPENSSL_NO_ENGINE
220 if (r->engine)
221 ENGINE_finish(r->engine);
222#endif
223
224 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
225
226 if (r->n != NULL) BN_clear_free(r->n);
227 if (r->e != NULL) BN_clear_free(r->e);
228 if (r->d != NULL) BN_clear_free(r->d);
229 if (r->p != NULL) BN_clear_free(r->p);
230 if (r->q != NULL) BN_clear_free(r->q);
231 if (r->dmp1 != NULL) BN_clear_free(r->dmp1);
232 if (r->dmq1 != NULL) BN_clear_free(r->dmq1);
233 if (r->iqmp != NULL) BN_clear_free(r->iqmp);
234 if (r->blinding != NULL) BN_BLINDING_free(r->blinding);
235 if (r->bignum_data != NULL) OPENSSL_free_locked(r->bignum_data);
236 OPENSSL_free(r);
237 }
238
239int RSA_up_ref(RSA *r)
240 {
241 int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
242#ifdef REF_PRINT
243 REF_PRINT("RSA",r);
244#endif
245#ifdef REF_CHECK
246 if (i < 2)
247 {
248 fprintf(stderr, "RSA_up_ref, bad reference count\n");
249 abort();
250 }
251#endif
252 return ((i > 1) ? 1 : 0);
253 }
254
255int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
256 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
257 {
258 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
259 new_func, dup_func, free_func);
260 }
261
262int RSA_set_ex_data(RSA *r, int idx, void *arg)
263 {
264 return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
265 }
266
267void *RSA_get_ex_data(const RSA *r, int idx)
268 {
269 return(CRYPTO_get_ex_data(&r->ex_data,idx));
270 }
271
272int RSA_size(const RSA *r)
273 {
274 return(BN_num_bytes(r->n));
275 }
276
277int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
278 RSA *rsa, int padding)
279 {
280 return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding));
281 }
282
283int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to,
284 RSA *rsa, int padding)
285 {
286 return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding));
287 }
288
289int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to,
290 RSA *rsa, int padding)
291 {
292 return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding));
293 }
294
295int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
296 RSA *rsa, int padding)
297 {
298 return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding));
299 }
300
301int RSA_flags(const RSA *r)
302 {
303 return((r == NULL)?0:r->meth->flags);
304 }
305
306void RSA_blinding_off(RSA *rsa)
307 {
308 if (rsa->blinding != NULL)
309 {
310 BN_BLINDING_free(rsa->blinding);
311 rsa->blinding=NULL;
312 }
313 rsa->flags &= ~RSA_FLAG_BLINDING;
314 rsa->flags |= RSA_FLAG_NO_BLINDING;
315 }
316
317int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx)
318 {
319 BIGNUM *A,*Ai = NULL;
320 BN_CTX *ctx;
321 int ret=0;
322
323 if (p_ctx == NULL)
324 {
325 if ((ctx=BN_CTX_new()) == NULL) goto err;
326 }
327 else
328 ctx=p_ctx;
329
330 /* XXXXX: Shouldn't this be RSA_blinding_off(rsa)? */
331 if (rsa->blinding != NULL)
332 {
333 BN_BLINDING_free(rsa->blinding);
334 rsa->blinding = NULL;
335 }
336
337 /* NB: similar code appears in setup_blinding (rsa_eay.c);
338 * this should be placed in a new function of its own, but for reasons
339 * of binary compatibility can't */
340
341 BN_CTX_start(ctx);
342 A = BN_CTX_get(ctx);
343 if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL)
344 {
345 /* if PRNG is not properly seeded, resort to secret exponent as unpredictable seed */
346 RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0);
347 if (!BN_pseudo_rand_range(A,rsa->n)) goto err;
348 }
349 else
350 {
351 if (!BN_rand_range(A,rsa->n)) goto err;
352 }
353 if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
354
355 if (!rsa->meth->bn_mod_exp(A,A,rsa->e,rsa->n,ctx,rsa->_method_mod_n))
356 goto err;
357 if ((rsa->blinding=BN_BLINDING_new(A,Ai,rsa->n)) == NULL) goto err;
358 /* to make things thread-safe without excessive locking,
359 * rsa->blinding will be used just by the current thread: */
360 rsa->blinding->thread_id = CRYPTO_thread_id();
361 rsa->flags |= RSA_FLAG_BLINDING;
362 rsa->flags &= ~RSA_FLAG_NO_BLINDING;
363 ret=1;
364err:
365 if (Ai != NULL) BN_free(Ai);
366 BN_CTX_end(ctx);
367 if (ctx != p_ctx) BN_CTX_free(ctx);
368 return(ret);
369 }
370
371int RSA_memory_lock(RSA *r)
372 {
373 int i,j,k,off;
374 char *p;
375 BIGNUM *bn,**t[6],*b;
376 BN_ULONG *ul;
377
378 if (r->d == NULL) return(1);
379 t[0]= &r->d;
380 t[1]= &r->p;
381 t[2]= &r->q;
382 t[3]= &r->dmp1;
383 t[4]= &r->dmq1;
384 t[5]= &r->iqmp;
385 k=sizeof(BIGNUM)*6;
386 off=k/sizeof(BN_ULONG)+1;
387 j=1;
388 for (i=0; i<6; i++)
389 j+= (*t[i])->top;
390 if ((p=OPENSSL_malloc_locked((off+j)*sizeof(BN_ULONG))) == NULL)
391 {
392 RSAerr(RSA_F_MEMORY_LOCK,ERR_R_MALLOC_FAILURE);
393 return(0);
394 }
395 bn=(BIGNUM *)p;
396 ul=(BN_ULONG *)&(p[off]);
397 for (i=0; i<6; i++)
398 {
399 b= *(t[i]);
400 *(t[i])= &(bn[i]);
401 memcpy((char *)&(bn[i]),(char *)b,sizeof(BIGNUM));
402 bn[i].flags=BN_FLG_STATIC_DATA;
403 bn[i].d=ul;
404 memcpy((char *)ul,b->d,sizeof(BN_ULONG)*b->top);
405 ul+=b->top;
406 BN_clear_free(b);
407 }
408
409 /* I should fix this so it can still be done */
410 r->flags&= ~(RSA_FLAG_CACHE_PRIVATE|RSA_FLAG_CACHE_PUBLIC);
411
412 r->bignum_data=p;
413 return(1);
414 }
diff --git a/src/lib/libcrypto/rsa/rsa_none.c b/src/lib/libcrypto/rsa/rsa_none.c
deleted file mode 100644
index e6f3e627ca..0000000000
--- a/src/lib/libcrypto/rsa/rsa_none.c
+++ /dev/null
@@ -1,98 +0,0 @@
1/* crypto/rsa/rsa_none.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/rsa.h>
63#include <openssl/rand.h>
64
65int RSA_padding_add_none(unsigned char *to, int tlen,
66 const unsigned char *from, int flen)
67 {
68 if (flen > tlen)
69 {
70 RSAerr(RSA_F_RSA_PADDING_ADD_NONE,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
71 return(0);
72 }
73
74 if (flen < tlen)
75 {
76 RSAerr(RSA_F_RSA_PADDING_ADD_NONE,RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE);
77 return(0);
78 }
79
80 memcpy(to,from,(unsigned int)flen);
81 return(1);
82 }
83
84int RSA_padding_check_none(unsigned char *to, int tlen,
85 const unsigned char *from, int flen, int num)
86 {
87
88 if (flen > tlen)
89 {
90 RSAerr(RSA_F_RSA_PADDING_CHECK_NONE,RSA_R_DATA_TOO_LARGE);
91 return(-1);
92 }
93
94 memset(to,0,tlen-flen);
95 memcpy(to+tlen-flen,from,flen);
96 return(tlen);
97 }
98
diff --git a/src/lib/libcrypto/rsa/rsa_oaep.c b/src/lib/libcrypto/rsa/rsa_oaep.c
deleted file mode 100644
index d43ecaca63..0000000000
--- a/src/lib/libcrypto/rsa/rsa_oaep.c
+++ /dev/null
@@ -1,212 +0,0 @@
1/* crypto/rsa/rsa_oaep.c */
2/* Written by Ulf Moeller. This software is distributed on an "AS IS"
3 basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. */
4
5/* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
6
7/* See Victor Shoup, "OAEP reconsidered," Nov. 2000,
8 * <URL: http://www.shoup.net/papers/oaep.ps.Z>
9 * for problems with the security proof for the
10 * original OAEP scheme, which EME-OAEP is based on.
11 *
12 * A new proof can be found in E. Fujisaki, T. Okamoto,
13 * D. Pointcheval, J. Stern, "RSA-OEAP is Still Alive!",
14 * Dec. 2000, <URL: http://eprint.iacr.org/2000/061/>.
15 * The new proof has stronger requirements for the
16 * underlying permutation: "partial-one-wayness" instead
17 * of one-wayness. For the RSA function, this is
18 * an equivalent notion.
19 */
20
21
22#if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
23#include <stdio.h>
24#include "cryptlib.h"
25#include <openssl/bn.h>
26#include <openssl/rsa.h>
27#include <openssl/evp.h>
28#include <openssl/rand.h>
29#include <openssl/sha.h>
30
31int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
32 const unsigned char *from, int flen,
33 const unsigned char *param, int plen)
34 {
35 int i, emlen = tlen - 1;
36 unsigned char *db, *seed;
37 unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH];
38
39 if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1)
40 {
41 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,
42 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
43 return 0;
44 }
45
46 if (emlen < 2 * SHA_DIGEST_LENGTH + 1)
47 {
48 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_KEY_SIZE_TOO_SMALL);
49 return 0;
50 }
51
52 dbmask = OPENSSL_malloc(emlen - SHA_DIGEST_LENGTH);
53 if (dbmask == NULL)
54 {
55 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
56 return 0;
57 }
58
59 to[0] = 0;
60 seed = to + 1;
61 db = to + SHA_DIGEST_LENGTH + 1;
62
63 EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL);
64 memset(db + SHA_DIGEST_LENGTH, 0,
65 emlen - flen - 2 * SHA_DIGEST_LENGTH - 1);
66 db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01;
67 memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, (unsigned int) flen);
68 if (RAND_bytes(seed, SHA_DIGEST_LENGTH) <= 0)
69 return 0;
70#ifdef PKCS_TESTVECT
71 memcpy(seed,
72 "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f",
73 20);
74#endif
75
76 PKCS1_MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH,
77 EVP_sha1());
78 for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++)
79 db[i] ^= dbmask[i];
80
81 PKCS1_MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH,
82 EVP_sha1());
83 for (i = 0; i < SHA_DIGEST_LENGTH; i++)
84 seed[i] ^= seedmask[i];
85
86 OPENSSL_free(dbmask);
87 return 1;
88 }
89
90int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
91 const unsigned char *from, int flen, int num,
92 const unsigned char *param, int plen)
93 {
94 int i, dblen, mlen = -1;
95 const unsigned char *maskeddb;
96 int lzero;
97 unsigned char *db = NULL, seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH];
98 int bad = 0;
99
100 if (--num < 2 * SHA_DIGEST_LENGTH + 1)
101 /* 'num' is the length of the modulus, i.e. does not depend on the
102 * particular ciphertext. */
103 goto decoding_err;
104
105 lzero = num - flen;
106 if (lzero < 0)
107 {
108 /* lzero == -1 */
109
110 /* signalling this error immediately after detection might allow
111 * for side-channel attacks (e.g. timing if 'plen' is huge
112 * -- cf. James H. Manger, "A Chosen Ciphertext Attack on RSA Optimal
113 * Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001),
114 * so we use a 'bad' flag */
115 bad = 1;
116 lzero = 0;
117 }
118 maskeddb = from - lzero + SHA_DIGEST_LENGTH;
119
120 dblen = num - SHA_DIGEST_LENGTH;
121 db = OPENSSL_malloc(dblen);
122 if (db == NULL)
123 {
124 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
125 return -1;
126 }
127
128 PKCS1_MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen, EVP_sha1());
129 for (i = lzero; i < SHA_DIGEST_LENGTH; i++)
130 seed[i] ^= from[i - lzero];
131
132 PKCS1_MGF1(db, dblen, seed, SHA_DIGEST_LENGTH, EVP_sha1());
133 for (i = 0; i < dblen; i++)
134 db[i] ^= maskeddb[i];
135
136 EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL);
137
138 if (memcmp(db, phash, SHA_DIGEST_LENGTH) != 0 || bad)
139 goto decoding_err;
140 else
141 {
142 for (i = SHA_DIGEST_LENGTH; i < dblen; i++)
143 if (db[i] != 0x00)
144 break;
145 if (db[i] != 0x01 || i++ >= dblen)
146 goto decoding_err;
147 else
148 {
149 /* everything looks OK */
150
151 mlen = dblen - i;
152 if (tlen < mlen)
153 {
154 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
155 mlen = -1;
156 }
157 else
158 memcpy(to, db + i, mlen);
159 }
160 }
161 OPENSSL_free(db);
162 return mlen;
163
164decoding_err:
165 /* to avoid chosen ciphertext attacks, the error message should not reveal
166 * which kind of decoding error happened */
167 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR);
168 if (db != NULL) OPENSSL_free(db);
169 return -1;
170 }
171
172int PKCS1_MGF1(unsigned char *mask, long len,
173 const unsigned char *seed, long seedlen, const EVP_MD *dgst)
174 {
175 long i, outlen = 0;
176 unsigned char cnt[4];
177 EVP_MD_CTX c;
178 unsigned char md[EVP_MAX_MD_SIZE];
179 int mdlen;
180
181 EVP_MD_CTX_init(&c);
182 mdlen = EVP_MD_size(dgst);
183 for (i = 0; outlen < len; i++)
184 {
185 cnt[0] = (unsigned char)((i >> 24) & 255);
186 cnt[1] = (unsigned char)((i >> 16) & 255);
187 cnt[2] = (unsigned char)((i >> 8)) & 255;
188 cnt[3] = (unsigned char)(i & 255);
189 EVP_DigestInit_ex(&c,dgst, NULL);
190 EVP_DigestUpdate(&c, seed, seedlen);
191 EVP_DigestUpdate(&c, cnt, 4);
192 if (outlen + mdlen <= len)
193 {
194 EVP_DigestFinal_ex(&c, mask + outlen, NULL);
195 outlen += mdlen;
196 }
197 else
198 {
199 EVP_DigestFinal_ex(&c, md, NULL);
200 memcpy(mask + outlen, md, len - outlen);
201 outlen = len;
202 }
203 }
204 EVP_MD_CTX_cleanup(&c);
205 return 0;
206 }
207
208int MGF1(unsigned char *mask, long len, const unsigned char *seed, long seedlen)
209 {
210 return PKCS1_MGF1(mask, len, seed, seedlen, EVP_sha1());
211 }
212#endif
diff --git a/src/lib/libcrypto/rsa/rsa_pk1.c b/src/lib/libcrypto/rsa/rsa_pk1.c
deleted file mode 100644
index 8560755f1d..0000000000
--- a/src/lib/libcrypto/rsa/rsa_pk1.c
+++ /dev/null
@@ -1,224 +0,0 @@
1/* crypto/rsa/rsa_pk1.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/rsa.h>
63#include <openssl/rand.h>
64
65int RSA_padding_add_PKCS1_type_1(unsigned char *to, int tlen,
66 const unsigned char *from, int flen)
67 {
68 int j;
69 unsigned char *p;
70
71 if (flen > (tlen-RSA_PKCS1_PADDING_SIZE))
72 {
73 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
74 return(0);
75 }
76
77 p=(unsigned char *)to;
78
79 *(p++)=0;
80 *(p++)=1; /* Private Key BT (Block Type) */
81
82 /* pad out with 0xff data */
83 j=tlen-3-flen;
84 memset(p,0xff,j);
85 p+=j;
86 *(p++)='\0';
87 memcpy(p,from,(unsigned int)flen);
88 return(1);
89 }
90
91int RSA_padding_check_PKCS1_type_1(unsigned char *to, int tlen,
92 const unsigned char *from, int flen, int num)
93 {
94 int i,j;
95 const unsigned char *p;
96
97 p=from;
98 if ((num != (flen+1)) || (*(p++) != 01))
99 {
100 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BLOCK_TYPE_IS_NOT_01);
101 return(-1);
102 }
103
104 /* scan over padding data */
105 j=flen-1; /* one for type. */
106 for (i=0; i<j; i++)
107 {
108 if (*p != 0xff) /* should decrypt to 0xff */
109 {
110 if (*p == 0)
111 { p++; break; }
112 else {
113 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BAD_FIXED_HEADER_DECRYPT);
114 return(-1);
115 }
116 }
117 p++;
118 }
119
120 if (i == j)
121 {
122 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_NULL_BEFORE_BLOCK_MISSING);
123 return(-1);
124 }
125
126 if (i < 8)
127 {
128 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_BAD_PAD_BYTE_COUNT);
129 return(-1);
130 }
131 i++; /* Skip over the '\0' */
132 j-=i;
133 if (j > tlen)
134 {
135 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,RSA_R_DATA_TOO_LARGE);
136 return(-1);
137 }
138 memcpy(to,p,(unsigned int)j);
139
140 return(j);
141 }
142
143int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen,
144 const unsigned char *from, int flen)
145 {
146 int i,j;
147 unsigned char *p;
148
149 if (flen > (tlen-11))
150 {
151 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
152 return(0);
153 }
154
155 p=(unsigned char *)to;
156
157 *(p++)=0;
158 *(p++)=2; /* Public Key BT (Block Type) */
159
160 /* pad out with non-zero random data */
161 j=tlen-3-flen;
162
163 if (RAND_bytes(p,j) <= 0)
164 return(0);
165 for (i=0; i<j; i++)
166 {
167 if (*p == '\0')
168 do {
169 if (RAND_bytes(p,1) <= 0)
170 return(0);
171 } while (*p == '\0');
172 p++;
173 }
174
175 *(p++)='\0';
176
177 memcpy(p,from,(unsigned int)flen);
178 return(1);
179 }
180
181int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen,
182 const unsigned char *from, int flen, int num)
183 {
184 int i,j;
185 const unsigned char *p;
186
187 p=from;
188 if ((num != (flen+1)) || (*(p++) != 02))
189 {
190 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_BLOCK_TYPE_IS_NOT_02);
191 return(-1);
192 }
193#ifdef PKCS1_CHECK
194 return(num-11);
195#endif
196
197 /* scan over padding data */
198 j=flen-1; /* one for type. */
199 for (i=0; i<j; i++)
200 if (*(p++) == 0) break;
201
202 if (i == j)
203 {
204 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_NULL_BEFORE_BLOCK_MISSING);
205 return(-1);
206 }
207
208 if (i < 8)
209 {
210 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_BAD_PAD_BYTE_COUNT);
211 return(-1);
212 }
213 i++; /* Skip over the '\0' */
214 j-=i;
215 if (j > tlen)
216 {
217 RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,RSA_R_DATA_TOO_LARGE);
218 return(-1);
219 }
220 memcpy(to,p,(unsigned int)j);
221
222 return(j);
223 }
224
diff --git a/src/lib/libcrypto/rsa/rsa_pss.c b/src/lib/libcrypto/rsa/rsa_pss.c
deleted file mode 100644
index 2815628f5f..0000000000
--- a/src/lib/libcrypto/rsa/rsa_pss.c
+++ /dev/null
@@ -1,261 +0,0 @@
1/* rsa_pss.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3 * project 2005.
4 */
5/* ====================================================================
6 * Copyright (c) 2005 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/rsa.h>
63#include <openssl/evp.h>
64#include <openssl/rand.h>
65#include <openssl/sha.h>
66
67const static unsigned char zeroes[] = {0,0,0,0,0,0,0,0};
68
69int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
70 const EVP_MD *Hash, const unsigned char *EM, int sLen)
71 {
72 int i;
73 int ret = 0;
74 int hLen, maskedDBLen, MSBits, emLen;
75 const unsigned char *H;
76 unsigned char *DB = NULL;
77 EVP_MD_CTX ctx;
78 unsigned char H_[EVP_MAX_MD_SIZE];
79
80 hLen = EVP_MD_size(Hash);
81 /*
82 * Negative sLen has special meanings:
83 * -1 sLen == hLen
84 * -2 salt length is autorecovered from signature
85 * -N reserved
86 */
87 if (sLen == -1) sLen = hLen;
88 else if (sLen == -2) sLen = -2;
89 else if (sLen < -2)
90 {
91 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED);
92 goto err;
93 }
94
95 MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
96 emLen = RSA_size(rsa);
97 if (EM[0] & (0xFF << MSBits))
98 {
99 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_FIRST_OCTET_INVALID);
100 goto err;
101 }
102 if (MSBits == 0)
103 {
104 EM++;
105 emLen--;
106 }
107 if (emLen < (hLen + sLen + 2)) /* sLen can be small negative */
108 {
109 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_DATA_TOO_LARGE);
110 goto err;
111 }
112 if (EM[emLen - 1] != 0xbc)
113 {
114 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_LAST_OCTET_INVALID);
115 goto err;
116 }
117 maskedDBLen = emLen - hLen - 1;
118 H = EM + maskedDBLen;
119 DB = OPENSSL_malloc(maskedDBLen);
120 if (!DB)
121 {
122 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, ERR_R_MALLOC_FAILURE);
123 goto err;
124 }
125 PKCS1_MGF1(DB, maskedDBLen, H, hLen, Hash);
126 for (i = 0; i < maskedDBLen; i++)
127 DB[i] ^= EM[i];
128 if (MSBits)
129 DB[0] &= 0xFF >> (8 - MSBits);
130 for (i = 0; DB[i] == 0 && i < (maskedDBLen-1); i++) ;
131 if (DB[i++] != 0x1)
132 {
133 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_RECOVERY_FAILED);
134 goto err;
135 }
136 if (sLen >= 0 && (maskedDBLen - i) != sLen)
137 {
138 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED);
139 goto err;
140 }
141 EVP_MD_CTX_init(&ctx);
142 EVP_DigestInit_ex(&ctx, Hash, NULL);
143 EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes);
144 EVP_DigestUpdate(&ctx, mHash, hLen);
145 if (maskedDBLen - i)
146 EVP_DigestUpdate(&ctx, DB + i, maskedDBLen - i);
147 EVP_DigestFinal(&ctx, H_, NULL);
148 EVP_MD_CTX_cleanup(&ctx);
149 if (memcmp(H_, H, hLen))
150 {
151 RSAerr(RSA_F_RSA_VERIFY_PKCS1_PSS, RSA_R_BAD_SIGNATURE);
152 ret = 0;
153 }
154 else
155 ret = 1;
156
157 err:
158 if (DB)
159 OPENSSL_free(DB);
160
161 return ret;
162
163 }
164
165int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
166 const unsigned char *mHash,
167 const EVP_MD *Hash, int sLen)
168 {
169 int i;
170 int ret = 0;
171 int hLen, maskedDBLen, MSBits, emLen;
172 unsigned char *H, *salt = NULL, *p;
173 EVP_MD_CTX ctx;
174
175 hLen = EVP_MD_size(Hash);
176 /*
177 * Negative sLen has special meanings:
178 * -1 sLen == hLen
179 * -2 salt length is maximized
180 * -N reserved
181 */
182 if (sLen == -1) sLen = hLen;
183 else if (sLen == -2) sLen = -2;
184 else if (sLen < -2)
185 {
186 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS, RSA_R_SLEN_CHECK_FAILED);
187 goto err;
188 }
189
190 MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
191 emLen = RSA_size(rsa);
192 if (MSBits == 0)
193 {
194 *EM++ = 0;
195 emLen--;
196 }
197 if (sLen == -2)
198 {
199 sLen = emLen - hLen - 2;
200 }
201 else if (emLen < (hLen + sLen + 2))
202 {
203 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS,
204 RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
205 goto err;
206 }
207 if (sLen > 0)
208 {
209 salt = OPENSSL_malloc(sLen);
210 if (!salt)
211 {
212 RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_PSS,
213 ERR_R_MALLOC_FAILURE);
214 goto err;
215 }
216 if (!RAND_bytes(salt, sLen))
217 goto err;
218 }
219 maskedDBLen = emLen - hLen - 1;
220 H = EM + maskedDBLen;
221 EVP_MD_CTX_init(&ctx);
222 EVP_DigestInit_ex(&ctx, Hash, NULL);
223 EVP_DigestUpdate(&ctx, zeroes, sizeof zeroes);
224 EVP_DigestUpdate(&ctx, mHash, hLen);
225 if (sLen)
226 EVP_DigestUpdate(&ctx, salt, sLen);
227 EVP_DigestFinal(&ctx, H, NULL);
228 EVP_MD_CTX_cleanup(&ctx);
229
230 /* Generate dbMask in place then perform XOR on it */
231 PKCS1_MGF1(EM, maskedDBLen, H, hLen, Hash);
232
233 p = EM;
234
235 /* Initial PS XORs with all zeroes which is a NOP so just update
236 * pointer. Note from a test above this value is guaranteed to
237 * be non-negative.
238 */
239 p += emLen - sLen - hLen - 2;
240 *p++ ^= 0x1;
241 if (sLen > 0)
242 {
243 for (i = 0; i < sLen; i++)
244 *p++ ^= salt[i];
245 }
246 if (MSBits)
247 EM[0] &= 0xFF >> (8 - MSBits);
248
249 /* H is already in place so just set final 0xbc */
250
251 EM[emLen - 1] = 0xbc;
252
253 ret = 1;
254
255 err:
256 if (salt)
257 OPENSSL_free(salt);
258
259 return ret;
260
261 }
diff --git a/src/lib/libcrypto/rsa/rsa_saos.c b/src/lib/libcrypto/rsa/rsa_saos.c
deleted file mode 100644
index 24fc94835e..0000000000
--- a/src/lib/libcrypto/rsa/rsa_saos.c
+++ /dev/null
@@ -1,149 +0,0 @@
1/* crypto/rsa/rsa_saos.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/rsa.h>
63#include <openssl/objects.h>
64#include <openssl/x509.h>
65
66int RSA_sign_ASN1_OCTET_STRING(int type,
67 const unsigned char *m, unsigned int m_len,
68 unsigned char *sigret, unsigned int *siglen, RSA *rsa)
69 {
70 ASN1_OCTET_STRING sig;
71 int i,j,ret=1;
72 unsigned char *p,*s;
73
74 sig.type=V_ASN1_OCTET_STRING;
75 sig.length=m_len;
76 sig.data=(unsigned char *)m;
77
78 i=i2d_ASN1_OCTET_STRING(&sig,NULL);
79 j=RSA_size(rsa);
80 if (i > (j-RSA_PKCS1_PADDING_SIZE))
81 {
82 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
83 return(0);
84 }
85 s=(unsigned char *)OPENSSL_malloc((unsigned int)j+1);
86 if (s == NULL)
87 {
88 RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE);
89 return(0);
90 }
91 p=s;
92 i2d_ASN1_OCTET_STRING(&sig,&p);
93 i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING);
94 if (i <= 0)
95 ret=0;
96 else
97 *siglen=i;
98
99 OPENSSL_cleanse(s,(unsigned int)j+1);
100 OPENSSL_free(s);
101 return(ret);
102 }
103
104int RSA_verify_ASN1_OCTET_STRING(int dtype,
105 const unsigned char *m,
106 unsigned int m_len, unsigned char *sigbuf, unsigned int siglen,
107 RSA *rsa)
108 {
109 int i,ret=0;
110 unsigned char *p,*s;
111 ASN1_OCTET_STRING *sig=NULL;
112
113 if (siglen != (unsigned int)RSA_size(rsa))
114 {
115 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_WRONG_SIGNATURE_LENGTH);
116 return(0);
117 }
118
119 s=(unsigned char *)OPENSSL_malloc((unsigned int)siglen);
120 if (s == NULL)
121 {
122 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,ERR_R_MALLOC_FAILURE);
123 goto err;
124 }
125 i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);
126
127 if (i <= 0) goto err;
128
129 p=s;
130 sig=d2i_ASN1_OCTET_STRING(NULL,&p,(long)i);
131 if (sig == NULL) goto err;
132
133 if ( ((unsigned int)sig->length != m_len) ||
134 (memcmp(m,sig->data,m_len) != 0))
135 {
136 RSAerr(RSA_F_RSA_VERIFY_ASN1_OCTET_STRING,RSA_R_BAD_SIGNATURE);
137 }
138 else
139 ret=1;
140err:
141 if (sig != NULL) M_ASN1_OCTET_STRING_free(sig);
142 if (s != NULL)
143 {
144 OPENSSL_cleanse(s,(unsigned int)siglen);
145 OPENSSL_free(s);
146 }
147 return(ret);
148 }
149
diff --git a/src/lib/libcrypto/rsa/rsa_sign.c b/src/lib/libcrypto/rsa/rsa_sign.c
deleted file mode 100644
index db86f1ac58..0000000000
--- a/src/lib/libcrypto/rsa/rsa_sign.c
+++ /dev/null
@@ -1,249 +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, const 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, *tmps = NULL;
76 const unsigned char *s = NULL;
77 X509_ALGOR algor;
78 ASN1_OCTET_STRING digest;
79 if((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_sign)
80 {
81 return rsa->meth->rsa_sign(type, m, m_len,
82 sigret, siglen, rsa);
83 }
84 /* Special case: SSL signature, just check the length */
85 if(type == NID_md5_sha1) {
86 if(m_len != SSL_SIG_LENGTH) {
87 RSAerr(RSA_F_RSA_SIGN,RSA_R_INVALID_MESSAGE_LENGTH);
88 return(0);
89 }
90 i = SSL_SIG_LENGTH;
91 s = m;
92 } else {
93 sig.algor= &algor;
94 sig.algor->algorithm=OBJ_nid2obj(type);
95 if (sig.algor->algorithm == NULL)
96 {
97 RSAerr(RSA_F_RSA_SIGN,RSA_R_UNKNOWN_ALGORITHM_TYPE);
98 return(0);
99 }
100 if (sig.algor->algorithm->length == 0)
101 {
102 RSAerr(RSA_F_RSA_SIGN,RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
103 return(0);
104 }
105 parameter.type=V_ASN1_NULL;
106 parameter.value.ptr=NULL;
107 sig.algor->parameter= &parameter;
108
109 sig.digest= &digest;
110 sig.digest->data=(unsigned char *)m; /* TMP UGLY CAST */
111 sig.digest->length=m_len;
112
113 i=i2d_X509_SIG(&sig,NULL);
114 }
115 j=RSA_size(rsa);
116 if (i > (j-RSA_PKCS1_PADDING_SIZE))
117 {
118 RSAerr(RSA_F_RSA_SIGN,RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
119 return(0);
120 }
121 if(type != NID_md5_sha1) {
122 tmps=(unsigned char *)OPENSSL_malloc((unsigned int)j+1);
123 if (tmps == NULL)
124 {
125 RSAerr(RSA_F_RSA_SIGN,ERR_R_MALLOC_FAILURE);
126 return(0);
127 }
128 p=tmps;
129 i2d_X509_SIG(&sig,&p);
130 s=tmps;
131 }
132 i=RSA_private_encrypt(i,s,sigret,rsa,RSA_PKCS1_PADDING);
133 if (i <= 0)
134 ret=0;
135 else
136 *siglen=i;
137
138 if(type != NID_md5_sha1) {
139 OPENSSL_cleanse(tmps,(unsigned int)j+1);
140 OPENSSL_free(tmps);
141 }
142 return(ret);
143 }
144
145int RSA_verify(int dtype, const unsigned char *m, unsigned int m_len,
146 unsigned char *sigbuf, unsigned int siglen, RSA *rsa)
147 {
148 int i,ret=0,sigtype;
149 unsigned char *p,*s;
150 X509_SIG *sig=NULL;
151
152 if (siglen != (unsigned int)RSA_size(rsa))
153 {
154 RSAerr(RSA_F_RSA_VERIFY,RSA_R_WRONG_SIGNATURE_LENGTH);
155 return(0);
156 }
157
158 if((rsa->flags & RSA_FLAG_SIGN_VER) && rsa->meth->rsa_verify)
159 {
160 return rsa->meth->rsa_verify(dtype, m, m_len,
161 sigbuf, siglen, rsa);
162 }
163
164 s=(unsigned char *)OPENSSL_malloc((unsigned int)siglen);
165 if (s == NULL)
166 {
167 RSAerr(RSA_F_RSA_VERIFY,ERR_R_MALLOC_FAILURE);
168 goto err;
169 }
170 if((dtype == NID_md5_sha1) && (m_len != SSL_SIG_LENGTH) ) {
171 RSAerr(RSA_F_RSA_VERIFY,RSA_R_INVALID_MESSAGE_LENGTH);
172 goto err;
173 }
174 i=RSA_public_decrypt((int)siglen,sigbuf,s,rsa,RSA_PKCS1_PADDING);
175
176 if (i <= 0) goto err;
177
178 /* Special case: SSL signature */
179 if(dtype == NID_md5_sha1) {
180 if((i != SSL_SIG_LENGTH) || memcmp(s, m, SSL_SIG_LENGTH))
181 RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
182 else ret = 1;
183 } else {
184 p=s;
185 sig=d2i_X509_SIG(NULL,&p,(long)i);
186
187 if (sig == NULL) goto err;
188
189 /* Excess data can be used to create forgeries */
190 if(p != s+i)
191 {
192 RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
193 goto err;
194 }
195
196 /* Parameters to the signature algorithm can also be used to
197 create forgeries */
198 if(sig->algor->parameter
199 && ASN1_TYPE_get(sig->algor->parameter) != V_ASN1_NULL)
200 {
201 RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
202 goto err;
203 }
204
205 sigtype=OBJ_obj2nid(sig->algor->algorithm);
206
207
208 #ifdef RSA_DEBUG
209 /* put a backward compatibility flag in EAY */
210 fprintf(stderr,"in(%s) expect(%s)\n",OBJ_nid2ln(sigtype),
211 OBJ_nid2ln(dtype));
212 #endif
213 if (sigtype != dtype)
214 {
215 if (((dtype == NID_md5) &&
216 (sigtype == NID_md5WithRSAEncryption)) ||
217 ((dtype == NID_md2) &&
218 (sigtype == NID_md2WithRSAEncryption)))
219 {
220 /* ok, we will let it through */
221#if !defined(OPENSSL_NO_STDIO) && !defined(OPENSSL_SYS_WIN16)
222 fprintf(stderr,"signature has problems, re-make with post SSLeay045\n");
223#endif
224 }
225 else
226 {
227 RSAerr(RSA_F_RSA_VERIFY,
228 RSA_R_ALGORITHM_MISMATCH);
229 goto err;
230 }
231 }
232 if ( ((unsigned int)sig->digest->length != m_len) ||
233 (memcmp(m,sig->digest->data,m_len) != 0))
234 {
235 RSAerr(RSA_F_RSA_VERIFY,RSA_R_BAD_SIGNATURE);
236 }
237 else
238 ret=1;
239 }
240err:
241 if (sig != NULL) X509_SIG_free(sig);
242 if (s != NULL)
243 {
244 OPENSSL_cleanse(s,(unsigned int)siglen);
245 OPENSSL_free(s);
246 }
247 return(ret);
248 }
249
diff --git a/src/lib/libcrypto/rsa/rsa_ssl.c b/src/lib/libcrypto/rsa/rsa_ssl.c
deleted file mode 100644
index ea72629494..0000000000
--- a/src/lib/libcrypto/rsa/rsa_ssl.c
+++ /dev/null
@@ -1,154 +0,0 @@
1/* crypto/rsa/rsa_ssl.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/rsa.h>
63#include <openssl/rand.h>
64
65int RSA_padding_add_SSLv23(unsigned char *to, int tlen,
66 const unsigned char *from, int flen)
67 {
68 int i,j;
69 unsigned char *p;
70
71 if (flen > (tlen-11))
72 {
73 RSAerr(RSA_F_RSA_PADDING_ADD_SSLV23,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
74 return(0);
75 }
76
77 p=(unsigned char *)to;
78
79 *(p++)=0;
80 *(p++)=2; /* Public Key BT (Block Type) */
81
82 /* pad out with non-zero random data */
83 j=tlen-3-8-flen;
84
85 if (RAND_bytes(p,j) <= 0)
86 return(0);
87 for (i=0; i<j; i++)
88 {
89 if (*p == '\0')
90 do {
91 if (RAND_bytes(p,1) <= 0)
92 return(0);
93 } while (*p == '\0');
94 p++;
95 }
96
97 memset(p,3,8);
98 p+=8;
99 *(p++)='\0';
100
101 memcpy(p,from,(unsigned int)flen);
102 return(1);
103 }
104
105int RSA_padding_check_SSLv23(unsigned char *to, int tlen,
106 const unsigned char *from, int flen, int num)
107 {
108 int i,j,k;
109 const unsigned char *p;
110
111 p=from;
112 if (flen < 10)
113 {
114 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_DATA_TOO_SMALL);
115 return(-1);
116 }
117 if ((num != (flen+1)) || (*(p++) != 02))
118 {
119 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_BLOCK_TYPE_IS_NOT_02);
120 return(-1);
121 }
122
123 /* scan over padding data */
124 j=flen-1; /* one for type */
125 for (i=0; i<j; i++)
126 if (*(p++) == 0) break;
127
128 if ((i == j) || (i < 8))
129 {
130 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_NULL_BEFORE_BLOCK_MISSING);
131 return(-1);
132 }
133 for (k= -8; k<0; k++)
134 {
135 if (p[k] != 0x03) break;
136 }
137 if (k == -1)
138 {
139 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_SSLV3_ROLLBACK_ATTACK);
140 return(-1);
141 }
142
143 i++; /* Skip over the '\0' */
144 j-=i;
145 if (j > tlen)
146 {
147 RSAerr(RSA_F_RSA_PADDING_CHECK_SSLV23,RSA_R_DATA_TOO_LARGE);
148 return(-1);
149 }
150 memcpy(to,p,(unsigned int)j);
151
152 return(j);
153 }
154
diff --git a/src/lib/libcrypto/rsa/rsa_x931.c b/src/lib/libcrypto/rsa/rsa_x931.c
deleted file mode 100644
index df3c45f802..0000000000
--- a/src/lib/libcrypto/rsa/rsa_x931.c
+++ /dev/null
@@ -1,177 +0,0 @@
1/* rsa_x931.c */
2/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3 * project 2005.
4 */
5/* ====================================================================
6 * Copyright (c) 2005 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stdio.h>
60#include "cryptlib.h"
61#include <openssl/bn.h>
62#include <openssl/rsa.h>
63#include <openssl/rand.h>
64#include <openssl/objects.h>
65
66int RSA_padding_add_X931(unsigned char *to, int tlen,
67 const unsigned char *from, int flen)
68 {
69 int j;
70 unsigned char *p;
71
72 /* Absolute minimum amount of padding is 1 header nibble, 1 padding
73 * nibble and 2 trailer bytes: but 1 hash if is already in 'from'.
74 */
75
76 j = tlen - flen - 2;
77
78 if (j < 0)
79 {
80 RSAerr(RSA_F_RSA_PADDING_ADD_X931,RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
81 return -1;
82 }
83
84 p=(unsigned char *)to;
85
86 /* If no padding start and end nibbles are in one byte */
87 if (j == 0)
88 *p++ = 0x6A;
89 else
90 {
91 *p++ = 0x6B;
92 if (j > 1)
93 {
94 memset(p, 0xBB, j - 1);
95 p += j - 1;
96 }
97 *p++ = 0xBA;
98 }
99 memcpy(p,from,(unsigned int)flen);
100 p += flen;
101 *p = 0xCC;
102 return(1);
103 }
104
105int RSA_padding_check_X931(unsigned char *to, int tlen,
106 const unsigned char *from, int flen, int num)
107 {
108 int i,j;
109 const unsigned char *p;
110
111 p=from;
112 if ((num != flen) || ((*p != 0x6A) && (*p != 0x6B)))
113 {
114 RSAerr(RSA_F_RSA_PADDING_CHECK_X931,RSA_R_INVALID_HEADER);
115 return -1;
116 }
117
118 if (*p++ == 0x6B)
119 {
120 j=flen-3;
121 for (i = 0; i < j; i++)
122 {
123 unsigned char c = *p++;
124 if (c == 0xBA)
125 break;
126 if (c != 0xBB)
127 {
128 RSAerr(RSA_F_RSA_PADDING_CHECK_X931,
129 RSA_R_INVALID_PADDING);
130 return -1;
131 }
132 }
133
134 j -= i;
135
136 if (i == 0)
137 {
138 RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_PADDING);
139 return -1;
140 }
141
142 }
143 else j = flen - 2;
144
145 if (p[j] != 0xCC)
146 {
147 RSAerr(RSA_F_RSA_PADDING_CHECK_X931, RSA_R_INVALID_TRAILER);
148 return -1;
149 }
150
151 memcpy(to,p,(unsigned int)j);
152
153 return(j);
154 }
155
156/* Translate between X931 hash ids and NIDs */
157
158int RSA_X931_hash_id(int nid)
159 {
160 switch (nid)
161 {
162 case NID_sha1:
163 return 0x33;
164
165 case NID_sha256:
166 return 0x34;
167
168 case NID_sha384:
169 return 0x36;
170
171 case NID_sha512:
172 return 0x35;
173
174 }
175 return -1;
176 }
177