]> git.cryptolib.org Git - avr-crypto-lib.git/commitdiff
camellia (C and ASM mixed) seems to work now; pure C version still broken
authorbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Tue, 9 Dec 2008 21:10:19 +0000 (21:10 +0000)
committerbg <bg@b1d182e4-1ff8-0310-901f-bddb46175740>
Tue, 9 Dec 2008 21:10:19 +0000 (21:10 +0000)
camellia.c
camellia.h
camellia_C.c
test_src/main-camellia-test.c

index a561d4ee2f637e851207dc687d21477581fcb370..4c5b1c85e020b254079952dabf94e2b11120d148 100644 (file)
@@ -63,8 +63,8 @@ uint32_t PROGMEM camellia_sigma[12]={ /* 48 byte table */
 #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])))) )
 
 
 
@@ -82,7 +82,7 @@ void camellia128_ctx_dump(camellia128_ctx_t *s){
 /*****************************************************************************/
 /* 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]; */
        
@@ -112,7 +112,7 @@ void camellia128_init(uint8_t* key, camellia128_ctx_t* s){
 /*****************************************************************************/
 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
@@ -130,12 +130,12 @@ void camellia128_keyop_inv(camellia128_ctx_t* s, int8_t q);
 #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])
@@ -157,17 +157,17 @@ void camellia128_enc(void* block, camellia128_ctx_t* s){
        /* 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 */
@@ -178,7 +178,7 @@ void camellia128_enc(void* block, camellia128_ctx_t* s){
        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);
@@ -189,14 +189,14 @@ void camellia128_enc(void* block, camellia128_ctx_t* s){
 
 /*****************************************************************************/
 
-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;
@@ -204,24 +204,24 @@ void camellia128_dec(void* block, camellia128_ctx_t* s){
        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);
        
index a636efa67365559fdd1ae7a41fdd9e34ab19c240..99700895d6fc1da5ae80afa7d64c8d28d59fab04 100644 (file)
@@ -29,9 +29,9 @@ typedef struct camellia128_ctx_s{
 }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_*/
index 1eea731e056057170a24adee7d0642ec0674555d..f341d6b566832853c8b4666bb88959f40acf4554 100644 (file)
@@ -169,15 +169,15 @@ uint64_t camellia_f(uint64_t x, uint64_t k){
 /*****************************************************************************/
 
 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(");
@@ -243,7 +243,7 @@ void camellia128_ctx_dump(camellia128_ctx_t *s){
 
 /*****************************************************************************/
 
-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];
        
@@ -321,7 +321,7 @@ void camellia128_keyop_inv(camellia128_ctx_t* s, int8_t q){
 #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);
@@ -350,7 +350,7 @@ 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, const camellia128_ctx_t* s){
 
        #define BL (((uint64_t*)block)[0])
        #define BR (((uint64_t*)block)[1])
@@ -389,10 +389,10 @@ void camellia128_enc(camellia128_ctx_t* s, void* block){
 */
        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);
@@ -404,10 +404,10 @@ void camellia128_enc(camellia128_ctx_t* s, void* block){
 /*/
        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);
@@ -444,14 +444,14 @@ void camellia128_enc(camellia128_ctx_t* s, void* block){
 
 /*****************************************************************************/
 
-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;
@@ -468,7 +468,7 @@ void camellia128_dec(camellia128_ctx_t* s, void* block){
                | (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 */
@@ -486,10 +486,10 @@ void camellia128_dec(camellia128_ctx_t* s, void* block){
        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);
@@ -505,7 +505,7 @@ void camellia128_dec(camellia128_ctx_t* s, void* block){
    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);
index 4d64a506be1605e2e7caea88c5ceedcbb85fb0d3..19153b57fcfa4548c537a7a0230ec3cdb89a691c 100644 (file)
@@ -103,9 +103,40 @@ void test_performance_camellia(void){
 /*****************************************************************************
  *  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){
@@ -120,7 +151,7 @@ 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;}