sync between svn and bzr

This commit is contained in:
bg 2010-02-06 00:31:24 +00:00
parent 5120e1b9ad
commit a28cd65029
12 changed files with 7717 additions and 0 deletions

641
gmsl-tests Normal file
View File

@ -0,0 +1,641 @@
# ----------------------------------------------------------------------------
#
# GNU Make Standard Library (GMSL) Test Suite
#
# Test suite for the GMSL
#
# 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.
#
# ----------------------------------------------------------------------------
.PHONY: all
all:
@echo
@echo Test Summary
@echo ------------
@echo "$(call int_decode,$(passed)) tests passed; $(call int_decode,$(failed)) tests failed"
include gmsl
passed :=
failed :=
ECHO := /bin/echo
start_test = $(shell $(ECHO) -n "Testing '$1': " >&2)$(eval current_test := OK)
stop_test = $(shell $(ECHO) " $(current_test)" >&2)
test_pass = .$(eval passed := $(call int_inc,$(passed)))
test_fail = X$(eval failed := $(call int_inc,$(failed)))$(eval current_test := ERROR '$1' != '$2')
test_assert = $(if $(filter undefined,$(origin 2)),$(eval 2 :=))$(shell $(ECHO) -n $(if $(call seq,$1,$2),$(call test_pass,$1,$2),$(call test_fail,$1,$2)) >&2)
$(call start_test,not)
$(call test_assert,$(call not,$(true)),$(false))
$(call test_assert,$(call not,$(false)),$(true))
$(call stop_test)
$(call start_test,or)
$(call test_assert,$(call or,$(true),$(true)),$(true))
$(call test_assert,$(call or,$(true),$(false)),$(true))
$(call test_assert,$(call or,$(false),$(true)),$(true))
$(call test_assert,$(call or,$(false),$(false)),$(false))
$(call stop_test)
$(call start_test,and)
$(call test_assert,$(call and,$(true),$(true)),$(true))
$(call test_assert,$(call and,$(true),$(false)),$(false))
$(call test_assert,$(call and,$(false),$(true)),$(false))
$(call test_assert,$(call and,$(false),$(false)),$(false))
$(call stop_test)
$(call start_test,xor)
$(call test_assert,$(call xor,$(true),$(true)),$(false))
$(call test_assert,$(call xor,$(true),$(false)),$(true))
$(call test_assert,$(call xor,$(false),$(true)),$(true))
$(call test_assert,$(call xor,$(false),$(false)),$(false))
$(call stop_test)
$(call start_test,nand)
$(call test_assert,$(call nand,$(true),$(true)),$(false))
$(call test_assert,$(call nand,$(true),$(false)),$(true))
$(call test_assert,$(call nand,$(false),$(true)),$(true))
$(call test_assert,$(call nand,$(false),$(false)),$(true))
$(call stop_test)
$(call start_test,nor)
$(call test_assert,$(call nor,$(true),$(true)),$(false))
$(call test_assert,$(call nor,$(true),$(false)),$(false))
$(call test_assert,$(call nor,$(false),$(true)),$(false))
$(call test_assert,$(call nor,$(false),$(false)),$(true))
$(call stop_test)
$(call start_test,xnor)
$(call test_assert,$(call xnor,$(true),$(true)),$(true))
$(call test_assert,$(call xnor,$(true),$(false)),$(false))
$(call test_assert,$(call xnor,$(false),$(true)),$(false))
$(call test_assert,$(call xnor,$(false),$(false)),$(true))
$(call stop_test)
$(call start_test,first)
$(call test_assert,$(call first,1 2 3),1)
$(call test_assert,$(call first,1),1)
$(call test_assert,$(call first,),)
$(call stop_test)
$(call start_test,last)
$(call test_assert,$(call last,1 2 3),3)
$(call test_assert,$(call last,1),1)
$(call test_assert,$(call last,),)
$(call stop_test)
$(call start_test,rest)
$(call test_assert,$(call rest,1 2 3),2 3)
$(call test_assert,$(call rest,1),)
$(call test_assert,$(call rest,),)
$(call stop_test)
$(call start_test,chop)
$(call test_assert,$(call chop,1 2 3),1 2)
$(call test_assert,$(call chop,1 2 3 4),1 2 3)
$(call test_assert,$(call chop,1),)
$(call test_assert,$(call chop,),)
$(call stop_test)
$(call start_test,length)
$(call test_assert,$(call length,1 2 3),3)
$(call test_assert,$(call length,1 2 3 4),4)
$(call test_assert,$(call length,1),1)
$(call test_assert,$(call length,),0)
$(call stop_test)
$(call start_test,map)
$(call test_assert,$(call map,origin,__undefined map MAKE),undefined file default)
$(call test_assert,$(call map,origin,),)
$(call stop_test)
joinem = $1$2
$(call start_test,pairmap)
$(call test_assert,$(call pairmap,addsuffix,2 1 3,a b c),a2 b1 c3)
$(call test_assert,$(call pairmap,addprefix,2 1 3,a b c d),2a 1b 3c d)
$(call test_assert,$(call pairmap,addprefix,2 1 3 4,a b c),2a 1b 3c)
$(call test_assert,$(call pairmap,joinem,2 1 3 4,a b c),2a 1b 3c 4)
$(call stop_test)
$(call start_test,seq)
$(call test_assert,$(call seq,abc,abc),T)
$(call test_assert,$(call seq,x,),)
$(call test_assert,$(call seq,,x),)
$(call test_assert,$(call seq,x,x),T)
$(call test_assert,$(call seq,a%c,abc),)
$(call test_assert,$(call seq,abc,a%c),)
$(call test_assert,$(call seq,abc,ABC),)
$(call test_assert,$(call seq,abc,),)
$(call test_assert,$(call seq,,),T)
$(call test_assert,$(call seq,a b c,a b c),T)
$(call test_assert,$(call seq,aa% bb% cc,aa% bb% cc),T)
$(call test_assert,$(call seq,aa% bb% cc,aa% bb cc),)
$(call test_assert,$(call seq,aa% bb% cc,xx yy zz),)
$(call stop_test)
$(call start_test,sne)
$(call test_assert,$(call sne,abc,abc),)
$(call test_assert,$(call sne,x,),T)
$(call test_assert,$(call sne,,x),T)
$(call test_assert,$(call sne,x,x),)
$(call test_assert,$(call sne,abc,ABC),T)
$(call test_assert,$(call sne,abc,),T)
$(call test_assert,$(call sne,,),)
$(call test_assert,$(call sne,a b c,a b c),)
$(call test_assert,$(call sne,aa% bb% cc,aa% bb% cc),)
$(call test_assert,$(call sne,aa% bb% cc,aa% bb cc),T)
$(call stop_test)
$(call start_test,strlen)
$(call test_assert,$(call strlen,),0)
$(call test_assert,$(call strlen,a),1)
$(call test_assert,$(call strlen,a b),3)
$(call test_assert,$(call strlen,a ),2)
$(call test_assert,$(call strlen, a),2)
$(call test_assert,$(call strlen, ),2)
$(call test_assert,$(call strlen, ),3)
$(call test_assert,$(call strlen, ),4)
$(call stop_test)
$(call start_test,substr)
$(call test_assert,$(call substr,some string,1,1),s)
$(call test_assert,$(call substr,some string,1,2),so)
$(call test_assert,$(call substr,some string,1,3),som)
$(call test_assert,$(call substr,some string,1,4),some)
$(call test_assert,$(call substr,some string,1,5),some )
$(call test_assert,$(call substr,some string,1,6),some s)
$(call test_assert,$(call substr,some string,5,5), )
$(call test_assert,$(call substr,some string,5,6), s)
$(call test_assert,$(call substr,some string,5,7), st)
$(call test_assert,$(call substr,some string,5,8), str)
$(call test_assert,$(call substr,some string,1,100),some string)
$(call stop_test)
$(call start_test,lc)
$(call test_assert,$(call lc,The Quick Brown Fox),the quick brown fox)
$(call test_assert,$(call lc,the1 quick2 brown3 fox4),the1 quick2 brown3 fox4)
$(call test_assert,$(call lc,The_),the_)
$(call test_assert,$(call lc,),)
$(call stop_test)
$(call start_test,uc)
$(call test_assert,$(call uc,The Quick Brown Fox),THE QUICK BROWN FOX)
$(call test_assert,$(call uc,the1 quick2 brown3 fox4),THE1 QUICK2 BROWN3 FOX4)
$(call test_assert,$(call uc,The_),THE_)
$(call test_assert,$(call uc,),)
$(call stop_test)
$(call start_test,tr)
$(call test_assert,$(call tr,A B C,1 2 3,CAPITAL),31PIT1L)
$(call test_assert,$(call tr,a b c,1 2 3,CAPITAL),CAPITAL)
$(call test_assert,$(call tr,E L I,3 1 1,I AM ELITE),1 AM 311T3)
$(call stop_test)
$(call start_test,leq)
$(call test_assert,$(call leq,1 2 3,1 2 3),T)
$(call test_assert,$(call leq,1 2 3,1 2 3 4),)
$(call test_assert,$(call leq,1 2 3 4,1 2 3),)
$(call test_assert,$(call leq,1,1),T)
$(call test_assert,$(call leq,,),T)
$(call stop_test)
$(call start_test,lne)
$(call test_assert,$(call lne,1 2 3,1 2 3),)
$(call test_assert,$(call lne,1 2 3,1 2 3 4),T)
$(call test_assert,$(call lne,1 2 3 4,1 2 3),T)
$(call test_assert,$(call lne,1,1),)
$(call test_assert,$(call lne,,),)
$(call stop_test)
$(call start_test,empty_set)
$(call test_assert,$(empty_set),)
$(call test_assert,$(empty_set),$(call set_create,))
$(call stop_test)
$(call start_test,set_create)
$(call test_assert,$(call set_create,),)
$(call test_assert,$(call set_create,1 2 2 3),1 2 3)
$(call test_assert,$(call set_create,2 1 1 2 2 3),1 2 3)
$(call test_assert,$(call set_create,1),1)
$(call stop_test)
$(call start_test,set_insert)
$(call test_assert,$(call set_insert,1,$(empty_set)),1)
$(call test_assert,$(call set_insert,1,$(call set_create,1)),1)
$(call test_assert,$(call set_insert,1,$(call set_create,1 2)),1 2)
$(call test_assert,$(call set_insert,0,$(call set_create,1 2)),0 1 2)
$(call stop_test)
$(call start_test,set_remove)
$(call test_assert,$(call set_remove,1,$(empty_set)),$(empty_set))
$(call test_assert,$(call set_remove,1,$(call set_create,1 2)),2)
$(call test_assert,$(call set_remove,1,$(call set_create,1 11 2)),11 2)
$(call test_assert,$(call set_remove,0,$(call set_create,1 2)),1 2)
$(call stop_test)
$(call start_test,set_is_member)
$(call test_assert,$(call set_is_member,1,$(empty_set)),)
$(call test_assert,$(call set_is_member,1,$(call set_create,2 3)),)
$(call test_assert,$(call set_is_member,1,$(call set_create,1 2 3)),T)
$(call test_assert,$(call set_is_member,1,$(call set_create,1)),T)
$(call stop_test)
$(call start_test,set_union)
$(call test_assert,$(call set_union,,),)
$(call test_assert,$(call set_union,1 2,),1 2)
$(call test_assert,$(call set_union,,3 4),3 4)
$(call test_assert,$(call set_union,1 2,3 4),1 2 3 4)
$(call test_assert,$(call set_union,1 2 3,3 4 5),1 2 3 4 5)
$(call stop_test)
$(call start_test,set_intersection)
$(call test_assert,$(call set_intersection,,),)
$(call test_assert,$(call set_intersection,1 2,),)
$(call test_assert,$(call set_intersection,,3 4),)
$(call test_assert,$(call set_intersection,1 2,3 4),)
$(call test_assert,$(call set_intersection,1 2 3 4,3 4 5),3 4)
$(call stop_test)
$(call start_test,set_is_subset)
$(call test_assert,$(call set_is_subset,,),T)
$(call test_assert,$(call set_is_subset,1 2,),)
$(call test_assert,$(call set_is_subset,,3 4),T)
$(call test_assert,$(call set_is_subset,1 2,3 4),)
$(call test_assert,$(call set_is_subset,1 2,1 2 3 4 5),T)
$(call test_assert,$(call set_is_subset,1 2,1 2),T)
$(call test_assert,$(call set_is_subset,1 2,1 3 4 5),)
$(call stop_test)
$(call start_test,set_equal)
$(call test_assert,$(call set_equal,,),T)
$(call test_assert,$(call set_equal,1,),)
$(call test_assert,$(call set_equal,,1),)
$(call test_assert,$(call set_equal,1,1),T)
$(call test_assert,$(call set_equal,1 2,),)
$(call test_assert,$(call set_equal,,1 2),)
$(call test_assert,$(call set_equal,1 2,1 2 3),)
$(call stop_test)
$(call start_test,int_encode)
$(call test_assert,$(call int_encode,0),)
$(call test_assert,$(call int_encode,1),x)
$(call test_assert,$(call int_encode,2),x x)
$(call test_assert,$(call int_encode,10),x x x x x x x x x x)
$(call stop_test)
$(call start_test,int_decode)
$(call test_assert,$(call int_decode,),0)
$(call test_assert,$(call int_decode,x),1)
$(call test_assert,$(call int_decode,x x),2)
$(call test_assert,$(call int_decode,x x x x x x x x x x),10)
$(call stop_test)
$(call start_test,int_plus)
$(call test_assert,$(call int_plus,$(call int_encode,3),$(call int_encode,4)),$(call int_encode,7))
$(call test_assert,$(call int_plus,$(call int_encode,0),$(call int_encode,4)),$(call int_encode,4))
$(call test_assert,$(call int_plus,$(call int_encode,3),$(call int_encode,0)),$(call int_encode,3))
$(call test_assert,$(call int_plus,$(call int_encode,0),$(call int_encode,0)),$(call int_encode,0))
$(call test_assert,$(call int_plus,$(call int_encode,1),$(call int_encode,0)),$(call int_encode,1))
$(call stop_test)
$(call start_test,plus)
$(call test_assert,$(call plus,3,4),7)
$(call test_assert,$(call plus,4,3),7)
$(call test_assert,$(call plus,0,4),4)
$(call test_assert,$(call plus,3,0),3)
$(call test_assert,$(call plus,0,0),0)
$(call stop_test)
__gmsl_warning = $1
$(call start_test,int_subtract)
$(call test_assert,$(call int_subtract,$(call int_encode,3),$(call int_encode,4)),Subtraction underflow)
$(call test_assert,$(call int_subtract,$(call int_encode,4),$(call int_encode,3)),$(call int_encode,1))
$(call test_assert,$(call int_subtract,$(call int_encode,3),$(call int_encode,0)),$(call int_encode,3))
$(call test_assert,$(call int_subtract,$(call int_encode,0),$(call int_encode,0)),$(call int_encode,0))
$(call test_assert,$(call int_subtract,$(call int_encode,1),$(call int_encode,0)),$(call int_encode,1))
$(call stop_test)
__gmsl_warning = x x x x x x x x x x
$(call start_test,subtract)
$(call test_assert,$(call subtract,3,4),10)
$(call test_assert,$(call subtract,4,3),1)
$(call test_assert,$(call subtract,3,0),3)
$(call test_assert,$(call subtract,0,0),0)
$(call stop_test)
$(call start_test,int_multiply)
$(call test_assert,$(call int_multiply,$(call int_encode,3),$(call int_encode,4)),$(call int_encode,12))
$(call test_assert,$(call int_multiply,$(call int_encode,4),$(call int_encode,3)),$(call int_encode,12))
$(call test_assert,$(call int_multiply,$(call int_encode,3),$(call int_encode,0)),$(call int_encode,0))
$(call test_assert,$(call int_multiply,$(call int_encode,0),$(call int_encode,0)),$(call int_encode,0))
$(call test_assert,$(call int_multiply,$(call int_encode,1),$(call int_encode,0)),$(call int_encode,0))
$(call stop_test)
$(call start_test,multiply)
$(call test_assert,$(call multiply,3,4),12)
$(call test_assert,$(call multiply,4,3),12)
$(call test_assert,$(call multiply,3,0),0)
$(call test_assert,$(call multiply,0,3),0)
$(call test_assert,$(call multiply,0,0),0)
$(call stop_test)
__gmsl_error = $1
$(call start_test,int_divide)
$(call test_assert,$(call int_divide,$(call int_encode,3),$(call int_encode,4)),$(call int_encode,0))
$(call test_assert,$(call int_divide,$(call int_encode,4),$(call int_encode,3)),$(call int_encode,1))
$(call test_assert,$(call int_divide,$(call int_encode,31),$(call int_encode,3)),$(call int_encode,10))
$(call test_assert,$(call int_divide,$(call int_encode,30),$(call int_encode,3)),$(call int_encode,10))
$(call test_assert,$(call int_divide,$(call int_encode,29),$(call int_encode,3)),$(call int_encode,9))
$(call test_assert,$(call int_divide,$(call int_encode,0),$(call int_encode,1)),$(call int_encode,0))
$(call test_assert,$(call int_divide,$(call int_encode,1),$(call int_encode,0)),Division by zero)
$(call stop_test)
__gmsl_error = x x x x x x x x x x
$(call start_test,divide)
$(call test_assert,$(call divide,3,4),0)
$(call test_assert,$(call divide,4,3),1)
$(call test_assert,$(call divide,21,2),10)
$(call test_assert,$(call divide,20,2),10)
$(call test_assert,$(call divide,19,2),9)
$(call test_assert,$(call divide,1,0),10)
$(call stop_test)
$(call start_test,associative array)
$(call test_assert,$(call get,myarray,key1),)
$(call set,myarray,key1,value1)
$(call test_assert,$(call get,myarray,key1),value1)
$(call test_assert,$(call get,myarray,key2),)
$(call test_assert,$(call get,myarray1,key1),)
$(call test_assert,$(call defined,myarray,key1),T)
$(call test_assert,$(call defined,myarray,key2),)
$(call test_assert,$(call defined,myarray1,key1),)
$(call set,myarray,key2,value2)
$(call test_assert,$(call keys,myarray),key1 key2)
$(call test_assert,$(call keys,myarray1),)
$(call stop_test)
$(call start_test,named stack)
$(call test_assert,$(call pop,mystack),)
$(call test_assert,$(call push,mystack,e2))
$(call push,mystack,e1)
$(call test_assert,$(call pop,mystack),e1)
$(call test_assert,$(call pop,mystack),e2)
$(call push,mystack,f3)
$(call push,mystack,f1)
$(call test_assert,$(call pop,mystack),f1)
$(call push,mystack,f2)
$(call test_assert,$(call peek,mystack),f2)
$(call test_assert,$(call depth,mystack),2)
$(call test_assert,$(call pop,mystack),f2)
$(call test_assert,$(call depth,mystack),1)
$(call test_assert,$(call pop,myotherstack),)
$(call stop_test)
$(call start_test,reverse)
$(call test_assert,$(call reverse,),)
$(call test_assert,$(call reverse,1),1)
$(call test_assert,$(call reverse,1 2),2 1)
$(call test_assert,$(call reverse,1 2 3),3 2 1)
$(call stop_test)
$(call start_test,uniq)
$(call test_assert,$(call uniq,),)
$(call test_assert,$(call uniq,a),a)
$(call test_assert,$(call uniq,a a),a)
$(call test_assert,$(call uniq,a aa),a aa)
$(call test_assert,$(call uniq,a aa a),a aa)
$(call test_assert,$(call uniq,a b ba ab b a a ba a),a b ba ab)
$(call stop_test)
c:=,
$(call start_test,split)
$(call test_assert,$(call split,$c,comma$cseparated$cstring),comma separated string)
$(call test_assert,$(call split,*,star*field*record),star field record)
$(call test_assert,$(call split,*,star*),star)
$(call test_assert,$(call split,*,star*field),star field)
$(call test_assert,$(call split,*,star****field),star field)
$(call test_assert,$(call split,*,),)
$(call stop_test)
$(call start_test,merge)
$(call test_assert,$(call merge,$c,list of things),list$cof$cthings)
$(call test_assert,$(call merge,*,list of things),list*of*things)
$(call test_assert,$(call merge,*,list),list)
$(call test_assert,$(call merge,*,),)
$(call stop_test)
$(call start_test,int_max)
$(call test_assert,$(call int_max,$(call int_encode,2),$(call int_encode,1)),$(call int_encode,2))
$(call test_assert,$(call int_max,$(call int_encode,1),$(call int_encode,2)),$(call int_encode,2))
$(call test_assert,$(call int_max,$(call int_encode,2),$(call int_encode,0)),$(call int_encode,2))
$(call test_assert,$(call int_max,$(call int_encode,0),$(call int_encode,2)),$(call int_encode,2))
$(call test_assert,$(call int_max,$(call int_encode,2),$(call int_encode,2)),$(call int_encode,2))
$(call test_assert,$(call int_max,$(call int_encode,0),$(call int_encode,0)),$(call int_encode,0))
$(call stop_test)
$(call start_test,max)
$(call test_assert,$(call max,2,1),2)
$(call test_assert,$(call max,1,2),2)
$(call test_assert,$(call max,2,0),2)
$(call test_assert,$(call max,0,2),2)
$(call test_assert,$(call max,2,2),2)
$(call test_assert,$(call max,0,0),0)
$(call stop_test)
$(call start_test,int_min)
$(call test_assert,$(call int_min,$(call int_encode,2),$(call int_encode,1)),$(call int_encode,1))
$(call test_assert,$(call int_min,$(call int_encode,1),$(call int_encode,2)),$(call int_encode,1))
$(call test_assert,$(call int_min,$(call int_encode,2),$(call int_encode,0)),$(call int_encode,0))
$(call test_assert,$(call int_min,$(call int_encode,0),$(call int_encode,2)),$(call int_encode,0))
$(call test_assert,$(call int_min,$(call int_encode,2),$(call int_encode,2)),$(call int_encode,2))
$(call test_assert,$(call int_min,$(call int_encode,0),$(call int_encode,0)),$(call int_encode,0))
$(call stop_test)
$(call start_test,min)
$(call test_assert,$(call min,2,1),1)
$(call test_assert,$(call min,1,2),1)
$(call test_assert,$(call min,2,0),0)
$(call test_assert,$(call min,0,2),0)
$(call test_assert,$(call min,2,2),2)
$(call test_assert,$(call min,0,0),0)
$(call stop_test)
__gmsl_error = $1
$(call start_test,assert functions)
$(call test_assert,$(call assert,$(true),ignore),)
$(call test_assert,$(call assert,$(false),failed),Assertion failure: failed)
$(call test_assert,$(call assert_exists,gmsl-tests),)
$(call test_assert,$(call assert_exists,MISSING-gmsl-tests),Assertion failure: file 'MISSING-gmsl-tests' missing)
$(call stop_test)
$(call start_test,int_inc)
$(call test_assert,$(call int_inc,$(call int_encode,0)),$(call int_encode,1))
$(call test_assert,$(call int_inc,$(call int_encode,1)),$(call int_encode,2))
$(call test_assert,$(call int_inc,$(call int_encode,4)),$(call int_encode,5))
$(call test_assert,$(call int_inc,$(call int_encode,10)),$(call int_encode,11))
$(call stop_test)
$(call start_test,inc)
$(call test_assert,$(call inc,0),1)
$(call test_assert,$(call inc,1),2)
$(call test_assert,$(call inc,4),5)
$(call test_assert,$(call inc,10),11)
$(call stop_test)
__gmsl_warning = $1
$(call start_test,int_dec)
$(call test_assert,$(call int_dec,$(call int_encode,0)),Decrement underflow)
$(call test_assert,$(call int_dec,$(call int_encode,1)),$(call int_encode,0))
$(call test_assert,$(call int_dec,$(call int_encode,4)),$(call int_encode,3))
$(call test_assert,$(call int_dec,$(call int_encode,10)),$(call int_encode,9))
$(call stop_test)
__gmsl_warning = x x x x x x x x x x
$(call start_test,dec)
$(call test_assert,$(call dec,0),10)
$(call test_assert,$(call dec,1),0)
$(call test_assert,$(call dec,4),3)
$(call test_assert,$(call dec,10),9)
$(call stop_test)
$(call start_test,int_double)
$(call test_assert,$(call int_double,$(call int_encode,0)),$(call int_encode,0))
$(call test_assert,$(call int_double,$(call int_encode,1)),$(call int_encode,2))
$(call test_assert,$(call int_double,$(call int_encode,4)),$(call int_encode,8))
$(call stop_test)
$(call start_test,double)
$(call test_assert,$(call double,0),0)
$(call test_assert,$(call double,1),2)
$(call test_assert,$(call double,4),8)
$(call stop_test)
$(call start_test,int_halve)
$(call test_assert,$(call int_halve,$(call int_encode,0)),$(call int_encode,0))
$(call test_assert,$(call int_halve,$(call int_encode,2)),$(call int_encode,1))
$(call test_assert,$(call int_halve,$(call int_encode,8)),$(call int_encode,4))
$(call test_assert,$(call int_halve,$(call int_encode,7)),$(call int_encode,3))
$(call stop_test)
$(call start_test,halve)
$(call test_assert,$(call halve,0),0)
$(call test_assert,$(call halve,2),1)
$(call test_assert,$(call halve,8),4)
$(call test_assert,$(call halve,7),3)
$(call stop_test)
$(call start_test,gt)
$(call test_assert,$(call gt,2,3),)
$(call test_assert,$(call gt,3,2),$(true))
$(call test_assert,$(call gt,2,2),)
$(call stop_test)
$(call start_test,gte)
$(call test_assert,$(call gte,2,3),)
$(call test_assert,$(call gte,3,2),$(true))
$(call test_assert,$(call gte,2,2),$(true))
$(call stop_test)
$(call start_test,lt)
$(call test_assert,$(call lt,2,3),$(true))
$(call test_assert,$(call lt,3,2),)
$(call test_assert,$(call lt,2,2),)
$(call stop_test)
$(call start_test,lte)
$(call test_assert,$(call lte,2,3),$(true))
$(call test_assert,$(call lte,3,2),)
$(call test_assert,$(call lte,2,2),$(true))
$(call stop_test)
$(call start_test,eq)
$(call test_assert,$(call eq,2,3),)
$(call test_assert,$(call eq,3,2),)
$(call test_assert,$(call eq,2,2),$(true))
$(call stop_test)
$(call start_test,ne)
$(call test_assert,$(call ne,2,3),$(true))
$(call test_assert,$(call ne,3,2),$(true))
$(call test_assert,$(call ne,2,2),)
$(call stop_test)
$(call start_test,int_gt)
$(call test_assert,$(call int_gt,$(call int_encode,2),$(call int_encode,3)),)
$(call test_assert,$(call int_gt,$(call int_encode,3),$(call int_encode,2)),$(true))
$(call test_assert,$(call int_gt,$(call int_encode,2),$(call int_encode,2)),)
$(call stop_test)
$(call start_test,int_gte)
$(call test_assert,$(call int_gte,$(call int_encode,2),$(call int_encode,3)),)
$(call test_assert,$(call int_gte,$(call int_encode,3),$(call int_encode,2)),$(true))
$(call test_assert,$(call int_gte,$(call int_encode,2),$(call int_encode,2)),$(true))
$(call stop_test)
$(call start_test,int_lt)
$(call test_assert,$(call int_lt,$(call int_encode,2),$(call int_encode,3)),$(true))
$(call test_assert,$(call int_lt,$(call int_encode,3),$(call int_encode,2)),)
$(call test_assert,$(call int_lt,$(call int_encode,2),$(call int_encode,2)),)
$(call stop_test)
$(call start_test,int_lte)
$(call test_assert,$(call int_lte,$(call int_encode,2),$(call int_encode,3)),$(true))
$(call test_assert,$(call int_lte,$(call int_encode,3),$(call int_encode,2)),)
$(call test_assert,$(call int_lte,$(call int_encode,2),$(call int_encode,2)),$(true))
$(call stop_test)
$(call start_test,int_eq)
$(call test_assert,$(call int_eq,$(call int_encode,2),$(call int_encode,3)),)
$(call test_assert,$(call int_eq,$(call int_encode,3),$(call int_encode,2)),)
$(call test_assert,$(call int_eq,$(call int_encode,2),$(call int_encode,2)),$(true))
$(call stop_test)
$(call start_test,int_ne)
$(call test_assert,$(call int_ne,$(call int_encode,2),$(call int_encode,3)),$(true))
$(call test_assert,$(call int_ne,$(call int_encode,3),$(call int_encode,2)),$(true))
$(call test_assert,$(call int_ne,$(call int_encode,2),$(call int_encode,2)),)
$(call stop_test)
$(call start_test,gmsl_compatible)
$(call test_assert,$(call gmsl_compatible,$(gmsl_version)),$(true))
$(call test_assert,$(call gmsl_compatible,0 9 0),$(true))
$(call test_assert,$(call gmsl_compatible,0 0 1),$(true))
$(call test_assert,$(call gmsl_compatible,0 0 0),$(true))
$(call test_assert,$(call gmsl_compatible,2 0 0),)
$(call test_assert,$(call gmsl_compatible,1 1 0),)
$(call test_assert,$(call gmsl_compatible,1 0 8),$(true))
$(call test_assert,$(call gmsl_compatible,1 0 10),)
$(call stop_test)

