Mercurial > projects > dcrypt
comparison dcrypt/crypto/ciphers/RC6.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 |
---|---|
63 setup(workingKey); | 63 setup(workingKey); |
64 | 64 |
65 initialized = true; | 65 initialized = true; |
66 } | 66 } |
67 | 67 |
68 uint processBlock(void[] input_, uint inOff, void[] output_, uint outOff) { | 68 ubyte[] process(void[] input_) { |
69 if (!initialized) | 69 if (!initialized) |
70 throw new NotInitializedError(name()~": Cipher not initialized"); | 70 throw new NotInitializedError(name()~": Cipher not initialized"); |
71 | 71 |
72 ubyte[] input = cast(ubyte[]) input_; | 72 ubyte[] input = cast(ubyte[]) input_; |
73 ubyte[] output = cast(ubyte[]) output_; | |
74 | 73 |
75 if ((inOff + BLOCK_SIZE) > input.length) | 74 if (input.length > blockSize) |
76 throw new ShortBufferError(name()~": Input buffer too short"); | 75 throw new ShortBufferError(name()~": Input buffer too short"); |
77 | 76 |
78 if ((outOff + BLOCK_SIZE) > output.length) | 77 uint A = Util.ubytesToUintLittle(input, 0), |
79 throw new ShortBufferError(name()~": Output buffer too short"); | 78 B = Util.ubytesToUintLittle(input, 4), |
80 | 79 C = Util.ubytesToUintLittle(input, 8), |
81 uint A = Util.ubytesToUintLittle(input, inOff), | 80 D = Util.ubytesToUintLittle(input, 12), |
82 B = Util.ubytesToUintLittle(input, inOff+4), | |
83 C = Util.ubytesToUintLittle(input, inOff+8), | |
84 D = Util.ubytesToUintLittle(input, inOff+12), | |
85 t, | 81 t, |
86 u; | 82 u; |
87 | 83 |
88 if (encrypt) { | 84 if (encrypt) { |
89 B += S[0]; | 85 B += S[0]; |
117 } | 113 } |
118 D -= S[1]; | 114 D -= S[1]; |
119 B -= S[0]; | 115 B -= S[0]; |
120 } | 116 } |
121 | 117 |
122 Util.uintToUbytesLittle(A, output, outOff); | 118 ubyte[] output = new ubyte[blockSize]; |
123 Util.uintToUbytesLittle(B, output, outOff+4); | 119 Util.uintToUbytesLittle(A, output, 0); |
124 Util.uintToUbytesLittle(C, output, outOff+8); | 120 Util.uintToUbytesLittle(B, output, 4); |
125 Util.uintToUbytesLittle(D, output, outOff+12); | 121 Util.uintToUbytesLittle(C, output, 8); |
126 | 122 Util.uintToUbytesLittle(D, output, 12); |
127 return BLOCK_SIZE; | 123 |
124 return output; | |
128 } | 125 } |
129 | 126 |
130 void reset() { | 127 void reset() { |
131 setup(workingKey); | 128 setup(workingKey); |
132 } | 129 } |
193 char[] result; | 190 char[] result; |
194 SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); | 191 SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); |
195 | 192 |
196 // Encryption | 193 // Encryption |
197 t.init(true, key); | 194 t.init(true, key); |
198 t.processBlock(Util.hexToUbytes(test_plaintexts[i]), 0, buffer, 0); | 195 buffer = t.process(Util.hexToUbytes(test_plaintexts[i])); |
199 result = Util.ubytesToHex(buffer); | 196 result = Util.ubytesToHex(buffer); |
200 assert(result == test_ciphertexts[i], | 197 assert(result == test_ciphertexts[i], |
201 t.name~": ("~result~") != ("~test_ciphertexts[i]~")"); | 198 t.name~": ("~result~") != ("~test_ciphertexts[i]~")"); |
202 | 199 |
203 // Decryption | 200 // Decryption |
204 t.init(false, key); | 201 t.init(false, key); |
205 t.processBlock(Util.hexToUbytes(test_ciphertexts[i]), 0, buffer, 0); | 202 buffer = t.process(Util.hexToUbytes(test_ciphertexts[i])); |
206 result = Util.ubytesToHex(buffer); | 203 result = Util.ubytesToHex(buffer); |
207 assert(result == test_plaintexts[i], | 204 assert(result == test_plaintexts[i], |
208 t.name~": ("~result~") != ("~test_plaintexts[i]~")"); | 205 t.name~": ("~result~") != ("~test_plaintexts[i]~")"); |
209 } | 206 } |
210 } | 207 } |