comparison dcrypt/crypto/ciphers/XTEA.d @ 8:23c62e28b3a4

Reworked symmetric cipher classes to have SymmetricCipher as their superclass, and follow the general interface of init(), process(), etc. Made sure everything still passed test vectors. Removed Cipher class. I'll worry about that shit when we support something other than symmetric ciphers.
author Thomas Dixon <reikon@reikon.us>
date Mon, 18 Aug 2008 01:14:37 -0400
parents 5cb17e09d685
children 8c7f8fecdd75
comparison
equal deleted inserted replaced
7:23e6e80f8ee3 8:23c62e28b3a4
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 uint processBlock(void[] input_, uint inOff, void[] output_, uint outOff) { 67 ubyte[] process(void[] input_) {
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 ubyte[] output = cast(ubyte[]) output_;
73 72
74 if ((inOff + BLOCK_SIZE) > input.length) 73 if (input.length < blockSize)
75 throw new ShortBufferError(name()~": Input buffer too short"); 74 throw new ShortBufferError(name()~": Input buffer too short");
76
77 if ((outOff + BLOCK_SIZE) > output.length)
78 throw new ShortBufferError(name()~": Output buffer too short");
79 75
80 uint v0 = Util.ubytesToUintBig(input, inOff), 76 uint v0 = Util.ubytesToUintBig(input, 0),
81 v1 = Util.ubytesToUintBig(input, inOff+4); 77 v1 = Util.ubytesToUintBig(input, 4);
82 78
83 if (encrypt) { 79 if (encrypt) {
84 for (int i = 0; i < ROUNDS; i++) { 80 for (int i = 0; i < ROUNDS; i++) {
85 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ sum0[i]; 81 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ sum0[i];
86 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ sum1[i]; 82 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ sum1[i];
90 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ sum1[i]; 86 v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ sum1[i];
91 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ sum0[i]; 87 v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ sum0[i];
92 } 88 }
93 } 89 }
94 90
95 Util.uintToUbytesBig(v0, output, outOff); 91 ubyte[] output = new ubyte[blockSize];
96 Util.uintToUbytesBig(v1, output, outOff+4); 92 Util.uintToUbytesBig(v0, output, 0);
93 Util.uintToUbytesBig(v1, output, 4);
97 94
98 return BLOCK_SIZE; 95 return output;
99 } 96 }
100 97
101 /** Some XTEA test vectors. */ 98 /** Some XTEA test vectors. */
102 version (UnitTest) { 99 version (UnitTest) {
103 unittest { 100 unittest {
146 char[] result; 143 char[] result;
147 SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); 144 SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key));
148 145
149 // Encryption 146 // Encryption
150 t.init(true, key); 147 t.init(true, key);
151 t.processBlock(Util.hexToUbytes(test_plaintexts[i]), 0, buffer, 0); 148 buffer = t.process(Util.hexToUbytes(test_plaintexts[i]));
152 result = Util.ubytesToHex(buffer); 149 result = Util.ubytesToHex(buffer);
153 assert(result == test_ciphertexts[i], 150 assert(result == test_ciphertexts[i],
154 t.name~": ("~result~") != ("~test_ciphertexts[i]~")"); 151 t.name~": ("~result~") != ("~test_ciphertexts[i]~")");
155 152
156 // Decryption 153 // Decryption
157 t.init(false, key); 154 t.init(false, key);
158 t.processBlock(Util.hexToUbytes(test_ciphertexts[i]), 0, buffer, 0); 155 buffer = t.process(Util.hexToUbytes(test_ciphertexts[i]));
159 result = Util.ubytesToHex(buffer); 156 result = Util.ubytesToHex(buffer);
160 assert(result == test_plaintexts[i], 157 assert(result == test_plaintexts[i],
161 t.name~": ("~result~") != ("~test_plaintexts[i]~")"); 158 t.name~": ("~result~") != ("~test_plaintexts[i]~")");
162 } 159 }
163 } 160 }