1 /* $OpenBSD: rsa.c,v 1.32 2014/06/24 01:13:21 djm Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
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".
14 * Copyright (c) 1999 Niels Provos. All rights reserved.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
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.
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.
37 * Description of the RSA algorithm can be found e.g. from the following
40 * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1994.
42 * Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to
43 * Computer Security. Prentice-Hall, 1989.
45 * Man Young Rhee: Cryptography and Secure Data Communications. McGraw-Hill,
48 * R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications
49 * System and Method. US Patent 4,405,829, 1983.
51 * Hans Riesel: Prime Numbers and Computer Methods for Factorization.
54 * The RSA Frequently Asked Questions document by RSA Data Security,
57 * RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as
60 * [gone - had to be deleted - what a pity]
65 #include <sys/types.h>
75 rsa_public_encrypt(BIGNUM
*out
, BIGNUM
*in
, RSA
*key
)
77 u_char
*inbuf
= NULL
, *outbuf
= NULL
;
78 int len
, ilen
, olen
, r
= SSH_ERR_INTERNAL_ERROR
;
80 if (BN_num_bits(key
->e
) < 2 || !BN_is_odd(key
->e
))
81 return SSH_ERR_INVALID_ARGUMENT
;
83 olen
= BN_num_bytes(key
->n
);
84 if ((outbuf
= malloc(olen
)) == NULL
) {
85 r
= SSH_ERR_ALLOC_FAIL
;
89 ilen
= BN_num_bytes(in
);
90 if ((inbuf
= malloc(ilen
)) == NULL
) {
91 r
= SSH_ERR_ALLOC_FAIL
;
96 if ((len
= RSA_public_encrypt(ilen
, inbuf
, outbuf
, key
,
97 RSA_PKCS1_PADDING
)) <= 0) {
98 r
= SSH_ERR_LIBCRYPTO_ERROR
;
102 if (BN_bin2bn(outbuf
, len
, out
) == NULL
) {
103 r
= SSH_ERR_LIBCRYPTO_ERROR
;
109 if (outbuf
!= NULL
) {
110 explicit_bzero(outbuf
, olen
);
114 explicit_bzero(inbuf
, ilen
);
121 rsa_private_decrypt(BIGNUM
*out
, BIGNUM
*in
, RSA
*key
)
123 u_char
*inbuf
= NULL
, *outbuf
= NULL
;
124 int len
, ilen
, olen
, r
= SSH_ERR_INTERNAL_ERROR
;
126 olen
= BN_num_bytes(key
->n
);
127 if ((outbuf
= malloc(olen
)) == NULL
) {
128 r
= SSH_ERR_ALLOC_FAIL
;
132 ilen
= BN_num_bytes(in
);
133 if ((inbuf
= malloc(ilen
)) == NULL
) {
134 r
= SSH_ERR_ALLOC_FAIL
;
137 BN_bn2bin(in
, inbuf
);
139 if ((len
= RSA_private_decrypt(ilen
, inbuf
, outbuf
, key
,
140 RSA_PKCS1_PADDING
)) <= 0) {
141 r
= SSH_ERR_LIBCRYPTO_ERROR
;
143 } else if (BN_bin2bn(outbuf
, len
, out
) == NULL
) {
144 r
= SSH_ERR_LIBCRYPTO_ERROR
;
149 if (outbuf
!= NULL
) {
150 explicit_bzero(outbuf
, olen
);
154 explicit_bzero(inbuf
, ilen
);
160 /* calculate p-1 and q-1 */
162 rsa_generate_additional_parameters(RSA
*rsa
)
168 if ((ctx
= BN_CTX_new()) == NULL
)
169 return SSH_ERR_ALLOC_FAIL
;
170 if ((aux
= BN_new()) == NULL
) {
171 r
= SSH_ERR_ALLOC_FAIL
;
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) ||
178 (BN_mod(rsa
->dmp1
, rsa
->d
, aux
, ctx
) == 0)) {
179 r
= SSH_ERR_LIBCRYPTO_ERROR
;