updates.
[silc.git] / lib / silccrypt / silchmac.c
index 1a00970e7f720f11dc8b386ef2f4353b09b209d0..dfde14da034cb6333c9da80c2863d9c6cf0759c5 100644 (file)
@@ -27,18 +27,20 @@ struct SilcHmacStruct {
   bool allocated_hash;         /* TRUE if the hash was allocated */
 
   unsigned char *key;
-  uint32 key_len;
+  SilcUInt32 key_len;
 
   unsigned char inner_pad[64];
   unsigned char outer_pad[64];
   void *hash_context;
 };
 
+#ifndef SILC_EPOC
 /* List of dynamically registered HMACs. */
 SilcDList silc_hmac_list = NULL;
+#endif /* SILC_EPOC */
 
 /* Default hmacs for silc_hmac_register_default(). */
-SilcHmacObject silc_default_hmacs[] =
+const SilcHmacObject silc_default_hmacs[] =
 {
   { "hmac-sha1-96", 12 },
   { "hmac-md5-96", 12 },
@@ -49,7 +51,7 @@ SilcHmacObject silc_default_hmacs[] =
 };
 
 static void silc_hmac_init_internal(SilcHmac hmac, unsigned char *key,
-                                   uint32 key_len)
+                                   SilcUInt32 key_len)
 {
   SilcHash hash = hmac->hash;
   unsigned char hvalue[20];
@@ -80,8 +82,9 @@ static void silc_hmac_init_internal(SilcHmac hmac, unsigned char *key,
 /* Registers a new HMAC into the SILC. This function is used at the
    initialization of the SILC. */
 
-bool silc_hmac_register(SilcHmacObject *hmac)
+bool silc_hmac_register(const SilcHmacObject *hmac)
 {
+#ifndef SILC_EPOC
   SilcHmacObject *new;
 
   SILC_LOG_DEBUG(("Registering new HMAC `%s'", hmac->name));
@@ -105,6 +108,7 @@ bool silc_hmac_register(SilcHmacObject *hmac)
     silc_hmac_list = silc_dlist_init();
   silc_dlist_add(silc_hmac_list, new);
 
+#endif /* SILC_EPOC */
   return TRUE;
 }
 
@@ -112,6 +116,7 @@ bool silc_hmac_register(SilcHmacObject *hmac)
 
 bool silc_hmac_unregister(SilcHmacObject *hmac)
 {
+#ifndef SILC_EPOC
   SilcHmacObject *entry;
 
   SILC_LOG_DEBUG(("Unregistering HMAC"));
@@ -133,6 +138,7 @@ bool silc_hmac_unregister(SilcHmacObject *hmac)
     }
   }
 
+#endif /* SILC_EPOC */
   return FALSE;
 }
 
@@ -142,11 +148,13 @@ bool silc_hmac_unregister(SilcHmacObject *hmac)
 
 bool silc_hmac_register_default(void)
 {
+#ifndef SILC_EPOC
   int i;
 
   for (i = 0; silc_default_hmacs[i].name; i++)
     silc_hmac_register(&(silc_default_hmacs[i]));
 
+#endif /* SILC_EPOC */
   return TRUE;
 }
 
@@ -157,8 +165,6 @@ bool silc_hmac_register_default(void)
 
 bool silc_hmac_alloc(char *name, SilcHash hash, SilcHmac *new_hmac)
 {
-  SilcHmacObject *entry;
-
   SILC_LOG_DEBUG(("Allocating new HMAC"));
 
   /* Allocate the new object */
@@ -186,7 +192,9 @@ bool silc_hmac_alloc(char *name, SilcHash hash, SilcHmac *new_hmac)
 
   (*new_hmac)->hash = hash;
 
+#ifndef SILC_EPOC
   if (silc_hmac_list) {
+    SilcHmacObject *entry;
     silc_dlist_start(silc_hmac_list);
     while ((entry = silc_dlist_get(silc_hmac_list)) != SILC_LIST_END) {
       if (!strcmp(entry->name, name)) {
@@ -195,6 +203,18 @@ bool silc_hmac_alloc(char *name, SilcHash hash, SilcHmac *new_hmac)
       }
     }
   }
+#else
+  {
+    /* On EPOC which don't have globals we check our constant hash list. */
+    int i;
+    for (i = 0; silc_default_hmacs[i].name; i++) {
+      if (!strcmp(silc_default_hmacs[i].name, name)) {
+       (*new_hmac)->hmac = (SilcHmacObject *)&(silc_default_hmacs[i]);
+       return TRUE;
+      }
+    }
+  }
+#endif /* SILC_EPOC */
 
   silc_free(*new_hmac);
   *new_hmac = NULL;
@@ -221,7 +241,7 @@ void silc_hmac_free(SilcHmac hmac)
 
 /* Returns the length of the MAC that the HMAC will produce. */
 
-uint32 silc_hmac_len(SilcHmac hmac)
+SilcUInt32 silc_hmac_len(SilcHmac hmac)
 {
   return hmac->hmac->len;
 }
@@ -244,6 +264,7 @@ const char *silc_hmac_get_name(SilcHmac hmac)
 
 bool silc_hmac_is_supported(const char *name)
 {
+#ifndef SILC_EPOC
   SilcHmacObject *entry;
 
   if (!name)
@@ -256,7 +277,14 @@ bool silc_hmac_is_supported(const char *name)
        return TRUE;
     }
   }
