Skip to content
Snippets Groups Projects
Commit e0e61f6f authored by da1l6's avatar da1l6
Browse files

Fix hash head not correctly calculated on key slot init.

The buffer that was used to calculate it did not contain the current salt.
Instead of creating a new buffer, we now use the sha1 block functions directly
and hash key and salt in two calls.

Additionally a ctx2hashhead function is added to convert only the first 4 bytes
of the hash instead of converting all 20 bytes and throwing 16 bytes away afterwards.
parent 3bdf8a8d
No related branches found
No related tags found
No related merge requests found
...@@ -215,34 +215,15 @@ uint8_t keystore_init_slot(uint16_t index, KEY keyout){ ...@@ -215,34 +215,15 @@ uint8_t keystore_init_slot(uint16_t index, KEY keyout){
if (!keystore_write_slot_status(index, KEYSLOT_USED)) return 0; if (!keystore_write_slot_status(index, KEYSLOT_USED)) return 0;
_delay_us(40); _delay_us(40);
HASH fullhash; sha1_ctx_t s;
sha1(&fullhash, keyout, (sizeof(KEY) + sizeof(SALT))*8); sha1_init(&s);
memcpy(&hashes[index], fullhash, sizeof(HASH_HEAD)); sha1_nextBlock(&s, keyout);
sha1_lastBlock(&s, salt, sizeof(SALT));
sha1_ctx2hashhead(&hashes[index], &s);
return 1; return 1;
} }
/**
* Initializes the given keyslot with an empty (all 0-bytes)
* @param index index to the keyslot to initialize
* @return 1 if successful, 0 if not.
*/
uint8_t keystore_init_slot_empty(uint16_t index, KEY keyout){
KEY key;
memset(key, 0, sizeof(KEY));
if (!keystore_write_slot_status(index, KEYSLOT_EMPTY)) return 0;
_delay_us(40);
if (!keystore_write_key(index, key)) return 0;
_delay_us(40);
if (!keystore_write_slot_status(index, KEYSLOT_USED)) return 0;
_delay_us(40);
HASH fullhash;
sha1(&fullhash, keyout, (sizeof(KEY) + sizeof(SALT))*8);
memcpy(&hashes[index], fullhash, sizeof(HASH_HEAD));
return 1;
}
uint8_t keystore_clear_slot(uint16_t index){ uint8_t keystore_clear_slot(uint16_t index){
if (!keystore_write_slot_status(index, KEYSLOT_EMPTY)) return 0; if (!keystore_write_slot_status(index, KEYSLOT_EMPTY)) return 0;
......
...@@ -231,6 +231,19 @@ void sha1_ctx2hash (sha1_hash_t *dest, sha1_ctx_t *state){ ...@@ -231,6 +231,19 @@ void sha1_ctx2hash (sha1_hash_t *dest, sha1_ctx_t *state){
/********************************************************************************************************/ /********************************************************************************************************/
void sha1_ctx2hashhead (uint8_t dest[4], sha1_ctx_t *state){
#if defined LITTLE_ENDIAN
((uint32_t*)dest)[0] = change_endian32(state->h[0]);
#elif BIG_ENDIAN
if (dest != state->h)
memcpy(dest, state->h, 4);
#else
# error unsupported endian type!
#endif
}
/********************************************************************************************************/
uint8_t sha1_ctx_hash_compare(sha1_hash_t *ref, sha1_ctx_t *state){ uint8_t sha1_ctx_hash_compare(sha1_hash_t *ref, sha1_ctx_t *state){
#if defined LITTLE_ENDIAN #if defined LITTLE_ENDIAN
uint8_t i; uint8_t i;
......
...@@ -102,6 +102,15 @@ void sha1_lastBlock (sha1_ctx_t *state, const void* block, uint16_t length_b); ...@@ -102,6 +102,15 @@ void sha1_lastBlock (sha1_ctx_t *state, const void* block, uint16_t length_b);
*/ */
void sha1_ctx2hash (sha1_hash_t *dest, sha1_ctx_t *state); void sha1_ctx2hash (sha1_hash_t *dest, sha1_ctx_t *state);
/** \fn sha1_ctx2hashhead(uint8_t dest[4], sha1_ctx_t *state)
* \brief convert a state variable into a 4 byte hash value head
* Writes the hash value head corresponding to the state to the memory pointed by dest.
* \param dest pointer to the hash value destination
* \param state pointer to the hash context
*/
void sha1_ctx2hashhead (uint8_t dest[4], sha1_ctx_t *state);
/** \fn sha1_ctx_hash_compare(sha1_hash_t *ref, sha1_ctx_t *state) /** \fn sha1_ctx_hash_compare(sha1_hash_t *ref, sha1_ctx_t *state)
* \brief compares a state variable with an actual hash value * \brief compares a state variable with an actual hash value
* \param ref sha1 hash to compere with * \param ref sha1 hash to compere with
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment