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