Mercurial > projects > dcrypt
comparison dcrypt/crypto/ciphers/XTEA.d @ 12:8c7f8fecdd75
Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Sat, 30 Aug 2008 14:38:23 -0400 |
parents | 23c62e28b3a4 |
children | 5ce3012f1def |
comparison
equal
deleted
inserted
replaced
11:02970e63257d | 12:8c7f8fecdd75 |
---|---|
62 sum1[i] = (j + subkeys[j >> 11 & 3]); | 62 sum1[i] = (j + subkeys[j >> 11 & 3]); |
63 } | 63 } |
64 initialized = true; | 64 initialized = true; |
65 } | 65 } |
66 | 66 |
67 ubyte[] process(void[] input_) { | 67 uint update(void[] input_, void[] output_) { |
68 if (!initialized) | 68 if (!initialized) |
69 throw new NotInitializedError(name()~": Cipher not initialized"); | 69 throw new NotInitializedError(name()~": Cipher not initialized"); |
70 | 70 |
71 ubyte[] input = cast(ubyte[]) input_; | 71 ubyte[] input = cast(ubyte[]) input_, |
72 output = cast(ubyte[]) output_; | |
72 | 73 |
73 if (input.length < blockSize) | 74 if (input.length < BLOCK_SIZE) |
74 throw new ShortBufferError(name()~": Input buffer too short"); | 75 throw new ShortBufferError(name()~": Input buffer too short"); |
76 | |
77 if (output.length < BLOCK_SIZE) | |
78 throw new ShortBufferError(name()~": Output buffer too short"); | |
75 | 79 |
76 uint v0 = Util.ubytesToUintBig(input, 0), | 80 uint v0 = Util.ubytesToUintBig(input, 0), |
77 v1 = Util.ubytesToUintBig(input, 4); | 81 v1 = Util.ubytesToUintBig(input, 4); |
78 | 82 |
79 if (encrypt) { | 83 if (encrypt) { |
86 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ sum1[i]; | 90 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ sum1[i]; |
87 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ sum0[i]; | 91 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ sum0[i]; |
88 } | 92 } |
89 } | 93 } |
90 | 94 |
91 ubyte[] output = new ubyte[blockSize]; | |
92 Util.uintToUbytesBig(v0, output, 0); | 95 Util.uintToUbytesBig(v0, output, 0); |
93 Util.uintToUbytesBig(v1, output, 4); | 96 Util.uintToUbytesBig(v1, output, 4); |
94 | 97 |
95 return output; | 98 return BLOCK_SIZE; |
96 } | 99 } |
97 | 100 |
98 /** Some XTEA test vectors. */ | 101 /** Some XTEA test vectors. */ |
99 version (UnitTest) { | 102 version (UnitTest) { |
100 unittest { | 103 unittest { |
143 char[] result; | 146 char[] result; |
144 SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); | 147 SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); |
145 | 148 |
146 // Encryption | 149 // Encryption |
147 t.init(true, key); | 150 t.init(true, key); |
148 buffer = t.process(Util.hexToUbytes(test_plaintexts[i])); | 151 t.update(Util.hexToUbytes(test_plaintexts[i]), buffer); |
149 result = Util.ubytesToHex(buffer); | 152 result = Util.ubytesToHex(buffer); |
150 assert(result == test_ciphertexts[i], | 153 assert(result == test_ciphertexts[i], |
151 t.name~": ("~result~") != ("~test_ciphertexts[i]~")"); | 154 t.name~": ("~result~") != ("~test_ciphertexts[i]~")"); |
152 | 155 |
153 // Decryption | 156 // Decryption |
154 t.init(false, key); | 157 t.init(false, key); |
155 buffer = t.process(Util.hexToUbytes(test_ciphertexts[i])); | 158 t.update(Util.hexToUbytes(test_ciphertexts[i]), buffer); |
156 result = Util.ubytesToHex(buffer); | 159 result = Util.ubytesToHex(buffer); |
157 assert(result == test_plaintexts[i], | 160 assert(result == test_plaintexts[i], |
158 t.name~": ("~result~") != ("~test_plaintexts[i]~")"); | 161 t.name~": ("~result~") != ("~test_plaintexts[i]~")"); |
159 } | 162 } |
160 } | 163 } |