blob: 5228fd398f3477d017be00dad0b4cb79ed5f474a [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 *
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 *
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035*/
36
37#include "includes.h"
Damien Miller95def091999-11-25 00:26:21 +110038RCSID("$Id: rsa.c,v 1.4 1999/11/24 13:26:22 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{
Damien Miller95def091999-11-25 00:26:21 +110049 RSA *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050
Damien Miller95def091999-11-25 00:26:21 +110051 key = RSA_generate_key(32, 3, NULL, NULL);
52 if (key == NULL)
53 return (0);
54 RSA_free(key);
55 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056}
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{
Damien Miller95def091999-11-25 00:26:21 +110065 RSA *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066
Damien Miller95def091999-11-25 00:26:21 +110067 if (rsa_verbose) {
68 printf("Generating RSA keys: ");
69 fflush(stdout);
70 }
71 key = RSA_generate_key(bits, 35, NULL, NULL);
72 if (key == NULL)
73 fatal("rsa_generate_key: key generation failed.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074
Damien Miller95def091999-11-25 00:26:21 +110075 /* Copy public key parameters */
76 pub->n = BN_new();
77 BN_copy(pub->n, key->n);
78 pub->e = BN_new();
79 BN_copy(pub->e, key->e);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
Damien Miller95def091999-11-25 00:26:21 +110081 /* Copy private key parameters */
82 prv->n = BN_new();
83 BN_copy(prv->n, key->n);
84 prv->e = BN_new();
85 BN_copy(prv->e, key->e);
86 prv->d = BN_new();
87 BN_copy(prv->d, key->d);
88 prv->p = BN_new();
89 BN_copy(prv->p, key->p);
90 prv->q = BN_new();
91 BN_copy(prv->q, key->q);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
Damien Miller95def091999-11-25 00:26:21 +110093 prv->dmp1 = BN_new();
94 BN_copy(prv->dmp1, key->dmp1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
Damien Miller95def091999-11-25 00:26:21 +110096 prv->dmq1 = BN_new();
97 BN_copy(prv->dmq1, key->dmq1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller95def091999-11-25 00:26:21 +110099 prv->iqmp = BN_new();
100 BN_copy(prv->iqmp, key->iqmp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
Damien Miller95def091999-11-25 00:26:21 +1100102 RSA_free(key);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
Damien Miller95def091999-11-25 00:26:21 +1100104 if (rsa_verbose)
105 printf("Key generation complete.\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106}
107
108void
Damien Miller95def091999-11-25 00:26:21 +1100109rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110{
Damien Miller95def091999-11-25 00:26:21 +1100111 char *inbuf, *outbuf;
112 int len, ilen, olen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000113
Damien Miller95def091999-11-25 00:26:21 +1100114 if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
115 fatal("rsa_public_encrypt() exponent too small or not odd");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116
Damien Miller95def091999-11-25 00:26:21 +1100117 olen = BN_num_bytes(key->n);
118 outbuf = xmalloc(olen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
Damien Miller95def091999-11-25 00:26:21 +1100120 ilen = BN_num_bytes(in);
121 inbuf = xmalloc(ilen);
122 BN_bn2bin(in, inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Damien Miller95def091999-11-25 00:26:21 +1100124 if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
125 RSA_PKCS1_PADDING)) <= 0)
126 fatal("rsa_public_encrypt() failed");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127
Damien Miller95def091999-11-25 00:26:21 +1100128 BN_bin2bn(outbuf, len, out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129
Damien Miller95def091999-11-25 00:26:21 +1100130 memset(outbuf, 0, olen);
131 memset(inbuf, 0, ilen);
132 xfree(outbuf);
133 xfree(inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134}
135
136void
137rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
138{
Damien Miller95def091999-11-25 00:26:21 +1100139 char *inbuf, *outbuf;
140 int len, ilen, olen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller95def091999-11-25 00:26:21 +1100142 olen = BN_num_bytes(key->n);
143 outbuf = xmalloc(olen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller95def091999-11-25 00:26:21 +1100145 ilen = BN_num_bytes(in);
146 inbuf = xmalloc(ilen);
147 BN_bn2bin(in, inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148
Damien Miller95def091999-11-25 00:26:21 +1100149 if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
150 RSA_SSLV23_PADDING)) <= 0)
151 fatal("rsa_private_decrypt() failed");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152
Damien Miller95def091999-11-25 00:26:21 +1100153 BN_bin2bn(outbuf, len, out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Damien Miller95def091999-11-25 00:26:21 +1100155 memset(outbuf, 0, olen);
156 memset(inbuf, 0, ilen);
157 xfree(outbuf);
158 xfree(inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000159}
160
161/* Set whether to output verbose messages during key generation. */
162
163void
164rsa_set_verbose(int verbose)
165{
Damien Miller95def091999-11-25 00:26:21 +1100166 rsa_verbose = verbose;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167}