Browse Source

auto-format xts code

Steffen Jaeckel 10 years ago
parent
commit
181d2f2df7

+ 43 - 48
src/modes/xts/xts_decrypt.c

@@ -10,9 +10,9 @@
  */
 #include "tomcrypt.h"
 
-/** 
-  Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects
-*/
+/**
+ Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects
+ */
 
 #ifdef LTC_XTS_MODE
 
@@ -24,23 +24,23 @@ static int tweak_uncrypt(const unsigned char *C, unsigned char *P, unsigned char
    /* tweak encrypt block i */
 #ifdef LTC_FAST
    for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
-      *((LTC_FAST_TYPE*)&P[x]) = *((LTC_FAST_TYPE*)&C[x]) ^ *((LTC_FAST_TYPE*)&T[x]);
+      *((LTC_FAST_TYPE *)&P[x]) = *((LTC_FAST_TYPE *)&C[x]) ^ *((LTC_FAST_TYPE *)&T[x]);
    }
 #else
    for (x = 0; x < 16; x++) {
-       P[x] = C[x] ^ T[x];
+      P[x] = C[x] ^ T[x];
    }
 #endif
-     
-   err = cipher_descriptor[xts->cipher].ecb_decrypt(P, P, &xts->key1);  
+
+   err = cipher_descriptor[xts->cipher].ecb_decrypt(P, P, &xts->key1);
 
 #ifdef LTC_FAST
    for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
-      *((LTC_FAST_TYPE*)&P[x]) ^=  *((LTC_FAST_TYPE*)&T[x]);
+      *((LTC_FAST_TYPE *)&P[x]) ^= *((LTC_FAST_TYPE *)&T[x]);
    }
 #else
    for (x = 0; x < 16; x++) {
-       P[x] = P[x] ^ T[x];
+      P[x] = P[x] ^ T[x];
    }
 #endif
 
