Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
embedded-crypto-lib
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bg nerilex
embedded-crypto-lib
Commits
99398ae8
Commit
99398ae8
authored
Jul 22, 2016
by
bg nerilex
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
adding pi-cipher
parent
7eadad3d
Changes
32
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
32 changed files
with
1871666 additions
and
37 deletions
+1871666
-37
build_system/Makefile
build_system/Makefile
+56
-24
build_system/stubs/algorithm/picipher_c.mk
build_system/stubs/algorithm/picipher_c.mk
+11
-0
build_system/stubs/algorithm/picipher_c2.mk
build_system/stubs/algorithm/picipher_c2.mk
+11
-0
build_system/stubs/board/simavr.mk
build_system/stubs/board/simavr.mk
+16
-0
src/algorithm/arcfour/arcfour-asm.S
src/algorithm/arcfour/arcfour-asm.S
+138
-0
src/algorithm/arcfour/arcfour.c
src/algorithm/arcfour/arcfour.c
+74
-0
src/algorithm/arcfour/arcfour.h
src/algorithm/arcfour/arcfour.h
+89
-0
src/algorithm/pi-cipher/pi-cipher.c
src/algorithm/pi-cipher/pi-cipher.c
+755
-0
src/algorithm/pi-cipher/pi16cipher-asm.S
src/algorithm/pi-cipher/pi16cipher-asm.S
+1570
-0
src/algorithm/pi-cipher/pi16cipher-asm2.S
src/algorithm/pi-cipher/pi16cipher-asm2.S
+1564
-0
src/algorithm/pi-cipher/pi16cipher-aux-asm.S
src/algorithm/pi-cipher/pi16cipher-aux-asm.S
+478
-0
src/algorithm/pi-cipher/pi16cipher.c
src/algorithm/pi-cipher/pi16cipher.c
+325
-0
src/algorithm/pi-cipher/pi32cipher.c
src/algorithm/pi-cipher/pi32cipher.c
+3
-0
src/algorithm/pi-cipher/pi64cipher.c
src/algorithm/pi-cipher/pi64cipher.c
+3
-0
src/include/pi-cipher.h
src/include/pi-cipher.h
+210
-0
src/include/pi16_parameter.h
src/include/pi16_parameter.h
+19
-0
src/include/pi16cipher.h
src/include/pi16cipher.h
+146
-0
src/include/pi32_parameter.h
src/include/pi32_parameter.h
+67
-0
src/include/pi32cipher.h
src/include/pi32cipher.h
+146
-0
src/include/pi64_parameter.h
src/include/pi64_parameter.h
+46
-0
src/include/pi64cipher.h
src/include/pi64cipher.h
+146
-0
src/platform_specific/avr_5/avr-asm-macros.S
src/platform_specific/avr_5/avr-asm-macros.S
+32
-3
src/platform_specific/avr_5/avr_setup.c
src/platform_specific/avr_5/avr_setup.c
+23
-0
src/platform_specific/avr_5/performance_test.c
src/platform_specific/avr_5/performance_test.c
+68
-10
src/platform_specific/avr_5/performance_test.h
src/platform_specific/avr_5/performance_test.h
+84
-0
src/test_programs/main-picipher-test.c
src/test_programs/main-picipher-test.c
+312
-0
testvectors/pi-cipher/pi16cipher096v2_4.test-vectors
testvectors/pi-cipher/pi16cipher096v2_4.test-vectors
+46879
-0
testvectors/pi-cipher/pi16cipher128v2_4.test-vectors
testvectors/pi-cipher/pi16cipher128v2_4.test-vectors
+46879
-0
testvectors/pi-cipher/pi32cipher128v2_16.test-vectors
testvectors/pi-cipher/pi32cipher128v2_16.test-vectors
+180079
-0
testvectors/pi-cipher/pi32cipher256v2_16.test-vectors
testvectors/pi-cipher/pi32cipher256v2_16.test-vectors
+180079
-0
testvectors/pi-cipher/pi64cipher128v2_16.test-vectors
testvectors/pi-cipher/pi64cipher128v2_16.test-vectors
+705679
-0
testvectors/pi-cipher/pi64cipher256v2_16.test-vectors
testvectors/pi-cipher/pi64cipher256v2_16.test-vectors
+705679
-0
No files found.
build_system/Makefile
View file @
99398ae8
...
...
@@ -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
D
EP_TEMPLATE
define
D
ep_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
...
...
build_system/stubs/algorithm/picipher_c.mk
0 → 100644
View file @
99398ae8
# 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
build_system/stubs/algorithm/picipher_c2.mk
0 → 100644
View file @
99398ae8
# 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
build_system/stubs/board/simavr.mk
0 → 100644
View file @
99398ae8
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
src/algorithm/arcfour/arcfour-asm.S
0 → 100644
View file @
99398ae8
/*
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
1
b
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
3
f
movw
r30
,
r24
3
:
inc
r0
brne
2
b
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
src/algorithm/arcfour/arcfour.c
0 → 100644
View file @
99398ae8
/* 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
];
}
src/algorithm/arcfour/arcfour.h
0 → 100644
View file @
99398ae8
/* 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
src/algorithm/pi-cipher/pi-cipher.c
0 → 100644
View file @
99398ae8
/* pi-cipher.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/>.
*/
#include <string.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#include "pi-cipher.h"
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define DEBUG 0
#if (PI_WORD_SIZE == 16)
# define load_word_little(mem) load_u16_little(mem)
# define store_word_little(mem, val) store_u16_little((mem), (val))
# define PRI_xw "04"PRIx16
static
uint16_t
load_u16_little
(
const
void
*
mem
)
{
uint16_t
ret
;
const
uint8_t
*
x
=
(
const
uint8_t
*
)
mem
;
ret
=
x
[
0
]
<<
0
|
x
[
1
]
<<
8
;
return
ret
;
}
static
void
store_u16_little
(
void
*
mem
,
uint16_t
val
)
{
uint8_t
*
x
=
(
uint8_t
*
)
mem
;
x
[
0
]
=
val
&
0xff
;
val
>>=
8
;
x
[
1
]
=
val
&
0xff
;
}
#elif (PI_WORD_SIZE == 32)
# define load_word_little(mem) load_u32_little(mem)
# define store_word_little(mem, val) store_u32_little((mem), (val))
# define PRI_xw "08"PRIx32
static
uint32_t
load_u32_little
(
const
void
*
mem
)
{
uint32_t
ret
;
const
uint8_t
*
x
=
(
const
uint8_t
*
)
mem
;
ret
=
(
uint32_t
)
x
[
0
]
<<
0
|
(
uint32_t
)
x
[
1
]
<<
8
|
(
uint32_t
)
x
[
2
]
<<
16
|
(
uint32_t
)
x
[
3
]
<<
24
;
return
ret
;
}
static
void
store_u32_little
(
void
*
mem
,
uint32_t
val
)
{
uint8_t
*
x
=
(
uint8_t
*
)
mem
;
x
[
0
]
=
val
&
0xff
;
val
>>=
8
;
x
[
1
]
=
val
&
0xff
;
val
>>=
8
;
x
[
2
]
=
val
&
0xff
;
val
>>=
8
;
x
[
3
]
=
val
&
0xff
;
}
#elif (PI_WORD_SIZE == 64)
# define load_word_little(mem) load_u64_little(mem)
# define store_word_little(mem, val) store_u64_little((mem), (val))
# define PRI_xw "016"PRIx64
static
uint64_t
load_u64_little
(
const
void
*
mem
)
{
uint64_t
ret
;
const
uint8_t
*
x
=
(
const
uint8_t
*
)
mem
;
ret
=
(
uint64_t
)
x
[
0
]
<<
0
|
(
uint64_t
)
x
[
1
]
<<
8
|
(
uint64_t
)
x
[
2
]
<<
16
|
(
uint64_t
)
x
[
3
]
<<
24
|
(
uint64_t
)
x
[
4
]
<<
32
|
(
uint64_t
)
x
[
5
]
<<
40
|
(
uint64_t
)
x
[
6
]
<<
48
|
(
uint64_t
)
x
[
7
]
<<
56
;
return
ret
;
}
static
void
store_u64_little
(
void
*
mem
,
uint64_t
val
)
{
uint8_t
*
x
=
(
uint8_t
*
)
mem
;
x
[
0
]
=
val
&
0xff
;
val
>>=
8
;
x
[
1
]
=
val
&
0xff
;
val
>>=
8
;
x
[
2
]
=
val
&
0xff
;
val
>>=
8
;
x
[
3
]
=
val
&
0xff
;
val
>>=
8
;
x
[
4
]
=
val
&
0xff
;
val
>>=
8
;
x
[
5
]
=
val
&
0xff
;
val
>>=
8
;
x
[
6
]
=
val
&
0xff
;
val
>>=
8
;
x
[
7
]
=
val
&
0xff
;
}
#endif