cmac+testvectors

This commit is contained in:
bg 2010-02-05 08:50:59 +00:00
parent 8abb80e1fe
commit 5120e1b9ad
25 changed files with 24261 additions and 41 deletions

View File

@ -25,21 +25,21 @@
#include "keysize_descriptor.h"
uint8_t bcal_cipher_init(const bcdesc_t* cipher_descriptor,
const void* key, uint16_t keysize, bcgen_ctx_t* ctx){
const void* key, uint16_t keysize_b, bcgen_ctx_t* ctx){
if(!is_valid_keysize_P((PGM_VOID_P)pgm_read_word(&(cipher_descriptor->valid_keysize_desc)),
keysize)){
keysize_b)){
return 1;
}
uint8_t flags;
bc_init_fpt init_fpt;
ctx->desc_ptr = (bcdesc_t*)cipher_descriptor;
ctx->keysize = keysize;
ctx->keysize = keysize_b;
flags = pgm_read_byte(cipher_descriptor->flags);
init_fpt.initvoid = (void_fpt)(pgm_read_word(&(cipher_descriptor->init.initvoid)));
if(init_fpt.initvoid == NULL){
if(!(ctx->ctx = malloc((keysize+7)/8)))
if(!(ctx->ctx = malloc((keysize_b+7)/8)))
return 2;
memcpy(ctx->ctx, key, (keysize+7)/8);
memcpy(ctx->ctx, key, (keysize_b+7)/8);
return 0;
}
if(!(ctx->ctx = malloc(pgm_read_word(&(cipher_descriptor->ctxsize_B)))))
@ -47,7 +47,7 @@ uint8_t bcal_cipher_init(const bcdesc_t* cipher_descriptor,
if((flags&BC_INIT_TYPE)==BC_INIT_TYPE_1){
init_fpt.init1((void*)key, (ctx->ctx));
}else{
init_fpt.init2((void*)key, keysize, (ctx->ctx));
init_fpt.init2((void*)key, keysize_b, (ctx->ctx));
}
return 0;
}

View File

@ -27,7 +27,7 @@
#include <avr/pgmspace.h>
uint8_t bcal_cipher_init(const bcdesc_t* cipher_descriptor,
const void* key, uint16_t keysize, bcgen_ctx_t* ctx);
const void* key, uint16_t keysize_b, bcgen_ctx_t* ctx);
void bcal_cipher_free(bcgen_ctx_t* ctx);
void bcal_cipher_enc(void* block, const bcgen_ctx_t* ctx);
void bcal_cipher_dec(void* block, const bcgen_ctx_t* ctx);

134
bcal-cmac.c Normal file
View File

