5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 * Created: Fri Mar 3 22:07:06 1995 ylo
12 * Description of the RSA algorithm can be found e.g. from the following sources:
14 * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1994.
16 * Jennifer Seberry and Josed Pieprzyk: Cryptography: An Introduction to
17 * Computer Security. Prentice-Hall, 1989.
19 * Man Young Rhee: Cryptography and Secure Data Communications. McGraw-Hill,
22 * R. Rivest, A. Shamir, and L. M. Adleman: Cryptographic Communications
23 * System and Method. US Patent 4,405,829, 1983.
25 * Hans Riesel: Prime Numbers and Computer Methods for Factorization.
28 * The RSA Frequently Asked Questions document by RSA Data Security, Inc., 1995.
30 * RSA in 3 lines of perl by Adam Back <aba@atlax.ex.ac.uk>, 1995, as included
33 * [gone - had to be deleted - what a pity]
52 key = RSA_generate_key(32, 3, NULL, NULL);
60 * Key generation progress meter callback
63 keygen_progress(int p, int n, void *arg)
65 const char progress_chars[] = ".o+O?";
67 if ((p < 0) || (p > (sizeof(progress_chars) - 2)))
68 p = sizeof(progress_chars) - 2;
70 putchar(progress_chars[p]);
75 * Seed OpenSSL's random number generator
82 get_random_bytes(buf, sizeof(buf));
83 RAND_seed(buf, sizeof(buf));
84 memset(buf, 0, sizeof(buf));
88 * Generates RSA public and private keys. This initializes the data
89 * structures; they should be freed with rsa_clear_private_key and
90 * rsa_clear_public_key.
94 rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits)
101 printf("Generating RSA keys: ");
103 key = RSA_generate_key(bits, 35, keygen_progress, NULL);
106 key = RSA_generate_key(bits, 35, NULL, NULL);
109 fatal("rsa_generate_key: key generation failed.");
111 /* Copy public key parameters */
113 BN_copy(pub->n, key->n);
115 BN_copy(pub->e, key->e);
117 /* Copy private key parameters */
119 BN_copy(prv->n, key->n);
121 BN_copy(prv->e, key->e);
123 BN_copy(prv->d, key->d);
125 BN_copy(prv->p, key->p);
127 BN_copy(prv->q, key->q);
129 prv->dmp1 = BN_new();
130 BN_copy(prv->dmp1, key->dmp1);
132 prv->dmq1 = BN_new();
133 BN_copy(prv->dmq1, key->dmq1);
135 prv->iqmp = BN_new();
136 BN_copy(prv->iqmp, key->iqmp);
141 printf("Key generation complete.\n");
145 rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
147 char *inbuf, *outbuf;
150 if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
151 fatal("rsa_public_encrypt() exponent too small or not odd");
153 olen = BN_num_bytes(key->n);
154 outbuf = xmalloc(olen);
156 ilen = BN_num_bytes(in);
157 inbuf = xmalloc(ilen);
158 BN_bn2bin(in, inbuf);
160 if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
161 RSA_PKCS1_PADDING)) <= 0)
162 fatal("rsa_public_encrypt() failed");
164 BN_bin2bn(outbuf, len, out);
166 memset(outbuf, 0, olen);
167 memset(inbuf, 0, ilen);
173 rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
175 char *inbuf, *outbuf;
178 olen = BN_num_bytes(key->n);
179 outbuf = xmalloc(olen);
181 ilen = BN_num_bytes(in);
182 inbuf = xmalloc(ilen);
183 BN_bn2bin(in, inbuf);
185 if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
186 RSA_SSLV23_PADDING)) <= 0)
187 fatal("rsa_private_decrypt() failed");
189 BN_bin2bn(outbuf, len, out);
191 memset(outbuf, 0, olen);
192 memset(inbuf, 0, ilen);
197 /* Set whether to output verbose messages during key generation. */
200 rsa_set_verbose(int verbose)
202 rsa_verbose = verbose;