- implemented RSA, only signing and verifying esma_pkcs1 padded
authorMartin Willi <martin@strongswan.org>
Sun, 4 Dec 2005 01:30:35 +0000 (01:30 -0000)
committerMartin Willi <martin@strongswan.org>
Sun, 4 Dec 2005 01:30:35 +0000 (01:30 -0000)
- removed gmp-helper: chunk_to_mpz is now done with gmp functions, prime generation in prime-pool
- added prime-pool (needs priority fix)
- proof of concept RSA authentication
- mpz uses LEAK_DETECTIVE
- configuration-manager supports rsa keys

29 files changed:
Source/charon/config/configuration_manager.c
Source/charon/config/configuration_manager.h
Source/charon/daemon.c
Source/charon/daemon.h
Source/charon/sa/authenticator.c
Source/charon/testcases/Makefile.testcases
Source/charon/testcases/prime_pool_test.c [new file with mode: 0644]
Source/charon/testcases/prime_pool_test.h [new file with mode: 0644]
Source/charon/testcases/rsa_test.c [new file with mode: 0644]
Source/charon/testcases/rsa_test.h [new file with mode: 0644]
Source/charon/testcases/testcases.c
Source/charon/threads/Makefile.threads
Source/charon/threads/prime_pool.c [new file with mode: 0644]
Source/charon/threads/prime_pool.h [new file with mode: 0644]
Source/charon/transforms/Makefile.transforms
Source/charon/transforms/crypters/crypter.h
Source/charon/transforms/diffie_hellman.c
Source/charon/transforms/hashers/hasher.c
Source/charon/transforms/hashers/hasher.h
Source/charon/transforms/rsa/Makefile.rsa [new file with mode: 0644]
Source/charon/transforms/rsa/rsa_private_key.c [new file with mode: 0644]
Source/charon/transforms/rsa/rsa_private_key.h [new file with mode: 0644]
Source/charon/transforms/rsa/rsa_public_key.c [new file with mode: 0644]
Source/charon/transforms/rsa/rsa_public_key.h [new file with mode: 0644]
Source/charon/utils/Makefile.utils
Source/charon/utils/allocator.c
Source/charon/utils/allocator.h
Source/charon/utils/gmp_helper.c [deleted file]
Source/charon/utils/gmp_helper.h [deleted file]

index 725d534..674828e 100644 (file)
@@ -50,6 +50,43 @@ struct preshared_secret_entry_t {
        chunk_t preshared_secret;
 };
 
+
+typedef struct rsa_private_key_entry_t rsa_private_key_entry_t;
+
+/**
+ * Entry for a rsa private key.
+ */
+struct rsa_private_key_entry_t {
+
+       /**
+        * Identification.
+        */
+       identification_t *identification;
+       
+       /**
+        * private key
+        */     
+       rsa_private_key_t* private_key;
+};
+
+typedef struct rsa_public_key_entry_t rsa_public_key_entry_t;
+
+/**
+ * Entry for a rsa private key.
+ */
+struct rsa_public_key_entry_t {
+
+       /**
+        * Identification.
+        */
+       identification_t *identification;
+       
+       /**
+        * private key
+        */     
+       rsa_public_key_t* public_key;
+};
+
 typedef struct configuration_entry_t configuration_entry_t;
 
 /* A configuration entry combines a configuration name with a init and sa 
@@ -142,6 +179,16 @@ struct private_configuration_manager_t {
         * Holding all preshared secrets.
         */
        linked_list_t *preshared_secrets;
+       
+       /**
+        * Holding all private secrets.
+        */
+       linked_list_t *rsa_private_keys;
+       
+       /**
+        * Holding all public secrets.
+        */
+       linked_list_t *rsa_public_keys;
 
        /**
         * Assigned logger object.
@@ -182,6 +229,28 @@ struct private_configuration_manager_t {
        void (*add_new_preshared_secret) (private_configuration_manager_t *this,id_type_t type, char *id_string, char *preshared_secret);
        
        /**
+        * Adds a new IKE_SA configuration
+        * 
+        * 
+        * @param this                          calling object
+        * @param type                          type of identification
+        * @param id_string                     identification as string
+        * @param preshared_secret      preshared secret as string
+        */
+       void (*add_new_rsa_private_key) (private_configuration_manager_t *this,id_type_t type, char *id_string, u_int8_t *key_pos, size_t key_len);
+       
+       /**
+        * Adds a new IKE_SA configuration
+        * 
+        * 
+        * @param this                          calling object
+        * @param type                          type of identification
+        * @param id_string                     identification as string
+        * @param preshared_secret      preshared secret as string
+        */
+       void (*add_new_rsa_public_key) (private_configuration_manager_t *this,id_type_t type, char *id_string, u_int8_t *key_pos, size_t key_len);
+       
+       /**
         * Load default configuration
         * 
         * 
@@ -190,6 +259,11 @@ struct private_configuration_manager_t {
        void (*load_default_config) (private_configuration_manager_t *this);
 };
 
+u_int8_t public_key_1[];
+u_int8_t private_key_1[];
+u_int8_t public_key_2[];
+u_int8_t private_key_2[];
+
 /**
  * Implementation of private_configuration_manager_t.load_default_config.
  */
@@ -244,7 +318,7 @@ static void load_default_config (private_configuration_manager_t *this)
 
        sa_config3 = sa_config_create(ID_IPV4_ADDR, "127.0.0.1", 
                                                                  ID_IPV4_ADDR, "127.0.0.1",
-                                                                 SHARED_KEY_MESSAGE_INTEGRITY_CODE);
+                                                                 RSA_DIGITAL_SIGNATURE);
 
        sa_config3->add_traffic_selector_initiator(sa_config3,ts);
        sa_config3->add_traffic_selector_responder(sa_config3,ts);
@@ -280,6 +354,11 @@ static void load_default_config (private_configuration_manager_t *this)
        this->add_new_preshared_secret(this,ID_IPV4_ADDR, "152.96.193.130","das ist ein sicheres wort");
        this->add_new_preshared_secret(this,ID_IPV4_ADDR, "152.96.193.131","das ist ein sicheres wort");
        this->add_new_preshared_secret(this,ID_IPV4_ADDR, "127.0.0.1","das ist ein sicheres wort");
+       
+       this->add_new_rsa_public_key(this,ID_IPV4_ADDR, "127.0.0.1", public_key_1, 256);
+       this->add_new_rsa_public_key(this,ID_IPV4_ADDR, "152.96.193.131", public_key_2, 256);
+       this->add_new_rsa_private_key(this,ID_IPV4_ADDR, "127.0.0.1", private_key_1, 1024);
+       this->add_new_rsa_private_key(this,ID_IPV4_ADDR, "152.96.193.131", private_key_2, 1024);
 }
 
 /**
@@ -527,6 +606,41 @@ static void add_new_preshared_secret (private_configuration_manager_t *this,id_t
        this->preshared_secrets->insert_last(this->preshared_secrets,entry);
 }
 
+/**
+ * Implementation of private_configuration_manager_t.add_new_preshared_secret.
+ */
+static void add_new_rsa_public_key (private_configuration_manager_t *this, id_type_t type, char *id_string, u_int8_t* key_pos, size_t key_len)
+{
+       chunk_t key;
+       key.ptr = key_pos;
+       key.len = key_len;
+       
+       rsa_public_key_entry_t *entry = allocator_alloc_thing(rsa_public_key_entry_t);
+       
+       entry->identification = identification_create_from_string(type,id_string);
+       entry->public_key = rsa_public_key_create();
+       entry->public_key->set_key(entry->public_key, key);
+       
+       this->rsa_public_keys->insert_last(this->rsa_public_keys, entry);
+}
+
+/**
+ * Implementation of private_configuration_manager_t.add_new_preshared_secret.
+ */
+static void add_new_rsa_private_key (private_configuration_manager_t *this, id_type_t type, char *id_string, u_int8_t* key_pos, size_t key_len)
+{
+       chunk_t key;
+       key.ptr = key_pos;
+       key.len = key_len;
+       
+       rsa_private_key_entry_t *entry = allocator_alloc_thing(rsa_private_key_entry_t);
+       
+       entry->identification = identification_create_from_string(type,id_string);
+       entry->private_key = rsa_private_key_create();
+       entry->private_key->set_key(entry->private_key, key);
+       
+       this->rsa_private_keys->insert_last(this->rsa_private_keys, entry);
+}
 
 /**
  * Implementation of configuration_manager_t.get_shared_secret.
@@ -552,6 +666,52 @@ static status_t get_shared_secret(private_configuration_manager_t *this, identif
 }
 
 /**
+ * Implementation of configuration_manager_t.get_shared_secret.
+ */
+static status_t get_rsa_public_key(private_configuration_manager_t *this, identification_t *identification, rsa_public_key_t **public_key)
+{
+       iterator_t *iterator;
+       
+       iterator = this->rsa_public_keys->create_iterator(this->rsa_public_keys,TRUE);
+       while (iterator->has_next(iterator))
+       {
+               rsa_public_key_entry_t *entry;
+               iterator->current(iterator,(void **) &entry);
+               if (entry->identification->equals(entry->identification,identification))
+               {
+                       *public_key = entry->public_key;
+                       iterator->destroy(iterator);
+                       return SUCCESS;
+               }
+       }
+       iterator->destroy(iterator);
+       return NOT_FOUND;
+}
+
+/**
+ * Implementation of configuration_manager_t.get_shared_secret.
+ */
+static status_t get_rsa_private_key(private_configuration_manager_t *this, identification_t *identification, rsa_private_key_t **private_key)
+{
+       iterator_t *iterator;
+       
+       iterator = this->rsa_private_keys->create_iterator(this->rsa_private_keys,TRUE);
+       while (iterator->has_next(iterator))
+       {
+               rsa_private_key_entry_t *entry;
+               iterator->current(iterator,(void **) &entry);
+               if (entry->identification->equals(entry->identification,identification))
+               {
+                       *private_key = entry->private_key;
+                       iterator->destroy(iterator);
+                       return SUCCESS;
+               }
+       }
+       iterator->destroy(iterator);
+       return NOT_FOUND;
+}
+
+/**
  * Implementation of configuration_manager_t.destroy.
  */
 static status_t get_retransmit_timeout (private_configuration_manager_t *this, u_int32_t retransmit_count, u_int32_t *timeout)
@@ -612,6 +772,26 @@ static void destroy(private_configuration_manager_t *this)
                allocator_free(entry);
        }
        this->preshared_secrets->destroy(this->preshared_secrets);
+       
+       while (this->rsa_private_keys->get_count(this->rsa_private_keys) > 0)
+       {
+               rsa_private_key_entry_t *entry;
+               this->rsa_private_keys->remove_first(this->rsa_private_keys,(void **) &entry);
+               entry->identification->destroy(entry->identification);
+               entry->private_key->destroy(entry->private_key);
+               allocator_free(entry);
+       }
+       this->rsa_private_keys->destroy(this->rsa_private_keys);
+       
+       while (this->rsa_public_keys->get_count(this->rsa_public_keys) > 0)
+       {
+               rsa_public_key_entry_t *entry;
+               this->rsa_public_keys->remove_first(this->rsa_public_keys,(void **) &entry);
+               entry->identification->destroy(entry->identification);
+               entry->public_key->destroy(entry->public_key);
+               allocator_free(entry);
+       }
+       this->rsa_public_keys->destroy(this->rsa_public_keys);
                
        this->logger->log(this->logger,CONTROL | MOST, "Destroy assigned logger");
        charon->logger_manager->destroy_logger(charon->logger_manager,this->logger);
