Mercurial > projects > dcrypt
annotate dcrypt/crypto/ciphers/RC6.d @ 36:fc97fffd106d default tip
Added tag 0.1 for changeset 6b2c35b84186
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Thu, 14 May 2009 17:46:46 -0400 |
parents | ad687db713a4 |
children |
rev | line source |
---|---|
0 | 1 /** |
2 * This file is part of the dcrypt project. | |
6
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
3 * |
0 | 4 * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved. |
5 * License: MIT | |
6 * Authors: Thomas Dixon | |
7 */ | |
8 | |
9 module dcrypt.crypto.ciphers.RC6; | |
10 | |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
11 import dcrypt.misc.Bitwise; |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
12 import dcrypt.misc.ByteConverter; |
0 | 13 import dcrypt.crypto.BlockCipher; |
14 | |
15 /** | |
16 * Implementation of the RC6-32/20/b cipher designed by | |
17 * Ron Rivest et al. of RSA Security. | |
6
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
18 * |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
19 * It should be noted that this algorithm is very similar to RC5. |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
20 * Currently there are no plans to implement RC5, but should that change |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
21 * in the future, it may be wise to rewrite both RC5 and RC6 to use some |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
22 * kind of template or base class. |
0 | 23 * |
6
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
24 * This algorithm is patented and trademarked. |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
25 * |
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
26 * References: http://people.csail.mit.edu/rivest/Rc6.pdf |
0 | 27 */ |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
28 class RC6 : BlockCipher |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
29 { |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
30 private |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
31 { |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
32 static const uint ROUNDS = 20, |
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
33 BLOCK_SIZE = 16, |
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
34 // Magic constants for a 32 bit word size |
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
35 P = 0xb7e15163, |
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
36 Q = 0x9e3779b9; |
1
483e4467b5f6
Added Blowfish with test vectors. Minor cleanup of other cipher classes (should probably clean more). Continued work on high-level cipher API (didn't get very far).
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
37 uint[] S; |
483e4467b5f6
Added Blowfish with test vectors. Minor cleanup of other cipher classes (should probably clean more). Continued work on high-level cipher API (didn't get very far).
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
38 ubyte[] workingKey; |
483e4467b5f6
Added Blowfish with test vectors. Minor cleanup of other cipher classes (should probably clean more). Continued work on high-level cipher API (didn't get very far).
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
39 } |
0 | 40 |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
41 string name() |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
42 { |
0 | 43 return "RC6"; |
44 } | |
45 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
46 uint blockSize() |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
47 { |
0 | 48 return BLOCK_SIZE; |
49 } | |
50 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
51 void init(bool encrypt, CipherParameters params) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
52 { |
0 | 53 SymmetricKey keyParams = cast(SymmetricKey)params; |
54 if (!keyParams) | |
55 throw new InvalidParameterError( | |
56 name()~": Invalid parameter passed to init"); | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
57 |
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.
Thomas Dixon <reikon@reikon.us>
parents:
12
diff
changeset
|
58 _encrypt = encrypt; |
0 | 59 |
60 uint len = keyParams.key.length; | |
61 if (len != 16 && len != 24 && len != 32) | |
62 throw new InvalidKeyError( | |
63 name()~": Invalid key length (requires 16/24/32 bytes)"); | |
64 | |
65 S = new uint[2*ROUNDS+4]; | |
66 | |
67 workingKey = keyParams.key; | |
68 setup(workingKey); | |
69 | |
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.
Thomas Dixon <reikon@reikon.us>
parents:
12
diff
changeset
|
70 _initialized = true; |
0 | 71 } |
72 | |
12
8c7f8fecdd75
Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
Thomas Dixon <reikon@reikon.us>
parents:
8
diff
changeset
|
73 uint update(void[] input_, void[] output_) { |
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.
Thomas Dixon <reikon@reikon.us>
parents:
12
diff
changeset
|
74 if (!_initialized) |
0 | 75 throw new NotInitializedError(name()~": Cipher not initialized"); |
76 | |
12
8c7f8fecdd75
Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
Thomas Dixon <reikon@reikon.us>
parents:
8
diff
changeset
|
77 ubyte[] input = cast(ubyte[]) input_, |
8c7f8fecdd75
Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
Thomas Dixon <reikon@reikon.us>
parents:
8
diff
changeset
|
78 output = cast(ubyte[]) output_; |
0 | 79 |
12
8c7f8fecdd75
Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
Thomas Dixon <reikon@reikon.us>
parents:
8
diff
changeset
|
80 if (input.length < BLOCK_SIZE) |
0 | 81 throw new ShortBufferError(name()~": Input buffer too short"); |
12
8c7f8fecdd75
Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
Thomas Dixon <reikon@reikon.us>
parents:
8
diff
changeset
|
82 |
8c7f8fecdd75
Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
Thomas Dixon <reikon@reikon.us>
parents:
8
diff
changeset
|
83 if (output.length < BLOCK_SIZE) |
8c7f8fecdd75
Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
Thomas Dixon <reikon@reikon.us>
parents:
8
diff
changeset
|
84 throw new ShortBufferError(name()~": Output buffer too short"); |
0 | 85 |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
86 uint A = ByteConverter.LittleEndian.to!(uint)(input[0..4]), |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
87 B = ByteConverter.LittleEndian.to!(uint)(input[4..8]), |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
88 C = ByteConverter.LittleEndian.to!(uint)(input[8..12]), |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
89 D = ByteConverter.LittleEndian.to!(uint)(input[12..16]), |
0 | 90 t, |
91 u; | |
92 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
93 if (_encrypt) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
94 { |
0 | 95 B += S[0]; |
96 D += S[1]; | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
97 |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
98 for (int i = 1; i <= ROUNDS; i++) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
99 { |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
100 t = Bitwise.rotateLeft(B*((B<<1)+1), 5); |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
101 u = Bitwise.rotateLeft(D*((D<<1)+1), 5); |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
102 A = Bitwise.rotateLeft(A^t, u) + S[i<<1]; |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
103 C = Bitwise.rotateLeft(C^u, t) + S[(i<<1)+1]; |
0 | 104 t = A; |
105 A = B; | |
106 B = C; | |
107 C = D; | |
108 D = t; | |
109 } | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
110 |
0 | 111 A += S[2*ROUNDS+2]; |
112 C += S[2*ROUNDS+3]; | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
113 } |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
114 else |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
115 { |
0 | 116 C -= S[2*ROUNDS+3]; |
117 A -= S[2*ROUNDS+2]; | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
118 |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
119 for (int i = ROUNDS; i >= 1; i--) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
120 { |
0 | 121 t = D; |
122 D = C; | |
123 C = B; | |
124 B = A; | |
125 A = t; | |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
126 u = Bitwise.rotateLeft(D*((D<<1)+1), 5); |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
127 t = Bitwise.rotateLeft(B*((B<<1)+1), 5); |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
128 C = Bitwise.rotateRight(C-S[(i<<1)+1], t) ^ u; |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
129 A = Bitwise.rotateRight(A-S[i<<1], u) ^ t; |
0 | 130 } |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
131 |
0 | 132 D -= S[1]; |
133 B -= S[0]; | |
134 } | |
135 | |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
136 output[0..4] = ByteConverter.LittleEndian.from!(uint)(A); |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
137 output[4..8] = ByteConverter.LittleEndian.from!(uint)(B); |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
138 output[8..12] = ByteConverter.LittleEndian.from!(uint)(C); |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
139 output[12..16] = ByteConverter.LittleEndian.from!(uint)(D); |
0 | 140 |
12
8c7f8fecdd75
Added ManagedBlockCipher, changed Crypto to just import everything, made Hash.update() return itself (for chaining) and ditched BlockCipherWrapper.
Thomas Dixon <reikon@reikon.us>
parents:
8
diff
changeset
|
141 return BLOCK_SIZE; |
0 | 142 } |
143 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
144 void reset() |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
145 { |
0 | 146 setup(workingKey); |
147 } | |
148 | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
149 void setup(ubyte[] key) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
150 { |
0 | 151 uint c = key.length/4; |
152 uint[] L = new uint[c]; | |
153 for (int i = 0, j = 0; i < c; i++, j+=4) | |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
154 L[i] = ByteConverter.LittleEndian.to!(uint)(key[j..j+int.sizeof]); |
21
ec23779ee794
Removed redundant test vector from Blowfish unittest.
Thomas Dixon <reikon@reikon.us>
parents:
14
diff
changeset
|
155 |
0 | 156 S[0] = P; |
157 for (int i = 1; i <= 2*ROUNDS+3; i++) | |
158 S[i] = S[i-1] + Q; | |
21
ec23779ee794
Removed redundant test vector from Blowfish unittest.
Thomas Dixon <reikon@reikon.us>
parents:
14
diff
changeset
|
159 |
ec23779ee794
Removed redundant test vector from Blowfish unittest.
Thomas Dixon <reikon@reikon.us>
parents:
14
diff
changeset
|
160 uint A, B, i, j, v = 3*(2*ROUNDS+4); // Relying on ints initializing to 0 |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
161 for (int s = 1; s <= v; s++) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
162 { |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
163 A = S[i] = Bitwise.rotateLeft(S[i]+A+B, 3); |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
164 B = L[j] = Bitwise.rotateLeft(L[j]+A+B, A+B); |
0 | 165 i = (i + 1) % (2*ROUNDS+4); |
166 j = (j + 1) % c; | |
167 } | |
168 } | |
169 | |
170 /** Some RC6 test vectors from the spec. */ | |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
171 debug (UnitTest) |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
172 { |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
173 unittest |
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
174 { |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
175 static string[] test_keys = [ |
0 | 176 "00000000000000000000000000000000", |
177 "0123456789abcdef0112233445566778", | |
178 "00000000000000000000000000000000"~ | |
179 "0000000000000000", | |
180 "0123456789abcdef0112233445566778"~ | |
181 "899aabbccddeeff0", | |
182 "00000000000000000000000000000000"~ | |
183 "00000000000000000000000000000000", | |
184 "0123456789abcdef0112233445566778"~ | |
185 "899aabbccddeeff01032547698badcfe" | |
186 ]; | |
187 | |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
188 static string[] test_plaintexts = [ |
0 | 189 "00000000000000000000000000000000", |
190 "02132435465768798a9bacbdcedfe0f1", | |
191 "00000000000000000000000000000000", | |
192 "02132435465768798a9bacbdcedfe0f1", | |
193 "00000000000000000000000000000000", | |
194 "02132435465768798a9bacbdcedfe0f1" | |
195 ]; | |
196 | |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
197 static string[] test_ciphertexts = [ |
0 | 198 "8fc3a53656b1f778c129df4e9848a41e", |
199 "524e192f4715c6231f51f6367ea43f18", | |
200 "6cd61bcb190b30384e8a3f168690ae82", | |
201 "688329d019e505041e52e92af95291d4", | |
202 "8f5fbd0510d15fa893fa3fda6e857ec2", | |
203 "c8241816f0d7e48920ad16a1674e5d48" | |
204 ]; | |
205 | |
206 RC6 t = new RC6(); | |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
207 foreach (uint i, string test_key; test_keys) |
27
8b5eaf3c2979
Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents:
26
diff
changeset
|
208 { |
0 | 209 ubyte[] buffer = new ubyte[t.blockSize]; |
28
ad687db713a4
Further reworked the code for hash padding. Replaced all instances of 'char[]' with 'string' and removed a few 'const' modifiers as per Glenn Haecker's patch for D2 compatibility. Updated CONTRIBUTORS file.
Thomas Dixon <reikon@reikon.us>
parents:
27
diff
changeset
|
210 string result; |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
211 SymmetricKey key = new SymmetricKey(ByteConverter.hexDecode(test_key)); |
0 | 212 |
213 // Encryption | |
214 t.init(true, key); | |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
215 t.update(ByteConverter.hexDecode(test_plaintexts[i]), buffer); |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
216 result = ByteConverter.hexEncode(buffer); |
0 | 217 assert(result == test_ciphertexts[i], |
2
71aae178f89a
Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents:
1
diff
changeset
|
218 t.name~": ("~result~") != ("~test_ciphertexts[i]~")"); |
0 | 219 |
220 // Decryption | |
221 t.init(false, key); | |
23
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
222 t.update(ByteConverter.hexDecode(test_ciphertexts[i]), buffer); |
4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents:
21
diff
changeset
|
223 result = ByteConverter.hexEncode(buffer); |
0 | 224 assert(result == test_plaintexts[i], |
6
5cb17e09d685
Minor edits to the unittests of hash functions and ciphers. Added AES and test vectors.
Thomas Dixon <reikon@reikon.us>
parents:
2
diff
changeset
|
225 t.name~": ("~result~") != ("~test_plaintexts[i]~")"); |
0 | 226 } |
227 } | |
228 } | |
229 } |