Browse Source

also test XTS accelerators

Steffen Jaeckel 10 years ago
parent
commit
f9c8c9c229
1 changed files with 129 additions and 68 deletions
  1. 129 68
      src/modes/xts/xts_test.c

+ 129 - 68
src/modes/xts/xts_test.c

@@ -12,6 +12,54 @@
 
 #ifdef LTC_XTS_MODE
 
+static int _xts_test_accel_xts_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long blocks,
+                                       unsigned char *tweak, symmetric_key *skey1, symmetric_key *skey2)
+{
+   int ret;
+   symmetric_xts xts;
+
+   /* AES can be under rijndael or aes... try to find it */
+   if ((xts.cipher = find_cipher("aes")) == -1) {
+      if ((xts.cipher = find_cipher("rijndael")) == -1) {
+         return CRYPT_NOP;
+      }
+   }
+   void *orig = cipher_descriptor[xts.cipher].accel_xts_encrypt;
+   cipher_descriptor[xts.cipher].accel_xts_encrypt = NULL;
+
+   XMEMCPY(&xts.key1, skey1, sizeof(symmetric_key));
+   XMEMCPY(&xts.key2, skey2, sizeof(symmetric_key));
+
+   ret = xts_encrypt(pt, blocks << 4, ct, tweak, &xts);
+   cipher_descriptor[xts.cipher].accel_xts_encrypt = orig;
+
+   return ret;
+}
+
+static int _xts_test_accel_xts_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long blocks,
+                                       unsigned char *tweak, symmetric_key *skey1, symmetric_key *skey2)
+{
+   int ret;
+   symmetric_xts xts;
+
+   /* AES can be under rijndael or aes... try to find it */
+   if ((xts.cipher = find_cipher("aes")) == -1) {
+      if ((xts.cipher = find_cipher("rijndael")) == -1) {
+         return CRYPT_NOP;
+      }
+   }
+   void *orig = cipher_descriptor[xts.cipher].accel_xts_decrypt;
+   cipher_descriptor[xts.cipher].accel_xts_decrypt = NULL;
+
+   XMEMCPY(&xts.key1, skey1, sizeof(symmetric_key));
+   XMEMCPY(&xts.key2, skey2, sizeof(symmetric_key));
+
+   ret = xts_decrypt(ct, blocks << 4, pt, tweak, &xts);
+   cipher_descriptor[xts.cipher].accel_xts_decrypt = orig;
+
+   return ret;
+}
+
 /**
   Source donated by Elliptic Semiconductor Inc (www.ellipticsemi.com) to the LibTom Projects
 
@@ -147,7 +195,7 @@ int xts_test(void)
    unsigned char OUT[512], Torg[16], T[16];
    ulong64 seq;
    symmetric_xts xts;
-   int i, j, err, idx;
+   int i, j, k, err, idx;
    unsigned long len;
 
    /* AES can be under rijndael or aes... try to find it */
@@ -156,89 +204,102 @@ int xts_test(void)
          return CRYPT_NOP;
       }
    }
-   for (j = 0; j < 2; j++) {
-      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;
-         }
-         len = tests[i].PTLEN / 2;
-
-         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);
-
-         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;
+   for (k = 0; k < 4; ++k) {
+      cipher_descriptor[idx].accel_xts_encrypt = NULL;
+      cipher_descriptor[idx].accel_xts_decrypt = NULL;
+      if (k & 0x1) {
+         cipher_descriptor[idx].accel_xts_encrypt = _xts_test_accel_xts_encrypt;
+      }
+      if (k & 0x2) {
+         cipher_descriptor[idx].accel_xts_decrypt = _xts_test_accel_xts_decrypt;
+      }
+      for (j = 0; j < 2; j++) {
+         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;
             }
-         } else {
-            err = xts_encrypt(tests[i].PTX, len, OUT, T, &xts);
-            if (err != CRYPT_OK) {
-               xts_done(&xts);
-               return err;
+            if ((k > 0) && (j == 1)) {
+               continue;
             }
-            err = xts_encrypt(&tests[i].PTX[len], len, &OUT[len], T, &xts);
+            len = tests[i].PTLEN / 2;
+
+            err = xts_start(idx, tests[i].key1, tests[i].key2, tests[i].keylen / 2, 0, &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("\nencrypt\n");
-            print_hex("should", tests[i].CTX, tests[i].PTLEN);
-            print_hex("is", OUT, tests[i].PTLEN);
-#endif
-            xts_done(&xts);
-            return CRYPT_FAIL_TESTVECTOR;
-         }
+            seq = tests[i].seqnum;
+            STORE64L(seq, Torg);
+            XMEMSET(Torg + 8, 0, 8);
 
-         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;
+            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;
+               }
             }
-         } else {
-            err = xts_decrypt(tests[i].CTX, len, OUT, T, &xts);
-            if (err != CRYPT_OK) {
+
+            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("\nencrypt\n");
+               print_hex("should", tests[i].CTX, tests[i].PTLEN);
+               print_hex("is", OUT, tests[i].PTLEN);
+#endif
                xts_done(&xts);
-               return err;
+               return CRYPT_FAIL_TESTVECTOR;
             }
-            err = xts_decrypt(&tests[i].CTX[len], len, &OUT[len], T, &xts);
-            if (err != CRYPT_OK) {
-               xts_done(&xts);
-               return err;
+
+            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;
+               }
             }
-         }
 
-         if (XMEMCMP(OUT, tests[i].PTX, tests[i].PTLEN)) {
+            if (XMEMCMP(OUT, tests[i].PTX, tests[i].PTLEN)) {
 #ifdef LTC_TEST_DBG
-            printf("\ndecrypt\n");
-            print_hex("should", tests[i].PTX, tests[i].PTLEN);
-            print_hex("is", OUT, tests[i].PTLEN);
+               printf("\ndecrypt\n");
+               print_hex("should", tests[i].PTX, tests[i].PTLEN);
+               print_hex("is", OUT, tests[i].PTLEN);
 #endif
+               xts_done(&xts);
+               return CRYPT_FAIL_TESTVECTOR;
+            }
             xts_done(&xts);
-            return CRYPT_FAIL_TESTVECTOR;
          }
-         xts_done(&xts);
       }
    }
    return CRYPT_OK;