#include "rsa.h"
#include "ssh.h"
#include "xmalloc.h"
+#include "random.h"
int rsa_verbose = 1;
+/*
+ * Seed OpenSSL's random number generator
+ */
+void
+seed_rng()
+{
+ char buf[64];
+
+ get_random_bytes(buf, sizeof(buf));
+ RAND_seed(buf, sizeof(buf));
+ memset(buf, 0, sizeof(buf));
+}
+
int
rsa_alive()
{
RSA *key;
+ seed_rng();
key = RSA_generate_key(32, 3, NULL, NULL);
if (key == NULL)
return (0);
return (1);
}
+/*
+ * Key generation progress meter callback
+ */
+void
+keygen_progress(int p, int n, void *arg)
+{
+ const char progress_chars[] = ".o+O?";
+
+ if ((p < 0) || (p > (sizeof(progress_chars) - 2)))
+ p = sizeof(progress_chars) - 2;
+
+ putchar(progress_chars[p]);
+ fflush(stdout);
+}
+
/*
* Generates RSA public and private keys. This initializes the data
* structures; they should be freed with rsa_clear_private_key and
{
RSA *key;
+ seed_rng();
+
if (rsa_verbose) {
printf("Generating RSA keys: ");
fflush(stdout);
+ key = RSA_generate_key(bits, 35, keygen_progress, NULL);
+ printf("\n");
+ } else {
+ key = RSA_generate_key(bits, 35, NULL, NULL);
}
- key = RSA_generate_key(bits, 35, NULL, NULL);
if (key == NULL)
fatal("rsa_generate_key: key generation failed.");
void
rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
{
- char *inbuf, *outbuf;
+ unsigned char *inbuf, *outbuf;
int len, ilen, olen;
if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
BN_bn2bin(in, inbuf);
if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
- RSA_PKCS1_PADDING)) <= 0)
+ RSA_PKCS1_PADDING)) <= 0)
fatal("rsa_public_encrypt() failed");
BN_bin2bn(outbuf, len, out);
void
rsa_private_decrypt(BIGNUM *out, BIGNUM *in, RSA *key)
{
- char *inbuf, *outbuf;
+ unsigned char *inbuf, *outbuf;
int len, ilen, olen;
olen = BN_num_bytes(key->n);
BN_bn2bin(in, inbuf);
if ((len = RSA_private_decrypt(ilen, inbuf, outbuf, key,
- RSA_SSLV23_PADDING)) <= 0)
+ RSA_PKCS1_PADDING)) <= 0)
fatal("rsa_private_decrypt() failed");
BN_bin2bn(outbuf, len, out);