From: bg Date: Tue, 18 Sep 2012 15:16:12 +0000 (+0200) Subject: improving present X-Git-Url: https://git.cryptolib.org/?p=arm-crypto-lib.git;a=commitdiff_plain;h=7390f9235d6bc08b7fe34a5f43a04bd3b58f6ea6 improving present --- diff --git a/bcal/bcal_present.c b/bcal/bcal_present.c deleted file mode 100644 index c9a4fa5..0000000 --- a/bcal/bcal_present.c +++ /dev/null @@ -1,51 +0,0 @@ -/* bcal_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 . -*/ -/** - * \file bcal_present.c - * \email daniel.otte@rub.de - * \author Daniel Otte - * \date 2009-01-09 - * \license GPLv3 or later - * - */ - -#include -#include "blockcipher_descriptor.h" -#include "present.h" -#include "keysize_descriptor.h" - -const char present_str[] = "Present"; - -const uint8_t present_keysize_desc[] = { KS_TYPE_LIST, 1, KS_INT(80), - KS_TYPE_TERMINATOR }; - -const bcdesc_t present_desc = { - BCDESC_TYPE_BLOCKCIPHER, - BC_INIT_TYPE_1, - present_str, - sizeof(present_ctx_t), - 64, - {(void_fpt)present_init}, - {(void_fpt)present_enc}, - {(void_fpt)present_dec}, - (bc_free_fpt)NULL, - present_keysize_desc -}; - - diff --git a/bcal/bcal_present.h b/bcal/bcal_present.h deleted file mode 100644 index 5f60c6e..0000000 --- a/bcal/bcal_present.h +++ /dev/null @@ -1,32 +0,0 @@ -/* bcal_present.h */ -/* - This file is part of the ARM-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 . -*/ -/** - * \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 "present.h" -#include "keysize_descriptor.h" - -extern const bcdesc_t present_desc; diff --git a/bcal/bcal_present128.c b/bcal/bcal_present128.c new file mode 100644 index 0000000..bc076c8 --- /dev/null +++ b/bcal/bcal_present128.c @@ -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 . +*/ +/** + * \file bcal_present.c + * \email daniel.otte@rub.de + * \author Daniel Otte + * \date 2009-01-09 + * \license GPLv3 or later + * + */ + +#include +#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 +}; + diff --git a/bcal/bcal_present128.h b/bcal/bcal_present128.h new file mode 100644 index 0000000..cfe177a --- /dev/null +++ b/bcal/bcal_present128.h @@ -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 . +*/ +/** + * \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; diff --git a/bcal/bcal_present80.c b/bcal/bcal_present80.c new file mode 100644 index 0000000..03dace2 --- /dev/null +++ b/bcal/bcal_present80.c @@ -0,0 +1,51 @@ +/* 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 . +*/ +/** + * \file bcal_present.c + * \email daniel.otte@rub.de + * \author Daniel Otte + * \date 2009-01-09 + * \license GPLv3 or later + * + */ + +#include +#include "blockcipher_descriptor.h" +#include "present80.h" +#include "keysize_descriptor.h" + +const char present80_str[] = "Present80"; + +const uint8_t present80_keysize_desc[] = { KS_TYPE_LIST, 1, KS_INT(80), + KS_TYPE_TERMINATOR }; + +const bcdesc_t present80_desc = { + BCDESC_TYPE_BLOCKCIPHER, + BC_INIT_TYPE_2, + present80_str, + sizeof(present80_ctx_t), + 64, + {(void_fpt)present80_init}, + {(void_fpt)present80_enc}, + {(void_fpt)present80_dec}, + (bc_free_fpt)NULL, + present80_keysize_desc +}; + + diff --git a/bcal/bcal_present80.h b/bcal/bcal_present80.h new file mode 100644 index 0000000..907ec3b --- /dev/null +++ b/bcal/bcal_present80.h @@ -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 . +*/ +/** + * \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 "present80.h" +#include "keysize_descriptor.h" + +extern const bcdesc_t present80_desc; diff --git a/present/present.c b/present/present.c deleted file mode 100644 index 4525571..0000000 --- a/present/present.c +++ /dev/null @@ -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 . -*/ -/** - * present.c - * a implementation of the PRESENT block-cipher - * author: Daniel Otte - * email: daniel.otte@rub.de - * license: GPLv3 - * - * */ - -#include -#include -#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]; - } -} diff --git a/present/present.h b/present/present.h deleted file mode 100644 index 0e34eb0..0000000 --- a/present/present.h +++ /dev/null @@ -1,34 +0,0 @@ -/* present.h */ -/* - This file is part of the ARM-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 . -*/ -#ifndef PRESENT_H_ -#define PRESENT_H_ - -#include - -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_*/ diff --git a/present/present128.c b/present/present128.c new file mode 100644 index 0000000..ad40a90 --- /dev/null +++ b/present/present128.c @@ -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 . +*/ +/** + * present128.c + * a implementation of the PRESENT block-cipher + * author: Daniel Otte + * email: daniel.otte@rub.de + * license: GPLv3 + * + * */ + +#include +#include +#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); +} +*/ diff --git a/present/present128.h b/present/present128.h new file mode 100644 index 0000000..d74fff9 --- /dev/null +++ b/present/present128.h @@ -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 . +*/ +#ifndef PRESENT128_H_ +#define PRESENT128_H_ + +#include + +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_*/ diff --git a/present/present80.c b/present/present80.c new file mode 100644 index 0000000..986fd5b --- /dev/null +++ b/present/present80.c @@ -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 . +*/ +/** + * present80.c + * a implementation of the PRESENT block-cipher + * author: Daniel Otte + * email: daniel.otte@rub.de + * license: GPLv3 + * + * */ + +#include +#include +#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); +} +*/ diff --git a/present/present80.h b/present/present80.h new file mode 100644 index 0000000..9fb73e4 --- /dev/null +++ b/present/present80.h @@ -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 . +*/ +#ifndef PRESENT80_COMMON_H_ +#define PRESENT80_COMMON_H_ + +#include + +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_*/ diff --git a/present/present_common.c b/present/present_common.c new file mode 100644 index 0000000..07533dc --- /dev/null +++ b/present/present_common.c @@ -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 . +*/ +/** + * present_common.c + * a implementation of the PRESENT block-cipher + * author: Daniel Otte + * email: daniel.otte@rub.de + * license: GPLv3 + * + * */ + +#include +#include +#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); +} + diff --git a/present/present_common.h b/present/present_common.h new file mode 100644 index 0000000..f1d7ea4 --- /dev/null +++ b/present/present_common.h @@ -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 . +*/ +#ifndef PRESENT_COMMON_H_ +#define PRESENT_COMMON_H_ + +#include + +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_*/ diff --git a/present/present_speed.c b/present/present_speed.c new file mode 100644 index 0000000..275ec43 --- /dev/null +++ b/present/present_speed.c @@ -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 . +*/ +/** + * present.c + * a implementation of the PRESENT block-cipher + * author: Daniel Otte + * email: daniel.otte@rub.de + * license: GPLv3 + * + * */ + +#include +#include +#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--); +} diff --git a/present/present_speed.h b/present/present_speed.h new file mode 100644 index 0000000..320a1d8 --- /dev/null +++ b/present/present_speed.h @@ -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 . +*/ +#ifndef PRESENT_H_ +#define PRESENT_H_ + +#include + +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_*/