blob: 0c9aef7916e1be63f7828926cfd94e2dd635674f [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10005 *
Damien Millere4340be2000-09-16 13:29:08 +11006 * 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 Miller4af51302000-04-16 11:18:38 +100012 *
Damien Millere4340be2000-09-16 13:29:08 +110013 * 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 Miller4af51302000-04-16 11:18:38 +100038 *
Damien Miller95def091999-11-25 00:26:21 +110039 * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1994.
Damien Miller4af51302000-04-16 11:18:38 +100040 *
Damien Miller95def091999-11-25 00:26:21 +110041 * Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to
42 * Computer Security. Prentice-Hall, 1989.
Damien Miller4af51302000-04-16 11:18:38 +100043 *
Damien Miller95def091999-11-25 00:26:21 +110044 * Man Young Rhee: Cryptography and Secure Data Communications. McGraw-Hill,
45 * 1994.
Damien Miller4af51302000-04-16 11:18:38 +100046 *
Damien Miller95def091999-11-25 00:26:21 +110047 * R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications
48 * System and Method. US Patent 4,405,829, 1983.
Damien Miller4af51302000-04-16 11:18:38 +100049 *
Damien Miller95def091999-11-25 00:26:21 +110050 * Hans Riesel: Prime Numbers and Computer Methods for Factorization.
51 * Birkhauser, 1994.
Damien Miller4af51302000-04-16 11:18:38 +100052 *
Damien Millere4340be2000-09-16 13:29:08 +110053 * The RSA Frequently Asked Questions document by RSA Data Security,
54 * Inc., 1995.
Damien Miller4af51302000-04-16 11:18:38 +100055 *
Damien Millere4340be2000-09-16 13:29:08 +110056 * RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as
57 * included below:
Damien Miller4af51302000-04-16 11:18:38 +100058 *
Damien Miller95def091999-11-25 00:26:21 +110059 * [gone - had to be deleted - what a pity]
Damien Millere4340be2000-09-16 13:29:08 +110060 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
62#include "includes.h"
Damien Millere4340be2000-09-16 13:29:08 +110063RCSID("$OpenBSD: rsa.c,v 1.16 2000/09/07 20:27:53 deraadt Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
65#include "rsa.h"
66#include "ssh.h"
67#include "xmalloc.h"
Damien Miller040f3832000-04-03 14:50:43 +100068#include "entropy.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
70int rsa_verbose = 1;
71
72int
73rsa_alive()
74{
Damien Miller95def091999-11-25 00:26:21 +110075 RSA *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076
Damien Millerfac99cd2000-03-05 16:10:45 +110077 seed_rng();
Damien Miller95def091999-11-25 00:26:21 +110078 key = RSA_generate_key(32, 3, NULL, NULL);
79 if (key == NULL)
80 return (0);
81 RSA_free(key);
82 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083}
84
Damien Miller5428f641999-11-25 11:54:57 +110085/*
86 * Generates RSA public and private keys. This initializes the data
87 * structures; they should be freed with rsa_clear_private_key and
88 * rsa_clear_public_key.
89 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
91void
92rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits)
93{
Damien Miller95def091999-11-25 00:26:21 +110094 RSA *key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
Damien Millerf07390e2000-01-29 20:40:22 +110096 seed_rng();
97
Damien Miller95def091999-11-25 00:26:21 +110098 if (rsa_verbose) {
99 printf("Generating RSA keys: ");
100 fflush(stdout);
101 }
Damien Millercb5e44a2000-09-29 12:12:36 +1100102 key = RSA_generate_key(bits, 35, NULL, NULL);
Damien Miller95def091999-11-25 00:26:21 +1100103 if (key == NULL)
104 fatal("rsa_generate_key: key generation failed.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Damien Miller95def091999-11-25 00:26:21 +1100106 /* Copy public key parameters */
107 pub->n = BN_new();
108 BN_copy(pub->n, key->n);
109 pub->e = BN_new();
110 BN_copy(pub->e, key->e);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111
Damien Miller95def091999-11-25 00:26:21 +1100112 /* Copy private key parameters */
113 prv->n = BN_new();
114 BN_copy(prv->n, key->n);
115 prv->e = BN_new();
116 BN_copy(prv->e, key->e);
117 prv->d = BN_new();
118 BN_copy(prv->d, key->d);
119 prv->p = BN_new();
120 BN_copy(prv->p, key->p);
121 prv->q = BN_new();
122 BN_copy(prv->q, key->q);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123
Damien Miller95def091999-11-25 00:26:21 +1100124 prv->dmp1 = BN_new();
125 BN_copy(prv->dmp1, key->dmp1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126
Damien Miller95def091999-11-25 00:26:21 +1100127 prv->dmq1 = BN_new();
128 BN_copy(prv->dmq1, key->dmq1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000129
Damien Miller95def091999-11-25 00:26:21 +1100130 prv->iqmp = BN_new();
131 BN_copy(prv->iqmp, key->iqmp);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Damien Miller95def091999-11-25 00:26:21 +1100133 RSA_free(key);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134
Damien Miller95def091999-11-25 00:26:21 +1100135 if (rsa_verbose)
136 printf("Key generation complete.\n");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137}
138
139void
Damien Miller95def091999-11-25 00:26:21 +1100140rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141{
Damien Miller7684ee12000-03-17 23:40:15 +1100142 unsigned char *inbuf, *outbuf;
Damien Miller95def091999-11-25 00:26:21 +1100143 int len, ilen, olen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller95def091999-11-25 00:26:21 +1100145 if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
146 fatal("rsa_public_encrypt() exponent too small or not odd");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147
Damien Miller95def091999-11-25 00:26:21 +1100148 olen = BN_num_bytes(key->n);
149 outbuf = xmalloc(olen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
Damien Miller95def091999-11-25 00:26:21 +1100151 ilen = BN_num_bytes(in);
152 inbuf = xmalloc(ilen);
153 BN_bn2bin(in, inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000154
Damien Miller95def091999-11-25 00:26:21 +1100155 if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
Damien Miller98c7ad62000-03-09 21:27:49 +1100156 RSA_PKCS1_PADDING)) <= 0)
Damien Miller95def091999-11-25 00:26:21 +1100157 fatal("rsa_public_encrypt() failed");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158
Damien Miller95def091999-11-25 00:26:21 +1100159 BN_bin2bn(outbuf, len, out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Miller95def091999-11-25 00:26:21 +1100161 memset(outbuf, 0, olen);
162 memset(inbuf, 0, ilen);
163 xfree(outbuf);
164 xfree(inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165}
166
167void
168rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
169{
Damien Miller7684ee12000-03-17 23:40:15 +1100170 unsigned char *inbuf, *outbuf;
Damien Miller95def091999-11-25 00:26:21 +1100171 int len, ilen, olen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172
Damien Miller95def091999-11-25 00:26:21 +1100173 olen = BN_num_bytes(key->n);
174 outbuf = xmalloc(olen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175
Damien Miller95def091999-11-25 00:26:21 +1100176 ilen = BN_num_bytes(in);
177 inbuf = xmalloc(ilen);
178 BN_bn2bin(in, inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000179
Damien Miller95def091999-11-25 00:26:21 +1100180 if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
Damien Miller98c7ad62000-03-09 21:27:49 +1100181 RSA_PKCS1_PADDING)) <= 0)
Damien Miller95def091999-11-25 00:26:21 +1100182 fatal("rsa_private_decrypt() failed");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183
Damien Miller95def091999-11-25 00:26:21 +1100184 BN_bin2bn(outbuf, len, out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185
Damien Miller95def091999-11-25 00:26:21 +1100186 memset(outbuf, 0, olen);
187 memset(inbuf, 0, ilen);
188 xfree(outbuf);
189 xfree(inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190}
191
192/* Set whether to output verbose messages during key generation. */
193
194void
195rsa_set_verbose(int verbose)
196{
Damien Miller95def091999-11-25 00:26:21 +1100197 rsa_verbose = verbose;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198}