- started to implement diffie hellman class
authorJan Hutter <jhutter@hsr.ch>
Thu, 17 Nov 2005 17:22:08 +0000 (17:22 -0000)
committerJan Hutter <jhutter@hsr.ch>
Thu, 17 Nov 2005 17:22:08 +0000 (17:22 -0000)
Source/charon/daemon.c
Source/charon/definitions.c
Source/charon/definitions.h
Source/charon/testcases/diffie_hellman_test.c [new file with mode: 0644]
Source/charon/testcases/diffie_hellman_test.h [new file with mode: 0644]
Source/charon/testcases/testcases.c
Source/charon/types.h
Source/charon/utils/gmp_helper.c [new file with mode: 0644]
Source/charon/utils/gmp_helper.h [new file with mode: 0644]
Source/charon/utils/randomizer.h

index d06d0a1..c79696c 100644 (file)
@@ -140,15 +140,15 @@ int main()
                destroy_and_exit(-1);
        }
        
-//     int i;
-//     for(i = 0; i<10; i++)
-//     {
-//             initiate_ike_sa_job_t *initiate_job;
-//             
-//             initiate_job = initiate_ike_sa_job_create("pinflb31");
-//             global_event_queue->add_relative(global_event_queue, (job_t*)initiate_job, i * 1000);
-//             
-//     }
+       int i;
+       for(i = 0; i<10; i++)
+       {
+               initiate_ike_sa_job_t *initiate_job;
+               
+               initiate_job = initiate_ike_sa_job_create("pinflb31");
+               global_event_queue->add_relative(global_event_queue, (job_t*)initiate_job, i * 1000);
+               
+       }
        
        logger->log(logger,CONTROL_MORE,"going to wait for exit signal");
        /* go and handle signals*/
index 4df8622..702c4fc 100644 (file)
@@ -19,7 +19,7 @@
  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * for more details.
  */
+
 #include "definitions.h"
 
 /*
index 8fe3c1c..0392f22 100644 (file)
@@ -23,7 +23,6 @@
 #ifndef DEFINITIONS_H_
 #define DEFINITIONS_H_
 
-
 #define MAPPING_END (-1)
 
 /**
@@ -53,4 +52,7 @@ struct mapping_s
 char *mapping_find(mapping_t *mappings, int value);
 
 
+
+
+
 #endif /*DEFINITIONS_H_*/
diff --git a/Source/charon/testcases/diffie_hellman_test.c b/Source/charon/testcases/diffie_hellman_test.c
new file mode 100644 (file)
index 0000000..2e1fd75
--- /dev/null
@@ -0,0 +1,56 @@
+/**
+ * @file diffie_hellman_test.c
+ * 
+ * @brief Tests to test the Diffie Hellman object diffie_hellman_t
+ * 
+ */
+
+/*
+ * 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 "diffie_hellman_test.h"
+
+#include "../transforms/diffie_hellman.h"
+
+#include "../globals.h"
+#include "../utils/logger_manager.h"
+#include "../utils/allocator.h"
+
+/* 
+ * described in Header-File
+ */
+void test_diffie_hellman(tester_t *tester)
+{
+       diffie_hellman_t *diffie_hellman;
+       logger_t *logger;
+       chunk_t public_value;
+
+       logger = global_logger_manager->create_logger(global_logger_manager,TESTER,"Diffie Hellman");
+
+
+       diffie_hellman = diffie_hellman_create(5);
+       tester->assert_true(tester,(diffie_hellman != NULL), "create call check");      
+       
+
+       
+       tester->assert_true(tester,(    diffie_hellman->get_my_public_value(diffie_hellman,&public_value) == SUCCESS), "get_my_public_value call check");
+
+       logger->log_chunk(logger,RAW,"Public value",&public_value);
+
+       allocator_free(public_value.ptr);
+               
+       tester->assert_true(tester,(diffie_hellman->destroy(diffie_hellman) == SUCCESS), "destroy call check");
+       global_logger_manager->destroy_logger(global_logger_manager,logger);
+}
diff --git a/Source/charon/testcases/diffie_hellman_test.h b/Source/charon/testcases/diffie_hellman_test.h
new file mode 100644 (file)
index 0000000..f42de18
--- /dev/null
@@ -0,0 +1,37 @@
+/**
+ * @file diffie_hellman_test.h
+ * 
+ * @brief Tests to test the Diffie Hellman object diffie_hellman_t
+ * 
+ */
+
+/*
+ * 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 DIFFIE_HELLMAN_TEST_H_
+#define DIFFIE_HELLMAN_TEST_H_
+
+#include "../utils/tester.h"
+
+/**
+ * @brief Test function used to test the diffie_hellman_t functionality
+ * 
+ * Tests are performed using one thread
+ *
+ * @param tester associated tester object
+ */
+void test_diffie_hellman(tester_t *tester);
+
+#endif /*DIFFIE_HELLMAN_TEST_H_*/
index aed858d..f2b2bc0 100644 (file)
@@ -47,6 +47,7 @@
 #include "generator_test.h"
 #include "parser_test.h"
 #include "packet_test.h"
