adding sha-224, going to test whole SHA2 this night ...

This commit is contained in:
bg 2011-10-11 01:10:32 +02:00
parent ba2a32a172
commit 9132c1117c
5 changed files with 325 additions and 2 deletions

49
hfal/hfal_sha224.c Normal file
View File

@ -0,0 +1,49 @@
/* hfal_sha224.c */
/*
This file is part of the ARM-Crypto-Lib.
Copyright (C) 2011 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_sha224.c
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2011-10-11
* \license GPLv3 or later
*
*/
#include <stdlib.h>
#include "hashfunction_descriptor.h"
#include "sha224.h"
static const char sha224_str[] = "SHA-224";
const hfdesc_t sha224_desc = {
HFDESC_TYPE_HASHFUNCTION,
0,
sha224_str,
sizeof(sha224_ctx_t),
512,
224,
(hf_init_fpt)sha224_init,
(hf_nextBlock_fpt)sha224_nextBlock,
(hf_lastBlock_fpt)sha224_lastBlock,
(hf_ctx2hash_fpt)sha224_ctx2hash,
(hf_free_fpt)NULL,
(hf_mem_fpt)sha224
};

36
hfal/hfal_sha224.h Normal file
View File

@ -0,0 +1,36 @@
/* hfal_sha224.h */
/*
This file is part of the ARM-Crypto-Lib.
Copyright (C) 2011 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file hfal_sha224.h
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2011-10-11
* \license GPLv3 or later
*
*/
#ifndef HFAL_SHA224_H_
#define HFAL_SHA224_H_
#include "hashfunction_descriptor.h"
extern const hfdesc_t sha224_desc;
#endif /* HFAL_SHA224_H_ */

View File

@ -6,8 +6,10 @@ HASHES += $(ALGO_NAME)
$(ALGO_NAME)_DIR := sha2/
$(ALGO_NAME)_INCDIR := hfal/
$(ALGO_NAME)_OBJ := sha512.o sha384.o sha2_large_common.o
$(ALGO_NAME)_TESTBIN := main-sha2-test.o $(CLI_STD) $(HFAL_STD) hfal_sha512.o hfal_sha384.o
$(ALGO_NAME)_OBJ := sha224.o sha256.o sha384.o sha512.o \
sha2_small_common.o sha2_large_common.o
$(ALGO_NAME)_TESTBIN := main-sha2-test.o $(CLI_STD) $(HFAL_STD) \
hfal_sha224.o hfal_sha256.o hfal_sha512.o hfal_sha384.o
$(ALGO_NAME)_NESSIE_TEST := "nessie"
$(ALGO_NAME)_PERFORMANCE_TEST := "performance"

128
sha2/sha224.c Normal file
View File

@ -0,0 +1,128 @@
/* sha224.c */
/*
This file is part of the ARM-Crypto-Lib.
Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file sha224.c
* \author Daniel Otte
* \date 16.05.2006
*
* \par License:
* GPL
*
* \brief SHA-224 implementation.
*
*
*/
#include <stdint.h>
#include <string.h> /* for memcpy, memmove, memset */
#include "sha2_small_common.h"
#include "sha224.h"
#define LITTLE_ENDIAN
#if defined LITTLE_ENDIAN
#elif defined BIG_ENDIAN
#else
#error specify endianess!!!
#endif
/*************************************************************************/
const
uint32_t sha224_init_vector[]={
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
};
/*************************************************************************/
/**
* \brief \c sh224_init initialises a sha224 context for hashing.
* \c sh224_init c initialises the given sha224 context for hashing
* @param state pointer to a sha224 context
* @return none
*/
void sha224_init(sha224_ctx_t *state){
state->length=0;
memcpy(state->h, sha224_init_vector, 8*4);
}
/*************************************************************************/
void sha224_nextBlock (sha224_ctx_t *state, const void* block){
sha2_small_common_nextBlock(state, block);
}
/*************************************************************************/
void sha224_lastBlock (sha224_ctx_t *state, const void* block, uint16_t length_b){
sha2_small_common_lastBlock(state, block, length_b);
}
/*************************************************************************/
/**
* \brief function to process the last block being hashed
* @param state Pointer to the context in which this block should be processed.
* @param block Pointer to the message wich should be hashed.
* @param length is the length of only THIS block in BITS not in bytes!
* bits are big endian, meaning high bits come first.
* if you have a message with bits at the end, the byte must be padded with zeros
*/
/*************************************************************************/
/*
* length in bits!
*/
void sha224(void* dest, const void* msg, uint32_t length_b){ /* length could be choosen longer but this is for µC */
sha224_ctx_t s;
sha224_init(&s);
while(length_b >= SHA224_BLOCK_BITS){
sha224_nextBlock(&s, msg);
msg = (uint8_t*)msg + SHA224_BLOCK_BITS/8;
length_b -= SHA224_BLOCK_BITS;
}
sha224_lastBlock(&s, msg, length_b);
sha224_ctx2hash(dest,&s);
}
/*************************************************************************/
void sha224_ctx2hash(void* dest, const sha224_ctx_t *state){
#if defined LITTLE_ENDIAN
uint8_t i, j, *s=(uint8_t*)(state->h);
i=7;
do{
j=3;
do{
*((uint8_t*)dest) = s[j];
dest = (uint8_t*)dest + 1;
}while(j--);
s += 4;
}while(--i);
#elif BIG_ENDIAN
memcpy(dest, state->h, 28);
#else
# error unsupported endian type!
#endif
}

