adding trivium

This commit is contained in:
bg 2011-07-20 01:06:21 +02:00
parent c88d997521
commit 94f6d6aad5
10 changed files with 6388 additions and 4 deletions

View File

@ -19,12 +19,18 @@
=end
def skip_header(file)
i=0
begin
i += 1
l = file.gets().strip
end until /[*]{10,}.*/.match(l)
begin
l = file.gets().strip
end until /[*]{10,}.*/.match(l)
end until i>10 or m=/[*]{10,}.*/.match(l)
if(!m)
file.seek(0, IO::SEEK_SET)
else
begin
l = file.gets().strip
end until /[*]{10,}.*/.match(l)
end
begin
l = file.gets().strip
end until /[=]{5,}.*/.match(l)

13
mkfiles/trivium.mk Normal file
View File

@ -0,0 +1,13 @@
# Makefile for Trivium
ALGO_NAME := TRIVIUM
# comment out the following line for removement of Trivium from the build process
STREAM_CIPHERS += $(ALGO_NAME)
$(ALGO_NAME)_DIR := trivium/
$(ALGO_NAME)_OBJ := trivium.o
$(ALGO_NAME)_INCDIR := memxor/ scal/
$(ALGO_NAME)_TEST_BIN := main-trivium-test.o $(CLI_STD) $(SCAL_STD) scal_trivium.o
$(ALGO_NAME)_NESSIE_TEST := "nessie"
$(ALGO_NAME)_PERFORMANCE_TEST := "performance"
$(ALGO_NAME)_DEF := NESSIE_ESTREAM=1

55
scal/scal_trivium.c Normal file
View File

@ -0,0 +1,55 @@
/* scal_trivium.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2011 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 <stdlib.h>
#include <stdint.h>
#include "streamcipher_descriptor.h"
#include "keysize_descriptor.h"
#include "trivium.h"
const char trivium_str[] = "Trivium";
const uint8_t trivium_keysize_desc[] = {
KS_TYPE_LIST, 1, KS_INT(80),
KS_TYPE_TERMINATOR };
const uint8_t trivium_ivsize_desc[] = {
KS_TYPE_LIST, 3, KS_INT(32), KS_INT(64), KS_INT(80),
KS_TYPE_TERMINATOR };
const scdesc_t trivium_desc = {
SCDESC_TYPE_STREAMCIPHER, /* abstraction layer type designator */
SC_INIT_TYPE_5|SC_GEN_TYPE_1, /* flags*/
trivium_str, /* name string pointer */
sizeof(trivium_ctx_t), /* size of context */
8, /* blocksize */
{(void_fpt)trivium_init}, /* init function pointer */
{(void_fpt)trivium_getbyte}, /* key stream generator function pointer */
{(void_fpt)NULL}, /* key stream generator for random access function pointer */
(sc_free_fpt)NULL, /* free function pointer */
trivium_keysize_desc, /* key size descriptor pointer */
trivium_ivsize_desc /* iv size descriptor pointer */
};

27
scal/scal_trivium.h Normal file
View File

@ -0,0 +1,27 @@
/* scal_trivium.h */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2011 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 SCAL_TRIVIUM_H_
#define SCAL_TRIVIUM_H_
#include "streamcipher_descriptor.h"
extern const scdesc_t trivium_desc;
#endif /* SCAL_TRIVIUM_H_ */

View File

@ -0,0 +1,141 @@
/* main-trivium-test.c */
/*
This file is part of the ARM-Crypto-Lib.
Copyright (C) 2006-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 "main-test-common.h"
#include "trivium.h"
#include "scal_trivium.h"
#include "scal-basic.h"
#include "scal-nessie.h"
#include "performance_test.h"
char* algo_name = "Trivium";
/*****************************************************************************
* additional validation-functions *
*****************************************************************************/
void testrun_nessie_trivium(void){
scal_nessie_set_estream(1);
scal_nessie_run(&trivium_desc);
}
void testrun_trivium(void){
uint8_t key[10];
uint8_t iv[4];
uint8_t buffer[64];
scgen_ctx_t ctx;
memset(key, 0, 10);
memset(iv, 0, 4);
key[0] = 0x80;
scal_cipher_init(&trivium_desc, key, 80, iv, 32, &ctx);
scal_cipher_gen_fillblock(buffer, 64, &ctx);
cli_putstr("\r\nTest:\r\n Key = ");
cli_hexdump(key, 10);
cli_putstr("\r\n IV = ");
cli_hexdump(iv, 4);
cli_putstr("\r\n Cipher = ");
cli_hexdump_block(buffer, 64, 4, 16);
scal_cipher_free(&ctx);
key[0] = 0x40;
scal_cipher_init(&trivium_desc, key, 80, iv, 32, &ctx);
scal_cipher_gen_fillblock(buffer, 64, &ctx);
cli_putstr("\r\nTest:\r\n Key = ");
cli_hexdump(key, 10);
cli_putstr("\r\n IV = ");
cli_hexdump(iv, 4);
cli_putstr("\r\n Cipher = ");
cli_hexdump_block(buffer, 64, 4, 16);
scal_cipher_free(&ctx);
key[0] = 0x20;
scal_cipher_init(&trivium_desc, key, 80, iv, 32, &ctx);
scal_cipher_gen_fillblock(buffer, 64, &ctx);
cli_putstr("\r\nTest:\r\n Key = ");
cli_hexdump(key, 10);
cli_putstr("\r\n IV = ");
cli_hexdump(iv, 4);
cli_putstr("\r\n Cipher = ");
cli_hexdump_block(buffer, 64, 4, 16);
scal_cipher_free(&ctx);
key[0] = 0x10;
scal_cipher_init(&trivium_desc, key, 80, iv, 32, &ctx);
scal_cipher_gen_fillblock(buffer, 64, &ctx);
cli_putstr("\r\nTest:\r\n Key = ");
cli_hexdump(key, 10);
cli_putstr("\r\n IV = ");
cli_hexdump(iv, 4);
cli_putstr("\r\n Cipher = ");
cli_hexdump_block(buffer, 64, 4, 16);
scal_cipher_free(&ctx);
}
void testrun_performance_trivium(void){
uint64_t t;
char str[16];
uint8_t key[10], iv[10];
trivium_ctx_t ctx;
calibrateTimer();
print_overhead();
memset(key, 0, 10);
memset(iv, 0, 10);
startTimer(1);
trivium_init(key, 80, iv, 80, &ctx);
t = stopTimer();
cli_putstr("\r\n\tctx-gen time: ");
ultoa((unsigned long)t, str, 10);
cli_putstr(str);
startTimer(1);
trivium_enc(&ctx);
t = stopTimer();
cli_putstr("\r\n\tencrypt time: ");
ultoa((unsigned long)t, str, 10);
cli_putstr(str);
cli_putstr("\r\n");
}
/*****************************************************************************
* main *
*****************************************************************************/
const char nessie_str[] = "nessie";
const char test_str[] = "test";
const char performance_str[] = "performance";
const char echo_str[] = "echo";
cmdlist_entry_t cmdlist[] = {
{ nessie_str, NULL, testrun_nessie_trivium},
{ test_str, NULL, testrun_trivium},
{ performance_str, NULL, testrun_performance_trivium},
{ echo_str, (void*)1, (void_fpt)echo_ctrl},
{ NULL, NULL, NULL}
};
int main (void){
main_setup();
for(;;){
welcome_msg(algo_name);
cmd_interface(cmdlist);
}
}

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

