doc: use ASCII in .texi files where UTF-8 isn't needed
[gnulib.git] / lib / rijndael-alg-fst.c
index 7f86c6c..edf6570 100644 (file)
@@ -1,5 +1,5 @@
 /* rijndael-alg-fst.c --- Rijndael cipher implementation.
- * Copyright (C) 2005 Free Software Foundation, Inc.
+ * Copyright (C) 2005-2006, 2009-2014 Free Software Foundation, Inc.
  *
  * This file is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published
@@ -12,9 +12,7 @@
  * General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this file; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
+ * along with this file; if not, see <http://www.gnu.org/licenses/>.
  *
  */
 
@@ -25,9 +23,7 @@
  * http://www.iaik.tu-graz.ac.at/research/krypto/AES/old/~rijmen/rijndael/rijndael-fst-3.0.zip
  */
 
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
+#include <config.h>
 
 /**
  * rijndael-alg-fst.c
@@ -739,23 +735,23 @@ static const uint32_t rcon[] = {
 };
 
 #define GETU32(pt) (((uint32_t)((pt)[0] & 0xFF) << 24) ^ \
-                   ((uint32_t)((pt)[1] & 0xFF) << 16) ^ \
-                   ((uint32_t)((pt)[2] & 0xFF) <<  8) ^ \
-                   ((uint32_t)((pt)[3] & 0xFF)))
-#define PUTU32(ct, st) {                                       \
-    (ct)[0] = (char)((st) >> 24);                              \
-    (ct)[1] = (char)((st) >> 16);                              \
-    (ct)[2] = (char)((st) >>  8);                              \
+                    ((uint32_t)((pt)[1] & 0xFF) << 16) ^ \
+                    ((uint32_t)((pt)[2] & 0xFF) <<  8) ^ \
+                    ((uint32_t)((pt)[3] & 0xFF)))
+#define PUTU32(ct, st) {                                        \
+    (ct)[0] = (char)((st) >> 24);                               \
+    (ct)[1] = (char)((st) >> 16);                               \
+    (ct)[2] = (char)((st) >>  8);                               \
     (ct)[3] = (char)(st); }
 
 /**
  * Expand the cipher key into the encryption key schedule.
  *
- * @return     the number of rounds for the given cipher key size.
+ * @return      the number of rounds for the given cipher key size.
  */
 int
 rijndaelKeySetupEnc (uint32_t rk[ /*4*(Nr + 1) */ ],
-                    const char cipherKey[], size_t keyBits)
+                     const char cipherKey[], size_t keyBits)
 {
   size_t i = 0;
   uint32_t temp;
@@ -767,78 +763,78 @@ rijndaelKeySetupEnc (uint32_t rk[ /*4*(Nr + 1) */ ],
   if (keyBits == 128)
     {
       for (;;)
-       {
-         temp = rk[3];
-         rk[4] = rk[0] ^
-           (Te4[(temp >> 16) & 0xff] & 0xff000000) ^
-           (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^
-           (Te4[(temp) & 0xff] & 0x0000ff00) ^
-           (Te4[(temp >> 24)] & 0x000000ff) ^ rcon[i];
-         rk[5] = rk[1] ^ rk[4];
-         rk[6] = rk[2] ^ rk[5];
-         rk[7] = rk[3] ^ rk[6];
-         if (++i == 10)
-           {
-             return 10;
-           }
-         rk += 4;
-       }
+        {
+          temp = rk[3];
+          rk[4] = rk[0] ^
+            (Te4[(temp >> 16) & 0xff] & 0xff000000) ^
+            (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^
+            (Te4[(temp) & 0xff] & 0x0000ff00) ^
+            (Te4[(temp >> 24)] & 0x000000ff) ^ rcon[i];
+          rk[5] = rk[1] ^ rk[4];
+          rk[6] = rk[2] ^ rk[5];
+          rk[7] = rk[3] ^ rk[6];
+          if (++i == 10)
+            {
+              return 10;
+            }
+          rk += 4;
+        }
     }
   rk[4] = GETU32 (cipherKey + 16);
   rk[5] = GETU32 (cipherKey + 20);
   if (keyBits == 192)
     {
       for (;;)
-       {
-         temp = rk[5];
-         rk[6] = rk[0] ^
-           (Te4[(temp >> 16) & 0xff] & 0xff000000) ^
-           (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^
-           (Te4[(temp) & 0xff] & 0x0000ff00) ^
-           (Te4[(temp >> 24)] & 0x000000ff) ^ rcon[i];
-         rk[7] = rk[1] ^ rk[6];
-         rk[8] = rk[2] ^ rk[7];
-         rk[9] = rk[3] ^ rk[8];
-         if (++i == 8)
-           {
-             return 12;
-           }
-         rk[10] = rk[4] ^ rk[9];
-         rk[11] = rk[5] ^ rk[10];
-         rk += 6;
-       }
+        {
+          temp = rk[5];
+          rk[6] = rk[0] ^
+            (Te4[(temp >> 16) & 0xff] & 0xff000000) ^
+            (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^
+            (Te4[(temp) & 0xff] & 0x0000ff00) ^
+            (Te4[(temp >> 24)] & 0x000000ff) ^ rcon[i];
+          rk[7] = rk[1] ^ rk[6];
+          rk[8] = rk[2] ^ rk[7];
+          rk[9] = rk[3] ^ rk[8];
+          if (++i == 8)
+            {
+              return 12;
+            }
+          rk[10] = rk[4] ^ rk[9];
+          rk[11] = rk[5] ^ rk[10];
+          rk += 6;
+        }
     }
   rk[6] = GETU32 (cipherKey + 24);
   rk[7] = GETU32 (cipherKey + 28);
   if (keyBits == 256)
     {
       for (;;)
-       {
-         temp = rk[7];
-         rk[8] = rk[0] ^
-           (Te4[(temp >> 16) & 0xff] & 0xff000000) ^
-           (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^
-           (Te4[(temp) & 0xff] & 0x0000ff00) ^
-           (Te4[(temp >> 24)] & 0x000000ff) ^ rcon[i];
-         rk[9] = rk[1] ^ rk[8];
-         rk[10] = rk[2] ^ rk[9];
-         rk[11] = rk[3] ^ rk[10];
-         if (++i == 7)
-           {
-             return 14;
-           }
-         temp = rk[11];
-         rk[12] = rk[4] ^
-           (Te4[(temp >> 24)] & 0xff000000) ^
-           (Te4[(temp >> 16) & 0xff] & 0x00ff0000) ^
-           (Te4[(temp >> 8) & 0xff] & 0x0000ff00) ^
-           (Te4[(temp) & 0xff] & 0x000000ff);
-         rk[13] = rk[5] ^ rk[12];
-         rk[14] = rk[6] ^ rk[13];
-         rk[15] = rk[7] ^ rk[14];
+        {
+          temp = rk[7];
+          rk[8] = rk[0] ^
+            (Te4[(temp >> 16) & 0xff] & 0xff000000) ^
+            (Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^
+            (Te4[(temp) & 0xff] & 0x0000ff00) ^
+            (Te4[(temp >> 24)] & 0x000000ff) ^ rcon[i];
+          rk[9] = rk[1] ^ rk[8];
+          rk[10] = rk[2] ^ rk[9];
+          rk[11] = rk[3] ^ rk[10];
+          if (++i == 7)
+            {
+              return 14;
+            }
+          temp = rk[11];
+          rk[12] = rk[4] ^
+            (Te4[(temp >> 24)] & 0xff000000) ^
+            (Te4[(temp >> 16) & 0xff] & 0x00ff0000) ^
+            (Te4[(temp >> 8) & 0xff] & 0x0000ff00) ^
+            (Te4[(temp) & 0xff] & 0x000000ff);
+          rk[13] = rk[5] ^ rk[12];
+          rk[14] = rk[6] ^ rk[13];
+          rk[15] = rk[7] ^ rk[14];
 
-         rk += 8;
-       }
+          rk += 8;
+        }
     }
   return 0;
 }
@@ -846,11 +842,11 @@ rijndaelKeySetupEnc (uint32_t rk[ /*4*(Nr + 1) */ ],
 /**
  * Expand the cipher key into the decryption key schedule.
  *
- * @return     the number of rounds for the given cipher key size.
+ * @return      the number of rounds for the given cipher key size.
  */
 int
 rijndaelKeySetupDec (uint32_t rk[ /*4*(Nr + 1) */ ],
-                    const char cipherKey[], size_t keyBits)
+                     const char cipherKey[], size_t keyBits)
 {
   size_t Nr, i, j;
   uint32_t temp;
@@ -879,32 +875,32 @@ rijndaelKeySetupDec (uint32_t rk[ /*4*(Nr + 1) */ ],
     {
       rk += 4;
       rk[0] =
-       Td0[Te4[(rk[0] >> 24)] & 0xff] ^
-       Td1[Te4[(rk[0] >> 16) & 0xff] & 0xff] ^
-       Td2[Te4[(rk[0] >> 8) & 0xff] & 0xff] ^
-       Td3[Te4[(rk[0]) & 0xff] & 0xff];
+        Td0[Te4[(rk[0] >> 24)] & 0xff] ^
+        Td1[Te4[(rk[0] >> 16) & 0xff] & 0xff] ^
+        Td2[Te4[(rk[0] >> 8) & 0xff] & 0xff] ^
+        Td3[Te4[(rk[0]) & 0xff] & 0xff];
       rk[1] =
-       Td0[Te4[(rk[1] >> 24)] & 0xff] ^
-       Td1[Te4[(rk[1] >> 16) & 0xff] & 0xff] ^
-       Td2[Te4[(rk[1] >> 8) & 0xff] & 0xff] ^
-       Td3[Te4[(rk[1]) & 0xff] & 0xff];
+        Td0[Te4[(rk[1] >> 24)] & 0xff] ^
+        Td1[Te4[(rk[1] >> 16) & 0xff] & 0xff] ^
+        Td2[Te4[(rk[1] >> 8) & 0xff] & 0xff] ^
+        Td3[Te4[(rk[1]) & 0xff] & 0xff];
       rk[2] =
-       Td0[Te4[(rk[2] >> 24)] & 0xff] ^
-       Td1[Te4[(rk[2] >> 16) & 0xff] & 0xff] ^
-       Td2[Te4[(rk[2] >> 8) & 0xff] & 0xff] ^
-       Td3[Te4[(rk[2]) & 0xff] & 0xff];
+        Td0[Te4[(rk[2] >> 24)] & 0xff] ^
+        Td1[Te4[(rk[2] >> 16) & 0xff] & 0xff] ^
+        Td2[Te4[(rk[2] >> 8) & 0xff] & 0xff] ^
+        Td3[Te4[(rk[2]) & 0xff] & 0xff];
       rk[3] =
-       Td0[Te4[(rk[3] >> 24)] & 0xff] ^
-       Td1[Te4[(rk[3] >> 16) & 0xff] & 0xff] ^
-       Td2[Te4[(rk[3] >> 8) & 0xff] & 0xff] ^
-       Td3[Te4[(rk[3]) & 0xff] & 0xff];
+        Td0[Te4[(rk[3] >> 24)] & 0xff] ^
+        Td1[Te4[(rk[3] >> 16) & 0xff] & 0xff] ^
+        Td2[Te4[(rk[3] >> 8) & 0xff] & 0xff] ^
+        Td3[Te4[(rk[3]) & 0xff] & 0xff];
     }
   return Nr;
 }
 
 void
 rijndaelEncrypt (const uint32_t rk[ /*4*(Nr + 1) */ ], size_t Nr,
-                const char pt[16], char ct[16])
+                 const char pt[16], char ct[16])
 {
   uint32_t s0, s1, s2, s3, t0, t1, t2, t3;
   size_t r;
@@ -924,44 +920,44 @@ rijndaelEncrypt (const uint32_t rk[ /*4*(Nr + 1) */ ], size_t Nr,
   for (;;)
     {
       t0 =
-       Te0[(s0 >> 24)] ^
-       Te1[(s1 >> 16) & 0xff] ^
-       Te2[(s2 >> 8) & 0xff] ^ Te3[(s3) & 0xff] ^ rk[4];
+        Te0[(s0 >> 24)] ^
+        Te1[(s1 >> 16) & 0xff] ^
+        Te2[(s2 >> 8) & 0xff] ^ Te3[(s3) & 0xff] ^ rk[4];
       t1 =
-       Te0[(s1 >> 24)] ^
-       Te1[(s2 >> 16) & 0xff] ^
-       Te2[(s3 >> 8) & 0xff] ^ Te3[(s0) & 0xff] ^ rk[5];
+        Te0[(s1 >> 24)] ^
+        Te1[(s2 >> 16) & 0xff] ^
+        Te2[(s3 >> 8) & 0xff] ^ Te3[(s0) & 0xff] ^ rk[5];
       t2 =
-       Te0[(s2 >> 24)] ^
-       Te1[(s3 >> 16) & 0xff] ^
-       Te2[(s0 >> 8) & 0xff] ^ Te3[(s1) & 0xff] ^ rk[6];
+        Te0[(s2 >> 24)] ^
+        Te1[(s3 >> 16) & 0xff] ^
+        Te2[(s0 >> 8) & 0xff] ^ Te3[(s1) & 0xff] ^ rk[6];
       t3 =
-       Te0[(s3 >> 24)] ^
-       Te1[(s0 >> 16) & 0xff] ^
-       Te2[(s1 >> 8) & 0xff] ^ Te3[(s2) & 0xff] ^ rk[7];
+        Te0[(s3 >> 24)] ^
+        Te1[(s0 >> 16) & 0xff] ^
+        Te2[(s1 >> 8) & 0xff] ^ Te3[(s2) & 0xff] ^ rk[7];
 
       rk += 8;
       if (--r == 0)
-       {
-         break;
-       }
+        {
+          break;
+        }
 
       s0 =
-       Te0[(t0 >> 24)] ^
-       Te1[(t1 >> 16) & 0xff] ^
-       Te2[(t2 >> 8) & 0xff] ^ Te3[(t3) & 0xff] ^ rk[0];
+        Te0[(t0 >> 24)] ^
+        Te1[(t1 >> 16) & 0xff] ^
+        Te2[(t2 >> 8) & 0xff] ^ Te3[(t3) & 0xff] ^ rk[0];
       s1 =
-       Te0[(t1 >> 24)] ^
-       Te1[(t2 >> 16) & 0xff] ^
-       Te2[(t3 >> 8) & 0xff] ^ Te3[(t0) & 0xff] ^ rk[1];
+        Te0[(t1 >> 24)] ^
+        Te1[(t2 >> 16) & 0xff] ^
+        Te2[(t3 >> 8) & 0xff] ^ Te3[(t0) & 0xff] ^ rk[1];
       s2 =
-       Te0[(t2 >> 24)] ^
-       Te1[(t3 >> 16) & 0xff] ^
-       Te2[(t0 >> 8) & 0xff] ^ Te3[(t1) & 0xff] ^ rk[2];
+        Te0[(t2 >> 24)] ^
+        Te1[(t3 >> 16) & 0xff] ^
+        Te2[(t0 >> 8) & 0xff] ^ Te3[(t1) & 0xff] ^ rk[2];
       s3 =
-       Te0[(t3 >> 24)] ^
-       Te1[(t0 >> 16) & 0xff] ^
-       Te2[(t1 >> 8) & 0xff] ^ Te3[(t2) & 0xff] ^ rk[3];
+        Te0[(t3 >> 24)] ^
+        Te1[(t0 >> 16) & 0xff] ^
+        Te2[(t1 >> 8) & 0xff] ^ Te3[(t2) & 0xff] ^ rk[3];
     }
   /*
    * apply last round and
@@ -995,7 +991,7 @@ rijndaelEncrypt (const uint32_t rk[ /*4*(Nr + 1) */ ], size_t Nr,
 
 void
 rijndaelDecrypt (const uint32_t rk[ /*4*(Nr + 1) */ ], size_t Nr,
-                const char ct[16], char pt[16])
+                 const char ct[16], char pt[16])
 {
   uint32_t s0, s1, s2, s3, t0, t1, t2, t3;
   size_t r;
@@ -1015,44 +1011,44 @@ rijndaelDecrypt (const uint32_t rk[ /*4*(Nr + 1) */ ], size_t Nr,
   for (;;)
     {
       t0 =
-       Td0[(s0 >> 24)] ^
-       Td1[(s3 >> 16) & 0xff] ^
-       Td2[(s2 >> 8) & 0xff] ^ Td3[(s1) & 0xff] ^ rk[4];
+        Td0[(s0 >> 24)] ^
+        Td1[(s3 >> 16) & 0xff] ^
+        Td2[(s2 >> 8) & 0xff] ^ Td3[(s1) & 0xff] ^ rk[4];
       t1 =
-       Td0[(s1 >> 24)] ^
-       Td1[(s0 >> 16) & 0xff] ^
-       Td2[(s3 >> 8) & 0xff] ^ Td3[(s2) & 0xff] ^ rk[5];
+        Td0[(s1 >> 24)] ^
+        Td1[(s0 >> 16) & 0xff] ^
+        Td2[(s3 >> 8) & 0xff] ^ Td3[(s2) & 0xff] ^ rk[5];
       t2 =
-       Td0[(s2 >> 24)] ^
-       Td1[(s1 >> 16) & 0xff] ^
-       Td2[(s0 >> 8) & 0xff] ^ Td3[(s3) & 0xff] ^ rk[6];
+        Td0[(s2 >> 24)] ^
+        Td1[(s1 >> 16) & 0xff] ^
+        Td2[(s0 >> 8) & 0xff] ^ Td3[(s3) & 0xff] ^ rk[6];
       t3 =
-       Td0[(s3 >> 24)] ^
-       Td1[(s2 >> 16) & 0xff] ^
-       Td2[(s1 >> 8) & 0xff] ^ Td3[(s0) & 0xff] ^ rk[7];
+        Td0[(s3 >> 24)] ^
+        Td1[(s2 >> 16) & 0xff] ^
+        Td2[(s1 >> 8) & 0xff] ^ Td3[(s0) & 0xff] ^ rk[7];
 
       rk += 8;
       if (--r == 0)
-       {
-         break;
-       }
+        {
+          break;
+        }
 
       s0 =
-       Td0[(t0 >> 24)] ^
-       Td1[(t3 >> 16) & 0xff] ^
-       Td2[(t2 >> 8) & 0xff] ^ Td3[(t1) & 0xff] ^ rk[0];
+        Td0[(t0 >> 24)] ^
+        Td1[(t3 >> 16) & 0xff] ^
+        Td2[(t2 >> 8) & 0xff] ^ Td3[(t1) & 0xff] ^ rk[0];
       s1 =
-       Td0[(t1 >> 24)] ^
-       Td1[(t0 >> 16) & 0xff] ^
-       Td2[(t3 >> 8) & 0xff] ^ Td3[(t2) & 0xff] ^ rk[1];
+        Td0[(t1 >> 24)] ^
+        Td1[(t0 >> 16) & 0xff] ^
+        Td2[(t3 >> 8) & 0xff] ^ Td3[(t2) & 0xff] ^ rk[1];
       s2 =
-       Td0[(t2 >> 24)] ^
-       Td1[(t1 >> 16) & 0xff] ^
-       Td2[(t0 >> 8) & 0xff] ^ Td3[(t3) & 0xff] ^ rk[2];
+        Td0[(t2 >> 24)] ^
+        Td1[(t1 >> 16) & 0xff] ^
+        Td2[(t0 >> 8) & 0xff] ^ Td3[(t3) & 0xff] ^ rk[2];
       s3 =
-       Td0[(t3 >> 24)] ^
-       Td1[(t2 >> 16) & 0xff] ^
-       Td2[(t1 >> 8) & 0xff] ^ Td3[(t0) & 0xff] ^ rk[3];
+        Td0[(t3 >> 24)] ^
+        Td1[(t2 >> 16) & 0xff] ^
+        Td2[(t1 >> 8) & 0xff] ^ Td3[(t0) & 0xff] ^ rk[3];
     }
   /*
    * apply last round and