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 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 5 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame^] | 6 | * As far as I am concerned, the code I have written for this software |
| 7 | * can be used freely for any purpose. Any derived versions of this |
| 8 | * software must be clearly marked as such, and if the derived work is |
| 9 | * incompatible with the protocol description in the RFC file, it must be |
| 10 | * called by a name other than "ssh" or "Secure Shell". |
| 11 | * |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 12 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame^] | 13 | * Copyright (c) 1999 Niels Provos. All rights reserved. |
| 14 | * |
| 15 | * Redistribution and use in source and binary forms, with or without |
| 16 | * modification, are permitted provided that the following conditions |
| 17 | * are met: |
| 18 | * 1. Redistributions of source code must retain the above copyright |
| 19 | * notice, this list of conditions and the following disclaimer. |
| 20 | * 2. Redistributions in binary form must reproduce the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer in the |
| 22 | * documentation and/or other materials provided with the distribution. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 25 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 26 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 27 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 29 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 33 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | * |
| 35 | * |
| 36 | * Description of the RSA algorithm can be found e.g. from the following |
| 37 | * sources: |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 38 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 39 | * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1994. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 40 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 41 | * Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to |
| 42 | * Computer Security. Prentice-Hall, 1989. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 43 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 44 | * Man Young Rhee: Cryptography and Secure Data Communications. McGraw-Hill, |
| 45 | * 1994. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 46 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 47 | * R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications |
| 48 | * System and Method. US Patent 4,405,829, 1983. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 49 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 50 | * Hans Riesel: Prime Numbers and Computer Methods for Factorization. |
| 51 | * Birkhauser, 1994. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 52 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame^] | 53 | * The RSA Frequently Asked Questions document by RSA Data Security, |
| 54 | * Inc., 1995. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 55 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame^] | 56 | * RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as |
| 57 | * included below: |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 58 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 59 | * [gone - had to be deleted - what a pity] |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame^] | 60 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 61 | |
| 62 | #include "includes.h" |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame^] | 63 | RCSID("$OpenBSD: rsa.c,v 1.16 2000/09/07 20:27:53 deraadt Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | |
| 65 | #include "rsa.h" |
| 66 | #include "ssh.h" |
| 67 | #include "xmalloc.h" |
Damien Miller | 040f383 | 2000-04-03 14:50:43 +1000 | [diff] [blame] | 68 | #include "entropy.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | |
| 70 | int rsa_verbose = 1; |
| 71 | |
| 72 | int |
| 73 | rsa_alive() |
| 74 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 75 | RSA *key; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 76 | |
Damien Miller | fac99cd | 2000-03-05 16:10:45 +1100 | [diff] [blame] | 77 | seed_rng(); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 78 | key = RSA_generate_key(32, 3, NULL, NULL); |
| 79 | if (key == NULL) |
| 80 | return (0); |
| 81 | RSA_free(key); |
| 82 | return (1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 83 | } |
| 84 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 85 | /* |
Damien Miller | fdb5f13 | 1999-12-17 14:02:47 +1100 | [diff] [blame] | 86 | * Key generation progress meter callback |
| 87 | */ |
| 88 | void |
| 89 | keygen_progress(int p, int n, void *arg) |
| 90 | { |
| 91 | const char progress_chars[] = ".o+O?"; |
| 92 | |
| 93 | if ((p < 0) || (p > (sizeof(progress_chars) - 2))) |
Damien Miller | f07390e | 2000-01-29 20:40:22 +1100 | [diff] [blame] | 94 | p = sizeof(progress_chars) - 2; |
Damien Miller | fdb5f13 | 1999-12-17 14:02:47 +1100 | [diff] [blame] | 95 | |
Damien Miller | f07390e | 2000-01-29 20:40:22 +1100 | [diff] [blame] | 96 | putchar(progress_chars[p]); |
Damien Miller | fdb5f13 | 1999-12-17 14:02:47 +1100 | [diff] [blame] | 97 | fflush(stdout); |
| 98 | } |
| 99 | |
| 100 | /* |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 101 | * Generates RSA public and private keys. This initializes the data |
| 102 | * structures; they should be freed with rsa_clear_private_key and |
| 103 | * rsa_clear_public_key. |
| 104 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 105 | |
| 106 | void |
| 107 | rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits) |
| 108 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 109 | RSA *key; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 110 | |
Damien Miller | f07390e | 2000-01-29 20:40:22 +1100 | [diff] [blame] | 111 | seed_rng(); |
| 112 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 113 | if (rsa_verbose) { |
| 114 | printf("Generating RSA keys: "); |
| 115 | fflush(stdout); |
Damien Miller | fdb5f13 | 1999-12-17 14:02:47 +1100 | [diff] [blame] | 116 | key = RSA_generate_key(bits, 35, keygen_progress, NULL); |
| 117 | printf("\n"); |
| 118 | } else { |
| 119 | key = RSA_generate_key(bits, 35, NULL, NULL); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 120 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 121 | if (key == NULL) |
| 122 | fatal("rsa_generate_key: key generation failed."); |
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 | /* Copy public key parameters */ |
| 125 | pub->n = BN_new(); |
| 126 | BN_copy(pub->n, key->n); |
| 127 | pub->e = BN_new(); |
| 128 | BN_copy(pub->e, key->e); |
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 | /* Copy private key parameters */ |
| 131 | prv->n = BN_new(); |
| 132 | BN_copy(prv->n, key->n); |
| 133 | prv->e = BN_new(); |
| 134 | BN_copy(prv->e, key->e); |
| 135 | prv->d = BN_new(); |
| 136 | BN_copy(prv->d, key->d); |
| 137 | prv->p = BN_new(); |
| 138 | BN_copy(prv->p, key->p); |
| 139 | prv->q = BN_new(); |
| 140 | BN_copy(prv->q, key->q); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 141 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 142 | prv->dmp1 = BN_new(); |
| 143 | BN_copy(prv->dmp1, key->dmp1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 144 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 145 | prv->dmq1 = BN_new(); |
| 146 | BN_copy(prv->dmq1, key->dmq1); |
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 | prv->iqmp = BN_new(); |
| 149 | BN_copy(prv->iqmp, key->iqmp); |
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 | RSA_free(key); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 152 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 153 | if (rsa_verbose) |
| 154 | printf("Key generation complete.\n"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 158 | rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 159 | { |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 160 | unsigned char *inbuf, *outbuf; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 161 | int len, ilen, olen; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 162 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 163 | if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e)) |
| 164 | fatal("rsa_public_encrypt() exponent too small or not odd"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 165 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 166 | olen = BN_num_bytes(key->n); |
| 167 | outbuf = xmalloc(olen); |
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 | ilen = BN_num_bytes(in); |
| 170 | inbuf = xmalloc(ilen); |
| 171 | BN_bn2bin(in, inbuf); |
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 | if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key, |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 174 | RSA_PKCS1_PADDING)) <= 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 175 | fatal("rsa_public_encrypt() failed"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 176 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 177 | BN_bin2bn(outbuf, len, out); |
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 | memset(outbuf, 0, olen); |
| 180 | memset(inbuf, 0, ilen); |
| 181 | xfree(outbuf); |
| 182 | xfree(inbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void |
| 186 | rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key) |
| 187 | { |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 188 | unsigned char *inbuf, *outbuf; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 189 | int len, ilen, olen; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 190 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 191 | olen = BN_num_bytes(key->n); |
| 192 | outbuf = xmalloc(olen); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 193 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 194 | ilen = BN_num_bytes(in); |
| 195 | inbuf = xmalloc(ilen); |
| 196 | BN_bn2bin(in, inbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 197 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 198 | if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key, |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 199 | RSA_PKCS1_PADDING)) <= 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 200 | fatal("rsa_private_decrypt() failed"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 201 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 202 | BN_bin2bn(outbuf, len, out); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 203 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 204 | memset(outbuf, 0, olen); |
| 205 | memset(inbuf, 0, ilen); |
| 206 | xfree(outbuf); |
| 207 | xfree(inbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* Set whether to output verbose messages during key generation. */ |
| 211 | |
| 212 | void |
| 213 | rsa_set_verbose(int verbose) |
| 214 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 215 | rsa_verbose = verbose; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 216 | } |