summaryrefslogtreecommitdiff
path: root/cesar/cp/secu/src
diff options
context:
space:
mode:
authordufour2009-03-24 14:30:00 +0000
committerdufour2009-03-24 14:30:00 +0000
commit4a720b13cec65c0602dd304959af64b39f1179dc (patch)
treeb31b4be4215d88e96affd2304cf89b23a3508341 /cesar/cp/secu/src
parent66e8ba3d2f3d79bfe96eb8e22808c322346181aa (diff)
* cp/secu, cp/cco/action, cp/sta/action (see #296):
- rewrite pbkdf1 function to be much cleaner (and remove some possible memory leaks), - update upper layer using this function, - cp_secu_aes_generate_key generate fixed length keys, - update tests and stubs according to new functions prototypes. git-svn-id: svn+ssh://pessac/svn/cesar/trunk@4292 017c9cb6-072f-447c-8318-d5b54f68fe89
Diffstat (limited to 'cesar/cp/secu/src')
-rw-r--r--cesar/cp/secu/src/pbkdf1.c101
-rw-r--r--cesar/cp/secu/src/secu.c21
2 files changed, 52 insertions, 70 deletions
diff --git a/cesar/cp/secu/src/pbkdf1.c b/cesar/cp/secu/src/pbkdf1.c
index abce6674bd..37f404a2f3 100644
--- a/cesar/cp/secu/src/pbkdf1.c
+++ b/cesar/cp/secu/src/pbkdf1.c
@@ -16,73 +16,60 @@
#include "cp/secu/defs.h"
#include "cp/secu/sha256.h"
+#include "cp/types.h"
#include "cp/secu/pbkdf1.h"
-
#include "cp/secu/inc/pbkdf1.h"
void
-secu_pbkdf1 (const u8 input[], uint input_len, u8 salt[],
- uint salt_len, uint it_count, u8 output_key[])
+secu_pbkdf1 (const u8 input[], const uint input_length,
+ const u8 salt[CP_SECU_SALT_SIZE], const uint it_count,
+ u8 output[CP_SECU_OUTPUT_KEY_SIZE])
{
+ /* Check parameters. */
+ dbg_assert (input);
+ dbg_assert (input_length && input_length <= CP_SECU_PWD_SIZE_MAX);
+ dbg_assert (output);
- u8 dk[68] = "";
+ /* Maximum size of dk. */
+ u8 dk[CP_SECU_PWD_SIZE_MAX + CP_SECU_SALT_SIZE];
u8 sha_output[CP_SECU_SHA256_SIZE];
- uint dp_size, i;
- dbg_assert (input);
- dbg_assert (output_key);
- if (salt_len)
- dbg_assert (salt);
- /*
- * Copy input to DK (max size cannot be greater than DK buffer)
- */
- for (dp_size = 0;
- (dp_size < input_len) && (dp_size < CP_SECU_PWD_SIZE_MAX);
- dp_size++)
- {
- dk[dp_size] = input[dp_size];
- }
- /*
- * Concat salt with input on DK and count total input size
- */
- for (i=0 ; i < salt_len ; i++)
- {
- dk[dp_size+i] = salt[i];
- }
- dp_size += salt_len;
- /*
- * Compute the first derived key with sha256
- */
- cp_secu_sha256 (dk, dp_size, sha_output);
- /*
- * compute the following iterations
- */
- for(i=1 ; i < it_count ; i++)
+ /* Concatenate input and salt. */
+ /* Add input. */
+ memcpy (dk, input, input_length);
+ /* If there is a salt, add it at the end. */
+ if (salt)
+ memcpy (dk + input_length, salt, CP_SECU_SALT_SIZE);
+
+ /* Compute the first derived key with sha256. */
+ cp_secu_sha256 (dk, input_length + (salt ? CP_SECU_SALT_SIZE : 0),
+ sha_output);
+ /* Compute the following iterations. */
+ uint i;
+ for (i = 1; i < it_count; i++)
{
- memcpy (dk, sha_output, CP_SECU_SHA256_SIZE);
- cp_secu_sha256 (dk, CP_SECU_SHA256_SIZE, sha_output);
+ if (i % 2)
+ cp_secu_sha256 (sha_output, CP_SECU_SHA256_SIZE, dk);
+ else
+ cp_secu_sha256 (dk, CP_SECU_SHA256_SIZE, sha_output);
}
- /*
- * we keep only the OutputKeySize leftmost bytes
- */
- memcpy(output_key, sha_output, CP_SECU_OUTPUT_KEY_SIZE);
+ /* Copy only CP_SECU_OUTPUT_KEY_SIZE as result. */
+ memcpy (output, it_count % 2 ? sha_output : dk, CP_SECU_OUTPUT_KEY_SIZE);
}
-/**
- * PBKDF1 functions for the Secu module.
- * \param buffer the input buffer.
- * \param length the buffer length.
- * \param key key kind.
- */
void
-cp_secu_pbkdf1 (const u8 *in, u8 *out, uint length, cp_secu_salt_kind_t key)
+cp_secu_pbkdf1 (const u8 input[], const uint input_length,
+ cp_key_t *output, cp_secu_salt_kind_t key)
{
- u8 salt [8] = {0x08, 0x85, 0x6d, 0xaf, 0x7c, 0xf5, 0x81, 0x00};
+ /* Check parameters. */
+ dbg_assert (input);
+ dbg_assert (input_length);
+ dbg_assert (output);
+ dbg_assert (sizeof (cp_key_t) == CP_SECU_OUTPUT_KEY_SIZE);
- dbg_assert (in);
- dbg_assert (out);
- dbg_assert (key < CP_SECU_SALT_KEY_NB);
+ /* The salt to use for the PBKDF1 function. */
+ u8 salt[CP_SECU_SALT_SIZE] = {0x08, 0x85, 0x6d, 0xaf, 0x7c, 0xf5, 0x81, 0x00};
switch (key)
{
@@ -94,14 +81,16 @@ cp_secu_pbkdf1 (const u8 *in, u8 *out, uint length, cp_secu_salt_kind_t key)
break;
case CP_SECU_SALT_SPIDCOM:
salt[0] += 2;
- salt[4] = 0xA2;
+ salt[4] = 0xA2;
break;
default:
- dbg_assert (false);
+ /* Unsupported value. */
+ dbg_assert_default ();
}
- secu_pbkdf1 (in, length, salt,
- CP_SECU_SALT_SIZE,
- CP_SECU_PBKDF1_ITERATION, out);
+ /* Call the real PBKDF1 function. */
+ secu_pbkdf1 (input, input_length,
+ salt, CP_SECU_PBKDF1_ITERATION,
+ (u8 *) output->key);
}
diff --git a/cesar/cp/secu/src/secu.c b/cesar/cp/secu/src/secu.c
index 39bfd895c3..e9b94a88f8 100644
--- a/cesar/cp/secu/src/secu.c
+++ b/cesar/cp/secu/src/secu.c
@@ -98,18 +98,15 @@ cp_secu_protocol_next (cp_secu_protocol_run_t *prun, bool last)
}
}
-/**
- * Generate the AES key.
- * \param num a random number.
- * \param output the key generated.
- */
void
-cp_secu_aes_generate_key (uint num, u8 *output)
+cp_secu_aes_generate_key (const uint num, cp_key_t *output)
{
- dbg_assert (num);
+ /* Check parameter. */
dbg_assert (output);
- cp_secu_pbkdf1 ((u8*) &num, output, 4, CP_SECU_SALT_SPIDCOM);
+ /* Call the real function to generate an AES key. */
+ cp_secu_pbkdf1 ((const u8 *) &num, sizeof (num),
+ output, CP_SECU_SALT_SPIDCOM);
}
/**
@@ -121,16 +118,12 @@ cp_secu_aes_generate_key (uint num, u8 *output)
cp_nid_t
cp_secu_nmk2nid(cp_key_t nmk, u8 security_level)
{
- uint i;
cp_nid_t nid = 0;
- u8 buffer[CP_NMK_SIZE];
+ u8 buffer[CP_SECU_OUTPUT_KEY_SIZE];
dbg_assert (security_level <= 2);
- for (i = 0; i < 4; i++)
- memcpy (buffer + i * 4, &nmk.key[i], 4);
-
- secu_pbkdf1 (buffer, CP_NMK_SIZE, NULL, 0, CP_SECU_PBKDF1_ITERATION_NID,
+ secu_pbkdf1 ((const u8 *) &nmk, sizeof (nmk), NULL, CP_SECU_PBKDF1_ITERATION_NID,
buffer);
/* Set the right nibble of rightmost octet of NID = rightmost nibble