115
trivium/trivium.c Normal file
View File

@ -0,0 +1,115 @@
/* trivium.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/>.
*/
/**
*
* author: Daniel Otte
* email: daniel.otte@rub.de
* license: GPLv3
*
*/
#include <stdint.h>
#include <string.h>
#include "trivium.h"
#define G(i) ((((*ctx)[(i)/8])>>(((i)%8)))&1)
#define S(i,v) ((*ctx)[(i)/8] = (((*ctx)[(i)/8]) & (uint8_t)~(1<<((i)%8))) | ((v)<<((i)%8)))
uint8_t trivium_enc(trivium_ctx_t* ctx){
uint8_t t1,t2,t3,z;
t1 = G(65) ^ G(92);
t2 = G(161) ^ G(176);
t3 = G(242) ^ G(287);
z = t1^t2^t3;
t1 ^= (G(90) & G(91)) ^ G(170);
t2 ^= (G(174) & G(175)) ^ G(263);
t3 ^= (G(285) & G(286)) ^ G(68);
/* shift whole state and insert ts later */
uint8_t i,c1=0,c2;
for(i=0; i<36; ++i){
c2=(((*ctx)[i])>>7);
(*ctx)[i] = (((*ctx)[i])<<1)|c1;
c1=c2;
}
/* insert ts */
S(0, t3);
S(93, t1);
S(177, t2);
return z?0x080:0x00;
}
uint8_t trivium_getbyte(trivium_ctx_t *ctx){
uint8_t r=0, i=0;
do{
r>>=1;
r |= trivium_enc(ctx);
}while(++i<8);
return r;
}
#define KEYSIZE_B ((keysize_b+7)/8)
#define IVSIZE_B ((ivsize_b +7)/8)
static const uint8_t rev_table[16] = {
0x00, 0x08, 0x04, 0x0C, /* 0000 1000 0100 1100 */
0x02, 0x0A, 0x06, 0x0E, /* 0010 1010 0110 1110 */
0x01, 0x09, 0x05, 0x0D, /* 0001 1001 0101 1101 */
0x03, 0x0B, 0x07, 0x0F /* 0011 1011 0111 1111 */
};
void trivium_init(const void* key, uint16_t keysize_b,
const void* iv, uint16_t ivsize_b,
trivium_ctx_t* ctx){
uint16_t i;
uint8_t c1,c2;
uint8_t t1,t2;
memset((*ctx)+KEYSIZE_B, 0, 35-KEYSIZE_B);
c2=0;
c1=KEYSIZE_B;
do{
t1 = ((uint8_t*)key)[--c1];
t2 = (rev_table[t1&0x0f]<<4)|(rev_table[t1>>4]);
(*ctx)[c2++] = t2;
}while(c1!=0);
c2=12;
c1=IVSIZE_B;
do{
t1 = ((uint8_t*)iv)[--c1];
t2 = (rev_table[t1&0x0f]<<4)|(rev_table[t1>>4]);
(*ctx)[c2++] = t2;
}while(c1!=0);
for(i=12+IVSIZE_B; i>10; --i){
c2=(((*ctx)[i])<<5);
(*ctx)[i] = (((*ctx)[i])>>3)|c1;
c1=c2;
}
(*ctx)[35] = 0xE0;
for(i=0; i<4*288; ++i){
trivium_enc(ctx);
}
}

30
trivium/trivium.h Normal file
View File

@ -0,0 +1,30 @@
/* trivium.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/>.
*/
#ifndef TRIVIUM_H_
#define TRIVIUM_H_
typedef uint8_t trivium_ctx_t[36]; /* 288bit */
uint8_t trivium_enc(trivium_ctx_t* ctx);
uint8_t trivium_getbyte(trivium_ctx_t* ctx);
void trivium_init(const void* key, uint16_t keysize_b,
const void* iv, uint16_t ivsize_b,
trivium_ctx_t* ctx);
#endif /*TRIVIUM_H_*/