]> git.ipfire.org Git - thirdparty/openssl.git/blob - rsaref/rsaref.c
Import of old SSLeay release: SSLeay 0.8.1b
[thirdparty/openssl.git] / rsaref / rsaref.c
1 /* rsaref/rsaref.c */
2 /* Copyright (C) 1995-1997 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 "bn.h"
62 #include "rsa.h"
63 #include "rsaref.h"
64 #include "rand.h"
65
66 /*
67 * RSAREFerr(RSAREF_F_RSA_REF_BN2BIN,RSAREF_R_CONTENT_ENCODING);
68 * RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_DECRYPT,RSAREF_R_DATA);
69 * RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_ENCRYPT,RSAREF_R_DIGEST_ALGORITHM);
70 * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_DECRYPT,RSAREF_R_ENCODING);
71 * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_KEY);
72 * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_KEY_ENCODING);
73 * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_LEN);
74 * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_MODULUS_LEN);
75 * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_NEED_RANDOM);
76 * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_PRIVATE_KEY);
77 * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_PUBLIC_KEY);
78 * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_SIGNATURE);
79 * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_SIGNATURE_ENCODING);
80 * RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,RSAREF_R_ENCRYPTION_ALGORITHM);
81 * RSAREFerr(RSAREF_F_RSAREF_BN2BIN,ERR_R_BN_LIB);
82 */
83
84 #ifndef NOPROTO
85 static int RSAref_bn2bin(BIGNUM * from, unsigned char* to, int max);
86 #ifdef undef
87 static BIGNUM* RSAref_bin2bn(unsigned char* from, BIGNUM * to, int max);
88 #endif
89 static int RSAref_Public_eay2ref(RSA * from, RSArefPublicKey * to);
90 static int RSAref_Private_eay2ref(RSA * from, RSArefPrivateKey * to);
91 int RSA_ref_private_decrypt(int len, unsigned char *from,
92 unsigned char *to, RSA *rsa, int padding);
93 int RSA_ref_private_encrypt(int len, unsigned char *from,
94 unsigned char *to, RSA *rsa, int padding);
95 int RSA_ref_public_encrypt(int len, unsigned char *from,
96 unsigned char *to, RSA *rsa, int padding);
97 int RSA_ref_public_decrypt(int len, unsigned char *from,
98 unsigned char *to, RSA *rsa, int padding);
99 static int BN_ref_mod_exp(BIGNUM *r,BIGNUM *a,BIGNUM *p,BIGNUM *m, BN_CTX *ctx);
100 static int RSA_ref_mod_exp(BIGNUM *r0, BIGNUM *I, RSA *rsa);
101 #else
102
103 static int RSAref_bn2bin();
104 #ifdef undef
105 static BIGNUM* RSAref_bin2bn();
106 #endif
107 static int RSAref_Public_eay2ref();
108 static int RSAref_Private_eay2ref();
109 static int BN_ref_mod_exp();
110 static int RSA_ref_mod_exp();
111 int RSA_ref_private_decrypt();
112 int RSA_ref_private_encrypt();
113 int RSA_ref_public_encrypt();
114 int RSA_ref_public_decrypt();
115 static int BN_ref_mod_exp();
116 static int RSA_ref_mod_exp();
117 #endif
118
119 static RSA_METHOD rsa_pkcs1_ref_meth={
120 "RSAref PKCS#1 RSA",
121 RSA_ref_public_encrypt,
122 RSA_ref_public_decrypt,
123 RSA_ref_private_encrypt,
124 RSA_ref_private_decrypt,
125 RSA_ref_mod_exp,
126 BN_ref_mod_exp,
127 NULL,
128 NULL,
129 };
130
131 RSA_METHOD *RSA_PKCS1_RSAref()
132 {
133 return(&rsa_pkcs1_ref_meth);
134 }
135
136 static int RSA_ref_mod_exp(r0, I, rsa)
137 BIGNUM *r0;
138 BIGNUM *I;
139 RSA *rsa;
140 {
141 RSAREFerr(RSAREF_F_RSA_REF_MOD_EXP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
142 return(0);
143 }
144
145 static int BN_ref_mod_exp(r,a,p,m,ctx)
146 BIGNUM *r,*a,*p,*m;
147 BN_CTX *ctx;
148 {
149 RSAREFerr(RSAREF_F_BN_REF_MOD_EXP,ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
150 return(0);
151 }
152
153 static int RSAref_bn2bin(from,to,max)
154 BIGNUM *from;
155 unsigned char *to; /* [max] */
156 int max;
157 {
158 int i;
159
160 i=BN_num_bytes(from);
161 if (i > max)
162 {
163 RSAREFerr(RSAREF_F_RSAREF_BN2BIN,RSAREF_R_LEN);
164 return(0);
165 }
166
167 memset(to,0,(unsigned int)max);
168 if (!BN_bn2bin(from,&(to[max-i])))
169 return(0);
170 return(1);
171 }
172
173 #ifdef undef
174 static BIGNUM *RSAref_bin2bn(from,to,max)
175 unsigned char *from; /* [max] */
176 BIGNUM *to;
177 int max;
178 {
179 int i;
180 BIGNUM *ret;
181
182 for (i=0; i<max; i++)
183 if (from[i]) break;
184
185 ret=BN_bin2bn(&(from[i]),max-i,to);
186 return(ret);
187 }
188
189 static int RSAref_Public_ref2eay(from,to)
190 RSArefPublicKey *from;
191 RSA *to;
192 {
193 to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN);
194 to->e=RSAref_bin2bn(from->e,NULL,RSAref_MAX_LEN);
195 if ((to->n == NULL) || (to->e == NULL)) return(0);
196 return(1);
197 }
198 #endif
199
200 static int RSAref_Public_eay2ref(from,to)
201 RSA *from;
202 RSArefPublicKey *to;
203 {
204 to->bits=BN_num_bits(from->n);
205 if (!RSAref_bn2bin(from->n,to->m,RSAref_MAX_LEN)) return(0);
206 if (!RSAref_bn2bin(from->e,to->e,RSAref_MAX_LEN)) return(0);
207 return(1);
208 }
209
210 #ifdef undef
211 static int RSAref_Private_ref2eay(from,to)
212 RSArefPrivateKey *from;
213 RSA *to;
214 {
215 if ((to->n=RSAref_bin2bn(from->m,NULL,RSAref_MAX_LEN)) == NULL)
216 return(0);
217 if ((to->e=RSAref_bin2bn(from->e,NULL,RSAref_MAX_LEN)) == NULL)
218 return(0);
219 if ((to->d=RSAref_bin2bn(from->d,NULL,RSAref_MAX_LEN)) == NULL)
220 return(0);
221 if ((to->p=RSAref_bin2bn(from->prime[0],NULL,RSAref_MAX_PLEN)) == NULL)
222 return(0);
223 if ((to->q=RSAref_bin2bn(from->prime[1],NULL,RSAref_MAX_PLEN)) == NULL)
224 return(0);
225 if ((to->dmp1=RSAref_bin2bn(from->pexp[0],NULL,RSAref_MAX_PLEN))
226 == NULL)
227 return(0);
228 if ((to->dmq1=RSAref_bin2bn(from->pexp[1],NULL,RSAref_MAX_PLEN))
229 == NULL)
230 return(0);
231 if ((to->iqmp=RSAref_bin2bn(from->coef,NULL,RSAref_MAX_PLEN)) == NULL)
232 return(0);
233 return(1);
234 }
235 #endif
236
237 static int RSAref_Private_eay2ref(from,to)
238 RSA *from;
239 RSArefPrivateKey *to;
240 {
241 to->bits=BN_num_bits(from->n);
242 if (!RSAref_bn2bin(from->n,to->m,RSAref_MAX_LEN)) return(0);
243 if (!RSAref_bn2bin(from->e,to->e,RSAref_MAX_LEN)) return(0);
244 if (!RSAref_bn2bin(from->d,to->d,RSAref_MAX_LEN)) return(0);
245 if (!RSAref_bn2bin(from->p,to->prime[0],RSAref_MAX_PLEN)) return(0);
246 if (!RSAref_bn2bin(from->q,to->prime[1],RSAref_MAX_PLEN)) return(0);
247 if (!RSAref_bn2bin(from->dmp1,to->pexp[0],RSAref_MAX_PLEN)) return(0);
248 if (!RSAref_bn2bin(from->dmq1,to->pexp[1],RSAref_MAX_PLEN)) return(0);
249 if (!RSAref_bn2bin(from->iqmp,to->coef,RSAref_MAX_PLEN)) return(0);
250 return(1);
251 }
252
253 int RSA_ref_private_decrypt(len,from,to,rsa,padding)
254 int len;
255 unsigned char *from,*to;
256 RSA *rsa;
257 int padding;
258 {
259 int i,outlen= -1;
260 RSArefPrivateKey RSAkey;
261
262 if (!RSAref_Private_eay2ref(rsa,&RSAkey))
263 goto err;
264 if ((i=RSAPrivateDecrypt(to,&outlen,from,len,&RSAkey)) != 0)
265 {
266 RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_DECRYPT,i);
267 outlen= -1;
268 }
269 err:
270 memset(&RSAkey,0,sizeof(RSAkey));
271 return(outlen);
272 }
273
274 int RSA_ref_private_encrypt(len,from,to,rsa,padding)
275 int len;
276 unsigned char *from,*to;
277 RSA *rsa;
278 int padding;
279 {
280 int i,outlen= -1;
281 RSArefPrivateKey RSAkey;
282
283 if (!RSAref_Private_eay2ref(rsa,&RSAkey))
284 goto err;
285 if ((i=RSAPrivateEncrypt(to,&outlen,from,len,&RSAkey)) != 0)
286 {
287 RSAREFerr(RSAREF_F_RSA_REF_PRIVATE_ENCRYPT,i);
288 outlen= -1;
289 }
290 err:
291 memset(&RSAkey,0,sizeof(RSAkey));
292 return(outlen);
293 }
294
295 int RSA_ref_public_decrypt(len,from,to,rsa,padding)
296 int len;
297 unsigned char *from,*to;
298 RSA *rsa;
299 int padding;
300 {
301 int i,outlen= -1;
302 RSArefPublicKey RSAkey;
303
304 if (!RSAref_Public_eay2ref(rsa,&RSAkey))
305 goto err;
306 if ((i=RSAPublicDecrypt(to,&outlen,from,len,&RSAkey)) != 0)
307 {
308 RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_DECRYPT,i);
309 outlen= -1;
310 }
311 err:
312 memset(&RSAkey,0,sizeof(RSAkey));
313 return(outlen);
314 }
315
316 int RSA_ref_public_encrypt(len,from,to,rsa,padding)
317 int len;
318 unsigned char *from,*to;
319 RSA *rsa;
320 int padding;
321 {
322 int outlen= -1;
323 int i;
324 RSArefPublicKey RSAkey;
325 RSARandomState rnd;
326 unsigned char buf[16];
327
328 R_RandomInit(&rnd);
329 R_GetRandomBytesNeeded((unsigned int *)&i,&rnd);
330 while (i > 0)
331 {
332 RAND_bytes(buf,16);
333 R_RandomUpdate(&rnd,buf,(unsigned int)((i>16)?16:i));
334 i-=16;
335 }
336
337 if (!RSAref_Public_eay2ref(rsa,&RSAkey))
338 goto err;
339 if ((i=RSAPublicEncrypt(to,&outlen,from,len,&RSAkey,&rnd)) != 0)
340 {
341 RSAREFerr(RSAREF_F_RSA_REF_PUBLIC_ENCRYPT,i);
342 outlen= -1;
343 goto err;
344 }
345 err:
346 memset(&RSAkey,0,sizeof(RSAkey));
347 R_RandomFinal(&rnd);
348 memset(&rnd,0,sizeof(rnd));
349 return(outlen);
350 }
351