certsgen.c
8.88 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <openssl/rsa.h>
#include <openssl/bn.h>
#include <stdlib.h>
#include <integer_math.h>
#include <sha1.h>
#define MAX_KEY_SIZE 4096
BSL_error
bigint_print(bigint *a, int int_max){
short int i;
for(i =0; i <= int_max; i++){
printf("%d = %08x\n ", i, a->half_word[i]);
}
return BSL_OK;
}
saveRsaData(FILE *writeptr, RSA *prsa){
unsigned char e_string[MAX_KEY_SIZE];
int e_size, i;
int n_size;
unsigned char n_string[MAX_KEY_SIZE];
unsigned char d_string[MAX_KEY_SIZE];
unsigned char p_string[MAX_KEY_SIZE];
unsigned char q_string[MAX_KEY_SIZE];
unsigned char dmp1_string[MAX_KEY_SIZE];
unsigned char dmq1_string[MAX_KEY_SIZE];
unsigned char iqmp_string[MAX_KEY_SIZE];
int d_size, p_size, q_size;
int dmp1_size, dmq1_size, iqmp_size;
e_size = BN_bn2bin(prsa->e, e_string);
for( i =0 ; i< e_size; i++){
printf("in save e[%d] = %02x\n", i, e_string[i]);
}
fprintf(writeptr, "\n%d\n", e_size);
for( i =0 ; i< e_size; i++){
fprintf(writeptr, "%02x ", e_string[i]);
}
n_size = BN_bn2bin(prsa->n, n_string);
for( i =0 ; i< n_size; i++){
printf("in save n[%d] = %02x\n", i, n_string[i]);
}
fprintf(writeptr, "\n%d\n", n_size);
for( i =0 ; i< n_size; i++){
fprintf(writeptr, "%02x ", n_string[i]);
}
d_size = BN_bn2bin(prsa->d, d_string);
fprintf(writeptr, "\n%d\n", d_size);
for( i =0 ; i< d_size; i++){
fprintf(writeptr, "%02x ", d_string[i]);
}
p_size = BN_bn2bin(prsa->p, p_string);
fprintf(writeptr, "\n%d\n", p_size);
for( i =0 ; i< p_size; i++){
fprintf(writeptr, "%02x ", p_string[i]);
}
q_size = BN_bn2bin(prsa->q, q_string);
fprintf(writeptr, "\n%d\n", q_size);
for( i =0 ; i< q_size; i++){
fprintf(writeptr, "%02x ", q_string[i]);
}
dmp1_size = BN_bn2bin(prsa->dmp1, dmp1_string);
fprintf(writeptr, "\n%d\n", dmp1_size);
for( i =0 ; i< dmp1_size; i++){
fprintf(writeptr, "%02x ", dmp1_string[i]);
}
dmq1_size = BN_bn2bin(prsa->dmq1, dmq1_string);
fprintf(writeptr, "\n%d\n", dmq1_size);
for( i =0 ; i< dmq1_size; i++){
fprintf(writeptr, "%02x ", dmq1_string[i]);
}
iqmp_size = BN_bn2bin(prsa->iqmp, iqmp_string);
fprintf(writeptr, "\n%d\n", iqmp_size);
for( i =0 ; i< iqmp_size; i++){
fprintf(writeptr, "%02x ", iqmp_string[i]);
}
}
readRsaData(FILE *readptr, RSA *prsa){
unsigned char e_string[MAX_KEY_SIZE], n_string[MAX_KEY_SIZE];
int e_size, n_size, i;
int d_size, p_size, q_size;
unsigned char d_string[MAX_KEY_SIZE], p_string[MAX_KEY_SIZE], q_string[MAX_KEY_SIZE];
unsigned char dmp1_string[MAX_KEY_SIZE];
unsigned char dmq1_string[MAX_KEY_SIZE];
unsigned char iqmp_string[MAX_KEY_SIZE];
int dmp1_size, dmq1_size, iqmp_size;
fscanf(readptr, "%d", &e_size);
for( i =0 ; i< e_size; i++){
fscanf(readptr, "%02x", &(e_string[i]));
}
prsa->e = BN_bin2bn(e_string, e_size, prsa->e);
fscanf(readptr, "%d", &n_size);
for( i =0 ; i< n_size; i++){
fscanf(readptr, "%02x", &(n_string[i]));
}
prsa->n = BN_bin2bn(n_string, n_size, prsa->n);
fscanf(readptr, "%d", &d_size);
for( i =0 ; i< d_size; i++){
fscanf(readptr, "%02x", &(d_string[i]));
}
prsa->d = BN_bin2bn(d_string, d_size, prsa->d);
fscanf(readptr, "%d", &p_size);
for( i =0 ; i< p_size; i++){
fscanf(readptr, "%02x", &(p_string[i]));
}
prsa->p = BN_bin2bn(p_string, p_size, prsa->p);
fscanf(readptr, "%d", &q_size);
for( i =0 ; i< q_size; i++){
fscanf(readptr, "%02x", &(q_string[i]));
}
prsa->q = BN_bin2bn(q_string, q_size, prsa->q);
fscanf(readptr, "%d", &dmp1_size);
for( i =0 ; i< dmp1_size; i++){
fscanf(readptr, "%02x", &(dmp1_string[i]));
}
prsa->dmp1 = BN_bin2bn(dmp1_string, dmp1_size, prsa->dmp1);
fscanf(readptr, "%d", &dmq1_size);
for( i =0 ; i< dmq1_size; i++){
fscanf(readptr, "%02x", &(dmq1_string[i]));
}
prsa->dmq1 = BN_bin2bn(dmq1_string, dmq1_size, prsa->dmq1);
fscanf(readptr, "%d", &iqmp_size);
for( i =0 ; i< iqmp_size; i++){
fscanf(readptr, "%02x", &(iqmp_string[i]));
}
prsa->iqmp = BN_bin2bn(iqmp_string, iqmp_size, prsa->iqmp);
}
int main(int argc, char **argv){
RSA *prsa;
int equal = 0;
bigint n, m, e, o;
bigint a;
int iter;
int size, newsize, i, e_size, n_size;
int num_bits, num_bytes, num_word, max_long, int_max, data_size;
unsigned char e_string[4096];
unsigned char n_string[4096];
unsigned char hash_data[20];
unsigned char *data;
unsigned char *padded_data;
unsigned char customResult[4096];
unsigned char *sign;
unsigned char *verify;
unsigned char input_string[2048];
int inputLen = 20;
SHA1Context sha;
/* cert types */
unsigned long certpublickey[128];
unsigned long certsign[128];
unsigned long certexponent;
FILE *rsawriteptr;
FILE *rsareadptr;
FILE *rsaptr;
int j;
prsa = RSA_new();
num_bits = 4096;
num_bytes = num_bits/8;
data_size = 512;
prsa = RSA_generate_key(num_bits, 3, NULL, NULL);
rsaptr = fopen("rsadata", "w");
rsawriteptr = fopen("rsabindata", "w");
RSA_print_fp(rsaptr, prsa, 0);
saveRsaData(rsawriteptr, prsa);
fclose(rsawriteptr);
free(prsa);
prsa = RSA_new();
num_bits = 4096;
rsareadptr = fopen("rsabindata", "r");
readRsaData(rsareadptr, prsa);
size = RSA_size(prsa);
sign = (unsigned char *) malloc(4096);
verify = (unsigned char *) malloc(4096);
data = (unsigned char *)malloc(4096);
padded_data = (unsigned char *) malloc(4096);
for(iter = 0; iter < 10; iter++){
for (i =0; i < data_size; i+=4){
*((unsigned long *) (data + i)) = (unsigned long) rand();
}
/* compute SHA-1 */
SHA1Reset(&sha);
SHA1Input(&sha, data, data_size);
SHA1Result(&sha, hash_data);
for( i = 0; i < 20; i++){
printf("hash = %02x\n", hash_data[i]);
}
RSA_padding_add_PKCS1_type_2(padded_data, data_size, hash_data, 20);
/*print padded data */
for(i = 0; i < data_size; i++){
printf("padded = %02x\n", padded_data[i]);
}
/* zero the sign */
for (i = 0; i < size; i++){
sign[i] = 0;
}
size = RSA_private_encrypt(data_size, padded_data, sign, prsa, RSA_NO_PADDING);
printf("size of encrypted = %d\n", size);
/* zero the result */
for(i = 0; i < size; i++){
verify[i] = 0;
}
newsize = RSA_public_decrypt(RSA_size(prsa), sign, verify, prsa, RSA_NO_PADDING);
printf("size of decrypted = %d\n", newsize);
for(i =0; i< data_size; i++){
printf("verify [%d] = %02x padded_data = %02x\n", i, verify[i], padded_data[i]);
}
/* call mod_exp from integer_math.h */
/* initialise bigint */
e_size = BN_bn2bin(prsa->e, e_string);
for( i =0 ; i< e_size; i++){
printf("e[%d] = %02x\n", i, e_string[i]);
}
n_size = BN_bn2bin(prsa->n, n_string);
for( i =0 ; i< n_size; i++){
printf("n[%d] = %02x\n", i, n_string[i]);
}
num_word = num_bits/WORD_SIZE;
max_long = num_word + 1;
int_max = (4*max_long) - 1;
charstring_to_bigint(&m, sign, data_size, int_max);
charstring_to_bigint(&n, n_string, n_size, int_max);
charstring_to_bigint(&e, e_string, e_size, int_max);
/*
printf("m\n");
bigint_print(&m, int_max);
printf("n\n");
bigint_print(&n, int_max);
printf("e\n");
bigint_print(&e, int_max);
*/
bigint_to_certint(certsign, num_word, &m, int_max);
bigint_to_certint(certpublickey, num_word, &n, int_max);
bigint_to_certint(&certexponent, 1, &e, int_max);
bsl_rsa_verify(customResult, certsign, certpublickey, &certexponent, num_bits);
/* print custom result of decryption */
equal = 0;
j= 0;
/* print output*/
for(i=data_size- 20; i < data_size; i++){
printf(" input = %02x custom = %02x\n", padded_data[i], customResult[i]);
if(hash_data[j] != customResult[i]){
equal = 1;
}
j++;
}
if(equal ==0){
printf("OK PASS test %d\n", iter);
}
else {
printf("NOT OK FAIL test %d\n", iter);
}
}
for(i =0 ; i< inputLen; i++){
input_string[i] = i;
}
charstring_to_bigint(&a, input_string, inputLen, int_max);
bigint_print(&a, int_max);
RSA_free(prsa);
free(sign);
free(verify);
free(data);
free(padded_data);
return 0;
}