-
Notifications
You must be signed in to change notification settings - Fork 0
/
keygen.c
88 lines (67 loc) · 2.66 KB
/
keygen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <gcrypt.h>
#define NEED_GCRYPT_VERSION "1.5.0"
int main(int argc, char **argv){
if (!gcry_check_version(NEED_GCRYPT_VERSION)){
fprintf (stderr, "libgcrypt is too old (need %s, have %s)\n",
NEED_GCRYPT_VERSION, gcry_check_version (NULL));
exit (2);
}
gcry_error_t err = 0;
/* We don't want to see any warnings, e.g. because we have not yet
parsed program options which might be used to suppress such
warnings. */
gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
/* Allocate a pool of 16k secure memory. */
gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
/* It is now okay to let Libgcrypt complain when there was/is
a problem with the secure memory. */
gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
/* Tell Libgcrypt that initialization has completed. */
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P))
{
fputs ("libgcrypt has not been initialized\n", stderr);
abort ();
}
gcry_sexp_t rsa_parms;
gcry_sexp_t rsa_keypair;
err = gcry_sexp_build(&rsa_parms, NULL, "(genkey (rsa (nbits 4:1024)))");
if (err) {
fprintf(stderr, "gcrypt: failed to create rsa params");
exit (2);
}
err = gcry_pk_genkey(&rsa_keypair, rsa_parms);
if (err) {
fprintf(stderr, "gcrypt: failed to create rsa key pair");
exit (2);
}
char buf[2048];
//gcry_sexp_t pubkey = gcry_sexp_nth(gcry_sexp_nth(rsa_keypair,1),1);
gcry_sexp_t pubkey = gcry_sexp_find_token(rsa_keypair, "public-key", 0);
//gcry_sexp_t pvtkey = gcry_sexp_nth(gcry_sexp_nth(rsa_keypair,2),1);
gcry_sexp_t pvtkey = gcry_sexp_find_token(rsa_keypair, "private-key", 0);
//printf("%d\n", gcry_sexp_length(pubkey));
gcry_sexp_sprint(pubkey, GCRYSEXP_FMT_ADVANCED, buf, 2047);
printf("%s\n",buf);
/*
gcry_sexp_t key2;
err = gcry_sexp_build(&key2, NULL, buf);
gcry_sexp_sprint(key2, GCRYSEXP_FMT_ADVANCED, buf, 2047);
printf("%s\n",buf);
*/
FILE *pubf = fopen("pubkey", "w");
fputs(buf, pubf);
fclose(pubf);
gcry_sexp_sprint(pvtkey, GCRYSEXP_FMT_ADVANCED, buf, 2047);
printf("%s\n",buf);
FILE *pvtf = fopen("pvtkey", "w");
fputs("(private-key\n(rsa\n", pvtf);
gcry_sexp_sprint(gcry_sexp_find_token(pvtkey, "n", 0), GCRYSEXP_FMT_ADVANCED, buf, 2047);
fputs(buf, pvtf);
gcry_sexp_sprint(gcry_sexp_find_token(pvtkey, "e", 0), GCRYSEXP_FMT_ADVANCED, buf, 2047);
fputs(buf, pvtf);
gcry_sexp_sprint(gcry_sexp_find_token(pvtkey, "d", 0), GCRYSEXP_FMT_ADVANCED, buf, 2047);
fputs(buf, pvtf);
fputs(")\n)", pvtf);
fclose(pvtf);
}