-
+#else
+  {
+    int i;
+    for (i = 0; silc_default_hmacs[i].name; i++)
+      if (!strcmp(silc_default_hmacs[i].name, name))
+       return TRUE;
+  }
+#endif /* SILC_EPOC */
   return FALSE;
 }
 
@@ -266,9 +294,9 @@ char *silc_hmac_get_supported()
 {
   SilcHmacObject *entry;
   char *list = NULL;
-  int len;
+  int len = 0;
 
-  len = 0;
+#ifndef SILC_EPOC
   if (silc_hmac_list) {
     silc_dlist_start(silc_hmac_list);
     while ((entry = silc_dlist_get(silc_hmac_list)) != SILC_LIST_END) {
@@ -280,8 +308,24 @@ char *silc_hmac_get_supported()
       memcpy(list + len, ",", 1);
       len++;
     }
-    list[len - 1] = 0;
   }
+#else
+  {
+    int i;
+    for (i = 0; silc_default_hmacs[i].name; i++) {
+      entry = (SilcHmacObject *)&(silc_default_hmacs[i]);
+      len += strlen(entry->name);
+      list = silc_realloc(list, len + 1);
+      
+      memcpy(list + (len - strlen(entry->name)), 
+            entry->name, strlen(entry->name));
+      memcpy(list + len, ",", 1);
+      len++;
+    }
+  }
+#endif /* SILC_EPOC */
+
+  list[len - 1] = 0;
 
   return list;
 }
@@ -289,7 +333,7 @@ char *silc_hmac_get_supported()
 /* Sets the HMAC key used in the HMAC creation */
 
 void silc_hmac_set_key(SilcHmac hmac, const unsigned char *key,
-                      uint32 key_len)
+                      SilcUInt32 key_len)
 {
   if (hmac->key) {
     memset(hmac->key, 0, hmac->key_len);
@@ -304,8 +348,8 @@ void silc_hmac_set_key(SilcHmac hmac, const unsigned char *key,
    uses the internal key set with silc_hmac_set_key. */
 
 void silc_hmac_make(SilcHmac hmac, unsigned char *data,
-                   uint32 data_len, unsigned char *return_hash,
-                   uint32 *return_len)
+                   SilcUInt32 data_len, unsigned char *return_hash,
+                   SilcUInt32 *return_len)
 {
   SILC_LOG_DEBUG(("Making HMAC for message"));
 
@@ -318,10 +362,10 @@ void silc_hmac_make(SilcHmac hmac, unsigned char *data,
    key. The key is sent as argument to the function. */
 
 void silc_hmac_make_with_key(SilcHmac hmac, unsigned char *data,
-                            uint32 data_len, 
-                            unsigned char *key, uint32 key_len,
+                            SilcUInt32 data_len, 
+                            unsigned char *key, SilcUInt32 key_len,
                             unsigned char *return_hash,
-                            uint32 *return_len)
+                            SilcUInt32 *return_len)
 {
   SILC_LOG_DEBUG(("Making HMAC for message"));
 
@@ -336,8 +380,8 @@ void silc_hmac_make_with_key(SilcHmac hmac, unsigned char *data,
    routine allows these dangerous truncations. */
 
 void silc_hmac_make_truncated(SilcHmac hmac, unsigned char *data,
-                             uint32 data_len,
-                             uint32 truncated_len,
+                             SilcUInt32 data_len,
+                             SilcUInt32 truncated_len,
                              unsigned char *return_hash)
 {
   unsigned char hvalue[20];
@@ -361,7 +405,7 @@ void silc_hmac_init(SilcHmac hmac)
 /* Same as above but with specific key */
 
 void silc_hmac_init_with_key(SilcHmac hmac, const unsigned char *key,
-                            uint32 key_len)
+                            SilcUInt32 key_len)
 {
   SilcHash hash = hmac->hash;
 
@@ -378,7 +422,7 @@ void silc_hmac_init_with_key(SilcHmac hmac, const unsigned char *key,
 /* Add data to be used in the MAC computation. */
 
 void silc_hmac_update(SilcHmac hmac, const unsigned char *data,
-                     uint32 data_len)
+                     SilcUInt32 data_len)
 {
   SilcHash hash = hmac->hash;
   hash->hash->update(hmac->hash_context, (unsigned char *)data, data_len);
@@ -387,7 +431,7 @@ void silc_hmac_update(SilcHmac hmac, const unsigned char *data,
 /* Compute the final MAC. */
 
 void silc_hmac_final(SilcHmac hmac, unsigned char *return_hash,
-                    uint32 *return_len)
+                    SilcUInt32 *return_len)
 {
   SilcHash hash = hmac->hash;
   unsigned char mac[20];