aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/utils/gmp_helper.c
diff options
context:
space:
mode:
authorMartin Willi <martin@strongswan.org>2005-11-28 20:29:47 +0000
committerMartin Willi <martin@strongswan.org>2005-11-28 20:29:47 +0000
commitd048df5cabd2d17713230f260bccebb205740498 (patch)
tree5c08427945e6a94421e84deada90aac2065fde18 /Source/charon/utils/gmp_helper.c
parent3fe058703ffe537dfdf68b9ad4d9143644230321 (diff)
downloadstrongswan-d048df5cabd2d17713230f260bccebb205740498.tar.bz2
strongswan-d048df5cabd2d17713230f260bccebb205740498.tar.xz
- return value cleanup
Diffstat (limited to 'Source/charon/utils/gmp_helper.c')
-rw-r--r--Source/charon/utils/gmp_helper.c213
1 files changed, 52 insertions, 161 deletions
diff --git a/Source/charon/utils/gmp_helper.c b/Source/charon/utils/gmp_helper.c
index dee334aa0..33050129a 100644
--- a/Source/charon/utils/gmp_helper.c
+++ b/Source/charon/utils/gmp_helper.c
@@ -44,7 +44,6 @@ struct private_gmp_helper_t {
* Public gmp_helper_t interface.
*/
gmp_helper_t public;
-
};
@@ -53,186 +52,83 @@ struct private_gmp_helper_t {
*/
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++)
- {
+ 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]);
- }
+ }
}
/**
* Implementation of gmp_helper_t.mpz_to_chunk.
*/
-static status_t mpz_to_chunk (private_gmp_helper_t *this,mpz_t *mpz_value, chunk_t *data,size_t bytes)
+static void 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;
- chunk_t tmp_chunk;
-
- tmp_chunk.len = bytes;
- tmp_chunk.ptr = allocator_alloc(tmp_chunk.len);
-
- if (tmp_chunk.ptr == NULL)
- {
- allocator_free_chunk(&tmp_chunk);
- return OUT_OF_RES;
- }
-
- /* free memory */
- memset(tmp_chunk.ptr,0,tmp_chunk.len);
-
- mpz_init(temp1);
- mpz_init(temp2);
-
- mpz_set(temp1, *mpz_value);
-
- for (i = tmp_chunk.len-1; i >= 0; i--)
- {
+ mpz_t temp1, temp2;
+ int i;
+ chunk_t tmp_chunk;
+
+ tmp_chunk.len = bytes;
+ tmp_chunk.ptr = allocator_alloc(tmp_chunk.len);
+
+ memset(tmp_chunk.ptr,0,tmp_chunk.len);
+
+ mpz_init(temp1);
+ mpz_init(temp2);
+
+ mpz_set(temp1, *mpz_value);
+
+ for (i = tmp_chunk.len-1; i >= 0; i--)
+ {
tmp_chunk.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);
- *data = tmp_chunk;
- if (status != SUCCESS)
- {
- allocator_free_chunk(&tmp_chunk);
}
- return status;
+
+ mpz_clear(temp1);
+ mpz_clear(temp2);
+ *data = tmp_chunk;
}
/**
* Implementation of gmp_helper_t.init_prime.
*/
-static status_t init_prime (private_gmp_helper_t *this, mpz_t *prime, int bytes)
+static void 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);
-
- /* 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;
-}
-
-/**
- * Implementation of gmp_helper_t.init_prime_fast.
- */
-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);
-
- /* 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;
+ chunk_t random_bytes;
+ randomizer = randomizer_create();
- /* make value odd */
- if (mpz_fdiv_ui(*prime, 2) != 1)
- {
- /* make value odd */
- mpz_add_ui(*prime,*prime,1);
- }
+ /* TODO change to true random device ? */
+ //randomizer->allocate_random_bytes(randomizer,bytes, &random_bytes);
+ randomizer->allocate_pseudo_random_bytes(randomizer,bytes, &random_bytes);
- tries = 1;
+ /* make sure most significant bit is set */
+ random_bytes.ptr[0] = random_bytes.ptr[0] | 0x80;
- /* 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 prime */
- if ((length < (bytes * 8)) || (length > ((bytes * 8) + 1)))
- {
- return FAILED;
- }
+ /* not needed anymore */
+ randomizer->destroy(randomizer);
-
- 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;
+ /* 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));
}
-
/**
* Implementation of gmp_helper_t.destroy.
*/
-static status_t destroy(private_gmp_helper_t *this)
+static void destroy(private_gmp_helper_t *this)
{
allocator_free(this);
- return SUCCESS;
}
/*
@@ -241,19 +137,14 @@ static status_t destroy(private_gmp_helper_t *this)
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;
+ this->public.destroy = (void (*)(gmp_helper_t *)) destroy;
+ this->public.init_prime = (void (*) (gmp_helper_t *, mpz_t *, int)) init_prime;
- /* private functions */
+ /* 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;
+ this->public.mpz_to_chunk = (void (*) (gmp_helper_t *,mpz_t *, chunk_t *,size_t )) mpz_to_chunk;
return &(this->public);
}