-
Notifications
You must be signed in to change notification settings - Fork 7
/
rsa_utils.c
238 lines (177 loc) · 4.87 KB
/
rsa_utils.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
#include "hex.h"
#include "mt19937.h"
#include "mini-gmp/mpz_invmod.h"
#include "mini-gmp/mpz_nextprime.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char weak_e[] = "3";
const char NIST_e[] = "65537";
int gen_random_hex_str(char **hexstring, size_t *hexlen, unsigned int minbitlen, struct mt19937_t *ext_gen)
{
size_t i;
unsigned char *random_str;
struct mt19937_t r_gen, *rgen;
//
if (NULL == ext_gen)
{
mt19937_init(&r_gen, time(NULL));
rgen = &r_gen;
}
// We pad the minimum length to fit a multiple of our mt19937 gen output
size_t min_charlen = minbitlen/(8*sizeof(char));
if (min_charlen % sizeof(uint32_t))
min_charlen += min_charlen - (min_charlen % sizeof(uint32_t));
random_str = malloc(min_charlen);
if (NULL == random_str)
return 0x01;
*hexstring = malloc(1 + 2*min_charlen);
if (NULL == *hexstring)
{
free(random_str);
return 0x01;
}
for (i = 0; i < min_charlen/sizeof(uint32_t); i++)
{
((uint32_t*) random_str)[i] = mt19937_get_value(rgen);
}
// Force the first bit to one in order to have a random integer greater than 2**minbitlen
random_str[0] |= 0x80;
// Force the last bit to one in order to have an odd number
random_str[min_charlen - 1] |= 0x01;
hex_encode(*hexstring, (char*) random_str, 2*min_charlen);
(*hexstring)[2*min_charlen] = 0x00;
free(random_str);
return 0x00;
}
int rsa_gen_prime_num(mpz_t *p, mpz_t *q, unsigned int bitlen_min)
{
struct mt19937_t r_gen;
size_t random_hexlen;
char *random_hexstr;
mpz_t min_p, min_q;
mpz_init(*p);
mpz_init(*q);
mt19937_init(&r_gen, time(NULL));
// Generate random hex-strings of at least BITLEN_MIN bits long.
if (gen_random_hex_str(&random_hexstr, &random_hexlen, bitlen_min, &r_gen))
return 0x01;
mpz_init_set_str(min_p, random_hexstr, 16);
free(random_hexstr);
if (gen_random_hex_str(&random_hexstr, &random_hexlen, bitlen_min, &r_gen))
return 0x01;
mpz_init_set_str(min_q, random_hexstr, 16);
free(random_hexstr);
// Search for next prime using GMP's Miller-Rabin test
mpz_nextprime(p, (const mpz_t*) &min_p);
mpz_nextprime(q, (const mpz_t*) &min_q);
mpz_clear(min_p);
mpz_clear(min_q);
return 0x00;
}
int rsa_gen_key(mpz_t *n, mpz_t *e, mpz_t *d, const unsigned int bitlen_min)
{
mpz_t p, q, totient, one, p_m_1, q_m_1;
mpz_init(*n);
mpz_init(totient);
mpz_init_set_ui(one, 1);
mpz_init(p_m_1);
mpz_init(q_m_1);
mpz_init_set_str(*e, /*NIST_e*/ weak_e, 0 );
mpz_init_set_si(*d, -1);
while ( 0 == mpz_cmp_si(*d, -1))
{
// RSA Prime number generation
rsa_gen_prime_num( &p, &q, bitlen_min / 2);
/* RSA key generation */
// n = p*q
mpz_mul(*n, p, q);
// phi = (p-1)*(q-1)
mpz_sub(p_m_1, p, one);
mpz_sub(q_m_1, q, one);
mpz_mul(totient, p_m_1, q_m_1 );
// d = 1/e % phi
mpz_clear(*d);
mpz_invmod(d, *e, totient);
//printf(".");
mpz_clear(p);
mpz_clear(q);
}
mpz_clear(totient);
mpz_clear(one);
mpz_clear(p_m_1);
mpz_clear(q_m_1);
return 0x00;
}
int rsa_encrypt(mpz_t *c, const mpz_t m, const mpz_t n, const mpz_t e)
{
// Encryption
mpz_init(*c);
mpz_powm(*c, m, e, n);
return 0x00;
}
int rsa_encrypt_msg(mpz_t *c, const mpz_t n, const mpz_t e, const char *secret, const size_t secret_len)
{
char *hex_secret = NULL;
mpz_t m;
// Message creation
hex_secret = malloc(1 + 2*secret_len*sizeof(char));
if (NULL == hex_secret)
return 0x01;
hex_encode(hex_secret, secret, 2*secret_len);
hex_secret[2*secret_len] = 0x00;
mpz_init_set_str(m, hex_secret, 16);
// Message len verification
if (0 < mpz_cmp(m, n))
{
printf("The secret message is 'longer' than the current rsa modulus can accomodate.\n");
return 0x01;
}
rsa_encrypt(c, m, n , e);
// Memory release
if (NULL != hex_secret)
free(hex_secret);
mpz_clear(m);
return 0x00;
}
int rsa_decrypt(mpz_t *m, const mpz_t c, const mpz_t d, const mpz_t n)
{
mpz_init(*m);
mpz_powm(*m, c, d, n);
return 0x00;
}
int rsa_decrypt_msg(char **secret, size_t *secret_len, const mpz_t c, const mpz_t d, const mpz_t n)
{
mpz_t m;
char *hex_decrypted;
size_t i,hex_dec_len;
rsa_decrypt(&m, c, d, n);
hex_decrypted = mpz_get_str(NULL, 16, m);
hex_dec_len = strlen(hex_decrypted);
*secret_len = hex_dec_len/2;
if (hex_dec_len % 2)
{
/*
* In the case where the resulting hex string length is odd (GMP strip every 0-leading digits)
* we need to pad it in order to decode it correctly. Since mpz_get_str() return a
* NULL-terminated string, we use to terminator to pad our value to the right.
*/
memcpy(hex_decrypted+1, hex_decrypted, hex_dec_len);
hex_decrypted[0] = 0x00;
(*secret_len)++;
}
*secret = malloc(1 + (*secret_len)*sizeof(char));
if (NULL == *secret)
{
free(hex_decrypted);
mpz_clear(m);
return 0x01;
}
memset(*secret, 0, *secret_len);
hex_decode(*secret, hex_decrypted, 2*(*secret_len));
(*secret)[*secret_len] = 0;
free(hex_decrypted);
mpz_clear(m);
return 0x00;
}