libtomcrypt doesn't output a correct RSA private key
gregoiregentil opened this issue · comments
I have the following code:
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/stat.h>
#define LTM_DESC
#include "../libtomcrypt/src/headers/tomcrypt.h"
int prng_idx;
int main(int argc, char **argv) {
ltc_mp = ltm_desc;
prng_idx = register_prng(&sprng_desc);
rsa_key key;
int bitsize = 1024;
int err = rsa_make_key(NULL, prng_idx, bitsize / 8, 65537, &key);
unsigned char PRIV[1024];
unsigned long PRIVlen = 1024;
unsigned char pem[1024];
rsa_export(PRIV, &PRIVlen, PK_PRIVATE, &key);
fprintf(stderr, "private der length: %lu\n", PRIVlen);
FILE *pf = fopen("/tmp/private.der", "wb");
fwrite(PRIV, PRIVlen, 1, pf);
fclose(pf);
unsigned char PUB[1024];
unsigned long PUBlen = 1024;
rsa_export(PUB, &PUBlen, PK_PUBLIC, &key);
fprintf(stderr, "public der length: %lu (libtomcrypt)\n", PUBlen);
rsa_free(&key);
system("openssl rsa -inform der -in /tmp/private.der -outform der -pubout -out /tmp/public.der");
struct stat statTest;
stat("/tmp/public.der", &statTest);
fprintf(stderr, "private der length: %lu (openssl)\n", statTest.st_size);
}
I compile it with:
gcc -c test.c -o a.o && gcc a.o libtomcrypt.a libtommath.a && ./a.out
It outputs:
private der length: 608
public der length: 140 (libtomcrypt)
writing RSA key
private der length: 162 (openssl)
Is this a bug or am I misunderstanding the format of the public key?
Latest development commit: 8fd5dad
Sorry. Not a bug: openssl rsa -RSAPublicKey_out outputs 140 bytes.
Is there an option (or a suggested patch/modification) to have libtomcrypt outputs the equivalent of -pubout?
Have you tried rsa_export(PUB, &PUBlen, PK_PUBLIC|PK_STD, &key);
?
Awesome! Thank you.