Mercurial > projects > dcrypt
comparison dcrypt/crypto/ciphers/TEA.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 |
---|---|
19 KEY_SIZE = 16, | 19 KEY_SIZE = 16, |
20 BLOCK_SIZE = 8, | 20 BLOCK_SIZE = 8, |
21 DELTA = 0x9e3779b9, | 21 DELTA = 0x9e3779b9, |
22 DECRYPT_SUM = 0xc6ef3720; | 22 DECRYPT_SUM = 0xc6ef3720; |
23 uint sk0, sk1, sk2, sk3, sum; | 23 uint sk0, sk1, sk2, sk3, sum; |
24 bool initialized, | |
25 encrypt; | |
26 } | 24 } |
27 | 25 |
28 void reset(){} | 26 void reset(){} |
29 | 27 |
30 char[] name() { | 28 char[] name() { |
38 void init(bool encrypt, CipherParameters params) { | 36 void init(bool encrypt, CipherParameters params) { |
39 SymmetricKey keyParams = cast(SymmetricKey)params; | 37 SymmetricKey keyParams = cast(SymmetricKey)params; |
40 if (!keyParams) | 38 if (!keyParams) |
41 throw new InvalidParameterError( | 39 throw new InvalidParameterError( |
42 name()~": Invalid parameter passed to init"); | 40 name()~": Invalid parameter passed to init"); |
43 this.encrypt = encrypt; | 41 _encrypt = encrypt; |
44 | 42 |
45 if (keyParams.key.length != KEY_SIZE) | 43 if (keyParams.key.length != KEY_SIZE) |
46 throw new InvalidKeyError( | 44 throw new InvalidKeyError( |
47 name()~": Invalid key length (requires 16 bytes)"); | 45 name()~": Invalid key length (requires 16 bytes)"); |
48 | 46 |
49 sk0 = Util.ubytesToUintBig(keyParams.key, 0); | 47 sk0 = Util.ubytesToUintBig(keyParams.key, 0); |
50 sk1 = Util.ubytesToUintBig(keyParams.key, 4); | 48 sk1 = Util.ubytesToUintBig(keyParams.key, 4); |
51 sk2 = Util.ubytesToUintBig(keyParams.key, 8); | 49 sk2 = Util.ubytesToUintBig(keyParams.key, 8); |
52 sk3 = Util.ubytesToUintBig(keyParams.key, 12); | 50 sk3 = Util.ubytesToUintBig(keyParams.key, 12); |
53 | 51 |
54 initialized = true; | 52 _initialized = true; |
55 } | 53 } |
56 | 54 |
57 uint update(void[] input_, void[] output_) { | 55 uint update(void[] input_, void[] output_) { |
58 if (!initialized) | 56 if (!_initialized) |
59 throw new NotInitializedError(name()~": Cipher not initialized"); | 57 throw new NotInitializedError(name()~": Cipher not initialized"); |
60 | 58 |
61 ubyte[] input = cast(ubyte[]) input_, | 59 ubyte[] input = cast(ubyte[]) input_, |
62 output = cast(ubyte[]) output_; | 60 output = cast(ubyte[]) output_; |
63 | 61 |
68 throw new ShortBufferError(name()~": Output buffer too short"); | 66 throw new ShortBufferError(name()~": Output buffer too short"); |
69 | 67 |
70 uint v0 = Util.ubytesToUintBig(input, 0), | 68 uint v0 = Util.ubytesToUintBig(input, 0), |
71 v1 = Util.ubytesToUintBig(input, 4); | 69 v1 = Util.ubytesToUintBig(input, 4); |
72 | 70 |
73 sum = encrypt ? 0 : DECRYPT_SUM; | 71 sum = _encrypt ? 0 : DECRYPT_SUM; |
74 for (int i = 0; i < ROUNDS; i++) { | 72 for (int i = 0; i < ROUNDS; i++) { |
75 if (encrypt) { | 73 if (_encrypt) { |
76 sum += DELTA; | 74 sum += DELTA; |
77 v0 += ((v1 << 4) + sk0) ^ (v1 + sum) ^ ((v1 >> 5) + sk1); | 75 v0 += ((v1 << 4) + sk0) ^ (v1 + sum) ^ ((v1 >> 5) + sk1); |
78 v1 += ((v0 << 4) + sk2) ^ (v0 + sum) ^ ((v0 >> 5) + sk3); | 76 v1 += ((v0 << 4) + sk2) ^ (v0 + sum) ^ ((v0 >> 5) + sk3); |
79 } else { | 77 } else { |
80 v1 -= ((v0 << 4) + sk2) ^ (v0 + sum) ^ ((v0 >> 5) + sk3); | 78 v1 -= ((v0 << 4) + sk2) ^ (v0 + sum) ^ ((v0 >> 5) + sk3); |