108
sha2/sha224.h Normal file
View File

@ -0,0 +1,108 @@
/* sha224.h */
/*
This file is part of the ARM-Crypto-Lib.
Copyright (C) 2011 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file sha224.h
* \author Daniel Otte
* \date 2011-10-11
* \license GPLv3 or later
*
*/
#ifndef SHA224_H_
#define SHA224_H_
#include <stdint.h>
#include "sha2_small_common.h"
/** \def SHA224_HASH_BITS
* defines the size of a SHA-224 hash value in bits
*/
/** \def SHA224_HASH_BYTES
* defines the size of a SHA-224 hash value in bytes
*/
/** \def SHA224_BLOCK_BITS
* defines the size of a SHA-224 input block in bits
*/
/** \def SHA224_BLOCK_BYTES
* defines the size of a SHA-224 input block in bytes
*/
#define SHA224_HASH_BITS 224
#define SHA224_HASH_BYTES (SHA224_HASH_BITS/8)
#define SHA224_BLOCK_BITS 512
#define SHA224_BLOCK_BYTES (SHA224_BLOCK_BITS/8)
/** \typedef sha224_ctx_t
* \brief SHA-224 context type
*
* A variable of this type may hold the state of a SHA-224 hashing process
*/
typedef sha2_small_common_ctx_t sha224_ctx_t;
/** \fn void sha224_init(sha224_ctx_t *state)
* \brief initialize a SHA-224 context
*
* This function sets a ::sha224_ctx_t to the initial values for hashing.
* \param state pointer to the SHA-224 hashing context
*/
void sha224_init(sha224_ctx_t *state);
/** \fn void sha224_nextBlock (sha224_ctx_t* state, const void* block)
* \brief update the context with a given block
*
* This function updates the SHA-224 hash context by processing the given block
* of fixed length.
* \param state pointer to the SHA-224 hash context
* \param block pointer to the block of fixed length (512 bit = 64 byte)
*/
void sha224_nextBlock (sha224_ctx_t* state, const void* block);
/** \fn void sha224_lastBlock(sha224_ctx_t* state, const void* block, uint16_t length_b)
* \brief finalize the context with the given block
*
* This function finalizes the SHA-224 hash context by processing the given block
* of variable length.
* \param state pointer to the SHA-224 hash context
* \param block pointer to the block of fixed length (512 bit = 64 byte)
* \param length_b the length of the block in bits
*/
void sha224_lastBlock(sha224_ctx_t* state, const void* block, uint16_t length_b);
/** \fn void sha224_ctx2hash(sha224_hash_t* dest, const sha224_ctx_t* state)
* \brief convert the hash state into the hash value
* This function reads the context and writes the hash value to the destination
* \param dest pointer to the location where the hash value should be written
* \param state pointer to the SHA-224 hash context
*/
void sha224_ctx2hash(void* dest, const sha224_ctx_t* state);
/** \fn void sha224(sha224_hash_t* dest, const void* msg, uint32_t length_b)
* \brief simple SHA-224 hashing function for direct hashing
*
* This function automatically hashes a given message of arbitary length with
* the SHA-224 hashing algorithm.
* \param dest pointer to the location where the hash value is going to be written to
* \param msg pointer to the message thats going to be hashed
* \param length_b length of the message in bits
*/
void sha224(void* dest, const void* msg, uint32_t length_b);
#endif /*SHA224_H_*/