summaryrefslogtreecommitdiff
path: root/cesar/cp/secu
diff options
context:
space:
mode:
authordufour2009-03-24 14:30:11 +0000
committerdufour2009-03-24 14:30:11 +0000
commit7c1747ba1aeeadf30e44b8c1585c8f0ed6f0dcc2 (patch)
tree80d8393796e29102012e81095907a04d98aa6c12 /cesar/cp/secu
parent4a720b13cec65c0602dd304959af64b39f1179dc (diff)
* cp/secu (closes #296):
- add function to generate hash of variable length. git-svn-id: svn+ssh://pessac/svn/cesar/trunk@4293 017c9cb6-072f-447c-8318-d5b54f68fe89
Diffstat (limited to 'cesar/cp/secu')
-rw-r--r--cesar/cp/secu/secu.h9
-rw-r--r--cesar/cp/secu/src/secu.c24
-rw-r--r--cesar/cp/secu/test/host-Makefile5
-rw-r--r--cesar/cp/secu/test/src/hash.c69
4 files changed, 106 insertions, 1 deletions
diff --git a/cesar/cp/secu/secu.h b/cesar/cp/secu/secu.h
index b13b3d219d..a7ba36d975 100644
--- a/cesar/cp/secu/secu.h
+++ b/cesar/cp/secu/secu.h
@@ -145,6 +145,15 @@ cp_secu_nmk2nid(cp_key_t nmk, u8 security_level);
void
cp_secu_tek_gen (const u32 left[], const u32 right[], cp_key_t *output);
+/**
+ * Generate some hash.
+ * \param seed a random seed.
+ * \param hash a buffer for the generated hash data.
+ * \param hash_length hash buffer length (in bytes).
+ */
+void
+cp_secu_generate_hash (const u32 seed, u8 *hash, const uint hash_length);
+
END_DECLS
#endif /* secu__h__ */
diff --git a/cesar/cp/secu/src/secu.c b/cesar/cp/secu/src/secu.c
index e9b94a88f8..647b10126a 100644
--- a/cesar/cp/secu/src/secu.c
+++ b/cesar/cp/secu/src/secu.c
@@ -165,3 +165,27 @@ cp_secu_tek_gen (const u32 left[], const u32 right[], cp_key_t *output)
memcpy (output->key, output_buffer, 16);
}
+void
+cp_secu_generate_hash (const u32 seed, u8 *hash, const uint hash_length)
+{
+
+ uint compt = 0;
+ uint iteration = hash_length / CP_SECU_OUTPUT_KEY_SIZE;
+ /* For each CP_SECU_OUTPUT_KEY_SIZE of the hash. */
+ for (compt = 0; compt < iteration; compt++)
+ {
+ /* Generate hash. */
+ cp_secu_pbkdf1 ((u8 *) &seed, sizeof (seed),
+ (cp_key_t *) &hash[compt * CP_SECU_OUTPUT_KEY_SIZE],
+ CP_SECU_SALT_SPIDCOM);
+ }
+ /* Check if last part is not enought for CP_SECU_OUTPUT_KEY_SIZE. */
+ uint modulo = hash_length % CP_SECU_OUTPUT_KEY_SIZE;
+ if (modulo)
+ {
+ cp_key_t key;
+ cp_secu_pbkdf1 ((u8 *) &seed, sizeof (seed), &key, CP_SECU_SALT_SPIDCOM);
+ /* Only copy amout of data required. */
+ memcpy (&hash[iteration * CP_SECU_OUTPUT_KEY_SIZE], key.key, modulo);
+ }
+}
diff --git a/cesar/cp/secu/test/host-Makefile b/cesar/cp/secu/test/host-Makefile
index 4ac8c3750a..8a5e9a52f0 100644
--- a/cesar/cp/secu/test/host-Makefile
+++ b/cesar/cp/secu/test/host-Makefile
@@ -1,6 +1,6 @@
BASE = ../../..
-HOST_PROGRAMS = test-sha2 test-aes test-prun test-nmk
+HOST_PROGRAMS = test-sha2 test-aes test-prun test-nmk hash
test-sha2_SOURCES = test-sha2.c
test-sha2_MODULES = lib cp/secu
@@ -14,6 +14,9 @@ test-prun_MODULES = lib cp/secu
test-nmk_SOURCES = test-nmk.c
test-nmk_MODULES = lib cp/secu
+hash_SOURCES = hash.c
+hash_MODULES = lib cp/secu
+
VARIANT = host
include $(BASE)/common/make/top.mk
diff --git a/cesar/cp/secu/test/src/hash.c b/cesar/cp/secu/test/src/hash.c
new file mode 100644
index 0000000000..f6206abdf0
--- /dev/null
+++ b/cesar/cp/secu/test/src/hash.c
@@ -0,0 +1,69 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2009 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file cp/secu/test/src/hash.c
+ * \brief Test hash generation.
+ * \ingroup test
+ */
+#include "common/std.h"
+#include "cp/secu/secu.h"
+#include "lib/test.h"
+#include "lib/blk.h"
+
+void
+test_hash_suite (test_t t);
+
+void
+test_hash_suite (test_t t)
+{
+ const uint hash_length = 400;
+ const uint byte_equal_threshold = 10;
+ /* Start hash tests. */
+ test_suite_begin (t, "hash");
+
+ test_begin (t, "two hashes should be almost different")
+ {
+ u8 hash1[hash_length];
+ u8 hash2[hash_length];
+
+ /* Generate two hashes. */
+ cp_secu_generate_hash (1, hash1, hash_length);
+ cp_secu_generate_hash (2, hash2, hash_length);
+
+ /* Count how many bytes are equals in hashes. */
+ uint compt, equal;
+ for (compt = 0, equal = 0; compt < hash_length; compt++)
+ {
+ if (hash1[compt] == hash2[compt])
+ equal++;
+ }
+ /* Fail if too much similarity. */
+ test_fail_if (equal >= byte_equal_threshold);
+ } test_end;
+
+ /* Check memory leak. */
+ test_case_begin (t, "memory");
+ test_begin (t, "memory")
+ {
+ test_fail_unless (blk_check_memory ());
+ } test_end;
+}
+
+int
+main (int argc, char **argv)
+{
+ test_t t;
+ /* Initialize test system. */
+ test_init (t, argc, argv);
+
+ test_hash_suite (t);
+
+ /* Print results. */
+ test_result (t);
+ return test_nb_failed (t) == 0 ? 0 : 1;
+}