14
mkfiles/aes128.mk Normal file
View File

@ -0,0 +1,14 @@
# Makefile for AES
ALGO_NAME := AES128_C
# comment out the following line for removement of AES from the build process
BLOCK_CIPHERS += $(ALGO_NAME)
$(ALGO_NAME)_DIR := aes/
$(ALGO_NAME)_OBJ := aes_enc.o aes_dec.o aes_sbox.o aes_invsbox.o \
aes_keyschedule.o gf256mul.o aes128_enc.o aes128_dec.o
$(ALGO_NAME)_TEST_BIN := main-aes128-test.o $(CLI_STD) \
nessie_bc_test.o nessie_common.o performance_test.o
$(ALGO_NAME)_NESSIE_TEST := test nessie
$(ALGO_NAME)_PERFORMANCE_TEST := performance

14
mkfiles/aes192.mk Normal file
View File

@ -0,0 +1,14 @@
# Makefile for AES
ALGO_NAME := AES192_C
# comment out the following line for removement of AES from the build process
BLOCK_CIPHERS += $(ALGO_NAME)
$(ALGO_NAME)_DIR := aes/
$(ALGO_NAME)_OBJ := aes_enc.o aes_dec.o aes_sbox.o aes_invsbox.o \
aes_keyschedule.o gf256mul.o aes192_enc.o aes192_dec.o
$(ALGO_NAME)_TEST_BIN := main-aes192-test.o $(CLI_STD) \
nessie_bc_test.o nessie_common.o performance_test.o
$(ALGO_NAME)_NESSIE_TEST := test nessie
$(ALGO_NAME)_PERFORMANCE_TEST := performance

14
mkfiles/aes256.mk Normal file
View File

@ -0,0 +1,14 @@
# Makefile for AES
ALGO_NAME := AES256_C
# comment out the following line for removement of AES from the build process
BLOCK_CIPHERS += $(ALGO_NAME)
$(ALGO_NAME)_DIR := aes/
$(ALGO_NAME)_OBJ := aes_enc.o aes_dec.o aes_sbox.o aes_invsbox.o \
aes_keyschedule.o gf256mul.o aes256_enc.o aes256_dec.o
$(ALGO_NAME)_TEST_BIN := main-aes256-test.o $(CLI_STD) \
nessie_bc_test.o nessie_common.o performance_test.o
$(ALGO_NAME)_NESSIE_TEST := test nessie
$(ALGO_NAME)_PERFORMANCE_TEST := performance

13
mkfiles/camellia_c.mk Normal file
View File

@ -0,0 +1,13 @@
# Makefile for camellia
ALGO_NAME := CAMELLIA_C
# comment out the following line for removement of serpent from the build process
BLOCK_CIPHERS += $(ALGO_NAME)
$(ALGO_NAME)_DIR := camellia/
$(ALGO_NAME)_OBJ := camellia_C.o
$(ALGO_NAME)_TEST_BIN := main-camellia-test.o $(CLI_STD) nessie_bc_test.o \
nessie_common.o performance_test.o
$(ALGO_NAME)_NESSIE_TEST := "nessie"
$(ALGO_NAME)_PERFORMANCE_TEST := "performance"

