This repository has been archived by the owner on Oct 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa_keys.c
274 lines (222 loc) · 5.42 KB
/
rsa_keys.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* File: rsa_keys.c
* Created by Hamza ESSAYEGH (Querdos)
*/
#include <stdlib.h>
#include <stdio.h>
#include <gmp.h>
#include <string.h>
#define BASE_SAVE 61
#define MAX_CHARS_LINES 50
/**
* Write chars (i.e. n_str, d_str, e_str) to a file (fp)
*/
int write_chars(char *str, int count, FILE *fp) {
int i, count_bis;
count_bis = count;
for (i=0; i<strlen(str); i++) {
fputc(str[i], fp);
count_bis++;
if (count_bis == MAX_CHARS_LINES) {
count_bis = 0;
fputc('\n', fp);
}
}
return count_bis;
}
/**
* Save the private and public key pair to a new dir .rsa
*
* return -1 if an error occured
*/
int save_keypair(mpz_t n, mpz_t e, mpz_t d) {
// vars
FILE *fp_rsa;
char *n_str, *e_str, *d_str;
int i, count;
// saving public key
fp_rsa = fopen(".rsa/rsa.pub", "w");
if (NULL == fp_rsa) {
printf("Unable to open '.rsa/rsa.pub' for write operation. Aborting.\n");
return -1;
}
// allocating
e_str = malloc((mpz_sizeinbase(e, BASE_SAVE)+2) * sizeof(char));
memset(e_str, '\0', sizeof(e_str));
mpz_get_str(e_str, BASE_SAVE, e);
fputs("--- BEGIN PUBLIC KEY ---\n", fp_rsa);
fputs(e_str, fp_rsa);
fputc('/', fp_rsa);
count = strlen(e_str) + 1;
free(e_str);
// allocating
n_str = malloc((mpz_sizeinbase(n, BASE_SAVE)+2) * sizeof(char));
mpz_get_str(n_str, BASE_SAVE, n);
write_chars(n_str, count, fp_rsa);
fputs("\n--- END PUBLIC KEY ---\n", fp_rsa);
fclose(fp_rsa);
// saving private key
fp_rsa = fopen(".rsa/rsa.priv", "w");
if (NULL == fp_rsa) {
printf("Unable to open '.rsa/rsa.priv' for write operation. Aborting.\n");
return -1;
}
fputs("--- BEGIN PRIVATE KEY ---\n", fp_rsa);
// allocating
d_str = malloc((mpz_sizeinbase(d, BASE_SAVE)+2) * sizeof(char));
mpz_get_str(d_str, BASE_SAVE, d);
count = write_chars(d_str, 0, fp_rsa);
free(d_str);
fputc('/', fp_rsa);
write_chars(n_str, count+1, fp_rsa);
free(n_str);
fputs("\n--- END PRIVATE KEY ---\n", fp_rsa);
// closing file
fclose(fp_rsa);
return 0;
}
/**
* Load private key into n and d
*/
int load_priv(mpz_t n, mpz_t d) {
// vars
FILE *fp_rsa_pub, *lines_cmd;
char *d_str;
char *n_str, line[MAX_CHARS_LINES], char_pub;
int chars, i, step, count;
// public key
fp_rsa_pub = fopen(".rsa/rsa.priv", "r");
// existance
if (NULL == fp_rsa_pub) {
printf("File '.rsa/rsa.priv' doesn't exists. Aborting.\n");
return -1;
}
// counting lines
lines_cmd = popen("wc -m < .rsa/rsa.pub", "r");
fgets(line, 10, lines_cmd);
chars = atoi(line); // removing first and last line
pclose(lines_cmd);
// allocating e_str (will never change: HbN)
d_str = malloc(chars * sizeof(char *));
if (NULL == d_str) {
printf("Memory error.\n");
exit(1);
}
memset(d_str, '\0', chars);
// allocating n_str (some \n will remain...)
n_str = (char *) malloc(chars * sizeof(char *));
if (NULL == n_str) {
printf("Memory error.\n");
free(d_str);
exit(1);
}
memset(n_str, '\0', chars);
step = 1;
count = 0;
while (fgets(line, MAX_CHARS_LINES+2, fp_rsa_pub) != NULL) {
// skipping first line
if (strcmp(line, "--- BEGIN PRIVATE KEY ---\n") == 0) {
continue;
}
// skipping last line
if (strcmp(line, "--- END PRIVATE KEY ---\n") == 0) {
fclose(fp_rsa_pub);
break;
}
// checking the space char
for (i=0; i<MAX_CHARS_LINES; i++) {
// step 2
if (line[i] == '/') {
count = 0;
step = 2;
continue;
}
// step 1, filling e_str
if (step == 1 && line[i] != '\n') {
d_str[count] = line[i];
count++;
}
// step 2, filling n_str
if (step == 2 && line[i] != '\n') {
n_str[count] = line[i];
count++;
}
}
}
// inits
mpz_inits(n, d, NULL);
// converting
mpz_set_str(n, n_str, BASE_SAVE);
free(n_str);
mpz_set_str(d, d_str, BASE_SAVE);
free(d_str);
return 0;
}
/**
* Load public key into n and e
*/
int load_pub(mpz_t n, mpz_t e) {
// vars
FILE *fp_rsa_pub, *lines_cmd;
char *n_str, *e_str, line[MAX_CHARS_LINES], char_pub;
int chars, i, step, count;
// public key
fp_rsa_pub = fopen(".rsa/rsa.pub", "r");
// existance
if (NULL == fp_rsa_pub) {
printf("File '.rsa/rsa.pub' doesn't exists. Aborting.\n");
return -1;
}
// counting lines
lines_cmd = popen("wc -m < .rsa/rsa.pub", "r");
fgets(line, 10, lines_cmd);
chars = atoi(line); // removing first and last line
pclose(lines_cmd);
// allocating e_str (will never change: HbN)
e_str = malloc(4 * sizeof(char *));
memset(e_str, '\0', sizeof(e_str));
// allocating n_str (some \n will remain...)
// TODO: allocate the correct space
n_str = malloc(chars * sizeof(char *));
memset(n_str, '\0', sizeof(n_str));
step = 1;
count = 0;
while (fgets(line, MAX_CHARS_LINES+2, fp_rsa_pub) != NULL) {
// skipping first line
if (strcmp(line, "--- BEGIN PUBLIC KEY ---\n") == 0) {
continue;
}
// skipping last line
if (strcmp(line, "--- END PUBLIC KEY ---\n") == 0) {
fclose(fp_rsa_pub);
break;
}
// checking the space char
for (i=0; i<MAX_CHARS_LINES; i++) {
// step 2
if (line[i] == '/') {
count = 0;
step = 2;
continue;
}
// step 1, filling e_str
if (step == 1 && line[i] != '\n') {
e_str[count] = line[i];
count++;
}
// step 2, filling n_str
if (step == 2 && line[i] != '\n') {
n_str[count] = line[i];
count++;
}
}
}
// inits
mpz_inits(n, e, NULL);
// converting
mpz_set_str(n, n_str, BASE_SAVE);
free(n_str);
mpz_set_str(e, e_str, BASE_SAVE);
free(e_str);
return 0;
}