]> andersk Git - openssh.git/blob - rsa.c
8db61a9be39f876229f339cefb40bd1900cca3fc
[openssh.git] / rsa.c
1 /*
2  * 
3  * rsa.c
4  * 
5  * Author: Tatu Ylonen <ylo@cs.hut.fi>
6  * 
7  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8  *                    All rights reserved
9  * 
10  * Created: Fri Mar  3 22:07:06 1995 ylo
11  * 
12  * Description of the RSA algorithm can be found e.g. from the following sources:
13  * 
14  *   Bruce Schneier: Applied Cryptography.  John Wiley & Sons, 1994.
15  * 
16  *   Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to
17  *   Computer Security.  Prentice-Hall, 1989.
18  * 
19  *   Man Young Rhee: Cryptography and Secure Data Communications.  McGraw-Hill,
20  *   1994.
21  * 
22  *   R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications
23  *   System and Method.  US Patent 4,405,829, 1983.
24  * 
25  *   Hans Riesel: Prime Numbers and Computer Methods for Factorization.
26  *   Birkhauser, 1994.
27  * 
28  *   The RSA Frequently Asked Questions document by RSA Data Security, Inc., 1995.
29  * 
30  *   RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as included
31  *   below:
32  * 
33  *     [gone - had to be deleted - what a pity]
34  * 
35 */
36
37 #include "includes.h"
38 RCSID("$Id$");
39
40 #include "rsa.h"
41 #include "ssh.h"
42 #include "xmalloc.h"
43 #include "random.h"
44
45 int rsa_verbose = 1;
46
47 /*
48  * Seed OpenSSL's random number generator
49  */
50 void
51 seed_rng()
52 {
53         char buf[64];
54
55         get_random_bytes(buf, sizeof(buf));
56         RAND_seed(buf, sizeof(buf));
57         memset(buf, 0, sizeof(buf));
58 }
59
60 int
61 rsa_alive()
62 {
63         RSA *key;
64
65         seed_rng();
66         key = RSA_generate_key(32, 3, NULL, NULL);
67         if (key == NULL)
68                 return (0);
69         RSA_free(key);
70         return (1);
71 }
72
73 /*
74  * Key generation progress meter callback
75  */
76 void
77 keygen_progress(int p, int n, void *arg)
78 {
79         const char progress_chars[] = ".o+O?";
80
81         if ((p < 0) || (p > (sizeof(progress_chars) - 2)))
82                 p = sizeof(progress_chars) - 2;
83
84         putchar(progress_chars[p]);
85         fflush(stdout);
86 }
87
88 /*
89  * Generates RSA public and private keys.  This initializes the data
90  * structures; they should be freed with rsa_clear_private_key and
91  * rsa_clear_public_key.
92  */
93
94 void
95 rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits)
96 {
97         RSA *key;
98
99         seed_rng();
100         
101         if (rsa_verbose) {
102                 printf("Generating RSA keys:  ");
103                 fflush(stdout);
104                 key = RSA_generate_key(bits, 35, keygen_progress, NULL);
105                 printf("\n");
106         } else {
107                 key = RSA_generate_key(bits, 35, NULL, NULL);
108         }
109         if (key == NULL)
110                 fatal("rsa_generate_key: key generation failed.");
111
112         /* Copy public key parameters */
113         pub->n = BN_new();
114         BN_copy(pub->n, key->n);
115         pub->e = BN_new();
116         BN_copy(pub->e, key->e);
117
118         /* Copy private key parameters */
119         prv->n = BN_new();
120         BN_copy(prv->n, key->n);
121         prv->e = BN_new();
122         BN_copy(prv->e, key->e);
123         prv->d = BN_new();
124         BN_copy(prv->d, key->d);
125         prv->p = BN_new();
126         BN_copy(prv->p, key->p);
127         prv->q = BN_new();
128         BN_copy(prv->q, key->q);
129
130         prv->dmp1 = BN_new();
131         BN_copy(prv->dmp1, key->dmp1);
132
133         prv->dmq1 = BN_new();
134         BN_copy(prv->dmq1, key->dmq1);
135
136         prv->iqmp = BN_new();
137         BN_copy(prv->iqmp, key->iqmp);
138
139         RSA_free(key);
140
141         if (rsa_verbose)
142                 printf("Key generation complete.\n");
143 }
144
145 void
146 rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
147 {
148         char *inbuf, *outbuf;
149         int len, ilen, olen;
150
151         if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
152                 fatal("rsa_public_encrypt() exponent too small or not odd");
153
154         olen = BN_num_bytes(key->n);
155         outbuf = xmalloc(olen);
156
157         ilen = BN_num_bytes(in);
158         inbuf = xmalloc(ilen);
159         BN_bn2bin(in, inbuf);
160
161         if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
162                                       RSA_PKCS1_PADDING)) <= 0)
163                 fatal("rsa_public_encrypt() failed");
164
165         BN_bin2bn(outbuf, len, out);
166
167         memset(outbuf, 0, olen);
168         memset(inbuf, 0, ilen);
169         xfree(outbuf);
170         xfree(inbuf);
171 }
172
173 void
174 rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
175 {
176         char *inbuf, *outbuf;
177         int len, ilen, olen;
178
179         olen = BN_num_bytes(key->n);
180         outbuf = xmalloc(olen);
181
182         ilen = BN_num_bytes(in);
183         inbuf = xmalloc(ilen);
184         BN_bn2bin(in, inbuf);
185
186         if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
187                                        RSA_SSLV23_PADDING)) <= 0)
188                 fatal("rsa_private_decrypt() failed");
189
190         BN_bin2bn(outbuf, len, out);
191
192         memset(outbuf, 0, olen);
193         memset(inbuf, 0, ilen);
194         xfree(outbuf);
195         xfree(inbuf);
196 }
197
198 /* Set whether to output verbose messages during key generation. */
199
200 void
201 rsa_set_verbose(int verbose)
202 {
203         rsa_verbose = verbose;
204 }
This page took 0.041301 seconds and 3 git commands to generate.