13
mkfiles/skein_c.mk Normal file
View File

@ -0,0 +1,13 @@
# Makefile for Skein
ALGO_NAME := SKEIN_C
# comment out the following line for removement of Skein from the build process
HASHES += $(ALGO_NAME)
$(ALGO_NAME)_DIR := skein/
$(ALGO_NAME)_OBJ := threefish256_enc.o threefish512_enc.o threefish1024_enc.o threefish_mix_c.o\
ubi256.o ubi512.o ubi1024.o memxor.o skein256.o skein512.o skein1024.o
$(ALGO_NAME)_TEST_BIN := main-skein-test.o hfal_skein256.o hfal_skein512.o hfal_skein1024.o $(CLI_STD) $(HFAL_STD)
$(ALGO_NAME)_NESSIE_TEST := test nessie
$(ALGO_NAME)_PERFORMANCE_TEST := performance

14
mkfiles/threefish_C.mk Normal file
View File

@ -0,0 +1,14 @@
# Makefile for threefish
ALGO_NAME := THREEFISH_C
# comment out the following line for removement of threefish from the build process
BLOCK_CIPHERS += $(ALGO_NAME)
$(ALGO_NAME)_DIR := skein/
$(ALGO_NAME)_OBJ := threefish256_enc.o threefish512_enc.o threefish1024_enc.o threefish_mix_c.o \
threefish_invmix_c.o threefish256_dec.o threefish512_dec.o threefish1024_dec.o
$(ALGO_NAME)_TEST_BIN := main-threefish-test.o $(CLI_STD) \
nessie_bc_test.o nessie_common.o performance_test.o
$(ALGO_NAME)_NESSIE_TEST := test nessie
$(ALGO_NAME)_PERFORMANCE_TEST := performance

