comparison dcrypt/crypto/ciphers/RC6.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 ec23779ee794
comparison
equal deleted inserted replaced
13:7ea528b61802 14:5ce3012f1def
31 // Magic constants for a 32 bit word size 31 // Magic constants for a 32 bit word size
32 P = 0xb7e15163, 32 P = 0xb7e15163,
33 Q = 0x9e3779b9; 33 Q = 0x9e3779b9;
34 uint[] S; 34 uint[] S;
35 ubyte[] workingKey; 35 ubyte[] workingKey;
36 bool initialized,
37 encrypt;
38 } 36 }
39 37
40 char[] name() { 38 char[] name() {
41 return "RC6"; 39 return "RC6";
42 } 40 }
48 void init(bool encrypt, CipherParameters params) { 46 void init(bool encrypt, CipherParameters params) {
49 SymmetricKey keyParams = cast(SymmetricKey)params; 47 SymmetricKey keyParams = cast(SymmetricKey)params;
50 if (!keyParams) 48 if (!keyParams)
51 throw new InvalidParameterError( 49 throw new InvalidParameterError(
52 name()~": Invalid parameter passed to init"); 50 name()~": Invalid parameter passed to init");
53 this.encrypt = encrypt; 51 _encrypt = encrypt;
54 52
55 uint len = keyParams.key.length; 53 uint len = keyParams.key.length;
56 if (len != 16 && len != 24 && len != 32) 54 if (len != 16 && len != 24 && len != 32)
57 throw new InvalidKeyError( 55 throw new InvalidKeyError(
58 name()~": Invalid key length (requires 16/24/32 bytes)"); 56 name()~": Invalid key length (requires 16/24/32 bytes)");
60 S = new uint[2*ROUNDS+4]; 58 S = new uint[2*ROUNDS+4];
61 59
62 workingKey = keyParams.key; 60 workingKey = keyParams.key;
63 setup(workingKey); 61 setup(workingKey);
64 62
65 initialized = true; 63 _initialized = true;
66 } 64 }
67 65
68 uint update(void[] input_, void[] output_) { 66 uint update(void[] input_, void[] output_) {
69 if (!initialized) 67 if (!_initialized)
70 throw new NotInitializedError(name()~": Cipher not initialized"); 68 throw new NotInitializedError(name()~": Cipher not initialized");
71 69
72 ubyte[] input = cast(ubyte[]) input_, 70 ubyte[] input = cast(ubyte[]) input_,
73 output = cast(ubyte[]) output_; 71 output = cast(ubyte[]) output_;
74 72
83 C = Util.ubytesToUintLittle(input, 8), 81 C = Util.ubytesToUintLittle(input, 8),
84 D = Util.ubytesToUintLittle(input, 12), 82 D = Util.ubytesToUintLittle(input, 12),
85 t, 83 t,
86 u; 84 u;
87 85
88 if (encrypt) { 86 if (_encrypt) {
89 B += S[0]; 87 B += S[0];
90 D += S[1]; 88 D += S[1];
91 for (int i = 1; i <= ROUNDS; i++) { 89 for (int i = 1; i <= ROUNDS; i++) {
92 t = Util.rotateLeft(B*((B<<1)+1), 5); 90 t = Util.rotateLeft(B*((B<<1)+1), 5);
93 u = Util.rotateLeft(D*((D<<1)+1), 5); 91 u = Util.rotateLeft(D*((D<<1)+1), 5);