rsa.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
4134N/A/*
4134N/A * Author: Tatu Ylonen <ylo@cs.hut.fi>
4134N/A * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4134N/A * All rights reserved
4134N/A *
4134N/A * As far as I am concerned, the code I have written for this software
4134N/A * can be used freely for any purpose. Any derived versions of this
4134N/A * software must be clearly marked as such, and if the derived work is
6983N/A * incompatible with the protocol description in the RFC file, it must be
6983N/A * called by a name other than "ssh" or "Secure Shell".
4134N/A *
4134N/A *
4134N/A * Copyright (c) 1999 Niels Provos. All rights reserved.
4134N/A *
6983N/A * Redistribution and use in source and binary forms, with or without
6983N/A * modification, are permitted provided that the following conditions
6983N/A * are met:
6983N/A * 1. Redistributions of source code must retain the above copyright
4134N/A * notice, this list of conditions and the following disclaimer.
4134N/A * 2. Redistributions in binary form must reproduce the above copyright
4134N/A * notice, this list of conditions and the following disclaimer in the
4134N/A * documentation and/or other materials provided with the distribution.
4134N/A *
4158N/A * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
6143N/A * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
4134N/A * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
4134N/A * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
4134N/A * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
5726N/A * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
5726N/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
6143N/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
4926N/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
4134N/A * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4134N/A *
4134N/A *
4134N/A * Description of the RSA algorithm can be found e.g. from the following
4134N/A * sources:
6143N/A *
4134N/A * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1994.
4134N/A *
4926N/A * Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to
6143N/A * Computer Security. Prentice-Hall, 1989.
6143N/A *
5734N/A * Man Young Rhee: Cryptography and Secure Data Communications. McGraw-Hill,
6143N/A * 1994.
6143N/A *
6143N/A * R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications
4609N/A * System and Method. US Patent 4,405,829, 1983.
4134N/A *
4134N/A * Hans Riesel: Prime Numbers and Computer Methods for Factorization.
4134N/A * Birkhauser, 1994.
5726N/A *
5726N/A * The RSA Frequently Asked Questions document by RSA Data Security,
4134N/A * Inc., 1995.
4134N/A *
4134N/A * RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as
5734N/A * included below:
5726N/A *
5734N/A * [gone - had to be deleted - what a pity]
5734N/A */
5734N/A
5734N/A#include "includes.h"
5734N/ARCSID("$OpenBSD: rsa.c,v 1.24 2001/12/27 18:22:16 markus Exp $");
5734N/A
5734N/A#pragma ident "%Z%%M% %I% %E% SMI"
5734N/A
5734N/A#include "rsa.h"
6143N/A#include "log.h"
5734N/A#include "xmalloc.h"
5734N/A
5734N/Avoid
5734N/Arsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
5734N/A{
5734N/A u_char *inbuf, *outbuf;
5734N/A int len, ilen, olen;
5734N/A
5734N/A if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
5734N/A fatal("rsa_public_encrypt() exponent too small or not odd");
5734N/A
5734N/A olen = BN_num_bytes(key->n);
5734N/A outbuf = xmalloc(olen);
5734N/A
5734N/A ilen = BN_num_bytes(in);
5734N/A inbuf = xmalloc(ilen);
5734N/A BN_bn2bin(in, inbuf);
5734N/A
5734N/A if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
5734N/A RSA_PKCS1_PADDING)) <= 0)
5734N/A fatal("rsa_public_encrypt() failed");
5734N/A
5734N/A BN_bin2bn(outbuf, len, out);
5734N/A
5734N/A memset(outbuf, 0, olen);
5734N/A memset(inbuf, 0, ilen);
5734N/A xfree(outbuf);
5734N/A xfree(inbuf);
5734N/A}
5734N/A
5734N/Aint
5734N/Arsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
5734N/A{
5734N/A u_char *inbuf, *outbuf;
5734N/A int len, ilen, olen;
5734N/A
5734N/A olen = BN_num_bytes(key->n);
5734N/A outbuf = xmalloc(olen);
5734N/A
5734N/A ilen = BN_num_bytes(in);
5734N/A inbuf = xmalloc(ilen);
5734N/A BN_bn2bin(in, inbuf);
5734N/A
5734N/A if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
5734N/A RSA_PKCS1_PADDING)) <= 0) {
5734N/A error("rsa_private_decrypt() failed");
5734N/A } else {
5734N/A BN_bin2bn(outbuf, len, out);
5734N/A }
5734N/A memset(outbuf, 0, olen);
5734N/A memset(inbuf, 0, ilen);
5734N/A xfree(outbuf);
6143N/A xfree(inbuf);
5734N/A return len;
5734N/A}
5734N/A
5734N/A/* calculate p-1 and q-1 */
5734N/Avoid
5734N/Arsa_generate_additional_parameters(RSA *rsa)
4134N/A{
5734N/A BIGNUM *aux;
5734N/A BN_CTX *ctx;
5734N/A
6143N/A if ((aux = BN_new()) == NULL)
5734N/A fatal("rsa_generate_additional_parameters: BN_new failed");
5734N/A if ((ctx = BN_CTX_new()) == NULL)
5734N/A fatal("rsa_generate_additional_parameters: BN_CTX_new failed");
5734N/A
5735N/A BN_sub(aux, rsa->q, BN_value_one());
5734N/A BN_mod(rsa->dmq1, rsa->d, aux, ctx);
5734N/A
5734N/A BN_sub(aux, rsa->p, BN_value_one());
5734N/A BN_mod(rsa->dmp1, rsa->d, aux, ctx);
5734N/A
5734N/A BN_clear_free(aux);
5734N/A BN_CTX_free(ctx);
5734N/A}
5734N/A
5734N/A