X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=camellia.c;h=a6a88d2419c6f499052e0291b6ba7262ca9ea21a;hb=cb23251fd9f7c2d9e519a84b6975091bceb0fa66;hp=d838c6bf8f5faacde3055e23491e5ad269846d44;hpb=38806fc75038026e167a26eff90b6111066a3642;p=avr-crypto-lib.git diff --git a/camellia.c b/camellia.c index d838c6b..a6a88d2 100644 --- a/camellia.c +++ b/camellia.c @@ -1,3 +1,21 @@ +/* camellia.c */ +/* + This file is part of the Crypto-avr-lib/microcrypt-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 . +*/ /** * * @@ -21,8 +39,8 @@ uint64_t camellia_fl(uint64_t x, uint64_t k); uint64_t camellia_fl_inv(uint64_t y, uint64_t k); /*****************************************************************************/ void change_endian(void* data, uint8_t length); - -uint64_t camellia_sigma[6]={ +/* +uint64_t PROGMEM camellia_sigma[6]={ / * 64 byte table * / 0xA09E667F3BCC908BLL, 0xB67AE8584CAA73B2LL, 0xC6EF372FE94F82BELL, @@ -30,27 +48,45 @@ uint64_t camellia_sigma[6]={ 0x10E527FADE682D1DLL, 0xB05688C2B3E6C1FDLL }; +*/ +uint32_t PROGMEM camellia_sigma[12]={ /* 64 byte table */ + 0x3BCC908BL, 0xA09E667FL, + 0x4CAA73B2L, 0xB67AE858L, + 0xE94F82BEL, 0xC6EF372FL, + 0xF1D36F1CL, 0x54FF53A5L, + 0xDE682D1DL, 0x10E527FAL, + 0xB3E6C1FDL, 0xB05688C2L +}; + +/* an ugly macro to load an entry form the table above */ +/* +#define SIGMA(p) (( ((uint64_t)(pgm_read_dword((prog_uint32_t*)camellia_sigma+2*(p)+1)))<<32) | \ + ((uint64_t)(pgm_read_dword((prog_uint32_t*)camellia_sigma+2*(p)+0))) ) +*/ +#define SIGMA(p) (( ((uint64_t)(pgm_read_dword(((prog_uint32_t*)camellia_sigma)[2*(p)+1])))<<32) | \ + ((uint64_t)(pgm_read_dword(((prog_uint32_t*)camellia_sigma)[2*(p)+0]))) ) + + /*****************************************************************************/ void camellia128_ctx_dump(camellia128_ctx_t *s){ - uart_putstr("\r\n==State Dump=="); - uart_putstr("\n\rKAl: "); uart_hexdump(&(s->kal), 8); - uart_putstr("\n\rKAr: "); uart_hexdump(&(s->kar), 8); - uart_putstr("\n\rKLl: "); uart_hexdump(&(s->kll), 8); - uart_putstr("\n\rKLr: "); uart_hexdump(&(s->klr), 8); + uart_putstr_P(PSTR("\r\n==State Dump==")); + uart_putstr_P(PSTR("\n\rKAl: ")); uart_hexdump(&(s->kal), 8); + uart_putstr_P(PSTR("\n\rKAr: ")); uart_hexdump(&(s->kar), 8); + uart_putstr_P(PSTR("\n\rKLl: ")); uart_hexdump(&(s->kll), 8); + uart_putstr_P(PSTR("\n\rKLr: ")); uart_hexdump(&(s->klr), 8); return; } /*****************************************************************************/ -//* -//extern prog_uint64_t camellia_sigma[6]; +/* extern prog_uint64_t camellia_sigma[6]; */ -void camellia128_init(camellia128_ctx_t* s, uint8_t* key){ +void camellia128_init(uint8_t* key, camellia128_ctx_t* s){ uint8_t i; - s->kll = 0; //((uint64_t*)key)[0]; + s->kll = 0; /* ((uint64_t*)key)[0]; */ -// / * load the key, endian-adjusted, to kll,klr * / + /* load the key, endian-adjusted, to kll,klr */ for(i=0; i<8; ++i){ s->kll <<= 8; s->kll |= *key++; @@ -59,22 +95,20 @@ void camellia128_init(camellia128_ctx_t* s, uint8_t* key){ s->klr <<= 8; s->klr |= *key++; } - + s->kal = s->kll; s->kar = s->klr; - s->kar ^= camellia_f(s->kal, camellia_sigma[0]); - s->kal ^= camellia_f(s->kar, camellia_sigma[1]); + s->kar ^= camellia_f(s->kal, SIGMA(0)); + s->kal ^= camellia_f(s->kar, SIGMA(1)); s->kal ^= s->kll; s->kar ^= s->klr; - s->kar ^= camellia_f(s->kal, camellia_sigma[2]); - s->kal ^= camellia_f(s->kar, camellia_sigma[3]); -// / ** / -// uart_putstr("\n\r----------------init finished--------------------"); + s->kar ^= camellia_f(s->kal, SIGMA(2)); + s->kal ^= camellia_f(s->kar, SIGMA(3)); } -//*/ + /*****************************************************************************/ void camellia128_keyop(camellia128_ctx_t* s, int8_t q); /*****************************************************************************/ @@ -100,14 +134,14 @@ void camellia_6rounds(camellia128_ctx_t* s, uint64_t* bl, uint64_t* br, uint8_t /*****************************************************************************/ -void camellia128_enc(camellia128_ctx_t* s, void* block){ +void camellia128_enc(void* block, camellia128_ctx_t* s){ #define BL (((uint64_t*)block)[0]) #define BR (((uint64_t*)block)[1]) /* endian adjustment */ /*BL*/ /* 1 2 3 4 5 6 7 8 - * 8 7 6 5 4 3 2 1 + * 8 7 6 5 4 3 2 1 */ uint64_t temp64; @@ -143,6 +177,7 @@ void camellia128_enc(camellia128_ctx_t* s, void* block){ BR = BL; BL = temp64; + camellia128_keyop(s,1); change_endian(&BL, 64/8); change_endian(&BR, 64/8); @@ -153,7 +188,7 @@ void camellia128_enc(camellia128_ctx_t* s, void* block){ /*****************************************************************************/ -void camellia128_dec(camellia128_ctx_t* s, void* block){ +void camellia128_dec(void* block, camellia128_ctx_t* s){ #define BL (((uint64_t*)block)[1]) #define BR (((uint64_t*)block)[0])