]> andersk Git - openssh.git/blame - rsa.c
- Seed OpenSSL's random number generator before generating RSA keypairs
[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
47int
48rsa_alive()
49{
5260325f 50 RSA *key;
8efc0c15 51
5260325f 52 key = RSA_generate_key(32, 3, NULL, NULL);
53 if (key == NULL)
54 return (0);
55 RSA_free(key);
56 return (1);
8efc0c15 57}
58
db513105 59/*
60 * Key generation progress meter callback
61 */
62void
63keygen_progress(int p, int n, void *arg)
64{
65 const char progress_chars[] = ".o+O?";
66
67 if ((p < 0) || (p > (sizeof(progress_chars) - 2)))
69c76614 68 p = sizeof(progress_chars) - 2;
db513105 69
69c76614 70 putchar(progress_chars[p]);
db513105 71 fflush(stdout);
72}
73
69c76614 74/*
75 * Seed OpenSSL's random number generator
76 */
77void
78seed_rng()
79{
80 char buf[32];
81
82 get_random_bytes(buf, sizeof(buf));
83 RAND_seed(buf, sizeof(buf));
84 memset(buf, 0, sizeof(buf));
85}
86
aa3378df 87/*
88 * Generates RSA public and private keys. This initializes the data
89 * structures; they should be freed with rsa_clear_private_key and
90 * rsa_clear_public_key.
91 */
8efc0c15 92
93void
94rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits)
95{
5260325f 96 RSA *key;
97
69c76614 98 seed_rng();
99
5260325f 100 if (rsa_verbose) {
101 printf("Generating RSA keys: ");
102 fflush(stdout);
db513105 103 key = RSA_generate_key(bits, 35, keygen_progress, NULL);
104 printf("\n");
105 } else {
106 key = RSA_generate_key(bits, 35, NULL, NULL);
5260325f 107 }
5260325f 108 if (key == NULL)
109 fatal("rsa_generate_key: key generation failed.");
110
111 /* Copy public key parameters */
112 pub->n = BN_new();
113 BN_copy(pub->n, key->n);
114 pub->e = BN_new();
115 BN_copy(pub->e, key->e);
116
117 /* Copy private key parameters */
118 prv->n = BN_new();
119 BN_copy(prv->n, key->n);
120 prv->e = BN_new();
121 BN_copy(prv->e, key->e);
122 prv->d = BN_new();
123 BN_copy(prv->d, key->d);
124 prv->p = BN_new();
125 BN_copy(prv->p, key->p);
126 prv->q = BN_new();
127 BN_copy(prv->q, key->q);
128
129 prv->dmp1 = BN_new();
130 BN_copy(prv->dmp1, key->dmp1);
131
132 prv->dmq1 = BN_new();
133 BN_copy(prv->dmq1, key->dmq1);
134
135 prv->iqmp = BN_new();
136 BN_copy(prv->iqmp, key->iqmp);
137
138 RSA_free(key);
139
140 if (rsa_verbose)
141 printf("Key generation complete.\n");
8efc0c15 142}
143
144void
5260325f 145rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
8efc0c15 146{
5260325f 147 char *inbuf, *outbuf;
148 int len, ilen, olen;
8efc0c15 149
5260325f 150 if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
151 fatal("rsa_public_encrypt() exponent too small or not odd");
8efc0c15 152
5260325f 153 olen = BN_num_bytes(key->n);
154 outbuf = xmalloc(olen);
8efc0c15 155
5260325f 156 ilen = BN_num_bytes(in);
157 inbuf = xmalloc(ilen);
158 BN_bn2bin(in, inbuf);
8efc0c15 159
5260325f 160 if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
161 RSA_PKCS1_PADDING)) <= 0)
162 fatal("rsa_public_encrypt() failed");
8efc0c15 163
5260325f 164 BN_bin2bn(outbuf, len, out);
8efc0c15 165
5260325f 166 memset(outbuf, 0, olen);
167 memset(inbuf, 0, ilen);
168 xfree(outbuf);
169 xfree(inbuf);
8efc0c15 170}
171
172void
173rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
174{
5260325f 175 char *inbuf, *outbuf;
176 int len, ilen, olen;
8efc0c15 177
5260325f 178 olen = BN_num_bytes(key->n);
179 outbuf = xmalloc(olen);
8efc0c15 180
5260325f 181 ilen = BN_num_bytes(in);
182 inbuf = xmalloc(ilen);
183 BN_bn2bin(in, inbuf);
8efc0c15 184
5260325f 185 if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
186 RSA_SSLV23_PADDING)) <= 0)
187 fatal("rsa_private_decrypt() failed");
8efc0c15 188
5260325f 189 BN_bin2bn(outbuf, len, out);
8efc0c15 190
5260325f 191 memset(outbuf, 0, olen);
192 memset(inbuf, 0, ilen);
193 xfree(outbuf);
194 xfree(inbuf);
8efc0c15 195}
196
197/* Set whether to output verbose messages during key generation. */
198
199void
200rsa_set_verbose(int verbose)
201{
5260325f 202 rsa_verbose = verbose;
8efc0c15 203}
This page took 0.10671 seconds and 5 git commands to generate.