comparison dcrypt/crypto/ciphers/XTEA.d @ 14:5ce3012f1def

Removed some redundancy in code. Added NotSupportedError, a base PRNG class and a class which creates a PRNG from a hash function. Changed the MAC class' finalization methods to digest and hexDigest instead of finish and hexFinish respectively. Also added a base Checksum class, crc32 and adler32 in dcrypt.misc as per request.
author Thomas Dixon <reikon@reikon.us>
date Tue, 18 Nov 2008 18:03:40 -0500
parents 8c7f8fecdd75
children 4589f8c5eb3c
comparison
equal deleted inserted replaced
13:7ea528b61802 14:5ce3012f1def
20 BLOCK_SIZE = 8, 20 BLOCK_SIZE = 8,
21 DELTA = 0x9e3779b9; 21 DELTA = 0x9e3779b9;
22 uint[] subkeys, 22 uint[] subkeys,
23 sum0, 23 sum0,
24 sum1; 24 sum1;
25 bool initialized,
26 encrypt;
27 } 25 }
28 26
29 void reset(){} 27 void reset(){}
30 28
31 char[] name() { 29 char[] name() {
39 void init(bool encrypt, CipherParameters params) { 37 void init(bool encrypt, CipherParameters params) {
40 SymmetricKey keyParams = cast(SymmetricKey)params; 38 SymmetricKey keyParams = cast(SymmetricKey)params;
41 if (!keyParams) 39 if (!keyParams)
42 throw new InvalidParameterError( 40 throw new InvalidParameterError(
43 name()~": Invalid parameter passed to init"); 41 name()~": Invalid parameter passed to init");
44 this.encrypt = encrypt; 42 _encrypt = encrypt;
45 43
46 if (keyParams.key.length != KEY_SIZE) 44 if (keyParams.key.length != KEY_SIZE)
47 throw new InvalidKeyError( 45 throw new InvalidKeyError(
48 name()~": Invalid key length (requires 16 bytes)"); 46 name()~": Invalid key length (requires 16 bytes)");
49 47
59 for (i = j = 0; i < ROUNDS; i++) { 57 for (i = j = 0; i < ROUNDS; i++) {
60 sum0[i] = (j + subkeys[j & 3]); 58 sum0[i] = (j + subkeys[j & 3]);
61 j += DELTA; 59 j += DELTA;
62 sum1[i] = (j + subkeys[j >> 11 & 3]); 60 sum1[i] = (j + subkeys[j >> 11 & 3]);
63 } 61 }
64 initialized = true; 62 _initialized = true;
65 } 63 }
66 64
67 uint update(void[] input_, void[] output_) { 65 uint update(void[] input_, void[] output_) {
68 if (!initialized) 66 if (!_initialized)
69 throw new NotInitializedError(name()~": Cipher not initialized"); 67 throw new NotInitializedError(name()~": Cipher not initialized");
70 68
71 ubyte[] input = cast(ubyte[]) input_, 69 ubyte[] input = cast(ubyte[]) input_,
72 output = cast(ubyte[]) output_; 70 output = cast(ubyte[]) output_;
73 71
78 throw new ShortBufferError(name()~": Output buffer too short"); 76 throw new ShortBufferError(name()~": Output buffer too short");
79 77
80 uint v0 = Util.ubytesToUintBig(input, 0), 78 uint v0 = Util.ubytesToUintBig(input, 0),
81 v1 = Util.ubytesToUintBig(input, 4); 79 v1 = Util.ubytesToUintBig(input, 4);
82 80
83 if (encrypt) { 81 if (_encrypt) {
84 for (int i = 0; i < ROUNDS; i++) { 82 for (int i = 0; i < ROUNDS; i++) {
85 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ sum0[i]; 83 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ sum0[i];
86 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ sum1[i]; 84 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ sum1[i];
87 } 85 }
88 } else { 86 } else {