]> andersk Git - openssh.git/blame - rsa.c
- Configure fix from Hiroshi Takekawa <takekawa@sr3.t.u-tokyo.ac.jp>
[openssh.git] / rsa.c
CommitLineData
8efc0c15 1/*
5260325f 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 *
8efc0c15 35*/
36
37#include "includes.h"
38RCSID("$Id$");
39
40#include "rsa.h"
41#include "ssh.h"
42#include "xmalloc.h"
69c76614 43#include "random.h"
8efc0c15 44
45int rsa_verbose = 1;
46
1c7ff935 47/*
48 * Seed OpenSSL's random number generator
49 */
50void
51seed_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
8efc0c15 60int
61rsa_alive()
62{
5260325f 63 RSA *key;
8efc0c15 64
54096dcc 65 seed_rng();
5260325f 66 key = RSA_generate_key(32, 3, NULL, NULL);
67 if (key == NULL)
68 return (0);
69 RSA_free(key);
70 return (1);
8efc0c15 71}
72
db513105 73/*
74 * Key generation progress meter callback
75 */
76void
77keygen_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)))
69c76614 82 p = sizeof(progress_chars) - 2;
db513105 83
69c76614 84 putchar(progress_chars[p]);
db513105 85 fflush(stdout);
86}
87
aa3378df 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 */
8efc0c15 93
94void
95rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits)
96{
5260325f 97 RSA *key;
98
69c76614 99 seed_rng();
100
5260325f 101 if (rsa_verbose) {
102 printf("Generating RSA keys: ");
103 fflush(stdout);
db513105 104 key = RSA_generate_key(bits, 35, keygen_progress, NULL);
105 printf("\n");
106 } else {
107 key = RSA_generate_key(bits, 35, NULL, NULL);
5260325f 108 }
5260325f 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");
8efc0c15 143}
144
145void
5260325f 146rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
8efc0c15 147{
5260325f 148 char *inbuf, *outbuf;
149 int len, ilen, olen;
8efc0c15 150
5260325f 151 if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
152 fatal("rsa_public_encrypt() exponent too small or not odd");
8efc0c15 153
5260325f 154 olen = BN_num_bytes(key->n);
155 outbuf = xmalloc(olen);
8efc0c15 156
5260325f 157 ilen = BN_num_bytes(in);
158 inbuf = xmalloc(ilen);
159 BN_bn2bin(in, inbuf);
8efc0c15 160
5260325f 161 if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
162 RSA_PKCS1_PADDING)) <= 0)
163 fatal("rsa_public_encrypt() failed");
8efc0c15 164
5260325f 165 BN_bin2bn(outbuf, len, out);
8efc0c15 166
5260325f 167 memset(outbuf, 0, olen);
168 memset(inbuf, 0, ilen);
169 xfree(outbuf);
170 xfree(inbuf);
8efc0c15 171}
172
173void
174rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
175{
5260325f 176 char *inbuf, *outbuf;
177 int len, ilen, olen;
8efc0c15 178
5260325f 179 olen = BN_num_bytes(key->n);
180 outbuf = xmalloc(olen);
8efc0c15 181
5260325f 182 ilen = BN_num_bytes(in);
183 inbuf = xmalloc(ilen);
184 BN_bn2bin(in, inbuf);
8efc0c15 185
5260325f 186 if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
187 RSA_SSLV23_PADDING)) <= 0)
188 fatal("rsa_private_decrypt() failed");
8efc0c15 189
5260325f 190 BN_bin2bn(outbuf, len, out);
8efc0c15 191
5260325f 192 memset(outbuf, 0, olen);
193 memset(inbuf, 0, ilen);
194 xfree(outbuf);
195 xfree(inbuf);
8efc0c15 196}
197
198/* Set whether to output verbose messages during key generation. */
199
200void
201rsa_set_verbose(int verbose)
202{
5260325f 203 rsa_verbose = verbose;
8efc0c15 204}
This page took 0.559001 seconds and 5 git commands to generate.