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