Mercurial > projects > dcrypt
diff dcrypt/crypto/ciphers/TEA.d @ 27:8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Sat, 09 May 2009 23:29:20 -0400 |
parents | 4589f8c5eb3c |
children | ad687db713a4 |
line wrap: on
line diff
--- a/dcrypt/crypto/ciphers/TEA.d Sun Mar 01 13:06:48 2009 -0500 +++ b/dcrypt/crypto/ciphers/TEA.d Sat May 09 23:29:20 2009 -0400 @@ -13,31 +13,37 @@ /** Implementation of the TEA cipher designed by David Wheeler and Roger Needham. */ -class TEA : BlockCipher { - private { +class TEA : BlockCipher +{ + private + { const uint ROUNDS = 32, KEY_SIZE = 16, BLOCK_SIZE = 8, - DELTA = 0x9e3779b9, - DECRYPT_SUM = 0xc6ef3720; + DELTA = 0x9e3779b9u, + DECRYPT_SUM = 0xc6ef3720u; uint sk0, sk1, sk2, sk3, sum; } void reset(){} - char[] name() { + char[] name() + { return "TEA"; } - uint blockSize() { + uint blockSize() + { return BLOCK_SIZE; } - void init(bool encrypt, CipherParameters params) { + void init(bool encrypt, CipherParameters params) + { SymmetricKey keyParams = cast(SymmetricKey)params; if (!keyParams) throw new InvalidParameterError( name()~": Invalid parameter passed to init"); + _encrypt = encrypt; if (keyParams.key.length != KEY_SIZE) @@ -52,7 +58,8 @@ _initialized = true; } - uint update(void[] input_, void[] output_) { + uint update(void[] input_, void[] output_) + { if (!_initialized) throw new NotInitializedError(name()~": Cipher not initialized"); @@ -69,12 +76,16 @@ v1 = ByteConverter.BigEndian.to!(uint)(input[4..8]); sum = _encrypt ? 0 : DECRYPT_SUM; - for (int i = 0; i < ROUNDS; i++) { - if (_encrypt) { + for (int i = 0; i < ROUNDS; i++) + { + if (_encrypt) + { sum += DELTA; v0 += ((v1 << 4) + sk0) ^ (v1 + sum) ^ ((v1 >> 5) + sk1); v1 += ((v0 << 4) + sk2) ^ (v0 + sum) ^ ((v0 >> 5) + sk3); - } else { + } + else + { v1 -= ((v0 << 4) + sk2) ^ (v0 + sum) ^ ((v0 >> 5) + sk3); v0 -= ((v1 << 4) + sk0) ^ (v1 + sum) ^ ((v1 >> 5) + sk1); sum -= DELTA; @@ -88,48 +99,53 @@ } /** Some TEA test vectors. */ - unittest { - static const char[][] test_keys = [ - "00000000000000000000000000000000", - "00000000000000000000000000000000", - "0123456712345678234567893456789a", - "0123456712345678234567893456789a" - ]; - - static const char[][] test_plaintexts = [ - "0000000000000000", - "0102030405060708", - "0000000000000000", - "0102030405060708" - ]; + debug (UnitTest) + { + unittest + { + static const char[][] test_keys = [ + "00000000000000000000000000000000", + "00000000000000000000000000000000", + "0123456712345678234567893456789a", + "0123456712345678234567893456789a" + ]; + + static const char[][] test_plaintexts = [ + "0000000000000000", + "0102030405060708", + "0000000000000000", + "0102030405060708" + ]; + + static const char[][] test_ciphertexts = [ + "41ea3a0a94baa940", + "6a2f9cf3fccf3c55", + "34e943b0900f5dcb", + "773dc179878a81c0" + ]; + - static const char[][] test_ciphertexts = [ - "41ea3a0a94baa940", - "6a2f9cf3fccf3c55", - "34e943b0900f5dcb", - "773dc179878a81c0" - ]; - - - TEA t = new TEA(); - foreach (uint i, char[] test_key; test_keys) { - ubyte[] buffer = new ubyte[t.blockSize]; - char[] result; - SymmetricKey key = new SymmetricKey(ByteConverter.hexDecode(test_key)); - - // Encryption - t.init(true, key); - t.update(ByteConverter.hexDecode(test_plaintexts[i]), buffer); - result = ByteConverter.hexEncode(buffer); - assert(result == test_ciphertexts[i], - t.name~": ("~result~") != ("~test_ciphertexts[i]~")"); + TEA t = new TEA(); + foreach (uint i, char[] test_key; test_keys) + { + ubyte[] buffer = new ubyte[t.blockSize]; + char[] result; + SymmetricKey key = new SymmetricKey(ByteConverter.hexDecode(test_key)); + + // Encryption + t.init(true, key); + t.update(ByteConverter.hexDecode(test_plaintexts[i]), buffer); + result = ByteConverter.hexEncode(buffer); + assert(result == test_ciphertexts[i], + t.name~": ("~result~") != ("~test_ciphertexts[i]~")"); - // Decryption - t.init(false, key); - t.update(ByteConverter.hexDecode(test_ciphertexts[i]), buffer); - result = ByteConverter.hexEncode(buffer); - assert(result == test_plaintexts[i], - t.name~": ("~result~") != ("~test_plaintexts[i]~")"); + // Decryption + t.init(false, key); + t.update(ByteConverter.hexDecode(test_ciphertexts[i]), buffer); + result = ByteConverter.hexEncode(buffer); + assert(result == test_plaintexts[i], + t.name~": ("~result~") != ("~test_plaintexts[i]~")"); + } } } }