blob: 113ee7fc42347977adc011b5610c790d466c825c [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".
Kevin Stevesef4eea92001-02-05 12:42:17 +000011 *
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"
Ben Lindstromf7297dd2001-07-04 05:02:23 +000063RCSID("$OpenBSD: rsa.c,v 1.23 2001/06/27 05:42:24 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
65#include "rsa.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000066#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067#include "xmalloc.h"
68
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069void
Damien Miller95def091999-11-25 00:26:21 +110070rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071{
Ben Lindstrom46c16222000-12-22 01:43:59 +000072 u_char *inbuf, *outbuf;
Damien Miller95def091999-11-25 00:26:21 +110073 int len, ilen, olen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074
Damien Miller95def091999-11-25 00:26:21 +110075 if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
76 fatal("rsa_public_encrypt() exponent too small or not odd");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077
Damien Miller95def091999-11-25 00:26:21 +110078 olen = BN_num_bytes(key->n);
79 outbuf = xmalloc(olen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100080
Damien Miller95def091999-11-25 00:26:21 +110081 ilen = BN_num_bytes(in);
82 inbuf = xmalloc(ilen);
83 BN_bn2bin(in, inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Damien Miller95def091999-11-25 00:26:21 +110085 if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
Damien Miller98c7ad62000-03-09 21:27:49 +110086 RSA_PKCS1_PADDING)) <= 0)
Damien Miller95def091999-11-25 00:26:21 +110087 fatal("rsa_public_encrypt() failed");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Miller95def091999-11-25 00:26:21 +110089 BN_bin2bn(outbuf, len, out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
Damien Miller95def091999-11-25 00:26:21 +110091 memset(outbuf, 0, olen);
92 memset(inbuf, 0, ilen);
93 xfree(outbuf);
94 xfree(inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095}
96
Damien Miller7650bc62001-01-30 09:27:26 +110097int
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
99{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000100 u_char *inbuf, *outbuf;
Damien Miller95def091999-11-25 00:26:21 +1100101 int len, ilen, olen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102
Damien Miller95def091999-11-25 00:26:21 +1100103 olen = BN_num_bytes(key->n);
104 outbuf = xmalloc(olen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Damien Miller95def091999-11-25 00:26:21 +1100106 ilen = BN_num_bytes(in);
107 inbuf = xmalloc(ilen);
108 BN_bn2bin(in, inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Damien Miller95def091999-11-25 00:26:21 +1100110 if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
Damien Miller7650bc62001-01-30 09:27:26 +1100111 RSA_PKCS1_PADDING)) <= 0) {
112 error("rsa_private_decrypt() failed");
113 } else {
114 BN_bin2bn(outbuf, len, out);
115 }
Damien Miller95def091999-11-25 00:26:21 +1100116 memset(outbuf, 0, olen);
117 memset(inbuf, 0, ilen);
118 xfree(outbuf);
119 xfree(inbuf);
Damien Miller7650bc62001-01-30 09:27:26 +1100120 return len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121}
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000122
123void
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000124rsa_generate_additional_parameters(RSA *rsa)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000125{
126 BIGNUM *aux;
127 BN_CTX *ctx;
128 /* Generate additional parameters */
129 aux = BN_new();
130 ctx = BN_CTX_new();
131
132 BN_sub(aux, rsa->q, BN_value_one());
133 BN_mod(rsa->dmq1, rsa->d, aux, ctx);
134
135 BN_sub(aux, rsa->p, BN_value_one());
136 BN_mod(rsa->dmp1, rsa->d, aux, ctx);
137
138 BN_clear_free(aux);
139 BN_CTX_free(ctx);
140}
141