blob: 08cc82007162ea7371504219ed8ba270a8c23e2f [file] [log] [blame]
Damien Millerd7834352006-08-05 12:39:39 +10001/* $OpenBSD: rsa.c,v 1.28 2006/08/03 03:34:42 deraadt Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Millere4340be2000-09-16 13:29:08 +11007 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
Kevin Stevesef4eea92001-02-05 12:42:17 +000012 *
Damien Miller4af51302000-04-16 11:18:38 +100013 *
Damien Millere4340be2000-09-16 13:29:08 +110014 * Copyright (c) 1999 Niels Provos. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 *
37 * Description of the RSA algorithm can be found e.g. from the following
38 * sources:
Damien Miller4af51302000-04-16 11:18:38 +100039 *
Damien Miller95def091999-11-25 00:26:21 +110040 * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1994.
Damien Miller4af51302000-04-16 11:18:38 +100041 *
Damien Miller95def091999-11-25 00:26:21 +110042 * Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to
43 * Computer Security. Prentice-Hall, 1989.
Damien Miller4af51302000-04-16 11:18:38 +100044 *
Damien Miller95def091999-11-25 00:26:21 +110045 * Man Young Rhee: Cryptography and Secure Data Communications. McGraw-Hill,
46 * 1994.
Damien Miller4af51302000-04-16 11:18:38 +100047 *
Damien Miller95def091999-11-25 00:26:21 +110048 * R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications
49 * System and Method. US Patent 4,405,829, 1983.
Damien Miller4af51302000-04-16 11:18:38 +100050 *
Damien Miller95def091999-11-25 00:26:21 +110051 * Hans Riesel: Prime Numbers and Computer Methods for Factorization.
52 * Birkhauser, 1994.
Damien Miller4af51302000-04-16 11:18:38 +100053 *
Damien Millere4340be2000-09-16 13:29:08 +110054 * The RSA Frequently Asked Questions document by RSA Data Security,
55 * Inc., 1995.
Damien Miller4af51302000-04-16 11:18:38 +100056 *
Damien Millere4340be2000-09-16 13:29:08 +110057 * RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as
58 * included below:
Damien Miller4af51302000-04-16 11:18:38 +100059 *
Damien Miller95def091999-11-25 00:26:21 +110060 * [gone - had to be deleted - what a pity]
Damien Millere4340be2000-09-16 13:29:08 +110061 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
63#include "includes.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
Damien Millerd7834352006-08-05 12:39:39 +100065#include <sys/types.h>
66
Damien Millerded319c2006-09-01 15:38:36 +100067#include <stdarg.h>
Damien Millere3476ed2006-07-24 14:13:33 +100068#include <string.h>
69
Damien Millerd7834352006-08-05 12:39:39 +100070#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071#include "rsa.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000072#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074void
Damien Miller95def091999-11-25 00:26:21 +110075rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100076{
Ben Lindstrom46c16222000-12-22 01:43:59 +000077 u_char *inbuf, *outbuf;
Damien Miller95def091999-11-25 00:26:21 +110078 int len, ilen, olen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Miller95def091999-11-25 00:26:21 +110080 if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
81 fatal("rsa_public_encrypt() exponent too small or not odd");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Miller95def091999-11-25 00:26:21 +110083 olen = BN_num_bytes(key->n);
84 outbuf = xmalloc(olen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Miller95def091999-11-25 00:26:21 +110086 ilen = BN_num_bytes(in);
87 inbuf = xmalloc(ilen);
88 BN_bn2bin(in, inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Damien Miller95def091999-11-25 00:26:21 +110090 if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
Damien Miller98c7ad62000-03-09 21:27:49 +110091 RSA_PKCS1_PADDING)) <= 0)
Damien Miller95def091999-11-25 00:26:21 +110092 fatal("rsa_public_encrypt() failed");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Miller95def091999-11-25 00:26:21 +110094 BN_bin2bn(outbuf, len, out);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
Damien Miller95def091999-11-25 00:26:21 +110096 memset(outbuf, 0, olen);
97 memset(inbuf, 0, ilen);
98 xfree(outbuf);
99 xfree(inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100}
101
Damien Miller7650bc62001-01-30 09:27:26 +1100102int
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
104{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000105 u_char *inbuf, *outbuf;
Damien Miller95def091999-11-25 00:26:21 +1100106 int len, ilen, olen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000107
Damien Miller95def091999-11-25 00:26:21 +1100108 olen = BN_num_bytes(key->n);
109 outbuf = xmalloc(olen);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000110
Damien Miller95def091999-11-25 00:26:21 +1100111 ilen = BN_num_bytes(in);
112 inbuf = xmalloc(ilen);
113 BN_bn2bin(in, inbuf);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Damien Miller95def091999-11-25 00:26:21 +1100115 if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
Damien Miller7650bc62001-01-30 09:27:26 +1100116 RSA_PKCS1_PADDING)) <= 0) {
117 error("rsa_private_decrypt() failed");
118 } else {
119 BN_bin2bn(outbuf, len, out);
120 }
Damien Miller95def091999-11-25 00:26:21 +1100121 memset(outbuf, 0, olen);
122 memset(inbuf, 0, ilen);
123 xfree(outbuf);
124 xfree(inbuf);
Damien Miller7650bc62001-01-30 09:27:26 +1100125 return len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000126}
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000127
Damien Millerda755162002-01-22 23:09:22 +1100128/* calculate p-1 and q-1 */
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000129void
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000130rsa_generate_additional_parameters(RSA *rsa)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000131{
132 BIGNUM *aux;
133 BN_CTX *ctx;
Damien Millerda755162002-01-22 23:09:22 +1100134
135 if ((aux = BN_new()) == NULL)
136 fatal("rsa_generate_additional_parameters: BN_new failed");
137 if ((ctx = BN_CTX_new()) == NULL)
138 fatal("rsa_generate_additional_parameters: BN_CTX_new failed");
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000139
140 BN_sub(aux, rsa->q, BN_value_one());
141 BN_mod(rsa->dmq1, rsa->d, aux, ctx);
142
143 BN_sub(aux, rsa->p, BN_value_one());
144 BN_mod(rsa->dmp1, rsa->d, aux, ctx);
145
146 BN_clear_free(aux);
147 BN_CTX_free(ctx);
148}
149