X-Git-Url: https://git.cryptolib.org/?p=avr-crypto-lib.git;a=blobdiff_plain;f=sha1%2Fsha1.c;h=bed8b5e039e92ec116508debdf217b54b687af51;hp=046d8e1d7aade9c41f7b67c080bd080b69722590;hb=4b5da1dc27a791b5c448274a3db09cd035b33493;hpb=bcf30b86c5b63415da79f7cd41e595ae2c510163 diff --git a/sha1/sha1.c b/sha1/sha1.c index 046d8e1..bed8b5e 100644 --- a/sha1/sha1.c +++ b/sha1/sha1.c @@ -1,7 +1,7 @@ /* sha1.c */ /* This file is part of the AVR-Crypto-Lib. - Copyright (C) 2008, 2009 Daniel Otte (daniel.otte@rub.de) + Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org) 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 @@ -35,6 +35,7 @@ # undef DEBUG #endif +#include "cli.h" #define LITTLE_ENDIAN @@ -92,7 +93,7 @@ uint32_t parity(uint32_t x, uint32_t y, uint32_t z){ typedef uint32_t (*pf_t)(uint32_t x, uint32_t y, uint32_t z); -void sha1_nextBlock (sha1_ctx_t *state, const void* block){ +void sha1_nextBlock (sha1_ctx_t *state, const void *block){ uint32_t a[5]; uint32_t w[16]; uint32_t temp; @@ -111,9 +112,14 @@ void sha1_nextBlock (sha1_ctx_t *state, const void* block){ #if DEBUG uint8_t dbgi; for(dbgi=0; dbgi<16; ++dbgi){ + /* DEBUG_S("\n\rBlock:"); DEBUG_B(dbgi); DEBUG_C(':'); + */ + cli_putstr_P(PSTR("\r\nBlock:")); + cli_hexdump(&dbgi, 1); + cli_putc(':'); cli_hexdump(&(w[dbgi]) ,4); } #endif @@ -167,30 +173,25 @@ void sha1_nextBlock (sha1_ctx_t *state, const void* block){ /********************************************************************************************************/ -void sha1_lastBlock(sha1_ctx_t *state, const void* block, uint16_t length){ - uint8_t lb[SHA1_BLOCK_BITS/8]; /* local block */ - while(length>=512){ +void sha1_lastBlock(sha1_ctx_t *state, const void *block, uint16_t length){ + uint8_t lb[SHA1_BLOCK_BYTES]; /* local block */ + while(length>=SHA1_BLOCK_BITS){ sha1_nextBlock(state, block); - length -=512; - block = (uint8_t*)block + 512/8; + length -= SHA1_BLOCK_BITS; + block = (uint8_t*)block + SHA1_BLOCK_BYTES; } state->length += length; - lb[length/8] = 0; - memcpy (lb, block, (length+7)/8); + memset(lb, 0, SHA1_BLOCK_BYTES); + memcpy (lb, block, (length+7)>>3); /* set the final one bit */ - lb[length/8] |= 0x80>>(length & 0x07); - length=(length)/8 +1; /* from now on length contains the number of BYTES in lb */ + lb[length>>3] |= 0x80>>(length & 0x07); - if (length>64-8){ /* not enouth space for 64bit length value */ - memset(lb+length, 0, 64-length); + if (length>512-64-1){ /* not enouth space for 64bit length value */ sha1_nextBlock(state, lb); state->length -= 512; - length = 0; + memset(lb, 0, SHA1_BLOCK_BYTES); } - - /* pad with zeros */ - memset(lb+length, 0, 64-length); /* store the 64bit length value */ #if defined LITTLE_ENDIAN /* this is now rolled up */ @@ -206,7 +207,7 @@ void sha1_lastBlock(sha1_ctx_t *state, const void* block, uint16_t length){ /********************************************************************************************************/ -void sha1_ctx2hash (sha1_hash_t *dest, sha1_ctx_t *state){ +void sha1_ctx2hash (void *dest, sha1_ctx_t *state){ #if defined LITTLE_ENDIAN uint8_t i; for(i=0; i<5; ++i){ @@ -225,7 +226,7 @@ void sha1_ctx2hash (sha1_hash_t *dest, sha1_ctx_t *state){ * * */ -void sha1 (sha1_hash_t *dest, const void* msg, uint32_t length){ +void sha1 (void *dest, const void *msg, uint32_t length){ sha1_ctx_t s; DEBUG_S("\r\nBLA BLUB"); sha1_init(&s);