blob: 1e8c434f91df8998466a195746a934470af9d73b [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * rsa.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Fri Mar 3 22:07:06 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 * Description of the RSA algorithm can be found e.g. from the following sources:
Damien Miller4af51302000-04-16 11:18:38 +100013 *
Damien Miller95def091999-11-25 00:26:21 +110014 * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1994.
Damien Miller4af51302000-04-16 11:18:38 +100015 *
Damien Miller95def091999-11-25 00:26:21 +110016 * Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to
17 * Computer Security. Prentice-Hall, 1989.
Damien Miller4af51302000-04-16 11:18:38 +100018 *
Damien Miller95def091999-11-25 00:26:21 +110019 * Man Young Rhee: Cryptography and Secure Data Communications. McGraw-Hill,
20 * 1994.
Damien Miller4af51302000-04-16 11:18:38 +100021 *
Damien Miller95def091999-11-25 00:26:21 +110022 * R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications
23 * System and Method. US Patent 4,405,829, 1983.
Damien Miller4af51302000-04-16 11:18:38 +100024 *
Damien Miller95def091999-11-25 00:26:21 +110025 * Hans Riesel: Prime Numbers and Computer Methods for Factorization.
26 * Birkhauser, 1994.
Damien Miller4af51302000-04-16 11:18:38 +100027 *
Damien Miller95def091999-11-25 00:26:21 +110028 * The RSA Frequently Asked Questions document by RSA Data Security, Inc., 1995.
Damien Miller4af51302000-04-16 11:18:38 +100029 *
Damien Miller95def091999-11-25 00:26:21 +110030 * RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as included
31 * below:
Damien Miller4af51302000-04-16 11:18:38 +100032 *
Damien Miller95def091999-11-25 00:26:21 +110033 * [gone - had to be deleted - what a pity]
Damien Miller4af51302000-04-16 11:18:38 +100034 *
Damien Millerd4a8b7e1999-10-27 13:42:43 +100035*/
36
37#include "includes.h"
Damien Miller4af51302000-04-16 11:18:38 +100038RCSID("$Id: rsa.c,v 1.14 2000/04/16 01:18:45 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039
40#include "rsa.h"
41#include "ssh.h"
42#include "xmalloc.h"
Damien Miller040f3832000-04-03 14:50:43 +100043#include "entropy.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45int rsa_verbose = 1;
46
47int
48rsa_alive()
49{
Damien Miller95def091999-11-25 00:26:21 +110050 RSA *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100051
Damien Millerfac99cd2000-03-05 16:10:45 +110052 seed_rng();
Damien Miller95def091999-11-25 00:26:21 +110053 key = RSA_generate_key(32, 3, NULL, NULL);
54 if (key == NULL)
55 return (0);
56 RSA_free(key);
57 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100058}
59
Damien Miller5428f641999-11-25 11:54:57 +110060/*
Damien Millerfdb5f131999-12-17 14:02:47 +110061 * Key generation progress meter callback
62 */
63void
64keygen_progress(int p, int n, void *arg)
65{
66 const char progress_chars[] = ".o+O?";
67
68 if ((p < 0) || (p > (sizeof(progress_chars) - 2)))
Damien Millerf07390e2000-01-29 20:40:22 +110069 p = sizeof(progress_chars) - 2;
Damien Millerfdb5f131999-12-17 14:02:47 +110070
Damien Millerf07390e2000-01-29 20:40:22 +110071 putchar(progress_chars[p]);
Damien Millerfdb5f131999-12-17 14:02:47 +110072 fflush(stdout);
73}
74
75/*
Damien Miller5428f641999-11-25 11:54:57 +110076 * Generates RSA public and private keys. This initializes the data
77 * structures; they should be freed with rsa_clear_private_key and
78 * rsa_clear_public_key.
79 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
81void
82rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits)
83{
Damien Miller95def091999-11-25 00:26:21 +110084 RSA *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Millerf07390e2000-01-29 20:40:22 +110086 seed_rng();
87
Damien Miller95def091999-11-25 00:26:21 +110088 if (rsa_verbose) {
89 printf("Generating RSA keys: ");
90 fflush(stdout);
Damien Millerfdb5f131999-12-17 14:02:47 +110091 key = RSA_generate_key(bits, 35, keygen_progress, NULL);
92 printf("\n");
93 } else {
94 key = RSA_generate_key(bits, 35, NULL, NULL);
Damien Miller95def091999-11-25 00:26:21 +110095 }
Damien Miller95def091999-11-25 00:26:21 +110096 if (key == NULL)
97 fatal("rsa_generate_key: key generation failed.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller95def091999-11-25 00:26:21 +110099 /* Copy public key parameters */
100 pub->n = BN_new();
101 BN_copy(pub->n, key->n);
102 pub->e = BN_new();
103 BN_copy(pub->e, key->e);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104
Damien Miller95def091999-11-25 00:26:21 +1100105 /* Copy private key parameters */
106 prv->n = BN_new();
107 BN_copy(prv->n, key->n);
108 prv->e = BN_new();
109 BN_copy(prv->e, key->e);
110 prv->d = BN_new();
111 BN_copy(prv->d, key->d);
112 prv->p = BN_new();
113 BN_copy(prv->p, key->p);
114 prv->q = BN_new();
115 BN_copy(prv->q, key->q);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000116
Damien Miller95def091999-11-25 00:26:21 +1100117 prv->dmp1 = BN_new();
118 BN_copy(prv->dmp1, key->dmp1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
Damien Miller95def091999-11-25 00:26:21 +1100120 prv->dmq1 = BN_new();
121 BN_copy(prv->dmq1, key->dmq1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
Damien Miller95def091999-11-25 00:26:21 +1100123 prv->iqmp = BN_new();
124 BN_copy(prv->iqmp, key->iqmp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
Damien Miller95def091999-11-25 00:26:21 +1100126 RSA_free(key);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127
Damien Miller95def091999-11-25 00:26:21 +1100128 if (rsa_verbose)
129 printf("Key generation complete.\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000130}
131
132void
Damien Miller95def091999-11-25 00:26:21 +1100133rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134{
Damien Miller7684ee12000-03-17 23:40:15 +1100135 unsigned char *inbuf, *outbuf;
Damien Miller95def091999-11-25 00:26:21 +1100136 int len, ilen, olen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137
Damien Miller95def091999-11-25 00:26:21 +1100138 if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
139 fatal("rsa_public_encrypt() exponent too small or not odd");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000140
Damien Miller95def091999-11-25 00:26:21 +1100141 olen = BN_num_bytes(key->n);
142 outbuf = xmalloc(olen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
Damien Miller95def091999-11-25 00:26:21 +1100144 ilen = BN_num_bytes(in);
145 inbuf = xmalloc(ilen);
146 BN_bn2bin(in, inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147
Damien Miller95def091999-11-25 00:26:21 +1100148 if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
Damien Miller98c7ad62000-03-09 21:27:49 +1100149 RSA_PKCS1_PADDING)) <= 0)
Damien Miller95def091999-11-25 00:26:21 +1100150 fatal("rsa_public_encrypt() failed");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151
Damien Miller95def091999-11-25 00:26:21 +1100152 BN_bin2bn(outbuf, len, out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153
Damien Miller95def091999-11-25 00:26:21 +1100154 memset(outbuf, 0, olen);
155 memset(inbuf, 0, ilen);
156 xfree(outbuf);
157 xfree(inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158}
159
160void
161rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
162{
Damien Miller7684ee12000-03-17 23:40:15 +1100163 unsigned char *inbuf, *outbuf;
Damien Miller95def091999-11-25 00:26:21 +1100164 int len, ilen, olen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165
Damien Miller95def091999-11-25 00:26:21 +1100166 olen = BN_num_bytes(key->n);
167 outbuf = xmalloc(olen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168
Damien Miller95def091999-11-25 00:26:21 +1100169 ilen = BN_num_bytes(in);
170 inbuf = xmalloc(ilen);
171 BN_bn2bin(in, inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172
Damien Miller95def091999-11-25 00:26:21 +1100173 if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
Damien Miller98c7ad62000-03-09 21:27:49 +1100174 RSA_PKCS1_PADDING)) <= 0)
Damien Miller95def091999-11-25 00:26:21 +1100175 fatal("rsa_private_decrypt() failed");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176
Damien Miller95def091999-11-25 00:26:21 +1100177 BN_bin2bn(outbuf, len, out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178
Damien Miller95def091999-11-25 00:26:21 +1100179 memset(outbuf, 0, olen);
180 memset(inbuf, 0, ilen);
181 xfree(outbuf);
182 xfree(inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183}
184
185/* Set whether to output verbose messages during key generation. */
186
187void
188rsa_set_verbose(int verbose)
189{
Damien Miller95def091999-11-25 00:26:21 +1100190 rsa_verbose = verbose;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191}