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]
48 * Seed OpenSSL's random number generator
55 get_random_bytes(buf, sizeof(buf));
56 RAND_seed(buf, sizeof(buf));
57 memset(buf, 0, sizeof(buf));
66 key = RSA_generate_key(32, 3, NULL, NULL);
74 * Key generation progress meter callback
77 keygen_progress(int p, int n, void *arg)
79 const char progress_chars[] = ".o+O?";
81 if ((p < 0) || (p > (sizeof(progress_chars) - 2)))
82 p = sizeof(progress_chars) - 2;
84 putchar(progress_chars[p]);
89 * Generates RSA public and private keys. This initializes the data
90 * structures; they should be freed with rsa_clear_private_key and
91 * rsa_clear_public_key.
95 rsa_generate_key(RSA *prv, RSA *pub, unsigned int bits)
102 printf("Generating RSA keys: ");
104 key = RSA_generate_key(bits, 35, keygen_progress, NULL);
107 key = RSA_generate_key(bits, 35, NULL, NULL);
110 fatal("rsa_generate_key: key generation failed.");
112 /* Copy public key parameters */
114 BN_copy(pub->n, key->n);
116 BN_copy(pub->e, key->e);
118 /* Copy private key parameters */
120 BN_copy(prv->n, key->n);
122 BN_copy(prv->e, key->e);
124 BN_copy(prv->d, key->d);
126 BN_copy(prv->p, key->p);
128 BN_copy(prv->q, key->q);
130 prv->dmp1 = BN_new();
131 BN_copy(prv->dmp1, key->dmp1);
133 prv->dmq1 = BN_new();
134 BN_copy(prv->dmq1, key->dmq1);
136 prv->iqmp = BN_new();
137 BN_copy(prv->iqmp, key->iqmp);
142 printf("Key generation complete.\n");
146 rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
148 char *inbuf, *outbuf;
151 if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
152 fatal("rsa_public_encrypt() exponent too small or not odd");
154 olen = BN_num_bytes(key->n);
155 outbuf = xmalloc(olen);
157 ilen = BN_num_bytes(in);
158 inbuf = xmalloc(ilen);
159 BN_bn2bin(in, inbuf);
161 if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
162 RSA_PKCS1_PADDING)) <= 0)
163 fatal("rsa_public_encrypt() failed");
165 BN_bin2bn(outbuf, len, out);
167 memset(outbuf, 0, olen);
168 memset(inbuf, 0, ilen);
174 rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
176 char *inbuf, *outbuf;
179 olen = BN_num_bytes(key->n);
180 outbuf = xmalloc(olen);
182 ilen = BN_num_bytes(in);
183 inbuf = xmalloc(ilen);
184 BN_bn2bin(in, inbuf);
186 if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
187 RSA_SSLV23_PADDING)) <= 0)
188 fatal("rsa_private_decrypt() failed");
190 BN_bin2bn(outbuf, len, out);
192 memset(outbuf, 0, olen);
193 memset(inbuf, 0, ilen);
198 /* Set whether to output verbose messages during key generation. */
201 rsa_set_verbose(int verbose)
203 rsa_verbose = verbose;