comparison dcrypt/crypto/ciphers/Blowfish.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
253 + S1[cast(ubyte)(x >> 16)]) 253 + S1[cast(ubyte)(x >> 16)])
254 ^ S2[cast(ubyte)(x >> 8)]) 254 ^ S2[cast(ubyte)(x >> 8)])
255 + S3[cast(ubyte)x]); 255 + S3[cast(ubyte)x]);
256 } 256 }
257 257
258 ubyte[] process(void[] input_) { 258 uint update(void[] input_, void[] output_) {
259 if (!initialized) 259 if (!initialized)
260 throw new NotInitializedError(name()~": Cipher not initialized."); 260 throw new NotInitializedError(name()~": Cipher not initialized.");
261 261
262 ubyte[] input = cast(ubyte[]) input_; 262 ubyte[] input = cast(ubyte[]) input_,
263 output = cast(ubyte[]) output_;
263 264
264 if (input.length < blockSize) 265 if (input.length < BLOCK_SIZE)
265 throw new ShortBufferError(name()~": Input buffer too short"); 266 throw new ShortBufferError(name()~": Input buffer too short");
267
268 if (output.length < BLOCK_SIZE)
269 throw new ShortBufferError(name()~": Output buffer too short");
266 270
267 uint xl = Util.ubytesToUintBig(input, 0), 271 uint xl = Util.ubytesToUintBig(input, 0),
268 xr = Util.ubytesToUintBig(input, 4), 272 xr = Util.ubytesToUintBig(input, 4),
269 i = 0; 273 i = 0;
270 274
273 xr ^= F(xl) ^ P[i++]; 277 xr ^= F(xl) ^ P[i++];
274 xl ^= F(xr) ^ P[i++]; 278 xl ^= F(xr) ^ P[i++];
275 } 279 }
276 xr ^= P[i]; 280 xr ^= P[i];
277 281
278 ubyte[] output = new ubyte[blockSize];
279 Util.uintToUbytesBig(xr, output, 0); 282 Util.uintToUbytesBig(xr, output, 0);
280 Util.uintToUbytesBig(xl, output, 4); 283 Util.uintToUbytesBig(xl, output, 4);
281 284
282 return output; 285 return BLOCK_SIZE;
283 } 286 }
284 287
285 void reset() { 288 void reset() {
286 setup(workingKey); 289 setup(workingKey);
287 } 290 }
299 P[i] ^= x; 302 P[i] ^= x;
300 } 303 }
301 304
302 ubyte[] t = new ubyte[BLOCK_SIZE]; // Initialized to 0's 305 ubyte[] t = new ubyte[BLOCK_SIZE]; // Initialized to 0's
303 for (int i = 0; i < PBOX_SIZE;) { 306 for (int i = 0; i < PBOX_SIZE;) {
304 t = process(t); 307 update(t, t);
305 P[i++] = Util.ubytesToUintBig(t, 0); 308 P[i++] = Util.ubytesToUintBig(t, 0);
306 P[i++] = Util.ubytesToUintBig(t, 4); 309 P[i++] = Util.ubytesToUintBig(t, 4);
307 } 310 }
308 311
309 for (int i = 0; i < SBOX_SIZE;) { 312 for (int i = 0; i < SBOX_SIZE;) {
310 t = process(t); 313 update(t, t);
311 S0[i++] = Util.ubytesToUintBig(t, 0); 314 S0[i++] = Util.ubytesToUintBig(t, 0);
312 S0[i++] = Util.ubytesToUintBig(t, 4); 315 S0[i++] = Util.ubytesToUintBig(t, 4);
313 } 316 }
314 317
315 for (int i = 0; i < SBOX_SIZE;) { 318 for (int i = 0; i < SBOX_SIZE;) {
316 t = process(t); 319 update(t, t);
317 S1[i++] = Util.ubytesToUintBig(t, 0); 320 S1[i++] = Util.ubytesToUintBig(t, 0);
318 S1[i++] = Util.ubytesToUintBig(t, 4); 321 S1[i++] = Util.ubytesToUintBig(t, 4);
319 } 322 }
320 323
321 for (int i = 0; i < SBOX_SIZE;) { 324 for (int i = 0; i < SBOX_SIZE;) {
322 t = process(t); 325 update(t, t);
323 S2[i++] = Util.ubytesToUintBig(t, 0); 326 S2[i++] = Util.ubytesToUintBig(t, 0);
324 S2[i++] = Util.ubytesToUintBig(t, 4); 327 S2[i++] = Util.ubytesToUintBig(t, 4);
325 } 328 }
326 329
327 for (int i = 0; i < SBOX_SIZE;) { 330 for (int i = 0; i < SBOX_SIZE;) {
328 t = process(t); 331 update(t, t);
329 S3[i++] = Util.ubytesToUintBig(t, 0); 332 S3[i++] = Util.ubytesToUintBig(t, 0);
330 S3[i++] = Util.ubytesToUintBig(t, 4); 333 S3[i++] = Util.ubytesToUintBig(t, 4);
331 } 334 }
332 } 335 }
333 336
373 char[] result; 376 char[] result;
374 SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key)); 377 SymmetricKey key = new SymmetricKey(Util.hexToUbytes(test_key));
375 378
376 // Encryption 379 // Encryption
377 t.init(true, key); 380 t.init(true, key);
378 buffer = t.process(Util.hexToUbytes(test_plaintexts[i])); 381 t.update(Util.hexToUbytes(test_plaintexts[i]), buffer);
379 result = Util.ubytesToHex(buffer); 382 result = Util.ubytesToHex(buffer);
380 assert(result == test_ciphertexts[i], 383 assert(result == test_ciphertexts[i],
381 t.name~": ("~result~") != ("~test_ciphertexts[i]~")"); 384 t.name~": ("~result~") != ("~test_ciphertexts[i]~")");
382 385
383 // Decryption 386 // Decryption
384 t.init(false, key); 387 t.init(false, key);
385 buffer = t.process(Util.hexToUbytes(test_ciphertexts[i])); 388 t.update(Util.hexToUbytes(test_ciphertexts[i]), buffer);
386 result = Util.ubytesToHex(buffer); 389 result = Util.ubytesToHex(buffer);
387 assert(result == test_plaintexts[i], 390 assert(result == test_plaintexts[i],
388 t.name~": ("~result~") != ("~test_plaintexts[i]~")"); 391 t.name~": ("~result~") != ("~test_plaintexts[i]~")");
389 } 392 }
390 } 393 }