View File

@ -0,0 +1,15 @@
# Makefile for threefish
ALGO_NAME := THREEFISH_SMALL
# comment out the following line for removement of threefish from the build process
BLOCK_CIPHERS += $(ALGO_NAME)
$(ALGO_NAME)_DIR := skein/
$(ALGO_NAME)_OBJ := threefish256_enc_small.o threefish512_enc.o threefish1024_enc.o\
threefish_mix.o threefish_mix_4c.o threefish_invmix_c.o \
threefish256_dec.o threefish512_dec.o threefish1024_dec.o
$(ALGO_NAME)_TEST_BIN := main-threefish-test.o $(CLI_STD) \
nessie_bc_test.o nessie_common.o performance_test.o
$(ALGO_NAME)_NESSIE_TEST := test nessie
$(ALGO_NAME)_PERFORMANCE_TEST := performance

14
mkfiles/ubi_c.mk Normal file
View File

@ -0,0 +1,14 @@
# Makefile for UBI
ALGO_NAME := UBI_C
# comment out the following line for removement of ubi from the build process
AUX += $(ALGO_NAME)
$(ALGO_NAME)_DIR := skein/
$(ALGO_NAME)_OBJ := threefish256_enc.o threefish512_enc.o threefish1024_enc.o threefish_mix_c.o\
ubi256.o ubi512.o ubi1024.o memxor.o
$(ALGO_NAME)_TEST_BIN := main-ubi-test.o $(CLI_STD) \
nessie_common.o performance_test.o
$(ALGO_NAME)_NESSIE_TEST := test nessie
$(ALGO_NAME)_PERFORMANCE_TEST := performance

