diff libtomcrypt/src/ciphers/multi2.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children
line wrap: on
line diff
--- a/libtomcrypt/src/ciphers/multi2.c	Thu Feb 08 23:11:40 2018 +0800
+++ b/libtomcrypt/src/ciphers/multi2.c	Fri Feb 09 21:44:05 2018 +0800
@@ -5,8 +5,6 @@
  *
  * The library is free for all purposes without any express
  * guarantee it works.
- *
- * Tom St Denis, [email protected], http://libtom.org
  */
 
 /**
@@ -58,7 +56,7 @@
 
    p[0] = dk[0]; p[1] = dk[1];
 
-   t = 4; 
+   t = 4;
    n = 0;
       pi1(p);
       pi2(p, k);
@@ -83,28 +81,28 @@
 {
    int n, t;
    for (t = n = 0; ; ) {
-      pi1(p); if (++n == N) break;       
+      pi1(p); if (++n == N) break;
       pi2(p, uk+t); if (++n == N) break;
       pi3(p, uk+t); if (++n == N) break;
       pi4(p, uk+t); if (++n == N) break;
       t ^= 4;
    }
-} 
+}
 
 static void decrypt(ulong32 *p, int N, ulong32 *uk)
 {
    int n, t;
-   for (t = 4*((N&1)^1), n = N; ;  ) {
-      switch (n >= 4 ? 4 : 0) {
-         case 4: pi4(p, uk+t); --n;
-         case 3: pi3(p, uk+t); --n;
-         case 2: pi2(p, uk+t); --n;
+   for (t = 4*(((N-1)>>2)&1), n = N; ;  ) {
+      switch (n<=4 ? n : ((n-1)%4)+1) {
+         case 4: pi4(p, uk+t); --n; /* FALLTHROUGH */
+         case 3: pi3(p, uk+t); --n; /* FALLTHROUGH */
+         case 2: pi2(p, uk+t); --n; /* FALLTHROUGH */
          case 1: pi1(p); --n; break;
          case 0: return;
       }
       t ^= 4;
    }
-} 
+}
 
 const struct ltc_cipher_descriptor multi2_desc = {
    "multi2",
@@ -116,7 +114,7 @@
    &multi2_test,
    &multi2_done,
    &multi2_keysize,
-   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
+   NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
 };
 
 int  multi2_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey)
@@ -129,7 +127,7 @@
 
    if (keylen != 40) return CRYPT_INVALID_KEYSIZE;
    if (num_rounds == 0) num_rounds = 128;
-   
+
    skey->multi2.N = num_rounds;
    for (x = 0; x < 8; x++) {
        LOAD32H(sk[x], key + x*4);
@@ -159,7 +157,7 @@
    LOAD32H(p[0], pt);
    LOAD32H(p[1], pt+4);
    encrypt(p, skey->multi2.N, skey->multi2.uk);
-   STORE32H(p[0], ct);   
+   STORE32H(p[0], ct);
    STORE32H(p[1], ct+4);
    return CRYPT_OK;
 }
@@ -180,7 +178,7 @@
    LOAD32H(p[0], ct);
    LOAD32H(p[1], ct+4);
    decrypt(p, skey->multi2.N, skey->multi2.uk);
-   STORE32H(p[0], pt);   
+   STORE32H(p[0], pt);
    STORE32H(p[1], pt+4);
    return CRYPT_OK;
 }
@@ -207,7 +205,7 @@
       0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00,
-   
+
       0x01, 0x23, 0x45, 0x67,
       0x89, 0xAB, 0xCD, 0xEF
    },
@@ -235,7 +233,7 @@
       0xb1, 0x27, 0xb9, 0x06,
       0xe7, 0x56, 0x22, 0x38,
    },
-   { 
+   {
       0x1f, 0xb4, 0x60, 0x60,
       0xd0, 0xb3, 0x4f, 0xa5
    },
@@ -258,26 +256,44 @@
          return err;
       }
 
-      if (XMEMCMP(buf, tests[x].ct, 8)) {
+      if (compare_testvector(buf, 8, tests[x].ct, 8, "Multi2 Encrypt", x)) {
          return CRYPT_FAIL_TESTVECTOR;
       }
-   
+
       if ((err = multi2_ecb_decrypt(buf, buf, &skey)) != CRYPT_OK) {
          return err;
       }
-      if (XMEMCMP(buf, tests[x].pt, 8)) {
+      if (compare_testvector(buf, 8, tests[x].pt, 8, "Multi2 Decrypt", x)) {
          return CRYPT_FAIL_TESTVECTOR;
       }
    }
-   
+
+   for (x = 128; x < 256; ++x) {
+        unsigned char ct[8];
+
+        if ((err = multi2_setup(tests[0].key, 40, x, &skey)) != CRYPT_OK) {
+                return err;
+        }
+        if ((err = multi2_ecb_encrypt(tests[0].pt, ct, &skey)) != CRYPT_OK) {
+                return err;
+        }
+        if ((err = multi2_ecb_decrypt(ct, buf, &skey)) != CRYPT_OK) {
+                return err;
+        }
+        if (compare_testvector(buf, 8, tests[0].pt, 8, "Multi2 Rounds", x)) {
+                return CRYPT_FAIL_TESTVECTOR;
+        }
+   }
+
    return CRYPT_OK;
 }
 
-/** Terminate the context 
+/** Terminate the context
    @param skey    The scheduled key
 */
 void multi2_done(symmetric_key *skey)
 {
+  LTC_UNUSED_PARAM(skey);
 }
 
 /**
@@ -298,6 +314,6 @@
 
 #endif
 
-/* $Source$ */
-/* $Revision$ */
-/* $Date$ */
+/* ref:         $Format:%D$ */
+/* git commit:  $Format:%H$ */
+/* commit time: $Format:%ai$ */