Commit 99398ae8 authored by bg nerilex's avatar bg nerilex

adding pi-cipher

parent 7eadad3d
......@@ -80,7 +80,7 @@ MCU_STUB_FILES := $(sort $(wildcard $(MCU_STUB_DIR)/*.mk))
CORE_STUB_FILES := $(sort $(wildcard $(CORE_STUB_DIR)/*.mk))
ALGORITHM_VARS = NAME INFO DIR INCDIR OBJ DEFS CFLAGS ASFLAGS TYPE TESTBIN
BOARD_VARS = NAME INFO DIR INCDIR OBJ DEFS CFLAGS ASFLAGS LDFLAGS MCU INTERFACE FREQUENCY FLASHCMD RUNCMD
BOARD_VARS = NAME INFO DIR INCDIR OBJ DEFS CFLAGS ASFLAGS LDFLAGS MCU INTERFACE FREQUENCY FLASHCMD RUNCMD DBGCMD
MCU_VARS = NAME INFO DIR INCDIR OBJ DEFS CFLAGS ASFLAGS LDFLAGS CORE
CORE_VARS = NAME INFO DIR INCDIR OBJ DEFS CFLAGS ASFLAGS LDFLAGS PREFIX MARCH
......@@ -91,10 +91,12 @@ CORE_VARS = NAME INFO DIR INCDIR OBJ DEFS CFLAGS ASFLAGS LDFLAGS PREFIX MAR
# takes a list of variable names and sets them to an empty string
set_empty = $(foreach VAR,$(1),$(eval $(VAR) := ))
prefix_var = $(eval $(call tr,-,_,$(1))_$(2) := $($(2)))
prefix_var = $(eval $(call tr,-,_,$(1))_$(2) = $($(2)))
prefix_vars = $(foreach VAR,$(2),$(call prefix_var,$(1),$(VAR)))
testbin = $(TEST_DIR)/main-$(call lc, $(strip $(1)))-test
#############
#
......@@ -221,6 +223,9 @@ $(foreach a, $(ALGORITHMS), $(eval ALGORITHM = $(a)) $(eval \
))
$(foreach a, $(ALGORITHMS), $(eval ALGORITHM = $(a)) $(eval \
$(a)_TESTTARGET := $(call testbin, $(a)) \
))
#-------------------------------------------------------------------------------
......@@ -324,7 +329,7 @@ endef
$(foreach a, $(ALGORITHMS), $(eval ALGORITHM = $(a)) \
$(eval $(call MainTestElf_Template, \
$(TEST_DIR)/main-$(call lc, $(a))-test.elf, \
$(call testbin, $(a)).elf, \
$($(a)_BINOBJ) $($(a)_TESTBINOBJ) \
)) \
)
......@@ -335,24 +340,34 @@ all: $(foreach algo, $(ALGORITHMS), $($(algo)_BINOBJ))
#-------------------------------------------------------------------------------
define DEP_TEMPLATE
define Dep_Template
$(1): $(2)
endef
$(foreach algo, $(ALGORITHMS), $(eval ALGORITHM = $(algo)) $(eval $(call DEP_TEMPLATE, \
$(algo)_TESTELF, \
$(TEST_DIR)/main-$(call lc, $(algo))-test.elf \
)))
$(foreach algo, $(ALGORITHMS), $(eval ALGORITHM = $(algo)) $(eval $(call DEP_TEMPLATE, \
$(algo)_TESTBIN, \
$(TEST_DIR)/main-$(call lc, $(algo))-test.bin \
)))
$(foreach algo, $(ALGORITHMS), \
$(foreach ext, elf bin hex, \
$(eval ALGORITHM=$(algo)) \
$(eval $(call Dep_Template, \
$(algo)_TEST$(call uc,$(ext)),\
$(call testbin,$(algo)).$(ext) \
)) \
) \
)
$(foreach algo, $(ALGORITHMS), $(eval ALGORITHM = $(algo)) $(eval $(call DEP_TEMPLATE, \
$(algo)_TESTHEX, \
$(TEST_DIR)/main-$(call lc, $(algo))-test.hex \
)))
#$(foreach algo, $(ALGORITHMS), $(eval ALGORITHM = $(algo)) $(eval $(call DEP_TEMPLATE, \
# $(algo)_TESTELF, \
# $(TEST_DIR)/main-$(call lc, $(algo))-test.elf \
#)))
#
#$(foreach algo, $(ALGORITHMS), $(eval ALGORITHM = $(algo)) $(eval $(call DEP_TEMPLATE, \
# $(algo)_TESTBIN, \
# $(TEST_DIR)/main-$(call lc, $(algo))-test.bin \
#)))
#
#$(foreach algo, $(ALGORITHMS), $(eval ALGORITHM = $(algo)) $(eval $(call DEP_TEMPLATE, \
# $(algo)_TESTHEX, \
# $(TEST_DIR)/main-$(call lc, $(algo))-test.hex \
#)))
#-------------------------------------------------------------------------------
......@@ -374,14 +389,13 @@ $(foreach algo, $(ALGORITHMS), $(eval ALGORITHM = $(algo)) $(eval $(call DEP_TEM
#-------------------------------------------------------------------------------
define Flash_Template
$(1)_FLASH: $(2)
$(1)_FLASH: $(strip $(2)).bin
@echo "[flash]: $(2)"
$(D)$(subst <PROGRAM_BIN>,$(call first,$(2)),$($(BOARD)_FLASHCMD))
$(D)$(call $(BOARD)_FLASHCMD, $(strip $(2)))
endef
$(foreach algo, $(ALGORITHMS), $(eval ALGORITHM = $(algo)) $(eval $(call Flash_Template, \
$(algo), \
$(TEST_DIR)/main-$(call lc, $(algo))-test.bin \
$(algo), $(call testbin, $(algo)) \
)))
#-------------------------------------------------------------------------------
......@@ -425,20 +439,38 @@ blockcipher_size: $(foreach algo, $(BLOCK_CIPHERS), $(algo)_SIZE)
tests: $(foreach a, $(ALGORITHMS), $(a)_TESTBIN)
#-------------------------------------------------------------------------------
#$(TEST_DIR)/main-$(call lc, $(strip $(1)))-test
define TestRun_Template
.phony: $(1)_TESTRUN
$(1)_TESTRUN: $(1)_FLASH
@echo "[test]: $(1)"
$(D)$(call $(BOARD)_RUNCMD, $(1))
$(D)$(call $(BOARD)_RUNCMD, $(2))
# $(RUBY) $(GET_TEST) $(TESTPORT) $(TESTPORTBAUDR) 8 1 nessie $(TESTLOG_DIR)$(TESTPREFIX) $(2)
endef
$(foreach algo,$(ALGORITHMS),$(eval $(call TestRun_Template, $(algo), $(call lc,$(algo)) )))
$(foreach algo,$(ALGORITHMS),\
$(eval ALGORITHM = $(algo))\
$(eval $(call TestRun_Template,$(algo),$(call testbin, $(algo)))))
all_testrun: $(foreach algo, $(ALGORITHMS), $(algo)_TESTRUN)
#-------------------------------------------------------------------------------
define DbgRun_Template
.phony: $(1)_DBGRUN
$(1)_DBGRUN: $(1)_FLASH
@echo "[dbg]: $(1)"
$(D)$(call $(BOARD)_DBGCMD, $(2))
endef
$(foreach algo,$(ALGORITHMS),\
$(eval ALGORITHM = $(algo))\
$(eval $(call DbgRun_Template,$(algo),$(call testbin, $(algo)))))
#-------------------------------------------------------------------------------
define Obj_Template
$(1)_OBJ: $(2)
endef
......
# Makefile for pi-cipher
NAME := PICIPHER_C
TYPE := AEAD_CIPHER
DIR := algorithm/pi-cipher
INCDIR := algorithm/arcfour
OBJ := pi16cipher.o pi16cipher-asm.o pi16cipher-aux-asm.o pi32cipher.o pi64cipher.o
TESTBIN := main-picipher-test.o arcfour-asm.o $(CLI_STD) performance_test.o
# Makefile for pi-cipher
NAME := PICIPHER_C2
TYPE := AEAD_CIPHER
DIR := algorithm/pi-cipher
INCDIR := algorithm/arcfour
OBJ := pi16cipher.o pi16cipher-asm2.o pi16cipher-aux-asm.o pi32cipher.o pi64cipher.o
TESTBIN := main-picipher-test.o arcfour-asm.o $(CLI_STD) performance_test.o
MCU := ATMEGA644
INFO := simaver emulator
PROGRAMMER =
PROG_PORT =
FLASHCMD := echo doing nothing to flash $$(1)
F_CPU = 16000000
RUNCMD = simavr-ecl -f $(F_CPU) -m $(call lc,$(MCU)) -i $$(1).elf
DBGCMD = simavr-ecl -g -f $(F_CPU) -m $(call lc,$(MCU)) -i $$(1).elf
OPTIMIZE = -Os
DEFS := -D$(call uc, $(MCU)) -DF_CPU=$(F_CPU) -DSIMAVR
# override CFLAGS_A = -MMD -MF$(DEP_DIR)$(patsubst %.o,%.d,$(notdir $(1))) $(DEBUG) $(WARNING) -std=$(CSTD) $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS)
CFLAGS := -mmcu=$(call lc,$(MCU)) -gdwarf-2
LDFLAGS =
ASFLAGS := -mmcu=$(call lc,$(MCU)) -Wa,--gdwarf-2
/* arcfour-asm.S */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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: arcfour-asm.S
* Author: Daniel Otte
* Date: 2006-07-06
* License: GPLv3 or later
* Description: Implementation of the ARCFOUR (RC4 compatible) stream cipher algorithm.
*
*/
#include <avr/io.h>
#include "avr-asm-macros.S"
/* +---+---+---------------------+
* | i | j | ......<256>........ |
* +---+---+---------------------+
*/
.global arcfour_init
/*
*== arcfour_init ==
* this function initialises the context
* param1: 16-bit pointer to the key
* given in r24:r25
* param2: 8-bit integer indicating keylength in bits
* given in r22:r23
* param3: 16-bit pointer to a ctx struct
* given in r20:r21
*/
arcfour_init:
push_ r28, r29
movw r26, r20 /* X points to ctx */
movw r30, r24 /* Z points to key */
st X+, r1
st X+, r1 /* X points to S */
movw r20, r26 /* store pointer to S in r21:r20 */
lsr r23
ror r22
lsr r23
ror r22
lsr r23
ror r22
1:
st X+, r1
inc r1
brne 1b
movw r26, r20
add r22, r30 /* r18 is keyindex counter */
clr r0
clr r19
2:
ld r23, X
ld r18, Z+
add r19, r18
add r19, r23
movw r28, r20 /* load pointer to S in Y */
add r28, r19
adc r29, r1
ld r18, Y
st Y, r23
st X+, r18
cp r30, r22
brne 3f
movw r30, r24
3:
inc r0
brne 2b
pop_ r29, r28
ret
/*
uint8_t arcfour_gen(arcfour_ctx_t *c){
uint8_t t;
c->i++;
c->j += c->s[c->i];
t = c->s[c->j];
c->s[c->j] = c->s[c->i];
c->s[c->i] = t;
return c->s[(c->s[c->j] + c->s[c->i]) & 0xff];
}
*/
.global arcfour_gen
;== arcfour_gen ==
; this function generates a keystream byte
; param1: 16-bit pointer to a ctx struct
; given in r25,r24
arcfour_gen:
movw r26, r24
ld r18, X
inc r18
st X+, r18
movw r30, r26
ld r19, X+
add r26, r18
adc r27, r1
ld r20, X
add r19, r20
st Z+, r19 /* i,j loaded&saved; X->S[i]; Z->S[0]; r20=S[i] */
add r30, r19
adc r31, r1
ld r21, Z /* X->S[i]; Z->S[j]; r20=S[i]; r21=S[j] */
st Z, r20
st X, r21
add r20, r21
adiw r24, 2
movw r26, r24 /* X and Z point to S */
add r26, r20
adc r27, r1
ld r24, X
clr r25
ret
/* arcfour.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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: arcfour.c
* Author: Daniel Otte
* email: bg@nerilex.org
* Date: 2006-06-07
* License: GPLv3 or later
* Description: Implementation of the ARCFOUR (RC4 compatible) stream cipher algorithm.
*
*/
#include <stdint.h>
#include "arcfour.h"
/*
* length is length of key in bytes!
*/
void arcfour_init(const void *key, uint16_t length_b, arcfour_ctx_t *ctx)
{
uint8_t t;
const uint8_t length_B = length_b / 8;
uint8_t nidx = length_B;
uint8_t x = 0, y = 0;
const uint8_t *kptr = (const uint8_t*) key;
do {
ctx->s[x] = x;
} while ((uint8_t) ++x);
do {
y += ctx->s[x] + *kptr++;
if (!--nidx) {
kptr = (const uint8_t*) key;
nidx = length_B;
}
y &= 0xff;
/* ctx->s[y] <--> ctx->s[x] */
t = ctx->s[y];
ctx->s[y] = ctx->s[x];
ctx->s[x] = t;
} while ((uint8_t) ++x);
ctx->i = ctx->j = 0;
}
uint8_t arcfour_gen(arcfour_ctx_t *ctx)
{
uint8_t t;
ctx->i++;
ctx->j += ctx->s[ctx->i];
/* ctx->s[i] <--> ctx->s[j] */
t = ctx->s[ctx->j];
ctx->s[ctx->j] = ctx->s[ctx->i];
ctx->s[ctx->i] = t;
return ctx->s[(ctx->s[ctx->j] + ctx->s[ctx->i]) & 0xff];
}
/* arcfour.h */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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: arcfour.h
* Author: Daniel Otte
* Date: 2006-06-07
* License: GPLv3+
* Description: Implementation of the ARCFOUR (RC4 compatible) stream cipher algorithm.
*/
/**
* \file arcfour.h
* \author Daniel Otte
* \date 2006-06-07
* \license GPLv3+
* \brief Implementation of the ARCFOUR (RC4 compatible) stream cipher algorithm.
*
* This header file defines the interface of the ARCFOUR cipher implementation.
*
* This implementation aims to be compatible with the ARCFOUR description
* available at
* http://www.mozilla.org/projects/security/pki/nss/draft-kaukonen-cipher-arcfour-03.txt
*/
#ifndef ARCFOUR_H_
#define ARCFOUR_H_
#include <stdint.h>
/** \typedef arcfour_ctx_t
* \brief type for arcfour context
*
* A variable of this type may contain a complete ARCFOUR context.
* The context is used to store the state of the cipher and gets
* created by the arcfour_init(arcfour_ctx_t *c, uint8_t *key, uint8_t length_B)
* function. The context is of the fixed size of 258 bytes
*/
/** \struct arcfour_ctx_st
* \brief base for ::arcfour_ctx_t
*
* The struct holds the two indices and the S-Box
*/
typedef struct arcfour_ctx_st {
uint8_t i, j;
uint8_t s[256];
} arcfour_ctx_t;
/** \fn void arcfour_init(const void *key, uint8_t length_B, arcfour_ctx_t *ctx)
* \brief setup a context with a key
*
* This function sets up a ::arcfour_ctx_t context using
* the supplied key of the given length.
* \param ctx pointer to the context
* \param key pointer to the key
* \param length_b length of the key in bits (between 8 and 2048)
*/
void arcfour_init(const void *key, uint16_t length_b, arcfour_ctx_t *ctx);
/** \fn uint8_t arcfour_gen(arcfour_ctx_t *ctx)
* \brief generates a byte of keystream
*
* This function generates the next byte of keystream
* from the supplied ::arcfour_ctx_t context which is updated accordingly
*
* \param ctx pointer to the context
* \return byte of keystream
*/
uint8_t arcfour_gen(arcfour_ctx_t *ctx);
#endif
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
#define PI_SIZE 32
#include "pi-cipher.c"
#define PI_SIZE 64
#include "pi-cipher.c"
This diff is collapsed.
/*
* pi16_parameter.h
*
* Created on: 07.09.2015
* Author: bg
*/
#ifndef PI16_PARAMETER_H_
#define PI16_PARAMETER_H_
typedef uint16_t word_t;
#define PI_WORD_SIZE 16
#define PI_TAG_BITS 128
#define PI_TAG_BYTES (PI_TAG_BITS / 8)
#endif /* PI16_PARAMETER_H_ */
This diff is collapsed.
/*
* pi32_parameter.h
*
* Created on: 2015-09-11
* Author: bg
*/
#ifndef PI32_PARAMETER_H_
#define PI32_PARAMETER_H_
typedef uint32_t word_t;
#define PI_WORD_SIZE 32
#define PI_TAG_BITS 256
#define PI_TAG_BYTES (PI_TAG_BITS / 8)
/*mu-transformation*/ \
#define PI_MU_CONST { \
0xF0E8E4E2, \
0xE1D8D4D2, \
0xD1CCCAC9, \
0xC6C5C3B8 \
}
/*nu-transformation*/ \
#define PI_NY_CONST { \
0xB4B2B1AC, \
0xAAA9A6A5, \
0xA39C9A99, \
0x9695938E \
}
/* FIXME */
#if 0
#define PI_MU_CONST { \
0x8D8B8778, \
0x7472716C, \
0x6A696665, \
0x635C5A59 \
}
#define PI_NY_CONST { \
0x5655534E, \
0x4D4B473C, \
0x3A393635, \
0x332E2D2B \
}
#endif
#define PI_MU_ROT_CONST { 5, 11, 17, 23 }
#define PI_NY_ROT_CONST { 3, 10, 19, 29 }
#define PI_CONST { \
{ 0x8D8B8778, 0x7472716C, 0x6A696665, 0x635C5A59 }, \
{ 0x5655534E, 0x4D4B473C, 0x3A393635, 0x332E2D2B }, \
{ 0x271E1D1B, 0x170FF0E8, 0xE4E2E1D8, 0xD4D2D1CC }, \
{ 0xCAC9C6C5, 0xC3B8B4B2, 0xB1ACAAA9, 0xA6A5A39C }, \
{ 0x9A999695, 0x938E8D8B, 0x87787472, 0x716C6A69 }, \
{ 0x6665635C, 0x5A595655, 0x534E4D4B, 0x473C3A39 } \
}
#endif /* PI32_PARAMETER_H_ */
This diff is collapsed.
/*
* pi64_parameter.h
*
* Created on: 2015-09-12
* Author: bg
*/
#ifndef PI64_PARAMETER_H_
#define PI64_PARAMETER_H_
typedef uint64_t word_t;
#define PI_WORD_SIZE 64
#define PI_TAG_BITS 512
#define PI_TAG_BYTES (PI_TAG_BITS / 8)
#define PI_MU_CONST { \
0xF0E8E4E2E1D8D4D2, \
0xD1CCCAC9C6C5C3B8, \
0xB4B2B1ACAAA9A6A5, \
0xA39C9A999695938E \
}
#define PI_MU_ROT_CONST { 7, 19, 31, 53 }
#define PI_NY_CONST { \
0x8D8B87787472716C, \
0x6A696665635C5A59, \
0x5655534E4D4B473C, \
0x3A393635332E2D2B \
}
#define PI_NY_ROT_CONST { 11, 23, 37, 59 }
#define PI_CONST { \
{ 0x271E1D1B170FF0E8, 0xE4E2E1D8D4D2D1CC, 0xCAC9C6C5C3B8B4B2, 0xB1ACAAA9A6A5A39C }, \