@ -0,0 +1,134 @@
/* bcal-omac.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <string.h>
#include "bcal-basic.h"
#include "bcal-cmac.h"
#include "memxor.h"
static uint8_t left_shift_be_block(void* block, uint8_t blocksize_B){
uint8_t c1=0, c2;
do{
--blocksize_B;
c2 = (((uint8_t*)block)[blocksize_B])>>7;
(((uint8_t*)block)[blocksize_B]) <<= 1;
(((uint8_t*)block)[blocksize_B]) |= c1;
c1 = c2;
}while(blocksize_B);
return c1;
}
static const uint8_t const_128 = 0x87;
static const uint8_t const_64 = 0x1b;
uint8_t bcal_cmac_init(const bcdesc_t* desc, const void* key, uint16_t keysize_b, bcal_cmac_ctx_t* ctx){
uint8_t r;
ctx->desc = (bcdesc_t*)desc;
ctx->blocksize_B = bcal_cipher_getBlocksize_b(desc)/8;
if (ctx->blocksize_B!=128/8 && ctx->blocksize_B!=64/8){
return 0x13;
}
ctx->accu = malloc(ctx->blocksize_B);
if(ctx->accu==NULL){
return 0x14;
}
ctx->k1 = malloc(ctx->blocksize_B);
if(ctx->k1==NULL){
return 0x15;
}
ctx->k2 = malloc(ctx->blocksize_B);
if(ctx->k2==NULL){
return 0x16;
}
r = bcal_cipher_init(desc, key, keysize_b, &(ctx->cctx));
if(r){
return r;
}
if(ctx->blocksize_B==128/8){
r = const_128;
}else{
r = const_64;
}
/* subkey computation */
memset(ctx->accu, 0x00, ctx->blocksize_B);
memset(ctx->k1, 0x00, ctx->blocksize_B);
bcal_cipher_enc(ctx->k1, &(ctx->cctx));
if(left_shift_be_block(ctx->k1, ctx->blocksize_B)){
ctx->k1[ctx->blocksize_B-1] ^= r;
}
memcpy(ctx->k2, ctx->k1, ctx->blocksize_B);
if(left_shift_be_block(ctx->k2, ctx->blocksize_B)){
ctx->k2[ctx->blocksize_B-1] ^= r;
}
return 0;
}
void bcal_cmac_free(bcal_cmac_ctx_t* ctx){
free(ctx->accu);
free(ctx->k1);
free(ctx->k2);
bcal_cipher_free(&(ctx->cctx));
}
void bcal_cmac_nextBlock (bcal_cmac_ctx_t* ctx, const void* block){
memxor(ctx->accu, block, ctx->blocksize_B);
bcal_cipher_enc(ctx->accu, &(ctx->cctx));
}
void bcal_cmac_lastBlock(bcal_cmac_ctx_t* ctx, const void* block, uint16_t length_b){
uint16_t blocksize_b;
blocksize_b = ctx->blocksize_B*8;
while(length_b>blocksize_b){
bcal_cmac_nextBlock(ctx, block);
block = (uint8_t*)block + ctx->blocksize_B;
length_b -= blocksize_b;
}
memxor(ctx->accu, block, (length_b+7)/8);
if(length_b==blocksize_b){
memxor(ctx->accu, ctx->k1, ctx->blocksize_B);
}else{
memxor(ctx->accu, ctx->k2, ctx->blocksize_B);
ctx->accu[length_b/8] ^= 0x80>>(length_b&7);
}
bcal_cipher_enc(ctx->accu, &(ctx->cctx));
}
void bcal_cmac_ctx2mac(void* dest, uint16_t length_b, const bcal_cmac_ctx_t* ctx){
memcpy(dest, ctx->accu, length_b/8);
if(length_b&7){
((uint8_t*)dest)[length_b/8] &= 0xff>>(length_b&7);
((uint8_t*)dest)[length_b/8] |= (0xff00>>(length_b&7))&(ctx->accu[length_b/8]);
}
}
void bcal_cmac(void* dest, uint16_t out_length_b, const void* block, uint32_t length_b, bcal_cmac_ctx_t* ctx){
uint16_t blocksize_b;
blocksize_b = ctx->blocksize_B*8;
while(length_b>blocksize_b){
bcal_cmac_nextBlock(ctx, block);
block = (uint8_t*)block + ctx->blocksize_B;
length_b -= blocksize_b;
}
bcal_cmac_lastBlock(ctx, block, length_b);
bcal_cmac_ctx2mac(dest, out_length_b, ctx);
}

43
bcal-cmac.h Normal file
View File

@ -0,0 +1,43 @@
/* bcal-cmac.h */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef BCALCMAC_H_
#define BCALCMAC_H_
#include <stdint.h>
#include "bcal-basic.h"
#include "blockcipher_descriptor.h"
typedef struct{
bcdesc_t* desc;
bcgen_ctx_t cctx;
uint8_t* accu;
uint8_t* k1;
uint8_t* k2;
uint8_t blocksize_B;
} bcal_cmac_ctx_t;
uint8_t bcal_cmac_init(const bcdesc_t* desc, const void* key, uint16_t keysize_b, bcal_cmac_ctx_t* ctx);
void bcal_cmac_free(bcal_cmac_ctx_t* ctx);
void bcal_cmac_nextBlock(bcal_cmac_ctx_t* ctx, const void* block);
void bcal_cmac_lastBlock(bcal_cmac_ctx_t* ctx, const void* block, uint16_t length_b);
void bcal_cmac_ctx2mac(void* dest, uint16_t length_b, const bcal_cmac_ctx_t* state);
void bcal_cmac(void* dest, uint16_t out_length_b, const void* block, uint32_t length_b, bcal_cmac_ctx_t* ctx);
#endif /* BCALCMAC_H_ */

View File

