1 2 /* This file has been explicitly broken by ryker for OpenBSD, July 3 * 1, 1998. In spite of the title, there is no implementation of the 4 * RSA algorithm left in this file. All these routines will return an 5 * error and fail when called. They exist as stubs and can be 6 * ressurected from the bit bucket by someone in the free world once 7 * the RSA algorithm is no longer subject to patent problems. Eric 8 * Young's original copyright is below. 9 */ 10 11 /* crypto/rsa/rsa_eay.c */ 12 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 13 * All rights reserved. 14 * 15 * This package is an SSL implementation written 16 * by Eric Young (eay@cryptsoft.com). 17 * The implementation was written so as to conform with Netscapes SSL. 18 * 19 * This library is free for commercial and non-commercial use as long as 20 * the following conditions are aheared to. The following conditions 21 * apply to all code found in this distribution, be it the RC4, RSA, 22 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 23 * included with this distribution is covered by the same copyright terms 24 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 25 * 26 * Copyright remains Eric Young's, and as such any Copyright notices in 27 * the code are not to be removed. 28 * If this package is used in a product, Eric Young should be given attribution 29 * as the author of the parts of the library used. 30 * This can be in the form of a textual message at program startup or 31 * in documentation (online or textual) provided with the package. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 3. All advertising materials mentioning features or use of this software 42 * must display the following acknowledgement: 43 * "This product includes cryptographic software written by 44 * Eric Young (eay@cryptsoft.com)" 45 * The word 'cryptographic' can be left out if the rouines from the library 46 * being used are not cryptographic related :-). 47 * 4. If you include any Windows specific code (or a derivative thereof) from 48 * the apps directory (application code) you must include an acknowledgement: 49 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 50 * 51 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 * 63 * The licence and distribution terms for any publically available version or 64 * derivative of this code cannot be changed. i.e. this code cannot simply be 65 * copied and put under another distribution licence 66 * [including the GNU Public Licence.] 67 */ 68 69 #include <stdio.h> 70 #include "cryptlib.h" 71 #include "bn.h" 72 #include "rsa.h" 73 #include "rand.h" 74 75 #ifndef NOPROTO 76 static int RSA_eay_public_encrypt(int flen, unsigned char *from, 77 unsigned char *to, RSA *rsa,int padding); 78 static int RSA_eay_private_encrypt(int flen, unsigned char *from, 79 unsigned char *to, RSA *rsa,int padding); 80 static int RSA_eay_public_decrypt(int flen, unsigned char *from, 81 unsigned char *to, RSA *rsa,int padding); 82 static int RSA_eay_private_decrypt(int flen, unsigned char *from, 83 unsigned char *to, RSA *rsa,int padding); 84 static int RSA_eay_mod_exp(BIGNUM *r0, BIGNUM *i, RSA *rsa); 85 static int RSA_eay_init(RSA *rsa); 86 static int RSA_eay_finish(RSA *rsa); 87 #else 88 static int RSA_eay_public_encrypt(); 89 static int RSA_eay_private_encrypt(); 90 static int RSA_eay_public_decrypt(); 91 static int RSA_eay_private_decrypt(); 92 static int RSA_eay_mod_exp(); 93 static int RSA_eay_init(); 94 static int RSA_eay_finish(); 95 #endif 96 97 static RSA_METHOD rsa_pkcs1_eay_meth={ 98 "Eric Young's PKCS#1 RSA", 99 RSA_eay_public_encrypt, 100 RSA_eay_public_decrypt, 101 RSA_eay_private_encrypt, 102 RSA_eay_private_decrypt, 103 RSA_eay_mod_exp, 104 BN_mod_exp_mont, 105 RSA_eay_init, 106 RSA_eay_finish, 107 0, 108 NULL, 109 }; 110 111 RSA_METHOD *RSA_PKCS1_SSLeay() 112 { 113 return(&rsa_pkcs1_eay_meth); 114 } 115 116 static int RSA_eay_public_encrypt(flen, from, to, rsa, padding) 117 int flen; 118 unsigned char *from; 119 unsigned char *to; 120 RSA *rsa; 121 int padding; 122 { 123 BIGNUM *f=NULL,*ret=NULL; 124 int i,j,k,num=0,r= -1; 125 unsigned char *buf=NULL; 126 BN_CTX *ctx=NULL; 127 128 /* Body of this routine removed for OpenBSD - will return 129 * when the RSA patent expires 130 */ 131 132 err: 133 if (ctx != NULL) BN_CTX_free(ctx); 134 if (f != NULL) BN_free(f); 135 if (ret != NULL) BN_free(ret); 136 if (buf != NULL) 137 { 138 memset(buf,0,num); 139 Free(buf); 140 } 141 return(r); 142 } 143 144 static int RSA_eay_private_encrypt(flen, from, to, rsa, padding) 145 int flen; 146 unsigned char *from; 147 unsigned char *to; 148 RSA *rsa; 149 int padding; 150 { 151 BIGNUM *f=NULL,*ret=NULL; 152 int i,j,k,num=0,r= -1; 153 unsigned char *buf=NULL; 154 BN_CTX *ctx=NULL; 155 156 /* Body of this routine removed for OpenBSD - will return 157 * when the RSA patent expires 158 */ 159 160 err: 161 if (ctx != NULL) BN_CTX_free(ctx); 162 if (ret != NULL) BN_free(ret); 163 if (f != NULL) BN_free(f); 164 if (buf != NULL) 165 { 166 memset(buf,0,num); 167 Free(buf); 168 } 169 return(r); 170 } 171 172 static int RSA_eay_private_decrypt(flen, from, to, rsa,padding) 173 int flen; 174 unsigned char *from; 175 unsigned char *to; 176 RSA *rsa; 177 int padding; 178 { 179 BIGNUM *f=NULL,*ret=NULL; 180 int j,num=0,r= -1; 181 unsigned char *p; 182 unsigned char *buf=NULL; 183 BN_CTX *ctx=NULL; 184 185 /* Body of this routine removed for OpenBSD - will return 186 * when the RSA patent expires 187 */ 188 189 err: 190 if (ctx != NULL) BN_CTX_free(ctx); 191 if (f != NULL) BN_free(f); 192 if (ret != NULL) BN_free(ret); 193 if (buf != NULL) 194 { 195 memset(buf,0,num); 196 Free(buf); 197 } 198 return(r); 199 } 200 201 static int RSA_eay_public_decrypt(flen, from, to, rsa, padding) 202 int flen; 203 unsigned char *from; 204 unsigned char *to; 205 RSA *rsa; 206 int padding; 207 { 208 BIGNUM *f=NULL,*ret=NULL; 209 int i,num=0,r= -1; 210 unsigned char *p; 211 unsigned char *buf=NULL; 212 BN_CTX *ctx=NULL; 213 214 215 /* Body of this routine removed for OpenBSD - will return 216 * when the RSA patent expires 217 */ 218 219 err: 220 if (ctx != NULL) BN_CTX_free(ctx); 221 if (f != NULL) BN_free(f); 222 if (ret != NULL) BN_free(ret); 223 if (buf != NULL) 224 { 225 memset(buf,0,num); 226 Free(buf); 227 } 228 return(r); 229 } 230 231 static int RSA_eay_mod_exp(r0, I, rsa) 232 BIGNUM *r0; 233 BIGNUM *I; 234 RSA *rsa; 235 { 236 BIGNUM *r1=NULL,*m1=NULL; 237 int ret=0; 238 BN_CTX *ctx; 239 240 if ((ctx=BN_CTX_new()) == NULL) goto err; 241 m1=BN_new(); 242 r1=BN_new(); 243 if ((m1 == NULL) || (r1 == NULL)) goto err; 244 245 /* Body of this routine removed for OpenBSD - will return 246 * when the RSA patent expires 247 */ 248 err: 249 if (m1 != NULL) BN_free(m1); 250 if (r1 != NULL) BN_free(r1); 251 BN_CTX_free(ctx); 252 return(ret); 253 } 254 255 static int RSA_eay_init(rsa) 256 RSA *rsa; 257 { 258 rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE; 259 return(1); 260 } 261 262 static int RSA_eay_finish(rsa) 263 RSA *rsa; 264 { 265 if (rsa->method_mod_n != NULL) 266 BN_MONT_CTX_free((BN_MONT_CTX *)rsa->method_mod_n); 267 if (rsa->method_mod_p != NULL) 268 BN_MONT_CTX_free((BN_MONT_CTX *)rsa->method_mod_p); 269 if (rsa->method_mod_q != NULL) 270 BN_MONT_CTX_free((BN_MONT_CTX *)rsa->method_mod_q); 271 return(1); 272 } 273 274 275