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
|
/**
* @file gmp_helper.c
*
* @brief Class with helper functions for gmp operations
*
*/
/*
* Copyright (C) 1999, 2000, 2001 Henry Spencer.
* Copyright (C) 2005 Jan Hutter, Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include <stdio.h>
#include "gmp_helper.h"
#include "allocator.h"
#include "randomizer.h"
/**
* Number of times the probabilistic primality test is applied
*/
#define PRIMECHECK_ROUNDS 30
/**
* Private data of an gmp_helper_t object.
*
*/
typedef struct private_gmp_helper_s private_gmp_helper_t;
struct private_gmp_helper_s {
/**
* public gmp_helper_t interface
*/
gmp_helper_t public;
};
/**
* Implements private_gmp_helper_t's chunk_to_mpz function.
* See #private_gmp_helper_t.chunk_to_mpz for description.
*/
static void chunk_to_mpz(private_gmp_helper_t *this, mpz_t *mpz_value, chunk_t data)
{
size_t i;
mpz_init_set_ui(*(mpz_value), 0);
for (i = 0; i < data.len; i++)
{
mpz_mul_ui(*(mpz_value),*(mpz_value), 1 << 8);
mpz_add_ui(*(mpz_value),*(mpz_value), data.ptr[i]);
}
}
/**
* Implements private_gmp_helper_t's mpz_to_chunk function.
* See #private_gmp_helper_t.mpz_to_chunk for description.
*/
static status_t mpz_to_chunk (private_gmp_helper_t *this,mpz_t *mpz_value, chunk_t *data,size_t bytes)
{
mpz_t temp1, temp2;
status_t status = SUCCESS;
int i;
data->len = bytes;
data->ptr = allocator_alloc(data->len);
if (data->ptr == NULL)
{
return OUT_OF_RES;
}
/* free memory */
memset(data->ptr,0,data->len);
mpz_init(temp1);
mpz_init(temp2);
mpz_set(temp1, *mpz_value);
for (i = data->len-1; i >= 0; i--)
{
data->ptr[i] = mpz_mdivmod_ui(temp2, NULL, temp1, 1 << 8);
mpz_set(temp1, temp2);
}
if (mpz_sgn(temp1) != 0)
{
fprintf (stderr,"value %d\n",mpz_sgn(temp1));
status = FAILED;
}
mpz_clear(temp1);
mpz_clear(temp2);
return status;
}
/**
* Implements gmp_helper_t's init_prime function.
* See #gmp_helper_t.init_prime for description.
*/
static status_t init_prime (private_gmp_helper_t *this, mpz_t *prime, int bytes)
{
randomizer_t *randomizer;
chunk_t random_bytes;
status_t status;
randomizer = randomizer_create();
if (randomizer == NULL)
{
return OUT_OF_RES;
}
/* TODO change to true random device ? */
// status = randomizer->allocate_random_bytes(randomizer,bytes, &random_bytes);
status = randomizer->allocate_pseudo_random_bytes(randomizer,bytes, &random_bytes);
/* make sure most significant bit is set */
random_bytes.ptr[0] = random_bytes.ptr[0] | 0x80;
/* not needed anymore */
randomizer->destroy(randomizer);
if (status != SUCCESS)
{
return status;
}
/* convert chunk to mpz value */
this->public.chunk_to_mpz(&(this->public),prime, random_bytes);
/* chunk is not used anymore */
allocator_free(random_bytes.ptr);
random_bytes.ptr = NULL;
/* composites are possible but should never occur */
mpz_nextprime (*(prime),*(prime));
return SUCCESS;
}
static status_t init_prime_fast (private_gmp_helper_t *this, mpz_t *prime, int bytes){
randomizer_t *randomizer;
chunk_t random_bytes;
status_t status;
unsigned long tries;
size_t length;
randomizer = randomizer_create();
if (randomizer == NULL)
{
return OUT_OF_RES;
}
/* TODO change to true random device ? */
// status = randomizer->allocate_random_bytes(randomizer,bytes, &random_bytes);
status = randomizer->allocate_pseudo_random_bytes(randomizer,bytes, &random_bytes);
/* make sure most significant bit is set */
random_bytes.ptr[0] = random_bytes.ptr[0] | 0x80;
/* not needed anymore */
randomizer->destroy(randomizer);
if (status != SUCCESS)
{
return status;
}
/* convert chunk to mpz value */
this->public.chunk_to_mpz(&(this->public),prime, random_bytes);
/* chunk is not used anymore */
allocator_free(random_bytes.ptr);
random_bytes.ptr = NULL;
/* make value odd */
if (mpz_fdiv_ui(*prime, 2) != 1)
{
/* make value odd */
mpz_add_ui(*prime,*prime,1);
}
tries = 1;
/* starting find a prime */
while (!mpz_probab_prime_p(*prime, PRIMECHECK_ROUNDS))
{
/* not a prime, increase by 2 */
mpz_add_ui(*prime, *prime, 2);
tries++;
}
length = mpz_sizeinbase(*prime, 2);
/* check bit length of primee */
if ((length < (bytes * 8)) || (length > ((bytes * 8) + 1)))
{
return FAILED;
}
if (length == ((bytes * 8) + 1))
{
/* carry out occured! retry */
mpz_clear(*prime);
/* recursive call */
return this->public.init_prime_fast(&(this->public),prime, bytes);
}
return SUCCESS;
}
/**
* Implements gmp_helper_t's destroy function.
* See #gmp_helper_t.destroy for description.
*/
static status_t destroy(private_gmp_helper_t *this)
{
allocator_free(this);
return SUCCESS;
}
/*
* Described in header
*/
gmp_helper_t *gmp_helper_create()
{
private_gmp_helper_t *this = allocator_alloc_thing(private_gmp_helper_t);
if ((this == NULL))
{
return NULL;
}
/* public functions */
this->public.destroy = (status_t (*)(gmp_helper_t *)) destroy;
this->public.init_prime = (status_t (*) (gmp_helper_t *, mpz_t *, int)) init_prime;
this->public.init_prime_fast = (status_t (*) (gmp_helper_t *, mpz_t *, int)) init_prime_fast;
/* private functions */
this->public.chunk_to_mpz = (void (*) (gmp_helper_t *,mpz_t *, chunk_t )) chunk_to_mpz;
this->public.mpz_to_chunk = (status_t (*) (gmp_helper_t *,mpz_t *, chunk_t *,size_t )) mpz_to_chunk;
return &(this->public);
}
|