Commit 7390f923 authored by bg's avatar bg

improving present

parent 257ce629
/* bcal_present.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_present.c
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2009-01-09
* \license GPLv3 or later
*
*/
#include <stdlib.h>
#include "blockcipher_descriptor.h"
#include "present128.h"
#include "keysize_descriptor.h"
const char present128_str[] = "Present128";
const uint8_t present128_keysize_desc[] = { KS_TYPE_LIST, 1, KS_INT(128),
KS_TYPE_TERMINATOR };
const bcdesc_t present128_desc = {
BCDESC_TYPE_BLOCKCIPHER,
BC_INIT_TYPE_2,
present128_str,
sizeof(present128_ctx_t),
64,
{(void_fpt)present128_init},
{(void_fpt)present128_enc},
{(void_fpt)present128_dec},
(bc_free_fpt)NULL,
present128_keysize_desc
};
/* bcal_present.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_present.h
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2009-01-09
* \license GPLv3 or later
*
*/
#include "blockcipher_descriptor.h"
#include "present128.h"
#include "keysize_descriptor.h"
extern const bcdesc_t present128_desc;
/* bcal_present.c */
/*
This file is part of the ARM-Crypto-Lib.
Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
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
......@@ -27,25 +27,25 @@
#include <stdlib.h>
#include "blockcipher_descriptor.h"
#include "present.h"
#include "present80.h"
#include "keysize_descriptor.h"
const char present_str[] = "Present";
const char present80_str[] = "Present80";
const uint8_t present_keysize_desc[] = { KS_TYPE_LIST, 1, KS_INT(80),
KS_TYPE_TERMINATOR };
const uint8_t present80_keysize_desc[] = { KS_TYPE_LIST, 1, KS_INT(80),
KS_TYPE_TERMINATOR };
const bcdesc_t present_desc = {
const bcdesc_t present80_desc = {
BCDESC_TYPE_BLOCKCIPHER,
BC_INIT_TYPE_1,
present_str,
sizeof(present_ctx_t),
BC_INIT_TYPE_2,
present80_str,
sizeof(present80_ctx_t),
64,
{(void_fpt)present_init},
{(void_fpt)present_enc},
{(void_fpt)present_dec},
{(void_fpt)present80_init},
{(void_fpt)present80_enc},
{(void_fpt)present80_dec},
(bc_free_fpt)NULL,
present_keysize_desc
present80_keysize_desc
};
/* bcal_present.h */
/*
This file is part of the ARM-Crypto-Lib.
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
......@@ -26,7 +26,7 @@
*/
#include "blockcipher_descriptor.h"
#include "present.h"
#include "present80.h"
#include "keysize_descriptor.h"
extern const bcdesc_t present_desc;
extern const bcdesc_t present80_desc;
/* present128.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/>.
*/
/**
* present128.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 "memxor.h"
#include "present_common.h"
#include "present128.h"
static
void key_update_128(uint8_t* buffer, uint8_t round){
uint8_t j;
uint8_t t8;
union __attribute__((packed)){
uint8_t v8[2];
uint16_t v16;
} tmp;
/* rotate buffer 67 right */
for(j=0; j<8; ++j){
tmp.v8[0] = buffer[j];
buffer[j] = buffer[j + 8];
buffer[j + 8] = tmp.v8[0];
}
j=0;
t8 = (uint16_t)buffer[15] << (5);
do{
tmp.v8[1] = buffer[j];
tmp.v16 >>= 3;
buffer[j] = tmp.v8[1] | t8;
t8 = tmp.v8[0] & 0xe0;
}while(++j<16);
/* rotating done now substitution */
buffer[0] = present_sbox(buffer[0]);
/* xor with round counter */
buffer[8] ^= round << 6;
buffer[7] ^= round >> 2;
}
static
void key_update_128_inv(uint8_t* buffer, uint8_t round){
uint8_t j;
uint8_t t8;
union __attribute__((packed)){
uint8_t v8[2];
uint16_t v16;
} tmp;
/* xor with round counter */
buffer[8] ^= round << 6;
buffer[7] ^= round >> 2;
/* rotating done now substitution */
buffer[0] = present_sbox_inv(buffer[0]);
/* rotate buffer 67 left */
for(j=0; j<8; ++j){
tmp.v8[0] = buffer[j];
buffer[j] = buffer[j + 8];
buffer[j + 8] = tmp.v8[0];
}
j=15;
t8 = (uint16_t)buffer[0] >> (5);
do{
tmp.v8[0] = buffer[j];
tmp.v16 <<= 3;
buffer[j] = tmp.v8[0] | t8;
t8 = tmp.v8[1] & 0x07;
}while(j--);
}
void present128_init(const uint8_t* key, uint8_t keysize_b, present128_ctx_t* ctx){
uint8_t i;
memcpy(ctx->fwd_key, key, 16);
memcpy(ctx->rev_key, key, 16);
for(i=1; i<32; ++i){
key_update_128(ctx->rev_key, i);
}
}
void present128_enc(void* buffer, present128_ctx_t* ctx){
present_generic_enc(buffer, (uint8_t*)ctx, 16, key_update_128);
}
void present128_dec(void* buffer, present128_ctx_t* ctx){
present_generic_dec(buffer, (uint8_t*)ctx, 16, key_update_128_inv);
}
/*
void present128_enc(void* buffer, present128_ctx_t* ctx){
uint8_t i,j,tmp[8], k[16];
memcpy(k, ctx->fwd_key, 16);
memxor(buffer, k, 8);
for(i=1; i<32; ++i){
j = 7;
do{
tmp[j] = present_sbox(((uint8_t*)buffer)[j]);
}while(j--);
present_p(buffer, tmp);
key_update_128(k, i);
memxor(buffer, k, 8);
}
}
void present128_dec(void* buffer, present128_ctx_t* ctx){
uint8_t j,tmp[8], k[16];
uint8_t i;
memcpy(k, ctx->rev_key, 16);
memxor(buffer, k, 8);
i = 31;
do{
present_p(tmp, buffer);
present_p(buffer, tmp);
j = 7;
do{
((uint8_t*)buffer)[j] = present_sbox_inv(((uint8_t*)buffer)[j]);
}while(j--);
key_update_128_inv(k, i);
memxor(buffer, k, 8);
}while(--i);
}
*/
/* present128.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 PRESENT128_H_
#define PRESENT128_H_
#include <stdint.h>
typedef struct __attribute__((packed)) present128_ctx_st {
uint8_t fwd_key[16];
uint8_t rev_key[16];
} present128_ctx_t;
void present128_init(const uint8_t* key, uint8_t keysize_b, present128_ctx_t* ctx);
void present128_enc(void* buffer, present128_ctx_t* ctx);
void present128_dec(void* buffer, present128_ctx_t* ctx);
#endif /*PRESENT128_H_*/
/* present80.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/>.
*/
/**
* present80.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 "memxor.h"
#include "present_common.h"
#include "present80.h"
static
void key_update(uint8_t* buffer, uint8_t round){
uint8_t j;
union __attribute__((packed)){
uint8_t v8[2];
uint16_t v16;
} tmp;
/* rotate buffer 19 right */
tmp.v16 = ((uint16_t*)buffer)[4];
j=4;
do{
((uint16_t*)buffer)[j] = ((uint16_t*)buffer)[j-1];
}while(--j);
((uint16_t*)buffer)[0] = tmp.v16;
uint8_t t8;
j=0;
t8 = (uint16_t)buffer[9] << (5);
do{
tmp.v8[1] = buffer[j];
tmp.v16 >>= 3;
buffer[j] = tmp.v8[1] | t8;
t8 = tmp.v8[0] & 0xe0;
}while(++j<10);
/* rotating done now substitution */
buffer[0] = (present_sbox(buffer[0])&0xF0) | ((buffer[0])&0x0F);
/* xor with round counter */
buffer[8] ^= round << 7;
buffer[7] ^= round >> 1;
}
static
void key_update_inv(uint8_t* buffer, uint8_t round){
uint8_t j;
union __attribute__((packed)){
uint8_t v8[2];
uint16_t v16;
} tmp;
/* xor with round counter */
buffer[8] ^= round << 7;
buffer[7] ^= round >> 1;
/* rotating done now substitution */
buffer[0] = (present_sbox_inv(buffer[0])&0xF0) | ((buffer[0])&0x0F);
/* rotate buffer 19 left */
tmp.v16 = ((uint16_t*)buffer)[0];
j=0;
do{
((uint16_t*)buffer)[j] = ((uint16_t*)buffer)[j+1];
}while(++j<4);
((uint16_t*)buffer)[4] = tmp.v16;
uint8_t t8;
j=9;
t8 = (uint16_t)buffer[0] >> (5);
do{
tmp.v8[0] = buffer[j];
tmp.v16 <<= 3;
buffer[j] = tmp.v8[0] | t8;
t8 = tmp.v8[1] & 0x07;
}while(j--);
}
void present80_init(const uint8_t* key, uint8_t keysize_b, present80_ctx_t* ctx){
uint8_t i;
memcpy(ctx->fwd_key, key, 10);
memcpy(ctx->rev_key, key, 10);
for(i=1; i<32; ++i){
key_update(ctx->rev_key, i);
}
}
void present80_enc(void* buffer, present80_ctx_t* ctx){
present_generic_enc(buffer, (uint8_t*)ctx, 10, key_update);
}
void present80_dec(void* buffer, present80_ctx_t* ctx){
present_generic_dec(buffer, (uint8_t*)ctx, 10, key_update_inv);
}
/*
void present80_enc(void* buffer, present80_ctx_t* ctx){
uint8_t i,j,tmp[8], k[10];
memcpy(k, ctx->fwd_key, 10);
memxor(buffer, k, 8);
for(i=1; i<32; ++i){
j = 7;
do{
tmp[j] = present_sbox(((uint8_t*)buffer)[j]);
}while(j--);
present_p(buffer, tmp);
key_update(k, i);
memxor(buffer, k, 8);
}
}
void present80_dec(void* buffer, present80_ctx_t* ctx){
uint8_t j,tmp[8], k[10];
uint8_t i;
memcpy(k, ctx->rev_key, 10);
memxor(buffer, k, 8);
i = 31;
do{
present_p(tmp, buffer);
present_p(buffer, tmp);
j = 7;
do{
((uint8_t*)buffer)[j] = sbox_inv(((uint8_t*)buffer)[j]);
}while(j--);
key_update_inv(k, i);
memxor(buffer, k, 8);
}while(--i);
}
*/
/* present.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 PRESENT80_COMMON_H_
#define PRESENT80_COMMON_H_
#include <stdint.h>
typedef struct __attribute__((packed)) present80_ctx_st {
uint8_t fwd_key[10];
uint8_t rev_key[10];
} present80_ctx_t;
void present80_init(const uint8_t* key, uint8_t keysize_b, present80_ctx_t* ctx);
void present80_enc(void* buffer, present80_ctx_t* ctx);
void present80_dec(void* buffer, present80_ctx_t* ctx);
#endif /*PRESENT80_H_*/
/* present_common.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/>.
*/
/**
* present_common.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 "memxor.h"
uint8_t present_sbox(uint8_t b){
static const 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]);
}
uint8_t present_sbox_inv(uint8_t b){
static const 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]);
}
void present_p(uint8_t* o, uint8_t* i){
uint8_t m,n=0,idx=0;
for(m=0; m<64; ++m){
o[idx] <<= 1;
o[idx] |= i[n] >> 7;
i[n] <<= 1;
idx = (idx + 2) & 7;
if((m & 7) == 7){
++n;
}
if(m == 31){
idx += 1;
}
}
}
void present_generic_enc(void* buffer, uint8_t* ctx, uint8_t ksize_B,
void(*update)(uint8_t*, uint8_t)){
uint8_t i,j,tmp[8], k[ksize_B];
memcpy(k, ctx, ksize_B);
memxor(buffer, k, 8);
for(i=1; i<32; ++i){
j = 7;
do{
tmp[j] = present_sbox(((uint8_t*)buffer)[j]);
}while(j--);
present_p(buffer, tmp);
update(k, i);
memxor(buffer, k, 8);
}
}
void present_generic_dec(void* buffer, uint8_t* ctx, uint8_t ksize_B,
void(*update)(uint8_t*, uint8_t)){
uint8_t j,tmp[8], k[ksize_B];
uint8_t i;
memcpy(k, ctx + ksize_B, ksize_B);
memxor(buffer, k, 8);
i = 31;
do{
present_p(tmp, buffer);
present_p(buffer, tmp);
j = 7;
do{
((uint8_t*)buffer)[j] = present_sbox_inv(((uint8_t*)buffer)[j]);
}while(j--);
update(k, i);
memxor(buffer, k, 8);
}while(--i);
}
/* present_common.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 PRESENT_COMMON_H_
#define PRESENT_COMMON_H_
#include <stdint.h>
uint8_t present_sbox(uint8_t b);
uint8_t present_sbox_inv(uint8_t b);
void present_p(uint8_t* o, uint8_t* i);
void present_generic_enc(void* buffer, uint8_t* ctx, uint8_t ksize_B,
void(*update)(uint8_t*, uint8_t));
void present_generic_dec(void* buffer, uint8_t* ctx, uint8_t ksize_B,
void(*update)(uint8_t*, uint8_t));
#endif /*PRESENT_H_*/
/* present.c */
/*
This file is part of the ARM-Crypto-Lib.
Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
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
......@@ -27,106 +27,76 @@
#include <string.h>
#include <stdint.h>
#include "present.h"
#include "present_speed.h"
static uint8_t sbox(uint8_t b){
const 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){
const 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);
static
void key_update(uint8_t* buffer, uint8_t round){
uint8_t j;
union __attribute__((packed)){
uint8_t v8[2];
uint16_t v16;
} tmp;
/* rotate buffer 19 right */
tmp.v16 = ((uint16_t*)buffer)[4];
j=4;
do{
((uint16_t*)buffer)[j] = ((uint16_t*)buffer)[j-1];
}while(--j);
((uint16_t*)buffer)[0] = tmp.v16;
uint8_t t8;
j=0;
t8 = (uint16_t)buffer[9] << (5);
do{
tmp.v8[1] = buffer[j];
tmp.v16 >>= 3;
buffer[j] = tmp.v8[1] | t8;
t8 = tmp.v8[0] & 0xe0;
}while(++j<10);
/* rotating done now substitution */
buffer[0] = (present_sbox(buffer[0])&0xF0) | ((buffer[0])&0x0F);
/* xor with round counter */
buffer[8] ^= round << 7;
buffer[7] ^= round >> 1;
}
void present_init(const uint8_t* key, uint8_t keysize_b, present_ctx_t* ctx){
uint8_t tmp[2];
union __attribute__((packed)) {
uint8_t v8[10];
uint64_t v64;
uint16_t v16[5];
struct __attribute__((packed)) {
uint8_t v8[1];
uint16_t v16[4];