Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1 | /* $OpenBSD: rsa.c,v 1.32 2014/06/24 01:13:21 djm Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 6 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 7 | * 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 Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 12 | * |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 13 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 14 | * 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 Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 39 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 40 | * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1994. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 41 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 42 | * Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to |
| 43 | * Computer Security. Prentice-Hall, 1989. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 44 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 45 | * Man Young Rhee: Cryptography and Secure Data Communications. McGraw-Hill, |
| 46 | * 1994. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 47 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 48 | * R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications |
| 49 | * System and Method. US Patent 4,405,829, 1983. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 50 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 51 | * Hans Riesel: Prime Numbers and Computer Methods for Factorization. |
| 52 | * Birkhauser, 1994. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 53 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 54 | * The RSA Frequently Asked Questions document by RSA Data Security, |
| 55 | * Inc., 1995. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 56 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 57 | * RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as |
| 58 | * included below: |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 59 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 60 | * [gone - had to be deleted - what a pity] |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 61 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 62 | |
| 63 | #include "includes.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 64 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 65 | #include <sys/types.h> |
| 66 | |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 67 | #include <stdarg.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 68 | #include <string.h> |
| 69 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 70 | #include "rsa.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 71 | #include "log.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 72 | #include "ssherr.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 73 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 74 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 75 | rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 76 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 77 | u_char *inbuf = NULL, *outbuf = NULL; |
| 78 | int len, ilen, olen, r = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 79 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 80 | if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e)) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 81 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 82 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 83 | olen = BN_num_bytes(key->n); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 84 | if ((outbuf = malloc(olen)) == NULL) { |
| 85 | r = SSH_ERR_ALLOC_FAIL; |
| 86 | goto out; |
| 87 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 88 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 89 | ilen = BN_num_bytes(in); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 90 | if ((inbuf = malloc(ilen)) == NULL) { |
| 91 | r = SSH_ERR_ALLOC_FAIL; |
| 92 | goto out; |
| 93 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 94 | BN_bn2bin(in, inbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 95 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 96 | if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 97 | RSA_PKCS1_PADDING)) <= 0) { |
| 98 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 99 | goto out; |
| 100 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 101 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 102 | if (BN_bin2bn(outbuf, len, out) == NULL) { |
| 103 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 104 | goto out; |
| 105 | } |
| 106 | r = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 107 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 108 | out: |
| 109 | if (outbuf != NULL) { |
| 110 | explicit_bzero(outbuf, olen); |
| 111 | free(outbuf); |
| 112 | } |
| 113 | if (inbuf != NULL) { |
| 114 | explicit_bzero(inbuf, ilen); |
| 115 | free(inbuf); |
| 116 | } |
| 117 | return r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | } |
| 119 | |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 120 | int |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 121 | rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key) |
| 122 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 123 | u_char *inbuf = NULL, *outbuf = NULL; |
| 124 | int len, ilen, olen, r = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 125 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 126 | olen = BN_num_bytes(key->n); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 127 | if ((outbuf = malloc(olen)) == NULL) { |
| 128 | r = SSH_ERR_ALLOC_FAIL; |
| 129 | goto out; |
| 130 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 131 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 132 | ilen = BN_num_bytes(in); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 133 | if ((inbuf = malloc(ilen)) == NULL) { |
| 134 | r = SSH_ERR_ALLOC_FAIL; |
| 135 | goto out; |
| 136 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 137 | BN_bn2bin(in, inbuf); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 138 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 139 | if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key, |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 140 | RSA_PKCS1_PADDING)) <= 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 141 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 142 | goto out; |
| 143 | } else if (BN_bin2bn(outbuf, len, out) == NULL) { |
| 144 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 145 | goto out; |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 146 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 147 | r = 0; |
| 148 | out: |
| 149 | if (outbuf != NULL) { |
| 150 | explicit_bzero(outbuf, olen); |
| 151 | free(outbuf); |
| 152 | } |
| 153 | if (inbuf != NULL) { |
| 154 | explicit_bzero(inbuf, ilen); |
| 155 | free(inbuf); |
| 156 | } |
| 157 | return r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 158 | } |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 159 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 160 | /* calculate p-1 and q-1 */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 161 | int |
Ben Lindstrom | f7297dd | 2001-07-04 05:02:23 +0000 | [diff] [blame] | 162 | rsa_generate_additional_parameters(RSA *rsa) |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 163 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 164 | BIGNUM *aux = NULL; |
| 165 | BN_CTX *ctx = NULL; |
| 166 | int r; |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 167 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 168 | if ((ctx = BN_CTX_new()) == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 169 | return SSH_ERR_ALLOC_FAIL; |
| 170 | if ((aux = BN_new()) == NULL) { |
| 171 | r = SSH_ERR_ALLOC_FAIL; |
| 172 | goto out; |
| 173 | } |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 174 | |
Darren Tucker | 0bc8557 | 2006-11-07 23:14:41 +1100 | [diff] [blame] | 175 | if ((BN_sub(aux, rsa->q, BN_value_one()) == 0) || |
| 176 | (BN_mod(rsa->dmq1, rsa->d, aux, ctx) == 0) || |
| 177 | (BN_sub(aux, rsa->p, BN_value_one()) == 0) || |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 178 | (BN_mod(rsa->dmp1, rsa->d, aux, ctx) == 0)) { |
| 179 | r = SSH_ERR_LIBCRYPTO_ERROR; |
| 180 | goto out; |
| 181 | } |
| 182 | r = 0; |
| 183 | out: |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 184 | BN_clear_free(aux); |
| 185 | BN_CTX_free(ctx); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 186 | return r; |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 187 | } |
| 188 | |