From 8d79feec50a05a02fadb12e516c20674e698d5ae Mon Sep 17 00:00:00 2001 From: bg Date: Tue, 5 Aug 2008 21:19:55 +0000 Subject: [PATCH] noekeon optimized++ --- Doxyfile | 74 +++++++++++++-------------------------------- Makefile | 20 ++++++++----- grain.h | 12 ++++---- main.dox | 18 ++++++++++- memxor.c | 8 +++-- memxor.h | 2 +- noekeon_asm.S | 53 ++++++++++++++++++++++---------- sha256.h | 83 ++++++++++++++++++++++++++++++++++++++++++--------- 8 files changed, 170 insertions(+), 100 deletions(-) diff --git a/Doxyfile b/Doxyfile index 3eed9fc..527caaa 100644 --- a/Doxyfile +++ b/Doxyfile @@ -10,7 +10,7 @@ CREATE_SUBDIRS = NO OUTPUT_LANGUAGE = English USE_WINDOWS_ENCODING = NO BRIEF_MEMBER_DESC = YES -REPEAT_BRIEF = YES +REPEAT_BRIEF = NO ABBREVIATE_BRIEF = "The $name class" \ "The $name widget" \ "The $name file" \ @@ -22,7 +22,7 @@ ABBREVIATE_BRIEF = "The $name class" \ a \ an \ the -ALWAYS_DETAILED_SEC = NO +ALWAYS_DETAILED_SEC = YES INLINE_INHERITED_MEMB = NO FULL_PATH_NAMES = YES STRIP_FROM_PATH = /home/bg/workspace/micro-crypt/ @@ -30,11 +30,12 @@ STRIP_FROM_INC_PATH = SHORT_NAMES = NO JAVADOC_AUTOBRIEF = NO MULTILINE_CPP_IS_BRIEF = NO -DETAILS_AT_TOP = NO +DETAILS_AT_TOP = YES INHERIT_DOCS = YES SEPARATE_MEMBER_PAGES = NO TAB_SIZE = 8 -ALIASES = "license=\par License:\n" "email=\par E-Mail:\n" +ALIASES = "license=\par License:\n" \ + "email=\par E-Mail:\n" OPTIMIZE_OUTPUT_FOR_C = YES OPTIMIZE_OUTPUT_JAVA = NO BUILTIN_STL_SUPPORT = NO @@ -43,13 +44,13 @@ SUBGROUPING = YES #--------------------------------------------------------------------------- # Build related configuration options #--------------------------------------------------------------------------- -EXTRACT_ALL = YES -EXTRACT_PRIVATE = YES -EXTRACT_STATIC = YES +EXTRACT_ALL = NO +EXTRACT_PRIVATE = NO +EXTRACT_STATIC = NO EXTRACT_LOCAL_CLASSES = YES EXTRACT_LOCAL_METHODS = NO -HIDE_UNDOC_MEMBERS = NO -HIDE_UNDOC_CLASSES = NO +HIDE_UNDOC_MEMBERS = YES +HIDE_UNDOC_CLASSES = YES HIDE_FRIEND_COMPOUNDS = NO HIDE_IN_BODY_DOCS = NO INTERNAL_DOCS = NO @@ -83,47 +84,14 @@ WARN_LOGFILE = # configuration options related to the input files #--------------------------------------------------------------------------- INPUT = /home/bg/workspace/micro-crypt -FILE_PATTERNS = *.c \ - *.cc \ - *.cxx \ - *.cpp \ - *.c++ \ - *.d \ - *.java \ - *.ii \ - *.ixx \ - *.ipp \ - *.i++ \ - *.inl \ - *.h \ - *.hh \ - *.hxx \ - *.hpp \ - *.h++ \ - *.idl \ - *.odl \ - *.cs \ - *.php \ - *.php3 \ - *.inc \ - *.m \ - *.mm \ +FILE_PATTERNS = *.h \ *.dox \ - *.py \ - *.C \ - *.CC \ - *.C++ \ - *.II \ - *.I++ \ *.H \ *.HH \ *.H++ \ - *.CS \ - *.PHP \ - *.PHP3 \ - *.M \ - *.MM \ - *.PY + RECURSIVE \ + = \ + NO RECURSIVE = NO EXCLUDE = EXCLUDE_SYMLINKS = NO @@ -138,14 +106,14 @@ FILTER_SOURCE_FILES = NO #--------------------------------------------------------------------------- # configuration options related to source browsing #--------------------------------------------------------------------------- -SOURCE_BROWSER = YES +SOURCE_BROWSER = NO INLINE_SOURCES = NO STRIP_CODE_COMMENTS = YES -REFERENCED_BY_RELATION = YES -REFERENCES_RELATION = YES +REFERENCED_BY_RELATION = NO +REFERENCES_RELATION = NO REFERENCES_LINK_SOURCE = YES USE_HTAGS = NO -VERBATIM_HEADERS = YES +VERBATIM_HEADERS = NO #--------------------------------------------------------------------------- # configuration options related to the alphabetical class index #--------------------------------------------------------------------------- @@ -170,7 +138,7 @@ BINARY_TOC = NO TOC_EXPAND = NO DISABLE_INDEX = NO ENUM_VALUES_PER_LINE = 4 -GENERATE_TREEVIEW = NO +GENERATE_TREEVIEW = YES TREEVIEW_WIDTH = 250 #--------------------------------------------------------------------------- # configuration options related to the LaTeX output @@ -253,8 +221,8 @@ COLLABORATION_GRAPH = YES GROUP_GRAPHS = YES UML_LOOK = NO TEMPLATE_RELATIONS = NO -INCLUDE_GRAPH = YES -INCLUDED_BY_GRAPH = YES +INCLUDE_GRAPH = NO +INCLUDED_BY_GRAPH = NO CALL_GRAPH = NO CALLER_GRAPH = NO GRAPHICAL_HIERARCHY = NO diff --git a/Makefile b/Makefile index 00d1f5f..06b4475 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,7 @@ all: $(foreach algo, $(ALGORITHMS), $(algo)_OBJ) define MAIN_OBJ_TEMPLATE $(2): $(3) $(4) - @echo "[gcc]: $$@" + @echo "[ld]: $$@" # echo $$^ @$(CC) $(CFLAGS) $(LDFLAGS)$(patsubst %.elf,%.map,$(2)) -o \ $(2) \ @@ -207,25 +207,31 @@ docu: # Rules for building the .text rom images %.hex: %.elf - $(OBJCOPY) -j .text -j .data -O ihex $< $@ + @echo "[objcopy]: $@" + @$(OBJCOPY) -j .text -j .data -O ihex $< $@ %.srec: %.elf - $(OBJCOPY) -j .text -j .data -O srec $< $@ + @echo "[objcopy]: $@" + @$(OBJCOPY) -j .text -j .data -O srec $< $@ %.bin: %.elf - $(OBJCOPY) -j .text -j .data -O binary $< $@ + @echo "[objcopy]: $@" + @$(OBJCOPY) -j .text -j .data -O binary $< $@ # Rules for building the .eeprom rom images %_eeprom.hex: %.elf - $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + @echo "[objcopy]: $@" + @$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ %_eeprom.srec: %.elf - $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@ + @echo "[objcopy]: $@" + @$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@ %_eeprom.bin: %.elf - $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@ + @echo "[objcopy]: $@" + @$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@ # Every thing below here is used by avr-libc's build system and can be ignored diff --git a/grain.h b/grain.h index 508c1d1..c8a3d7f 100644 --- a/grain.h +++ b/grain.h @@ -16,12 +16,12 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -/** - * - * author: Daniel Otte - * email: daniel.otte@rub.de - * license: GPLv3 - * + +/** \file grain.h + * \author Daniel Otte + * \email daniel.otte@rub.de + * \license GPLv3 or later + * \brief implementation of the Grain streamcipher */ #ifndef GRAIN_H_ diff --git a/main.dox b/main.dox index 199a398..54db535 100644 --- a/main.dox +++ b/main.dox @@ -40,7 +40,11 @@ \subsection Hashes Hashes A Hash function produces a fixed length output (called hash value or message - digest) from a variable length message input. + digest) from a variable length message input. Most hash algorithms have a + state which is updated by the hash core function from a block of fixed size + from the message. A "lastBlock" function is used to add the last block which + is not required to have the fixed length. An addition function is then used + to transform the state into the final hash value. Implemented hash functions: - MD5 (md5.h) @@ -48,10 +52,22 @@ - SHA-256 (sha256.h) \subsection MACs Message-Authentification-Codes (MACs) + Message-Authentification-Codes work like hash function but take an additional + key. The produced output is also called MAC and is highly dependant on the + key. They can be thought of symmetric signatures. + + Implemented MAC functions: - HMAC-SHA-1 (hmac-sha1.h) - HMAC-SHA-256 (hmac-sha256.h) \subsection PRNGs Pseudo-Random-Number-Generators (PRNGs) + PRNGs produce pseudo random output determinated by the input. They aim to + produce an arbitrary length of good cryptographically secure random if the + input has enough entropy. PRNGs differ in the way they accept input. Some + are seeded only once, other (like the implemented) maintain an entropy pool + to which input can be added at virtually any time. + + Implemented PRNGs: - Entropium (entropium.h) */ \ No newline at end of file diff --git a/memxor.c b/memxor.c index 1fbde5a..7485b3e 100644 --- a/memxor.c +++ b/memxor.c @@ -1,8 +1,12 @@ #include -void memxor(void * dest, const void * src, uint16_t n){ +#include "memxor.h" + +void memxor(void* dest, const void* src, uint16_t n){ while(n--){ - ((uint8_t*)dest)[n] ^= ((uint8_t*)src)[n]; + *((uint8_t*)dest) ^= *((uint8_t*)src); + dest = (uint8_t*)dest +1; + src = (uint8_t*)src +1; } } diff --git a/memxor.h b/memxor.h index d84f335..a62a616 100644 --- a/memxor.h +++ b/memxor.h @@ -2,6 +2,6 @@ #define MEMXOR_H_ #include -void memxor(void * dest, const void * src, uint16_t n); +void memxor(void* dest, const void* src, uint16_t n); #endif diff --git a/noekeon_asm.S b/noekeon_asm.S index 8ebcc93..146cbb4 100644 --- a/noekeon_asm.S +++ b/noekeon_asm.S @@ -68,6 +68,18 @@ clr r1 .endm +push_all_func: + pop r31 + pop r30 + push_all + ijmp + +pop_all_func: + pop r31 + pop r30 + pop_all + ijmp + .macro xchg a b eor \a, \b eor \b, \a @@ -275,6 +287,7 @@ theta: ret /******************************************************************************/ +#ifndef NOEKEON_NO_ENC ; === noekeon_enc === ; ; param1: pointer to buffer/state (r24,r25) @@ -282,7 +295,7 @@ theta: ; .global noekeon_enc noekeon_enc: - push_all + rcall push_all_func /* load state */ movw r26, r22 ldi r28, 2 @@ -312,7 +325,7 @@ noekeon_enc: lpm r0, Z push r0 3: - call round /* pops rc2 & rc1 */ + rcall round /* pops rc2 & rc1 */ pop r22 dec r22 push r22 @@ -322,7 +335,7 @@ noekeon_enc: ldi r22, 0xD4 eor state0_3, r22 - call theta + rcall theta pop r31 pop r30 @@ -335,10 +348,13 @@ noekeon_enc: dec r22 brne 1b - pop_all + rcall pop_all_func ret +#endif /******************************************************************************/ /******************************************************************************/ +#ifndef NOEKEON_NO_DEC + ; === noekeon_dec === ; ; param1: pointer to buffer/state (r24,r25) @@ -346,7 +362,7 @@ noekeon_enc: ; .global noekeon_dec noekeon_dec: - push_all + rcall push_all_func /* allocate 16 bytes on the stack */ in r30, _SFR_IO_ADDR(SPL) in r31, _SFR_IO_ADDR(SPH) @@ -377,7 +393,7 @@ noekeon_dec: movw r26, r30 sbiw r26, 16 /* set X back to begining of stack key */ - call theta + rcall theta /* mov state to stackkey */ clr r29 @@ -419,7 +435,7 @@ noekeon_dec: push r0 push r1 3: - call round /* pops rc2 & rc1 */ + rcall round /* pops rc2 & rc1 */ pop r22 dec r22 push r22 @@ -427,7 +443,7 @@ noekeon_dec: ;---- pop r22 - call theta + rcall theta ldi r22, 0x80 eor state0_3, r22 @@ -450,16 +466,18 @@ write_state_back: adiw r30, 16 out _SFR_IO_ADDR(SPH), r31 out _SFR_IO_ADDR(SPL), r30 - pop_all + rcall pop_all_func ret +#endif /******************************************************************************/ + round: pop r24 pop r25 pop r1 eor state0_3, r1 - call theta + rcall theta pop r1 eor state0_3, r1 push r25 @@ -467,9 +485,9 @@ round: pi_gamma_pi: ldi r30, pm_lo8(bigendian_rotl32) ldi r31, pm_hi8(bigendian_rotl32) - call pi + rcall pi /* pi1 done; now gamma */ - call gamma_1 + rcall gamma_1 /* a[0] <-> a[3] */ xchg state0_0, state3_0 xchg state0_1, state3_1 @@ -480,10 +498,10 @@ pi_gamma_pi: op32 eor, state2, state1 op32 eor, state2, state3 - call gamma_1 + rcall gamma_1 ldi r30, pm_lo8(bigendian_rotr32) ldi r31, pm_hi8(bigendian_rotr32) - call pi + rcall pi ret gamma_1: @@ -573,6 +591,9 @@ void noekeon_init(void* key, noekeon_ctx_t* ctx){ noekeon_enc(ctx, nullv); } */ + +#ifndef NOEKEON_NO_INIT + .global noekeon_init noekeon_init: ; === noekeon_init === @@ -606,7 +627,7 @@ noekeon_init: sbiw r26, 16 movw r24, r26 - call noekeon_enc + rcall noekeon_enc in r30, _SFR_IO_ADDR(SPL) in r31, _SFR_IO_ADDR(SPH) @@ -615,6 +636,6 @@ noekeon_init: out _SFR_IO_ADDR(SPL), r30 ret - +#endif diff --git a/sha256.h b/sha256.h index ec1dac0..ec43488 100644 --- a/sha256.h +++ b/sha256.h @@ -17,11 +17,10 @@ along with this program. If not, see . */ /** - * \file sha256-asm.h - * \author Daniel Otte - * \date 2006-05-16 - * \par License - * GPL + * \file sha256.h + * \author Daniel Otte + * \date 2006-05-16 + * \license GPLv3 or later * */ @@ -33,35 +32,91 @@ #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) -/** - * \brief sha256 context type +/** \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 initialise 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); -void sha256_nextBlock (sha256_ctx_t *state, const void* block); -void sha256_lastBlock(sha256_ctx_t *state, const void* block, uint16_t length_b); +/** \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); -void sha256_ctx2hash(sha256_hash_t *dest, const sha256_ctx_t *state); +/** \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); -/* - * length in bits! +/** \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(sha256_hash_t *dest, const void* msg, uint32_t length_b); -uint32_t change_endian32(uint32_t x); +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 automaticaly 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_*/ -- 2.39.5