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