summaryrefslogtreecommitdiff
path: root/libtomcrypt/src/ciphers/multi2.c
diff options
context:
space:
mode:
Diffstat (limited to 'libtomcrypt/src/ciphers/multi2.c')
-rw-r--r--libtomcrypt/src/ciphers/multi2.c66
1 files changed, 41 insertions, 25 deletions
diff --git a/libtomcrypt/src/ciphers/multi2.c b/libtomcrypt/src/ciphers/multi2.c
index db0b3ba..86c1812 100644
--- a/libtomcrypt/src/ciphers/multi2.c
+++ b/libtomcrypt/src/ciphers/multi2.c
@@ -5,8 +5,6 @@
*
* The library is free for all purposes without any express
* guarantee it works.
- *
- * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
*/
/**
@@ -58,7 +56,7 @@ static void setup(ulong32 *dk, ulong32 *k, ulong32 *uk)
p[0] = dk[0]; p[1] = dk[1];
- t = 4;
+ t = 4;
n = 0;
pi1(p);
pi2(p, k);
@@ -83,28 +81,28 @@ static void encrypt(ulong32 *p, int N, ulong32 *uk)
{
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 @@ const struct ltc_cipher_descriptor multi2_desc = {
&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 @@ int multi2_setup(const unsigned char *key, int keylen, int num_rounds, symmetri
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 @@ int multi2_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key
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 @@ int multi2_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key
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 @@ int multi2_test(void)
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
-
+
0x01, 0x23, 0x45, 0x67,
0x89, 0xAB, 0xCD, 0xEF
},
@@ -235,7 +233,7 @@ int multi2_test(void)
0xb1, 0x27, 0xb9, 0x06,
0xe7, 0x56, 0x22, 0x38,
},
- {
+ {
0x1f, 0xb4, 0x60, 0x60,
0xd0, 0xb3, 0x4f, 0xa5
},
@@ -258,26 +256,44 @@ int multi2_test(void)
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 @@ int multi2_keysize(int *keysize)
#endif
-/* $Source$ */
-/* $Revision$ */
-/* $Date$ */
+/* ref: $Format:%D$ */
+/* git commit: $Format:%H$ */
+/* commit time: $Format:%ai$ */