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 | fdb5f13 | 1999-12-17 14:02:47 +1100 | [diff] [blame^] | 38 | RCSID("$Id: rsa.c,v 1.6 1999/12/17 03:02:47 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" |
| 43 | |
| 44 | int rsa_verbose = 1; |
| 45 | |
| 46 | int |
| 47 | rsa_alive() |
| 48 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 49 | RSA *key; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 51 | key = RSA_generate_key(32, 3, NULL, NULL); |
| 52 | if (key == NULL) |
| 53 | return (0); |
| 54 | RSA_free(key); |
| 55 | return (1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 56 | } |
| 57 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 58 | /* |
Damien Miller | fdb5f13 | 1999-12-17 14:02:47 +1100 | [diff] [blame^] | 59 | * Key generation progress meter callback |
| 60 | */ |
| 61 | void |
| 62 | keygen_progress(int p, int n, void *arg) |
| 63 | { |
| 64 | const char progress_chars[] = ".o+O?"; |
| 65 | |
| 66 | if ((p < 0) || (p > (sizeof(progress_chars) - 2))) |
| 67 | p = 4; |
| 68 | |
| 69 | printf("%c", progress_chars[p]); |
| 70 | fflush(stdout); |
| 71 | } |
| 72 | |
| 73 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 74 | * Generates RSA public and private keys. This initializes the data |
| 75 | * structures; they should be freed with rsa_clear_private_key and |
| 76 | * rsa_clear_public_key. |
| 77 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 78 | |
| 79 | void |
| 80 | rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits) |
| 81 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 82 | RSA *key; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 83 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 84 | if (rsa_verbose) { |
| 85 | printf("Generating RSA keys: "); |
| 86 | fflush(stdout); |
Damien Miller | fdb5f13 | 1999-12-17 14:02:47 +1100 | [diff] [blame^] | 87 | key = RSA_generate_key(bits, 35, keygen_progress, NULL); |
| 88 | printf("\n"); |
| 89 | } else { |
| 90 | key = RSA_generate_key(bits, 35, NULL, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 91 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 92 | if (key == NULL) |
| 93 | fatal("rsa_generate_key: key generation failed."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 94 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 95 | /* Copy public key parameters */ |
| 96 | pub->n = BN_new(); |
| 97 | BN_copy(pub->n, key->n); |
| 98 | pub->e = BN_new(); |
| 99 | BN_copy(pub->e, key->e); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 100 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 101 | /* Copy private key parameters */ |
| 102 | prv->n = BN_new(); |
| 103 | BN_copy(prv->n, key->n); |
| 104 | prv->e = BN_new(); |
| 105 | BN_copy(prv->e, key->e); |
| 106 | prv->d = BN_new(); |
| 107 | BN_copy(prv->d, key->d); |
| 108 | prv->p = BN_new(); |
| 109 | BN_copy(prv->p, key->p); |
| 110 | prv->q = BN_new(); |
| 111 | BN_copy(prv->q, key->q); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 112 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 113 | prv->dmp1 = BN_new(); |
| 114 | BN_copy(prv->dmp1, key->dmp1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 115 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 116 | prv->dmq1 = BN_new(); |
| 117 | BN_copy(prv->dmq1, key->dmq1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 119 | prv->iqmp = BN_new(); |
| 120 | BN_copy(prv->iqmp, key->iqmp); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 121 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 122 | RSA_free(key); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 123 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 124 | if (rsa_verbose) |
| 125 | printf("Key generation complete.\n"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 129 | rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 130 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 131 | char *inbuf, *outbuf; |
| 132 | int len, ilen, olen; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 133 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 134 | if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e)) |
| 135 | fatal("rsa_public_encrypt() exponent too small or not odd"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 136 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 137 | olen = BN_num_bytes(key->n); |
| 138 | outbuf = xmalloc(olen); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 139 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 140 | ilen = BN_num_bytes(in); |
| 141 | inbuf = xmalloc(ilen); |
| 142 | BN_bn2bin(in, inbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 143 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 144 | if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key, |
| 145 | RSA_PKCS1_PADDING)) <= 0) |
| 146 | fatal("rsa_public_encrypt() failed"); |
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 | BN_bin2bn(outbuf, len, out); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 149 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 150 | memset(outbuf, 0, olen); |
| 151 | memset(inbuf, 0, ilen); |
| 152 | xfree(outbuf); |
| 153 | xfree(inbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void |
| 157 | rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key) |
| 158 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 159 | char *inbuf, *outbuf; |
| 160 | int len, ilen, olen; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 161 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 162 | olen = BN_num_bytes(key->n); |
| 163 | outbuf = xmalloc(olen); |
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 | ilen = BN_num_bytes(in); |
| 166 | inbuf = xmalloc(ilen); |
| 167 | BN_bn2bin(in, inbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 168 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 169 | if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key, |
| 170 | RSA_SSLV23_PADDING)) <= 0) |
| 171 | fatal("rsa_private_decrypt() failed"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 172 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 173 | BN_bin2bn(outbuf, len, out); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 174 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 175 | memset(outbuf, 0, olen); |
| 176 | memset(inbuf, 0, ilen); |
| 177 | xfree(outbuf); |
| 178 | xfree(inbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* Set whether to output verbose messages during key generation. */ |
| 182 | |
| 183 | void |
| 184 | rsa_set_verbose(int verbose) |
| 185 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 186 | rsa_verbose = verbose; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 187 | } |