summaryrefslogtreecommitdiff
path: root/cesar/cp2
diff options
context:
space:
mode:
authorlaranjeiro2008-06-17 13:50:14 +0000
committerlaranjeiro2008-06-17 13:50:14 +0000
commit402be329044891a65347d0ff353d4c331a116a0d (patch)
treee912501631809c41812b8d633c742747c683dfb6 /cesar/cp2
parent6cd1ea58dd7032cdede4f314d7290f3737bcb832 (diff)
cp2/secu : testing the SHA256. (Still a bug in the SHA computation).
git-svn-id: svn+ssh://pessac/svn/cesar/trunk@2359 017c9cb6-072f-447c-8318-d5b54f68fe89
Diffstat (limited to 'cesar/cp2')
-rw-r--r--cesar/cp2/secu/Module2
-rw-r--r--cesar/cp2/secu/inc/sha256.h39
-rw-r--r--cesar/cp2/secu/secu.h2
-rw-r--r--cesar/cp2/secu/sha256.h26
-rw-r--r--cesar/cp2/secu/src/sha256.c156
-rw-r--r--cesar/cp2/secu/test/Makefile8
-rw-r--r--cesar/cp2/secu/test/src/test-sha2.c57
7 files changed, 288 insertions, 2 deletions
diff --git a/cesar/cp2/secu/Module b/cesar/cp2/secu/Module
index b7aa509ead..6107eb45d3 100644
--- a/cesar/cp2/secu/Module
+++ b/cesar/cp2/secu/Module
@@ -1 +1 @@
-SOURCES :=
+SOURCES := sha256.c
diff --git a/cesar/cp2/secu/inc/sha256.h b/cesar/cp2/secu/inc/sha256.h
new file mode 100644
index 0000000000..c15be95868
--- /dev/null
+++ b/cesar/cp2/secu/inc/sha256.h
@@ -0,0 +1,39 @@
+#ifndef cp2_secu_inc_sha256_h
+#define cp2_secu_inc_sha256_h
+/* Cesar project {{{
+ *
+ * Copyright (C) 2008 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file cp2/secu/inc/sha256.h
+ * \brief SHA 256 Private functions.
+ * \ingroup cp2_secu
+ */
+
+struct sha2_t
+{
+ /*!< intermediate digest state */
+ u32 state[8];
+};
+typedef struct sha2_t sha2_t;
+
+/** Initialise the SHA context.
+ *
+ * \param ctx the module context.
+ */
+void
+cp_secu_sha2_init (sha2_t *ctx);
+
+/**
+ * Process the Hash
+ * \param ctx the module context.
+ * \param data the data to hash.
+ * \param length the length in bytes.
+ */
+void
+cp_secu_sha2_process (sha2_t *ctx, u8 *data, uint length);
+
+#endif /* cp2_secu_inc_sha256_h */
diff --git a/cesar/cp2/secu/secu.h b/cesar/cp2/secu/secu.h
index be10bf21f0..94a001ce5c 100644
--- a/cesar/cp2/secu/secu.h
+++ b/cesar/cp2/secu/secu.h
@@ -9,7 +9,7 @@
* }}} */
/**
* \file Cesare/cp/secu/secu.h
- * \brief library of security related functions (crypto, ...)
+ * \brief library of security related functions (crypto, ...)
* \ingroup cp/secu
*
* « long description »
diff --git a/cesar/cp2/secu/sha256.h b/cesar/cp2/secu/sha256.h
new file mode 100644
index 0000000000..056fb75fc3
--- /dev/null
+++ b/cesar/cp2/secu/sha256.h
@@ -0,0 +1,26 @@
+#ifndef cp2_secu_sha256_h
+#define cp2_secu_sha256_h
+/* Cesar project {{{
+ *
+ * Copyright (C) 2008 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file cp2/secu/sha256.h
+ * \brief SHA 256.
+ * \ingroup cp2_secu
+ *
+ * see http://en.wikipedia.org/wiki/SHA256#SHA-2
+ */
+
+/** SHA 256
+ * \param data the buffer containing the data to hash.
+ * \param length the length of the data.
+ * \return the buffer with the hashed value.
+ */
+void
+cp_secu_sha256 (u8 *data, uint length);
+
+#endif /* cp2_secu_sha256_h */
diff --git a/cesar/cp2/secu/src/sha256.c b/cesar/cp2/secu/src/sha256.c
new file mode 100644
index 0000000000..0b65eb90fe
--- /dev/null
+++ b/cesar/cp2/secu/src/sha256.c
@@ -0,0 +1,156 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2008 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file cp2/secu/src/sha256.c
+ * \brief SHA 256
+ * \ingroup cp2_secu
+ *
+ * see http://en.wikipedia.org/wiki/SHA256#SHA-2
+ */
+#include "common/std.h"
+#include "lib/read_word.h"
+#include "lib/swap.h"
+#include "string.h"
+#include "stdio.h"
+
+#include "cp2/secu/sha256.h"
+#include "cp2/secu/inc/sha256.h"
+
+static const uint
+round_const [64] = {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
+ 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01,
+ 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa,
+ 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
+ 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138,
+ 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624,
+ 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
+ 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f,
+ 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
+
+/** Initialise the SHA context.
+ *
+ * \param ctx the module context.
+ */
+void
+cp_secu_sha2_init (sha2_t *ctx)
+{
+ dbg_assert(ctx);
+
+ /* SHA-256 */
+ ctx->state[0] = 0x6A09E667;
+ ctx->state[1] = 0xBB67AE85;
+ ctx->state[2] = 0x3C6EF372;
+ ctx->state[3] = 0xA54FF53A;
+ ctx->state[4] = 0x510E527F;
+ ctx->state[5] = 0x9B05688C;
+ ctx->state[6] = 0x1F83D9AB;
+ ctx->state[7] = 0x5BE0CD19;
+}
+
+/**
+ * Process the Hash
+ * \param ctx the module context.
+ * \param data the data to hash.
+ * \param length the length in bytes.
+ */
+void
+cp_secu_sha2_process (sha2_t *ctx, u8 *data, uint length)
+{
+ uint words[64];
+ uint i;
+ uint s0;
+ uint s1;
+ uint a,b,c,d,e,f,g,h;
+ uint maj, t1, t2, ch;
+
+ dbg_assert (ctx);
+ dbg_assert (data);
+
+ data[length] = 0x80;
+ length++;
+ i = length * 8;
+ memcpy (data + length, &i, sizeof(i));
+ length += sizeof (i);
+
+ // Break the data into 16 32bit-integer big endian.
+ for (i = 0; i < 16; i++)
+ {
+ words[i] = swap32(read_u32_from_word (data + (i * 4)));
+ }
+
+ for (i = 16; i < 64; i++)
+ {
+ s0 = ROR(words[i-15], 7) ^ ROR(words[i-15], 18)
+ ^ (words[i-15] >> 3);
+ s1 = ROR(words[i-2], 17) ^ ROR(words[i-2], 19)
+ ^ (words[i-2] >> 10);
+
+ words[i] = words[i-16] + s0 + words[i-7] + s1;
+ }
+
+ // Initialise hash value for this chunk.
+ a = ctx->state[0];
+ b = ctx->state[1];
+ c = ctx->state[2];
+ d = ctx->state[3];
+ e = ctx->state[4];
+ f = ctx->state[5];
+ g = ctx->state[6];
+ f = ctx->state[7];
+
+ // Main loop.
+ for (i = 0; i < 64; i++)
+ {
+ s0 = ROR(a,2) ^ ROR (a, 13) ^ ROR (a, 22);
+ maj = (a & b) ^ (a & c) ^ (b & c);
+ t2 = s0 + maj;
+ s1 = ROR (e, 6) ^ ROR (e, 11) ^ ROR (e, 25);
+ ch = (e & f) ^ ((~e) & g);
+ t1 = h + s1 + ch + round_const[i] + words[i];
+
+ h = g;
+ g = f;
+ f = e;
+ e = d + t1;
+ d = c;
+ c = b;
+ b = a;
+ a = t1 + t2;
+ }
+
+ ctx->state[0] += a;
+ ctx->state[1] += b;
+ ctx->state[2] += c;
+ ctx->state[3] += d;
+ ctx->state[4] += e;
+ ctx->state[5] += f;
+ ctx->state[6] += g;
+ ctx->state[7] += h;
+
+ for (i = 0; i < 8; i++)
+ memcpy (&data[i * 4], &ctx->state[i], 4);
+}
+
+/** SHA 256
+ * \param data the buffer containing the data to hash.
+ * \param length the length of the data.
+ * \return the buffer with the hashed value.
+ */
+void
+cp_secu_sha256 (u8 *data, uint length)
+{
+ sha2_t sha;
+ dbg_assert (data);
+ dbg_assert (length);
+
+ cp_secu_sha2_init (&sha);
+ cp_secu_sha2_process (&sha, data, length);
+}
+
diff --git a/cesar/cp2/secu/test/Makefile b/cesar/cp2/secu/test/Makefile
new file mode 100644
index 0000000000..83a44e1f39
--- /dev/null
+++ b/cesar/cp2/secu/test/Makefile
@@ -0,0 +1,8 @@
+BASE = ../../..
+
+HOST_PROGRAMS = test-sha2
+
+test-sha2_SOURCES = test-sha2.c
+test-sha2_MODULES = lib cp2/secu
+
+include $(BASE)/common/make/top.mk
diff --git a/cesar/cp2/secu/test/src/test-sha2.c b/cesar/cp2/secu/test/src/test-sha2.c
new file mode 100644
index 0000000000..e4da68d8c3
--- /dev/null
+++ b/cesar/cp2/secu/test/src/test-sha2.c
@@ -0,0 +1,57 @@
+/* Cesar project {{{
+ *
+ * Copyright (C) 2008 Spidcom
+ *
+ * <<<Licence>>>
+ *
+ * }}} */
+/**
+ * \file cp2/secu/test/src/test-sha2.c
+ * \brief Test SHA 256.
+ * \ingroup cp2_secu
+ *
+ */
+#include "common/std.h"
+#include "lib/test.h"
+#include "lib/read_word.h"
+#include "string.h"
+
+#include "cp2/secu/sha256.h"
+
+void
+test_case_sha2 (test_t test)
+{
+ u8 buffer[64] __attribute__((aligned(64))) =
+ "The quick brown fox jumps over the lazy dog\0";
+ uint result [16] = {0xd7a8fbb3, 0x07d78094, 0x69ca9abc,
+ 0xb0082e4f, 0x8d5651e4, 0x6d3cdb76, 0x2d02d0bf, 0x37c9e592};
+ uint i;
+
+ cp_secu_sha256 (buffer, strlen( (char*) buffer));
+
+ test_case_begin (test, "SHA 256");
+
+ test_begin (test, "SHA 256")
+ {
+ for (i = 0; i < 16; i++)
+ {
+ test_fail_if (read_u32_from_word (buffer) != result[i],
+ "Wrong buffer out value, value read : %x, shall be : %x",
+ read_u32_from_word (buffer), result[i]);
+ }
+ }
+ test_end;
+}
+
+int
+main (void)
+{
+ test_t test;
+
+ test_init (test, 0, NULL);
+
+ test_case_sha2 (test);
+
+ test_result (test);
+ return test_nb_failed (test) == 0 ? 0 : 1;
+}