chunk: Add predictable hash function
[strongswan.git] / src / libstrongswan / utils / chunk.c
index 61c7bd5..04f3eea 100644 (file)
@@ -706,6 +706,12 @@ u_int64_t chunk_mac(chunk_t chunk, u_char *key)
 static u_char key[16];
 
 /**
+ * Static key used in case predictable hash values are required.
+ */
+static u_char static_key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+                                                         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
+
+/**
  * Only allocate the key once
  */
 static pthread_once_t key_allocated = PTHREAD_ONCE_INIT;
@@ -767,6 +773,22 @@ u_int32_t chunk_hash(chunk_t chunk)
 /**
  * Described in header.
  */
+u_int32_t chunk_hash_static_inc(chunk_t chunk, u_int32_t hash)
+{      /* we could use a mac of the previous hash, but this is faster */
+       return chunk_mac_inc(chunk, static_key, ((u_int64_t)hash) << 32 | hash);
+}
+
+/**
+ * Described in header.
+ */
+u_int32_t chunk_hash_static(chunk_t chunk)
+{
+       return chunk_mac(chunk, static_key);
+}
+
+/**
+ * Described in header.
+ */
 int chunk_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
                                          const void *const *args)
 {