blob: 6845fab9dcf6fb043c475e3b8a33923fb403f2a5 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
2
3rsa.c
4
5Author: Tatu Ylonen <ylo@cs.hut.fi>
6
7Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 All rights reserved
9
10Created: Fri Mar 3 22:07:06 1995 ylo
11
12Description 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"
Damien Miller356a0b01999-11-08 15:30:59 +110038RCSID("$Id: rsa.c,v 1.2 1999/11/08 04:30:59 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "rsa.h"
41#include "ssh.h"
42#include "xmalloc.h"
43
44int rsa_verbose = 1;
45
46int
47rsa_alive()
48{
49 RSA *key;
50
51 key = RSA_generate_key(32, 3, NULL, NULL);
52 if (key == NULL)
53 return (0);
54 RSA_free(key);
55 return (1);
56}
57
58/* Generates RSA public and private keys. This initializes the data
59 structures; they should be freed with rsa_clear_private_key and
60 rsa_clear_public_key. */
61
62void
63rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits)
64{
65 RSA *key;
66
67 if (rsa_verbose) {
68 printf("Generating RSA keys: ");
69 fflush(stdout);
70 }
71
72 key = RSA_generate_key(bits, 35, NULL, NULL);
Damien Miller356a0b01999-11-08 15:30:59 +110073 if (key == NULL)
74 fatal("rsa_generate_key: key generation failed.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
76 /* Copy public key parameters */
77 pub->n = BN_new();
78 BN_copy(pub->n, key->n);
79 pub->e = BN_new();
80 BN_copy(pub->e, key->e);
81
82 /* Copy private key parameters */
83 prv->n = BN_new();
84 BN_copy(prv->n, key->n);
85 prv->e = BN_new();
86 BN_copy(prv->e, key->e);
87 prv->d = BN_new();
88 BN_copy(prv->d, key->d);
89 prv->p = BN_new();
90 BN_copy(prv->p, key->p);
91 prv->q = BN_new();
92 BN_copy(prv->q, key->q);
93
94 prv->dmp1 = BN_new();
95 BN_copy(prv->dmp1, key->dmp1);
96
97 prv->dmq1 = BN_new();
98 BN_copy(prv->dmq1, key->dmq1);
99
100 prv->iqmp = BN_new();
101 BN_copy(prv->iqmp, key->iqmp);
102
103 RSA_free(key);
104
105 if (rsa_verbose)
106 printf("Key generation complete.\n");
107}
108
109void
110rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA* key)
111{
112 char *inbuf, *outbuf;
Damien Miller356a0b01999-11-08 15:30:59 +1100113 int in_len;
114 int out_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115 int len;
116
117 if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
118 fatal("rsa_public_encrypt() exponent too small or not odd");
119
Damien Miller356a0b01999-11-08 15:30:59 +1100120 out_len = BN_num_bytes(key->n);
121 outbuf = xmalloc(out_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
Damien Miller356a0b01999-11-08 15:30:59 +1100123 in_len = BN_num_bytes(in);
124 inbuf = xmalloc(in_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125 BN_bn2bin(in, inbuf);
126
Damien Miller356a0b01999-11-08 15:30:59 +1100127 if ((len = RSA_public_encrypt(in_len, inbuf, outbuf, key,
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128 RSA_PKCS1_PADDING)) <= 0)
129 fatal("rsa_public_encrypt() failed");
130
131 BN_bin2bn(outbuf, len, out);
132
Damien Miller356a0b01999-11-08 15:30:59 +1100133 memset(outbuf, 0, out_len);
134 memset(inbuf, 0, in_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135 xfree(outbuf);
136 xfree(inbuf);
137}
138
139void
140rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
141{
142 char *inbuf, *outbuf;
Damien Miller356a0b01999-11-08 15:30:59 +1100143 int in_len;
144 int out_len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145 int len;
146
Damien Miller356a0b01999-11-08 15:30:59 +1100147 out_len = BN_num_bytes(key->n);
148 outbuf = xmalloc(out_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149
Damien Miller356a0b01999-11-08 15:30:59 +1100150 in_len = BN_num_bytes(in);
151 inbuf = xmalloc(in_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152 BN_bn2bin(in, inbuf);
153
Damien Miller356a0b01999-11-08 15:30:59 +1100154 if ((len = RSA_private_decrypt(in_len, inbuf, outbuf, key,
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155 RSA_SSLV23_PADDING)) <= 0)
156 fatal("rsa_private_decrypt() failed");
157
158 BN_bin2bn(outbuf, len, out);
159
Damien Miller356a0b01999-11-08 15:30:59 +1100160 memset(outbuf, 0, out_len);
161 memset(inbuf, 0, in_len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162 xfree(outbuf);
163 xfree(inbuf);
164}
165
166/* Set whether to output verbose messages during key generation. */
167
168void
169rsa_set_verbose(int verbose)
170{
171 rsa_verbose = verbose;
172}