]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
6ae2364d | 2 | * |
5260325f | 3 | * rsa.c |
6ae2364d | 4 | * |
5260325f | 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
6ae2364d | 6 | * |
5260325f | 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
8 | * All rights reserved | |
6ae2364d | 9 | * |
5260325f | 10 | * Created: Fri Mar 3 22:07:06 1995 ylo |
6ae2364d | 11 | * |
5260325f | 12 | * Description of the RSA algorithm can be found e.g. from the following sources: |
6ae2364d | 13 | * |
5260325f | 14 | * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1994. |
6ae2364d | 15 | * |
5260325f | 16 | * Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to |
17 | * Computer Security. Prentice-Hall, 1989. | |
6ae2364d | 18 | * |
5260325f | 19 | * Man Young Rhee: Cryptography and Secure Data Communications. McGraw-Hill, |
20 | * 1994. | |
6ae2364d | 21 | * |
5260325f | 22 | * R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications |
23 | * System and Method. US Patent 4,405,829, 1983. | |
6ae2364d | 24 | * |
5260325f | 25 | * Hans Riesel: Prime Numbers and Computer Methods for Factorization. |
26 | * Birkhauser, 1994. | |
6ae2364d | 27 | * |
5260325f | 28 | * The RSA Frequently Asked Questions document by RSA Data Security, Inc., 1995. |
6ae2364d | 29 | * |
5260325f | 30 | * RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as included |
31 | * below: | |
6ae2364d | 32 | * |
5260325f | 33 | * [gone - had to be deleted - what a pity] |
6ae2364d | 34 | * |
8efc0c15 | 35 | */ |
36 | ||
37 | #include "includes.h" | |
74fc9186 | 38 | RCSID("$OpenBSD: rsa.c,v 1.15 2000/06/20 01:39:44 markus Exp $"); |
8efc0c15 | 39 | |
40 | #include "rsa.h" | |
41 | #include "ssh.h" | |
42 | #include "xmalloc.h" | |
bfc9a610 | 43 | #include "entropy.h" |
8efc0c15 | 44 | |
45 | int rsa_verbose = 1; | |
46 | ||
47 | int | |
48 | rsa_alive() | |
49 | { | |
5260325f | 50 | RSA *key; |
8efc0c15 | 51 | |
54096dcc | 52 | seed_rng(); |
5260325f | 53 | key = RSA_generate_key(32, 3, NULL, NULL); |
54 | if (key == NULL) | |
55 | return (0); | |
56 | RSA_free(key); | |
57 | return (1); | |
8efc0c15 | 58 | } |
59 | ||
db513105 | 60 | /* |
61 | * Key generation progress meter callback | |
62 | */ | |
63 | void | |
64 | keygen_progress(int p, int n, void *arg) | |
65 | { | |
66 | const char progress_chars[] = ".o+O?"; | |
67 | ||
68 | if ((p < 0) || (p > (sizeof(progress_chars) - 2))) | |
69c76614 | 69 | p = sizeof(progress_chars) - 2; |
db513105 | 70 | |
69c76614 | 71 | putchar(progress_chars[p]); |
db513105 | 72 | fflush(stdout); |
73 | } | |
74 | ||
aa3378df | 75 | /* |
76 | * Generates RSA public and private keys. This initializes the data | |
77 | * structures; they should be freed with rsa_clear_private_key and | |
78 | * rsa_clear_public_key. | |
79 | */ | |
8efc0c15 | 80 | |
81 | void | |
82 | rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits) | |
83 | { | |
5260325f | 84 | RSA *key; |
85 | ||
69c76614 | 86 | seed_rng(); |
87 | ||
5260325f | 88 | if (rsa_verbose) { |
89 | printf("Generating RSA keys: "); | |
90 | fflush(stdout); | |
db513105 | 91 | key = RSA_generate_key(bits, 35, keygen_progress, NULL); |
92 | printf("\n"); | |
93 | } else { | |
94 | key = RSA_generate_key(bits, 35, NULL, NULL); | |
5260325f | 95 | } |
5260325f | 96 | if (key == NULL) |
97 | fatal("rsa_generate_key: key generation failed."); | |
98 | ||
99 | /* Copy public key parameters */ | |
100 | pub->n = BN_new(); | |
101 | BN_copy(pub->n, key->n); | |
102 | pub->e = BN_new(); | |
103 | BN_copy(pub->e, key->e); | |
104 | ||
105 | /* Copy private key parameters */ | |
106 | prv->n = BN_new(); | |
107 | BN_copy(prv->n, key->n); | |
108 | prv->e = BN_new(); | |
109 | BN_copy(prv->e, key->e); | |
110 | prv->d = BN_new(); | |
111 | BN_copy(prv->d, key->d); | |
112 | prv->p = BN_new(); | |
113 | BN_copy(prv->p, key->p); | |
114 | prv->q = BN_new(); | |
115 | BN_copy(prv->q, key->q); | |
116 | ||
117 | prv->dmp1 = BN_new(); | |
118 | BN_copy(prv->dmp1, key->dmp1); | |
119 | ||
120 | prv->dmq1 = BN_new(); | |
121 | BN_copy(prv->dmq1, key->dmq1); | |
122 | ||
123 | prv->iqmp = BN_new(); | |
124 | BN_copy(prv->iqmp, key->iqmp); | |
125 | ||
126 | RSA_free(key); | |
127 | ||
128 | if (rsa_verbose) | |
129 | printf("Key generation complete.\n"); | |
8efc0c15 | 130 | } |
131 | ||
132 | void | |
5260325f | 133 | rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key) |
8efc0c15 | 134 | { |
610cd5c6 | 135 | unsigned char *inbuf, *outbuf; |
5260325f | 136 | int len, ilen, olen; |
8efc0c15 | 137 | |
5260325f | 138 | if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e)) |
139 | fatal("rsa_public_encrypt() exponent too small or not odd"); | |
8efc0c15 | 140 | |
5260325f | 141 | olen = BN_num_bytes(key->n); |
142 | outbuf = xmalloc(olen); | |
8efc0c15 | 143 | |
5260325f | 144 | ilen = BN_num_bytes(in); |
145 | inbuf = xmalloc(ilen); | |
146 | BN_bn2bin(in, inbuf); | |
8efc0c15 | 147 | |
5260325f | 148 | if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key, |
c8d54615 | 149 | RSA_PKCS1_PADDING)) <= 0) |
5260325f | 150 | fatal("rsa_public_encrypt() failed"); |
8efc0c15 | 151 | |
5260325f | 152 | BN_bin2bn(outbuf, len, out); |
8efc0c15 | 153 | |
5260325f | 154 | memset(outbuf, 0, olen); |
155 | memset(inbuf, 0, ilen); | |
156 | xfree(outbuf); | |
157 | xfree(inbuf); | |
8efc0c15 | 158 | } |
159 | ||
160 | void | |
161 | rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key) | |
162 | { | |
610cd5c6 | 163 | unsigned char *inbuf, *outbuf; |
5260325f | 164 | int len, ilen, olen; |
8efc0c15 | 165 | |
5260325f | 166 | olen = BN_num_bytes(key->n); |
167 | outbuf = xmalloc(olen); | |
8efc0c15 | 168 | |
5260325f | 169 | ilen = BN_num_bytes(in); |
170 | inbuf = xmalloc(ilen); | |
171 | BN_bn2bin(in, inbuf); | |
8efc0c15 | 172 | |
5260325f | 173 | if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key, |
c8d54615 | 174 | RSA_PKCS1_PADDING)) <= 0) |
5260325f | 175 | fatal("rsa_private_decrypt() failed"); |
8efc0c15 | 176 | |
5260325f | 177 | BN_bin2bn(outbuf, len, out); |
8efc0c15 | 178 | |
5260325f | 179 | memset(outbuf, 0, olen); |
180 | memset(inbuf, 0, ilen); | |
181 | xfree(outbuf); | |
182 | xfree(inbuf); | |
8efc0c15 | 183 | } |
184 | ||
185 | /* Set whether to output verbose messages during key generation. */ | |
186 | ||
187 | void | |
188 | rsa_set_verbose(int verbose) | |
189 | { | |
5260325f | 190 | rsa_verbose = verbose; |
8efc0c15 | 191 | } |