]> andersk Git - openssh.git/blame - rsa.c
- (dtucker) [sshd.c] Use privsep_pw if we have it, but only require it
[openssh.git] / rsa.c
CommitLineData
31652869 1/* $OpenBSD: rsa.c,v 1.28 2006/08/03 03:34:42 deraadt Exp $ */
8efc0c15 2/*
5260325f 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5260325f 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
6ae2364d 6 *
bcbf86ec 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".
2b87da3b 12 *
bcbf86ec 13 *
14 * Copyright (c) 1999 Niels Provos. All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
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.
6ae2364d 24 *
bcbf86ec 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.
35 *
36 *
37 * Description of the RSA algorithm can be found e.g. from the following
38 * sources:
6ae2364d 39 *
5260325f 40 * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1994.
6ae2364d 41 *
5260325f 42 * Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to
43 * Computer Security. Prentice-Hall, 1989.
6ae2364d 44 *
5260325f 45 * Man Young Rhee: Cryptography and Secure Data Communications. McGraw-Hill,
46 * 1994.
6ae2364d 47 *
5260325f 48 * R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications
49 * System and Method. US Patent 4,405,829, 1983.
6ae2364d 50 *
5260325f 51 * Hans Riesel: Prime Numbers and Computer Methods for Factorization.
52 * Birkhauser, 1994.
6ae2364d 53 *
bcbf86ec 54 * The RSA Frequently Asked Questions document by RSA Data Security,
55 * Inc., 1995.
6ae2364d 56 *
bcbf86ec 57 * RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as
58 * included below:
6ae2364d 59 *
5260325f 60 * [gone - had to be deleted - what a pity]
bcbf86ec 61 */
8efc0c15 62
63#include "includes.h"
8efc0c15 64
31652869 65#include <sys/types.h>
66
24436b92 67#include <stdarg.h>
00146caa 68#include <string.h>
69
31652869 70#include "xmalloc.h"
8efc0c15 71#include "rsa.h"
42f11eb2 72#include "log.h"
8efc0c15 73
8efc0c15 74void
5260325f 75rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
8efc0c15 76{
1e3b8b07 77 u_char *inbuf, *outbuf;
5260325f 78 int len, ilen, olen;
8efc0c15 79
5260325f 80 if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
81 fatal("rsa_public_encrypt() exponent too small or not odd");
8efc0c15 82
5260325f 83 olen = BN_num_bytes(key->n);
84 outbuf = xmalloc(olen);
8efc0c15 85
5260325f 86 ilen = BN_num_bytes(in);
87 inbuf = xmalloc(ilen);
88 BN_bn2bin(in, inbuf);
8efc0c15 89
5260325f 90 if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
c8d54615 91 RSA_PKCS1_PADDING)) <= 0)
5260325f 92 fatal("rsa_public_encrypt() failed");
8efc0c15 93
5260325f 94 BN_bin2bn(outbuf, len, out);
8efc0c15 95
5260325f 96 memset(outbuf, 0, olen);
97 memset(inbuf, 0, ilen);
98 xfree(outbuf);
99 xfree(inbuf);
8efc0c15 100}
101
46aa2d1f 102int
8efc0c15 103rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
104{
1e3b8b07 105 u_char *inbuf, *outbuf;
5260325f 106 int len, ilen, olen;
8efc0c15 107
5260325f 108 olen = BN_num_bytes(key->n);
109 outbuf = xmalloc(olen);
8efc0c15 110
5260325f 111 ilen = BN_num_bytes(in);
112 inbuf = xmalloc(ilen);
113 BN_bn2bin(in, inbuf);
8efc0c15 114
5260325f 115 if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
46aa2d1f 116 RSA_PKCS1_PADDING)) <= 0) {
117 error("rsa_private_decrypt() failed");
118 } else {
119 BN_bin2bn(outbuf, len, out);
120 }
5260325f 121 memset(outbuf, 0, olen);
122 memset(inbuf, 0, ilen);
123 xfree(outbuf);
124 xfree(inbuf);
46aa2d1f 125 return len;
8efc0c15 126}
457fc0c6 127
b775c6f2 128/* calculate p-1 and q-1 */
457fc0c6 129void
2b63e803 130rsa_generate_additional_parameters(RSA *rsa)
457fc0c6 131{
132 BIGNUM *aux;
133 BN_CTX *ctx;
b775c6f2 134
135 if ((aux = BN_new()) == NULL)
136 fatal("rsa_generate_additional_parameters: BN_new failed");
137 if ((ctx = BN_CTX_new()) == NULL)
138 fatal("rsa_generate_additional_parameters: BN_CTX_new failed");
457fc0c6 139
140 BN_sub(aux, rsa->q, BN_value_one());
141 BN_mod(rsa->dmq1, rsa->d, aux, ctx);
142
143 BN_sub(aux, rsa->p, BN_value_one());
144 BN_mod(rsa->dmp1, rsa->d, aux, ctx);
145
146 BN_clear_free(aux);
147 BN_CTX_free(ctx);
148}
149
This page took 0.320555 seconds and 5 git commands to generate.