]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - camellia.c
camellia (C and ASM mixed) seems to work now; pure C version still broken
[avr-crypto-lib.git] / camellia.c
index e0e21597d4433c6bb4e22682f7bf60f2c5e5c702..4c5b1c85e020b254079952dabf94e2b11120d148 100644 (file)
@@ -49,7 +49,7 @@ uint64_t PROGMEM camellia_sigma[6]={ / * 64 byte table * /
        0xB05688C2B3E6C1FDLL
 };     
 */
-uint32_t PROGMEM camellia_sigma[12]={ /* 64 byte table */
+uint32_t PROGMEM camellia_sigma[12]={ /* 48 byte table */
          0x3BCC908BL, 0xA09E667FL,
          0x4CAA73B2L, 0xB67AE858L,
          0xE94F82BEL, 0xC6EF372FL,
@@ -63,8 +63,8 @@ uint32_t PROGMEM camellia_sigma[12]={ /* 64 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(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]; */
        
@@ -112,7 +112,7 @@ void camellia128_init(camellia128_ctx_t* s, uint8_t* key){
 /*****************************************************************************/
 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,18 +130,19 @@ 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);
 /*****************************************************************************/
 
 
-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])
        /* 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;
@@ -156,17 +157,17 @@ void camellia128_enc(camellia128_ctx_t* s, void* block){
        /* 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 */
@@ -177,7 +178,7 @@ void camellia128_enc(camellia128_ctx_t* s, void* block){
        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);
@@ -188,14 +189,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
+        /* 1 2 3 4 5 6 7 8
+         * 8 7 6 5 4 3 2 1
          */
         
        uint64_t temp64;
@@ -203,24 +204,24 @@ void camellia128_dec(camellia128_ctx_t* s, void* block){
        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);