# HG changeset patch # User Thomas Dixon # Date 1234659500 18000 # Node ID 4589f8c5eb3c923070b1576acab3dd59acfa5b5c # Parent 74303a71703259bcd493d33cdb5e9c730906cb79 Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes. diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/Hash.d --- a/dcrypt/crypto/Hash.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/Hash.d Sat Feb 14 19:58:20 2009 -0500 @@ -8,7 +8,8 @@ module dcrypt.crypto.Hash; -public import dcrypt.misc.Util; +public import dcrypt.misc.ByteConverter; +public import dcrypt.misc.Bitwise; /** Base class for all cryptographic hash functions */ class Hash { @@ -112,7 +113,7 @@ * Returns: Representation of the final hash value in hex. */ char[] hexDigest() { - return Util.ubytesToHex(digest()); + return ByteConverter.hexEncode(digest()); } /** Reset hash to initial state. */ diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/MAC.d --- a/dcrypt/crypto/MAC.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/MAC.d Sat Feb 14 19:58:20 2009 -0500 @@ -11,7 +11,7 @@ public import dcrypt.crypto.params.CipherParameters; public import dcrypt.crypto.params.SymmetricKey; public import dcrypt.crypto.errors.InvalidParameterError; -import dcrypt.misc.Util; +import dcrypt.misc.ByteConverter; /** Base MAC class */ abstract class MAC { @@ -48,6 +48,6 @@ /** Returns: The computed MAC in hexadecimal. */ char[] hexDigest() { - return Util.ubytesToHex(digest()); + return ByteConverter.hexEncode(digest()); } -} \ No newline at end of file +} diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/ManagedBlockCipher.d --- a/dcrypt/crypto/ManagedBlockCipher.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/ManagedBlockCipher.d Sat Feb 14 19:58:20 2009 -0500 @@ -132,7 +132,7 @@ if (index) result += cipher.update(buffer[0..index], output[result..result+index]); - } else { + } else { // decrypt if (streamMode || index == blockSize) { result += cipher.update(buffer[0..index], buffer[0..index]); index = 0; diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/ciphers/AES.d --- a/dcrypt/crypto/ciphers/AES.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/ciphers/AES.d Sat Feb 14 19:58:20 2009 -0500 @@ -8,7 +8,8 @@ module dcrypt.crypto.ciphers.AES; -import dcrypt.misc.Util; +import dcrypt.misc.ByteConverter; +import dcrypt.misc.Bitwise; import dcrypt.crypto.BlockCipher; /** @@ -811,17 +812,17 @@ if (output.length < BLOCK_SIZE) throw new ShortBufferError(name()~": Output buffer too short"); - s0 = w[0] ^ Util.ubytesToUintBig(input, 0); - s1 = w[1] ^ Util.ubytesToUintBig(input, 4); - s2 = w[2] ^ Util.ubytesToUintBig(input, 8); - s3 = w[3] ^ Util.ubytesToUintBig(input, 12); + s0 = w[0] ^ ByteConverter.BigEndian.to!(uint)(input[0..4]); + s1 = w[1] ^ ByteConverter.BigEndian.to!(uint)(input[4..8]); + s2 = w[2] ^ ByteConverter.BigEndian.to!(uint)(input[8..12]); + s3 = w[3] ^ ByteConverter.BigEndian.to!(uint)(input[12..16]); - if (_encrypt) encryptBlock(); else decryptBlock(); + (_encrypt) ? encryptBlock() : decryptBlock(); - Util.uintToUbytesBig(s0, output, 0); - Util.uintToUbytesBig(s1, output, 4); - Util.uintToUbytesBig(s2, output, 8); - Util.uintToUbytesBig(s3, output, 12); + output[0..4] = ByteConverter.BigEndian.from!(uint)(s0); + output[4..8] = ByteConverter.BigEndian.from!(uint)(s1); + output[8..12] = ByteConverter.BigEndian.from!(uint)(s2); + output[12..16] = ByteConverter.BigEndian.from!(uint)(s3); return BLOCK_SIZE; } @@ -829,10 +830,10 @@ void reset() {} private uint subWord(uint x) { - return ((S[x>>24] << 24) | - (S[cast(ubyte)(x>>8)] << 8) | - (S[cast(ubyte)(x>>16)] << 16)| - (S[cast(ubyte)x])); + return ((S[x>>24] << 24) | + (S[cast(ubyte)(x>>8)] << 8) | + (S[cast(ubyte)(x>>16)] << 16)| + (S[cast(ubyte)x])); } private void setup(ubyte[] key) { @@ -841,13 +842,13 @@ w = new uint[4*(ROUNDS+1)]; for (uint i = 0, j = 0; i < nk; i++, j+=4) - w[i] = Util.ubytesToUintBig(key, j); + w[i] = ByteConverter.BigEndian.to!(uint)(key[j..j+int.sizeof]); for (uint i = nk; i < w.length; i++) { uint t = w[i-1]; if (i % nk == 0) - t = subWord(Util.rotateLeft(t, 8)) ^ RCON[(i/nk)-1]; + t = subWord(Bitwise.rotateLeft(t, 8)) ^ RCON[(i/nk)-1]; else if (nk > 6 && (i % nk == 4)) t = subWord(t); w[i] = w[i-nk] ^ t; @@ -902,19 +903,19 @@ foreach (uint i, char[] test_key; test_keys) { ubyte[] buffer = new ubyte[t.blockSize]; char[] result; - SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); + SymmetricKey key = new SymmetricKey(ByteConverter.hexDecode(test_key)); // Encryption t.init(true, key); - t.update(Util.hexToUbytes(test_plaintexts[i]), buffer); - result = Util.ubytesToHex(buffer); + 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(Util.hexToUbytes(test_ciphertexts[i]), buffer); - result = Util.ubytesToHex(buffer); + t.update(ByteConverter.hexDecode(test_ciphertexts[i]), buffer); + result = ByteConverter.hexEncode(buffer); assert(result == test_plaintexts[i], t.name~": ("~result~") != ("~test_plaintexts[i]~")"); } diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/ciphers/Blowfish.d --- a/dcrypt/crypto/ciphers/Blowfish.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/ciphers/Blowfish.d Sat Feb 14 19:58:20 2009 -0500 @@ -10,7 +10,8 @@ module dcrypt.crypto.ciphers.Blowfish; -import dcrypt.misc.Util; +import dcrypt.misc.ByteConverter; +import dcrypt.misc.Bitwise; import dcrypt.crypto.BlockCipher; /** Implementation of the Blowfish cipher designed by Bruce Schneier. */ @@ -262,8 +263,8 @@ if (output.length < BLOCK_SIZE) throw new ShortBufferError(name()~": Output buffer too short"); - uint xl = Util.ubytesToUintBig(input, 0), - xr = Util.ubytesToUintBig(input, 4), + uint xl = ByteConverter.BigEndian.to!(uint)(input[0..4]), + xr = ByteConverter.BigEndian.to!(uint)(input[4..8]), i = 0; xl ^= P[i++]; @@ -273,8 +274,8 @@ } xr ^= P[i]; - Util.uintToUbytesBig(xr, output, 0); - Util.uintToUbytesBig(xl, output, 4); + output[0..4] = ByteConverter.BigEndian.from!(uint)(xr); + output[4..8] = ByteConverter.BigEndian.from!(uint)(xl); return BLOCK_SIZE; } @@ -305,32 +306,32 @@ ubyte[] t = new ubyte[BLOCK_SIZE]; // Initialized to 0's for (int i = 0; i < PBOX_SIZE;) { update(t, t); - P[i++] = Util.ubytesToUintBig(t, 0); - P[i++] = Util.ubytesToUintBig(t, 4); + P[i++] = ByteConverter.BigEndian.to!(uint)(t[0..4]); + P[i++] = ByteConverter.BigEndian.to!(uint)(t[4..8]); } for (int i = 0; i < SBOX_SIZE;) { update(t, t); - S0[i++] = Util.ubytesToUintBig(t, 0); - S0[i++] = Util.ubytesToUintBig(t, 4); + S0[i++] = ByteConverter.BigEndian.to!(uint)(t[0..4]); + S0[i++] = ByteConverter.BigEndian.to!(uint)(t[4..8]); } for (int i = 0; i < SBOX_SIZE;) { update(t, t); - S1[i++] = Util.ubytesToUintBig(t, 0); - S1[i++] = Util.ubytesToUintBig(t, 4); + S1[i++] = ByteConverter.BigEndian.to!(uint)(t[0..4]); + S1[i++] = ByteConverter.BigEndian.to!(uint)(t[4..8]); } for (int i = 0; i < SBOX_SIZE;) { update(t, t); - S2[i++] = Util.ubytesToUintBig(t, 0); - S2[i++] = Util.ubytesToUintBig(t, 4); + S2[i++] = ByteConverter.BigEndian.to!(uint)(t[0..4]); + S2[i++] = ByteConverter.BigEndian.to!(uint)(t[4..8]); } for (int i = 0; i < SBOX_SIZE;) { update(t, t); - S3[i++] = Util.ubytesToUintBig(t, 0); - S3[i++] = Util.ubytesToUintBig(t, 4); + S3[i++] = ByteConverter.BigEndian.to!(uint)(t[0..4]); + S3[i++] = ByteConverter.BigEndian.to!(uint)(t[4..8]); } } @@ -371,19 +372,19 @@ foreach (uint i, char[] test_key; test_keys) { ubyte[] buffer = new ubyte[t.blockSize]; char[] result; - SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); + SymmetricKey key = new SymmetricKey(ByteConverter.hexDecode(test_key)); // Encryption t.init(true, key); - t.update(Util.hexToUbytes(test_plaintexts[i]), buffer); - result = Util.ubytesToHex(buffer); + 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(Util.hexToUbytes(test_ciphertexts[i]), buffer); - result = Util.ubytesToHex(buffer); + t.update(ByteConverter.hexDecode(test_ciphertexts[i]), buffer); + result = ByteConverter.hexEncode(buffer); assert(result == test_plaintexts[i], t.name~": ("~result~") != ("~test_plaintexts[i]~")"); } diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/ciphers/RC4.d --- a/dcrypt/crypto/ciphers/RC4.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/ciphers/RC4.d Sat Feb 14 19:58:20 2009 -0500 @@ -11,7 +11,7 @@ import dcrypt.crypto.StreamCipher; version (UnitTest) { - import dcrypt.misc.Util; + import dcrypt.misc.ByteConverter; } /** Implementation of RC4 designed by Ron Rivest of RSA Security. */ @@ -182,19 +182,19 @@ ubyte[] buffer = new ubyte[test_plaintexts[i].length>>1]; char[] result; - r.init(true, new SymmetricKey(Util.hexToUbytes(test_key))); + r.init(true, new SymmetricKey(ByteConverter.hexDecode(test_key))); // Encryption - r.update(Util.hexToUbytes(test_plaintexts[i]), buffer); - result = Util.ubytesToHex(buffer); + r.update(ByteConverter.hexDecode(test_plaintexts[i]), buffer); + result = ByteConverter.hexEncode(buffer); assert(result == test_ciphertexts[i], r.name~": ("~result~") != ("~test_ciphertexts[i]~")"); r.reset(); // Decryption - r.update(Util.hexToUbytes(test_ciphertexts[i]), buffer); - result = Util.ubytesToHex(buffer); + r.update(ByteConverter.hexDecode(test_ciphertexts[i]), buffer); + result = ByteConverter.hexEncode(buffer); assert(result == test_plaintexts[i], r.name~": ("~result~") != ("~test_plaintexts[i]~")"); } diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/ciphers/RC6.d --- a/dcrypt/crypto/ciphers/RC6.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/ciphers/RC6.d Sat Feb 14 19:58:20 2009 -0500 @@ -8,7 +8,8 @@ module dcrypt.crypto.ciphers.RC6; -import dcrypt.misc.Util; +import dcrypt.misc.Bitwise; +import dcrypt.misc.ByteConverter; import dcrypt.crypto.BlockCipher; /** @@ -76,10 +77,10 @@ if (output.length < BLOCK_SIZE) throw new ShortBufferError(name()~": Output buffer too short"); - uint A = Util.ubytesToUintLittle(input, 0), - B = Util.ubytesToUintLittle(input, 4), - C = Util.ubytesToUintLittle(input, 8), - D = Util.ubytesToUintLittle(input, 12), + uint A = ByteConverter.LittleEndian.to!(uint)(input[0..4]), + B = ByteConverter.LittleEndian.to!(uint)(input[4..8]), + C = ByteConverter.LittleEndian.to!(uint)(input[8..12]), + D = ByteConverter.LittleEndian.to!(uint)(input[12..16]), t, u; @@ -87,10 +88,10 @@ B += S[0]; D += S[1]; for (int i = 1; i <= ROUNDS; i++) { - t = Util.rotateLeft(B*((B<<1)+1), 5); - u = Util.rotateLeft(D*((D<<1)+1), 5); - A = Util.rotateLeft(A^t, u) + S[i<<1]; - C = Util.rotateLeft(C^u, t) + S[(i<<1)+1]; + t = Bitwise.rotateLeft(B*((B<<1)+1), 5); + u = Bitwise.rotateLeft(D*((D<<1)+1), 5); + A = Bitwise.rotateLeft(A^t, u) + S[i<<1]; + C = Bitwise.rotateLeft(C^u, t) + S[(i<<1)+1]; t = A; A = B; B = C; @@ -108,19 +109,19 @@ C = B; B = A; A = t; - u = Util.rotateLeft(D*((D<<1)+1), 5); - t = Util.rotateLeft(B*((B<<1)+1), 5); - C = Util.rotateRight(C-S[(i<<1)+1], t) ^ u; - A = Util.rotateRight(A-S[i<<1], u) ^ t; + u = Bitwise.rotateLeft(D*((D<<1)+1), 5); + t = Bitwise.rotateLeft(B*((B<<1)+1), 5); + C = Bitwise.rotateRight(C-S[(i<<1)+1], t) ^ u; + A = Bitwise.rotateRight(A-S[i<<1], u) ^ t; } D -= S[1]; B -= S[0]; } - Util.uintToUbytesLittle(A, output, 0); - Util.uintToUbytesLittle(B, output, 4); - Util.uintToUbytesLittle(C, output, 8); - Util.uintToUbytesLittle(D, output, 12); + output[0..4] = ByteConverter.LittleEndian.from!(uint)(A); + output[4..8] = ByteConverter.LittleEndian.from!(uint)(B); + output[8..12] = ByteConverter.LittleEndian.from!(uint)(C); + output[12..16] = ByteConverter.LittleEndian.from!(uint)(D); return BLOCK_SIZE; } @@ -133,7 +134,7 @@ uint c = key.length/4; uint[] L = new uint[c]; for (int i = 0, j = 0; i < c; i++, j+=4) - L[i] = Util.ubytesToUintLittle(key, j); + L[i] = ByteConverter.LittleEndian.to!(uint)(key[j..j+int.sizeof]); S[0] = P; for (int i = 1; i <= 2*ROUNDS+3; i++) @@ -141,8 +142,8 @@ uint A, B, i, j, v = 3*(2*ROUNDS+4); // Relying on ints initializing to 0 for (int s = 1; s <= v; s++) { - A = S[i] = Util.rotateLeft(S[i]+A+B, 3); - B = L[j] = Util.rotateLeft(L[j]+A+B, A+B); + A = S[i] = Bitwise.rotateLeft(S[i]+A+B, 3); + B = L[j] = Bitwise.rotateLeft(L[j]+A+B, A+B); i = (i + 1) % (2*ROUNDS+4); j = (j + 1) % c; } @@ -186,19 +187,19 @@ foreach (uint i, char[] test_key; test_keys) { ubyte[] buffer = new ubyte[t.blockSize]; char[] result; - SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); + SymmetricKey key = new SymmetricKey(ByteConverter.hexDecode(test_key)); // Encryption t.init(true, key); - t.update(Util.hexToUbytes(test_plaintexts[i]), buffer); - result = Util.ubytesToHex(buffer); + 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(Util.hexToUbytes(test_ciphertexts[i]), buffer); - result = Util.ubytesToHex(buffer); + t.update(ByteConverter.hexDecode(test_ciphertexts[i]), buffer); + result = ByteConverter.hexEncode(buffer); assert(result == test_plaintexts[i], t.name~": ("~result~") != ("~test_plaintexts[i]~")"); } diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/ciphers/TEA.d --- a/dcrypt/crypto/ciphers/TEA.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/ciphers/TEA.d Sat Feb 14 19:58:20 2009 -0500 @@ -8,7 +8,7 @@ module dcrypt.crypto.ciphers.TEA; -import dcrypt.misc.Util; +import dcrypt.misc.ByteConverter; import dcrypt.crypto.BlockCipher; /** Implementation of the TEA cipher designed by @@ -44,10 +44,10 @@ throw new InvalidKeyError( name()~": Invalid key length (requires 16 bytes)"); - sk0 = Util.ubytesToUintBig(keyParams.key, 0); - sk1 = Util.ubytesToUintBig(keyParams.key, 4); - sk2 = Util.ubytesToUintBig(keyParams.key, 8); - sk3 = Util.ubytesToUintBig(keyParams.key, 12); + sk0 = ByteConverter.BigEndian.to!(uint)(keyParams.key[0..4]); + sk1 = ByteConverter.BigEndian.to!(uint)(keyParams.key[4..8]); + sk2 = ByteConverter.BigEndian.to!(uint)(keyParams.key[8..12]); + sk3 = ByteConverter.BigEndian.to!(uint)(keyParams.key[12..16]); _initialized = true; } @@ -65,8 +65,8 @@ if (output.length < BLOCK_SIZE) throw new ShortBufferError(name()~": Output buffer too short"); - uint v0 = Util.ubytesToUintBig(input, 0), - v1 = Util.ubytesToUintBig(input, 4); + uint v0 = ByteConverter.BigEndian.to!(uint)(input[0..4]), + v1 = ByteConverter.BigEndian.to!(uint)(input[4..8]); sum = _encrypt ? 0 : DECRYPT_SUM; for (int i = 0; i < ROUNDS; i++) { @@ -81,8 +81,8 @@ } } - Util.uintToUbytesBig(v0, output, 0); - Util.uintToUbytesBig(v1, output, 4); + output[0..4] = ByteConverter.BigEndian.from!(uint)(v0); + output[4..8] = ByteConverter.BigEndian.from!(uint)(v1); return BLOCK_SIZE; } @@ -115,19 +115,19 @@ foreach (uint i, char[] test_key; test_keys) { ubyte[] buffer = new ubyte[t.blockSize]; char[] result; - SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); + SymmetricKey key = new SymmetricKey(ByteConverter.hexDecode(test_key)); // Encryption t.init(true, key); - t.update(Util.hexToUbytes(test_plaintexts[i]), buffer); - result = Util.ubytesToHex(buffer); + 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(Util.hexToUbytes(test_ciphertexts[i]), buffer); - result = Util.ubytesToHex(buffer); + t.update(ByteConverter.hexDecode(test_ciphertexts[i]), buffer); + result = ByteConverter.hexEncode(buffer); assert(result == test_plaintexts[i], t.name~": ("~result~") != ("~test_plaintexts[i]~")"); } diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/ciphers/XTEA.d --- a/dcrypt/crypto/ciphers/XTEA.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/ciphers/XTEA.d Sat Feb 14 19:58:20 2009 -0500 @@ -8,7 +8,7 @@ module dcrypt.crypto.ciphers.XTEA; -import dcrypt.misc.Util; +import dcrypt.misc.ByteConverter; import dcrypt.crypto.BlockCipher; /** Implementation of the XTEA cipher designed by @@ -50,8 +50,8 @@ sum1 = new uint[32]; int i, j; - for (i = j = 0; i < 4; i++, j+=4) - subkeys[i] = Util.ubytesToUintBig(keyParams.key, j); + for (i = j = 0; i < 4; i++, j+=int.sizeof) + subkeys[i] = ByteConverter.BigEndian.to!(uint)(keyParams.key[j..j+int.sizeof]); // Precompute the values of sum + k[] to speed up encryption for (i = j = 0; i < ROUNDS; i++) { @@ -75,8 +75,8 @@ if (output.length < BLOCK_SIZE) throw new ShortBufferError(name()~": Output buffer too short"); - uint v0 = Util.ubytesToUintBig(input, 0), - v1 = Util.ubytesToUintBig(input, 4); + uint v0 = ByteConverter.BigEndian.to!(uint)(input[0..4]), + v1 = ByteConverter.BigEndian.to!(uint)(input[4..8]); if (_encrypt) { for (int i = 0; i < ROUNDS; i++) { @@ -90,8 +90,8 @@ } } - Util.uintToUbytesBig(v0, output, 0); - Util.uintToUbytesBig(v1, output, 4); + output[0..4] = ByteConverter.BigEndian.from!(uint)(v0); + output[4..8] = ByteConverter.BigEndian.from!(uint)(v1); return BLOCK_SIZE; } @@ -142,19 +142,19 @@ foreach (uint i, char[] test_key; test_keys) { ubyte[] buffer = new ubyte[t.blockSize]; char[] result; - SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); + SymmetricKey key = new SymmetricKey(ByteConverter.hexDecode(test_key)); // Encryption t.init(true, key); - t.update(Util.hexToUbytes(test_plaintexts[i]), buffer); - result = Util.ubytesToHex(buffer); + 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(Util.hexToUbytes(test_ciphertexts[i]), buffer); - result = Util.ubytesToHex(buffer); + t.update(ByteConverter.hexDecode(test_ciphertexts[i]), buffer); + result = ByteConverter.hexEncode(buffer); assert(result == test_plaintexts[i], t.name~": ("~result~") != ("~test_plaintexts[i]~")"); } diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/hashes/MD5.d --- a/dcrypt/crypto/hashes/MD5.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/hashes/MD5.d Sat Feb 14 19:58:20 2009 -0500 @@ -64,8 +64,8 @@ void transform(ubyte[] input) { uint[] w = new uint[16]; - for (int i = 0, j = 0; i < 16; i++,j+=4) - w[i] = Util.ubytesToUintLittle(input, j); + for (int i = 0, j = 0; i < 16; i++,j+=int.sizeof) + w[i] = ByteConverter.LittleEndian.to!(uint)(input[j..j+int.sizeof]); uint a = h0, b = h1, @@ -171,28 +171,28 @@ private void ff(ref uint a, uint b, uint c, uint d, uint x, uint s, uint ac) { a += f(b, c, d) + x + ac; - a = Util.rotateLeft(a, s); + a = Bitwise.rotateLeft(a, s); a += b; } private void gg(ref uint a, uint b, uint c, uint d, uint x, uint s, uint ac) { a += g(b, c, d) + x + ac; - a = Util.rotateLeft(a, s); + a = Bitwise.rotateLeft(a, s); a += b; } private void hh(ref uint a, uint b, uint c, uint d, uint x, uint s, uint ac) { a += h(b, c, d) + x + ac; - a = Util.rotateLeft(a, s); + a = Bitwise.rotateLeft(a, s); a += b; } private void ii(ref uint a, uint b, uint c, uint d, uint x, uint s, uint ac) { a += i(b, c, d) + x + ac; - a = Util.rotateLeft(a, s); + a = Bitwise.rotateLeft(a, s); a += b; } @@ -200,10 +200,10 @@ padMessage(MODE_MD); ubyte[] result = new ubyte[digestSize]; - Util.uintToUbytesLittle(h0, result, 0); - Util.uintToUbytesLittle(h1, result, 4); - Util.uintToUbytesLittle(h2, result, 8); - Util.uintToUbytesLittle(h3, result, 12); + result[0..4] = ByteConverter.LittleEndian.from!(uint)(h0); + result[4..8] = ByteConverter.LittleEndian.from!(uint)(h1); + result[8..12] = ByteConverter.LittleEndian.from!(uint)(h2); + result[12..16] = ByteConverter.LittleEndian.from!(uint)(h3); reset(); return result; diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/hashes/SHA1.d --- a/dcrypt/crypto/hashes/SHA1.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/hashes/SHA1.d Sat Feb 14 19:58:20 2009 -0500 @@ -40,11 +40,11 @@ void transform(ubyte[] input) { uint[] w = new uint[80]; - for (int i = 0, j = 0; i < 16; i++,j+=4) - w[i] = Util.ubytesToUintBig(input, j); + for (int i = 0, j = 0; i < 16; i++,j+=int.sizeof) + w[i] = ByteConverter.BigEndian.to!(uint)(input[j..j+int.sizeof]); for (int i = 16; i < 80; i++) - w[i] = Util.rotateLeft(w[i-3]^w[i-8]^w[i-14]^w[i-16], 1); + w[i] = Bitwise.rotateLeft(w[i-3]^w[i-8]^w[i-14]^w[i-16], 1); uint a = h0, b = h1, @@ -54,71 +54,71 @@ int i = 0; for (; i < 20;) { - e += Util.rotateLeft(a, 5) + f0(b, c, d) + w[i++]; - b = Util.rotateLeft(b, 30); + e += Bitwise.rotateLeft(a, 5) + f0(b, c, d) + w[i++]; + b = Bitwise.rotateLeft(b, 30); - d += Util.rotateLeft(e, 5) + f0(a, b, c) + w[i++]; - a = Util.rotateLeft(a, 30); + d += Bitwise.rotateLeft(e, 5) + f0(a, b, c) + w[i++]; + a = Bitwise.rotateLeft(a, 30); - c += Util.rotateLeft(d, 5) + f0(e, a, b) + w[i++]; - e = Util.rotateLeft(e, 30); + c += Bitwise.rotateLeft(d, 5) + f0(e, a, b) + w[i++]; + e = Bitwise.rotateLeft(e, 30); - b += Util.rotateLeft(c, 5) + f0(d, e, a) + w[i++]; - d = Util.rotateLeft(d, 30); + b += Bitwise.rotateLeft(c, 5) + f0(d, e, a) + w[i++]; + d = Bitwise.rotateLeft(d, 30); - a += Util.rotateLeft(b, 5) + f0(c, d, e) + w[i++]; - c = Util.rotateLeft(c, 30); + a += Bitwise.rotateLeft(b, 5) + f0(c, d, e) + w[i++]; + c = Bitwise.rotateLeft(c, 30); } for (; i < 40;) { - e += Util.rotateLeft(a, 5) + f1(b, c, d) + w[i++]; - b = Util.rotateLeft(b, 30); + e += Bitwise.rotateLeft(a, 5) + f1(b, c, d) + w[i++]; + b = Bitwise.rotateLeft(b, 30); - d += Util.rotateLeft(e, 5) + f1(a, b, c) + w[i++]; - a = Util.rotateLeft(a, 30); + d += Bitwise.rotateLeft(e, 5) + f1(a, b, c) + w[i++]; + a = Bitwise.rotateLeft(a, 30); - c += Util.rotateLeft(d, 5) + f1(e, a, b) + w[i++]; - e = Util.rotateLeft(e, 30); + c += Bitwise.rotateLeft(d, 5) + f1(e, a, b) + w[i++]; + e = Bitwise.rotateLeft(e, 30); - b += Util.rotateLeft(c, 5) + f1(d, e, a) + w[i++]; - d = Util.rotateLeft(d, 30); + b += Bitwise.rotateLeft(c, 5) + f1(d, e, a) + w[i++]; + d = Bitwise.rotateLeft(d, 30); - a += Util.rotateLeft(b, 5) + f1(c, d, e) + w[i++]; - c = Util.rotateLeft(c, 30); + a += Bitwise.rotateLeft(b, 5) + f1(c, d, e) + w[i++]; + c = Bitwise.rotateLeft(c, 30); } for (; i < 60;) { - e += Util.rotateLeft(a, 5) + f2(b, c, d) + w[i++]; - b = Util.rotateLeft(b, 30); + e += Bitwise.rotateLeft(a, 5) + f2(b, c, d) + w[i++]; + b = Bitwise.rotateLeft(b, 30); - d += Util.rotateLeft(e, 5) + f2(a, b, c) + w[i++]; - a = Util.rotateLeft(a, 30); + d += Bitwise.rotateLeft(e, 5) + f2(a, b, c) + w[i++]; + a = Bitwise.rotateLeft(a, 30); - c += Util.rotateLeft(d, 5) + f2(e, a, b) + w[i++]; - e = Util.rotateLeft(e, 30); + c += Bitwise.rotateLeft(d, 5) + f2(e, a, b) + w[i++]; + e = Bitwise.rotateLeft(e, 30); - b += Util.rotateLeft(c, 5) + f2(d, e, a) + w[i++]; - d = Util.rotateLeft(d, 30); + b += Bitwise.rotateLeft(c, 5) + f2(d, e, a) + w[i++]; + d = Bitwise.rotateLeft(d, 30); - a += Util.rotateLeft(b, 5) + f2(c, d, e) + w[i++]; - c = Util.rotateLeft(c, 30); + a += Bitwise.rotateLeft(b, 5) + f2(c, d, e) + w[i++]; + c = Bitwise.rotateLeft(c, 30); } for (; i < 80;) { - e += Util.rotateLeft(a, 5) + f3(b, c, d) + w[i++]; - b = Util.rotateLeft(b, 30); + e += Bitwise.rotateLeft(a, 5) + f3(b, c, d) + w[i++]; + b = Bitwise.rotateLeft(b, 30); - d += Util.rotateLeft(e, 5) + f3(a, b, c) + w[i++]; - a = Util.rotateLeft(a, 30); + d += Bitwise.rotateLeft(e, 5) + f3(a, b, c) + w[i++]; + a = Bitwise.rotateLeft(a, 30); - c += Util.rotateLeft(d, 5) + f3(e, a, b) + w[i++]; - e = Util.rotateLeft(e, 30); + c += Bitwise.rotateLeft(d, 5) + f3(e, a, b) + w[i++]; + e = Bitwise.rotateLeft(e, 30); - b += Util.rotateLeft(c, 5) + f3(d, e, a) + w[i++]; - d = Util.rotateLeft(d, 30); + b += Bitwise.rotateLeft(c, 5) + f3(d, e, a) + w[i++]; + d = Bitwise.rotateLeft(d, 30); - a += Util.rotateLeft(b, 5) + f3(c, d, e) + w[i++]; - c = Util.rotateLeft(c, 30); + a += Bitwise.rotateLeft(b, 5) + f3(c, d, e) + w[i++]; + c = Bitwise.rotateLeft(c, 30); } h0 += a; @@ -148,11 +148,11 @@ padMessage(MODE_SHA); ubyte[] result = new ubyte[digestSize]; - Util.uintToUbytesBig(h0, result, 0); - Util.uintToUbytesBig(h1, result, 4); - Util.uintToUbytesBig(h2, result, 8); - Util.uintToUbytesBig(h3, result, 12); - Util.uintToUbytesBig(h4, result, 16); + result[0..4] = ByteConverter.BigEndian.from!(uint)(h0); + result[4..8] = ByteConverter.BigEndian.from!(uint)(h1); + result[8..12] = ByteConverter.BigEndian.from!(uint)(h2); + result[12..16] = ByteConverter.BigEndian.from!(uint)(h3); + result[16..20] = ByteConverter.BigEndian.from!(uint)(h4); reset(); return result; diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/hashes/SHA224.d --- a/dcrypt/crypto/hashes/SHA224.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/hashes/SHA224.d Sat Feb 14 19:58:20 2009 -0500 @@ -35,13 +35,13 @@ ubyte[] result = new ubyte[digestSize]; - Util.uintToUbytesBig(h0, result, 0); - Util.uintToUbytesBig(h1, result, 4); - Util.uintToUbytesBig(h2, result, 8); - Util.uintToUbytesBig(h3, result, 12); - Util.uintToUbytesBig(h4, result, 16); - Util.uintToUbytesBig(h5, result, 20); - Util.uintToUbytesBig(h6, result, 24); + result[0..4] = ByteConverter.BigEndian.from!(uint)(h0); + result[4..8] = ByteConverter.BigEndian.from!(uint)(h1); + result[8..12] = ByteConverter.BigEndian.from!(uint)(h2); + result[12..16] = ByteConverter.BigEndian.from!(uint)(h3); + result[16..20] = ByteConverter.BigEndian.from!(uint)(h4); + result[20..24] = ByteConverter.BigEndian.from!(uint)(h5); + result[24..28] = ByteConverter.BigEndian.from!(uint)(h6); reset(); return result; diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/hashes/SHA256.d --- a/dcrypt/crypto/hashes/SHA256.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/hashes/SHA256.d Sat Feb 14 19:58:20 2009 -0500 @@ -57,8 +57,8 @@ void transform(ubyte[] input) { uint[] w = new uint[64]; - for (int i = 0, j = 0; i < 16; i++,j+=4) - w[i] = Util.ubytesToUintBig(input, j); + for (int i = 0, j = 0; i < 16; i++,j+=int.sizeof) + w[i] = ByteConverter.BigEndian.to!(uint)(input[j..j+int.sizeof]); for (int i = 16; i < 64; i++) w[i] = theta1(w[i-2]) + w[i-7] + theta0(w[i-15]) + w[i-16]; @@ -104,33 +104,33 @@ } private uint sum0(uint x) { - return Util.rotateRight(x,2)^Util.rotateRight(x,13)^Util.rotateRight(x,22); + return Bitwise.rotateRight(x,2)^Bitwise.rotateRight(x,13)^Bitwise.rotateRight(x,22); } private uint sum1(uint x) { - return Util.rotateRight(x,6)^Util.rotateRight(x,11)^Util.rotateRight(x,25); + return Bitwise.rotateRight(x,6)^Bitwise.rotateRight(x,11)^Bitwise.rotateRight(x,25); } private uint theta0(uint x) { - return Util.rotateRight(x,7)^Util.rotateRight(x,18)^(x >> 3); + return Bitwise.rotateRight(x,7)^Bitwise.rotateRight(x,18)^(x >> 3); } private uint theta1(uint x) { - return Util.rotateRight(x,17)^Util.rotateRight(x,19)^(x >> 10); + return Bitwise.rotateRight(x,17)^Bitwise.rotateRight(x,19)^(x >> 10); } ubyte[] digest() { padMessage(MODE_SHA); ubyte[] result = new ubyte[digestSize]; - Util.uintToUbytesBig(h0, result, 0); - Util.uintToUbytesBig(h1, result, 4); - Util.uintToUbytesBig(h2, result, 8); - Util.uintToUbytesBig(h3, result, 12); - Util.uintToUbytesBig(h4, result, 16); - Util.uintToUbytesBig(h5, result, 20); - Util.uintToUbytesBig(h6, result, 24); - Util.uintToUbytesBig(h7, result, 28); + result[0..4] = ByteConverter.BigEndian.from!(uint)(h0); + result[4..8] = ByteConverter.BigEndian.from!(uint)(h1); + result[8..12] = ByteConverter.BigEndian.from!(uint)(h2); + result[12..16] = ByteConverter.BigEndian.from!(uint)(h3); + result[16..20] = ByteConverter.BigEndian.from!(uint)(h4); + result[20..24] = ByteConverter.BigEndian.from!(uint)(h5); + result[24..28] = ByteConverter.BigEndian.from!(uint)(h6); + result[28..32] = ByteConverter.BigEndian.from!(uint)(h7); reset(); return result; diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/hashes/SHA384.d --- a/dcrypt/crypto/hashes/SHA384.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/hashes/SHA384.d Sat Feb 14 19:58:20 2009 -0500 @@ -34,12 +34,12 @@ padMessage(MODE_SHA); ubyte[] result = new ubyte[digestSize]; - Util.ulongToUbytesBig(h0, result, 0); - Util.ulongToUbytesBig(h1, result, 8); - Util.ulongToUbytesBig(h2, result, 16); - Util.ulongToUbytesBig(h3, result, 24); - Util.ulongToUbytesBig(h4, result, 32); - Util.ulongToUbytesBig(h5, result, 40); + result[0..8] = ByteConverter.BigEndian.from!(ulong)(h0); + result[8..16] = ByteConverter.BigEndian.from!(ulong)(h1); + result[16..24] = ByteConverter.BigEndian.from!(ulong)(h2); + result[24..32] = ByteConverter.BigEndian.from!(ulong)(h3); + result[32..40] = ByteConverter.BigEndian.from!(ulong)(h4); + result[40..48] = ByteConverter.BigEndian.from!(ulong)(h5); reset(); return result; diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/hashes/SHA512.d --- a/dcrypt/crypto/hashes/SHA512.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/hashes/SHA512.d Sat Feb 14 19:58:20 2009 -0500 @@ -61,8 +61,8 @@ void transform(ubyte[] input) { ulong[] w = new ulong[80]; - for (int i = 0, j = 0; i < 16; i++,j+=8) - w[i] = Util.ubytesToUlongBig(input, j); + for (int i = 0, j = 0; i < 16; i++,j+=long.sizeof) + w[i] = ByteConverter.BigEndian.to!(ulong)(input[j..j+long.sizeof]); for (int i = 16; i < 80; i++) w[i] = theta1(w[i-2]) + w[i-7] + theta0(w[i-15]) + w[i-16]; @@ -108,37 +108,37 @@ } private ulong sum0(ulong x) { - return (Util.rotateRight64(x,28)^ - Util.rotateRight64(x,34)^ - Util.rotateRight64(x,39)); + return (Bitwise.rotateRight(x,28)^ + Bitwise.rotateRight(x,34)^ + Bitwise.rotateRight(x,39)); } private ulong sum1(ulong x) { - return (Util.rotateRight64(x,14)^ - Util.rotateRight64(x,18)^ - Util.rotateRight64(x,41)); + return (Bitwise.rotateRight(x,14)^ + Bitwise.rotateRight(x,18)^ + Bitwise.rotateRight(x,41)); } private ulong theta0(ulong x) { - return Util.rotateRight64(x,1)^Util.rotateRight64(x,8)^(x >> 7); + return Bitwise.rotateRight(x,1)^Bitwise.rotateRight(x,8)^(x >> 7); } private ulong theta1(ulong x) { - return Util.rotateRight64(x,19)^Util.rotateRight64(x,61)^(x >> 6); + return Bitwise.rotateRight(x,19)^Bitwise.rotateRight(x,61)^(x >> 6); } ubyte[] digest() { padMessage(MODE_SHA); ubyte[] result = new ubyte[digestSize]; - Util.ulongToUbytesBig(h0, result, 0); - Util.ulongToUbytesBig(h1, result, 8); - Util.ulongToUbytesBig(h2, result, 16); - Util.ulongToUbytesBig(h3, result, 24); - Util.ulongToUbytesBig(h4, result, 32); - Util.ulongToUbytesBig(h5, result, 40); - Util.ulongToUbytesBig(h6, result, 48); - Util.ulongToUbytesBig(h7, result, 56); + result[0..8] = ByteConverter.BigEndian.from!(ulong)(h0); + result[8..16] = ByteConverter.BigEndian.from!(ulong)(h1); + result[16..24] = ByteConverter.BigEndian.from!(ulong)(h2); + result[24..32] = ByteConverter.BigEndian.from!(ulong)(h3); + result[32..40] = ByteConverter.BigEndian.from!(ulong)(h4); + result[40..48] = ByteConverter.BigEndian.from!(ulong)(h5); + result[48..56] = ByteConverter.BigEndian.from!(ulong)(h6); + result[56..64] = ByteConverter.BigEndian.from!(ulong)(h7); reset(); return result; diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/macs/HMAC.d --- a/dcrypt/crypto/macs/HMAC.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/macs/HMAC.d Sat Feb 14 19:58:20 2009 -0500 @@ -102,7 +102,7 @@ } char[] hexDigest() { - return Util.ubytesToHex(digest()); + return ByteConverter.hexEncode(digest()); } HMAC copy() { @@ -146,13 +146,13 @@ HMAC h = new HMAC(new SHA1()); foreach (uint i, char[] k; test_keys) { - h.init(new SymmetricKey(Util.hexToUbytes(k))); + h.init(new SymmetricKey(ByteConverter.hexDecode(k))); for (int j = 0; j < test_repeat[i]; j++) - h.update(Util.hexToUbytes(test_inputs[i])); + h.update(ByteConverter.hexDecode(test_inputs[i])); char[] mac = h.hexDigest(); assert(mac == test_results[i], h.name~": ("~mac~") != ("~test_results[i]~")"); } } } -} \ No newline at end of file +} diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/modes/CBC.d --- a/dcrypt/crypto/modes/CBC.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/modes/CBC.d Sat Feb 14 19:58:20 2009 -0500 @@ -13,12 +13,12 @@ version (UnitTest) { import dcrypt.crypto.ciphers.XTEA; - import dcrypt.misc.Util; + import dcrypt.misc.ByteConverter; } /** This class implements the cipher block chaining (CBC) block mode. */ class CBC : BlockCipher { - private BlockCipher m_cipher; + private BlockCipher wrappedCipher; private ubyte[] iv, previousCiphertext, cbcOutput; @@ -30,21 +30,21 @@ * cipher = Block cipher to wrap. */ this (BlockCipher cipher) { - m_cipher = cipher; + wrappedCipher = cipher; } /** Returns: The underlying cipher we are wrapping. */ BlockCipher cipher() { - return m_cipher; + return wrappedCipher; } char[] name() { - return m_cipher.name~"/CBC"; + return wrappedCipher.name~"/CBC"; } /** * Throws: dcrypt.crypto.errors.InvalidParameterError if params aren't - * an instance of dcrypt.crypto.params.ParametersWithIV. + * an instance of dcrypt.crypto.params.ParametersWithIV. */ void init(bool encrypt, CipherParameters params) { ParametersWithIV ivParams = cast(ParametersWithIV)params; @@ -57,7 +57,7 @@ name()~": IV must be same length as cipher block size"); this.encrypt = encrypt; - m_cipher.init(encrypt, ivParams.parameters); + wrappedCipher.init(encrypt, ivParams.parameters); iv = ivParams.iv[0..blockSize]; previousCiphertext = new ubyte[blockSize]; @@ -87,7 +87,7 @@ previousCiphertext[i] ^= input[i]; // E_k(P_i XOR C_i-1) - m_cipher.update(previousCiphertext, cbcOutput); + wrappedCipher.update(previousCiphertext, cbcOutput); // Store C_i for next block previousCiphertext[] = cbcOutput; @@ -99,7 +99,7 @@ ubyte[] t = input[0..blockSize]; // D_k(C_i) - m_cipher.update(t, cbcOutput); + wrappedCipher.update(t, cbcOutput); // P_i = D_k(C_i) XOR C_i-1 for (int i = 0; i < blockSize; i++) @@ -113,12 +113,12 @@ } uint blockSize() { - return m_cipher.blockSize; + return wrappedCipher.blockSize; } void reset() { previousCiphertext[] = iv; - m_cipher.reset(); + wrappedCipher.reset(); } /** Test vectors for CBC mode. Assumes XTEA passes test vectors. */ @@ -158,22 +158,22 @@ buffer = new ubyte[32]; char[] result; for (int i = 0; i < test_keys.length; i++) { - SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_keys[i])); + SymmetricKey key = new SymmetricKey(ByteConverter.hexDecode(test_keys[i])); ParametersWithIV params = new ParametersWithIV(key, iv); // Encryption c.init(true, params); for (int j = 0; j < 32; j+=x.blockSize) - c.update(Util.hexToUbytes(test_plaintexts[i])[j..j+x.blockSize], buffer[j..j+x.blockSize]); - result = Util.ubytesToHex(buffer); + c.update(ByteConverter.hexDecode(test_plaintexts[i])[j..j+x.blockSize], buffer[j..j+x.blockSize]); + result = ByteConverter.hexEncode(buffer); assert(result == test_ciphertexts[i], c.name()~": ("~result~") != ("~test_ciphertexts[i]~")"); // Decryption c.init(false, params); for (int j = 0; j < 32; j+=x.blockSize) - c.update(Util.hexToUbytes(test_ciphertexts[i])[j..j+x.blockSize], buffer[j..j+x.blockSize]); - result = Util.ubytesToHex(buffer); + c.update(ByteConverter.hexDecode(test_ciphertexts[i])[j..j+x.blockSize], buffer[j..j+x.blockSize]); + result = ByteConverter.hexEncode(buffer); assert(result == test_plaintexts[i], c.name()~": ("~result~") != ("~test_plaintexts[i]~")"); } diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/modes/CTR.d --- a/dcrypt/crypto/modes/CTR.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/modes/CTR.d Sat Feb 14 19:58:20 2009 -0500 @@ -15,7 +15,7 @@ /** This class implements the counter (CTR/SIC/ICM) block mode, treating the counter as a big endian integer. */ class CTR : BlockCipher { - private BlockCipher m_cipher; + private BlockCipher wrappedCipher; private ubyte[] iv, counter, counterOutput; @@ -26,21 +26,21 @@ * cipher = Block cipher to wrap. */ this (BlockCipher cipher) { - m_cipher = cipher; + wrappedCipher = cipher; } /** Returns: The underlying cipher we are wrapping. */ BlockCipher cipher() { - return m_cipher; + return wrappedCipher; } char[] name() { - return m_cipher.name~"/CTR"; + return wrappedCipher.name~"/CTR"; } /** * Throws: dcrypt.crypto.errors.InvalidParameterError if params aren't - * an instance of dcrypt.crypto.params.ParametersWithIV. + * an instance of dcrypt.crypto.params.ParametersWithIV. */ void init(bool encrypt, CipherParameters params) { ParametersWithIV ivParams = cast(ParametersWithIV)params; @@ -52,7 +52,7 @@ throw new InvalidParameterError( name()~": IV must be same length as cipher block size"); - m_cipher.init(true, ivParams.parameters); + wrappedCipher.init(true, ivParams.parameters); iv = ivParams.iv[0..blockSize]; counter = new ubyte[blockSize]; @@ -76,7 +76,7 @@ throw new ShortBufferError(name()~": Output buffer too short"); // Encrypt the counter - m_cipher.update(counter, counterOutput); + wrappedCipher.update(counter, counterOutput); // XOR output with plaintext to create ciphertext for (int i = 0; i < len; i++) @@ -92,11 +92,11 @@ } uint blockSize() { - return m_cipher.blockSize; + return wrappedCipher.blockSize; } void reset() { counter[] = iv; - m_cipher.reset(); + wrappedCipher.reset(); } } diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/crypto/prngs/PBKDF2.d --- a/dcrypt/crypto/prngs/PBKDF2.d Sat Jan 10 13:17:58 2009 -0500 +++ b/dcrypt/crypto/prngs/PBKDF2.d Sat Feb 14 19:58:20 2009 -0500 @@ -8,7 +8,7 @@ module dcrypt.crypto.prngs.PBKDF2; -import dcrypt.misc.Util; +import dcrypt.misc.ByteConverter; import dcrypt.crypto.PRNG; import dcrypt.crypto.MAC; import dcrypt.crypto.macs.HMAC; @@ -34,6 +34,7 @@ blockCount, index; } + /** * Params: * password = User supplied password @@ -82,7 +83,7 @@ ubyte[] t = new ubyte[salt.length + uint.sizeof]; t[0..salt.length] = salt; - Util.uintToUbytesBig(blockCount, t, salt.length); + t[salt.length..salt.length+int.sizeof] = ByteConverter.BigEndian.from!(uint)(blockCount); for (uint j = 0; j < iterations; j++) { prf.reset(); @@ -141,10 +142,10 @@ pbkdf2 = new PBKDF2(p, test_salts[i], test_iterations[i]); ubyte[] result = new ubyte[test_results[i].length >> 1]; pbkdf2.read(result); - char[] hexResult = Util.ubytesToHex(result); + char[] hexResult = ByteConverter.hexEncode(result); assert(hexResult == test_results[i], pbkdf2.name~": ("~hexResult~") != ("~test_results[i]~")"); } } } -} \ No newline at end of file +} diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/misc/Bitwise.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dcrypt/misc/Bitwise.d Sat Feb 14 19:58:20 2009 -0500 @@ -0,0 +1,45 @@ +/** + * This file is part of the dcrypt project. + * + * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved. + * License: MIT + * Authors: Thomas Dixon + */ + +module dcrypt.misc.Bitwise; + +/** Common bitwise operations */ +struct Bitwise { + + static uint rotateLeft(uint x, int y) { + return (x << y) | (x >> (32-y)); + } + + static uint rotateLeft(uint x, uint y) { + return (x << y) | (x >> (32u-y)); + } + + static ulong rotateLeft(ulong x, int y) { + return (x << y) | (x >> (64-y)); + } + + static ulong rotateLeft(ulong x, uint y) { + return (x << y) | (x >> (64u-y)); + } + + static uint rotateRight(uint x, int y) { + return (x >> y) | (x << (32-y)); + } + + static uint rotateRight(uint x, uint y) { + return (x >> y) | (x << (32u-y)); + } + + static ulong rotateRight(ulong x, int y) { + return (x >> y) | (x << (64-y)); + } + + static ulong rotateRight(ulong x, uint y) { + return (x >> y) | (x << (64u-y)); + } +} diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/misc/ByteConverter.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dcrypt/misc/ByteConverter.d Sat Feb 14 19:58:20 2009 -0500 @@ -0,0 +1,156 @@ +/** + * This file is part of the dcrypt project. + * + * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved. + * License: MIT + * Authors: Thomas Dixon + */ + +module dcrypt.misc.ByteConverter; + +/** Converts between integral types and unsigned byte arrays */ +struct ByteConverter { + private static const char[] hexits = "0123456789abcdef"; + + /** Conversions between little endian integrals and bytes */ + struct LittleEndian { + /** + * Converts the supplied array to integral type T + * + * Params: + * x_ = The supplied array of bytes (ubytes, bytes, chars, whatever) + * + * Returns: + * A integral of type T created with the supplied bytes placed + * in the specified byte order. + */ + static T to(T)(void[] x_) { + ubyte[] x = cast(ubyte[])x_; + + T result = ((x[0] & 0xff) | + ((x[1] & 0xff) << 8)); + + static if (T.sizeof >= int.sizeof) { + result |= ((x[2] & 0xff) << 16) | + ((x[3] & 0xff) << 24); + } + + static if (T.sizeof >= long.sizeof) { + result |= (cast(T)(x[4] & 0xff) << 32) | + (cast(T)(x[5] & 0xff) << 40) | + (cast(T)(x[6] & 0xff) << 48) | + (cast(T)(x[7] & 0xff) << 56); + } + + return result; + } + + /** + * Converts the supplied integral to an array of unsigned bytes. + * + * Params: + * input = Integral to convert to bytes + * + * Returns: + * Integral input of type T split into its respective bytes + * with the bytes placed in the specified byte order. + */ + static ubyte[] from(T)(T input) { + ubyte[] output = new ubyte[T.sizeof]; + + output[0] = cast(ubyte)(input); + output[1] = cast(ubyte)(input >> 8); + + static if (T.sizeof >= int.sizeof) { + output[2] = cast(ubyte)(input >> 16); + output[3] = cast(ubyte)(input >> 24); + } + + static if (T.sizeof >= long.sizeof) { + output[4] = cast(ubyte)(input >> 32); + output[5] = cast(ubyte)(input >> 40); + output[6] = cast(ubyte)(input >> 48); + output[7] = cast(ubyte)(input >> 56); + } + + return output; + } + } + + /** Conversions between big endian integrals and bytes */ + struct BigEndian { + + static T to(T)(void[] x_) { + ubyte[] x = cast(ubyte[])x_; + + static if (is(T == ushort) || is(T == short)) { + return cast(T) (((x[0] & 0xff) << 8) | + (x[1] & 0xff)); + } else static if (is(T == uint) || is(T == int)) { + return cast(T) (((x[0] & 0xff) << 24) | + ((x[1] & 0xff) << 16) | + ((x[2] & 0xff) << 8) | + (x[3] & 0xff)); + } else static if (is(T == ulong) || is(T == long)) { + return cast(T) ((cast(T)(x[0] & 0xff) << 56) | + (cast(T)(x[1] & 0xff) << 48) | + (cast(T)(x[2] & 0xff) << 40) | + (cast(T)(x[3] & 0xff) << 32) | + ((x[4] & 0xff) << 24) | + ((x[5] & 0xff) << 16) | + ((x[6] & 0xff) << 8) | + (x[7] & 0xff)); + } + } + + static ubyte[] from(T)(T input) { + ubyte[] output = new ubyte[T.sizeof]; + + static if (T.sizeof == long.sizeof) { + output[0] = cast(ubyte)(input >> 56); + output[1] = cast(ubyte)(input >> 48); + output[2] = cast(ubyte)(input >> 40); + output[3] = cast(ubyte)(input >> 32); + output[4] = cast(ubyte)(input >> 24); + output[5] = cast(ubyte)(input >> 16); + output[6] = cast(ubyte)(input >> 8); + output[7] = cast(ubyte)(input); + } else static if (T.sizeof == int.sizeof) { + output[0] = cast(ubyte)(input >> 24); + output[1] = cast(ubyte)(input >> 16); + output[2] = cast(ubyte)(input >> 8); + output[3] = cast(ubyte)(input); + } else static if (T.sizeof == short.sizeof) { + output[0] = cast(ubyte)(input >> 8); + output[1] = cast(ubyte)(input); + } + return output; + } + } + + static char[] hexEncode(void[] input_) { + ubyte[] input = cast(ubyte[])input_; + char[] output = new char[input.length<<1]; + + int i = 0; + foreach (ubyte j; input) { + output[i++] = hexits[j>>4]; + output[i++] = hexits[j&0xf]; + } + return output; + } + + static ubyte[] hexDecode(char[] input) { + ubyte[] output = new ubyte[input.length>>1]; + + static ubyte[char] hexitIndex; + for (int i = 0; i < hexits.length; i++) + hexitIndex[hexits[i]] = i; + + for (int i = 0, j = 0; i < output.length; i++) { + output[i] = hexitIndex[input[j++]] << 4; + output[i] |= hexitIndex[input[j++]]; + } + return output; + } +} diff -r 74303a717032 -r 4589f8c5eb3c dcrypt/misc/Util.d --- a/dcrypt/misc/Util.d Sat Jan 10 13:17:58 2009 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,159 +0,0 @@ -/** - * This file is part of the dcrypt project. - * - * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved. - * License: MIT - * Authors: Thomas Dixon - */ - -// TODO: WRITE DOCS -module dcrypt.misc.Util; - -/** Utility functions */ -struct Util { - static const char[] hexits = "0123456789abcdef"; - - final static uint ubytesToUintBig(ubyte[] x, uint offset) { - return cast(uint) (((x[offset++] & 0xff) << 24) | - ((x[offset++] & 0xff) << 16) | - ((x[offset++] & 0xff) << 8) | - (x[offset] & 0xff)); - } - - final static uint ubytesToUintLittle(ubyte[] x, uint offset) { - return cast(uint) ((x[offset++] & 0xff) | - ((x[offset++] & 0xff) << 8) | - ((x[offset++] & 0xff) << 16) | - ((x[offset] & 0xff) << 24)); - } - - final static ulong ubytesToUlongLittle(ubyte[] x, uint offset) { - return cast(ulong) ((x[offset++] & 0xff) | - ((x[offset++] & 0xff) << 8) | - ((x[offset++] & 0xff) << 16) | - ((x[offset++] & 0xff) << 24) | - (cast(ulong)(x[offset++] & 0xff) << 32) | - (cast(ulong)(x[offset++] & 0xff) << 40) | - (cast(ulong)(x[offset++] & 0xff) << 48) | - (cast(ulong)(x[offset] & 0xff) << 56)); - } - - final static ulong ubytesToUlongBig(ubyte[] x, uint offset) { - return cast(ulong) ((cast(ulong)(x[offset++] & 0xff) << 56) | - (cast(ulong)(x[offset++] & 0xff) << 48) | - (cast(ulong)(x[offset++] & 0xff) << 40) | - (cast(ulong)(x[offset++] & 0xff) << 32) | - ((x[offset++] & 0xff) << 24) | - ((x[offset++] & 0xff) << 16) | - ((x[offset++] & 0xff) << 8) | - (x[offset] & 0xff)); - } - - final static ushort ubytesToUshortLittle(ubyte[] x, uint offset) { - return cast(ushort) ((x[offset++] & 0xff) | - ((x[offset] & 0xff) << 8)); - } - - final static ushort ubytesToUshortBig(ubyte[] x, uint offset) { - return cast(ushort) (((x[offset++] & 0xff) << 8) | - (x[offset] & 0xff)); - } - - final static void uintToUbytesBig(uint x, ubyte[] output, uint outOff) { - output[outOff++] = cast(ubyte)(x >> 24); - output[outOff++] = cast(ubyte)(x >> 16); - output[outOff++] = cast(ubyte)(x >> 8); - output[outOff] = cast(ubyte)(x); - } - - final static void uintToUbytesLittle(uint x, ubyte[] output, uint outOff) { - output[outOff++] = cast(ubyte)(x); - output[outOff++] = cast(ubyte)(x >> 8); - output[outOff++] = cast(ubyte)(x >> 16); - output[outOff] = cast(ubyte)(x >> 24); - } - - final static void ulongToUbytesBig(ulong x, ubyte[] output, uint outOff) { - output[outOff++] = cast(ubyte)(x >> 56); - output[outOff++] = cast(ubyte)(x >> 48); - output[outOff++] = cast(ubyte)(x >> 40); - output[outOff++] = cast(ubyte)(x >> 32); - output[outOff++] = cast(ubyte)(x >> 24); - output[outOff++] = cast(ubyte)(x >> 16); - output[outOff++] = cast(ubyte)(x >> 8); - output[outOff] = cast(ubyte)(x); - } - - final static void ulongToUbytesLittle(ulong x, ubyte[] output, uint outOff) { - output[outOff++] = cast(ubyte)(x); - output[outOff++] = cast(ubyte)(x >> 8); - output[outOff++] = cast(ubyte)(x >> 16); - output[outOff++] = cast(ubyte)(x >> 24); - output[outOff++] = cast(ubyte)(x >> 32); - output[outOff++] = cast(ubyte)(x >> 40); - output[outOff++] = cast(ubyte)(x >> 48); - output[outOff] = cast(ubyte)(x >> 56); - } - - final static uint rotateLeft(uint x, uint y) { - return (x << y) | (x >> (32-y)); - } - - final static ulong rotateLeft64(ulong x, uint y) { - return (x << y) | (x >> (64-y)); - } - - final static uint rotateRight(uint x, uint y) { - return (x >> y) | (x << (32-y)); - } - - final static ulong rotateRight64(ulong x, uint y) { - return (x >> y) | (x << (64-y)); - } - - final static char[] ubytesToHex(ubyte[] input) { - char[] output = new char[input.length<<1]; - int i = 0; - foreach (ubyte j; input) { - output[i++] = hexits[j >> 4]; - output[i++] = hexits[j & 0xf]; - } - return output; - } - - final static ubyte[] hexToUbytes(char[] input) { - ubyte[] output = new ubyte[input.length>>1]; - static ubyte[char] hexitIndex; - for (int i = 0; i < hexits.length; i++) - hexitIndex[hexits[i]] = i; - for (int i = 0, j = 0; i < output.length; i++) { - output[i] = hexitIndex[input[j++]] << 4; - output[i] |= hexitIndex[input[j++]]; - } - return output; - } - - final static char[] stringToLower(char[] input) { - char[] output = new char[input.length]; - foreach (uint i, char c; input) - output[i] = ((c >= 'A' && c <= 'Z') ? c+32 : c); - return output; - } - - final static char[] stringToUpper(char[] input) { - char[] output = new char[input.length]; - foreach (uint i, char c; input) - output[i] = ((c >= 'a' && c <= 'z') ? c-32 : c); - return output; - } - - final static char[] stringToAlphanumeric(char[] input) { - char[] output = ""; - foreach (char c; input) - if ((c >= 'a' && c <= 'z') || - (c >= 'A' && c <= 'Z') || - (c >= '0' && c <= '9')) - output ~= c; - return output; - } -} diff -r 74303a717032 -r 4589f8c5eb3c dsss.conf --- a/dsss.conf Sat Jan 10 13:17:58 2009 -0500 +++ b/dsss.conf Sat Feb 14 19:58:20 2009 -0500 @@ -2,3 +2,4 @@ [dcrypt] buildflags = -O target = dcrypt +