aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/utils
diff options
context:
space:
mode:
Diffstat (limited to 'Source/charon/utils')
-rw-r--r--Source/charon/utils/Makefile.utils4
-rw-r--r--Source/charon/utils/allocator.c51
-rw-r--r--Source/charon/utils/allocator.h19
-rw-r--r--Source/charon/utils/gmp_helper.c150
-rw-r--r--Source/charon/utils/gmp_helper.h92
5 files changed, 55 insertions, 261 deletions
diff --git a/Source/charon/utils/Makefile.utils b/Source/charon/utils/Makefile.utils
index b9c00675b..01e3bab32 100644
--- a/Source/charon/utils/Makefile.utils
+++ b/Source/charon/utils/Makefile.utils
@@ -19,10 +19,6 @@ OBJS+= $(BUILD_DIR)allocator.o
$(BUILD_DIR)allocator.o : $(UTILS_DIR)allocator.c $(UTILS_DIR)allocator.h
$(CC) $(CFLAGS) -c -o $@ $<
-OBJS+= $(BUILD_DIR)gmp_helper.o
-$(BUILD_DIR)gmp_helper.o : $(UTILS_DIR)gmp_helper.c $(UTILS_DIR)gmp_helper.h
- $(CC) $(CFLAGS) -c -o $@ $<
-
OBJS+= $(BUILD_DIR)linked_list.o
$(BUILD_DIR)linked_list.o : $(UTILS_DIR)linked_list.c $(UTILS_DIR)linked_list.h
$(CC) $(CFLAGS) -c -o $@ $<
diff --git a/Source/charon/utils/allocator.c b/Source/charon/utils/allocator.c
index b22dec039..0d936a425 100644
--- a/Source/charon/utils/allocator.c
+++ b/Source/charon/utils/allocator.c
@@ -25,6 +25,7 @@
#include <string.h>
#include <assert.h>
#include <stdio.h>
+#include <gmp.h>
#include "allocator.h"
@@ -213,26 +214,17 @@ static void * reallocate(allocator_t *allocator, void * old, size_t bytes, char
private_allocator_t *this = (private_allocator_t *) allocator;
memory_hdr_t *allocated_memory;
- if (old == NULL)
- {
- return NULL;
- }
pthread_mutex_lock( &(this->mutex));
allocated_memory = ((memory_hdr_t *)old) - 1;
void *new_space = this->allocate_special(this,bytes,file,line,FALSE);
- if (new_space == NULL)
- {
- pthread_mutex_unlock(&(this->mutex));
- this->public.free_pointer(&(this->public),old);
- return NULL;
+ if (old != NULL)
+ {
+ /* the smaller size is copied to avoid overflows */
+ memcpy(new_space,old,(allocated_memory->info.size_of_memory < bytes) ? allocated_memory->info.size_of_memory : bytes);
}
-
-
- /* the smaller size is copied to avoid overflows */
- memcpy(new_space,old,(allocated_memory->info.size_of_memory < bytes) ? allocated_memory->info.size_of_memory : bytes);
pthread_mutex_unlock(&(this->mutex));
this->public.free_pointer(&(this->public),old);
@@ -331,7 +323,40 @@ static private_allocator_t allocator = {
mutex: PTHREAD_MUTEX_INITIALIZER
};
+
allocator_t *global_allocator = &(allocator.public);
+
+/*
+ * alloc function for gmp
+ */
+void *gmp_alloc(size_t bytes)
+{
+ return allocator.allocate_special(&allocator, bytes, "[ gmp internal ]", 0 , TRUE);
+}
+
+/*
+ * realloc function for gmp
+ */
+void *gmp_realloc(void *old, size_t old_bytes, size_t new_bytes)
+{
+ return global_allocator->reallocate(global_allocator, old, new_bytes, "[ gmp internal ]", 0);
+}
+/*
+ * free function for gmp
+ */
+void gmp_free(void *ptr, size_t bytes)
+{
+ free_pointer(global_allocator, ptr);
+}
+
+/*
+ * Described in header
+ */
+void allocator_init()
+{
+ mp_set_memory_functions (gmp_alloc, gmp_realloc, gmp_free);
+}
+
#else /* !LEAK_DETECTION */
diff --git a/Source/charon/utils/allocator.h b/Source/charon/utils/allocator.h
index 7ea0fb60d..a2f3292b6 100644
--- a/Source/charon/utils/allocator.h
+++ b/Source/charon/utils/allocator.h
@@ -172,7 +172,15 @@
void (*report_memory_leaks) (allocator_t *this);
};
-
+
+ /**
+ * @brief Initialize the allocator.
+ *
+ * Setup the allocator (currently set
+ * allocation functions for libgmp)
+ */
+ void allocator_init();
+
/**
* @brief Global allocater_t object.
*
@@ -316,7 +324,14 @@
*
* @ingroup utils
*/
- #define report_memory_leaks(void) {}
+ #define report_memory_leaks() {}
+
+ /**
+ * Initialize the allocator.
+ *
+ * @ingroup utils
+ */
+ #define allocator_init() {}
#endif
#endif /*ALLOCATOR_H_*/
diff --git a/Source/charon/utils/gmp_helper.c b/Source/charon/utils/gmp_helper.c
deleted file mode 100644
index 33050129a..000000000
--- a/Source/charon/utils/gmp_helper.c
+++ /dev/null
@@ -1,150 +0,0 @@
-/**
- * @file gmp_helper.c
- *
- * @brief Implementation of gmp_helper_t.
- *
- */
-
-/*
- * 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 <utils/allocator.h>
-#include <utils/randomizer.h>
-
-/**
- * Number of times the probabilistic primality test is applied.
- */
-#define PRIMECHECK_ROUNDS 30
-
-
-typedef struct private_gmp_helper_t private_gmp_helper_t;
-
-/**
- * Private data of an gmp_helper_t object.
- */
-struct private_gmp_helper_t {
- /**
- * Public gmp_helper_t interface.
- */
- gmp_helper_t public;
-};
-
-
-/**
- * Implementation of gmp_helper_t.chunk_to_mpz.
- */
-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]);
- }
-}
-
-/**
- * Implementation of gmp_helper_t.mpz_to_chunk.
- */
-static void mpz_to_chunk (private_gmp_helper_t *this,mpz_t *mpz_value, chunk_t *data,size_t bytes)
-{
- 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);
- }
-
- mpz_clear(temp1);
- mpz_clear(temp2);
- *data = tmp_chunk;
-}
-
-/**
- * Implementation of gmp_helper_t.init_prime.
- */
-static void init_prime (private_gmp_helper_t *this, mpz_t *prime, int bytes)
-{
- randomizer_t *randomizer;
- chunk_t random_bytes;
- randomizer = randomizer_create();
-
- /* TODO change to true random device ? */
- //randomizer->allocate_random_bytes(randomizer,bytes, &random_bytes);
- 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));
-}
-
-/**
- * Implementation of gmp_helper_t.destroy.
- */
-static void destroy(private_gmp_helper_t *this)
-{
- allocator_free(this);
-}
-
-/*
- * Described in header
- */
-gmp_helper_t *gmp_helper_create()
-{
- private_gmp_helper_t *this = allocator_alloc_thing(private_gmp_helper_t);
-
- /* public functions */
- this->public.destroy = (void (*)(gmp_helper_t *)) destroy;
- this->public.init_prime = (void (*) (gmp_helper_t *, mpz_t *, int)) init_prime;
-
- /* private functions */
- this->public.chunk_to_mpz = (void (*) (gmp_helper_t *,mpz_t *, chunk_t )) chunk_to_mpz;
- this->public.mpz_to_chunk = (void (*) (gmp_helper_t *,mpz_t *, chunk_t *,size_t )) mpz_to_chunk;
-
- return &(this->public);
-}
diff --git a/Source/charon/utils/gmp_helper.h b/Source/charon/utils/gmp_helper.h
deleted file mode 100644
index 3b274cf05..000000000
--- a/Source/charon/utils/gmp_helper.h
+++ /dev/null
@@ -1,92 +0,0 @@
-/**
- * @file gmp_helper.h
- *
- * @brief Interface of gmp_helper_t.
- *
- */
-
-/*
- * Copyright (C) 1997 Angelos D. Keromytis.
- * 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.
- */
-
-
-
-#ifndef GMP_HELPER_H_
-#define GMP_HELPER_H_
-
-#include <gmp.h>
-
-#include <types.h>
-
-
-typedef struct gmp_helper_t gmp_helper_t;
-
-/**
- * @brief Class with helper functions to manipulate gmp values.
- *
- * @ingroup utils
- */
-struct gmp_helper_t {
-
- /**
- * Initialize an mpz_t to a random prime of specified size.
- *
- *
- * @param this calling object
- * @param[out] var pointer to mpz_t variable to initialize
- * @param[in] bytes length of given prime in bytes
- */
- void (*init_prime) (gmp_helper_t *this, mpz_t *var, int bytes);
-
- /**
- * Convert network form (binary bytes, big-endian) to mpz_t of gmp library.
- *
- * The given mpz_t gets initialized in this function.
- *
- * @param this calling private_gmp_helper_t object
- * @param mpz_value pointer to a mpz_t value
- * @param data chunk_t containing the network form of data
- */
- void (*chunk_to_mpz) (gmp_helper_t *this,mpz_t *mpz_value, chunk_t data);
-
- /**
- * Convert mpz_t to network form (binary bytes, big-endian).
- *
- * @param this calling private_gmp_helper_t object
- * @param mpz_value mpz_value to convert
- * @param data chunk_t where the data are written to
- * @param bytes number of bytes to copy
- */
- void (*mpz_to_chunk) (gmp_helper_t *this, mpz_t *mpz_value, chunk_t *data, size_t bytes);
-
- /**
- * @brief Destroys an gmp_helper_t object.
- *
- * @param this gmp_helper_t object to destroy
- * @return SUCCESS in any case
- */
- void (*destroy) (gmp_helper_t *this);
-};
-
-/**
- * Creates a new gmp_helper_t object
- *
- * @return gmp_helper_t object
- *
- * @ingroup utils
- */
-gmp_helper_t *gmp_helper_create();
-
-#endif /*GMP_HELPER_H_*/