587
test_src/cmacvs.c Normal file
View File

@ -0,0 +1,587 @@
/* cmacvs.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2006 2007 2008 2009 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file cmacvs.c
* \author Daniel Otte
* \date 2010-02-02
* \license GPLv3 or later
*
*/
#include <avr/pgmspace.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "blockcipher_descriptor.h"
#include "bcal-basic.h"
#include "bcal-cmac.h"
#include "cmacvs.h"
#include "string-extras.h"
#include "cli.h"
#ifdef DEBUG
# undef DEBUG
#endif
#define DEBUG 0
#if DEBUG
# include "config.h"
# include <util/delay.h>
#endif
bcdesc_t* cmacvs_algo=NULL;
bcdesc_t** cmacvs_algolist=NULL;
void cmacvs_listalgos(void){
char option = 'a';
bcdesc_t* t;
uint8_t i=0;
cli_putstr_P(PSTR("\r\nthe following algorithms are available:\r\n"));
while(option<='z' && (t=(bcdesc_t*)pgm_read_word(&(cmacvs_algolist[i])))){
cli_putc('\t');
cli_putc((t==cmacvs_algo)?'*':' ');
cli_putc(option++);
cli_putstr_P(PSTR(":\t"));
cli_putstr_P((void*)(pgm_read_word(&(t->name))));
cli_putstr_P(PSTR("\r\n"));
i++;
}
}
void cmacvs_setalgo(char* param){
param = strstrip(param);
if(param[1]=='\0'){ /* single letter specified */
uint8_t i,option = param[0]-'a';
if(!cmacvs_algolist){
cli_putstr_P(PSTR("\r\nERROR: cmacvs_algolist not set!"));
return;
}
for(i=0; i<=option; ++i){
if((void*)pgm_read_word(&(cmacvs_algolist[i]))==NULL){
cli_putstr_P(PSTR("\r\nERROR: invalid selection!"));
return;
}
}
cmacvs_algo=(bcdesc_t*)pgm_read_word(&(cmacvs_algolist[option]));
} else { /* name specifyed */
bcdesc_t* t=NULL;
uint8_t i=0;
while((t=(bcdesc_t*)pgm_read_word(&(cmacvs_algolist[i]))) &&
strcasecmp_P(param, (void*)pgm_read_word(&(t->name))))
++i;
if(t){
cmacvs_algo=t;
}else{
cli_putstr_P(PSTR("\r\nERROR: could not find \""));
cli_putstr(param);
cli_putstr_P(PSTR("\"!"));
}
}
}
typedef struct {
uint16_t buffer_idx;
uint16_t buffersize_B;
uint32_t blocks;
bcal_cmac_ctx_t ctx;
uint8_t* buffer;
uint8_t in_byte;
} cmacvs_ctx_t;
static cmacvs_ctx_t cmacvs_ctx;
uint8_t buffer_add(char c){
uint8_t v,t;
if(cmacvs_ctx.buffer_idx==cmacvs_ctx.buffersize_B){
bcal_cmac_nextBlock(&(cmacvs_ctx.ctx), cmacvs_ctx.buffer);
++cmacvs_ctx.blocks;
cmacvs_ctx.buffer_idx=0;
cmacvs_ctx.in_byte=0;
cli_putc('.');
memset(cmacvs_ctx.buffer, 0, cmacvs_ctx.buffersize_B);
}
if(c>='0' && c<='9'){
v=c-'0';
}else{
c &= (uint8_t)~('a' ^ 'A');
if(c>='A' && c<='F'){
v=c-'A'+10;
}else{
return 1;
}
}
t=cmacvs_ctx.buffer[cmacvs_ctx.buffer_idx];
if(cmacvs_ctx.in_byte){
t |= v;
cmacvs_ctx.buffer[cmacvs_ctx.buffer_idx]=t;
cmacvs_ctx.buffer_idx++;
cmacvs_ctx.in_byte = 0;
}else{
t |= v<<4;
cmacvs_ctx.buffer[cmacvs_ctx.buffer_idx]=t;
cmacvs_ctx.in_byte = 1;
}
return 0;
}
int32_t getValue_P(PGM_P key){
uint32_t val=0;
char instr[21];
char* str2;
for(;;){
memset(instr, 0, 21);
cli_getsn_cecho(instr, 20);
str2 = strstrip(instr);
if(!strncasecmp_P(str2, key, strlen_P(key))){
while(*str2 && *str2!='=')
str2++;
if(*str2=='='){
do{
str2++;
}while(*str2 && !isdigit(*str2));
val=(uint32_t)strtoul(str2, NULL, 10);
return val;
}
} else {
if(!strncasecmp_P(str2, PSTR("EXIT"), 4)){
cli_putstr_P(PSTR("\r\n got exit ..."));
return -1;
}
}
}
return -2;
}
uint8_t getKey(void* key_buffer, uint8_t klen_B){
char c;
uint8_t v,i=0;
memset(key_buffer, 0x00, klen_B);
do{
c = cli_getc_cecho();
}while((c|('a'^'A'))!='k');
do{
c = cli_getc_cecho();
}while((c|('a'^'A'))!='e');
do{
c = cli_getc_cecho();
}while((c|('a'^'A'))!='y');
do{
c = cli_getc_cecho();
}while(c!='=');
klen_B *= 2;
while(klen_B){
v = 0x10;
c = cli_getc_cecho();
if(c>='0' && c<='9'){
v = c-'0';
}else{
c |= 'A'^'a';
if(c>='a' && c<='f'){
v= c-'a'+10;
}
}
if(v<0x10){
if((i&1)==0){
v<<=4;
}
((uint8_t*)key_buffer)[i/2] |= v;
++i;
--klen_B;
}
}
return 0;
}
uint8_t getMac(void* mac_buffer, uint8_t mlen_B){
char c;
uint8_t v,i=0;
memset(mac_buffer, 0x00, mlen_B);
do{
c = cli_getc_cecho();
}while((c|('a'^'A'))!='m');
do{
c = cli_getc_cecho();
}while((c|('a'^'A'))!='a');
do{
c = cli_getc_cecho();
}while((c|('a'^'A'))!='c');
do{
c = cli_getc_cecho();
}while(c!='=');
mlen_B *= 2;
while(mlen_B){
v = 0x10;
c = cli_getc_cecho();
if(c>='0' && c<='9'){
v = c-'0';
}else{
c |= 'A'^'a';
if(c>='a' && c<='f'){
v= c-'a'+10;
}
}
if(v<0x10){
if((i&1)==0){
v<<=4;
}
((uint8_t*)mac_buffer)[i/2] |= v;
++i;
--mlen_B;
}
}
return 0;
}
void cmacvs_test1(void){ /* Gen tests */
int32_t klen, mlen, tlen;
int32_t expect_input=0;
if(!cmacvs_algo){
cli_putstr_P(PSTR("\r\nERROR: select algorithm first!"));
return;
}
char c;
cmacvs_ctx.buffersize_B=pgm_read_word(&(cmacvs_algo->blocksize_b))/8;
uint8_t tag[cmacvs_ctx.buffersize_B];
uint8_t buffer[cmacvs_ctx.buffersize_B+5];
cmacvs_ctx.buffer = buffer;
cli_putstr_P(PSTR("\r\nbuffer_size = 0x"));
cli_hexdump_rev(&(cmacvs_ctx.buffersize_B), 2);
cli_putstr_P(PSTR(" bytes"));
for(;;){
cmacvs_ctx.blocks = 0;
memset(buffer, 0, cmacvs_ctx.buffersize_B);
klen = getValue_P(PSTR("Klen"));
if(klen<0){
return;
}
mlen = getValue_P(PSTR("Mlen"));
if(mlen<0){
return;
}
tlen = getValue_P(PSTR("Tlen"));
if(tlen<0){
return;
}
uint8_t key_buffer[klen];
#if DEBUG
cli_putstr_P(PSTR("\r\nKLen == "));
cli_hexdump_rev(&klen, 4);
cli_putstr_P(PSTR("\r\nMLen == "));
cli_hexdump_rev(&mlen, 4);
cli_putstr_P(PSTR("\r\nTLen == "));
cli_hexdump_rev(&tlen, 4);
#endif
getKey(key_buffer, klen);
if(mlen==0){
expect_input=2;
}else{
expect_input=mlen*2;
}
#if DEBUG
cli_putstr_P(PSTR("\r\nexpected_input == "));
cli_hexdump_rev(&expect_input, 4);
if(expect_input==0)
cli_putstr_P(PSTR("\r\nexpected_input == 0 !!!"));
#endif
uint8_t ret;
#if DEBUG
cli_putstr_P(PSTR("\r\n CMAC init"));
cli_putstr_P(PSTR("\r\n (2) expected_input == "));
cli_hexdump_rev(&expect_input, 4);
#endif
ret = bcal_cmac_init(cmacvs_algo, key_buffer, klen*8, &(cmacvs_ctx.ctx));
if(ret){
cli_putstr_P(PSTR("\r\n bcal_cmac_init returned with: "));
cli_hexdump(&ret, 1);
return;
}
#if DEBUG
cli_putstr_P(PSTR("\r\n (3) expected_input == "));
cli_hexdump_rev(&expect_input, 4);
cli_putstr_P(PSTR("\r\n"));
#endif
while((c=cli_getc_cecho())!='M' && c!='m'){
if(!isspace(c)){
cli_putstr_P(PSTR("\r\nERROR: wrong input (1) [0x"));
cli_hexdump(&c, 1);
cli_putstr_P(PSTR("]!\r\n"));
bcal_cmac_free(&(cmacvs_ctx.ctx));
return;
}
}
if((c=cli_getc_cecho())!='s' && c!='S'){
cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n"));
bcal_cmac_free(&(cmacvs_ctx.ctx));
return;
}
if((c=cli_getc_cecho())!='g' && c!='G'){
cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n"));
bcal_cmac_free(&(cmacvs_ctx.ctx));
return;
}
while((c=cli_getc_cecho())!='='){
if(!isspace(c)){
cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n"));
bcal_cmac_free(&(cmacvs_ctx.ctx));
return;
}
}
#if DEBUG
cli_putstr_P(PSTR("\r\nparsing started"));
#endif
cmacvs_ctx.buffer_idx = 0;
cmacvs_ctx.in_byte = 0;
cmacvs_ctx.blocks = 0;
while(expect_input>0){
c=cli_getc_cecho();
#if DEBUG
cli_putstr_P(PSTR("\r\n\t("));
cli_hexdump_rev(&expect_input, 4);
cli_putstr_P(PSTR(") "));
_delay_ms(500);
#endif
if(buffer_add(c)==0){
--expect_input;
}else{
if(!isblank((uint16_t)c)){
cli_putstr_P(PSTR("\r\nERROR: wrong input (5) ("));
cli_putc(c);
cli_putstr_P(PSTR(")!\r\n"));
bcal_cmac_free(&(cmacvs_ctx.ctx));
return;
}
}
}
#if DEBUG
cli_putstr_P(PSTR("\r\nBuffer-A:"));
cli_hexdump_block(buffer, cmacvs_ctx.buffersize_B, 5, 8);
cli_putstr_P(PSTR("\r\n starting finalisation"));
cli_putstr_P(PSTR("\r\n\tblocks == "));
cli_hexdump_rev(&(cmacvs_ctx.blocks),4);
cli_putstr_P(PSTR("\r\n\tbuffer_idx == "));
cli_hexdump_rev(&(cmacvs_ctx.buffer_idx),2);
cli_putstr_P(PSTR("\r\n\tin_byte == "));
cli_hexdump_rev(&(cmacvs_ctx.in_byte),1);
// _delay_ms(500);
cli_putstr_P(PSTR("\r\n starting last block"));
cli_putstr_P(PSTR("\r\n\tlength == "));
cli_hexdump_rev(&mlen,4);
cli_putstr_P(PSTR("\r\n\tbuffersize_B == "));
cli_hexdump_rev(&(cmacvs_ctx.buffersize_B),2);
uint16_t temp=(mlen-cmacvs_ctx.blocks*cmacvs_ctx.buffersize_B)*8;
cli_putstr_P(PSTR("\r\n\t (temp) == "));
cli_hexdump_rev(&temp,2);
// _delay_ms(500);
#endif
uint16_t temp=(mlen-cmacvs_ctx.blocks*cmacvs_ctx.buffersize_B)*8;
bcal_cmac_lastBlock( &(cmacvs_ctx.ctx), buffer, /* be aware of freaking compilers!!! */
// length-(cmacvs_ctx.blocks)*((cmacvs_ctx.buffersize_B)*8));
temp );
#if DEBUG
cli_putstr_P(PSTR("\r\n starting ctx2cmac"));
_delay_ms(500);
#endif
bcal_cmac_ctx2mac(tag, tlen*8, &(cmacvs_ctx.ctx));
#if DEBUG
cli_putstr_P(PSTR("\r\n starting cmac free"));
#endif
bcal_cmac_free(&(cmacvs_ctx.ctx));
cli_putstr_P(PSTR("\r\n Mac = "));
cli_hexdump(tag, tlen);
}
}
void cmacvs_test2(void){ /* Ver tests */
int32_t klen, mlen, tlen;
int32_t expect_input=0;
if(!cmacvs_algo){
cli_putstr_P(PSTR("\r\nERROR: select algorithm first!"));
return;
}
char c;
cmacvs_ctx.buffersize_B=pgm_read_word(&(cmacvs_algo->blocksize_b))/8;
uint8_t tag[cmacvs_ctx.buffersize_B];
uint8_t tag_ref[cmacvs_ctx.buffersize_B];
uint8_t buffer[cmacvs_ctx.buffersize_B+5];
cmacvs_ctx.buffer = buffer;
cli_putstr_P(PSTR("\r\nbuffer_size = 0x"));
cli_hexdump_rev(&(cmacvs_ctx.buffersize_B), 2);
cli_putstr_P(PSTR(" bytes"));
for(;;){
cmacvs_ctx.blocks = 0;
memset(buffer, 0, cmacvs_ctx.buffersize_B);
klen = getValue_P(PSTR("Klen"));
if(klen<0){
return;
}
mlen = getValue_P(PSTR("Mlen"));
if(mlen<0){
return;
}
tlen = getValue_P(PSTR("Tlen"));
if(tlen<0){
return;
}
uint8_t key_buffer[klen];
#if DEBUG
cli_putstr_P(PSTR("\r\nKLen == "));
cli_hexdump_rev(&klen, 4);
cli_putstr_P(PSTR("\r\nMLen == "));
cli_hexdump_rev(&mlen, 4);
cli_putstr_P(PSTR("\r\nTLen == "));
cli_hexdump_rev(&tlen, 4);
#endif
getKey(key_buffer, klen);
if(mlen==0){
expect_input=2;
}else{
expect_input=mlen*2;
}
#if DEBUG
cli_putstr_P(PSTR("\r\nexpected_input == "));
cli_hexdump_rev(&expect_input, 4);
if(expect_input==0)
cli_putstr_P(PSTR("\r\nexpected_input == 0 !!!"));
#endif
uint8_t ret;
#if DEBUG
cli_putstr_P(PSTR("\r\n CMAC init"));
cli_putstr_P(PSTR("\r\n (2) expected_input == "));
cli_hexdump_rev(&expect_input, 4);
#endif
ret = bcal_cmac_init(cmacvs_algo, key_buffer, klen*8, &(cmacvs_ctx.ctx));
if(ret){
cli_putstr_P(PSTR("\r\n bcal_cmac_init returned with: "));
cli_hexdump(&ret, 1);
return;
}
#if DEBUG
cli_putstr_P(PSTR("\r\n (3) expected_input == "));
cli_hexdump_rev(&expect_input, 4);
cli_putstr_P(PSTR("\r\n"));
#endif
while((c=cli_getc_cecho())!='M' && c!='m'){
if(!isspace(c)){
cli_putstr_P(PSTR("\r\nERROR: wrong input (1) [0x"));
cli_hexdump(&c, 1);
cli_putstr_P(PSTR("]!\r\n"));
bcal_cmac_free(&(cmacvs_ctx.ctx));
return;
}
}
if((c=cli_getc_cecho())!='s' && c!='S'){
cli_putstr_P(PSTR("\r\nERROR: wrong input (2)!\r\n"));
bcal_cmac_free(&(cmacvs_ctx.ctx));
return;
}
if((c=cli_getc_cecho())!='g' && c!='G'){
cli_putstr_P(PSTR("\r\nERROR: wrong input (3)!\r\n"));
bcal_cmac_free(&(cmacvs_ctx.ctx));
return;
}
while((c=cli_getc_cecho())!='='){
if(!isspace(c)){
cli_putstr_P(PSTR("\r\nERROR: wrong input (4)!\r\n"));
bcal_cmac_free(&(cmacvs_ctx.ctx));
return;
}
}
#if DEBUG
cli_putstr_P(PSTR("\r\nparsing started"));
#endif
cmacvs_ctx.buffer_idx = 0;
cmacvs_ctx.in_byte = 0;
cmacvs_ctx.blocks = 0;
while(expect_input>0){
c=cli_getc_cecho();
#if DEBUG
cli_putstr_P(PSTR("\r\n\t("));
cli_hexdump_rev(&expect_input, 4);
cli_putstr_P(PSTR(") "));
_delay_ms(500);
#endif
if(buffer_add(c)==0){
--expect_input;
}else{
if(!isblank((uint16_t)c)){
cli_putstr_P(PSTR("\r\nERROR: wrong input (5) ("));
cli_putc(c);
cli_putstr_P(PSTR(")!\r\n"));
bcal_cmac_free(&(cmacvs_ctx.ctx));
return;
}
}
}
#if DEBUG
cli_putstr_P(PSTR("\r\nBuffer-A:"));
cli_hexdump_block(buffer, cmacvs_ctx.buffersize_B, 5, 8);
cli_putstr_P(PSTR("\r\n starting finalisation"));
cli_putstr_P(PSTR("\r\n\tblocks == "));
cli_hexdump_rev(&(cmacvs_ctx.blocks),4);
cli_putstr_P(PSTR("\r\n\tbuffer_idx == "));
cli_hexdump_rev(&(cmacvs_ctx.buffer_idx),2);
cli_putstr_P(PSTR("\r\n\tin_byte == "));
cli_hexdump_rev(&(cmacvs_ctx.in_byte),1);
// _delay_ms(500);
cli_putstr_P(PSTR("\r\n starting last block"));
cli_putstr_P(PSTR("\r\n\tlength == "));
cli_hexdump_rev(&mlen,4);
cli_putstr_P(PSTR("\r\n\tbuffersize_B == "));
cli_hexdump_rev(&(cmacvs_ctx.buffersize_B),2);
uint16_t temp=(mlen-cmacvs_ctx.blocks*cmacvs_ctx.buffersize_B)*8;
cli_putstr_P(PSTR("\r\n\t (temp) == "));
cli_hexdump_rev(&temp,2);
// _delay_ms(500);
#endif
uint16_t temp=(mlen-cmacvs_ctx.blocks*cmacvs_ctx.buffersize_B)*8;
bcal_cmac_lastBlock( &(cmacvs_ctx.ctx), buffer, /* be aware of freaking compilers!!! */
// length-(cmacvs_ctx.blocks)*((cmacvs_ctx.buffersize_B)*8));
temp );
#if DEBUG
cli_putstr_P(PSTR("\r\n starting ctx2cmac"));
_delay_ms(500);
#endif
bcal_cmac_ctx2mac(tag, tlen*8, &(cmacvs_ctx.ctx));
#if DEBUG
cli_putstr_P(PSTR("\r\n starting cmac free"));
#endif
bcal_cmac_free(&(cmacvs_ctx.ctx));
cli_putstr_P(PSTR("\r\n Mac = "));
cli_hexdump(tag, tlen);
getMac(tag_ref, tlen);
if(memcmp(tag, tag_ref, tlen)){
cli_putstr_P(PSTR("\r\n Result = F"));
}else{
cli_putstr_P(PSTR("\r\n Result = P"));
}
}
}

42
test_src/cmacvs.h Normal file
View File

@ -0,0 +1,42 @@
/* cmacvs.h */
/*
This file is part of the AVR-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 <http://www.gnu.org/licenses/>.
*/
/**
* \file cmacvs.h
* \author Daniel Otte
* \date 2010-02-02
* \license GPLv3 or later
*
*/
#ifndef CMACVS_H_
#define CMACVS_H_
#include <stdlib.h>
#include "blockcipher_descriptor.h"
extern bcdesc_t* cmacvs_algo;
extern bcdesc_t** cmacvs_algolist;
void cmacvs_listalgos(void);
void cmacvs_setalgo(char* param);
void cmacvs_test1(void);
void cmacvs_test2(void);
#endif /* CMACVS_H_ */

File diff suppressed because it is too large Load Diff