+#include "diffie_hellman_test.h"
 
 
 /* output for test messages */
@@ -167,6 +168,12 @@ test_t packet_test = {test_packet,"Packet"};
 
 
 /**
+ * Test for packet_t
+ */
+test_t diffie_hellman_test = {test_diffie_hellman,"Diffie Hellman"};
+
+
+/**
  * Global job-queue
  */
 job_queue_t *global_job_queue;
@@ -219,7 +226,7 @@ logger_manager_t *global_logger_manager;
        &sender_test,
        &receiver_test,
        &ike_sa_id_test,
-       //&ike_sa_test,
+       &ike_sa_test,
        &generator_test1,
        &generator_test2,
        &parser_test1,
@@ -235,6 +242,7 @@ logger_manager_t *global_logger_manager;
        &generator_test8,
        &ike_sa_manager_test,
        &packet_test,
+       &diffie_hellman_test,
        NULL
        };
        global_logger_manager = logger_manager_create(ALL);
@@ -247,13 +255,13 @@ logger_manager_t *global_logger_manager;
        global_configuration_manager = configuration_manager_create();
        global_ike_sa_manager = ike_sa_manager_create();
        
-       global_logger_manager->disable_logger_level(global_logger_manager,TESTER,ALL);
+       //global_logger_manager->disable_logger_level(global_logger_manager,TESTER,ALL);
                
        tester_t *tester = tester_create(test_output, FALSE);
 
 
-       tester->perform_tests(tester,all_tests);
-       //tester->perform_test(tester,&packet_test); 
+       //tester->perform_tests(tester,all_tests);
+       tester->perform_test(tester,&diffie_hellman_test); 
 
 
        
index a02c5df..d6d3c97 100644 (file)
@@ -24,6 +24,7 @@
 #ifndef TYPES_H_
 #define TYPES_H_
 
+#include <gmp.h> 
 #include <sys/types.h>
 #include <stdlib.h>
 
@@ -68,4 +69,5 @@ typedef int bool;
 #define TRUE           1
 
 
+
 #endif /*TYPES_H_*/
diff --git a/Source/charon/utils/gmp_helper.c b/Source/charon/utils/gmp_helper.c
new file mode 100644 (file)
index 0000000..6d6ddc5
--- /dev/null
@@ -0,0 +1,170 @@
+/**
+ * @file gmp_helper.c
+ * 
+ * @brief Class with helper functions for gmp operations
+ * 
+ */
+
+/*
+ * 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 "gmp_helper.h"
+
+#include "allocator.h"
+#include "randomizer.h"
+
+/**
+ * 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)
+    {
+               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;
+    } 
+    
+       status = randomizer->allocate_random_bytes(randomizer,bytes, &random_bytes);
+       /* 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;   
+
+       mpz_nextprime (*(prime),*(prime));
+
+       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;
+       
+       /* 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);
+}
diff --git a/Source/charon/utils/gmp_helper.h b/Source/charon/utils/gmp_helper.h
new file mode 100644 (file)
index 0000000..478dd06
--- /dev/null
@@ -0,0 +1,97 @@
+/**
+ * @file gmp_helper.c
+ * 
+ * @brief Class with helper functions for gmp operations
+ * 
+ */
+
+/*
+ * 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"
+
+/**
+ * Class with helper functions to manipulate gmp values 
+ * 
+ */
+typedef struct gmp_helper_s gmp_helper_t;
+
+struct gmp_helper_s {
+
+       /**
+        * @brief initialize an mpz_t to a random prime of specified size
+        *
+        *
+        * @param                       this calling object
+        * @param[out] var      mpz_t variable to initialize
+        * @param[in]           bytes length of given prime in bytes
+        * @return              
+        *                              - SUCCCESS
+        *                              - FAILED
+        *                              - OUT_OF_RES
+        */
+       status_t (*init_prime) (gmp_helper_t *this, mpz_t *var, int bytes);
+       
+       /* Convert network form (binary bytes, big-endian) to mpz_t of gmp library.
+        * 
+        * @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 
+        * 
+        * @return                                      
+        *                                                      - SUCCESS
+        *                                                      - OUT_OF_RES
+        *                                                      - FAILED if mpz_t value was longer then given bytes count
+        */
+       status_t (*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
+        */
+       status_t (*destroy) (gmp_helper_t *this);
+};
+
+/**
+ * Creates a new gmp_helper_t object
+ * 
+ * @return
+ *                                                     - gmp_helper_t if successfully
+ *                                                     - NULL if out of ressources
+ */
+gmp_helper_t *gmp_helper_create();
+
+
+#endif /*GMP_HELPER_H_*/
index cf6870a..0d37572 100644 (file)
@@ -78,7 +78,7 @@ struct randomizer_s {
         * 
         * @param this                                  calling randomizer_t object
         * @param bytes                                 Number of bytes to allocate
-        * @param[out] chunk                    chunk which will hold the allocated random bytes
+        * @param[out] chunk                            chunk which will hold the allocated random bytes
         * @return
         *                                                              - SUCCESS
         *                                                              - OUT_OF_RES