From 2aee09dcaf731b4086e7cdb865ee77b2715ce751 Mon Sep 17 00:00:00 2001 From: bg Date: Sun, 13 Apr 2008 03:05:55 +0000 Subject: [PATCH] +RC5 +size-statistics tool +small modification to nessie_bc_test (optional free() function) --- Makefile | 27 +- __gmsl | 837 ++++++++++++++++++++++++++++++++++++++++++++ avr-makefile.inc | 3 +- config.h | 2 +- gmsl | 82 +++++ main-noekeon-test.c | 14 +- main-rc5-test.c | 123 +++++++ nessie_bc_test.c | 12 +- nessie_bc_test.h | 10 +- noekeon_asm.S | 22 +- rc5.c | 80 +++++ rc5.h | 28 ++ sumsize.rb | 36 ++ 13 files changed, 1233 insertions(+), 43 deletions(-) create mode 100644 __gmsl create mode 100644 gmsl create mode 100644 main-rc5-test.c create mode 100644 rc5.c create mode 100644 rc5.h create mode 100644 sumsize.rb diff --git a/Makefile b/Makefile index 79b3338..153b7f3 100644 --- a/Makefile +++ b/Makefile @@ -24,13 +24,9 @@ ALGORITHMS_TEST_BIN_IMM = $(foreach a, $(ALGORITHMS_TEST_BIN), $($(a))) ALGORITHMS_NESSIE_TEST = $(patsubst %,%_NESSIE_TEST, $(ALGORITHMS)) ALGORITHMS_PERFORMANCE_TEST = $(patsubst %,%_PERORMANCE_TEST, $(ALGORITHMS)) -#ALGORITHMS_LC = #algorithm names in lowercase -#ALGORITHMS_LC = $(foreach a, $(ALGORITHMS), $$(lc Text)) ALGORITHMS_LC = $(call lc,$(ALGORITHMS)) -PRG = remove_me -DEFS = -LIBS = +PRG = remove_me define BLA_TEMPLATE2 $(2): $(3) @@ -71,11 +67,11 @@ info: # echo $(ALGORITHMS_PERFORMANCE_TEST) %.o: %.c - @echo "[gcc]: $@" + @echo "[gcc]: $@" @$(CC) $(CFLAGS) -c -o $@ $< %.o: %.S - @echo "[as] : $@" + @echo "[as] : $@" @$(CC) $(ASFLAGS) -c -o $@ $< @@ -94,13 +90,17 @@ hashes: $(patsubst %, %_OBJ, $(HASHES)) .PHONY: macs macs: $(patsubst %, %_OBJ, $(MACS)) +.PHONY: prngs prngs: $(patsubst %, %_OBJ, $(PRNGS)) tests: $(ALGORITHMS_TEST_BIN) \ $(ALGORITHMS_TEST_BIN_MAIN_ELF) \ $(ALGORITHMS_TEST_BIN_MAIN_HEX) - +.PHONY: stats +stats: $(patsubst %, %_size.txt, $(ALGORITHMS_LC)) + $(RUBY) sumsize.rb $^ > sizestats.txt + define OBJ_TEMPLATE $(1)_OBJ: $(2) # @echo " ALGO: $(1)" @@ -121,9 +121,8 @@ $(ALGORITHMS_TEST_BIN): $(ALGORITHMS_TEST_BIN_IMM) define SIZE_TEMPLATE $(1)_size.txt: $(2) - @echo " ALGO: $(1)" - @echo " REQ: $(2)" - $(SIZE) $(2) > $(1)_size.txt + @echo "[size]: $(1)_size.txt" + @$(SIZE) $(2) > $(1)_size.txt endef $(foreach algo, $(ALGORITHMS), $(eval $(call SIZE_TEMPLATE, $(call lc,$(algo)), $($(algo)_OBJ)))) @@ -136,7 +135,7 @@ all: $(PRG).elf lst text eeprom .PHONY: clean clean: - rm -rf *.o *.elf *.eps *.png *.pdf *.bak + rm -rf *.o *.elf *.eps *.png *.pdf *.bak *_size.txt rm -rf *.lst *.map $(EXTRA_CLEAN_FILES) flash: @@ -181,10 +180,6 @@ esrec: $(PRG)_eeprom.srec %_eeprom.bin: %.elf $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@ - -#%_size.txt: %.o -# $(SIZE) $< > $@ - # Every thing below here is used by avr-libc's build system and can be ignored diff --git a/__gmsl b/__gmsl new file mode 100644 index 0000000..96ae750 --- /dev/null +++ b/__gmsl @@ -0,0 +1,837 @@ +# ---------------------------------------------------------------------------- +# +# GNU Make Standard Library (GMSL) +# +# A library of functions to be used with GNU Make's $(call) that +# provides functionality not available in standard GNU Make. +# +# Copyright (c) 2005-2007 John Graham-Cumming +# +# This file is part of GMSL +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# Neither the name of the John Graham-Cumming nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# ---------------------------------------------------------------------------- + +# This is the GNU Make Standard Library version number as a list with +# three items: major, minor, revision + +gmsl_version := 1 0 9 + +# Used to output warnings and error from the library, it's possible to +# disable any warnings or errors by overriding these definitions +# manually or by setting GMSL_NO_WARNINGS or GMSL_NO_ERRORS + +__gmsl_name := GNU Make Standard Library +__gmsl_warning = $(warning $(__gmsl_name): $1) +__gmsl_error = $(error $(__gmsl_name): $1) + +ifdef GMSL_NO_WARNINGS +__gmsl_warning := +endif +ifdef GMSL_NO_ERRORS +__gmsl_error := +endif + +# If GMSL_TRACE is enabled then calls to the library functions are +# traced to stdout using warning messages with their arguments + +ifdef GMSL_TRACE +__gmsl_tr1 = $(warning $0('$1')) +__gmsl_tr2 = $(warning $0('$1','$2')) +__gmsl_tr3 = $(warning $0('$1','$2','$3')) +else +__gmsl_tr1 := +__gmsl_tr2 := +__gmsl_tr3 := +endif + +# Figure out whether we have $(eval) or not (GNU Make 3.80 and above) +# if we do not then output a warning message, if we do then some +# functions will be enabled. + +__gmsl_have_eval := $(false) +__gmsl_ignore := $(eval __gmsl_have_eval := $(true)) + +# See if we have $(lastword) (GNU Make 3.81 and above) + +__gmsl_have_lastword := $(lastword $(false) $(true)) + +# See if we have native or and and (GNU Make 3.81 and above) + +__gmsl_have_or := $(if $(filter-out undefined, \ + $(origin or)),$(call or,$(true),$(false))) +__gmsl_have_and := $(if $(filter-out undefined, \ + $(origin and)),$(call and,$(true),$(true))) + +ifneq ($(__gmsl_have_eval),$(true)) +$(call __gmsl_warning,GNU Make $(MAKE_VERSION) does not support $$(eval): some functions disabled) +endif + +# ---------------------------------------------------------------------------- +# Function: gmsl_compatible +# Arguments: List containing the desired library version number (maj min rev) +# Returns: $(true) if this version of the library is compatible +# with the requested version number, otherwise $(false) +# ---------------------------------------------------------------------------- +gmsl_compatible = $(strip \ + $(if $(call gt,$(word 1,$1),$(word 1,$(gmsl_version))), \ + $(false), \ + $(if $(call lt,$(word 1,$1),$(word 1,$(gmsl_version))), \ + $(true), \ + $(if $(call gt,$(word 2,$1),$(word 2,$(gmsl_version))), \ + $(false), \ + $(if $(call lt,$(word 2,$1),$(word 2,$(gmsl_version))), \ + $(true), \ + $(call lte,$(word 3,$1),$(word 3,$(gmsl_version)))))))) + +# ########################################################################### +# LOGICAL OPERATORS +# ########################################################################### + +# not is defined in gmsl + +# ---------------------------------------------------------------------------- +# Function: and +# Arguments: Two boolean values +# Returns: Returns $(true) if both of the booleans are true +# ---------------------------------------------------------------------------- +ifneq ($(__gmsl_have_and),$(true)) +and = $(__gmsl_tr2)$(if $1,$(if $2,$(true),$(false)),$(false)) +endif + +# ---------------------------------------------------------------------------- +# Function: or +# Arguments: Two boolean values +# Returns: Returns $(true) if either of the booleans is true +# ---------------------------------------------------------------------------- +ifneq ($(__gmsl_have_or),$(true)) +or = $(__gmsl_tr2)$(if $1$2,$(true),$(false)) +endif + +# ---------------------------------------------------------------------------- +# Function: xor +# Arguments: Two boolean values +# Returns: Returns $(true) if exactly one of the booleans is true +# ---------------------------------------------------------------------------- +xor = $(__gmsl_tr2)$(if $1,$(if $2,$(false),$(true)),$(if $2,$(true),$(false))) + +# ---------------------------------------------------------------------------- +# Function: nand +# Arguments: Two boolean values +# Returns: Returns value of 'not and' +# ---------------------------------------------------------------------------- +nand = $(__gmsl_tr2)$(if $1,$(if $2,$(false),$(true)),$(true)) + +# ---------------------------------------------------------------------------- +# Function: nor +# Arguments: Two boolean values +# Returns: Returns value of 'not or' +# ---------------------------------------------------------------------------- +nor = $(__gmsl_tr2)$(if $1$2,$(false),$(true)) + +# ---------------------------------------------------------------------------- +# Function: xnor +# Arguments: Two boolean values +# Returns: Returns value of 'not xor' +# ---------------------------------------------------------------------------- +xnor =$(__gmsl_tr2)$(if $1,$(if $2,$(true),$(false)),$(if $2,$(false),$(true))) + +# ########################################################################### +# LIST MANIPULATION FUNCTIONS +# ########################################################################### + +# ---------------------------------------------------------------------------- +# Function: first (same as LISP's car, or head) +# Arguments: 1: A list +# Returns: Returns the first element of a list +# ---------------------------------------------------------------------------- +first = $(__gmsl_tr1)$(firstword $1) + +# ---------------------------------------------------------------------------- +# Function: last +# Arguments: 1: A list +# Returns: Returns the last element of a list +# ---------------------------------------------------------------------------- +ifeq ($(__gmsl_have_lastword),$(true)) +last = $(__gmsl_tr1)$(lastword $1) +else +last = $(__gmsl_tr1)$(if $1,$(word $(words $1),$1)) +endif + +# ---------------------------------------------------------------------------- +# Function: rest (same as LISP's cdr, or tail) +# Arguments: 1: A list +# Returns: Returns the list with the first element removed +# ---------------------------------------------------------------------------- +rest = $(__gmsl_tr1)$(wordlist 2,$(words $1),$1) + +# ---------------------------------------------------------------------------- +# Function: chop +# Arguments: 1: A list +# Returns: Returns the list with the last element removed +# ---------------------------------------------------------------------------- +chop = $(__gmsl_tr1)$(wordlist 2,$(words $1),x $1) + +# ---------------------------------------------------------------------------- +# Function: map +# Arguments: 1: Name of function to $(call) for each element of list +# 2: List to iterate over calling the function in 1 +# Returns: The list after calling the function on each element +# ---------------------------------------------------------------------------- +map = $(__gmsl_tr2)$(strip $(foreach a,$2,$(call $1,$a))) + +# ---------------------------------------------------------------------------- +# Function: pairmap +# Arguments: 1: Name of function to $(call) for each pair of elements +# 2: List to iterate over calling the function in 1 +# 3: Second list to iterate over calling the function in 1 +# Returns: The list after calling the function on each pair of elements +# ---------------------------------------------------------------------------- +pairmap = $(strip $(__gmsl_tr3)\ + $(if $2$3,$(call $1,$(call first,$2),$(call first,$3)) \ + $(call pairmap,$1,$(call rest,$2),$(call rest,$3)))) + +# ---------------------------------------------------------------------------- +# Function: leq +# Arguments: 1: A list to compare against... +# 2: ...this list +# Returns: Returns $(true) if the two lists are identical +# ---------------------------------------------------------------------------- +leq = $(__gmsl_tr2)$(strip $(if $(call seq,$(words $1),$(words $2)), \ + $(call __gmsl_list_equal,$1,$2),$(false))) + +__gmsl_list_equal = $(if $(strip $1), \ + $(if $(call seq,$(call first,$1),$(call first,$2)), \ + $(call __gmsl_list_equal, \ + $(call rest,$1), \ + $(call rest,$2)), \ + $(false)), \ + $(true)) + +# ---------------------------------------------------------------------------- +# Function: lne +# Arguments: 1: A list to compare against... +# 2: ...this list +# Returns: Returns $(true) if the two lists are different +# ---------------------------------------------------------------------------- +lne = $(__gmsl_tr2)$(call not,$(call leq,$1,$2)) + +# ---------------------------------------------------------------------------- +# Function: reverse +# Arguments: 1: A list to reverse +# Returns: The list with its elements in reverse order +# ---------------------------------------------------------------------------- +reverse =$(__gmsl_tr1)$(strip $(if $1,$(call reverse,$(call rest,$1)) \ + $(call first,$1))) + +# ---------------------------------------------------------------------------- +# Function: uniq +# Arguments: 1: A list from which to remove repeated elements +# Returns: The list with duplicate elements removed without reordering +# ---------------------------------------------------------------------------- +uniq = $(strip $(__gmsl_tr1)$(if $1,$(call uniq,$(call chop,$1)) \ + $(if $(filter $(call last,$1),$(call chop,$1)),,$(call last,$1)))) + +# ---------------------------------------------------------------------------- +# Function: length +# Arguments: 1: A list +# Returns: The number of elements in the list +# ---------------------------------------------------------------------------- +length = $(__gmsl_tr1)$(words $1) + +# ########################################################################### +# STRING MANIPULATION FUNCTIONS +# ########################################################################### + +# Helper function that translates any GNU Make 'true' value (i.e. a +# non-empty string) to our $(true) + +__gmsl_make_bool = $(if $(strip $1),$(true),$(false)) + +# ---------------------------------------------------------------------------- +# Function: seq +# Arguments: 1: A string to compare against... +# 2: ...this string +# Returns: Returns $(true) if the two strings are identical +# ---------------------------------------------------------------------------- +seq = $(__gmsl_tr2)$(if $(filter-out xx,x$(subst $1,,$2)$(subst $2,,$1)x),$(false),$(true)) + +# ---------------------------------------------------------------------------- +# Function: sne +# Arguments: 1: A string to compare against... +# 2: ...this string +# Returns: Returns $(true) if the two strings are not the same +# ---------------------------------------------------------------------------- +sne = $(__gmsl_tr2)$(call not,$(call seq,$1,$2)) + +# ---------------------------------------------------------------------------- +# Function: split +# Arguments: 1: The character to split on +# 2: A string to split +# Returns: Splits a string into a list separated by spaces at the split +# character in the first argument +# ---------------------------------------------------------------------------- +split = $(__gmsl_tr2)$(strip $(subst $1, ,$2)) + +# ---------------------------------------------------------------------------- +# Function: merge +# Arguments: 1: The character to put between fields +# 2: A list to merge into a string +# Returns: Merges a list into a single string, list elements are separated +# by the character in the first argument +# ---------------------------------------------------------------------------- +merge = $(__gmsl_tr2)$(strip $(if $2, \ + $(if $(call seq,1,$(words $2)), \ + $2,$(call first,$2)$1$(call merge,$1,$(call rest,$2))))) + +ifdef __gmsl_have_eval +# ---------------------------------------------------------------------------- +# Function: tr +# Arguments: 1: The list of characters to translate from +# 2: The list of characters to translate to +# 3: The text to translate +# Returns: Returns the text after translating characters +# ---------------------------------------------------------------------------- +tr = $(__gmsl_tr3)$(strip $(eval __gmsl_t := $3) \ + $(foreach c, \ + $(join $(addsuffix :,$1),$2), \ + $(eval __gmsl_t := \ + $(subst $(word 1,$(subst :, ,$c)),$(word 2,$(subst :, ,$c)), \ + $(__gmsl_t))))$(__gmsl_t)) + +# Common character classes for use with the tr function. Each of +# these is actually a variable declaration and must be wrapped with +# $() or ${} to be used. + +[A-Z] := A B C D E F G H I J K L M N O P Q R S T U V W X Y Z # +[a-z] := a b c d e f g h i j k l m n o p q r s t u v w x y z # +[0-9] := 0 1 2 3 4 5 6 7 8 9 # +[A-F] := A B C D E F # + +# ---------------------------------------------------------------------------- +# Function: uc +# Arguments: 1: Text to upper case +# Returns: Returns the text in upper case +# ---------------------------------------------------------------------------- +uc = $(__gmsl_tr1)$(call tr,$([a-z]),$([A-Z]),$1) + +# ---------------------------------------------------------------------------- +# Function: lc +# Arguments: 1: Text to lower case +# Returns: Returns the text in lower case +# ---------------------------------------------------------------------------- +lc = $(__gmsl_tr1)$(call tr,$([A-Z]),$([a-z]),$1) + +# ---------------------------------------------------------------------------- +# Function: strlen +# Arguments: 1: A string +# Returns: Returns the length of the string +# ---------------------------------------------------------------------------- +__gmsl_characters := A B C D E F G H I J K L M N O P Q R S T U V W X Y Z +__gmsl_characters += a b c d e f g h i j k l m n o p q r s t u v w y z +__gmsl_characters += 0 1 2 3 4 5 6 7 8 9 +__gmsl_characters += ` ~ ! @ \# $$ % ^ & * ( ) - _ = + +__gmsl_characters += { } [ ] \ : ; ' " < > , . / ? | + +# Aside: if you read the above you might think that the lower-case +# letter x is missing, and that that's an error. It is missing, but +# it's not an error. __gmsl_characters is used by the strlen +# function. strlen works by transforming every character and space +# into the letter x and then counting the x's. Since there's no need +# to transform x into x I omitted it. + +# This results in __gmsl_space containing just a space + +__gmsl_space := +__gmsl_space += + +strlen = $(strip $(eval __temp := $(subst $(__gmsl_space),x,$1))$(foreach a,$(__gmsl_characters),$(eval __temp := $$(subst $$a,x,$(__temp))))$(eval __temp := $(subst x,x ,$(__temp)))$(words $(__temp))) + +# This results in __gmsl_newline containing just a newline + +define __gmsl_newline + + +endef + +# This results in __gmsl_tab containing a tab + +__gmsl_tab := # + +# ---------------------------------------------------------------------------- +# Function: substr +# Arguments: 1: A string +# 2: Start position (first character is 1) +# 3: End position (inclusive) +# Returns: A substring. +# Note: The string in $1 must not contain a § +# ---------------------------------------------------------------------------- + +substr = $(strip $(eval __temp := $$(subst $$(__gmsl_space),§ ,$$1))$(foreach a,$(__gmsl_characters),$(eval __temp := $$(subst $$a,$$a$$(__gmsl_space),$(__temp))))$(eval __temp := $(wordlist $2,$3,$(__temp))))$(subst §,$(__gmsl_space),$(subst $(__gmsl_space),,$(__temp))) + +endif # __gmsl_have_eval + +# ########################################################################### +# SET MANIPULATION FUNCTIONS +# ########################################################################### + +# Sets are represented by sorted, deduplicated lists. To create a set +# from a list use set_create, or start with the empty_set and +# set_insert individual elements + +# This is the empty set +empty_set := + +# ---------------------------------------------------------------------------- +# Function: set_create +# Arguments: 1: A list of set elements +# Returns: Returns the newly created set +# ---------------------------------------------------------------------------- +set_create = $(__gmsl_tr1)$(sort $1) + +# ---------------------------------------------------------------------------- +# Function: set_insert +# Arguments: 1: A single element to add to a set +# 2: A set +# Returns: Returns the set with the element added +# ---------------------------------------------------------------------------- +set_insert = $(__gmsl_tr2)$(sort $1 $2) + +# ---------------------------------------------------------------------------- +# Function: set_remove +# Arguments: 1: A single element to remove from a set +# 2: A set +# Returns: Returns the set with the element removed +# ---------------------------------------------------------------------------- +set_remove = $(__gmsl_tr2)$(filter-out $1,$2) + +# ---------------------------------------------------------------------------- +# Function: set_is_member +# Arguments: 1: A single element +# 2: A set +# Returns: Returns $(true) if the element is in the set +# ---------------------------------------------------------------------------- +set_is_member = $(if $(filter $1,$2),$(true),$(false)) + +# ---------------------------------------------------------------------------- +# Function: set_union +# Arguments: 1: A set +# 2: Another set +# Returns: Returns the union of the two sets +# ---------------------------------------------------------------------------- +set_union = $(sort $1 $2) + +# ---------------------------------------------------------------------------- +# Function: set_intersection +# Arguments: 1: A set +# 2: Another set +# Returns: Returns the intersection of the two sets +# ---------------------------------------------------------------------------- +set_intersection = $(filter $1,$2) + +# ---------------------------------------------------------------------------- +# Function: set_is_subset +# Arguments: 1: A set +# 2: Another set +# Returns: Returns $(true) if the first set is a subset of the second +# ---------------------------------------------------------------------------- +set_is_subset = $(call set_equal,$(call set_intersection,$1,$2),$1) + +# ---------------------------------------------------------------------------- +# Function: set_equal +# Arguments: 1: A set +# 2: Another set +# Returns: Returns $(true) if the two sets are identical +# ---------------------------------------------------------------------------- +set_equal = $(call seq,$1,$2) + +# ########################################################################### +# ARITHMETIC LIBRARY +# ########################################################################### + +# Integers a represented by lists with the equivalent number of x's. +# For example the number 4 is x x x x. The maximum integer that the +# library can handle as _input_ is __gmsl_input_int which is defined +# here as 65536 + +__gmsl_sixteen := x x x x x x x x x x x x x x x x +__gmsl_input_int := $(foreach a,$(__gmsl_sixteen), \ + $(foreach b,$(__gmsl_sixteen), \ + $(foreach c,$(__gmsl_sixteen), \ + $(__gmsl_sixteen))))) + +# ---------------------------------------------------------------------------- +# Function: int_decode +# Arguments: 1: A number of x's representation +# Returns: Returns the integer for human consumption that is represented +# by the string of x's +# ---------------------------------------------------------------------------- +int_decode = $(__gmsl_tr1)$(words $1) + +# ---------------------------------------------------------------------------- +# Function: int_encode +# Arguments: 1: A number in human-readable integer form +# Returns: Returns the integer encoded as a string of x's +# ---------------------------------------------------------------------------- +int_encode = $(__gmsl_tr1)$(wordlist 1,$1,$(__gmsl_input_int)) + +# The arithmetic library functions come in two forms: one form of each +# function takes integers as arguments and the other form takes the +# encoded form (x's created by a call to int_encode). For example, +# there are two plus functions: +# +# plus Called with integer arguments and returns an integer +# int_plus Called with encoded arguments and returns an encoded result +# +# plus will be slower than int_plus because its arguments and result +# have to be translated between the x's format and integers. If doing +# a complex calculation use the int_* forms with a single encoding of +# inputs and single decoding of the output. For simple calculations +# the direct forms can be used. + +# Helper function used to wrap an int_* function into a function that +# takes a pair of integers, perhaps a function and returns an integer +# result +__gmsl_int_wrap = $(call int_decode,$(call $1,$(call int_encode,$2),$(call int_encode,$3))) +__gmsl_int_wrap1 = $(call int_decode,$(call $1,$(call int_encode,$2))) +__gmsl_int_wrap2 = $(call $1,$(call int_encode,$2),$(call int_encode,$3)) + +# ---------------------------------------------------------------------------- +# Function: int_plus +# Arguments: 1: A number in x's representation +# 2: Another number in x's represntation +# Returns: Returns the sum of the two numbers in x's representation +# ---------------------------------------------------------------------------- +int_plus = $(strip $(__gmsl_tr2)$1 $2) + +# ---------------------------------------------------------------------------- +# Function: plus (wrapped version of int_plus) +# Arguments: 1: An integer +# 2: Another integer +# Returns: Returns the sum of the two integers +# ---------------------------------------------------------------------------- +plus = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_plus,$1,$2) + +# ---------------------------------------------------------------------------- +# Function: int_subtract +# Arguments: 1: A number in x's representation +# 2: Another number in x's represntation +# Returns: Returns the difference of the two numbers in x's representation, +# or outputs an error on a numeric underflow +# ---------------------------------------------------------------------------- +int_subtract = $(strip $(__gmsl_tr2)$(if $(call int_gte,$1,$2), \ + $(filter-out xx,$(join $1,$2)), \ + $(call __gmsl_warning,Subtraction underflow))) + +# ---------------------------------------------------------------------------- +# Function: subtract (wrapped version of int_subtract) +# Arguments: 1: An integer +# 2: Another integer +# Returns: Returns the difference of the two integers, +# or outputs an error on a numeric underflow +# ---------------------------------------------------------------------------- +subtract = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_subtract,$1,$2) + +# ---------------------------------------------------------------------------- +# Function: int_multiply +# Arguments: 1: A number in x's representation +# 2: Another number in x's represntation +# Returns: Returns the product of the two numbers in x's representation +# ---------------------------------------------------------------------------- +int_multiply = $(strip $(__gmsl_tr2)$(foreach a,$1,$2)) + +# ---------------------------------------------------------------------------- +# Function: multiply (wrapped version of int_multiply) +# Arguments: 1: An integer +# 2: Another integer +# Returns: Returns the product of the two integers +# ---------------------------------------------------------------------------- +multiply = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_multiply,$1,$2) + +# ---------------------------------------------------------------------------- +# Function: int_divide +# Arguments: 1: A number in x's representation +# 2: Another number in x's represntation +# Returns: Returns the result of integer division of argument 1 divided +# by argument 2 in x's representation +# ---------------------------------------------------------------------------- +int_divide = $(__gmsl_tr2)$(strip $(if $2, \ + $(if $(call int_gte,$1,$2), \ + x $(call int_divide,$(call int_subtract,$1,$2),$2),), \ + $(call __gmsl_error,Division by zero))) + +# ---------------------------------------------------------------------------- +# Function: divide (wrapped version of int_divide) +# Arguments: 1: An integer +# 2: Another integer +# Returns: Returns the integer division of the first argument by the second +# ---------------------------------------------------------------------------- +divide = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_divide,$1,$2) + +# ---------------------------------------------------------------------------- +# Function: int_max, int_min +# Arguments: 1: A number in x's representation +# 2: Another number in x's represntation +# Returns: Returns the maximum or minimum of its arguments in x's +# representation +# ---------------------------------------------------------------------------- +int_max = $(__gmsl_tr2)$(subst xx,x,$(join $1,$2)) +int_min = $(__gmsl_tr2)$(subst xx,x,$(filter xx,$(join $1,$2))) + +# ---------------------------------------------------------------------------- +# Function: max, min +# Arguments: 1: An integer +# 2: Another integer +# Returns: Returns the maximum or minimum of its integer arguments +# ---------------------------------------------------------------------------- +max = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_max,$1,$2) +min = $(__gmsl_tr2)$(call __gmsl_int_wrap,int_min,$1,$2) + +# ---------------------------------------------------------------------------- +# Function: int_gt, int_gte, int_lt, int_lte, int_eq, int_ne +# Arguments: Two x's representation numbers to be compared +# Returns: $(true) or $(false) +# +# int_gt First argument greater than second argument +# int_gte First argument greater than or equal to second argument +# int_lt First argument less than second argument +# int_lte First argument less than or equal to second argument +# int_eq First argument is numerically equal to the second argument +# int_ne First argument is not numerically equal to the second argument +# ---------------------------------------------------------------------------- +int_gt = $(__gmsl_tr2)$(call __gmsl_make_bool, \ + $(filter-out $(words $2), \ + $(words $(call int_max,$1,$2)))) +int_gte = $(__gmsl_tr2)$(call __gmsl_make_bool, \ + $(call int_gt,$1,$2)$(call int_eq,$1,$2)) +int_lt = $(__gmsl_tr2)$(call __gmsl_make_bool, \ + $(filter-out $(words $1), \ + $(words $(call int_max,$1,$2)))) +int_lte = $(__gmsl_tr2)$(call __gmsl_make_bool, \ + $(call int_lt,$1,$2)$(call int_eq,$1,$2)) +int_eq = $(__gmsl_tr2)$(call __gmsl_make_bool, \ + $(filter $(words $1),$(words $2))) +int_ne = $(__gmsl_tr2)$(call __gmsl_make_bool, \ + $(filter-out $(words $1),$(words $2))) + +# ---------------------------------------------------------------------------- +# Function: gt, gte, lt, lte, eq, ne +# Arguments: Two integers to be compared +# Returns: $(true) or $(false) +# +# gt First argument greater than second argument +# gte First argument greater than or equal to second argument +# lt First argument less than second argument +# lte First argument less than or equal to second argument +# eq First argument is numerically equal to the second argument +# ne First argument is not numerically equal to the second argument +# ---------------------------------------------------------------------------- +gt = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_gt,$1,$2) +gte = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_gte,$1,$2) +lt = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_lt,$1,$2) +lte = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_lte,$1,$2) +eq = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_eq,$1,$2) +ne = $(__gmsl_tr2)$(call __gmsl_int_wrap2,int_ne,$1,$2) + +# increment adds 1 to its argument, decrement subtracts 1. Note that +# decrement does not range check and hence will not underflow, but +# will incorrectly say that 0 - 1 = 0 + +# ---------------------------------------------------------------------------- +# Function: int_inc +# Arguments: 1: A number in x's representation +# Returns: The number incremented by 1 in x's representation +# ---------------------------------------------------------------------------- +int_inc = $(strip $(__gmsl_tr1)$1 x) + +# ---------------------------------------------------------------------------- +# Function: inc +# Arguments: 1: An integer +# Returns: The argument incremented by 1 +# ---------------------------------------------------------------------------- +inc = $(__gmsl_tr1)$(call __gmsl_int_wrap1,int_inc,$1) + +# ---------------------------------------------------------------------------- +# Function: int_dec +# Arguments: 1: A number in x's representation +# Returns: The number decremented by 1 in x's representation +# ---------------------------------------------------------------------------- +int_dec = $(__gmsl_tr1)$(strip $(if $(call sne,0,$(words $1)), \ + $(wordlist 2,$(words $1),$1), \ + $(call __gmsl_warning,Decrement underflow))) + +# ---------------------------------------------------------------------------- +# Function: dec +# Arguments: 1: An integer +# Returns: The argument decremented by 1 +# ---------------------------------------------------------------------------- +dec = $(__gmsl_tr1)$(call __gmsl_int_wrap1,int_dec,$1) + +# double doubles its argument, and halve halves it + +# ---------------------------------------------------------------------------- +# Function: int_double +# Arguments: 1: A number in x's representation +# Returns: The number doubled (i.e. * 2) and returned in x's representation +# ---------------------------------------------------------------------------- +int_double = $(strip $(__gmsl_tr1)$1 $1) + +# ---------------------------------------------------------------------------- +# Function: double +# Arguments: 1: An integer +# Returns: The integer times 2 +# ---------------------------------------------------------------------------- +double = $(__gmsl_tr1)$(call __gmsl_int_wrap1,int_double,$1) + +# ---------------------------------------------------------------------------- +# Function: int_halve +# Arguments: 1: A number in x's representation +# Returns: The number halved (i.e. / 2) and returned in x's representation +# ---------------------------------------------------------------------------- +int_halve = $(__gmsl_tr1)$(strip $(subst xx,x,$(filter-out xy x y, \ + $(join $1,$(foreach a,$1,y x))))) + +# ---------------------------------------------------------------------------- +# Function: halve +# Arguments: 1: An integer +# Returns: The integer divided by 2 +# ---------------------------------------------------------------------------- +halve = $(__gmsl_tr1)$(call __gmsl_int_wrap1,int_halve,$1) + +ifdef __gmsl_have_eval +# ########################################################################### +# ASSOCIATIVE ARRAYS +# ########################################################################### + +# ---------------------------------------------------------------------------- +# Function: set +# Arguments: 1: Name of associative array +# 2: The key value to associate +# 3: The value associated with the key +# Returns: None +# ---------------------------------------------------------------------------- +set = $(__gmsl_tr3)$(eval __gmsl_aa_$1_$2 = $3) + +# ---------------------------------------------------------------------------- +# Function: get +# Arguments: 1: Name of associative array +# 2: The key to retrieve +# Returns: The value stored in the array for that key +# ---------------------------------------------------------------------------- +get = $(strip $(__gmsl_tr2)$(if $(filter-out undefined,$(origin __gmsl_aa_$1_$2)), \ + $(__gmsl_aa_$1_$2))) + +# ---------------------------------------------------------------------------- +# Function: keys +# Arguments: 1: Name of associative array +# Returns: Returns a list of all defined keys in the array +# ---------------------------------------------------------------------------- +keys = $(__gmsl_tr1)$(sort $(patsubst __gmsl_aa_$1_%,%, \ + $(filter __gmsl_aa_$1_%,$(.VARIABLES)))) + +# ---------------------------------------------------------------------------- +# Function: defined +# Arguments: 1: Name of associative array +# 2: The key to test +# Returns: Returns true if the key is defined (i.e. not empty) +# ---------------------------------------------------------------------------- +defined = $(__gmsl_tr2)$(call sne,$(call get,$1,$2),) + +endif # __gmsl_have_eval + +ifdef __gmsl_have_eval +# ########################################################################### +# NAMED STACKS +# ########################################################################### + +# ---------------------------------------------------------------------------- +# Function: push +# Arguments: 1: Name of stack +# 2: Value to push onto the top of the stack (must not contain +# a space) +# Returns: None +# ---------------------------------------------------------------------------- +push = $(__gmsl_tr2)$(eval __gmsl_stack_$1 := $2 $(if $(filter-out undefined,\ + $(origin __gmsl_stack_$1)),$(__gmsl_stack_$1))) + +# ---------------------------------------------------------------------------- +# Function: pop +# Arguments: 1: Name of stack +# Returns: Top element from the stack after removing it +# ---------------------------------------------------------------------------- +pop = $(__gmsl_tr1)$(strip $(if $(filter-out undefined,$(origin __gmsl_stack_$1)), \ + $(call first,$(__gmsl_stack_$1)) \ + $(eval __gmsl_stack_$1 := $(call rest,$(__gmsl_stack_$1))))) + +# ---------------------------------------------------------------------------- +# Function: peek +# Arguments: 1: Name of stack +# Returns: Top element from the stack without removing it +# ---------------------------------------------------------------------------- +peek = $(__gmsl_tr1)$(call first,$(__gmsl_stack_$1)) + +# ---------------------------------------------------------------------------- +# Function: depth +# Arguments: 1: Name of stack +# Returns: Number of items on the stack +# ---------------------------------------------------------------------------- +depth = $(__gmsl_tr1)$(words $(__gmsl_stack_$1)) + +endif # __gmsl_have_eval + +# ########################################################################### +# DEBUGGING FACILITIES +# ########################################################################### + +# ---------------------------------------------------------------------------- +# Target: gmsl-print-% +# Arguments: The % should be replaced by the name of a variable that you +# wish to print out. +# Action: Echos the name of the variable that matches the % and its value. +# For example, 'make gmsl-print-SHELL' will output the value of +# the SHELL variable +# ---------------------------------------------------------------------------- +gmsl-print-%: ; @echo $* = $($*) + +# ---------------------------------------------------------------------------- +# Function: assert +# Arguments: 1: A boolean that must be true or the assertion will fail +# 2: The message to print with the assertion +# Returns: None +# ---------------------------------------------------------------------------- +assert = $(if $1,,$(call __gmsl_error,Assertion failure: $2)) + +# ---------------------------------------------------------------------------- +# Function: assert_exists +# Arguments: 1: Name of file that must exist, if it is missing an assertion +# will be generated +# Returns: None +# ---------------------------------------------------------------------------- +assert_exists = $(call assert,$(wildcard $1),file '$1' missing) diff --git a/avr-makefile.inc b/avr-makefile.inc index dfc3474..f760d4d 100644 --- a/avr-makefile.inc +++ b/avr-makefile.inc @@ -2,7 +2,7 @@ OBJ = $(SERPENT_OBJ) MCU_TARGET = atmega644 OPTIMIZE = -Os -DEFS = -DATMEGA644 +DEFS = -D$(call uc, $(MCU_TARGET)) FLASHCMD = avrdude -p $(MCU_TARGET) -P /dev/ttyUSB0 -c avr911 -U flash:w:$(PRG).hex # -U eeprom:w:$(PRG)_eeprom.hex #uisp -dprog=bsd -dlpt=/dev/parport1 --upload if=$(PRG).hex @@ -18,3 +18,4 @@ override ASFLAGS = -mmcu=$(MCU_TARGET) OBJCOPY = avr-objcopy OBJDUMP = avr-objdump SIZE = avr-size +RUBY = ruby \ No newline at end of file diff --git a/config.h b/config.h index 09cbf61..9ebb444 100644 --- a/config.h +++ b/config.h @@ -23,7 +23,7 @@ #define UART_CTS_BIT 1 */ -#define ATMEGA644 +//#define ATMEGA644 /* this is now done by make */ #endif diff --git a/gmsl b/gmsl new file mode 100644 index 0000000..450f523 --- /dev/null +++ b/gmsl @@ -0,0 +1,82 @@ +# ---------------------------------------------------------------------------- +# +# GNU Make Standard Library (GMSL) +# +# A library of functions to be used with GNU Make's $(call) that +# provides functionality not available in standard GNU Make. +# +# Copyright (c) 2005-2007 John Graham-Cumming +# +# This file is part of GMSL +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# Neither the name of the John Graham-Cumming nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +# ---------------------------------------------------------------------------- + +# Determine if the library has already been included and if so don't +# bother including it again + +ifndef __gmsl_included + +# Standard definitions for true and false. true is any non-empty +# string, false is an empty string. These are intended for use with +# $(if). + +true := T +false := + +# ---------------------------------------------------------------------------- +# Function: not +# Arguments: 1: A boolean value +# Returns: Returns the opposite of the arg. (true -> false, false -> true) +# ---------------------------------------------------------------------------- +not = $(if $1,$(false),$(true)) + +# Prevent reinclusion of the library + +__gmsl_included := $(true) + +# Try to determine where this file is located. If the caller did +# include /foo/gmsl then extract the /foo/ so that __gmsl gets +# included transparently + +__gmsl_root := $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)) + +# If there are any spaces in the path in __gmsl_root then give up + +ifeq (1,$(words $(__gmsl_root))) +__gmsl_root := $(patsubst %gmsl,%,$(__gmsl_root)) +else +__gmsl_root := +endif + +include $(__gmsl_root)__gmsl + +endif # __gmsl_included + diff --git a/main-noekeon-test.c b/main-noekeon-test.c index 43eb3a9..2c19de4 100644 --- a/main-noekeon-test.c +++ b/main-noekeon-test.c @@ -152,7 +152,7 @@ void testrun_stdtest_noekeon(void){ void testrun_performance_noekeon(void){ uint16_t i,c; uint64_t t; - char str[6]; + char str[16]; uint8_t key[16], data[16]; noekeon_ctx_t ctx; @@ -173,21 +173,27 @@ void testrun_performance_noekeon(void){ noekeon_init(key, &ctx); t = stopTimer(); uart_putstr_P(PSTR("\r\n\tctx-gen time: ")); - uart_hexdump(&t, 8); + ultoa((unsigned long)t, str, 10); + uart_putstr(str); +// uart_hexdump(&t, 8); startTimer(1); noekeon_enc(data, ctx); t = stopTimer(); uart_putstr_P(PSTR("\r\n\tencrypt time: ")); - uart_hexdump(&t, 8); + ultoa((unsigned long)t, str, 10); + uart_putstr(str); +// uart_hexdump(&t, 8); startTimer(1); noekeon_dec(data, ctx); t = stopTimer(); uart_putstr_P(PSTR("\r\n\tdecrypt time: ")); - uart_hexdump(&t, 8); + ultoa((unsigned long)t, str, 10); + uart_putstr(str); +// uart_hexdump(&t, 8); uart_putstr_P(PSTR("\r\n")); } /***************************************************************************** diff --git a/main-rc5-test.c b/main-rc5-test.c new file mode 100644 index 0000000..f3258b5 --- /dev/null +++ b/main-rc5-test.c @@ -0,0 +1,123 @@ +/* + * rc5 test-suit + * +*/ + +#include "config.h" +#include "serial-tools.h" +#include "uart.h" +#include "debug.h" + +#include "rc5.h" +#include "nessie_bc_test.h" +#include "cli.h" +#include "performance_test.h" + +#include +#include +#include + +#define RC5_ROUNDS 12 +char* cipher_name = "RC5-32/12/16"; + +/***************************************************************************** + * additional validation-functions * + *****************************************************************************/ +void rc5_genctx_dummy(uint8_t* key, uint16_t keysize_b, void* ctx){ + rc5_init(key, keysize_b, RC5_ROUNDS, ctx); +} + +void testrun_nessie_rc5(void){ + nessie_bc_init(); + nessie_bc_ctx.blocksize_B = 8; + nessie_bc_ctx.keysize_b = 128; + nessie_bc_ctx.name = cipher_name; + nessie_bc_ctx.ctx_size_B = sizeof(rc5_ctx_t); + nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)rc5_enc; + nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)rc5_dec; + nessie_bc_ctx.cipher_free = (nessie_bc_free_fpt)rc5_free; + nessie_bc_ctx.cipher_genctx = (nessie_bc_gen_fpt)rc5_genctx_dummy; + + nessie_bc_run(); +} + + +void testrun_performance_rc5(void){ + uint16_t i,c; + uint64_t t; + char str[16]; + uint8_t key[16], data[16]; + rc5_ctx_t ctx; + + calibrateTimer(); + getOverhead(&c, &i); + uart_putstr_P(PSTR("\r\n\r\n=== benchmark ===")); + utoa(c, str, 10); + uart_putstr_P(PSTR("\r\n\tconst overhead: ")); + uart_putstr(str); + utoa(i, str, 10); + uart_putstr_P(PSTR("\r\n\tinterrupt overhead: ")); + uart_putstr(str); + + memset(key, 0, 16); + memset(data, 0, 16); + + startTimer(1); + rc5_init(key, 128, RC5_ROUNDS, &ctx); + t = stopTimer(); + uart_putstr_P(PSTR("\r\n\tctx-gen time: ")); + ultoa((unsigned long)t, str, 10); + uart_putstr(str); + + startTimer(1); + rc5_enc(data, &ctx); + t = stopTimer(); + uart_putstr_P(PSTR("\r\n\tencrypt time: ")); + ultoa((unsigned long)t, str, 10); + uart_putstr(str); + + startTimer(1); + rc5_dec(data, &ctx); + t = stopTimer(); + uart_putstr_P(PSTR("\r\n\tdecrypt time: ")); + ultoa((unsigned long)t, str, 10); + uart_putstr(str); + + startTimer(1); + rc5_free(&ctx); + t = stopTimer(); + uart_putstr_P(PSTR("\r\n\tfree time: ")); + ultoa((unsigned long)t, str, 10); + uart_putstr(str); + uart_putstr_P(PSTR("\r\n")); +} +/***************************************************************************** + * main * + *****************************************************************************/ + +typedef void(*void_fpt)(void); + +int main (void){ + char str[20]; + DEBUG_INIT(); + uart_putstr("\r\n"); + + uart_putstr_P(PSTR("\r\n\r\nCrypto-VS (")); + uart_putstr(cipher_name); + uart_putstr_P(PSTR(")\r\nloaded and running\r\n")); + + PGM_P u = PSTR("nessie\0test\0performance\0"); + void_fpt v[] = {testrun_nessie_rc5, testrun_nessie_rc5, testrun_performance_rc5}; + + while(1){ + if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;} + if(execcommand_d0_P(str, u, v)<0){ + uart_putstr_P(PSTR("\r\nunknown command\r\n")); + } + continue; + error: + uart_putstr("ERROR\r\n"); + } + +} + diff --git a/nessie_bc_test.c b/nessie_bc_test.c index b6256d4..11deea7 100644 --- a/nessie_bc_test.c +++ b/nessie_bc_test.c @@ -15,6 +15,15 @@ nessie_bc_ctx_t nessie_bc_ctx; +void nessie_bc_init(void){ + memset(&nessie_bc_ctx, 0, sizeof(nessie_bc_ctx_t)); +} +static +void nessie_bc_free(void* ctx){ + if(nessie_bc_ctx.cipher_free) + nessie_bc_ctx.cipher_free(ctx); +} + void nessie_bc_enc(uint8_t* key, uint8_t* pt){ uint8_t ctx[nessie_bc_ctx.ctx_size_B]; uint8_t buffer[nessie_bc_ctx.blocksize_B]; @@ -43,6 +52,7 @@ void nessie_bc_enc(uint8_t* key, uint8_t* pt){ } nessie_print_item("Iterated 1000 times", buffer, nessie_bc_ctx.blocksize_B); #endif + nessie_bc_free(ctx); } void nessie_bc_dec(uint8_t* key, uint8_t* ct){ @@ -58,7 +68,7 @@ void nessie_bc_dec(uint8_t* key, uint8_t* ct){ nessie_print_item("plain", buffer, nessie_bc_ctx.blocksize_B); nessie_bc_ctx.cipher_enc(buffer, ctx); nessie_print_item("encrypted", buffer, nessie_bc_ctx.blocksize_B); - + nessie_bc_free(ctx); } void nessie_bc_run(void){ diff --git a/nessie_bc_test.h b/nessie_bc_test.h index 099a7e2..959bbe2 100644 --- a/nessie_bc_test.h +++ b/nessie_bc_test.h @@ -4,6 +4,7 @@ #include typedef void (*nessie_bc_gen_fpt)(uint8_t* key, uint16_t keysize_b, void* ctx); +typedef void (*nessie_bc_free_fpt)(void* ctx); typedef void (*nessie_bc_enc_fpt)(void* buffer, void* ctx); typedef void (*nessie_bc_dec_fpt)(void* buffer, void* ctx); @@ -12,14 +13,17 @@ typedef struct nessie_bc_ctx_st{ uint16_t blocksize_B; uint16_t ctx_size_B; char* name; - nessie_bc_gen_fpt cipher_genctx; - nessie_bc_enc_fpt cipher_enc; - nessie_bc_dec_fpt cipher_dec; + nessie_bc_gen_fpt cipher_genctx; + nessie_bc_free_fpt cipher_free; + nessie_bc_enc_fpt cipher_enc; + nessie_bc_dec_fpt cipher_dec; } nessie_bc_ctx_t; extern nessie_bc_ctx_t nessie_bc_ctx; void nessie_bc_run(void); +void nessie_bc_init(void); + #endif /*NESSIE_BC_TEST_H_*/ diff --git a/noekeon_asm.S b/noekeon_asm.S index 43ffa93..d880296 100644 --- a/noekeon_asm.S +++ b/noekeon_asm.S @@ -83,7 +83,6 @@ \op \p\()\d, \q\()\z .endm -.global bigendian_rotl32 ; === bigendian_rotl32 === ; this function rotates a 32bit bigendian word n bits to the left ; param1: the 32-bit value @@ -94,7 +93,6 @@ ; given in r25,r24,r23,r22 bigendian_rotl32: - in r0, _SFR_IO_ADDR(SREG) /* copy high bit of r22 to carry */ mov r1, r22 2: @@ -109,13 +107,11 @@ bigendian_rotl32: brne 2b bigendian_rotl32_exit: clr r1 - out _SFR_IO_ADDR(SREG), r0 ret /******************************************************************************/ -.global bigendian_rotr32 ; === bigendian_rotl32 === ; this function rotates a 32bit bigendian word n bits to the right ; param1: the 32-bit value @@ -126,7 +122,6 @@ bigendian_rotl32_exit: ; given in r25,r24,r23,r22 bigendian_rotr32: - in r0, _SFR_IO_ADDR(SREG) /* copy high bit of r25 to carry */ mov r1, r25 @@ -141,7 +136,6 @@ bigendian_rotr32: brne 2b bigendian_rotr32_exit: clr r1 - out _SFR_IO_ADDR(SREG), r0 ret /******************************************************************************/ @@ -457,7 +451,8 @@ round: push r25 push r24 pi_gamma_pi: - clc + ldi r30, lo8(bigendian_rotl32) + ldi r31, hi8(bigendian_rotl32) call pi /* pi1 done; now gamma */ call gamma_1 @@ -485,7 +480,8 @@ pi_gamma_pi: eor state2_3, state3_3 */ call gamma_1 - sec + ldi r30, lo8(bigendian_rotr32) + ldi r31, hi8(bigendian_rotr32) call pi ret @@ -529,15 +525,7 @@ gamma_1: eor state0_3, r1 ret -pi: - brcs 1f - ldi r30, lo8(bigendian_rotl32) - ldi r31, hi8(bigendian_rotl32) - rjmp 2f -1: - ldi r30, lo8(bigendian_rotr32) - ldi r31, hi8(bigendian_rotr32) -2: +pi: lsr r31 ror r30 /* a[1] <<<= 1*/ diff --git a/rc5.c b/rc5.c new file mode 100644 index 0000000..5bfaaf3 --- /dev/null +++ b/rc5.c @@ -0,0 +1,80 @@ +/* rc5.c a C implementation of RC5 for AVR microcontrollers + * + * author: Daniel Otte + * email: daniel.otte@rub.de + * license: GPLv3 + * + * this implementation is limited to 64bit blocks and a maximum of 255 rounds + * + */ + +#include +#include /* malloc() & free() */ +#include /* memset() & memcpy() */ +#include "rc5.h" + + +#define A (((uint32_t*)buffer)[0]) +#define B (((uint32_t*)buffer)[1]) +#define ROTL32(v,n) (((v)<<(n))|((v)>>(32-(n)))) +#define ROTR32(v,n) (((v)>>(n))|((v)<<(32-(n)))) + +void rc5_enc(void* buffer, const rc5_ctx_t* ctx){ + uint8_t i; + A += ctx->s[0]; + B += ctx->s[1]; + for(i=0; irounds; ++i){ + A = ROTL32(A^B, B&31) + ctx->s[(i+1)*2+0]; + B = ROTL32(A^B, A&31) + ctx->s[(i+1)*2+1]; + } +} + +void rc5_dec(void* buffer, const rc5_ctx_t* ctx){ + uint8_t i; + for(i=ctx->rounds; i>0; --i){ + B = ROTR32(B - ctx->s[i*2+1], A&31) ^ A; + A = ROTR32(A - ctx->s[i*2+0], B&31) ^ B; + } + B -= ctx->s[1]; + A -= ctx->s[0]; +} +/* +P32 = 10110111111000010101000101100011 = b7e15163 +Q32 = 10011110001101110111100110111001 = 9e3779b9 +*/ +#define P32 0xb7e15163 +#define Q32 0x9e3779b9 + + +void rc5_init(void* key, uint16_t keysize_b, uint8_t rounds, rc5_ctx_t* ctx){ + uint16_t c,n,m,j,i,t; + uint32_t a,b,l[(keysize_b+31)/32]; + ctx->rounds = rounds; + t=2*(rounds+1); + c=(keysize_b+31)/32; + ctx->s = malloc(t*sizeof(uint32_t)); + + memset(l, 0, sizeof(uint32_t)*c); + memcpy(l, key, (keysize_b+7)/8); + + ctx->s[0] = P32; + for(i=1; is[i] = ctx->s[i-1] + Q32; + } + + m = ((t>c)?t:c)*3; + i=j=0; + a=b=0; + for(n=0; ns[i]=ROTL32(ctx->s[i]+a+b, 3); + b=l[j]=ROTL32(l[j]+a+b, (a+b)&31); + i=(i+1)%t; + j=(j+1)%c; + } +} + +void rc5_free(rc5_ctx_t* ctx){ + if(ctx->s) + free(ctx->s); +} + diff --git a/rc5.h b/rc5.h new file mode 100644 index 0000000..3ff2ed6 --- /dev/null +++ b/rc5.h @@ -0,0 +1,28 @@ +/* rc5.h a C implementation of RC5 for AVR microcontrollers + * + * author: Daniel Otte + * email: daniel.otte@rub.de + * license: GPLv3 + * + * this implementation is limited to 64bit blocks and a maximum of 255 rounds + * + */ +#ifndef RC5_H_ +#define RC5_H_ + + +#include +#include /* malloc() & free() */ +#include /* memset() & memcpy() */ + +typedef struct rc5_ctx_st { + uint8_t rounds; + uint32_t *s; +}rc5_ctx_t; + +void rc5_enc(void* buffer, const rc5_ctx_t* ctx); +void rc5_dec(void* buffer, const rc5_ctx_t* ctx); +void rc5_init(void* key, uint16_t keysize_b, uint8_t rounds, rc5_ctx_t* ctx); +void rc5_free(rc5_ctx_t* ctx); + +#endif /*RC5_H_*/ diff --git a/sumsize.rb b/sumsize.rb new file mode 100644 index 0000000..d20b872 --- /dev/null +++ b/sumsize.rb @@ -0,0 +1,36 @@ +#!/usr/local/bin/ruby + +def scanFile (filename) +bytes = 0 + +# text data bss dec hex filename +# 420 0 0 420 1a4 shabea.o +# 1600 0 0 1600 640 sha256-asm.o + + +file=File.open(filename) +name= /(.*)_size.txt/.match(filename)[1] +if (not name) + name=filename +end + +while line=file.gets do + m = /^\s*\d+\s+\d+\s+\d+\s+(\d+)\s+.*/.match(line) + if m + bytes += m[1].to_i + end +end + +#puts "#{name}:\t#{bytes} bytes" +printf("%6d bytes\t%s\n", bytes, name) + +end + + + +#======================================================== +# MAIN +#======================================================== + +ARGV.each do |x| scanFile(x) end + -- 2.39.5