#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]))) )
+#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])))) )
/*****************************************************************************/
/* extern prog_uint64_t camellia_sigma[6]; */
-void camellia128_init(uint8_t* key, camellia128_ctx_t* s){
+void camellia128_init(const uint8_t* key, camellia128_ctx_t* s){
uint8_t i;
s->kll = 0; /* ((uint64_t*)key)[0]; */
/*****************************************************************************/
void camellia128_keyop(camellia128_ctx_t* s, int8_t q);
/*****************************************************************************/
-void camellia128_keyop_inv(camellia128_ctx_t* s, int8_t q);
+void camellia128_keyop_inv(camellia128_ctx_t* s, int8_t q);
/*****************************************************************************/
#define SEL_KA 1
#define KEY_ROL17 0x08
#define KEY_ROL15 0x00
-void camellia_6rounds(camellia128_ctx_t* s, uint64_t* bl, uint64_t* br,
+void camellia_6rounds(const camellia128_ctx_t* s, uint64_t* bl, uint64_t* br,
uint8_t roundop, uint8_t keychoice);
/*****************************************************************************/
-void camellia128_enc(void* block, camellia128_ctx_t* s){
+void camellia128_enc(void* block, const camellia128_ctx_t* s){
#define BL (((uint64_t*)block)[0])
#define BR (((uint64_t*)block)[1])
/* the first 6 */
camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_POSTC1 , 0x33);
/* FL injection */
- camellia128_keyop(s, -1);
+ camellia128_keyop((camellia128_ctx_t*)s, -1);
BL = camellia_fl(BL, s->kal);
BR = camellia_fl_inv(BR, s->kar);
- camellia128_keyop(s, -1);
+ camellia128_keyop((camellia128_ctx_t*)s, -1);
/* middle 6 */
camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_INC2 , 0x34);
/* FL injection */
- camellia128_keyop(s, 1);
+ camellia128_keyop((camellia128_ctx_t*)s, 1);
BL = camellia_fl(BL, s->kll);
BR = camellia_fl_inv(BR, s->klr);
- camellia128_keyop(s, 1);
+ camellia128_keyop((camellia128_ctx_t*)s, 1);
/* last 6 */
camellia_6rounds(s, &BL, &BR, KEY_ROL17 | KEY_DIR_NORM | KEY_POSTC2 , 0x0C);
/* Postwhitening */
BR = BL;
BL = temp64;
- camellia128_keyop(s,1);
+ camellia128_keyop((camellia128_ctx_t*)s,1);
change_endian(&BL, 64/8);
change_endian(&BR, 64/8);
/*****************************************************************************/
-void camellia128_dec(void* block, camellia128_ctx_t* s){
+void camellia128_dec(void* block, const camellia128_ctx_t* s){
#define BL (((uint64_t*)block)[1])
#define BR (((uint64_t*)block)[0])
/* endian adjustment */
/*BL*/
- /* 1 2 3 4 5 6 7 8
- * 8 7 6 5 4 3 2 1
+ /* 1 2 3 4 5 6 7 8
+ * 8 7 6 5 4 3 2 1
*/
uint64_t temp64;
change_endian(&BL, 64/8);
change_endian(&BR, 64/8);
- camellia128_keyop_inv(s, 1);
+ camellia128_keyop_inv((camellia128_ctx_t*)s, 1);
/* Prewhitening */
BR ^= s->kal; /* kw3 */
BL ^= s->kar; /* kw4 */
/* the first 6 */
camellia_6rounds(s, &BR, &BL, KEY_ROL17 | KEY_DIR_INV | KEY_POSTC1 , 0x0C);
/* FL injection */
- camellia128_keyop_inv(s, 1);
+ camellia128_keyop_inv((camellia128_ctx_t*)s, 1);
BR = camellia_fl(BR, s->klr);
BL = camellia_fl_inv(BL, s->kll);
- camellia128_keyop_inv(s, 1);
+ camellia128_keyop_inv((camellia128_ctx_t*)s, 1);
/* middle 6 */
camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_INC2 , 0x0B);
/* FL injection */
- camellia128_keyop_inv(s, -1);
+ camellia128_keyop_inv((camellia128_ctx_t*)s, -1);
BR = camellia_fl(BR, s->kar);
BL = camellia_fl_inv(BL, s->kal);
- camellia128_keyop_inv(s, -1);
+ camellia128_keyop_inv((camellia128_ctx_t*)s, -1);
/* last 6 */
camellia_6rounds(s, &BR, &BL, KEY_ROL15 | KEY_DIR_INV | KEY_POSTC2 , 0x33);
}camellia128_ctx_t;
-void camellia128_init(uint8_t* key, camellia128_ctx_t* s);
-void camellia128_enc(void* block, camellia128_ctx_t* s);
-void camellia128_dec(void* block, camellia128_ctx_t* s);
+void camellia128_init(const uint8_t* key, camellia128_ctx_t* s);
+void camellia128_enc(void* block, const camellia128_ctx_t* s);
+void camellia128_dec(void* block, const camellia128_ctx_t* s);
#endif /*CAMELLIA_H_*/
/*****************************************************************************/
uint64_t camellia_fl(uint64_t x, uint64_t k){
- volatile uint64_t lx[1], lk[1], y[1];
+ uint64_t lx[1], lk[1], y[1];
lx[0]=x; lk[0] = k;
#define Y ((uint32_t*)y)
#define X ((uint32_t*)lx)
#define K ((uint32_t*)lk)
- Y[0] = rol32((X[1]) & K[1],1) ^ (X[0]); /* Yr */
- Y[1] = (Y[0] | K[0]) ^ (X[1]); /* Yl */
+ Y[0] = rol32((X[1]) & K[1] ,1) ^ (X[0]); /* Yr */
+ Y[1] = (Y[0] | K[0]) ^ (X[1]); /* Yl */
/*
uart_putstr("\r\nFL(");
/*****************************************************************************/
-void camellia128_init(camellia128_ctx_t* s, uint8_t* key){
+void camellia128_init(const uint8_t* key, camellia128_ctx_t* s){
uint8_t i;
s->kll = 0; //((uint64_t*)key)[0];
#define KEY_ROL17 0x08
#define KEY_ROL15 0x00
-void camellia_6rounds(camellia128_ctx_t* s, uint64_t* bl, uint64_t* br, uint8_t roundop, uint8_t keychoice){
+void camellia_6rounds(const camellia128_ctx_t* s, uint64_t* bl, uint64_t* br, uint8_t roundop, uint8_t keychoice){
uint8_t i;
uint64_t* k[4];
k[0] = &(s->kll);
/*****************************************************************************/
-void camellia128_enc(camellia128_ctx_t* s, void* block){
+void camellia128_enc(void* block, const camellia128_ctx_t* s){
#define BL (((uint64_t*)block)[0])
#define BR (((uint64_t*)block)[1])
*/
camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_POSTC1 , 0x33);
/* FL injection */
- camellia128_keyop(s, -1);
+ camellia128_keyop((camellia128_ctx_t*)s, -1);
BL = camellia_fl(BL, s->kal);
BR = camellia_fl_inv(BR, s->kar);
- camellia128_keyop(s, -1);
+ camellia128_keyop((camellia128_ctx_t*)s, -1);
/* middle 6 */
/* BR ^= camellia_f(BL, s->kll);
BL ^= camellia_f(BR, s->klr);
/*/
camellia_6rounds(s, &BL, &BR, KEY_ROL15 | KEY_DIR_NORM | KEY_INC2 , 0x34);
/* FL injection */
- camellia128_keyop(s, 1);
+ camellia128_keyop((camellia128_ctx_t*)s, 1);
BL = camellia_fl(BL, s->kll);
BR = camellia_fl_inv(BR, s->klr);
- camellia128_keyop(s, 1);
+ camellia128_keyop((camellia128_ctx_t*)s, 1);
/* last 6 */
/* BR ^= camellia_f(BL, s->kll);
BL ^= camellia_f(BR, s->klr);
/*****************************************************************************/
-void camellia128_dec(camellia128_ctx_t* s, void* block){
+void camellia128_dec(void* block, const camellia128_ctx_t* s){
#define BL (((uint64_t*)block)[1])
#define BR (((uint64_t*)block)[0])
/* 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;
| (temp64 & (0xffLL<<40))>>(3*8) | (temp64 & (0xffLL<<16))<<(3*8) /* 3 & 6 */
| (temp64 & (0xffLL<<32))>>(1*8) | (temp64 & (0xffLL<<24))<<(1*8); /* 4 & 5 */
- camellia128_keyop_inv(s, 1);
+ camellia128_keyop_inv((camellia128_ctx_t*)s, 1);
/* Prewhitening */
BR ^= s->kal; /* kw3 */
BL ^= s->kar; /* kw4 */
camellia_6rounds(s, &BR, &BL, KEY_ROL17 | KEY_DIR_INV | KEY_POSTC1 , 0x0C);
/* FL injection */
- camellia128_keyop_inv(s, 1);
+ camellia128_keyop_inv((camellia128_ctx_t*)s, 1);
BR = camellia_fl(BR, s->klr);
BL = camellia_fl_inv(BL, s->kll);
- camellia128_keyop_inv(s, 1);
+ camellia128_keyop_inv((camellia128_ctx_t*)s, 1);
/* middle 6 */
/* BL ^= camellia_f(BR, s->kar);
BR ^= camellia_f(BL, s->kal);
camellia128_keyop_inv(s, -1);
BR = camellia_fl(BR, s->kar);
BL = camellia_fl_inv(BL, s->kal);
- camellia128_keyop_inv(s, -1);
+ camellia128_keyop_inv((camellia128_ctx_t*)s, -1);
/* last 6 */
/*
BL ^= camellia_f(BR, s->kar);
/*****************************************************************************
* self tests *
*****************************************************************************/
+/*
+128-bit key
+key 01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10
+plaintext 01 23 45 67 89 ab cd ef fe dc ba 98 76 54 32 10
+ciphertext 67 67 31 38 54 96 69 73 08 57 06 56 48 ea be 43
+*/
+void testrun_camellia(void){
+
+ uint8_t data[16] = { 0x01, 0x23, 0x45, 0x67,
+ 0x89, 0xab, 0xcd, 0xef,
+ 0xfe, 0xdc, 0xba, 0x98,
+ 0x76, 0x54, 0x32, 0x10 };
+/*
+ uint8_t data[16] = {
+ 0x10, 0x32, 0x54, 0x76,
+ 0x98, 0xba, 0xdc, 0xfe,
+ 0xfe, 0xcd, 0xab, 0x89
+ 0x67, 0x89, 0x67, 0x01};
+*/
+ camellia128_ctx_t ctx;
+ camellia128_init(data, &ctx);
+ uart_putstr_P(PSTR("\r\n key: "));
+ uart_hexdump(data, 16);
+ uart_putstr_P(PSTR("\r\n plaintext: "));
+ uart_hexdump(data, 16);
+ camellia128_enc(data, &ctx);
+ uart_putstr_P(PSTR("\r\n ciphertext: "));
+ uart_hexdump(data, 16);
+
+}
+
/*****************************************************************************
- * main *
+ * main *
*****************************************************************************/
int main (void){
uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
PGM_P u = PSTR("nessie\0test\0performance\0");
- void_fpt v[] = {testrun_nessie_camellia, testrun_nessie_camellia, test_performance_camellia};
+ void_fpt v[] = {testrun_nessie_camellia, testrun_camellia, test_performance_camellia};
while(1){
if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}