@@ -633,11 +813,15 @@ configuration_manager_t *configuration_manager_create(u_int32_t first_retransmit
        this->public.get_sa_config_for_init_config_and_id =(status_t (*) (configuration_manager_t *, init_config_t *, identification_t *, identification_t *,sa_config_t **)) get_sa_config_for_init_config_and_id;
        this->public.get_retransmit_timeout = (status_t (*) (configuration_manager_t *, u_int32_t retransmit_count, u_int32_t *timeout))get_retransmit_timeout;
        this->public.get_shared_secret = (status_t (*) (configuration_manager_t *, identification_t *, chunk_t *))get_shared_secret;
+       this->public.get_rsa_private_key = (status_t (*) (configuration_manager_t *, identification_t *, rsa_private_key_t**))get_rsa_private_key;
+       this->public.get_rsa_public_key = (status_t (*) (configuration_manager_t *, identification_t *, rsa_public_key_t**))get_rsa_public_key;
        
        /* private functions */
        this->load_default_config = load_default_config;
        this->add_new_configuration = add_new_configuration;
        this->add_new_preshared_secret = add_new_preshared_secret;
+       this->add_new_rsa_public_key = add_new_rsa_public_key;
+       this->add_new_rsa_private_key = add_new_rsa_private_key;
        
        /* private variables */
        this->logger = charon->logger_manager->create_logger(charon->logger_manager,CONFIGURATION_MANAGER,NULL);
@@ -645,6 +829,8 @@ configuration_manager_t *configuration_manager_create(u_int32_t first_retransmit
        this->sa_configs = linked_list_create();
        this->init_configs = linked_list_create();
        this->preshared_secrets = linked_list_create();
+       this->rsa_private_keys = linked_list_create();
+       this->rsa_public_keys = linked_list_create();
        this->max_retransmit_count = max_retransmit_count;
        this->first_retransmit_timeout = first_retransmit_timeout;
        
@@ -652,3 +838,187 @@ configuration_manager_t *configuration_manager_create(u_int32_t first_retransmit
 
        return (&this->public);
 }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+u_int8_t public_key_1[] = {
+       0xD4,0x8D,0x40,0x8E,0xBD,0xFC,0x6D,0xE9,0xDB,0x1C,0xD2,0x21,0x19,0x37,0x6B,0xE2,
+       0xDC,0xCE,0x74,0xA2,0x63,0xF6,0xD8,0x8D,0xAF,0x1C,0xC0,0xFF,0x07,0x3F,0xFB,0x52,
+       0x59,0x45,0x01,0x10,0x35,0xA9,0xB8,0x16,0x69,0x31,0x19,0x4F,0xDD,0x66,0xAD,0xAC,
+       0x80,0x11,0x33,0x38,0x5A,0x11,0xF9,0x33,0x3F,0xD2,0x41,0x4A,0x21,0x9B,0x54,0x44,
+       0x00,0xB6,0x07,0x33,0x4A,0x5B,0x4E,0x09,0x7C,0x9D,0xB8,0xDE,0x6B,0xA2,0xB2,0x78,
+       0x23,0x3D,0xF0,0xB7,0x37,0x2B,0x7A,0x71,0x50,0x6E,0xEA,0x93,0x3E,0xB5,0x2C,0xBD,
+       0xD6,0x08,0x43,0x12,0x0A,0xE8,0x8D,0xE6,0x6C,0x24,0xCC,0x3F,0xF7,0x18,0x7E,0x87,
+       0x59,0x0C,0xA9,0x5D,0x85,0xF8,0x6E,0x83,0xD8,0x18,0x77,0x07,0xB6,0x44,0x3C,0x8D,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01
+};
+
+u_int8_t private_key_1[] = {
+       0xD4,0x8D,0x40,0x8E,0xBD,0xFC,0x6D,0xE9,0xDB,0x1C,0xD2,0x21,0x19,0x37,0x6B,0xE2,
+       0xDC,0xCE,0x74,0xA2,0x63,0xF6,0xD8,0x8D,0xAF,0x1C,0xC0,0xFF,0x07,0x3F,0xFB,0x52,
+       0x59,0x45,0x01,0x10,0x35,0xA9,0xB8,0x16,0x69,0x31,0x19,0x4F,0xDD,0x66,0xAD,0xAC,
+       0x80,0x11,0x33,0x38,0x5A,0x11,0xF9,0x33,0x3F,0xD2,0x41,0x4A,0x21,0x9B,0x54,0x44,
+       0x00,0xB6,0x07,0x33,0x4A,0x5B,0x4E,0x09,0x7C,0x9D,0xB8,0xDE,0x6B,0xA2,0xB2,0x78,
+       0x23,0x3D,0xF0,0xB7,0x37,0x2B,0x7A,0x71,0x50,0x6E,0xEA,0x93,0x3E,0xB5,0x2C,0xBD,
+       0xD6,0x08,0x43,0x12,0x0A,0xE8,0x8D,0xE6,0x6C,0x24,0xCC,0x3F,0xF7,0x18,0x7E,0x87,
+       0x59,0x0C,0xA9,0x5D,0x85,0xF8,0x6E,0x83,0xD8,0x18,0x77,0x07,0xB6,0x44,0x3C,0x8D,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0xEE,0xF2,0x37,0xF2,0x98,0xEB,0x33,0xC6,0x84,0xE8,0xB9,0xD1,0x18,0xB5,0x29,0x00,
+       0xAC,0x6B,0x78,0xBC,0x9E,0xB6,0x01,0x21,0x29,0xEE,0x4A,0x99,0xFB,0x3D,0x07,0x23,
+       0x77,0x84,0x93,0x4B,0x53,0x49,0xB0,0xA4,0x6F,0xB0,0xF5,0x50,0xDB,0x35,0xDD,0xDF,
+       0x41,0x6F,0x7B,0xA9,0x88,0x3D,0x0B,0x1C,0x2E,0x2B,0x44,0x35,0x24,0x72,0x66,0xC1,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0xE3,0xB8,0xC8,0x30,0x67,0xD0,0x5D,0xF1,0x32,0x64,0xDC,0x4B,0xB3,0x7E,0xE3,0x1A,
+       0xC5,0xBC,0xAC,0xC9,0x95,0x5C,0x96,0x0D,0x5A,0x52,0x90,0xE0,0x08,0x3F,0xA6,0x71,
+       0xC7,0x18,0xC5,0x64,0xA2,0xE4,0xB8,0x43,0x5A,0x8A,0x7A,0x9B,0xDF,0xDA,0x81,0x85,
+       0x6C,0x0F,0xA4,0xC9,0xAC,0x25,0x19,0x54,0xFE,0x75,0xAA,0x1D,0x22,0xB8,0xF4,0xCD,
+       0x1A,0x91,0xC2,0xA3,0x65,0x3F,0xD7,0xFC,0x7E,0xE1,0x92,0x29,0xC5,0x85,0x6E,0x44,
+       0xC8,0x4D,0xBD,0x7A,0x2C,0x2D,0x47,0xE2,0x24,0x24,0xDF,0xC2,0x31,0x65,0x8F,0xD4,
+       0xBA,0x28,0x7C,0x4A,0xCA,0xAE,0x79,0xBE,0xC1,0x6C,0xFC,0x09,0x45,0xF7,0x87,0x17,
+       0xB4,0x55,0x92,0x15,0xC5,0xFA,0x8F,0xB0,0x56,0x96,0xC1,0x87,0x12,0xFE,0xDF,0xF0,
+       0x3A,0xE1,0xB1,0x83,0x19,0x74,0xF0,0x7D,0x37,0x41,0x3E,0x6A,0xFE,0x33,0x3E,0x74,
+       0x01,0x45,0xE4,0x65,0xAE,0xC9,0xAE,0x64,0xE3,0xF1,0x90,0xFD,0x1A,0x30,0x44,0x82,
+       0xEE,0x34,0x94,0xF2,0x68,0x3D,0x61,0x90,0xFB,0xEB,0xD8,0x18,0xE6,0x7C,0xEC,0x69,
+       0x70,0xD0,0xEB,0x2F,0xC1,0x3D,0x9C,0x6A,0x4B,0x89,0x50,0x6B,0x3F,0xA5,0x38,0x41,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x65,0xEE,0x34,0x09,0xAC,0x4C,0x21,0x71,0x1D,0x3F,0x7E,0x0D,0x01,0xC2,0x3E,0x34,
+       0x88,0x58,0xEC,0x4F,0x62,0x50,0xF7,0xD8,0x62,0xDF,0xC1,0x39,0x40,0xA0,0xBF,0x0B,
+       0xD5,0x2F,0x5B,0xFA,0x35,0x14,0x69,0x63,0x2C,0x36,0x4B,0xDF,0xEB,0x33,0x66,0x6B,
+       0x97,0xA9,0x6C,0x12,0x5D,0x08,0xD5,0x55,0x77,0x28,0x83,0xD7,0x3B,0xAE,0x05,0xC1,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x9F,0x96,0x17,0x75,0x14,0xCB,0xC9,0x8A,0x06,0xAE,0xF8,0x53,0x74,0xEF,0x2F,0x68,
+       0xCB,0xBA,0x75,0xBC,0xAF,0x97,0xBA,0xF0,0x90,0xA3,0xDC,0x33,0xA4,0x94,0x36,0xA8,
+       0xF5,0xC6,0x3E,0x4F,0x50,0x78,0xC9,0x49,0x2A,0x62,0x71,0x9A,0x5B,0x3E,0x5E,0x16,
+       0x8A,0xAC,0x4B,0xE7,0xA9,0x64,0x36,0x64,0x82,0x0F,0x23,0xB0,0x57,0x6D,0x16,0xE1,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x25,0xF1,0x40,0x05,0x58,0x19,0x37,0x61,0x34,0x98,0xBB,0x29,0x1B,0x44,0x08,0x1A,
+       0xD3,0x66,0x62,0x4C,0x9C,0x47,0xD2,0x91,0x60,0x46,0x6F,0x8E,0xA6,0xE7,0x80,0x7B,
+       0x17,0x77,0x9A,0xB5,0x18,0x8A,0x15,0x8F,0x77,0xA1,0x55,0x3E,0x96,0x66,0x86,0x57,
+       0x75,0x73,0xF5,0x57,0x50,0x28,0xEA,0x83,0x14,0xB1,0x55,0xA3,0x82,0xCD,0x36,0xF8
+};
+u_int8_t public_key_2[] = {
+       0x88,0x3E,0xE2,0x2E,0x5D,0x01,0x13,0xDF,0x1D,0x8B,0xF4,0x39,0xCA,0xE6,0x3C,0xE1,
+       0x46,0x8E,0xD4,0xF1,0x06,0x56,0x12,0x8D,0xCD,0x51,0xBD,0x32,0xF5,0x18,0x15,0x4D,
+       0x0F,0x98,0xDF,0xFF,0xA5,0xA3,0xAB,0x39,0x43,0xC4,0xF6,0xAC,0x98,0x5C,0x84,0x63,
+       0x8C,0x46,0x33,0xA2,0x23,0x8C,0xF0,0x4D,0xFE,0xE7,0xF3,0x38,0xC4,0x19,0x39,0xC4,
+       0x90,0xF4,0xC8,0x0D,0xB0,0xFE,0x65,0x11,0x0B,0x41,0x73,0xBB,0x05,0xA6,0x4B,0xC5,
+       0x27,0xA4,0x48,0x21,0xC5,0xAE,0x91,0x9C,0xD8,0x62,0x27,0xBE,0xDF,0xDA,0xC6,0x4E,
+       0xC1,0x6E,0x5B,0x61,0x51,0xAA,0xC9,0x53,0xCD,0x02,0x5B,0xC5,0xEE,0xE9,0xC7,0x7B,
+       0xB1,0x7E,0xD2,0xC2,0xFE,0x5F,0xD7,0x0F,0x75,0x2B,0xB9,0x49,0x5F,0x35,0xF1,0x83,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01
+};
+u_int8_t private_key_2[] = {
+       0x88,0x3E,0xE2,0x2E,0x5D,0x01,0x13,0xDF,0x1D,0x8B,0xF4,0x39,0xCA,0xE6,0x3C,0xE1,
+       0x46,0x8E,0xD4,0xF1,0x06,0x56,0x12,0x8D,0xCD,0x51,0xBD,0x32,0xF5,0x18,0x15,0x4D,
+       0x0F,0x98,0xDF,0xFF,0xA5,0xA3,0xAB,0x39,0x43,0xC4,0xF6,0xAC,0x98,0x5C,0x84,0x63,
+       0x8C,0x46,0x33,0xA2,0x23,0x8C,0xF0,0x4D,0xFE,0xE7,0xF3,0x38,0xC4,0x19,0x39,0xC4,
+       0x90,0xF4,0xC8,0x0D,0xB0,0xFE,0x65,0x11,0x0B,0x41,0x73,0xBB,0x05,0xA6,0x4B,0xC5,
+       0x27,0xA4,0x48,0x21,0xC5,0xAE,0x91,0x9C,0xD8,0x62,0x27,0xBE,0xDF,0xDA,0xC6,0x4E,
+       0xC1,0x6E,0x5B,0x61,0x51,0xAA,0xC9,0x53,0xCD,0x02,0x5B,0xC5,0xEE,0xE9,0xC7,0x7B,
+       0xB1,0x7E,0xD2,0xC2,0xFE,0x5F,0xD7,0x0F,0x75,0x2B,0xB9,0x49,0x5F,0x35,0xF1,0x83,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0xE8,0x37,0xB6,0x08,0xD8,0x9C,0x72,0xC5,0x34,0xDB,0x3A,0xA2,0xF9,0x24,0xE1,0x44,
+       0x23,0x3B,0x72,0x70,0x5D,0xCC,0xC3,0xBA,0x3D,0xCE,0x82,0xAC,0x6A,0x71,0x72,0x90,
+       0xC7,0x94,0xB3,0x8B,0x85,0xE0,0xEF,0x39,0xF0,0xE4,0x08,0x31,0xEA,0xE6,0x3B,0x7D,
+       0xB0,0x36,0xFA,0x71,0x6E,0xA3,0xF9,0x4C,0x39,0x05,0x8C,0xB7,0x8C,0x99,0x94,0x85,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x96,0x32,0xF9,0xD9,0xA8,0xC0,0x84,0xFD,0xE5,0x6B,0xA6,0xC2,0x85,0x85,0x68,0x17,
+       0x7E,0x98,0xD0,0x6A,0xDC,0xD8,0x4C,0x46,0xCB,0x6D,0x4C,0x25,0xE5,0xF9,0x58,0xB2,
+       0x17,0xE4,0x20,0x8A,0x87,0x0D,0xD7,0x4C,0x79,0xA3,0xB3,0x69,0x98,0x7F,0x5D,0x08,
+       0x33,0x5B,0xAD,0xA3,0x34,0xE8,0x55,0x5E,0x09,0x60,0x70,0xA8,0x11,0xFD,0x70,0x67,
+       0x00,0xE1,0xA7,0x44,0xF5,0x85,0x14,0x43,0xD5,0x45,0x1A,0x87,0x65,0x30,0xA8,0x24,
+       0x2C,0xF8,0xAF,0x97,0xFF,0x9A,0x7E,0xF4,0x3B,0xE7,0xD3,0x79,0x88,0xEC,0x66,0xF6,
+       0xE0,0xAA,0xF4,0x88,0x0A,0xE2,0x4C,0x31,0x4A,0xA6,0xF3,0x91,0x9A,0x4A,0xBE,0xF0,
+       0x85,0xEF,0xCE,0x55,0xB6,0x35,0x2B,0x38,0xD5,0xF5,0x5A,0x35,0x7B,0xCF,0x4D,0xF8,
+       0x5D,0x1E,0x57,0x99,0xAF,0xED,0x33,0x6F,0xD5,0xA7,0x49,0x5B,0x14,0x4C,0x7D,0x17,
+       0x81,0xAE,0x1E,0xDA,0x9D,0xFB,0xA9,0xC3,0x00,0x4C,0x17,0x37,0x30,0x96,0x60,0xE1,
+       0x6A,0xCC,0xD3,0xDB,0x40,0xCE,0x96,0x96,0x0D,0x95,0x0D,0x84,0x38,0xBD,0xDA,0x2F,
+       0xEC,0xED,0x22,0x39,0x8E,0x8C,0xDF,0xCD,0x07,0xCF,0x0F,0xB0,0x2B,0x76,0xDB,0xC1,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0xA5,0x37,0x9E,0x08,0x45,0x35,0x6A,0x62,0xEC,0xEC,0x5D,0x97,0xBE,0x73,0x82,0xE2,
+       0x9B,0xBE,0x9B,0xF9,0x5E,0x83,0x65,0x6E,0x88,0xB2,0xF9,0x3D,0xFA,0xAD,0xA4,0xB9,
+       0x65,0x86,0x63,0x08,0x0D,0xC4,0xAF,0xF0,0x25,0x77,0xD8,0x6C,0xCB,0x97,0xEB,0x13,
+       0xCD,0xE0,0x0F,0xE7,0xCC,0xB4,0x55,0x96,0xE9,0xAB,0x0D,0x27,0x3A,0x9D,0xBA,0x91,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x44,0xA3,0x44,0xF4,0x47,0x9E,0xBA,0xE7,0xBF,0xF8,0xC2,0xFB,0x2F,0xC3,0x38,0x3F,
+       0x4C,0x56,0x0F,0x20,0x56,0x8D,0xED,0xC5,0x88,0x5F,0x09,0x26,0x64,0x82,0xDF,0x1A,
+       0x7B,0xBA,0x7F,0x78,0x6E,0xA1,0x4F,0x9B,0x1E,0x17,0x45,0xFC,0xE2,0x78,0x89,0x8E,
+       0x1E,0xD2,0x2D,0x76,0x60,0xCE,0x2F,0x7C,0xCA,0xB2,0x2C,0xA9,0x51,0x97,0x4C,0xCF,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+       0x01,0x40,0x4B,0x7D,0xAB,0x8A,0xB9,0x5E,0xEE,0xA1,0x81,0xED,0x27,0x89,0xF6,0x4C,
+       0x59,0x8C,0x23,0x14,0x3B,0x1B,0xBA,0xC3,0xB2,0x00,0x9A,0x9E,0xDF,0x54,0x82,0xA7,
+       0x3E,0xC9,0x23,0x85,0x4D,0xD3,0x80,0xA7,0x89,0x11,0xBA,0x76,0xF5,0xC1,0x55,0x37,
+       0x0A,0x0D,0x8C,0x07,0x0A,0xC8,0xC5,0x11,0x74,0x9C,0xB6,0x80,0x3B,0x0A,0x9A,0xA2
+};
index fe01615..8b51a24 100644 (file)
@@ -26,6 +26,8 @@
 #include <types.h>
 #include <config/init_config.h>
 #include <config/sa_config.h>
+#include <transforms/rsa/rsa_private_key.h>
+#include <transforms/rsa/rsa_public_key.h>
 
 
 typedef struct configuration_manager_t configuration_manager_t;
@@ -133,6 +135,36 @@ struct configuration_manager_t {
         *                              - SUCCESS
         */     
        status_t (*get_shared_secret) (configuration_manager_t *this, identification_t *identification, chunk_t *preshared_secret);
+       
+       /**
+        * Get the RSA public key of a specific ID.
+        * 
+        * Object is not cloned and shuld not be destroyed.
+        * 
+        * @param this                                          calling object
+        * @param identification                        identification_t object identifiying the ID.
+        * @param[out] public_key                       the public key will be written there
+        * 
+        * @return              
+        *                              - NOT_FOUND                     if no key is configured for specific id
+        *                              - SUCCESS
+        */     
+       status_t (*get_rsa_public_key) (configuration_manager_t *this, identification_t *identification, rsa_public_key_t **public_key);
+       
+       /**
+        * Get the RSA public key of a specific ID.
+        * 
+        * Object is not cloned and shuld not be destroyed.
+        * 
+        * @param this                                          calling object
+        * @param identification                        identification_t object identifiying the ID.
+        * @param[out] private_key                      the private key will be written there
+        * 
+        * @return              
+        *                              - NOT_FOUND                     if no key is configured for specific id
+        *                              - SUCCESS
+        */     
+       status_t (*get_rsa_private_key) (configuration_manager_t *this, identification_t *identification, rsa_private_key_t **private_key);
 
        /**
         * Destroys configuration manager
index d1e0c13..be99007 100644 (file)
@@ -179,6 +179,7 @@ static void initialize(private_daemon_t *this)
        this->public.sender = sender_create();
        this->public.receiver = receiver_create();
        this->public.scheduler = scheduler_create();
+       this->public.prime_pool = prime_pool_create(0);
        this->public.thread_pool = thread_pool_create(NUMBER_OF_WORKING_THREADS);       
 }
 
@@ -203,6 +204,10 @@ static void destroy(private_daemon_t *this)
        {
                this->public.thread_pool->destroy(this->public.thread_pool);    
        }
+       if (this->public.prime_pool != NULL)
+       {
+               this->public.prime_pool->destroy(this->public.prime_pool);      
+       }
        if (this->public.job_queue != NULL)
        {
                this->public.job_queue->destroy(this->public.job_queue);
@@ -240,7 +245,7 @@ static void destroy(private_daemon_t *this)
  * @return     created daemon_t
  */
 private_daemon_t *daemon_create()
-{
+{      
        private_daemon_t *this = allocator_alloc_thing(private_daemon_t);
                
        /* assign methods */
@@ -285,6 +290,9 @@ int main(int argc, char *argv[])
 {
        private_daemon_t *private_charon;
        
+       /* allocation needs initialization, before any allocs are done */
+       allocator_init();
+       
        private_charon = daemon_create();
        charon = (daemon_t*)private_charon;
        
index 98d366c..b0591d8 100644 (file)
@@ -29,6 +29,7 @@
 #include <threads/receiver.h>
 #include <threads/scheduler.h>
 #include <threads/thread_pool.h>
+#include <threads/prime_pool.h>
 #include <sa/ike_sa_manager.h>
 #include <queues/send_queue.h>
 #include <queues/job_queue.h>
@@ -54,7 +55,7 @@
  * Port on which the daemon will 
  * listen for incoming traffic.
  */
-#define IKEV2_UDP_PORT 500
+#define IKEV2_UDP_PORT 4500
 
 /**
  * First retransmit timeout in milliseconds.
@@ -129,6 +130,11 @@ struct daemon_t {
        thread_pool_t *thread_pool;
        
        /**
+        * Low-priority thread which generates primes
+        */
+       prime_pool_t *prime_pool;
+       
+       /**
         * @brief shut down the daemon
         * 
         * @param this          the daemon to kill
index 8efbb70..6baac9c 100644 (file)
@@ -152,7 +152,7 @@ static status_t verify_auth_data (private_authenticator_t *this,auth_payload_t *
                case SHARED_KEY_MESSAGE_INTEGRITY_CODE:
                {
                        
-                       identification_t *other_id =other_id_payload->get_identification(other_id_payload);
+                       identification_t *other_id = other_id_payload->get_identification(other_id_payload);
                        chunk_t auth_data = auth_payload->get_data(auth_payload);
                        chunk_t preshared_secret;
                        status_t status;
@@ -183,6 +183,37 @@ static status_t verify_auth_data (private_authenticator_t *this,auth_payload_t *
                        allocator_free_chunk(&my_auth_data);            
                        return SUCCESS;
                }
+               case RSA_DIGITAL_SIGNATURE:
+               {
+                       identification_t *other_id = other_id_payload->get_identification(other_id_payload);
+                       rsa_public_key_t *public_key;
+                       status_t status;
+                       chunk_t octets, auth_data;
+                       
+                       auth_data = auth_payload->get_data(auth_payload);
+                       
+                       status = charon->configuration_manager->get_rsa_public_key(charon->configuration_manager, other_id, &public_key);
+                       other_id->destroy(other_id);
+                       if (status != SUCCESS)
+                       {
+                               return status;  
+                       }
+                       
+                       octets = this->allocate_octets(this,last_received_packet,my_nonce,other_id_payload,initiator);
+                       
+                       status = public_key->verify_emsa_pkcs1_signature(public_key, octets, auth_data);
+                       if (status == SUCCESS)
+                       {
+                               *verified = TRUE;       
+                       }
+                       else
+                       {
+                               *verified = FALSE;      
+                       }
+                       
+                       allocator_free_chunk(&octets);
+                       return status;
+               }
                default:
                {
                        return NOT_SUPPORTED;
@@ -224,6 +255,36 @@ static status_t compute_auth_data (private_authenticator_t *this,auth_payload_t
                        allocator_free_chunk(&auth_data);
                        return SUCCESS;
                }
+               case RSA_DIGITAL_SIGNATURE:
+               {
+                       identification_t *my_id = my_id_payload->get_identification(my_id_payload);
+                       rsa_private_key_t *private_key;
+                       status_t status;
+                       chunk_t octets, auth_data;
+                       
+                       status = charon->configuration_manager->get_rsa_private_key(charon->configuration_manager, my_id, &private_key);
+                       my_id->destroy(my_id);
+                       if (status != SUCCESS)
+                       {
+                               return status;  
+                       }
+                       
+                       octets = this->allocate_octets(this,last_sent_packet,other_nonce,my_id_payload,initiator);
+                       
+                       status = private_key->build_emsa_pkcs1_signature(private_key, HASH_SHA1, octets, &auth_data);
+                       allocator_free_chunk(&octets);
+                       if (status != SUCCESS)
+                       {
+                               return status;  
+                       }
+                       
+                       *auth_payload = auth_payload_create();
+                       (*auth_payload)->set_auth_method((*auth_payload), RSA_DIGITAL_SIGNATURE);
+                       (*auth_payload)->set_data((*auth_payload),auth_data);
+
+                       allocator_free_chunk(&auth_data);
+                       return SUCCESS;
+               }
                default:
                {
                        return NOT_SUPPORTED;
index 8463c1a..bdb54c2 100644 (file)
@@ -115,3 +115,11 @@ $(BUILD_DIR)init_config_test.o :   $(TESTCASES_DIR)init_config_test.c $(TESTCASES_
 TEST_OBJS+= $(BUILD_DIR)sa_config_test.o
 $(BUILD_DIR)sa_config_test.o :         $(TESTCASES_DIR)sa_config_test.c $(TESTCASES_DIR)sa_config_test.h
                                                                        $(CC) $(CFLAGS) -c -o $@ $<
+
+TEST_OBJS+= $(BUILD_DIR)rsa_test.o
+$(BUILD_DIR)rsa_test.o :                       $(TESTCASES_DIR)rsa_test.c $(TESTCASES_DIR)rsa_test.h
+                                                                       $(CC) $(CFLAGS) -c -o $@ $<
+
+TEST_OBJS+= $(BUILD_DIR)prime_pool_test.o
+$(BUILD_DIR)prime_pool_test.o :                $(TESTCASES_DIR)prime_pool_test.c $(TESTCASES_DIR)prime_pool_test.h
+                                                                       $(CC) $(CFLAGS) -c -o $@ $<
diff --git a/Source/charon/testcases/prime_pool_test.c b/Source/charon/testcases/prime_pool_test.c
new file mode 100644 (file)
index 0000000..a8fca0a
--- /dev/null
@@ -0,0 +1,67 @@
+/**
+ * @file prime_pool_test.h
+ * 
+ * @brief Tests for the hasher_t classes.
+ * 
+ */
+
+/*
+ * 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 <string.h>
+#include <unistd.h>
+#include "prime_pool_test.h"
+
+#include <daemon.h>
+#include <utils/allocator.h>
+#include <utils/logger.h>
+#include <threads/prime_pool.h>
+
+
+/* 
+ * described in Header-File
+ */
+void test_prime_pool(tester_t *tester)
+{
+       mpz_t p1, p2, p3, p4;
+       prime_pool_t *prime_pool;
+       
+       prime_pool = prime_pool_create(20);
+       
+       prime_pool->get_prime(prime_pool, 4, &p1);
+       sleep(1);
+       tester->assert_true(tester, prime_pool->get_count(prime_pool, 4) == 20, "number of  4 bytes primes");
+       tester->assert_true(tester, prime_pool->get_count(prime_pool, 8) == 0, "number of  8 bytes primes");
+       tester->assert_true(tester, prime_pool->get_count(prime_pool, 16) == 0, "number of 16 bytes primes");
+       prime_pool->get_prime(prime_pool, 8, &p2);      
+       sleep(1);
+       tester->assert_true(tester, prime_pool->get_count(prime_pool, 4) == 20, "number of  4 bytes primes");
+       tester->assert_true(tester, prime_pool->get_count(prime_pool, 8) == 20, "number of  8 bytes primes");
+       tester->assert_true(tester, prime_pool->get_count(prime_pool, 16) == 0, "number of 16 bytes primes");
+       prime_pool->get_prime(prime_pool, 16, &p3);
+       sleep(1);
+       tester->assert_true(tester, prime_pool->get_count(prime_pool, 4) == 20, "number of  4 bytes primes");
+       tester->assert_true(tester, prime_pool->get_count(prime_pool, 8) == 20, "number of  8 bytes primes");
+       tester->assert_true(tester, prime_pool->get_count(prime_pool, 16) == 20, "number of 16 bytes primes");
+       prime_pool->get_prime(prime_pool, 16, &p4);
+       
+       mpz_clear(p1);
+       mpz_clear(p2);
+       mpz_clear(p3);
+       mpz_clear(p4);
+       prime_pool->destroy(prime_pool);
+       
+}
diff --git a/Source/charon/testcases/prime_pool_test.h b/Source/charon/testcases/prime_pool_test.h
new file mode 100644 (file)
index 0000000..5e6080f
--- /dev/null
@@ -0,0 +1,39 @@
+/**
+ * @file prime_pool_test.h
+ * 
+ * @brief Tests for the prime_pool_t class.
+ * 
+ */
+
+/*
+ * 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 PRIME_POOL_TEST_H
+#define PRIME_POOL_TEST_H
+
+#include <threads/prime_pool.h>
+#include <utils/tester.h>
+
+/**
+ * @brief Test function used to test the prime_pool functionality.
+ *
+ * @param tester associated tester object
+ * 
+ * @ingroup testcases
+ */
+void test_prime_pool(tester_t *tester);
+
+
+#endif /*PRIME_POOL_TEST_H*/
diff --git a/Source/charon/testcases/rsa_test.c b/Source/charon/testcases/rsa_test.c
new file mode 100644 (file)
index 0000000..37f3495
--- /dev/null
@@ -0,0 +1,85 @@
+/**
+ * @file rsa_test.h
+ * 
+ * @brief Tests for the hasher_t classes.
+ * 
+ */
+
+/*
+ * 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 <string.h>
+#include "rsa_test.h"
+
+#include <daemon.h>
+#include <utils/allocator.h>
+#include <utils/logger.h>
+
+
+/* 
+ * described in Header-File
+ */
+void test_rsa(tester_t *tester)
+{
+       rsa_private_key_t *private_key;
+       rsa_public_key_t *public_key;
+       chunk_t data, signature, private_key_chunk, public_key_chunk;
+       logger_t *logger;
+       status_t status;
+       u_int8_t test_data[] = {
+               0x01,0x02,0x03,0x04,
+               0x01,0x02,0x03,0x04,
+               0x01,0x02,0x03,0x04,
+               0x01,0x02,0x03,0x04,
+               0x01,0x02,0x03,0x04,
+               0x01,0x02,0x03,0x04,
+               0x01,0x02,0x03,0x04,
+               0x01,0x02,0x03,0x04,
+               0x01,0x02,0x03,0x04,
+       };
+       data.ptr = test_data;
+       data.len = sizeof(test_data);
+       
+       logger = charon->logger_manager->create_logger(charon->logger_manager, TESTER, NULL);
+       logger->enable_level(logger, FULL);
+       
+       private_key = rsa_private_key_create();
+       
+       private_key->generate_key(private_key, 1024);
+       
+       status = private_key->build_emsa_pkcs1_signature(private_key, HASH_MD5, data, &signature);
+       tester->assert_true(tester, status == SUCCESS, "build emsa_pkcs1_signature");
+       
+       public_key = private_key->get_public_key(private_key);
+       
+       status = public_key->verify_emsa_pkcs1_signature(public_key, data, signature);
+       tester->assert_true(tester, status == SUCCESS, "verify emsa_pkcs1_signature");
+       
+       public_key->get_key(public_key, &public_key_chunk);
+       private_key->get_key(private_key, &private_key_chunk);
+       
+       logger->log_chunk(logger, RAW, "Public Key", &public_key_chunk);
+       logger->log_chunk(logger, RAW, "Private Key", &private_key_chunk);
+       
+       
+       allocator_free(public_key_chunk.ptr);
+       allocator_free(private_key_chunk.ptr);
+       allocator_free(signature.ptr);
+       
+       private_key->destroy(private_key);
+       public_key->destroy(public_key);
+       
+}
diff --git a/Source/charon/testcases/rsa_test.h b/Source/charon/testcases/rsa_test.h
new file mode 100644 (file)
index 0000000..3bc749f
--- /dev/null
@@ -0,0 +1,41 @@
+/**
+ * @file rsa_test.h
+ * 
+ * @brief Tests for the rsa_public_key_t and rsa_private_key classes.
+ * 
+ */
+
+/*
+ * 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 RSA_TEST_H
+#define RSA_TEST_H
+
+#include <transforms/rsa/rsa_public_key.h>
+#include <transforms/rsa/rsa_private_key.h>
+
+#include <utils/tester.h>
+
+/**
+ * @brief Test function used to test the rsa functionality.
+ *
+ * @param tester associated tester object
+ * 
+ * @ingroup testcases
+ */
+void test_rsa(tester_t *tester);
+
+
+#endif /*RSA_TEST_H*/
index f02d9d4..03a318f 100644 (file)
@@ -58,6 +58,8 @@
 #include <testcases/encryption_payload_test.h>
 #include <testcases/init_config_test.h>
 #include <testcases/sa_config_test.h>
+#include <testcases/rsa_test.h>
+#include <testcases/prime_pool_test.h>
 
 /* output for test messages */
 extern FILE * stderr;
@@ -108,6 +110,8 @@ test_t hmac_signer_test2 = {test_hmac_sha1_signer, "HMAC SHA1 signer test"};
 test_t encryption_payload_test = {test_encryption_payload, "encryption payload test"};
 test_t init_config_test = {test_init_config, "init_config_t test"};
 test_t sa_config_test = {test_sa_config, "sa_config_t test"};
+test_t rsa_test = {test_rsa, "RSA private/public key test"};
+test_t prime_pool_test = {test_prime_pool, "Prime pool"};
 
 
 daemon_t* charon;
@@ -118,6 +122,7 @@ static void daemon_kill(daemon_t *this, char* none)
        this->socket->destroy(this->socket);
        this->ike_sa_manager->destroy(this->ike_sa_manager);
        this->job_queue->destroy(this->job_queue);
+       this->prime_pool->destroy(this->prime_pool);
        this->event_queue->destroy(this->event_queue);
        this->send_queue->destroy(this->send_queue);
        this->configuration_manager->destroy(this->configuration_manager);
@@ -130,18 +135,19 @@ static void daemon_kill(daemon_t *this, char* none)
  * @return     created daemon_t
  */
 daemon_t *daemon_create()
-{
+{      
        charon = allocator_alloc_thing(daemon_t);
                
        /* assign methods */
        charon->kill = daemon_kill;
        
        charon->logger_manager = logger_manager_create(0);
-       charon->socket = socket_create(4601);
+       charon->socket = socket_create(4600);
        charon->ike_sa_manager = ike_sa_manager_create();
        charon->job_queue = job_queue_create();
        charon->event_queue = event_queue_create();
        charon->send_queue = send_queue_create();
+       charon->prime_pool = prime_pool_create(0);
        charon->configuration_manager = configuration_manager_create(RETRANSMIT_TIMEOUT,MAX_RETRANSMIT_COUNT);
        charon->sender = NULL;
        charon->receiver = NULL;
@@ -203,8 +209,13 @@ int main()
                &encryption_payload_test,
                &init_config_test,
                &sa_config_test,
+               &rsa_test,
+               &prime_pool_test,
                NULL
        };
+       
+       /* allocator needs initialization */
+       allocator_init();
  
        daemon_create();
  
@@ -214,8 +225,8 @@ int main()
        tester_t *tester = tester_create(test_output, FALSE);
        
 
-       tester->perform_tests(tester,all_tests);
-//     tester->perform_test(tester,&sa_config_test); 
+//     tester->perform_tests(tester,all_tests);
+       tester->perform_test(tester,&rsa_test); 
        
        
        tester->destroy(tester);
index 4c9cd5e..4ed507a 100644 (file)
@@ -29,3 +29,7 @@ $(BUILD_DIR)sender.o :                        $(THREADS_DIR)sender.c $(THREADS_DIR)sender.h
 OBJS+= $(BUILD_DIR)thread_pool.o
 $(BUILD_DIR)thread_pool.o :            $(THREADS_DIR)thread_pool.c $(THREADS_DIR)thread_pool.h
                                                                $(CC) $(CFLAGS) -c -o $@ $<
+                                                               
+OBJS+= $(BUILD_DIR)prime_pool.o
+$(BUILD_DIR)prime_pool.o :             $(THREADS_DIR)prime_pool.c $(THREADS_DIR)prime_pool.h
+                                                               $(CC) $(CFLAGS) -c -o $@ $<
diff --git a/Source/charon/threads/prime_pool.c b/Source/charon/threads/prime_pool.c
new file mode 100644 (file)
index 0000000..d8fc2b1
--- /dev/null
@@ -0,0 +1,357 @@
+/**
+ * @file prime_pool.c
+ *
+ * @brief Implementation of prime_pool_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 <pthread.h>
+
+#include "prime_pool.h"
+
+#include <utils/allocator.h>
+#include <utils/linked_list.h>
+#include <utils/randomizer.h>
+
+
+typedef struct prime_list_t prime_list_t;
+
+/**
+ * A prime_list_t contains prime for a specific size.
+ */
+struct prime_list_t {
+       /**
+        * Size of the stored primes 
+        */
+       size_t prime_size;
+       
+       /**
+        * is this much used prime_size ?
+        */
+       u_int32_t usage;
+       
+       /**
+        * list of primes
+        */
+       linked_list_t *primes;
+};
+
+typedef struct private_prime_pool_t private_prime_pool_t;
+
+ /**
+ * @brief Private Variables and Functions of prime_pool class
+ *
+ */
+struct private_prime_pool_t {
+       /**
+        * Public part of the prime_pool_t object
+        */
+       prime_pool_t public;
+
+       /**
+        * A list which contains a set of prime_list_t's
+        */
+       linked_list_t *prime_lists;
+
+       /**
+        * prime generation is stopped if more than
+        * that primes of a kind are already generated
+        */
+       int generation_limit;
+       
+       /**
+        * access to prime_lists is locked through this mutex
+        */
+       pthread_mutex_t mutex;
+
+       /**
+        * If the queue is empty a thread has to wait
+        * This condvar is used to wake up such a thread
+        */
+       pthread_cond_t condvar;
+       
+       /**
+        * prime generation thread 
+        */
+       pthread_t thread;
+       
+       /**
+        * Function for the prime thread, generate primes
+        */
+       void (*generate_primes) (private_prime_pool_t *this);
+       
+       /**
+        * calculate a prime of requested size
+        */
+       void (*compute_prime) (private_prime_pool_t *this, size_t prime_size, mpz_t *prime);
+};
+
+
+/**
+ * implements prime_pool_t.get_count
+ */
+static int get_count(private_prime_pool_t *this, size_t prime_size)
+{
+       int count = 0;
+       iterator_t *iterator;
+       
+       pthread_mutex_lock(&(this->mutex));
+       
+       iterator = this->prime_lists->create_iterator(this->prime_lists, TRUE);
+       while (iterator->has_next(iterator))
+       {
+               prime_list_t *prime_list;
+               iterator->current(iterator, (void*)&prime_list);
+               if (prime_list->prime_size == prime_size)
+               {
+                       count = prime_list->primes->get_count(prime_list->primes);
+                       break;
+               }
+       }
+       iterator->destroy(iterator);
+       
+       pthread_mutex_unlock(&(this->mutex));
+       return count;
+}
+
+/**
+ * implements prime_pool_t.get_prime
+ */
+static void get_prime(private_prime_pool_t *this, size_t prime_size, mpz_t *prime)
+{
+       bool prime_found = FALSE;
+       iterator_t *iterator;
+       bool create_new_list = TRUE;
+       
+       pthread_mutex_lock(&(this->mutex));
+       
+       iterator = this->prime_lists->create_iterator(this->prime_lists, TRUE);
+       while (iterator->has_next(iterator))
+       {
+               prime_list_t *prime_list;
+               iterator->current(iterator, (void*)&prime_list);
+               if (prime_list->prime_size == prime_size)
+               {
+                       mpz_t *removed_prime;
+                       create_new_list = FALSE;
+                       prime_list->usage += 2;
+                       if (prime_list->primes->remove_first(prime_list->primes, (void*)&removed_prime) == SUCCESS)
+                       {
+                               mpz_init_set(*prime, *removed_prime);
+                               mpz_clear(*removed_prime);
+                               allocator_free(removed_prime);
+                               prime_found = TRUE;
+                       }
+                       /* wake up prime thread, he may be sleeping */
+                       pthread_cond_signal(&(this->condvar));
+                       break;
+               }
+       }
+       iterator->destroy(iterator);
+       
+       if (create_new_list)
+       {
+               /* there is no list for this prime size, create one */
+               prime_list_t *prime_list;
+               prime_list = allocator_alloc_thing(prime_list_t);
+               prime_list->usage = 1;
+               prime_list->primes = linked_list_create();
+               prime_list->prime_size = prime_size;
+               this->prime_lists->insert_last(this->prime_lists, (void*)prime_list);
+               /* wake up prime thread, he may be sleeping */
+               pthread_cond_signal(&(this->condvar));
+       }
+       
+       pthread_mutex_unlock(&(this->mutex));
+       
+       if (!prime_found)
+       {
+               /* no prime found, create one ourself */
+               this->compute_prime(this, prime_size, prime);
+       }
+}
+
+/**
+ * implements private_prime_pool_t.compute_prime
+ */
+void compute_prime(private_prime_pool_t *this, size_t prime_size, mpz_t *prime)
+{
+       randomizer_t *randomizer;
+       chunk_t random_bytes;
+       
+       randomizer = randomizer_create();
+       mpz_init(*prime);
+       
+       do
+       {
+               /* TODO change to true random device ? */
+               randomizer->allocate_pseudo_random_bytes(randomizer, prime_size, &random_bytes);
+               
+               /* make sure most significant bit is set */
+               random_bytes.ptr[0] = random_bytes.ptr[0] | 0x80;
+               
+               /* convert chunk to mpz value */
+               mpz_import(*prime, random_bytes.len, 1, 1, 1, 0, random_bytes.ptr);
+
+               /* get next prime */
+               mpz_nextprime (*prime, *prime);
+
+               allocator_free(random_bytes.ptr);
+       }
+       /* check if it isnt too large */
+       while (((mpz_sizeinbase(*prime, 2) + 7) / 8) > prime_size);
+       
+       randomizer->destroy(randomizer);
+}
+
+/**
+ * implements private_prime_pool_t.generate_primes
+ */
+void generate_primes(private_prime_pool_t *this)
+{
+       /* allow cancellation */
+       pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
+       
+       /* reduce priority */
+       while (TRUE)
+       {
+               prime_list_t *selected_prime_list = NULL;
+               u_int32_t max_usage = 0;
+               iterator_t *iterator;
+               mpz_t *prime;
+               
+               pthread_mutex_lock(&(this->mutex));
+               
+               /* get aprime to generate */
+               iterator = this->prime_lists->create_iterator(this->prime_lists, TRUE);
+               while (iterator->has_next(iterator))
+               {
+                       prime_list_t *prime_list;
+                       iterator->current(iterator, (void*)&prime_list);
+                       if (prime_list->usage > max_usage)
+                       {
+                               if (prime_list->primes->get_count(prime_list->primes) < this->generation_limit)
+                               {
+                                       /* there is work to do */
+                                       max_usage = prime_list->usage;
+                                       selected_prime_list = prime_list;
+                               }
+                       }
+               }
+               iterator->destroy(iterator);
+               
+               if (selected_prime_list == NULL)
+               {               
+                       /* wait, be able to cancel */
+                       pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock, (void*)&(this->mutex));
+                       pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
+
+                       pthread_cond_wait(&(this->condvar), &(this->mutex));
+
+                       pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
+                       pthread_cleanup_pop(0);
+               }
+               
+               pthread_mutex_unlock(&(this->mutex));
+               
+               if (selected_prime_list != NULL)
+               {
+                       /* generate the prime of requested size */
+                       prime = allocator_alloc_thing(mpz_t);
+                       compute_prime(this, selected_prime_list->prime_size, prime);
+                       
+                       /* insert prime */
+                       pthread_mutex_lock(&(this->mutex));
+                       selected_prime_list->primes->insert_last(selected_prime_list->primes, (void*)prime);
+                       pthread_mutex_unlock(&(this->mutex));
+               }
+       }
+}
+
+
+ /**
+ * implements prime_pool_t.destroy
+ */
+static void destroy (private_prime_pool_t *this)
+{      
+       if (this->generation_limit > 0)
+       {
+               pthread_cancel(this->thread);
+               pthread_join(this->thread, NULL);
+       }
+       
+
+       while ((this->prime_lists->get_count(this->prime_lists) > 0))
+       {
+               prime_list_t *prime_list;
+               
+               this->prime_lists->remove_last(this->prime_lists, (void*)&prime_list);
+               
+               /* clear every mpz */
+               while (prime_list->primes->get_count(prime_list->primes) > 0)
+               {
+                       mpz_t *prime;
+                       prime_list->primes->remove_last(prime_list->primes, (void**)&prime);
+                       mpz_clear(*prime);
+                       allocator_free(prime);
+               }
+               prime_list->primes->destroy(prime_list->primes);
+               allocator_free(prime_list);
+       }
+       this->prime_lists->destroy(this->prime_lists);
+
+       pthread_mutex_destroy(&(this->mutex));
+       pthread_cond_destroy(&(this->condvar));
+
+       allocator_free(this);
+}
+
+/*
+ *
+ * Documented in header
+ */
+prime_pool_t *prime_pool_create(int generation_limit)
+{
+       private_prime_pool_t *this = allocator_alloc_thing(private_prime_pool_t);
+       
+       /* public functions */
+       this->public.get_count = (int(*)(prime_pool_t*,size_t)) get_count;
+       this->public.get_prime = (void(*)(prime_pool_t*,size_t,mpz_t*)) get_prime;
+       this->public.destroy = (void(*)(prime_pool_t*)) destroy;
+
+       /* private members */
+       this->generate_primes = generate_primes;
+       this->compute_prime = compute_prime;
+       this->generation_limit = generation_limit;
+       this->prime_lists = linked_list_create();
+       pthread_mutex_init(&(this->mutex), NULL);
+       pthread_cond_init(&(this->condvar), NULL);
+       
+       if (generation_limit > 0)
+       {
+               if (pthread_create(&(this->thread), NULL, (void*(*)(void*))this->generate_primes, this) != 0)
+               {
+                       pthread_mutex_destroy(&(this->mutex));
+                       pthread_cond_destroy(&(this->condvar));
+                       this->prime_lists->destroy(this->prime_lists);
+                       allocator_free(this);
+                       return NULL;
+               }
+       }
+       return (&this->public);
+}
diff --git a/Source/charon/threads/prime_pool.h b/Source/charon/threads/prime_pool.h
new file mode 100644 (file)
index 0000000..b9b5497
--- /dev/null
@@ -0,0 +1,85 @@
+/**
+ * @file prime_pool.h
+ *
+ * @brief Interface of prime_pool_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 PRIME_POOL_H_
+#define PRIME_POOL_H_
+
+#include <gmp.h>
+
+#include <types.h>
+#include <network/packet.h>
+
+
+typedef struct prime_pool_t prime_pool_t;
+
+/**
+ * @brief Prime generation
+ * 
+ * Starts a low-priority thread which will
+ * generate generate primes in the background.
+ * This increases responsibility, since prime generation
+ * is the most time-comsuming task.
+ * 
+ * @ingroup threads
+ */
+struct prime_pool_t {
+
+       /**
+        * @brief Get the number of available primes for the given prime size.
+        *
+        * @param prime_pool_t  calling object
+        * @param                               size of the prime
+        * @returns                     number of primes
+        */
+       int (*get_count) (prime_pool_t *prime_pool, size_t prime_size);
+
+       /**
+        * @brief Get a prime for the given size.
+        *
+        * If no primes are available, the threads generates one of its own.
+        * Prime is allocated and must be freed by caller.
+        *
+        * @param prime_pool_t  calling object
+        * @return                              chunk containing the prime
+        */
+       void (*get_prime) (prime_pool_t *prime_pool, size_t prime_size, mpz_t *prime);
+
+       /**
+        * @brief destroys a prime_pool object.
+        *
+        * Stopps the prime thread and destroys the pool.
+        *
+        * @param prime_pool_t  calling object
+        */
+       void (*destroy) (prime_pool_t *prime_pool);
+};
+
+/**
+ * @brief Creates a prime pool with a thread in it.
+ *
+ * @return prime_pool_t the prime_pool
+ * 
+ * @ingroup threads
+ */
+prime_pool_t *prime_pool_create(int generation_limit);
+
+#endif /*PRIME_POOL_H_*/
index 9bc8076..bed865c 100644 (file)
@@ -18,6 +18,7 @@ include $(TRANSFORMS_DIR)crypters/Makefile.crypters
 include $(TRANSFORMS_DIR)hashers/Makefile.hashers
 include $(TRANSFORMS_DIR)prfs/Makefile.prfs
 include $(TRANSFORMS_DIR)signers/Makefile.signers
+include $(TRANSFORMS_DIR)rsa/Makefile.rsa
 
 OBJS+= $(BUILD_DIR)diffie_hellman.o
 $(BUILD_DIR)diffie_hellman.o : $(TRANSFORMS_DIR)diffie_hellman.c $(TRANSFORMS_DIR)diffie_hellman.h
index cd96747..1ff4229 100644 (file)
@@ -57,6 +57,9 @@ typedef struct crypter_t crypter_t;
 /**
  * @brief Generic interface for symmetric encryption algorithms.
  * 
+ * @todo Distinguish between block_size and key_size, since not all
+ * algorithms use key_size == block_size (e.g. 3DES).
+ * 
  * @ingroup crypters
  */
 struct crypter_t {
index dc2acc6..266c4b3 100644 (file)
@@ -27,9 +27,9 @@
 
 #include "diffie_hellman.h"
 
+#include <daemon.h>
 #include <utils/allocator.h>
 #include <utils/randomizer.h>
-#include <utils/gmp_helper.h>
 
 
 /** 
@@ -404,11 +404,6 @@ struct private_diffie_hellman_t {
         * True if shared secret is computed and stored in my_public_value.
         */
        bool shared_secret_is_computed;
-
-       /**
-        * helper class for gmp functions.
-        */     
-       gmp_helper_t *gmp_helper;
        
        /**
         * Sets the modulus for a specific diffie hellman group.
@@ -450,7 +445,7 @@ static status_t set_modulus(private_diffie_hellman_t *this)
                        chunk_t modulus_chunk;
                        modulus_chunk.ptr = modulus_info_entries[i].modulus;
                        modulus_chunk.len = modulus_info_entries[i].modulus_length;
-                       this->gmp_helper->chunk_to_mpz(this->gmp_helper,&(this->modulus),modulus_chunk);
+                       mpz_import(this->modulus, modulus_chunk.len, 1, 1, 1, 0, modulus_chunk.ptr);
                        this->modulus_length = modulus_chunk.len;
                        this->generator = modulus_info_entries[i].generator;
                        status = SUCCESS;
@@ -465,7 +460,7 @@ static status_t set_modulus(private_diffie_hellman_t *this)
  */
 static void set_other_public_value(private_diffie_hellman_t *this,chunk_t public_value)
 {
-       this->gmp_helper->chunk_to_mpz(this->gmp_helper,&(this->other_public_value),public_value);
+       mpz_import(this->other_public_value, public_value.len, 1, 1, 1, 0, public_value.ptr);
        this->compute_shared_secret(this);
 }
 
@@ -478,7 +473,8 @@ static status_t get_other_public_value(private_diffie_hellman_t *this,chunk_t *p
        {
                return FAILED;
        }
-       this->gmp_helper->mpz_to_chunk(this->gmp_helper,&(this->other_public_value), public_value,this->modulus_length);
+       public_value->len = this->modulus_length;
+    public_value->ptr = mpz_export(NULL, NULL, 1, public_value->len, 1, 0, this->other_public_value);
        return SUCCESS;
 }
 
@@ -517,7 +513,8 @@ static void compute_public_value (private_diffie_hellman_t *this)
  */
 static void get_my_public_value(private_diffie_hellman_t *this,chunk_t *public_value)
 {
-       this->gmp_helper->mpz_to_chunk(this->gmp_helper,&(this->my_public_value), public_value,this->modulus_length);
+       public_value->len = this->modulus_length;
+    public_value->ptr = mpz_export(NULL, NULL, 1, public_value->len, 1, 0, this->my_public_value);
 }
 
 /**
@@ -529,7 +526,8 @@ static status_t get_shared_secret(private_diffie_hellman_t *this,chunk_t *secret
        {
                return FAILED;
        }
-       this->gmp_helper->mpz_to_chunk(this->gmp_helper,&(this->shared_secret), secret,this->modulus_length);
+       secret->len = this->modulus_length;
+    secret->ptr = mpz_export(NULL, NULL, 1, secret->len, 1, 0, this->shared_secret);
        return SUCCESS;
 }
 
@@ -538,7 +536,6 @@ static status_t get_shared_secret(private_diffie_hellman_t *this,chunk_t *secret
  */
 static void destroy(private_diffie_hellman_t *this)
 {
-       this->gmp_helper->destroy(this->gmp_helper);
        mpz_clear(this->modulus);
        mpz_clear(this->my_prime);
        mpz_clear(this->my_public_value);
@@ -575,17 +572,14 @@ diffie_hellman_t *diffie_hellman_create(diffie_hellman_group_t dh_group_number)
        /* private variables */
        this->dh_group_number = dh_group_number;
        
-       this->gmp_helper = gmp_helper_create();
-       
        /* set this->modulus */ 
        if (this->set_modulus(this) != SUCCESS)
        {
-               this->gmp_helper->destroy(this->gmp_helper);
                allocator_free(this);
                return NULL;
        }
        
-       this->gmp_helper->init_prime(this->gmp_helper,&(this->my_prime),this->modulus_length);
+       charon->prime_pool->get_prime(charon->prime_pool, this->modulus_length, &(this->my_prime));
        
        this->compute_public_value(this);
        
index 170dfe8..0b86eef 100644 (file)
  * mappings for hash_algorithm_t
  */
 mapping_t hash_algorithm_m[] = {
-       {HASH_SHA1, "HASH_SHA1"},
-       {HASH_MD5, "HASH_MD5"},
+       {HASH_MD2,"HASH_MD2"},
+       {HASH_MD5,"HASH_MD5"},
+       {HASH_SHA1,"HASH_SHA1"},
+       {HASH_SHA256,"HASH_SHA256"},
+       {HASH_SHA384,"HASH_SHA384"},
+       {HASH_SHA512,"HASH_SHA512"},
        {MAPPING_END, NULL}
 };
 
index eda6fe1..90cf89e 100644 (file)
@@ -32,8 +32,12 @@ typedef enum hash_algorithm_t hash_algorithm_t;
  * @brief Algorithms to use for hashing.
  */
 enum hash_algorithm_t {
-       HASH_SHA1,
-       HASH_MD5
+       HASH_MD2,
+       HASH_MD5, /* supported */
+       HASH_SHA1, /* supported */
+       HASH_SHA256,
+       HASH_SHA384,
+       HASH_SHA512,
 };
 
 /**
diff --git a/Source/charon/transforms/rsa/Makefile.rsa b/Source/charon/transforms/rsa/Makefile.rsa
new file mode 100644 (file)
index 0000000..4afe373
--- /dev/null
@@ -0,0 +1,23 @@
+# 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.
+#
+
+RSA_DIR= $(TRANSFORMS_DIR)rsa/
+
+OBJS+= $(BUILD_DIR)rsa_private_key.o
+$(BUILD_DIR)rsa_private_key.o :                        $(RSA_DIR)rsa_private_key.c $(RSA_DIR)rsa_private_key.h
+                                                                               $(CC) $(CFLAGS) -c -o $@ $<
+                                                                               
+OBJS+= $(BUILD_DIR)rsa_public_key.o
+$(BUILD_DIR)rsa_public_key.o :                 $(RSA_DIR)rsa_public_key.c $(RSA_DIR)rsa_public_key.h
+                                                                               $(CC) $(CFLAGS) -c -o $@ $<
\ No newline at end of file
diff --git a/Source/charon/transforms/rsa/rsa_private_key.c b/Source/charon/transforms/rsa/rsa_private_key.c
new file mode 100644 (file)
index 0000000..cc153f7
--- /dev/null
@@ -0,0 +1,530 @@
+/**
+ * @file rsa_private_key.c
+ * 
+ * @brief Implementation of rsa_private_key_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 <gmp.h>
+
+#include "rsa_private_key.h"
+
+#include <daemon.h>
+#include <utils/allocator.h>
+
+
+/* oids for hash algorithms are defined in
+ * rsa_public_key.c
+ */
+extern u_int8_t md2_oid[18];
+extern u_int8_t md5_oid[18];
+extern u_int8_t sha1_oid[15];
+extern u_int8_t sha256_oid[19];
+extern u_int8_t sha384_oid[19];
+extern u_int8_t sha512_oid[19];
+
+/**
+ *  Public exponent to use for key generation
+ */
+#define PUBLIC_EXPONENT 0x10001
+
+
+typedef struct private_rsa_private_key_t private_rsa_private_key_t;
+
+/**
+ * private data structure for rsa_private_key.
+ */
+struct private_rsa_private_key_t {
+       /**
+        * Public interface for this signer.
+        */
+       rsa_private_key_t public;
+       
+       /**
+        * is the key already set ?
+        */
+       bool is_key_set;
+       
+       /**
+        * public modulus
+        */
+       mpz_t n;
+       
+       /**
+        * public exponent
+        */
+       mpz_t e;
+       
+       /**
+        * private Prime 1
+        */
+       mpz_t p;
+       
+       /**
+        * private Prime 2
+        */
+       mpz_t q;
+       
+       /**
+        * private exponent
+        */
+       mpz_t d;
+       
+       /**
+        * private exponent 1
+        */
+       mpz_t exp1;
+       
+       /**
+        * private exponent 2
+        */
+       mpz_t exp2;
+       
+       /**
+        * private coefficient
+        */
+       mpz_t coeff;
+       
+       /**
+        * keysize in bytes
+        */
+       size_t k;
+       
+       /**
+        * @brief Implements the RSADP algorithm specified in PKCS#1.
+        */
+       chunk_t (*rsadp) (private_rsa_private_key_t *this, chunk_t data);
+               
+       /**
+        * @brief Implements the RSASP1 algorithm specified in PKCS#1.
+        */
+       chunk_t (*rsasp1) (private_rsa_private_key_t *this, chunk_t data);
+       
+};
+
+/**
+ * Implements private_rsa_private_key_t.rsadp
+ * Implements private_rsa_private_key_t.rsasp1
+ */
+static chunk_t rsadp(private_rsa_private_key_t *this, chunk_t data)
+{
+       mpz_t t1, t2;
+       chunk_t decrypted;
+       
+    mpz_init(t1);
+    mpz_init(t2);
+    
+    mpz_import(t1, data.len, 1, 1, 1, 0, data.ptr);
+    
+    mpz_powm(t2, t1, this->exp1, this->p);     /* m1 = c^dP mod p */
+    mpz_powm(t1, t1, this->exp2, this->q);     /* m2 = c^dQ mod Q */
+    mpz_sub(t2, t2, t1);                                       /* h = qInv (m1 - m2) mod p */
+    mpz_mod(t2, t2, this->p);
+    mpz_mul(t2, t2, this->coeff);
+    mpz_mod(t2, t2, this->p);
+
+    mpz_mul(t2, t2, this->q);                          /* m = m2 + h q */
+    mpz_add(t1, t1, t2);
+
+    decrypted.len = this->k;
+    decrypted.ptr = mpz_export(NULL, NULL, 1, decrypted.len, 1, 0, t1);
+
+    mpz_clear(t1);
+    mpz_clear(t2);
+    
+    return decrypted;
+}
+
+/**
+ * implementation of rsa_private_key.build_emsa_signature.
+ */
+static status_t build_emsa_pkcs1_signature(private_rsa_private_key_t *this, hash_algorithm_t hash_algorithm, chunk_t data, chunk_t *signature)
+{
+       hasher_t *hasher;
+       chunk_t hash;
+       chunk_t oid;
+       chunk_t em;
+       
+       /* get oid string prepended to hash */
+       switch (hash_algorithm)
+       {       
+               case HASH_MD2:
+               {
+                       oid.ptr = md2_oid;
+                       oid.len = sizeof(md2_oid);
+                       break;
+               }
+               case HASH_MD5:
+               {
+                       oid.ptr = md5_oid;
+                       oid.len = sizeof(md5_oid);
+                       break;
+               }
+               case HASH_SHA1:
+               {
+                       oid.ptr = sha1_oid;
+                       oid.len = sizeof(sha1_oid);
+                       break;
+               }
+               case HASH_SHA256:
+               {
+                       oid.ptr = sha256_oid;
+                       oid.len = sizeof(sha256_oid);
+                       break;
+               }
+               case HASH_SHA384:
+               {
+                       oid.ptr = sha384_oid;
+                       oid.len = sizeof(sha384_oid);
+                       break;
+               }
+               case HASH_SHA512:
+               {
+                       oid.ptr = sha512_oid;
+                       oid.len = sizeof(sha512_oid);
+                       break;
+               }
+               default:
+               {
+                       return NOT_SUPPORTED;   
+               }
+       }
+       
+       /* get hasher */
+       hasher = hasher_create(hash_algorithm);
+       if (hasher == NULL)
+       {
+               return NOT_SUPPORTED;   
+       }
+       
+       /* build hash */
+       hasher->allocate_hash(hasher, data, &hash);
+       hasher->destroy(hasher);
+       
+       /* build chunk to rsa-decrypt:
+        * EM = 0x00 || 0x01 || PS || 0x00 || T. 
+        * PS = 0xFF padding, with length to fill em
+        * T = oid || hash
+        */
+       em.len = this->k;
+       em.ptr = allocator_alloc(em.len);
+       
+       /* fill em with padding */
+       memset(em.ptr, 0xFF, em.len);
+       /* set magic bytes */
+       *(em.ptr) = 0x00;
+       *(em.ptr+1) = 0x01;
+       *(em.ptr + em.len - hash.len - oid.len - 1) = 0x00;
+       /* set hash */
+       memcpy(em.ptr + em.len - hash.len, hash.ptr, hash.len);
+       /* set oid */
+       memcpy(em.ptr + em.len - hash.len - oid.len, oid.ptr, oid.len);
+
+       
+       /* build signature */
+       *signature = this->rsasp1(this, em);
+       
+       allocator_free(hash.ptr);
+       allocator_free(em.ptr);
+       
+       return SUCCESS; 
+}
+
+       
+/**
+ * implementation of rsa_private_key.set_key.
+ */
+static status_t set_key(private_rsa_private_key_t *this, chunk_t key)
+{
+       chunk_t n, e, p, q, d, exp1, exp2, coeff;
+       this->k = key.len / 8;
+       
+       n.len = this->k;
+       e.len = this->k;
+       p.len = this->k;
+       q.len = this->k;
+       d.len = this->k;
+       exp1.len = this->k;
+       exp2.len = this->k;
+       coeff.len = this->k;
+       
+       n.ptr = key.ptr + this->k * 0;
+       e.ptr = key.ptr + this->k * 1;
+       p.ptr = key.ptr + this->k * 2;
+       q.ptr = key.ptr + this->k * 3;
+       d.ptr = key.ptr + this->k * 4;
+       exp1.ptr = key.ptr + this->k * 5;
+       exp2.ptr = key.ptr + this->k * 6;
+       coeff.ptr = key.ptr + this->k * 7;
+       
+       mpz_import(this->n, this->k, 1, 1, 1, 0, n.ptr);
+       mpz_import(this->e, this->k, 1, 1, 1, 0, e.ptr);
+       mpz_import(this->p, this->k, 1, 1, 1, 0, p.ptr);
+       mpz_import(this->q, this->k, 1, 1, 1, 0, q.ptr);
+       mpz_import(this->d, this->k, 1, 1, 1, 0, d.ptr);
+       mpz_import(this->exp1, this->k, 1, 1, 1, 0, exp1.ptr);
+       mpz_import(this->exp2, this->k, 1, 1, 1, 0, exp2.ptr);
+       mpz_import(this->coeff, this->k, 1, 1, 1, 0, coeff.ptr);
+       
+       this->is_key_set = TRUE;
+       
+       return SUCCESS;
+
+}
+       
+/**
+ * implementation of rsa_private_key.get_key.
+ */
+static status_t get_key(private_rsa_private_key_t *this, chunk_t *key)
+{
+       if (!this->is_key_set)
+       {
+               return INVALID_STATE;   
+       }
+       
+       chunk_t n, e, p, q, d, exp1, exp2, coeff;
+
+       n.len = this->k;
+       n.ptr = mpz_export(NULL, NULL, 1, n.len, 1, 0, this->n);
+       e.len = this->k;
+       e.ptr = mpz_export(NULL, NULL, 1, e.len, 1, 0, this->e);
+       p.len = this->k;
+       p.ptr = mpz_export(NULL, NULL, 1, p.len, 1, 0, this->p);
+       q.len = this->k;
+       q.ptr = mpz_export(NULL, NULL, 1, q.len, 1, 0, this->q);
+       d.len = this->k;
+       d.ptr = mpz_export(NULL, NULL, 1, d.len, 1, 0, this->d);
+       exp1.len = this->k;
+       exp1.ptr = mpz_export(NULL, NULL, 1, exp1.len, 1, 0, this->exp1);
+       exp2.len = this->k;
+       exp2.ptr = mpz_export(NULL, NULL, 1, exp2.len, 1, 0, this->exp2);
+       coeff.len = this->k;
+       coeff.ptr = mpz_export(NULL, NULL, 1, coeff.len, 1, 0, this->coeff);
+       
+       key->len = this->k * 8;
+       key->ptr = allocator_alloc(key->len);
+       memcpy(key->ptr + this->k * 0, n.ptr , n.len);
+       memcpy(key->ptr + this->k * 1, e.ptr, e.len);
+       memcpy(key->ptr + this->k * 2, p.ptr, p.len);
+       memcpy(key->ptr + this->k * 3, q.ptr, q.len);
+       memcpy(key->ptr + this->k * 4, d.ptr, d.len);
+       memcpy(key->ptr + this->k * 5, exp1.ptr, exp1.len);
+       memcpy(key->ptr + this->k * 6, exp2.ptr, exp2.len);
+       memcpy(key->ptr + this->k * 7, coeff.ptr, coeff.len);
+       
+       allocator_free(n.ptr);
+       allocator_free(e.ptr);
+       allocator_free(p.ptr);
+       allocator_free(q.ptr);
+       allocator_free(d.ptr);
+       allocator_free(exp1.ptr);
+       allocator_free(exp2.ptr);
+       allocator_free(coeff.ptr);
+       
+       return SUCCESS;
+}
+       
+/**
+ * implementation of rsa_private_key.load_key.
+ */
+static status_t load_key(private_rsa_private_key_t *this, char *file)
+{
+       return NOT_SUPPORTED;
+}
+
+/**
+ * implementation of rsa_private_key.save_key.
+ */
+static status_t save_key(private_rsa_private_key_t *this, char *file)
+{
+       return NOT_SUPPORTED;
+}
+
+/**
+ * implementation of rsa_private_key.generate_key.
+ */
+static status_t generate_key(private_rsa_private_key_t *this, size_t key_size)
+{
+       mpz_t p, q, n, e, d, exp1, exp2, coeff;
+       mpz_t m, q1, t;
+       
+       if (key_size <= 0) 
+       {
+               return INVALID_ARG;
+       }
+       
+       if (this->is_key_set)
+       {
+               mpz_clear(this->n);
+               mpz_clear(this->e);
+               mpz_clear(this->p);
+               mpz_clear(this->q);
+               mpz_clear(this->d);
+               mpz_clear(this->exp1);
+               mpz_clear(this->exp2);
+               mpz_clear(this->coeff);
+       }
+       
+       key_size = key_size / 8;
+       
+       mpz_init(t);    
+       mpz_init(n);
+       mpz_init(d);
+       mpz_init(exp1);
+       mpz_init(exp2);
+       mpz_init(coeff);
+       
+       /* Get values of primes p and q  */
+       charon->prime_pool->get_prime(charon->prime_pool, key_size/2, &p);
+       charon->prime_pool->get_prime(charon->prime_pool, key_size/2, &q);
+
+       /* Swapping Primes so p is larger then q */
+       if (mpz_cmp(p, q) < 0)                                  
+       {
+               mpz_set(t, p);
+               mpz_set(p, q);
+               mpz_set(q, t);
+       }
+       
+       mpz_mul(n, p, q);                                               /* n = p*q */
+       mpz_init_set_ui(e, PUBLIC_EXPONENT);    /* assign public exponent */
+       mpz_init_set(m, p);                                     /* m = p */
+       mpz_sub_ui(m, m, 1);                                    /* m = m -1 */
+       mpz_init_set(q1, q);                                    /* q1 = q */
+       mpz_sub_ui(q1, q1, 1);                                  /* q1 = q1 -1 */
+       mpz_gcd(t, m, q1);                                              /* t = gcd(p-1, q-1) */
+       mpz_mul(m, m, q1);                                              /* m = (p-1)*(q-1) */
+       mpz_divexact(m, m, t);                                  /* m = m / t */
+       mpz_gcd(t, m, e);                                               /* t = gcd(m, e) (greatest common divisor) */
+
+       mpz_invert(d, e, m);                                    /* e has an inverse mod m */
+       if (mpz_cmp_ui(d, 0) < 0)                               /* make sure d is positive */
+       {
+               mpz_add(d, d, m);
+       }
+       mpz_sub_ui(t, p, 1);                                    /* t = p-1 */
+       mpz_mod(exp1, d, t);                                    /* exp1 = d mod p-1 */
+       mpz_sub_ui(t, q, 1);                                    /* t = q-1 */
+       mpz_mod(exp2, d, t);                                    /* exp2 = d mod q-1 */
+       
+       mpz_invert(coeff, q, p);                                /* coeff = q^-1 mod p */
+       if (mpz_cmp_ui(coeff, 0) < 0)                   /* make coeff d is positive */
+       {
+               mpz_add(coeff, coeff, p);
+       }
+
+       mpz_clear(q1);
+       mpz_clear(m);
+       mpz_clear(t);
+
+       /* apply values */
+       *(this->p) = *p;                                
+       *(this->q) = *q;
+       *(this->n) = *n;
+       *(this->e) = *e;
+       *(this->d) = *d;
+       *(this->exp1) = *exp1;
+       *(this->exp2) = *exp2;
+       *(this->coeff) = *coeff;
+       
+       /* set key size in bytes */
+       
+       this->is_key_set = TRUE;
+       this->k = key_size;
+       
+       return SUCCESS;
+}
+
+
+rsa_public_key_t *get_public_key(private_rsa_private_key_t *this)
+{
+       rsa_public_key_t *public_key;
+       //chunk_t key;
+       
+       public_key = rsa_public_key_create();
+       
+       if (this->is_key_set)
+       {       
+       
+               chunk_t n, e, key;
+
+               n.len = this->k;
+               n.ptr = mpz_export(NULL, NULL, 1, n.len, 1, 0, this->n);
+               e.len = this->k;
+               e.ptr = mpz_export(NULL, NULL, 1, e.len, 1, 0, this->e);
+               
+               key.len = this->k * 2;
+               key.ptr = allocator_alloc(key.len);
+               memcpy(key.ptr, n.ptr, n.len);
+               memcpy(key.ptr + n.len, e.ptr, e.len);
+               allocator_free(n.ptr);
+               allocator_free(e.ptr);
+               
+               public_key->set_key(public_key, key);
+               allocator_free(key.ptr);
+
+       }
+       
+       return public_key;
+}
+
+
+/**
+ * implementation of rsa_private_key.destroy.
+ */
+static void destroy(private_rsa_private_key_t *this)
+{
+       if (this->is_key_set)
+       {
+               mpz_clear(this->n);
+               mpz_clear(this->e);
+               mpz_clear(this->p);
+               mpz_clear(this->q);
+               mpz_clear(this->d);
+               mpz_clear(this->exp1);
+               mpz_clear(this->exp2);
+               mpz_clear(this->coeff);
+       }
+       allocator_free(this);
+}
+
+/*
+ * Described in header
+ */
+rsa_private_key_t *rsa_private_key_create(hash_algorithm_t hash_algoritm)
+{
+       private_rsa_private_key_t *this = allocator_alloc_thing(private_rsa_private_key_t);
+       
+       /* public functions */
+       this->public.build_emsa_pkcs1_signature = (status_t (*) (rsa_private_key_t*,hash_algorithm_t,chunk_t,chunk_t*))build_emsa_pkcs1_signature;
+       this->public.set_key = (status_t (*) (rsa_private_key_t*,chunk_t))set_key;
+       this->public.get_key = (status_t (*) (rsa_private_key_t*,chunk_t*))get_key;
+       this->public.load_key = (status_t (*) (rsa_private_key_t*,char*))load_key;
+       this->public.save_key = (status_t (*) (rsa_private_key_t*,char*))save_key;
+       this->public.generate_key = (status_t (*) (rsa_private_key_t*,size_t))generate_key;
+       this->public.get_public_key = (rsa_public_key_t *(*) (rsa_private_key_t*))get_public_key;
+       this->public.destroy = (void (*) (rsa_private_key_t*))destroy;
+       
+       /* private functions */
+       this->rsadp = rsadp;
+       this->rsasp1 = rsadp; /* same algorithm */
+       
+       this->is_key_set = FALSE;
+       
+       return &(this->public);
+}
diff --git a/Source/charon/transforms/rsa/rsa_private_key.h b/Source/charon/transforms/rsa/rsa_private_key.h
new file mode 100644 (file)
index 0000000..15f9033
--- /dev/null
@@ -0,0 +1,68 @@
+/**
+ * @file rsa_private_key.h
+ * 
+ * @brief Interface rsa_private_key_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 RSA_PRIVATE_KEY_H_
+#define RSA_PRIVATE_KEY_H_
+
+#include <types.h>
+#include <definitions.h>
+
+#include <transforms/rsa/rsa_public_key.h>
+#include <transforms/hashers/hasher.h>
+
+
+typedef struct rsa_private_key_t rsa_private_key_t;
+
+/**
+ * @brief RSA private key with associated functions.
+ * 
+ * Currently only supports signing using EMSA encoding.
+ * 
+ * @ingroup asymmetrics
+ */
+struct rsa_private_key_t {
+
+       status_t (*build_emsa_pkcs1_signature) (rsa_private_key_t *this, hash_algorithm_t hash_algorithm, chunk_t data, chunk_t *signature);
+       
+       status_t (*set_key) (rsa_private_key_t *this, chunk_t key);
+       
+       status_t (*get_key) (rsa_private_key_t *this, chunk_t *key);
+       
+       status_t (*load_key) (rsa_private_key_t *this, char *file);
+       
+       status_t (*save_key) (rsa_private_key_t *this, char *file);
+       
+       status_t (*generate_key) (rsa_private_key_t *this, size_t key_size);
+       
+       rsa_public_key_t *(*get_public_key) (rsa_private_key_t *this);
+
+       void (*destroy) (rsa_private_key_t *this);
+};
+
+/**
+ * Types are defined in public_key.h
+ * 
+ * @ingroup asymmetrics
+ */
+rsa_private_key_t *rsa_private_key_create();
+
+#endif /*RSA_PRIVATE_KEY_H_*/
diff --git a/Source/charon/transforms/rsa/rsa_public_key.c b/Source/charon/transforms/rsa/rsa_public_key.c
new file mode 100644 (file)
index 0000000..3856fc8
--- /dev/null
@@ -0,0 +1,364 @@
+/**
+ * @file rsa_public_key.c
+ * 
+ * @brief Implementation of rsa_public_key_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 <gmp.h>
+
+#include "rsa_public_key.h"
+
+#include <daemon.h>
+#include <utils/allocator.h>
+#include <transforms/hashers/hasher.h>
+
+/* since we don't have an ASN1 parser/generator,
+ * we use these predefined values for
+ * hash algorithm oids. These also contain
+ * the length of the following hash.
+ * These values are also used in rsa_private_key.c
+ */
+
+u_int8_t md2_oid[18] = {
+       0x30,0x20,0x30,0x0c,0x06,0x08,0x2a,0x86,
+       0x48,0x86,0xf7,0x0d,0x02,0x02,0x05,0x00,
+       0x04,0x10
+};
+
+u_int8_t md5_oid[] = {
+       0x30,0x20,0x30,0x0c,0x06,0x08,0x2a,0x86,
+       0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,
+       0x04,0x10
+};
+
+u_int8_t sha1_oid[] = {
+       0x30,0x21,0x30,0x09,0x06,0x05,0x2b,0x0e,
+       0x03,0x02,0x1a,0x05,0x00,0x04,0x14
+};
+
+u_int8_t sha256_oid[] = {
+       0x30,0x31,0x30,0x0d,0x06,0x09,0x60,0x86,
+       0x48,0x01,0x65,0x03,0x04,0x02,0x01,0x05,
+       0x00,0x04,0x20
+};
+
+u_int8_t sha384_oid[] = {
+       0x30,0x41,0x30,0x0d,0x06,0x09,0x60,0x86,
+       0x48,0x01,0x65,0x03,0x04,0x02,0x02,0x05,
+       0x00,0x04,0x30
+};
+
+u_int8_t sha512_oid[] = {
+       0x30,0x51,0x30,0x0d,0x06,0x09,0x60,0x86,
+       0x48,0x01,0x65,0x03,0x04,0x02,0x03,0x05,
+       0x00,0x04,0x40
+};
+
+
+typedef struct private_rsa_public_key_t private_rsa_public_key_t;
+
+/**
+ * private data structure with signing context.
+ */
+struct private_rsa_public_key_t {
+       /**
+        * Public interface for this signer.
+        */
+       rsa_public_key_t public;
+       
+       /**
+        * is the key already set ?
+        */
+       bool is_key_set;
+       
+       /**
+        * public modulus
+        */
+       mpz_t n;
+       /**
+        * public exponent
+        */
+       mpz_t e;
+       
+       /**
+        * keysize in bytes
+        */
+       size_t k;
+       
+       /**
+        * @brief Implements the RSAEP algorithm specified in PKCS#1.
+        */
+       chunk_t (*rsaep) (private_rsa_public_key_t *this, chunk_t data);
+               
+       /**
+        * @brief Implements the RSASVP1 algorithm specified in PKCS#1.
+        */
+       chunk_t (*rsavp1) (private_rsa_public_key_t *this, chunk_t data);
+};
+
+/**
+ * Implements private_rsa_public_key_t.rsadp
+ * Implements private_rsa_public_key_t.rsavp1
+ */
+static chunk_t rsaep(private_rsa_public_key_t *this, chunk_t data)
+{
+       mpz_t m, c;
+       chunk_t encrypted;
+       
+       mpz_init(c);
+       mpz_init(m);
+       
+       mpz_import(m, data.len, 1, 1, 1, 0, data.ptr);
+       
+       mpz_powm(c, m, this->e, this->n);
+
+    encrypted.len = this->k;
+    encrypted.ptr = mpz_export(NULL, NULL, 1, encrypted.len, 1, 0, c);
+       
+       mpz_clear(c);
+       mpz_clear(m);   
+       
+       return encrypted;
+}
+
+/**
+ * implementation of rsa_public_key.verify_emsa_signature.
+ */
+static status_t verify_emsa_pkcs1_signature(private_rsa_public_key_t *this, chunk_t data, chunk_t signature)
+{
+       hasher_t *hasher = NULL;
+       chunk_t hash;
+       chunk_t em;
+       u_int8_t *pos;
+       
+       if (signature.len > this->k)
+       {
+               return INVALID_ARG;     
+       }
+       
+       /* unpack signature */
+       em = this->rsavp1(this, signature);
+       
+       /* result should look like this:
+        * EM = 0x00 || 0x01 || PS || 0x00 || T. 
+        * PS = 0xFF padding, with length to fill em
+        * T = oid || hash
+        */     
+       
+       /* check magic bytes */
+       if ((*(em.ptr) != 0x00) ||
+               (*(em.ptr+1) != 0x01))
+       {
+               allocator_free(em.ptr);
+               return FAILED;
+       }
+       
+       /* find magic 0x00 */
+       pos = em.ptr + 2;
+       while (pos <= em.ptr + em.len)
+       {
+               if (*pos == 0x00)
+               {
+                       /* found magic byte, stop */
+                       pos++;
+                       break;
+               }
+               else if (*pos != 0xFF)
+               {
+                       /* bad padding, decryption failed ?!*/
+                       allocator_free(em.ptr);
+                       return FAILED;  
+               }
+               pos++;
+       }
+
+       if (pos + 20 > em.ptr + em.len)
+       {
+               /* not enought room for oid compare */
+               allocator_free(em.ptr);
+               return FAILED;  
+       }
+       
+       if (memcmp(md2_oid, pos, sizeof(md2_oid)) == 0)
+       {
+               hasher = hasher_create(HASH_MD2);
+               pos += sizeof(md2_oid);
+       }
+       else if (memcmp(md5_oid, pos, sizeof(md5_oid)) == 0)
+       {
+               hasher = hasher_create(HASH_MD5);
+               pos += sizeof(md5_oid);
+       }
+       else if (memcmp(sha1_oid, pos, sizeof(sha1_oid)) == 0)
+       {
+               hasher = hasher_create(HASH_SHA1);
+               pos += sizeof(sha1_oid);
+       }
+       else if (memcmp(sha256_oid, pos, sizeof(sha256_oid)) == 0)
+       {
+               hasher = hasher_create(HASH_SHA256);
+               pos += sizeof(sha256_oid);
+       }
+       else if (memcmp(sha384_oid, pos, sizeof(sha384_oid)) == 0)
+       {
+               hasher = hasher_create(HASH_SHA384);
+               pos += sizeof(sha384_oid);
+       }
+       else if (memcmp(sha512_oid, pos, sizeof(sha512_oid)) == 0)
+       {
+               hasher = hasher_create(HASH_SHA512);
+               pos += sizeof(sha512_oid);
+       }
+       
+       if (hasher == NULL)
+       {
+               /* not supported hash algorithm */
+               allocator_free(em.ptr);
+               return NOT_SUPPORTED;   
+       }
+       
+       if (pos + hasher->get_block_size(hasher) != em.ptr + em.len)
+       {
+               /* bad length */
+               allocator_free(em.ptr);
+               hasher->destroy(hasher);
+               return FAILED;  
+       }
+       
+       /* build own hash for a compare */
+       hasher->allocate_hash(hasher, data, &hash);
+       hasher->destroy(hasher);
+       
+       if (memcmp(hash.ptr, pos, hash.len) != 0)
+       {
+               /* hash does not equal */
+               allocator_free(hash.ptr);
+               allocator_free(em.ptr);
+               return FAILED;  
+                       
+       }
+       
+       /* seems good */
+       allocator_free(hash.ptr);
+       allocator_free(em.ptr);
+       return SUCCESS; 
+}
+       
+/**
+ * implementation of rsa_public_key.set_key.
+ */
+static status_t set_key(private_rsa_public_key_t *this, chunk_t key)
+{
+       chunk_t n, e;
+       
+       n.len = key.len/2;
+       n.ptr = key.ptr;
+       e.len = n.len;
+       e.ptr = key.ptr + n.len;
+       
+       mpz_import(this->n, n.len, 1, 1, 1, 0, n.ptr);
+       mpz_import(this->e, n.len, 1, 1, 1, 0, e.ptr);
+       
+       this->k = n.len;
+       
+       this->is_key_set = TRUE;
+       
+       return SUCCESS;
+}      
+
+       
+/**
+ * implementation of rsa_public_key.get_key.
+ */
+static status_t get_key(private_rsa_public_key_t *this, chunk_t *key)
+{
+       if (!this->is_key_set)
+       {
+               return INVALID_STATE;   
+       }
+       
+       chunk_t n, e;
+
+       n.len = this->k;
+       n.ptr = mpz_export(NULL, NULL, 1, n.len, 1, 0, this->n);
+       e.len = this->k;
+       e.ptr = mpz_export(NULL, NULL, 1, e.len, 1, 0, this->e);
+       
+       key->len = this->k * 2;
+       key->ptr = allocator_alloc(key->len);
+       memcpy(key->ptr, n.ptr, n.len);
+       memcpy(key->ptr + n.len, e.ptr, e.len);
+       allocator_free(n.ptr);
+       allocator_free(e.ptr);
+       
+       return SUCCESS;
+}
+       
+/**
+ * implementation of rsa_public_key.load_key.
+ */
+static status_t load_key(private_rsa_public_key_t *this, char *file)
+{
+       return NOT_SUPPORTED;
+}
+
+/**
+ * implementation of rsa_public_key.save_key.
+ */
+static status_t save_key(private_rsa_public_key_t *this, char *file)
+{
+       return NOT_SUPPORTED;
+}
+
+/**
+ * implementation of rsa_public_key.destroy.
+ */
+static void destroy(private_rsa_public_key_t *this)
+{
+       if (this->is_key_set)
+       {
+               mpz_clear(this->n);
+               mpz_clear(this->e);
+       }
+       allocator_free(this);
+}
+
+/*
+ * Described in header
+ */
+rsa_public_key_t *rsa_public_key_create()
+{
+       private_rsa_public_key_t *this = allocator_alloc_thing(private_rsa_public_key_t);
+       
+       /* public functions */
+       this->public.verify_emsa_pkcs1_signature = (status_t (*) (rsa_public_key_t*,chunk_t,chunk_t))verify_emsa_pkcs1_signature;
+       this->public.set_key = (status_t (*) (rsa_public_key_t*,chunk_t))set_key;
+       this->public.get_key = (status_t (*) (rsa_public_key_t*,chunk_t*))get_key;
+       this->public.load_key = (status_t (*) (rsa_public_key_t*,char*))load_key;
+       this->public.save_key = (status_t (*) (rsa_public_key_t*,char*))save_key;
+       this->public.destroy = (void (*) (rsa_public_key_t*))destroy;
+       
+       /* private functions */
+       this->rsaep = rsaep;
+       this->rsavp1 = rsaep; /* same algorithm */
+       
+       this->is_key_set = FALSE;
+       
+       return &(this->public);
+}
diff --git a/Source/charon/transforms/rsa/rsa_public_key.h b/Source/charon/transforms/rsa/rsa_public_key.h
new file mode 100644 (file)
index 0000000..10343f5
--- /dev/null
@@ -0,0 +1,63 @@
+/**
+ * @file rsa_public_key.h
+ * 
+ * @brief Interface rsa_public_key_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 RSA_PUBLIC_KEY_H_
+#define RSA_PUBLIC_KEY_H_
+
+#include <gmp.h>
+
+#include <types.h>
+#include <definitions.h>
+
+
+typedef struct rsa_public_key_t rsa_public_key_t;
+
+/**
+ * @brief RSA public key with associated functions.
+ * 
+ * Currently only supports signature verification using
+ * the EMSA encoding (see PKCS1)
+ * 
+ * @ingroup asymmetrics
+ */
+struct rsa_public_key_t {
+
+       status_t (*verify_emsa_pkcs1_signature) (rsa_public_key_t *this, chunk_t data, chunk_t signature);
+       
+       status_t (*set_key) (rsa_public_key_t *this, chunk_t key);
+       
+       status_t (*get_key) (rsa_public_key_t *this, chunk_t *key);
+       
+       status_t (*load_key) (rsa_public_key_t *this, char *file);
+       
+       status_t (*save_key) (rsa_public_key_t *this, char *file);
+
+       void (*destroy) (rsa_public_key_t *this);
+};
+
+/**
+ * 
+ * @ingroup asymmetrics
+ */
+rsa_public_key_t *rsa_public_key_create();
+
+#endif /*RSA_PUBLIC_KEY_H_*/
index b9c0067..01e3bab 100644 (file)
@@ -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 $@ $<
index b22dec0..0d936a4 100644 (file)
@@ -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 */
 
 
index 7ea0fb6..a2f3292 100644 (file)
                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.
         * 
         * 
         * @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 (file)
index 3305012..0000000
+++ /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 (file)
index 3b274cf..0000000
+++ /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_*/