From: bg Date: Mon, 10 Oct 2011 16:55:59 +0000 (+0200) Subject: moving sha256 arround X-Git-Url: https://git.cryptolib.org/?a=commitdiff_plain;h=38d2de57dfdb3c2012b5bdeff9d163673fbc9e94;p=arm-crypto-lib.git moving sha256 arround --- diff --git a/mkfiles/sha256_c.mk b/mkfiles/sha256_c.mk index 734184c..0378c65 100644 --- a/mkfiles/sha256_c.mk +++ b/mkfiles/sha256_c.mk @@ -4,9 +4,9 @@ ALGO_NAME := SHA256_C # comment out the following line for removement of SHA256 from the build process HASHES += $(ALGO_NAME) -$(ALGO_NAME)_DIR := sha256/ +$(ALGO_NAME)_DIR := sha2/ $(ALGO_NAME)_INCDIR := hfal/ -$(ALGO_NAME)_OBJ := sha256.o +$(ALGO_NAME)_OBJ := sha256.o sha2_small_common.o $(ALGO_NAME)_TESTBIN := main-sha256-test.o $(CLI_STD) $(HFAL_STD) hfal_sha256.o $(ALGO_NAME)_NESSIE_TEST := "nessie" $(ALGO_NAME)_PERFORMANCE_TEST := "performance" diff --git a/sha2/sha256.c b/sha2/sha256.c new file mode 100644 index 0000000..cf61fba --- /dev/null +++ b/sha2/sha256.c @@ -0,0 +1,127 @@ +/* sha256.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 . +*/ +/** + * \file sha256.c + * \author Daniel Otte + * \date 16.05.2006 + * + * \par License: + * GPL + * + * \brief SHA-256 implementation. + * + * + */ + +#include +#include /* for memcpy, memmove, memset */ +#include "sha2_small_common.h" +#include "sha256.h" + +#define LITTLE_ENDIAN + +#if defined LITTLE_ENDIAN +#elif defined BIG_ENDIAN +#else + #error specify endianess!!! +#endif + + +/*************************************************************************/ + +const +uint32_t sha256_init_vector[]={ + 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, + 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19 }; + + +/*************************************************************************/ + +/** + * \brief \c sh256_init initialises a sha256 context for hashing. + * \c sh256_init c initialises the given sha256 context for hashing + * @param state pointer to a sha256 context + * @return none + */ +void sha256_init(sha256_ctx_t *state){ + state->length=0; + memcpy(state->h, sha256_init_vector, 8*4); +} + +/*************************************************************************/ +void sha256_nextBlock (sha256_ctx_t *state, const void* block){ + sha2_small_common_nextBlock(state, block); +} + +/*************************************************************************/ +void sha256_lastBlock (sha256_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 sha256(void* dest, const void* msg, uint32_t length_b){ /* length could be choosen longer but this is for µC */ + sha256_ctx_t s; + sha256_init(&s); + while(length_b >= SHA256_BLOCK_BITS){ + sha256_nextBlock(&s, msg); + msg = (uint8_t*)msg + SHA256_BLOCK_BITS/8; + length_b -= SHA256_BLOCK_BITS; + } + sha256_lastBlock(&s, msg, length_b); + sha256_ctx2hash(dest,&s); +} + + + +/*************************************************************************/ + +void sha256_ctx2hash(void* dest, const sha256_ctx_t *state){ +#if defined LITTLE_ENDIAN + uint8_t i, j, *s=(uint8_t*)(state->h); + i=8; + 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, 32); +#else +# error unsupported endian type! +#endif +} + + diff --git a/sha2/sha256.h b/sha2/sha256.h new file mode 100644 index 0000000..2adf929 --- /dev/null +++ b/sha2/sha256.h @@ -0,0 +1,108 @@ +/* sha256.h */ +/* + This file is part of the ARM-Crypto-Lib. + Copyright (C) 2008 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 . +*/ +/** + * \file sha256.h + * \author Daniel Otte + * \date 2006-05-16 + * \license GPLv3 or later + * + */ + +#ifndef SHA256_H_ +#define SHA256_H_ + +#include +#include "sha2_small_common.h" +/** \def SHA256_HASH_BITS + * defines the size of a SHA-256 hash value in bits + */ + +/** \def SHA256_HASH_BYTES + * defines the size of a SHA-256 hash value in bytes + */ + +/** \def SHA256_BLOCK_BITS + * defines the size of a SHA-256 input block in bits + */ + +/** \def SHA256_BLOCK_BYTES + * defines the size of a SHA-256 input block in bytes + */ + +#define SHA256_HASH_BITS 256 +#define SHA256_HASH_BYTES (SHA256_HASH_BITS/8) +#define SHA256_BLOCK_BITS 512 +#define SHA256_BLOCK_BYTES (SHA256_BLOCK_BITS/8) + +/** \typedef sha256_ctx_t + * \brief SHA-256 context type + * + * A variable of this type may hold the state of a SHA-256 hashing process + */ +typedef sha2_small_common_ctx_t sha256_ctx_t; + +/** \fn void sha256_init(sha256_ctx_t *state) + * \brief initialize a SHA-256 context + * + * This function sets a ::sha256_ctx_t to the initial values for hashing. + * \param state pointer to the SHA-256 hashing context + */ +void sha256_init(sha256_ctx_t *state); + +/** \fn void sha256_nextBlock (sha256_ctx_t* state, const void* block) + * \brief update the context with a given block + * + * This function updates the SHA-256 hash context by processing the given block + * of fixed length. + * \param state pointer to the SHA-256 hash context + * \param block pointer to the block of fixed length (512 bit = 64 byte) + */ +void sha256_nextBlock (sha256_ctx_t* state, const void* block); + +/** \fn void sha256_lastBlock(sha256_ctx_t* state, const void* block, uint16_t length_b) + * \brief finalize the context with the given block + * + * This function finalizes the SHA-256 hash context by processing the given block + * of variable length. + * \param state pointer to the SHA-256 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 sha256_lastBlock(sha256_ctx_t* state, const void* block, uint16_t length_b); + +/** \fn void sha256_ctx2hash(sha256_hash_t* dest, const sha256_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-256 hash context + */ +void sha256_ctx2hash(void* dest, const sha256_ctx_t* state); + +/** \fn void sha256(sha256_hash_t* dest, const void* msg, uint32_t length_b) + * \brief simple SHA-256 hashing function for direct hashing + * + * This function automatically hashes a given message of arbitary length with + * the SHA-256 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 sha256(void* dest, const void* msg, uint32_t length_b); + +#endif /*SHA256_H_*/ diff --git a/sha2/sha2_small_common.c b/sha2/sha2_small_common.c new file mode 100644 index 0000000..d1c5d36 --- /dev/null +++ b/sha2/sha2_small_common.c @@ -0,0 +1,153 @@ +/* sha2_small_common.c */ +/* + This file is part of the ARM-Crypto-Lib. + Copyright (C) 2006-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 . +*/ + +#include +#include +#include "sha2_small_common.h" + +#define LITTLE_ENDIAN + +/** + * rotate x right by n positions + */ +static +uint32_t rotr32( uint32_t x, uint8_t n){ + return ((x>>n) | (x<<(32-n))); +} + +static +uint32_t rotl32( uint32_t x, uint8_t n){ + return ((x<>(32-n))); +} + + +/*************************************************************************/ + +// #define CHANGE_ENDIAN32(x) (((x)<<24) | ((x)>>24) | (((x)& 0x0000ff00)<<8) | (((x)& 0x00ff0000)>>8)) +static +uint32_t change_endian32(uint32_t x){ + return (((x)<<24) | ((x)>>24) | (((x)& 0x0000ff00)<<8) | (((x)& 0x00ff0000)>>8)); +} + + +/* sha256 functions as macros for speed and size, cause they are called only once */ + +#define CH(x,y,z) (((x)&(y)) ^ ((~(x))&(z))) +#define MAJ(x,y,z) (((x)&(y)) ^ ((x)&(z)) ^ ((y)&(z))) + +#define SIGMA0(x) (rotr32((x),2) ^ rotr32((x),13) ^ rotl32((x),10)) +#define SIGMA1(x) (rotr32((x),6) ^ rotr32((x),11) ^ rotl32((x),7)) +#define SIGMA_a(x) (rotr32((x),7) ^ rotl32((x),14) ^ ((x)>>3)) +#define SIGMA_b(x) (rotl32((x),15) ^ rotl32((x),13) ^ ((x)>>10)) + +const +uint32_t k[]={ + 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 +}; + + + +/** + * block must be, 512, Bit = 64, Byte, long !!! + */ +void sha2_small_common_nextBlock (sha2_small_common_ctx_t *state, const void* block){ + uint32_t w[64]; /* this is 256, byte, large, */ + uint8_t i; + uint32_t a[8],t1,t2; + + /* init w */ +#if defined LITTLE_ENDIAN + for (i=0; i<16; ++i){ + w[i]= change_endian32(((uint32_t*)block)[i]); + } +#elif defined BIG_ENDIAN + memcpy((void*)w, block, 64); +#endif + for (i=16; i<64; ++i){ + w[i] = SIGMA_b(w[i-2]) + w[i-7] + SIGMA_a(w[i-15]) + w[i-16]; + } + + /* init working variables */ + memcpy((void*)a,(void*)(state->h), 8*4); + + /* do the, fun stuff, */ + for (i=0; i<64; ++i){ + t1 = a[7] + SIGMA1(a[4]) + CH(a[4],a[5],a[6]) + k[i] + w[i]; + t2 = SIGMA0(a[0]) + MAJ(a[0],a[1],a[2]); + memmove(&(a[1]), &(a[0]), 7*4); /* a[7]=a[6]; a[6]=a[5]; a[5]=a[4]; a[4]=a[3]; a[3]=a[2]; a[2]=a[1]; a[1]=a[0]; */ + a[4] += t1; + a[0] = t1 + t2; + } + + /* update, the, state, */ + for (i=0; i<8; ++i){ + state->h[i] += a[i]; + } + state->length += 512; +} + + +void sha2_small_common_lastBlock(sha2_small_common_ctx_t *state, const void* block, uint16_t length_b){ + uint8_t lb[512/8]; /* local block */ +// uint64_t len; + while(length_b>=512){ + sha2_small_common_nextBlock(state, block); + length_b -= 512; + block = (uint8_t*)block+64; + } + + state->length += length_b; + memcpy (&(lb[0]), block, length_b/8); + + /* set the final one bit */ + if (length_b & 0x7){ // if we have single bits at the end + lb[length_b/8] = ((uint8_t*)(block))[length_b/8]; + } else { + lb[length_b/8] = 0; + } + lb[length_b/8] |= 0x80>>(length_b & 0x7); + length_b =(length_b >> 3) + 1; /* from now on length contains the number of BYTES in lb*/ + /* pad with zeros */ + if (length_b>64-8){ /* not enouth space for 64bit length value */ + memset((void*)(&(lb[length_b])), 0, 64-length_b); + sha2_small_common_nextBlock(state, lb); + state->length -= 512; + length_b = 0; + } + memset((void*)(&(lb[length_b])), 0, 56-length_b); + /* store the 64bit length value */ +#if defined LITTLE_ENDIAN + /* this is now rolled up */ + uint8_t i; + for (i=1; i<=8; ++i){ + lb[55+i] = (uint8_t)(state->length>>(64- 8*i)); + } +#elif defined BIG_ENDIAN + *((uint64_t)&(lb[56])) = state->length; +#endif + sha2_small_common_nextBlock(state, lb); +} + diff --git a/sha2/sha2_small_common.h b/sha2/sha2_small_common.h new file mode 100644 index 0000000..9010cfb --- /dev/null +++ b/sha2/sha2_small_common.h @@ -0,0 +1,31 @@ +/* sha2_small_common.h */ +/* + This file is part of the AVR-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 . +*/ + +#ifndef SHA2_SMALL_COMMON_H_ +#define SHA2_SMALL_COMMON_H_ + +typedef struct { + uint32_t h[8]; + uint64_t length; +} sha2_small_common_ctx_t; + +void sha2_small_common_nextBlock(sha2_small_common_ctx_t* state, const void* block); +void sha2_small_common_lastBlock(sha2_small_common_ctx_t* state, const void* block, uint16_t length_b); + +#endif /* SHA2_SMALL_COMMON_H_ */ diff --git a/sha256/sha256.c b/sha256/sha256.c deleted file mode 100644 index 3e66355..0000000 --- a/sha256/sha256.c +++ /dev/null @@ -1,243 +0,0 @@ -/* sha256.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 . -*/ -/** - * \file sha256.c - * \author Daniel Otte - * \date 16.05.2006 - * - * \par License: - * GPL - * - * \brief SHA-256 implementation. - * - * - */ - -#include -#include /* for memcpy, memmove, memset */ -#include "sha256.h" - -#define LITTLE_ENDIAN - -#if defined LITTLE_ENDIAN -#elif defined BIG_ENDIAN -#else - #error specify endianess!!! -#endif - - -/*************************************************************************/ - -const -uint32_t sha256_init_vector[]={ - 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, - 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19 }; - - -/*************************************************************************/ - -/** - * \brief \c sh256_init initialises a sha256 context for hashing. - * \c sh256_init c initialises the given sha256 context for hashing - * @param state pointer to a sha256 context - * @return none - */ -void sha256_init(sha256_ctx_t *state){ - state->length=0; - memcpy(state->h, sha256_init_vector, 8*4); -} - -/*************************************************************************/ - -/** - * rotate x right by n positions - */ -static -uint32_t rotr32( uint32_t x, uint8_t n){ - return ((x>>n) | (x<<(32-n))); -} - -static -uint32_t rotl32( uint32_t x, uint8_t n){ - return ((x<>(32-n))); -} - -/*************************************************************************/ - -// #define CHANGE_ENDIAN32(x) (((x)<<24) | ((x)>>24) | (((x)& 0x0000ff00)<<8) | (((x)& 0x00ff0000)>>8)) -static -uint32_t change_endian32(uint32_t x){ - return (((x)<<24) | ((x)>>24) | (((x)& 0x0000ff00)<<8) | (((x)& 0x00ff0000)>>8)); -} - - -/*************************************************************************/ - -/* sha256 functions as macros for speed and size, cause they are called only once */ - -#define CH(x,y,z) (((x)&(y)) ^ ((~(x))&(z))) -#define MAJ(x,y,z) (((x)&(y)) ^ ((x)&(z)) ^ ((y)&(z))) - -#define SIGMA0(x) (rotr32((x),2) ^ rotr32((x),13) ^ rotl32((x),10)) -#define SIGMA1(x) (rotr32((x),6) ^ rotr32((x),11) ^ rotl32((x),7)) -#define SIGMA_a(x) (rotr32((x),7) ^ rotl32((x),14) ^ ((x)>>3)) -#define SIGMA_b(x) (rotl32((x),15) ^ rotl32((x),13) ^ ((x)>>10)) - -const -uint32_t k[]={ - 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 -}; - - -/*************************************************************************/ - -/** - * block must be, 512, Bit = 64, Byte, long !!! - */ -void sha256_nextBlock (sha256_ctx_t *state, const void* block){ - uint32_t w[64]; /* this is 256, byte, large, */ - uint8_t i; - uint32_t a[8],t1,t2; - - /* init w */ -#if defined LITTLE_ENDIAN - for (i=0; i<16; ++i){ - w[i]= change_endian32(((uint32_t*)block)[i]); - } -#elif defined BIG_ENDIAN - memcpy((void*)w, block, 64); -#endif - for (i=16; i<64; ++i){ - w[i] = SIGMA_b(w[i-2]) + w[i-7] + SIGMA_a(w[i-15]) + w[i-16]; - } - - /* init working variables */ - memcpy((void*)a,(void*)(state->h), 8*4); - - /* do the, fun stuff, */ - for (i=0; i<64; ++i){ - t1 = a[7] + SIGMA1(a[4]) + CH(a[4],a[5],a[6]) + k[i] + w[i]; - t2 = SIGMA0(a[0]) + MAJ(a[0],a[1],a[2]); - memmove(&(a[1]), &(a[0]), 7*4); /* a[7]=a[6]; a[6]=a[5]; a[5]=a[4]; a[4]=a[3]; a[3]=a[2]; a[2]=a[1]; a[1]=a[0]; */ - a[4] += t1; - a[0] = t1 + t2; - } - - /* update, the, state, */ - for (i=0; i<8; ++i){ - state->h[i] += a[i]; - } - state->length += 512; -} - - -/*************************************************************************/ - -/** - * \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 - */ -void sha256_lastBlock(sha256_ctx_t *state, const void* block, uint16_t length){ - uint8_t lb[SHA256_BLOCK_BITS/8]; /* local block */ - while(length>=SHA256_BLOCK_BITS){ - sha256_nextBlock(state, block); - length -= SHA256_BLOCK_BITS; - block = (uint8_t*)block+SHA256_BLOCK_BYTES; - } - - state->length += length; - memcpy (&(lb[0]), block, length/8); - - /* set the final one bit */ - if (length & 0x7){ // if we have single bits at the end - lb[length/8] = ((uint8_t*)(block))[length/8]; - } else { - lb[length/8] = 0; - } - lb[length/8] |= 0x80>>(length & 0x7); - length =(length >> 3) + 1; /* from now on length contains the number of BYTES in lb*/ - /* pad with zeros */ - if (length>64-8){ /* not enouth space for 64bit length value */ - memset((void*)(&(lb[length])), 0, 64-length); - sha256_nextBlock(state, lb); - state->length -= 512; - length = 0; - } - memset((void*)(&(lb[length])), 0, 56-length); - /* store the 64bit length value */ -#if defined LITTLE_ENDIAN - /* this is now rolled up */ - uint8_t i; - for (i=1; i<=8; ++i){ - lb[55+i] = (uint8_t)(state->length>>(64- 8*i)); - } -#elif defined BIG_ENDIAN - *((uint64_t)&(lb[56])) = state->length; -#endif - sha256_nextBlock(state, lb); -} - - -/*************************************************************************/ - -/* - * length in bits! - */ -void sha256(sha256_hash_t *dest, const void* msg, uint32_t length){ /* length could be choosen longer but this is for µC */ - sha256_ctx_t s; - sha256_init(&s); - while(length >= SHA256_BLOCK_BITS){ - sha256_nextBlock(&s, msg); - msg = (uint8_t*)msg + SHA256_BLOCK_BITS/8; - length -= SHA256_BLOCK_BITS; - } - sha256_lastBlock(&s, msg, length); - sha256_ctx2hash(dest,&s); -} - - - -/*************************************************************************/ - -void sha256_ctx2hash(sha256_hash_t *dest, const sha256_ctx_t *state){ -#if defined LITTLE_ENDIAN - uint8_t i; - for(i=0; i<8; ++i){ - ((uint32_t*)dest)[i] = change_endian32(state->h[i]); - } -#elif BIG_ENDIAN - if (dest != state->h) - memcpy(dest, state->h, SHA256_HASH_BITS/8); -#else -# error unsupported endian type! -#endif -} - - diff --git a/sha256/sha256.h b/sha256/sha256.h deleted file mode 100644 index 1a310d7..0000000 --- a/sha256/sha256.h +++ /dev/null @@ -1,119 +0,0 @@ -/* sha256.h */ -/* - This file is part of the ARM-Crypto-Lib. - Copyright (C) 2008 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 . -*/ -/** - * \file sha256.h - * \author Daniel Otte - * \date 2006-05-16 - * \license GPLv3 or later - * - */ - -#ifndef SHA256_H_ -#define SHA256_H_ - -#include - -/** \def SHA256_HASH_BITS - * defines the size of a SHA-256 hash value in bits - */ - -/** \def SHA256_HASH_BYTES - * defines the size of a SHA-256 hash value in bytes - */ - -/** \def SHA256_BLOCK_BITS - * defines the size of a SHA-256 input block in bits - */ - -/** \def SHA256_BLOCK_BYTES - * defines the size of a SHA-256 input block in bytes - */ - -#define SHA256_HASH_BITS 256 -#define SHA256_HASH_BYTES (SHA256_HASH_BITS/8) -#define SHA256_BLOCK_BITS 512 -#define SHA256_BLOCK_BYTES (SHA256_BLOCK_BITS/8) - -/** \typedef sha256_ctx_t - * \brief SHA-256 context type - * - * A variable of this type may hold the state of a SHA-256 hashing process - */ -typedef struct { - uint32_t h[8]; - uint64_t length; -} sha256_ctx_t; - -/** \typedef sha256_hash_t - * \brief SHA-256 hash value type - * - * A variable of this type may hold the hash value produced by the - * sha256_ctx2hash(sha256_hash_t* dest, const sha256_ctx_t* state) function. - */ -typedef uint8_t sha256_hash_t[SHA256_HASH_BYTES]; - -/** \fn void sha256_init(sha256_ctx_t *state) - * \brief initialize a SHA-256 context - * - * This function sets a ::sha256_ctx_t to the initial values for hashing. - * \param state pointer to the SHA-256 hashing context - */ -void sha256_init(sha256_ctx_t *state); - -/** \fn void sha256_nextBlock (sha256_ctx_t* state, const void* block) - * \brief update the context with a given block - * - * This function updates the SHA-256 hash context by processing the given block - * of fixed length. - * \param state pointer to the SHA-256 hash context - * \param block pointer to the block of fixed length (512 bit = 64 byte) - */ -void sha256_nextBlock (sha256_ctx_t* state, const void* block); - -/** \fn void sha256_lastBlock(sha256_ctx_t* state, const void* block, uint16_t length_b) - * \brief finalize the context with the given block - * - * This function finalizes the SHA-256 hash context by processing the given block - * of variable length. - * \param state pointer to the SHA-256 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 sha256_lastBlock(sha256_ctx_t* state, const void* block, uint16_t length_b); - -/** \fn void sha256_ctx2hash(sha256_hash_t* dest, const sha256_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-256 hash context - */ -void sha256_ctx2hash(sha256_hash_t* dest, const sha256_ctx_t* state); - -/** \fn void sha256(sha256_hash_t* dest, const void* msg, uint32_t length_b) - * \brief simple SHA-256 hashing function for direct hashing - * - * This function automatically hashes a given message of arbitary length with - * the SHA-256 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 sha256(sha256_hash_t* dest, const void* msg, uint32_t length_b); - -#endif /*SHA256_H_*/ diff --git a/test_src/main-sha256-test.c b/test_src/main-sha256-test.c index 872e071..29c2500 100644 --- a/test_src/main-sha256-test.c +++ b/test_src/main-sha256-test.c @@ -21,15 +21,7 @@ * */ -#include -#include -#include -#include "config.h" -#include "cli.h" -#include "dump.h" -#include "uart_lowlevel.h" -#include "sysclock.h" -#include "hw_gptm.h" +#include "main-test-common.h" #include "shavs.h" #include "nessie_hash_test.h" @@ -41,14 +33,6 @@ #include "sha256.h" #include "hfal_sha256.h" -void uart0_putc(char byte){ - uart_putc(UART_0, byte); -} - -char uart0_getc(void){ - return uart_getc(UART_0); -} - const char* algo_name = "SHA-256"; const hfdesc_t* algolist[] = { @@ -97,10 +81,10 @@ void test_monte(void){ 0x38, 0xF0, 0xDF, 0x70, 0x1D, 0xA9, 0x3C, 0x3B, 0xF2, 0xC9, 0xC8, 0x68, 0x96, 0xE7, 0xE6, 0xC7 }; uint8_t hash[SHA256_HASH_BYTES]; - sha256((sha256_hash_t*)hash, data1, 3*32*8); + sha256(hash, data1, 3*32*8); cli_putstr("\r\n hash(data1) = "); cli_hexdump(hash, 32); - sha256((sha256_hash_t*)hash, data2, 3*32*8); + sha256(hash, data2, 3*32*8); cli_putstr("\r\n hash(data2) = "); cli_hexdump(hash, 32); } @@ -125,7 +109,7 @@ void test_monte2(void){ 0x39, 0xd8, 0x35, 0xa7, 0x24, 0xe2, 0xfa, 0xe7 }; uint8_t hash[SHA256_HASH_BYTES]; - sha256((sha256_hash_t*)hash, data, 1024); + sha256(hash, data, 1024); cli_putstr("\r\n hash(data) = "); cli_hexdump(hash, 32); } @@ -164,26 +148,14 @@ const cmdlist_entry_t cmdlist[] = { }; int main(void) { - sysclk_set_freq(SYS_FREQ); - sysclk_mosc_verify_enable(); - uart_init(UART_0, 115200, 8, UART_PARATY_NONE, UART_STOPBITS_ONE); - gptm_set_timer_32periodic(TIMER0); - - cli_rx = uart0_getc; - cli_tx = uart0_putc; + main_setup(); shavs_algolist=(hfdesc_t**)algolist; shavs_algo=(hfdesc_t*)&sha256_desc; for(;;){ - cli_putstr("\r\n\r\nARM-Crypto-Lib VS ("); - cli_putstr(algo_name); - cli_putstr("; "); - cli_putstr(__DATE__); - cli_putc(' '); - cli_putstr(__TIME__); - cli_putstr(")\r\nloaded and running\r\n"); - cmd_interface(cmdlist); + welcome_msg(algo_name); + cmd_interface(cmdlist); } }