@ -41,11 +41,11 @@ const uint8_t aes192_keysize_desc[] PROGMEM = { KS_TYPE_LIST, 1, KS_INT(192),
const bcdesc_t aes192_desc PROGMEM = {
BCDESC_TYPE_BLOCKCIPHER,
BC_INIT_TYPE_2,
BC_INIT_TYPE_1,
aes192_str,
sizeof(aes192_ctx_t),
128,
{(void_fpt)aes_init},
{(void_fpt)aes192_init},
{(void_fpt)aes192_enc},
{(void_fpt)aes192_dec},
(bc_free_fpt)NULL,

View File

@ -41,11 +41,11 @@ const uint8_t aes256_keysize_desc[] PROGMEM = { KS_TYPE_LIST, 1, KS_INT(256),
const bcdesc_t aes256_desc PROGMEM = {
BCDESC_TYPE_BLOCKCIPHER,
BC_INIT_TYPE_2,
BC_INIT_TYPE_1,
aes256_str,
sizeof(aes256_ctx_t),
128,
{(void_fpt)aes_init},
{(void_fpt)aes256_init},
{(void_fpt)aes256_enc},
{(void_fpt)aes256_dec},
(bc_free_fpt)NULL,

View File

@ -51,7 +51,7 @@ const bcdesc_t tdes_desc PROGMEM = {
BC_INIT_TYPE_1,
tdes_str,
24,
128,
64,
{(void_fpt)NULL},
{(void_fpt)tdes_dummy_enc},
{(void_fpt)tdes_dummy_dec},

70
bcal_tdes2.c Normal file
View File

@ -0,0 +1,70 @@
/* bcal_tdes2.c */
/*
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 bcal_tdes.c
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2010-02-02
* \license GPLv3 or later
*
*/
#include <avr/pgmspace.h>
#include <stdlib.h>
#include "blockcipher_descriptor.h"
#include "des.h"
#include "keysize_descriptor.h"
const char tdes2_str[] PROGMEM = "TDES-2";
const uint8_t tdes2_keysize_desc[] PROGMEM = { KS_TYPE_LIST, 1, KS_INT(128),
KS_TYPE_TERMINATOR };
static
void tdes_dummy_enc(void* block, void* key){
tdes_enc(block, block, key);
}
static
void tdes_dummy_dec(void* block, void* key){
tdes_dec(block, block, key);
}
static
void tdes2_init(void* key, void* ctx){
memcpy(ctx, key, 16);
memcpy((uint8_t*)ctx+16, key, 8);
}
const bcdesc_t tdes2_desc PROGMEM = {
BCDESC_TYPE_BLOCKCIPHER,
BC_INIT_TYPE_1,
tdes2_str,
24,
64,
{(void_fpt)tdes2_init},
{(void_fpt)tdes_dummy_enc},
{(void_fpt)tdes_dummy_dec},
(bc_free_fpt)NULL,
tdes2_keysize_desc
};

38
bcal_tdes2.h Normal file
View File

@ -0,0 +1,38 @@
/* bcal_tdes2.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 bcal_tdes.h
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2009-01-09
* \license GPLv3 or later
*
*/
#ifndef BCAL_TDES2_H_
#define BCAL_TDES2_H_
#include <avr/pgmspace.h>
#include "blockcipher_descriptor.h"
#include "des.h"
#include "keysize_descriptor.h"
extern const bcdesc_t tdes2_desc;
#endif /* BCAL_TDES2_H_ */

View File

@ -46,7 +46,7 @@
*
* This function encrypts a block of 64 bits (8 bytes) with the DES algorithm.
* Key expansion is done automatically. The key is 64 bits long, but note that
* only 56 bits are used (the LSB of each byte is droped). The input and output
* only 56 bits are used (the LSB of each byte is dropped). The input and output
* blocks may overlap.
*
* \param out pointer to the block (64 bit = 8 byte) where the ciphertext is written to
@ -60,7 +60,7 @@ void des_enc(void* out, const void* in, const void* key);
*
* This function decrypts a block of 64 bits (8 bytes) with the DES algorithm.
* Key expansion is done automatically. The key is 64 bits long, but note that
* only 56 bits are used (the LSB of each byte is droped). The input and output
* only 56 bits are used (the LSB of each byte is dropped). The input and output
* blocks may overlap.
*
* \param out pointer to the block (64 bit = 8 byte) where the plaintext is written to
@ -74,7 +74,7 @@ void des_dec(void* out, const void* in, const void* key);
*
* This function encrypts a block of 64 bits (8 bytes) with the Tripple-DES (EDE)
* algorithm. Key expansion is done automatically. The key is 192 bits long, but
* note that only 178 bits are used (the LSB of each byte is droped). The input
* note that only 178 bits are used (the LSB of each byte is dropped). The input
* and output blocks may overlap.
*
* \param out pointer to the block (64 bit = 8 byte) where the ciphertext is written to
@ -88,7 +88,7 @@ void tdes_enc(void* out, const void* in, const void* key);
*
* This function decrypts a block of 64 bits (8 bytes) with the Tripple-DES (EDE)
* algorithm. Key expansion is done automatically. The key is 192 bits long, but
* note that only 178 bits are used (the LSB of each byte is droped). The input
* note that only 178 bits are used (the LSB of each byte is dropped). The input
* and output blocks may overlap.
*
* \param out pointer to the block (64 bit = 8 byte) where the plaintext is written to

359
host/cmacvs_test.rb Normal file
View File

@ -0,0 +1,359 @@
#!/usr/bin/ruby
# cmacvs_test.rb
=begin
This file is part of the AVR-Crypto-Lib.
Copyright (C) 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/>.
=end
$debug = true
$debug = false
require 'rubygems'
require 'serialport'
require 'getopt/std'
$buffer_size = 0
$conffile_check = Hash.new
$conffile_check.default = 0
################################################################################
# readconfigfile #
################################################################################
def readconfigfile(fname, conf)
return conf if $conffile_check[fname]==1
$conffile_check[fname]=1
section = "default"
if not File.exists?(fname)
return conf
end
file = File.open(fname, "r")
until file.eof
line = file.gets()
next if /[\s]*#/.match(line)
if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
section=m[1]
conf[m[1]] = Hash.new
next
end
next if not /=/.match(line)
m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
if m[1]=="include"
Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) }
else
conf[section][m[1]] = m[2]
end
end
file.close()
return conf
end
################################################################################
# reset_system #
################################################################################
def reset_system
$sp.print("exit\r")
sleep 0.1
$sp.print("exit\r")
sleep 0.1
end
################################################################################
# scan_system #
################################################################################
def scan_system
algos = Hash.new
$sp.print("cmacvs_list\r")
while true
line=$sp.gets()
return algos if /^>$/.match(line)
if m = /[\*\ ]([a-z]):[\s]*([a-zA-Z0-9+_-]+)/.match(line)
algos[m[2]]=m[1]
end
end
end
################################################################################
# init_system #
################################################################################
def init_system(algo_select, algo_id)
$sp.print("echo off \r")
print("DBG i: " + "echo off \r"+"\n") if $debug
sleep 1
$sp.print("cmacvs_set #{algo_select}\r")
print("DBG i: " + "cmacvs_set #{$algo_select} \r"+"\n") if $debug
sleep 1
$sp.print("cmacvs_test#{algo_id} \r")
print("DBG i: " + "cmacvs_test#{algo_id} \r"+"\n") if $debug
begin
line=$sp.gets()
end while not m=/buffer_size[\s]*=[\s]*0x([0-9A-Fa-f]*)/.match(line)
$buffer_size = m[1].to_i(16)
end
################################################################################
# get_md #
################################################################################
def get_mac
begin
line = $sp.gets()
line = "" if line==nil
puts("DBG got: "+line) if $debug
end while not /[\s]*Mac[\s]*=.*/.match(line)
return line
end
################################################################################
# get_result #
################################################################################
def get_result
begin
line = $sp.gets()
line = "" if line==nil
puts("DBG got: "+line) if $debug
end while not /[\s]*Result[\s]*=.*/.match(line)
puts "DBG i: got result: "+line if $debug
return line
end
################################################################################
# send_md #
################################################################################
def send_test(klen, mlen, tlen, key, msg, mac=nil)
$sp.printf("Klen = %s\n\r", klen)
$sp.printf("Mlen = %s\n\r", mlen)
$sp.printf("Tlen = %s\n\r", tlen)
$sp.printf("Key = %s\n\r", key)
$sp.print("Msg = ")
for i in 0..msg.length-1
$sp.print(msg[i].chr)
# print("DBG s: "+ md_string[i].chr) if $debug
# sleep(0.001)
if((i%($buffer_size*2)==0)&&(i!=0))
begin
line=$sp.gets()
end while not /\./.match(line)
end
end
$sp.printf("Mac = %s\n\r", mac) if mac
end
################################################################################
# get_next_kv_pair #
################################################################################
def get_next_kv_pair(file)
loop do
return nil if file.eof
lb = file.gets()
m=lb.match(/[\s]*([\w\d_-]*)[\s]*=[\s]*([\w\d_-]*)/)
puts "DBG i: found #{m[1]} with value #{m[2]}" if m && $debug
return [m[1],m[2]] if m
end
end
################################################################################
# run_test_gen #
################################################################################
def run_test_gen(filename, skip=0)
nerrors = 0
line=1
if not File.exist?(filename)
puts("ERROR file "+filename+" does not exist!")
return nerrors
end
pos = 0
file = File.new(filename, "r");
until file.eof
params = Hash.new
begin
m = get_next_kv_pair(file)
return nerrors if m==nil
params[m[0]] = m[1]
end until m[0]=='Mac'
if(skip>0)
skip -= 1
redo
end
puts("DBG sending: ") if $debug
send_test(params['Klen'], params['Mlen'], params['Tlen'], params['Key'], params['Msg'])
avr_md = get_mac()
a = params['Mac'];
b = (/[\s]*Mac[\s]*=[\s]*([0-9a-fA-F]*).*/.match(avr_md))[1];
a.upcase!
b.upcase!
printf("\n%4d (%4d) [%5d]: ", line, (line-1)*$linewidth, params['Count']) if (pos%$linewidth==0 and $linewidth!=0)
line += 1 if (pos%$linewidth==0 and $linewidth!=0)
#sleep(1)
#putc((a==b)?'*':'!')
if(a==b)
putc('*')
else
putc('!')
# printf("<%d>",len)
printf("\nError @%05d: %s [should]\n != %s [is]- ", params['Count'].to_i , a, b)
nerrors += 1
end
pos += 1
end
file.close()
return nerrors
end
################################################################################
# run_test_ver #
################################################################################
def run_test_ver(filename, skip=0)
nerrors = 0
line=1
if not File.exist?(filename)
puts("ERROR file "+filename+" does not exist!")
return nerrors
end
pos = 0
file = File.new(filename, "r");
until file.eof
params = Hash.new
begin
m = get_next_kv_pair(file)
return nerrors if m==nil
params[m[0]] = m[1]
end until m[0]=='Result'
if(skip>0)
skip -= 1
redo
end
puts("DBG sending: ") if $debug
send_test(params['Klen'], params['Mlen'], params['Tlen'], params['Key'], params['Msg'], params['Mac'])
avr_res = get_result()
a = params['Result'].match(/[\s]*([PF])/)[1];
b = /[\s]*Result[\s]*=[\s]*([PF])/.match(avr_res)[1];
a.upcase!
b.upcase!
printf("\n%4d (%4d) [%5d]: ", line, (line-1)*$linewidth, params['Count']) if (pos%$linewidth==0 and $linewidth!=0)
line += 1 if (pos%$linewidth==0 and $linewidth!=0)
#sleep(1)
#putc((a==b)?'*':'!')
if(a==b)
putc('*')
else
putc('!')
# printf("<%d>",len)
printf("\nError @%05d: %s [should]\n != %s [is]- ", params['Count'].to_i , a, b)
nerrors += 1
end
pos += 1
end
file.close()
return nerrors
end
################################################################################
# MAIN #
################################################################################
opts = Getopt::Std.getopts("s:f:i:j:hdca")
conf = Hash.new
conf = readconfigfile("/etc/testport.conf", conf)
conf = readconfigfile("~/.testport.conf", conf)
conf = readconfigfile("testport.conf", conf)
conf = readconfigfile(opts["f"], conf) if opts["f"]
#puts conf.inspect
puts("serial port interface version: " + SerialPort::VERSION);
$linewidth = 64
params = { "baud" => conf["PORT"]["baud"].to_i,
"data_bits" => conf["PORT"]["databits"].to_i,
"stop_bits" => conf["PORT"]["stopbits"].to_i,
"parity" => SerialPort::NONE }
params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd"
params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even"
params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark"
params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
puts("\nPort: "+conf["PORT"]["port"]+"@" +
params["baud"].to_s +
" " +
params["data_bits"].to_s +
conf["PORT"]["paraty"][0,1].upcase +
params["stop_bits"].to_s +
"\n")
$sp = SerialPort.new(conf["PORT"]["port"], params)
$sp.read_timeout=1000; # 5 minutes
$sp.flow_control = SerialPort::SOFT
reset_system()
algos=scan_system()
#puts algos.inspect
if opts["d"]
$debug = true
end
if opts["s"]
algos_rev = algos.invert
algo_tasks = Array.new
opts["s"].each_byte{ |x|
if algos_rev[x.chr]
algo_tasks << [algos_rev[x.chr],x.chr]
end
}
else
algo_tasks=algos.sort
end
algo_tasks.each do |algoa|
algo = algoa[0]
if conf[algo]==nil
puts("No test-set defined for #{algo} \r\n")
next
else
i=0
i = opts["j"] if opts["j"]
logfile=File.open(conf["PORT"]["testlogbase"]+algo+".txt", "a")
while conf[algo]["file_#{i}"] != nil
puts("Testing #{algo} with #{conf[algo]["file_#{i}"]}")
reset_system()
init_system(algoa[1], (conf[algo]["file_#{i}_test"]=='gen')?'1':'2')
skip=0
skip=opts["i"].to_i if opts["i"]
nerrors=run_test_gen(conf[algo]["file_#{i}"], skip) if conf[algo]["file_#{i}_test"]=='gen'
nerrors=run_test_ver(conf[algo]["file_#{i}"], skip) if conf[algo]["file_#{i}_test"]=='ver'
if nerrors == 0
puts("\n[ok]")
logfile.puts("[ok] "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
else
puts("\n[errors: "+ nerrors.to_s() +"]")
logfile.puts("[error] "+nerrors.to_s+" "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
end
i += 1
end
logfile.close()
end
end

View File

@ -11,7 +11,7 @@ $(ALGO_NAME)_TEST_BIN := main-aes-test.o $(CLI_STD) \
nessie_bc_test.o nessie_common.o performance_test.o memxor.o \
bcal_aes128.o bcal_aes192.o bcal_aes256.o bcal-basic.o bcal-cbc.o \
keysize_descriptor.o dump-asm.o dump-decl.o bcal-cfb_byte.o \
bcal-cfb_bit.o bcal-ofb.o bcal-ctr.o
bcal-cfb_bit.o bcal-ofb.o bcal-ctr.o bcal-cmac.o cmacvs.o
$(ALGO_NAME)_NESSIE_TEST := test nessie
$(ALGO_NAME)_PERFORMANCE_TEST := performance

View File

@ -14,7 +14,7 @@ $(ALGO_NAME)_TEST_BIN := main-aes-test.o $(CLI_STD) \
nessie_bc_test.o nessie_common.o performance_test.o memxor.o \
bcal_aes128.o bcal_aes192.o bcal_aes256.o bcal-basic.o bcal-cbc.o \
keysize_descriptor.o dump-asm.o dump-decl.o bcal-cfb_byte.o \
bcal-cfb_bit.o bcal-ofb.o bcal-ctr.o
bcal-cfb_bit.o bcal-ofb.o bcal-ctr.o bcal-cmac.o
$(ALGO_NAME)_NESSIE_TEST := test nessie
$(ALGO_NAME)_PERFORMANCE_TEST := performance

View File

@ -41,6 +41,8 @@
#include "bcal-cfb_bit.h"
#include "bcal-ofb.h"
#include "bcal-ctr.h"
#include "bcal-cmac.h"
#include "cmacvs.h"
#include <stdint.h>
#include <string.h>
@ -49,6 +51,13 @@
char* algo_name = "AES";
const bcdesc_t* algolist[] PROGMEM = {
(bcdesc_t*)&aes128_desc,
(bcdesc_t*)&aes192_desc,
(bcdesc_t*)&aes256_desc,
NULL
};
/*****************************************************************************
* additional validation-functions *
*****************************************************************************/
@ -414,6 +423,110 @@ void testrun_aes128_ctr(void){
bcal_ctr_free(&ctx);
}
void testrun_aes128_cmac(void){
uint8_t key[16];
uint8_t tag[16];
uint8_t plain[64];
uint16_t length[] = { 0, 128, 320, 512 };
bcal_cmac_ctx_t ctx;
uint8_t r,i;
memcpy_P(key, modes_key, 16);
memcpy_P(plain, modes_plain, 64);
cli_putstr_P(PSTR("\r\n** AES128-CMAC-TEST **"));
cli_putstr_P(PSTR("\r\n key: "));
cli_hexdump(key, 128/8);
for(i=0; i<4; ++i){
r = bcal_cmac_init(&aes128_desc, key, 128, &ctx);
cli_putstr_P(PSTR("\r\n init = 0x"));
cli_hexdump(&r, 1);
cli_putstr_P(PSTR("\r\n message: "));
cli_hexdump_block(plain, length[i]/8, 4, 16);
if(r)
return;
bcal_cmac(tag, 128, plain, length[i], &ctx);
cli_putstr_P(PSTR("\r\n tag: "));
cli_hexdump_block(tag, 128/8, 4, 16);
bcal_cmac_free(&ctx);
}
}
/*
Klen = 16
Mlen = 18
Tlen = 2
Key = 3250974e306b4b678f914b514d1e90f6
Msg = cf132fd4ebc25fd3866f1a95a6193a1a9cdf
*/
void testrun_aes128_cmac72(void){
uint8_t key[16]= {
0x32, 0x50, 0x97, 0x4e, 0x30, 0x6b, 0x4b, 0x67,
0x8f, 0x91, 0x4b, 0x51, 0x4d, 0x1e, 0x90, 0xf6
};
uint8_t tag[2];
uint8_t plain[18] = {
0xcf, 0x13, 0x2f, 0xd4, 0xeb, 0xc2, 0x5f, 0xd3,
0x86, 0x6f, 0x1a, 0x95, 0xa6, 0x19, 0x3a, 0x1a,
0x9c, 0xdf,
};
bcal_cmac_ctx_t ctx;
uint8_t r;
cli_putstr_P(PSTR("\r\n** AES128-CMAC-72-TEST **"));
cli_putstr_P(PSTR("\r\n key: "));
cli_hexdump(key, 128/8);
r = bcal_cmac_init(&aes128_desc, key, 128, &ctx);
cli_putstr_P(PSTR("\r\n init = 0x"));
cli_hexdump(&r, 1);
cli_putstr_P(PSTR("\r\n message: "));
cli_hexdump_block(plain, 18, 4, 16);
if(r)
return;
bcal_cmac(tag, 16, plain, 18*8, &ctx);
cli_putstr_P(PSTR("\r\n tag: "));
cli_hexdump_block(tag, 2, 4, 16);
bcal_cmac_free(&ctx);
}
/*
Count = 0
Klen = 24
Mlen = 0
Tlen = 2
Key = 2b2aaa666be161ed16648e862ac9bd1e317f71bc69e268b5
Msg = 00
*/
void testrun_aes192_cmac0(void){
uint8_t key[24]= {
0x2b, 0x2a, 0xaa, 0x66, 0x6b, 0xe1, 0x61, 0xed,
0x16, 0x64, 0x8e, 0x86, 0x2a, 0xc9, 0xbd, 0x1e,
0x31, 0x7f, 0x71, 0xbc, 0x69, 0xe2, 0x68, 0xb5
};
uint8_t tag[2];
uint8_t plain[1] = {
0x00
};
bcal_cmac_ctx_t ctx;
uint8_t r;
cli_putstr_P(PSTR("\r\n** AES192-CMAC-0-TEST **"));
cli_putstr_P(PSTR("\r\n key: "));
cli_hexdump(key, 192/8);
r = bcal_cmac_init(&aes192_desc, key, 192, &ctx);
cli_putstr_P(PSTR("\r\n init = 0x"));
cli_hexdump(&r, 1);
if(r)
return;
bcal_cmac(tag, 16, plain, 0*8, &ctx);
cli_putstr_P(PSTR("\r\n tag: "));
cli_hexdump_block(tag, 2, 4, 16);
bcal_cmac_free(&ctx);
}
/*****************************************************************************/
void testrun_performance_aes128(void){
@ -545,31 +658,45 @@ void testrun_performance_aes(void){
* main *
*****************************************************************************/
const char nessie_str[] PROGMEM = "nessie";
const char test_str[] PROGMEM = "test";
const char testkey_str[] PROGMEM = "testkey";
const char testcbc_str[] PROGMEM = "testcbc";
const char testcfb8_str[] PROGMEM = "testcfb8";
const char testcfb1_str[] PROGMEM = "testcfb1";
const char testofb_str[] PROGMEM = "testofb";
const char testctr_str[] PROGMEM = "testctr";
const char performance_str[] PROGMEM = "performance";
const char dump_str[] PROGMEM = "dump";
const char echo_str[] PROGMEM = "echo";
const char nessie_str[] PROGMEM = "nessie";
const char test_str[] PROGMEM = "test";
const char testkey_str[] PROGMEM = "testkey";
const char testcbc_str[] PROGMEM = "testcbc";
const char testcfb8_str[] PROGMEM = "testcfb8";
const char testcfb1_str[] PROGMEM = "testcfb1";
const char testofb_str[] PROGMEM = "testofb";
const char testctr_str[] PROGMEM = "testctr";
const char testcmac_str[] PROGMEM = "testcmac";
const char testcmac72_str[] PROGMEM = "testcmac72";
const char testcmac0_str[] PROGMEM = "testcmac0";
const char cmacvs_list_str[] PROGMEM = "cmacvs_list";
const char cmacvs_set_str[] PROGMEM = "cmacvs_set";
const char cmacvs_test1_str[] PROGMEM = "cmacvs_test1";
const char cmacvs_test2_str[] PROGMEM = "cmacvs_test2";
const char performance_str[] PROGMEM = "performance";
const char dump_str[] PROGMEM = "dump";
const char echo_str[] PROGMEM = "echo";
cmdlist_entry_t cmdlist[] PROGMEM = {
{ nessie_str, NULL, testrun_nessie_aes },
{ test_str, NULL, testrun_test_aes},
{ testkey_str, NULL, testrun_testkey_aes},
{ testcbc_str, NULL, testrun_aes128_cbc},
{ testcfb8_str, NULL, testrun_aes128_cfb8},
{ testcfb1_str, NULL, testrun_aes128_cfb1},
{ testofb_str, NULL, testrun_aes128_ofb},
{ testctr_str, NULL, testrun_aes128_ctr},
{ performance_str, NULL, testrun_performance_aes},
{ dump_str, (void*)1, (void_fpt)dump},
{ echo_str, (void*)1, (void_fpt)echo_ctrl},
{ NULL, NULL, NULL}
{ nessie_str, NULL, testrun_nessie_aes },
{ test_str, NULL, testrun_test_aes },
{ testkey_str, NULL, testrun_testkey_aes },
{ testcbc_str, NULL, testrun_aes128_cbc },
{ testcfb8_str, NULL, testrun_aes128_cfb8 },
{ testcfb1_str, NULL, testrun_aes128_cfb1 },
{ testofb_str, NULL, testrun_aes128_ofb },
{ testctr_str, NULL, testrun_aes128_ctr },
{ testcmac_str, NULL, testrun_aes128_cmac },
{ testcmac72_str, NULL, testrun_aes128_cmac72 },
{ testcmac0_str, NULL, testrun_aes192_cmac0 },
{ cmacvs_list_str, NULL, cmacvs_listalgos },
{ cmacvs_set_str, (void*)1, (void_fpt)cmacvs_setalgo },
{ cmacvs_test1_str, NULL, cmacvs_test1 },
{ cmacvs_test2_str, NULL, cmacvs_test2 },
{ performance_str, NULL, testrun_performance_aes },
{ dump_str, (void*)1, (void_fpt)dump },
{ echo_str, (void*)1, (void_fpt)echo_ctrl },
{ NULL, NULL, NULL }
};
@ -578,6 +705,8 @@ int main (void){
cli_rx = (cli_rx_fpt)uart0_getc;
cli_tx = (cli_tx_fpt)uart0_putc;
cmacvs_algolist=(bcdesc_t**)algolist;
cmacvs_algo=(bcdesc_t*)&aes128_desc;
for(;;){
cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
cli_putstr(algo_name);

21
testconf/AES_CMAC.conf Normal file
View File

@ -0,0 +1,21 @@
[AES-128]
algo=a
#file_0=testvectors/cmacvs/CMACGenAES128.fax
#file_0_test=gen
file_0=testvectors/cmacvs/CMACVerAES128.fax
file_0_test=ver
[AES-192]
algo=a
file_0=testvectors/cmacvs/CMACGenAES192.fax
file_0_test=gen
file_1=testvectors/cmacvs/CMACVerAES192.fax
file_1_test=ver
[AES-256]
algo=a
file_0=testvectors/cmacvs/CMACGenAES256.fax
file_0_test=gen
file_1=testvectors/cmacvs/CMACVerAES256.fax
file_1_test=ver

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff