-
Notifications
You must be signed in to change notification settings - Fork 2
/
gcrypt_keygen.c
133 lines (111 loc) · 3.5 KB
/
gcrypt_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <gcrypt.h>
/*
* code from https://github.com/vedantk/gcrypt-example
*/
void initialize(){
gcry_error_t err = 0;
if (!gcry_check_version(GCRYPT_VERSION)){
printf("gcrypt version is mismatched.\n");
exit(1);
}
err |= gcry_control(GCRYCTL_DISABLE_SECMEM, 0);
err |= gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
if (err){
printf("there is an error in gcrypt initialization.\n");
exit(1);
}
}
void keygen(gcry_sexp_t *pp, gcry_sexp_t *kp){
gcry_error_t err = 0;
err = gcry_sexp_build(pp, NULL, "(genkey (elg (nbits 3:512)))");
if(err){
printf("cannot establish the ElGamal key generation.\n");
exit(1);
}
err = gcry_pk_genkey(kp, *pp);
if(err){
printf("cannot generate the ElGamal key pair.\n");
exit(1);
}
}
void savekey(gcry_sexp_t *kp){
FILE *fp = fopen("./key", "wb");
if(fp == NULL){
printf("cannot create the key file.\n");
exit(1);
}
void *elgamal_buf = malloc(512 / 8 * 1024);
if(elgamal_buf == NULL){
printf("failed to allocate the space to store the key.\n");
exit(1);
}
size_t elgamal_buf_len;
elgamal_buf_len = gcry_sexp_sprint(*kp, GCRYSEXP_FMT_DEFAULT, elgamal_buf, 512 / 8 * 1024);
fwrite(elgamal_buf, 1, elgamal_buf_len, fp);
fclose(fp);
free(elgamal_buf);
}
void savepp(gcry_sexp_t *kp){
gcry_error_t err = 0;
gcry_sexp_t elgamal_p_exp = gcry_sexp_find_token(*kp, "p", 1);
gcry_sexp_t elgamal_y_exp = gcry_sexp_find_token(*kp, "y", 1);
gcry_mpi_t elgamal_p = gcry_mpi_new(1024);
gcry_sexp_extract_param(elgamal_p_exp, NULL, "p", &elgamal_p, NULL);
gcry_mpi_t elgamal_y = gcry_mpi_new(1024);
gcry_sexp_extract_param(elgamal_y_exp, NULL, "y", &elgamal_y, NULL);
void *elgamal_p_str = malloc(512 / 8 * 1024);
if(elgamal_p_str == NULL){
printf("failed to allocate the space to store the prime p.\n");
exit(1);
}
err = gcry_mpi_print(GCRYMPI_FMT_HEX, elgamal_p_str, 512 / 8 * 1024, NULL, elgamal_p);
if(err){
printf("failed to convert the prime p.\n");
exit(1);
}
FILE *fp_p = fopen("./p", "wb");
if(fp_p == NULL){
printf("failed to store the prime p.\n");
exit(1);
}
fprintf(fp_p, "%s\n", (char*) elgamal_p_str);
fclose(fp_p);
void *elgamal_y_str = malloc(512 / 8 * 1024);
if(elgamal_y_str == NULL){
printf("failed to allocate the space to store the public key y.\n");
exit(1);
}
err = gcry_mpi_print(GCRYMPI_FMT_HEX, elgamal_y_str, 512 / 8 * 1024, NULL, elgamal_y);
if(err){
printf("failed to convert the public key y.\n");
exit(1);
}
FILE *fp_y = fopen("./y", "wb");
if(fp_y == NULL){
printf("failed to store the public key y.\n");
exit(1);
}
fprintf(fp_y, "%s\n", (char*) elgamal_y_str);
fclose(fp_y);
gcry_sexp_release(elgamal_p_exp);
gcry_sexp_release(elgamal_y_exp);
gcry_mpi_release(elgamal_p);
gcry_mpi_release(elgamal_y);
free(elgamal_p_str);
free(elgamal_y_str);
}
int main(){
gcry_error_t err = 0;
initialize();
gcry_sexp_t elgamal_parms;
gcry_sexp_t elgamal_keypair;
keygen(&elgamal_parms, &elgamal_keypair);
savekey(&elgamal_keypair);
savepp(&elgamal_keypair);
gcry_sexp_release(elgamal_parms);
gcry_sexp_release(elgamal_keypair);
return 0;
}