improving present

This commit is contained in:
bg 2012-09-18 17:16:12 +02:00
parent 257ce629cc
commit 7390f9235d
13 changed files with 696 additions and 150 deletions

50
bcal/bcal_present128.c Normal file
View File

@ -0,0 +1,50 @@
/* 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
};

32
bcal/bcal_present128.h Normal file
View File

@ -0,0 +1,32 @@
/* 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;

View File

@ -1,7 +1,7 @@
/* 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
};

View File

@ -1,6 +1,6 @@
/* 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;

View File

@ -1,132 +0,0 @@
/* present.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/>.
*/
/**
* 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){
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);
}
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];
} off1;
} b;
uint8_t i;
memcpy(b.v8, key, 10);
memcpy(&(ctx->k[0]), b.v8+2, 8);
for(i=1; i<32; ++i){
/* rotate buffer 19 right */
memcpy(tmp, b.v8, 2);
memmove(b.v8, b.v8+2, 8);
memcpy(b.v8+8, tmp, 2);
/* three shifts to do*/
tmp[1]=b.v8[0];
b.v64 >>= 3;
b.v16[4] >>= 3;
b.v8[9] |= tmp[1]<<5;
b.v8[7] |= tmp[0]<<5;
/* rotating done now substitution */
b.v8[9] = (sbox(b.v8[9])&0xF0) | ((b.v8[9])&0x0F);
/* xor with round counter */
b.off1.v16[0] ^= (uint16_t)i<<7;
memcpy(&(ctx->k[i]), b.v8+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];
}
}

145
present/present128.c Normal file
View File

@ -0,0 +1,145 @@
/* 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);
}
*/

35
present/present128.h Normal file
View File

@ -0,0 +1,35 @@
/* 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_*/

144
present/present80.c Normal file
View File

@ -0,0 +1,144 @@
/* 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);
}
*/

34
present/present80.h Normal file
View File

@ -0,0 +1,34 @@
/* 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_*/

103
present/present_common.c Normal file
View File

@ -0,0 +1,103 @@
/* 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);
}

33
present/present_common.h Normal file
View File

@ -0,0 +1,33 @@
/* 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_*/

102
present/present_speed.c Normal file
View File

@ -0,0 +1,102 @@
/* 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/>.
*/
/**
* 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_speed.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;
}
void present_init(const uint8_t* key, uint8_t keysize_b, present_ctx_t* ctx){
uint8_t i,key_buffer[10];
memcpy(key_buffer, key, 10);
memcpy(&(ctx->k[0]), key_buffer, 8);
for(i=1; i<32; ++i){
key_update(key_buffer, i);
memcpy(&(ctx->k[i]), key_buffer, 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];
memxor(buffer, &ctx->k[i], 8);
j = 7;
do{
tmp[j] = present_sbox(((uint8_t*)buffer)[j]);
}while(j--);
present_p(buffer, tmp);
}
memxor(buffer, &ctx->k[31], 8);
}
void present_dec(void* buffer, present_ctx_t* ctx){
uint8_t j,tmp[8];
uint8_t i;
memxor(buffer, &ctx->k[31], 8);
i = 30;
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--);
memxor(buffer, &ctx->k[i], 8);
}while(i--);
}

View File

@ -1,6 +1,6 @@
/* 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