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