[Ffmpeg-cvslog] r7494 - trunk/libavutil/aes.c

michael subversion
Sun Jan 14 20:39:38 CET 2007


Author: michael
Date: Sun Jan 14 20:39:38 2007
New Revision: 7494

Modified:
   trunk/libavutil/aes.c

Log:
merge encrypt and decrypt so the source is simpler and the compiler can choose with inlining if it wants speed or small size


Modified: trunk/libavutil/aes.c
==============================================================================
--- trunk/libavutil/aes.c	(original)
+++ trunk/libavutil/aes.c	Sun Jan 14 20:39:38 2007
@@ -71,40 +71,30 @@
 #endif
 }
 
-void av_aes_decrypt(AVAES *a){
+static inline void crypt(AVAES *a, int s, uint8_t *sbox, uint32_t *multbl){
     int t, r;
 
     for(r=a->rounds; r>1; r--){
         addkey(a->state, a->round_key[r]);
-        SUBSHIFT3x((a->state[0]+1))
+        SUBSHIFT3x((a->state[0]+1+s))
         SUBSHIFT2x((a->state[0]+2))
-        SUBSHIFT1x((a->state[0]+3))
-        mix(a->state, dec_multbl);
+        SUBSHIFT1x((a->state[0]+3-s))
+        mix(a->state, multbl);
     }
     addkey(a->state, a->round_key[1]);
-    SUBSHIFT0((a->state[0]+0), inv_sbox)
-    SUBSHIFT3((a->state[0]+1), inv_sbox)
-    SUBSHIFT2((a->state[0]+2), inv_sbox)
-    SUBSHIFT1((a->state[0]+3), inv_sbox)
+    SUBSHIFT0((a->state[0]+0  ), sbox)
+    SUBSHIFT3((a->state[0]+1+s), sbox)
+    SUBSHIFT2((a->state[0]+2  ), sbox)
+    SUBSHIFT1((a->state[0]+3-s), sbox)
     addkey(a->state, a->round_key[0]);
 }
 
-void av_aes_encrypt(AVAES *a){
-    int r, t;
+void av_aes_decrypt(AVAES *a){
+    crypt(a, 0, inv_sbox, dec_multbl);
+}
 
-    for(r=0; r<a->rounds-1; r++){
-        addkey(a->state, a->round_key[r]);
-        SUBSHIFT1x((a->state[0]+1))
-        SUBSHIFT2x((a->state[0]+2))
-        SUBSHIFT3x((a->state[0]+3))
-        mix(a->state, enc_multbl);
-    }
-    addkey(a->state, a->round_key[r]);
-    SUBSHIFT0((a->state[0]+0), sbox)
-    SUBSHIFT1((a->state[0]+1), sbox)
-    SUBSHIFT2((a->state[0]+2), sbox)
-    SUBSHIFT3((a->state[0]+3), sbox)
-    addkey(a->state, a->round_key[r+1]);
+void av_aes_encrypt(AVAES *a){
+    crypt(a, 2, sbox, enc_multbl);
 }
 
 static init_multbl2(uint8_t tbl[1024], int c[4], uint8_t *log8, uint8_t *alog8, uint8_t *sbox){
@@ -188,6 +178,11 @@
                 a->round_key[i][0][j]= sbox[a->round_key[i][0][j]];
             mix(a->round_key[i], dec_multbl);
         }
+    }else{
+        for(i=0; i<(rounds+1)/2; i++){
+            for(j=0; j<16; j++)
+                FFSWAP(int, a->round_key[i][0][j], a->round_key[rounds-i][0][j]);
+        }
     }
 
     return a;




More information about the ffmpeg-cvslog mailing list