@@ -48,30 +48,28 @@ static int tweak_uncrypt(const unsigned char *C, unsigned char *P, unsigned char
    xts_mult_x(T);
 
    return err;
-}   
+}
 
 /** XTS Decryption
-  @param ct     [in] Ciphertext
-  @param ptlen  Length of plaintext (and ciphertext)
-  @param pt     [out]  Plaintext
-  @param tweak  [in] The 128--bit encryption tweak (e.g. sector number)
-  @param xts    The XTS structure
-  Returns CRYPT_OK upon success
-*/int xts_decrypt(
-   const unsigned char *ct, unsigned long ptlen,
-         unsigned char *pt,
-         unsigned char *tweak,
-         symmetric_xts *xts)
+ @param ct     [in] Ciphertext
+ @param ptlen  Length of plaintext (and ciphertext)
+ @param pt     [out]  Plaintext
+ @param tweak  [in] The 128--bit encryption tweak (e.g. sector number)
+ @param xts    The XTS structure
+ Returns CRYPT_OK upon success
+ */
+int xts_decrypt(const unsigned char *ct, unsigned long ptlen, unsigned char *pt, unsigned char *tweak,
+                symmetric_xts *xts)
 {
    unsigned char PP[16], CC[16], T[16];
    unsigned long i, m, mo, lim;
-   int           err;
+   int err;
 
    /* check inputs */
-   LTC_ARGCHK(pt    != NULL);
-   LTC_ARGCHK(ct    != NULL);
+   LTC_ARGCHK(pt != NULL);
+   LTC_ARGCHK(ct != NULL);
    LTC_ARGCHK(tweak != NULL);
-   LTC_ARGCHK(xts   != NULL);
+   LTC_ARGCHK(xts != NULL);
 
    /* check if valid */
    if ((err = cipher_is_valid(xts->cipher)) != CRYPT_OK) {
@@ -79,7 +77,7 @@ static int tweak_uncrypt(const unsigned char *C, unsigned char *P, unsigned char
    }
 
    /* get number of blocks */
-   m  = ptlen >> 4;
+   m = ptlen >> 4;
    mo = ptlen & 15;
 
    /* must have at least one full block */
@@ -95,30 +93,29 @@ static int tweak_uncrypt(const unsigned char *C, unsigned char *P, unsigned char
 
    if (cipher_descriptor[xts->cipher].accel_xts_decrypt && lim > 0) {
 
-	   /* use accelerated decryption for whole blocks */
-	   if ((err = cipher_descriptor[xts->cipher].accel_xts_decrypt(ct, pt,
-			lim, tweak, &xts->key1, &xts->key2) != CRYPT_OK)) {
-	      return err;
-	   }
-	   ct += lim * 16;
-	   pt += lim * 16;
+      /* use accelerated decryption for whole blocks */
+      if ((err = cipher_descriptor[xts->cipher].accel_xts_decrypt(ct, pt, lim, tweak, &xts->key1, &xts->key2) !=
+                 CRYPT_OK)) {
+         return err;
+      }
+      ct += lim * 16;
+      pt += lim * 16;
 
-	   /* tweak is encrypted on output */
-	   XMEMCPY(T, tweak, sizeof(T));
+      /* tweak is encrypted on output */
+      XMEMCPY(T, tweak, sizeof(T));
    } else {
       /* encrypt the tweak */
-      if ((err = cipher_descriptor[xts->cipher].ecb_encrypt(tweak, T,
-			&xts->key2)) != CRYPT_OK) {
-	 return err;
+      if ((err = cipher_descriptor[xts->cipher].ecb_encrypt(tweak, T, &xts->key2)) != CRYPT_OK) {
+         return err;
       }
 
       for (i = 0; i < lim; i++) {
-	 err = tweak_uncrypt(ct, pt, T, xts);
-	 ct += 16;
-	 pt += 16;
+         err = tweak_uncrypt(ct, pt, T, xts);
+         ct += 16;
+         pt += 16;
       }
    }
-   
+
    /* if ptlen not divide 16 then */
    if (mo > 0) {
       XMEMCPY(CC, T, 16);
@@ -131,11 +128,11 @@ static int tweak_uncrypt(const unsigned char *C, unsigned char *P, unsigned char
 
       /* Pm = first ptlen % 16 bytes of PP */
       for (i = 0; i < mo; i++) {
-          CC[i]    = ct[16+i];
-          pt[16+i] = PP[i];
+         CC[i] = ct[16 + i];
+         pt[16 + i] = PP[i];
       }
       for (; i < 16; i++) {
-          CC[i] = PP[i];
+         CC[i] = PP[i];
       }
 
       /* Pm-1 = Tweak uncrypt CC */
@@ -145,8 +142,7 @@ static int tweak_uncrypt(const unsigned char *C, unsigned char *P, unsigned char
    }
 
    /* Decrypt the tweak back */
-   if ((err = cipher_descriptor[xts->cipher].ecb_decrypt(T, tweak,
-			&xts->key2)) != CRYPT_OK) {
+   if ((err = cipher_descriptor[xts->cipher].ecb_decrypt(T, tweak, &xts->key2)) != CRYPT_OK) {
       return err;
    }
 
@@ -158,4 +154,3 @@ static int tweak_uncrypt(const unsigned char *C, unsigned char *P, unsigned char
 /* $Source$ */
 /* $Revision$ */
 /* $Date$ */
-

+ 2 - 3
src/modes/xts/xts_done.c

@@ -10,13 +10,13 @@
  */
 #include "tomcrypt.h"
 
-/** 
+/**
   Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects
 */
 
 #ifdef LTC_XTS_MODE
 
-/** Terminate XTS state 
+/** Terminate XTS state
    @param XTS    The state to terminate
 */
 void xts_done(symmetric_xts *xts)
@@ -31,4 +31,3 @@ void xts_done(symmetric_xts *xts)
 /* $Source$ */
 /* $Revision$ */
 /* $Date$ */
-

+ 35 - 41
src/modes/xts/xts_encrypt.c

@@ -10,9 +10,9 @@
  */
 #include "tomcrypt.h"
 
-/** 
-  Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects
-*/
+/**
+ Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects
+ */
 
 #ifdef LTC_XTS_MODE
 
@@ -24,25 +24,25 @@ static int tweak_crypt(const unsigned char *P, unsigned char *C, unsigned char *
    /* tweak encrypt block i */
 #ifdef LTC_FAST
    for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
-      *((LTC_FAST_TYPE*)&C[x]) = *((LTC_FAST_TYPE*)&P[x]) ^ *((LTC_FAST_TYPE*)&T[x]);
+      *((LTC_FAST_TYPE *)&C[x]) = *((LTC_FAST_TYPE *)&P[x]) ^ *((LTC_FAST_TYPE *)&T[x]);
    }
 #else
    for (x = 0; x < 16; x++) {
       C[x] = P[x] ^ T[x];
    }
 #endif
-     
+
    if ((err = cipher_descriptor[xts->cipher].ecb_encrypt(C, C, &xts->key1)) != CRYPT_OK) {
       return err;
    }
 
 #ifdef LTC_FAST
    for (x = 0; x < 16; x += sizeof(LTC_FAST_TYPE)) {
-      *((LTC_FAST_TYPE*)&C[x]) ^= *((LTC_FAST_TYPE*)&T[x]);
+      *((LTC_FAST_TYPE *)&C[x]) ^= *((LTC_FAST_TYPE *)&T[x]);
    }
 #else
    for (x = 0; x < 16; x++) {
-       C[x] = C[x] ^ T[x];
+      C[x] = C[x] ^ T[x];
    }
 #endif
 
@@ -50,31 +50,28 @@ static int tweak_crypt(const unsigned char *P, unsigned char *C, unsigned char *
    xts_mult_x(T);
 
    return CRYPT_OK;
-}   
+}
 
 /** XTS Encryption
-  @param pt     [in]  Plaintext
-  @param ptlen  Length of plaintext (and ciphertext)
-  @param ct     [out] Ciphertext
-  @param tweak  [in] The 128--bit encryption tweak (e.g. sector number)
-  @param xts    The XTS structure
-  Returns CRYPT_OK upon success
-*/
-int xts_encrypt(
-   const unsigned char *pt, unsigned long ptlen,
-         unsigned char *ct,
-         unsigned char *tweak,
-         symmetric_xts *xts)
+ @param pt     [in]  Plaintext
+ @param ptlen  Length of plaintext (and ciphertext)
+ @param ct     [out] Ciphertext
+ @param tweak  [in] The 128--bit encryption tweak (e.g. sector number)
+ @param xts    The XTS structure
+ Returns CRYPT_OK upon success
+ */
+int xts_encrypt(const unsigned char *pt, unsigned long ptlen, unsigned char *ct, unsigned char *tweak,
+                symmetric_xts *xts)
 {
    unsigned char PP[16], CC[16], T[16];
    unsigned long i, m, mo, lim;
-   int           err;
+   int err;
 
    /* check inputs */
-   LTC_ARGCHK(pt    != NULL);
-   LTC_ARGCHK(ct    != NULL);
+   LTC_ARGCHK(pt != NULL);
+   LTC_ARGCHK(ct != NULL);
    LTC_ARGCHK(tweak != NULL);
-   LTC_ARGCHK(xts   != NULL);
+   LTC_ARGCHK(xts != NULL);
 
    /* check if valid */
    if ((err = cipher_is_valid(xts->cipher)) != CRYPT_OK) {
@@ -82,7 +79,7 @@ int xts_encrypt(
    }
 
    /* get number of blocks */
-   m  = ptlen >> 4;
+   m = ptlen >> 4;
    mo = ptlen & 15;
 
    /* must have at least one full block */
@@ -99,9 +96,9 @@ int xts_encrypt(
    if (cipher_descriptor[xts->cipher].accel_xts_encrypt && lim > 0) {
 
       /* use accelerated encryption for whole blocks */
-      if ((err = cipher_descriptor[xts->cipher].accel_xts_encrypt(pt, ct, lim,
-			tweak, &xts->key1, &xts->key2) != CRYPT_OK)) {
-	 return err;
+      if ((err = cipher_descriptor[xts->cipher].accel_xts_encrypt(pt, ct, lim, tweak, &xts->key1, &xts->key2) !=
+                 CRYPT_OK)) {
+         return err;
       }
       ct += lim * 16;
       pt += lim * 16;
@@ -111,18 +108,17 @@ int xts_encrypt(
    } else {
 
       /* encrypt the tweak */
-      if ((err = cipher_descriptor[xts->cipher].ecb_encrypt(tweak, T,
-			&xts->key2)) != CRYPT_OK) {
-	 return err;
+      if ((err = cipher_descriptor[xts->cipher].ecb_encrypt(tweak, T, &xts->key2)) != CRYPT_OK) {
+         return err;
       }
 
       for (i = 0; i < lim; i++) {
-	 err = tweak_crypt(pt, ct, T, xts);
-	 ct += 16;
-	 pt += 16;
+         err = tweak_crypt(pt, ct, T, xts);
+         ct += 16;
+         pt += 16;
       }
    }
-   
+
    /* if ptlen not divide 16 then */
    if (mo > 0) {
       /* CC = tweak encrypt block m-1 */
@@ -132,12 +128,12 @@ int xts_encrypt(
 
       /* Cm = first ptlen % 16 bytes of CC */
       for (i = 0; i < mo; i++) {
-          PP[i] = pt[16+i];
-          ct[16+i] = CC[i];
+         PP[i] = pt[16 + i];
+         ct[16 + i] = CC[i];
       }
 
       for (; i < 16; i++) {
-          PP[i] = CC[i];
+         PP[i] = CC[i];
       }
 
       /* Cm-1 = Tweak encrypt PP */
@@ -147,8 +143,7 @@ int xts_encrypt(
    }
 
    /* Decrypt the tweak back */
-   if ((err = cipher_descriptor[xts->cipher].ecb_decrypt(T, tweak,
-		&xts->key2)) != CRYPT_OK) {
+   if ((err = cipher_descriptor[xts->cipher].ecb_decrypt(T, tweak, &xts->key2)) != CRYPT_OK) {
       return err;
    }
 
@@ -160,4 +155,3 @@ int xts_encrypt(
 /* $Source$ */
 /* $Revision$ */
 /* $Date$ */
-

+ 6 - 12
src/modes/xts/xts_init.c

@@ -10,13 +10,12 @@
  */
 #include "tomcrypt.h"
 
-/** 
+/**
   Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects
 */
 
 #ifdef LTC_XTS_MODE
 
-
 /** Start XTS mode
    @param cipher      The index of the cipher to use
    @param key1        The encrypt key
@@ -26,19 +25,15 @@
    @param xts         [out] XTS structure
    Returns CRYPT_OK upon success.
 */
-int xts_start(                int  cipher,
-              const unsigned char *key1, 
-              const unsigned char *key2, 
-                    unsigned long  keylen,
-                              int  num_rounds, 
-                    symmetric_xts *xts)
+int xts_start(int cipher, const unsigned char *key1, const unsigned char *key2, unsigned long keylen, int num_rounds,
+              symmetric_xts *xts)
 {
    int err;
 
    /* check inputs */
-   LTC_ARGCHK(key1  != NULL);
-   LTC_ARGCHK(key2  != NULL);
-   LTC_ARGCHK(xts   != NULL);
+   LTC_ARGCHK(key1 != NULL);
+   LTC_ARGCHK(key2 != NULL);
+   LTC_ARGCHK(xts != NULL);
 
    /* check if valid */
    if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
@@ -66,4 +61,3 @@ int xts_start(                int  cipher,
 /* $Source$ */
 /* $Revision$ */
 /* $Date$ */
-

+ 12 - 13
src/modes/xts/xts_mult_x.c

@@ -10,28 +10,28 @@
  */
 #include "tomcrypt.h"
 
-/** 
+/**
   Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects
 */
 
 #ifdef LTC_XTS_MODE
 
-/** multiply by x 
+/** multiply by x
   @param I      The value to multiply by x (LFSR shift)
 */
 void xts_mult_x(unsigned char *I)
 {
-  int x;
-  unsigned char t, tt;
+   int x;
+   unsigned char t, tt;
 
-  for (x = t = 0; x < 16; x++) {
-     tt   = I[x] >> 7;
-     I[x] = ((I[x] << 1) | t) & 0xFF;
-     t    = tt;
-  }
-  if (tt) {
-     I[0] ^= 0x87;
-  } 
+   for (x = t = 0; x < 16; x++) {
+      tt = I[x] >> 7;
+      I[x] = ((I[x] << 1) | t) & 0xFF;
+      t = tt;
+   }
+   if (tt) {
+      I[0] ^= 0x87;
+   }
 }
 
 #endif
@@ -39,4 +39,3 @@ void xts_mult_x(unsigned char *I)
 /* $Source$ */
 /* $Revision$ */
 /* $Date$ */
-

+ 47 - 47
src/modes/xts/xts_test.c

@@ -14,6 +14,7 @@
 
 /**
   Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects
+
   Returns CRYPT_OK upon success.
 */
 int xts_test(void)
@@ -21,7 +22,8 @@ int xts_test(void)
 #ifdef LTC_NO_TEST
    return CRYPT_NOP;
 #else
-   static const struct {
+   static const struct
+   {
       int keylen;
       unsigned char key1[32];
       unsigned char key2[32];
@@ -143,9 +145,9 @@ int xts_test(void)
 
 };
    unsigned char OUT[512], Torg[16], T[16];
-   ulong64       seq;
+   ulong64 seq;
    symmetric_xts xts;
-   int           i, j, err, idx;
+   int i, j, err, idx;
    unsigned long len;
 
    /* AES can be under rijndael or aes... try to find it */
@@ -154,51 +156,51 @@ int xts_test(void)
          return CRYPT_NOP;
       }
    }
-
    for (j = 0; j < 2; j++) {
-     for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
+      for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
          /* skip the cases where
           * the length is smaller than 2*blocklen
           * or the length is not a multiple of 32
           */
          if ((j == 1) && ((tests[i].PTLEN < 32) || (tests[i].PTLEN % 32))) {
-             continue;
+            continue;
          }
-         len = tests[i].PTLEN/2;
+         len = tests[i].PTLEN / 2;
 
-         err = xts_start(idx, tests[i].key1, tests[i].key2, tests[i].keylen/2, 0, &xts);
+         err = xts_start(idx, tests[i].key1, tests[i].key2, tests[i].keylen / 2, 0, &xts);
          if (err != CRYPT_OK) {
             return err;
          }
 
          seq = tests[i].seqnum;
-         STORE64L(seq,Torg);
-         XMEMSET(Torg+8, 0, 8);
+         STORE64L(seq, Torg);
+         XMEMSET(Torg + 8, 0, 8);
 
          XMEMCPY(T, Torg, sizeof(T));
          if (j == 0) {
-           err = xts_encrypt(tests[i].PTX, tests[i].PTLEN, OUT, T, &xts);
-           if (err != CRYPT_OK) {
-              xts_done(&xts);
-              return err;
-           }
-         }
-         else {
-           err = xts_encrypt(tests[i].PTX, len, OUT, T, &xts);
-           if (err != CRYPT_OK) {
-              xts_done(&xts);
-              return err;
-           }
-           err = xts_encrypt(&tests[i].PTX[len], len, &OUT[len], T, &xts);
-           if (err != CRYPT_OK) {
-              xts_done(&xts);
-              return err;
-           }
+            err = xts_encrypt(tests[i].PTX, tests[i].PTLEN, OUT, T, &xts);
+            if (err != CRYPT_OK) {
+               xts_done(&xts);
+               return err;
+            }
+         } else {
+            err = xts_encrypt(tests[i].PTX, len, OUT, T, &xts);
+            if (err != CRYPT_OK) {
+               xts_done(&xts);
+               return err;
+            }
+            err = xts_encrypt(&tests[i].PTX[len], len, &OUT[len], T, &xts);
+            if (err != CRYPT_OK) {
+               xts_done(&xts);
+               return err;
+            }
          }
 
          if (XMEMCMP(OUT, tests[i].CTX, tests[i].PTLEN)) {
 #ifdef LTC_TEST_DBG
-            printf("\nTestcase #%d with original length %lu and half of it %lu\n", i, tests[i].PTLEN, len);
+            printf("\nTestcase #%d with original length %lu and half of it "
+                   "%lu\n",
+                   i, tests[i].PTLEN, len);
             printf("\nencrypt\n");
             print_hex("should", tests[i].CTX, tests[i].PTLEN);
             print_hex("is", OUT, tests[i].PTLEN);
@@ -209,23 +211,22 @@ int xts_test(void)
 
          XMEMCPY(T, Torg, sizeof(T));
          if (j == 0) {
-           err = xts_decrypt(tests[i].CTX, tests[i].PTLEN, OUT, T, &xts);
-           if (err != CRYPT_OK) {
-              xts_done(&xts);
-              return err;
-           }
-         }
-         else {
-           err = xts_decrypt(tests[i].CTX, len, OUT, T, &xts);
-           if (err != CRYPT_OK) {
-              xts_done(&xts);
-              return err;
-           }
-           err = xts_decrypt(&tests[i].CTX[len], len, &OUT[len], T, &xts);
-           if (err != CRYPT_OK) {
-              xts_done(&xts);
-              return err;
-           }
+            err = xts_decrypt(tests[i].CTX, tests[i].PTLEN, OUT, T, &xts);
+            if (err != CRYPT_OK) {
+               xts_done(&xts);
+               return err;
+            }
+         } else {
+            err = xts_decrypt(tests[i].CTX, len, OUT, T, &xts);
+            if (err != CRYPT_OK) {
+               xts_done(&xts);
+               return err;
+            }
+            err = xts_decrypt(&tests[i].CTX[len], len, &OUT[len], T, &xts);
+            if (err != CRYPT_OK) {
+               xts_done(&xts);
+               return err;
+            }
          }
 
          if (XMEMCMP(OUT, tests[i].PTX, tests[i].PTLEN)) {
@@ -238,7 +239,7 @@ int xts_test(void)
             return CRYPT_FAIL_TESTVECTOR;
          }
          xts_done(&xts);
-     }
+      }
    }
    return CRYPT_OK;
 #endif
@@ -249,4 +250,3 @@ int xts_test(void)
 /* $Source$ */
 /* $Revision$ */
 /* $Date$ */
-