+Present +some fixes at nessie_common
This commit is contained in:
parent
d4b9cfc34c
commit
b567660a24
|
@ -0,0 +1,55 @@
|
|||
/**
|
||||
*
|
||||
* author: Daniel Otte
|
||||
* email: daniel.otte@rub.de
|
||||
* license: GPLv3
|
||||
*
|
||||
* components to help implementing simple command based interaction
|
||||
*
|
||||
**/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <avr/pgmspace.h>
|
||||
|
||||
int16_t findstring_d0(const char* str, const char* v){
|
||||
uint8_t i=0;
|
||||
while(*v){
|
||||
if(!strcmp(str, v)){
|
||||
return i;
|
||||
}
|
||||
while(*v++) /* go to the next string */
|
||||
;
|
||||
++i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int16_t findstring_d0_P(const char* str, PGM_P v){
|
||||
uint8_t i=0;
|
||||
while(pgm_read_byte(v)){
|
||||
if(!strcmp_P(str, v)){
|
||||
return i;
|
||||
}
|
||||
while(pgm_read_byte(v++)) /* go to the next string */
|
||||
;
|
||||
++i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int16_t execcommand_d0_P(const char* str, PGM_P v, void(*fpt[])(void) ){
|
||||
uint8_t i=0;
|
||||
while(pgm_read_byte(v)){
|
||||
if(!strcmp_P(str, v)){
|
||||
(fpt[i])();
|
||||
return i;
|
||||
}
|
||||
while(pgm_read_byte(v++)) /* go to the next string */
|
||||
;
|
||||
++i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
#ifndef CLI_H_
|
||||
#define CLI_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <avr/pgmspace.h>
|
||||
|
||||
int16_t findstring_d0(const char* str, const char* v);
|
||||
int16_t findstring_d0_P(const char* str, PGM_P v);
|
||||
|
||||
int16_t execcommand_d0_P(const char* str, PGM_P v, void(*fpt[])(void) );
|
||||
#endif /*CLI_H_*/
|
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
* present test-suit
|
||||
*
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include "serial-tools.h"
|
||||
#include "uart.h"
|
||||
#include "debug.h"
|
||||
|
||||
#include "present.h"
|
||||
#include "nessie_bc_test.h"
|
||||
#include "cli.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
char* cipher_name = "Present";
|
||||
|
||||
/*****************************************************************************
|
||||
* additional validation-functions *
|
||||
*****************************************************************************/
|
||||
void present_genctx_dummy(uint8_t* key, uint16_t keysize_b, present_ctx_t* ctx){
|
||||
present_init(key, keysize_b, ctx);
|
||||
}
|
||||
|
||||
void testrun_nessie_present(void){
|
||||
nessie_bc_ctx.blocksize_B = 8;
|
||||
nessie_bc_ctx.keysize_b = 80;
|
||||
nessie_bc_ctx.name = cipher_name;
|
||||
nessie_bc_ctx.ctx_size_B = sizeof(present_ctx_t);
|
||||
nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)present_enc;
|
||||
nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)present_dec;
|
||||
nessie_bc_ctx.cipher_genctx = (nessie_bc_gen_fpt)present_genctx_dummy;
|
||||
|
||||
nessie_bc_run();
|
||||
}
|
||||
|
||||
void testrun_selfenc(uint8_t* key, uint8_t* buffer){
|
||||
present_ctx_t ctx;
|
||||
uart_putstr_P(PSTR("\r\nkey : "));
|
||||
uart_hexdump(key, 10);
|
||||
uart_putstr_P(PSTR("\r\nplain : "));
|
||||
uart_hexdump(buffer, 8);
|
||||
present_init(key, 80, &ctx);
|
||||
present_enc(buffer, &ctx);
|
||||
uart_putstr_P(PSTR("\r\ncipher: "));
|
||||
uart_hexdump(buffer, 8);
|
||||
present_dec(buffer, &ctx);
|
||||
uart_putstr_P(PSTR("\r\nplain : "));
|
||||
uart_hexdump(buffer, 8);
|
||||
uart_putstr_P(PSTR("\r\n"));
|
||||
}
|
||||
|
||||
void testrun_self_present(void){
|
||||
uint8_t buffer[8], key[10];
|
||||
uart_putstr_P(PSTR("\r\n\r\n=== Testvectors from the paper ===\r\n"));
|
||||
|
||||
memset(buffer, 0, 8);
|
||||
memset(key, 0, 10);
|
||||
testrun_selfenc(key, buffer);
|
||||
|
||||
memset(buffer, 0, 8);
|
||||
memset(key, 0xFF, 10);
|
||||
testrun_selfenc(key, buffer);
|
||||
|
||||
memset(buffer, 0xFF, 8);
|
||||
memset(key, 0, 10);
|
||||
testrun_selfenc(key, buffer);
|
||||
|
||||
memset(buffer, 0xFF, 8);
|
||||
memset(key, 0xFF, 10);
|
||||
testrun_selfenc(key, buffer);
|
||||
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* 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\0");
|
||||
void_fpt v[] = {testrun_nessie_present, testrun_self_present};
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
}
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
#include "serpent.h"
|
||||
#include "nessie_bc_test.h"
|
||||
#include "cli.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
@ -47,6 +48,8 @@ void testrun_nessie_serpent(void){
|
|||
* main *
|
||||
*****************************************************************************/
|
||||
|
||||
typedef void(*void_fpt)(void);
|
||||
|
||||
int main (void){
|
||||
char str[20];
|
||||
DEBUG_INIT();
|
||||
|
@ -56,12 +59,14 @@ int main (void){
|
|||
uart_putstr(cipher_name);
|
||||
uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
|
||||
|
||||
restart:
|
||||
PGM_P u = PSTR("nessie\0test\0");
|
||||
void_fpt v[] = {testrun_nessie_serpent, testrun_nessie_serpent};
|
||||
|
||||
while(1){
|
||||
if (!getnextwordn(str,20)) {DEBUG_S("DBG: W1\r\n"); goto error;}
|
||||
if (strcmp(str, "nessie")) {DEBUG_S("DBG: 1b\r\n"); goto error;}
|
||||
testrun_nessie_serpent();
|
||||
goto restart;
|
||||
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");
|
||||
|
|
|
@ -146,7 +146,7 @@ void nessie_print_header(char* name,
|
|||
uart_putstr(str);
|
||||
uart_putstr_P(PSTR(" bits"));
|
||||
}
|
||||
uart_putstr_P(PSTR(" bits"));
|
||||
uart_putstr_P(PSTR("\r\n"));
|
||||
}
|
||||
|
||||
void nessie_print_footer(void){
|
||||
|
|
|
@ -0,0 +1,105 @@
|
|||
/**
|
||||
* present.c
|
||||
* a implementation of the PRESENT block-cipher
|
||||
* author: Daniel Otte
|
||||
* email: daniel.otte@rub.de
|
||||
* license: GPLv3
|
||||
*
|
||||
* */
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include "present.h"
|
||||
|
||||
static uint8_t sbox(uint8_t b){
|
||||
uint8_t sb[]={0xC, 0x5, 0x6, 0xB,
|
||||
0x9, 0x0, 0xA, 0xD,
|
||||
0x3, 0xE, 0xF, 0x8,
|
||||
0x4, 0x7, 0x1, 0x2 };
|
||||
return (((sb[b>>4])<<4)|(sb[b&0xf]));
|
||||
}
|
||||
|
||||
static uint8_t sbox_inv(uint8_t b){
|
||||
uint8_t sb[]={0x5, 0xE, 0xF, 0x8,
|
||||
0xC, 0x1, 0x2, 0xD,
|
||||
0xB, 0x4, 0x6, 0x3,
|
||||
0x0, 0x7, 0x9, 0xA };
|
||||
return (((sb[b>>4])<<4)|(sb[b&0xf]));
|
||||
}
|
||||
|
||||
#define SHR_O(a) c=(a)&1; (a)>>=1;
|
||||
#define SHR_I(a) (a)=(c?0x8000:0x0000) | ((a)>>1);
|
||||
|
||||
static void p(uint16_t* o, uint8_t* i){
|
||||
uint8_t c;
|
||||
uint8_t m,n;
|
||||
for(m=0; m<8; ++m){
|
||||
for(n=0; n<2; ++n){
|
||||
SHR_O(i[m]);
|
||||
SHR_I(o[0]);
|
||||
SHR_O(i[m]);
|
||||
SHR_I(o[1]);
|
||||
SHR_O(i[m]);
|
||||
SHR_I(o[2]);
|
||||
SHR_O(i[m]);
|
||||
SHR_I(o[3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void p_inv(uint8_t* o, uint8_t* i){
|
||||
uint8_t tmp[8];
|
||||
p((uint16_t*)tmp, i);
|
||||
p((uint16_t*)o, tmp);
|
||||
}
|
||||
|
||||
void present_init(const uint8_t* key, uint8_t keysize_b, present_ctx_t* ctx){
|
||||
uint8_t buffer[10], tmp[2];
|
||||
uint8_t i;
|
||||
memcpy(buffer, key, 10);
|
||||
memcpy(&(ctx->k[0]), buffer+2, 8);
|
||||
for(i=1; i<32; ++i){
|
||||
/* rotate buffer 19 right */
|
||||
memcpy(tmp, buffer, 2);
|
||||
memmove(buffer, buffer+2, 8);
|
||||
memcpy(buffer+8, tmp, 2);
|
||||
/* three shifts to do*/
|
||||
tmp[1]=buffer[0];
|
||||
*((uint64_t*)buffer)>>=3;
|
||||
*((uint16_t*)(buffer+8))>>=3;
|
||||
buffer[9] |= tmp[1]<<5;
|
||||
buffer[7] |= tmp[0]<<5;
|
||||
/* rotating done now substitution */
|
||||
buffer[9] = (sbox(buffer[9])&0xF0) | ((buffer[9])&0x0F);
|
||||
/* xor with round counter */
|
||||
*((uint16_t*)(buffer+1)) ^= (uint16_t)i<<7;
|
||||
memcpy(&(ctx->k[i]), buffer+2, 8);
|
||||
}
|
||||
}
|
||||
|
||||
void present_enc(void* buffer, present_ctx_t* ctx){
|
||||
uint8_t i,j,tmp[8];
|
||||
for(i=0; i<31; ++i){
|
||||
*((uint64_t*)buffer) ^= ctx->k[i];
|
||||
for(j=0; j<8; ++j){
|
||||
tmp[j] = sbox(((uint8_t*)buffer)[j]);
|
||||
}
|
||||
p((uint16_t*)buffer, tmp);
|
||||
}
|
||||
*((uint64_t*)buffer) ^= ctx->k[31];
|
||||
}
|
||||
|
||||
|
||||
void present_dec(void* buffer, present_ctx_t* ctx){
|
||||
uint8_t j,tmp[8];
|
||||
int8_t i;
|
||||
*((uint64_t*)buffer) ^= ctx->k[31];
|
||||
|
||||
for(i=30; i>=0; --i){
|
||||
p_inv(tmp, (uint8_t*)buffer);
|
||||
for(j=0; j<8; ++j){
|
||||
((uint8_t*)buffer)[j] = sbox_inv(tmp[j]);
|
||||
}
|
||||
*((uint64_t*)buffer) ^= ctx->k[i];
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef PRESENT_H_
|
||||
#define PRESENT_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct present_ctx_st{
|
||||
uint64_t k[32];
|
||||
} present_ctx_t;
|
||||
|
||||
|
||||
void present_init(const uint8_t* key, uint8_t keysize_b, present_ctx_t* ctx);
|
||||
void present_enc(void* buffer, present_ctx_t* ctx);
|
||||
void present_dec(void* buffer, present_ctx_t* ctx);
|
||||
|
||||
|
||||
#endif /*PRESENT_H_*/
|
|
@ -0,0 +1,13 @@
|
|||
# Makefile for present
|
||||
ALGO_NAME := PRESENT
|
||||
|
||||
# comment out the following line for removement of present from the build process
|
||||
BLOCK_CIPHERS += $(ALGO_NAME)
|
||||
|
||||
|
||||
$(ALGO_NAME)_OBJ := present.o
|
||||
$(ALGO_NAME)_TEST_BIN := main-present-test.o debug.o uart.o serial-tools.o \
|
||||
present.o nessie_bc_test.o nessie_common.o cli.o
|
||||
$(ALGO_NAME)_NESSIE_TEST := "nessie"
|
||||
$(ALGO_NAME)_PEROFRMANCE_TEST := "performance"
|
||||
|
|
@ -8,7 +8,7 @@ BLOCK_CIPHERS += $(ALGO_NAME)
|
|||
$(ALGO_NAME)_OBJ := serpent.o serpent-sboxes-bitslice.o
|
||||
$(ALGO_NAME)_TEST_BIN := main-serpent-test.o debug.o uart.o serial-tools.o \
|
||||
serpent.o serpent-sboxes-bitslice.o nessie_bc_test.o \
|
||||
nessie_common.o
|
||||
nessie_common.o cli.o
|
||||
$(ALGO_NAME)_NESSIE_TEST := "nessie"
|
||||
$(ALGO_NAME)_PEROFRMANCE_TEST := "performance"
|
||||
|
||||
|
|
Loading…
Reference in New Issue