Annotation of src/usr.bin/ssh/rsa.c, Revision 1.25
1.1 provos 1: /*
1.10 deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
1.14 markus 5: *
1.16 deraadt 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".
1.21 stevesk 11: *
1.16 deraadt 12: *
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.
1.14 markus 34: *
1.16 deraadt 35: *
36: * Description of the RSA algorithm can be found e.g. from the following
37: * sources:
1.14 markus 38: *
1.10 deraadt 39: * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1994.
1.14 markus 40: *
1.10 deraadt 41: * Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to
42: * Computer Security. Prentice-Hall, 1989.
1.14 markus 43: *
1.10 deraadt 44: * Man Young Rhee: Cryptography and Secure Data Communications. McGraw-Hill,
45: * 1994.
1.14 markus 46: *
1.10 deraadt 47: * R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications
48: * System and Method. US Patent 4,405,829, 1983.
1.14 markus 49: *
1.10 deraadt 50: * Hans Riesel: Prime Numbers and Computer Methods for Factorization.
51: * Birkhauser, 1994.
1.14 markus 52: *
1.16 deraadt 53: * The RSA Frequently Asked Questions document by RSA Data Security,
54: * Inc., 1995.
1.14 markus 55: *
1.16 deraadt 56: * RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as
57: * included below:
1.14 markus 58: *
1.10 deraadt 59: * [gone - had to be deleted - what a pity]
1.16 deraadt 60: */
1.1 provos 61:
62: #include "includes.h"
63:
64: #include "rsa.h"
1.19 markus 65: #include "log.h"
1.1 provos 66: #include "xmalloc.h"
67:
68: void
1.9 markus 69: rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
1.1 provos 70: {
1.18 markus 71: u_char *inbuf, *outbuf;
1.9 markus 72: int len, ilen, olen;
1.4 provos 73:
1.9 markus 74: if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
75: fatal("rsa_public_encrypt() exponent too small or not odd");
1.1 provos 76:
1.9 markus 77: olen = BN_num_bytes(key->n);
78: outbuf = xmalloc(olen);
1.1 provos 79:
1.9 markus 80: ilen = BN_num_bytes(in);
81: inbuf = xmalloc(ilen);
82: BN_bn2bin(in, inbuf);
1.1 provos 83:
1.9 markus 84: if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
1.12 markus 85: RSA_PKCS1_PADDING)) <= 0)
1.9 markus 86: fatal("rsa_public_encrypt() failed");
1.1 provos 87:
1.9 markus 88: BN_bin2bn(outbuf, len, out);
1.1 provos 89:
1.9 markus 90: memset(outbuf, 0, olen);
91: memset(inbuf, 0, ilen);
92: xfree(outbuf);
93: xfree(inbuf);
1.1 provos 94: }
95:
1.20 markus 96: int
1.1 provos 97: rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
98: {
1.18 markus 99: u_char *inbuf, *outbuf;
1.9 markus 100: int len, ilen, olen;
1.1 provos 101:
1.9 markus 102: olen = BN_num_bytes(key->n);
103: outbuf = xmalloc(olen);
1.1 provos 104:
1.9 markus 105: ilen = BN_num_bytes(in);
106: inbuf = xmalloc(ilen);
107: BN_bn2bin(in, inbuf);
108:
109: if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
1.20 markus 110: RSA_PKCS1_PADDING)) <= 0) {
111: error("rsa_private_decrypt() failed");
112: } else {
113: BN_bin2bn(outbuf, len, out);
114: }
1.9 markus 115: memset(outbuf, 0, olen);
116: memset(inbuf, 0, ilen);
117: xfree(outbuf);
118: xfree(inbuf);
1.20 markus 119: return len;
1.1 provos 120: }
1.22 markus 121:
1.24 markus 122: /* calculate p-1 and q-1 */
1.22 markus 123: void
1.23 markus 124: rsa_generate_additional_parameters(RSA *rsa)
1.22 markus 125: {
126: BIGNUM *aux;
127: BN_CTX *ctx;
1.24 markus 128:
129: if ((aux = BN_new()) == NULL)
130: fatal("rsa_generate_additional_parameters: BN_new failed");
131: if ((ctx = BN_CTX_new()) == NULL)
132: fatal("rsa_generate_additional_parameters: BN_CTX_new failed");
1.22 markus 133:
134: BN_sub(aux, rsa->q, BN_value_one());
135: BN_mod(rsa->dmq1, rsa->d, aux, ctx);
136:
137: BN_sub(aux, rsa->p, BN_value_one());
138: BN_mod(rsa->dmp1, rsa->d, aux, ctx);
139:
140: BN_clear_free(aux);
141: BN_CTX_free(ctx);
142: }
143: