xref: /titanic_50/usr/src/cmd/ssh/libssh/common/rsa.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4*7c478bd9Sstevel@tonic-gate  *                    All rights reserved
5*7c478bd9Sstevel@tonic-gate  *
6*7c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
7*7c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
8*7c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
9*7c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
10*7c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
11*7c478bd9Sstevel@tonic-gate  *
12*7c478bd9Sstevel@tonic-gate  *
13*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 Niels Provos.  All rights reserved.
14*7c478bd9Sstevel@tonic-gate  *
15*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
16*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
17*7c478bd9Sstevel@tonic-gate  * are met:
18*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
19*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
20*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
21*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
22*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
23*7c478bd9Sstevel@tonic-gate  *
24*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26*7c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27*7c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28*7c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29*7c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30*7c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31*7c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32*7c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  * Description of the RSA algorithm can be found e.g. from the following
37*7c478bd9Sstevel@tonic-gate  * sources:
38*7c478bd9Sstevel@tonic-gate  *
39*7c478bd9Sstevel@tonic-gate  *   Bruce Schneier: Applied Cryptography.  John Wiley & Sons, 1994.
40*7c478bd9Sstevel@tonic-gate  *
41*7c478bd9Sstevel@tonic-gate  *   Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to
42*7c478bd9Sstevel@tonic-gate  *   Computer Security.  Prentice-Hall, 1989.
43*7c478bd9Sstevel@tonic-gate  *
44*7c478bd9Sstevel@tonic-gate  *   Man Young Rhee: Cryptography and Secure Data Communications.  McGraw-Hill,
45*7c478bd9Sstevel@tonic-gate  *   1994.
46*7c478bd9Sstevel@tonic-gate  *
47*7c478bd9Sstevel@tonic-gate  *   R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications
48*7c478bd9Sstevel@tonic-gate  *   System and Method.  US Patent 4,405,829, 1983.
49*7c478bd9Sstevel@tonic-gate  *
50*7c478bd9Sstevel@tonic-gate  *   Hans Riesel: Prime Numbers and Computer Methods for Factorization.
51*7c478bd9Sstevel@tonic-gate  *   Birkhauser, 1994.
52*7c478bd9Sstevel@tonic-gate  *
53*7c478bd9Sstevel@tonic-gate  *   The RSA Frequently Asked Questions document by RSA Data Security,
54*7c478bd9Sstevel@tonic-gate  *   Inc., 1995.
55*7c478bd9Sstevel@tonic-gate  *
56*7c478bd9Sstevel@tonic-gate  *   RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as
57*7c478bd9Sstevel@tonic-gate  * included below:
58*7c478bd9Sstevel@tonic-gate  *
59*7c478bd9Sstevel@tonic-gate  *     [gone - had to be deleted - what a pity]
60*7c478bd9Sstevel@tonic-gate  */
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate #include "includes.h"
63*7c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: rsa.c,v 1.24 2001/12/27 18:22:16 markus Exp $");
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate #include "rsa.h"
68*7c478bd9Sstevel@tonic-gate #include "log.h"
69*7c478bd9Sstevel@tonic-gate #include "xmalloc.h"
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate void
rsa_public_encrypt(BIGNUM * out,BIGNUM * in,RSA * key)72*7c478bd9Sstevel@tonic-gate rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
73*7c478bd9Sstevel@tonic-gate {
74*7c478bd9Sstevel@tonic-gate 	u_char *inbuf, *outbuf;
75*7c478bd9Sstevel@tonic-gate 	int len, ilen, olen;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
78*7c478bd9Sstevel@tonic-gate 		fatal("rsa_public_encrypt() exponent too small or not odd");
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	olen = BN_num_bytes(key->n);
81*7c478bd9Sstevel@tonic-gate 	outbuf = xmalloc(olen);
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	ilen = BN_num_bytes(in);
84*7c478bd9Sstevel@tonic-gate 	inbuf = xmalloc(ilen);
85*7c478bd9Sstevel@tonic-gate 	BN_bn2bin(in, inbuf);
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
88*7c478bd9Sstevel@tonic-gate 	    RSA_PKCS1_PADDING)) <= 0)
89*7c478bd9Sstevel@tonic-gate 		fatal("rsa_public_encrypt() failed");
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	BN_bin2bn(outbuf, len, out);
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	memset(outbuf, 0, olen);
94*7c478bd9Sstevel@tonic-gate 	memset(inbuf, 0, ilen);
95*7c478bd9Sstevel@tonic-gate 	xfree(outbuf);
96*7c478bd9Sstevel@tonic-gate 	xfree(inbuf);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate int
rsa_private_decrypt(BIGNUM * out,BIGNUM * in,RSA * key)100*7c478bd9Sstevel@tonic-gate rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
101*7c478bd9Sstevel@tonic-gate {
102*7c478bd9Sstevel@tonic-gate 	u_char *inbuf, *outbuf;
103*7c478bd9Sstevel@tonic-gate 	int len, ilen, olen;
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate 	olen = BN_num_bytes(key->n);
106*7c478bd9Sstevel@tonic-gate 	outbuf = xmalloc(olen);
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	ilen = BN_num_bytes(in);
109*7c478bd9Sstevel@tonic-gate 	inbuf = xmalloc(ilen);
110*7c478bd9Sstevel@tonic-gate 	BN_bn2bin(in, inbuf);
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
113*7c478bd9Sstevel@tonic-gate 	    RSA_PKCS1_PADDING)) <= 0) {
114*7c478bd9Sstevel@tonic-gate 		error("rsa_private_decrypt() failed");
115*7c478bd9Sstevel@tonic-gate 	} else {
116*7c478bd9Sstevel@tonic-gate 		BN_bin2bn(outbuf, len, out);
117*7c478bd9Sstevel@tonic-gate 	}
118*7c478bd9Sstevel@tonic-gate 	memset(outbuf, 0, olen);
119*7c478bd9Sstevel@tonic-gate 	memset(inbuf, 0, ilen);
120*7c478bd9Sstevel@tonic-gate 	xfree(outbuf);
121*7c478bd9Sstevel@tonic-gate 	xfree(inbuf);
122*7c478bd9Sstevel@tonic-gate 	return len;
123*7c478bd9Sstevel@tonic-gate }
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate /* calculate p-1 and q-1 */
126*7c478bd9Sstevel@tonic-gate void
rsa_generate_additional_parameters(RSA * rsa)127*7c478bd9Sstevel@tonic-gate rsa_generate_additional_parameters(RSA *rsa)
128*7c478bd9Sstevel@tonic-gate {
129*7c478bd9Sstevel@tonic-gate 	BIGNUM *aux;
130*7c478bd9Sstevel@tonic-gate 	BN_CTX *ctx;
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	if ((aux = BN_new()) == NULL)
133*7c478bd9Sstevel@tonic-gate 		fatal("rsa_generate_additional_parameters: BN_new failed");
134*7c478bd9Sstevel@tonic-gate 	if ((ctx = BN_CTX_new()) == NULL)
135*7c478bd9Sstevel@tonic-gate 		fatal("rsa_generate_additional_parameters: BN_CTX_new failed");
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	BN_sub(aux, rsa->q, BN_value_one());
138*7c478bd9Sstevel@tonic-gate 	BN_mod(rsa->dmq1, rsa->d, aux, ctx);
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	BN_sub(aux, rsa->p, BN_value_one());
141*7c478bd9Sstevel@tonic-gate 	BN_mod(rsa->dmp1, rsa->d, aux, ctx);
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	BN_clear_free(aux);
144*7c478bd9Sstevel@tonic-gate 	BN_CTX_free(ctx);
145*7c478bd9Sstevel@tonic-gate }
146*7c478bd9Sstevel@tonic-gate 
147