Mercurial > projects > dcrypt
annotate dcrypt/crypto/modes/CTR.d @ 23:4589f8c5eb3c
Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
author | Thomas Dixon <reikon@reikon.us> |
---|---|
date | Sat, 14 Feb 2009 19:58:20 -0500 |
parents | 8c7f8fecdd75 |
children | 8b5eaf3c2979 |
rev | line source |
---|---|
0 | 1 /** |
2 * This file is part of the dcrypt project. | |
3 * | |
4 * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved. | |
5 * License: MIT | |
6 * Authors: Thomas Dixon | |
7 */ | |
8 | |
9 module dcrypt.crypto.modes.CTR; | |
10 | |
11 import dcrypt.crypto.BlockCipher; | |
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:
10
diff
changeset
|
12 public import dcrypt.crypto.params.ParametersWithIV; |
0 | 13 |
14 | |
15 /** This class implements the counter (CTR/SIC/ICM) block mode, | |
16 treating the counter as a big endian integer. */ | |
17 class CTR : BlockCipher { | |
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:
12
diff
changeset
|
18 private BlockCipher wrappedCipher; |
0 | 19 private ubyte[] iv, |
20 counter, | |
21 counterOutput; | |
22 private bool initialized = false; | |
23 | |
24 /** | |
25 * Params: | |
26 * cipher = Block cipher to wrap. | |
27 */ | |
28 this (BlockCipher cipher) { | |
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:
12
diff
changeset
|
29 wrappedCipher = cipher; |
0 | 30 } |
31 | |
32 /** Returns: The underlying cipher we are wrapping. */ | |
33 BlockCipher cipher() { | |
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:
12
diff
changeset
|
34 return wrappedCipher; |
0 | 35 } |
36 | |
37 char[] name() { | |
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:
12
diff
changeset
|
38 return wrappedCipher.name~"/CTR"; |
0 | 39 } |
40 | |
41 /** | |
42 * Throws: dcrypt.crypto.errors.InvalidParameterError if params aren'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:
12
diff
changeset
|
43 * an instance of dcrypt.crypto.params.ParametersWithIV. |
0 | 44 */ |
45 void init(bool encrypt, CipherParameters params) { | |
46 ParametersWithIV ivParams = cast(ParametersWithIV)params; | |
47 | |
48 if (!ivParams) | |
49 throw new InvalidParameterError( | |
50 name()~": Block mode requires IV (use ParametersWithIV)"); | |
51 if (ivParams.iv.length != blockSize) | |
52 throw new InvalidParameterError( | |
53 name()~": IV must be same length as cipher block size"); | |
54 | |
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:
12
diff
changeset
|
55 wrappedCipher.init(true, ivParams.parameters); |
0 | 56 |
57 iv = ivParams.iv[0..blockSize]; | |
58 counter = new ubyte[blockSize]; | |
59 counter[] = iv; | |
60 counterOutput = new ubyte[blockSize]; | |
61 | |
62 initialized = true; | |
63 } | |
64 | |
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:
10
diff
changeset
|
65 uint update(void[] input_, void[] output_) { |
0 | 66 if (!initialized) |
67 throw new NotInitializedError( | |
68 name()~": Block mode not initialized"); | |
69 | |
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:
10
diff
changeset
|
70 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:
10
diff
changeset
|
71 output = cast(ubyte[]) output_; |
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:
10
diff
changeset
|
72 |
10
cd376996cdb3
Renamed SymmetricCipher back to Cipher (we don't support any other kind atm, I'll deal with it when we do.). Added BlockCipherWrapper for the encryption of arbitrary streams with or without padding. Removed hashByName, and replaced it with createHash. Re-did the high-level API, and filled out Crypto. Added cipher creation via createCipher. Added dsk to the CONTRIBUTORS file for helping with the design of the high-level API.
Thomas Dixon <reikon@reikon.us>
parents:
8
diff
changeset
|
73 uint len = (counter.length > input.length) ? input.length : counter.length; |
0 | 74 |
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:
10
diff
changeset
|
75 if (len > output.length) |
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:
10
diff
changeset
|
76 throw new ShortBufferError(name()~": Output buffer too short"); |
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:
10
diff
changeset
|
77 |
0 | 78 // Encrypt the counter |
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:
12
diff
changeset
|
79 wrappedCipher.update(counter, counterOutput); |
0 | 80 |
81 // XOR output with plaintext to create ciphertext | |
10
cd376996cdb3
Renamed SymmetricCipher back to Cipher (we don't support any other kind atm, I'll deal with it when we do.). Added BlockCipherWrapper for the encryption of arbitrary streams with or without padding. Removed hashByName, and replaced it with createHash. Re-did the high-level API, and filled out Crypto. Added cipher creation via createCipher. Added dsk to the CONTRIBUTORS file for helping with the design of the high-level API.
Thomas Dixon <reikon@reikon.us>
parents:
8
diff
changeset
|
82 for (int i = 0; i < len; i++) |
8
23c62e28b3a4
Reworked symmetric cipher classes to have SymmetricCipher as their superclass, and follow the general interface of init(), process(), etc. Made sure everything still passed test vectors. Removed Cipher class. I'll worry about that shit when we support something other than symmetric ciphers.
Thomas Dixon <reikon@reikon.us>
parents:
0
diff
changeset
|
83 counterOutput[i] ^= input[i]; |
0 | 84 |
85 // Increment the counter | |
86 for (int i = counter.length-1; i >= 0; i--) | |
87 if (++counter[i]) break; | |
88 | |
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:
10
diff
changeset
|
89 output[0..len] = counterOutput[0..len]; |
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:
10
diff
changeset
|
90 |
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:
10
diff
changeset
|
91 return len; |
0 | 92 } |
93 | |
94 uint blockSize() { | |
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:
12
diff
changeset
|
95 return wrappedCipher.blockSize; |
0 | 96 } |
97 | |
98 void reset() { | |
99 counter[] = iv; | |
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:
12
diff
changeset
|
100 wrappedCipher.reset(); |
0 | 101 } |
102 } |