Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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 | * |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 35 | */ |
| 36 | |
| 37 | #include "includes.h" |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 38 | RCSID("$Id: rsa.c,v 1.10 2000/03/09 10:27:51 damien Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | |
| 40 | #include "rsa.h" |
| 41 | #include "ssh.h" |
| 42 | #include "xmalloc.h" |
Damien Miller | f07390e | 2000-01-29 20:40:22 +1100 | [diff] [blame] | 43 | #include "random.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | |
| 45 | int rsa_verbose = 1; |
| 46 | |
Damien Miller | cf87b5b | 2000-03-05 17:14:38 +1100 | [diff] [blame] | 47 | /* |
| 48 | * Seed OpenSSL's random number generator |
| 49 | */ |
| 50 | void |
| 51 | seed_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 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 60 | int |
| 61 | rsa_alive() |
| 62 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 63 | RSA *key; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | |
Damien Miller | fac99cd | 2000-03-05 16:10:45 +1100 | [diff] [blame] | 65 | seed_rng(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 66 | key = RSA_generate_key(32, 3, NULL, NULL); |
| 67 | if (key == NULL) |
| 68 | return (0); |
| 69 | RSA_free(key); |
| 70 | return (1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 71 | } |
| 72 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 73 | /* |
Damien Miller | fdb5f13 | 1999-12-17 14:02:47 +1100 | [diff] [blame] | 74 | * Key generation progress meter callback |
| 75 | */ |
| 76 | void |
| 77 | keygen_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))) |
Damien Miller | f07390e | 2000-01-29 20:40:22 +1100 | [diff] [blame] | 82 | p = sizeof(progress_chars) - 2; |
Damien Miller | fdb5f13 | 1999-12-17 14:02:47 +1100 | [diff] [blame] | 83 | |
Damien Miller | f07390e | 2000-01-29 20:40:22 +1100 | [diff] [blame] | 84 | putchar(progress_chars[p]); |
Damien Miller | fdb5f13 | 1999-12-17 14:02:47 +1100 | [diff] [blame] | 85 | fflush(stdout); |
| 86 | } |
| 87 | |
| 88 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 93 | |
| 94 | void |
| 95 | rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits) |
| 96 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 97 | RSA *key; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 98 | |
Damien Miller | f07390e | 2000-01-29 20:40:22 +1100 | [diff] [blame] | 99 | seed_rng(); |
| 100 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 101 | if (rsa_verbose) { |
| 102 | printf("Generating RSA keys: "); |
| 103 | fflush(stdout); |
Damien Miller | fdb5f13 | 1999-12-17 14:02:47 +1100 | [diff] [blame] | 104 | key = RSA_generate_key(bits, 35, keygen_progress, NULL); |
| 105 | printf("\n"); |
| 106 | } else { |
| 107 | key = RSA_generate_key(bits, 35, NULL, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 108 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 109 | if (key == NULL) |
| 110 | fatal("rsa_generate_key: key generation failed."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 111 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 117 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 129 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 130 | prv->dmp1 = BN_new(); |
| 131 | BN_copy(prv->dmp1, key->dmp1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 132 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 133 | prv->dmq1 = BN_new(); |
| 134 | BN_copy(prv->dmq1, key->dmq1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 135 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 136 | prv->iqmp = BN_new(); |
| 137 | BN_copy(prv->iqmp, key->iqmp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 138 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 139 | RSA_free(key); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 140 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 141 | if (rsa_verbose) |
| 142 | printf("Key generation complete.\n"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 146 | rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 147 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 148 | char *inbuf, *outbuf; |
| 149 | int len, ilen, olen; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 150 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 151 | if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e)) |
| 152 | fatal("rsa_public_encrypt() exponent too small or not odd"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 153 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 154 | olen = BN_num_bytes(key->n); |
| 155 | outbuf = xmalloc(olen); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 156 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 157 | ilen = BN_num_bytes(in); |
| 158 | inbuf = xmalloc(ilen); |
| 159 | BN_bn2bin(in, inbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 160 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 161 | if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key, |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 162 | RSA_PKCS1_PADDING)) <= 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 163 | fatal("rsa_public_encrypt() failed"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 164 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 165 | BN_bin2bn(outbuf, len, out); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 166 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 167 | memset(outbuf, 0, olen); |
| 168 | memset(inbuf, 0, ilen); |
| 169 | xfree(outbuf); |
| 170 | xfree(inbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void |
| 174 | rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key) |
| 175 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 176 | char *inbuf, *outbuf; |
| 177 | int len, ilen, olen; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 178 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 179 | olen = BN_num_bytes(key->n); |
| 180 | outbuf = xmalloc(olen); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 181 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 182 | ilen = BN_num_bytes(in); |
| 183 | inbuf = xmalloc(ilen); |
| 184 | BN_bn2bin(in, inbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 185 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 186 | if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key, |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 187 | RSA_PKCS1_PADDING)) <= 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 188 | fatal("rsa_private_decrypt() failed"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 189 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 190 | BN_bin2bn(outbuf, len, out); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 191 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 192 | memset(outbuf, 0, olen); |
| 193 | memset(inbuf, 0, ilen); |
| 194 | xfree(outbuf); |
| 195 | xfree(inbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* Set whether to output verbose messages during key generation. */ |
| 199 | |
| 200 | void |
| 201 | rsa_set_verbose(int verbose) |
| 202 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 203 | rsa_verbose = verbose; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 204 | } |