comparison dcrypt/crypto/ciphers/RC4.d @ 24:6428dfd7fede

Implemented Salsa20. Added some error checking to RC4's returnByte. Fixed copyright year in Bitwise.d and ByteConverter.d. Added Robert Smith to contributors file. Thanks buddy :)
author Thomas Dixon <reikon@reikon.us>
date Thu, 19 Feb 2009 14:45:13 -0500
parents 4589f8c5eb3c
children 176c933827a8
comparison
equal deleted inserted replaced
23:4589f8c5eb3c 24:6428dfd7fede
1 /** 1 /**
2 * This file is part of the dcrypt project. 2 * This file is part of the dcrypt project.
3 * 3 *
4 * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved. 4 * Copyright: Copyright (C) dcrypt contributors 2009. All rights reserved.
5 * License: MIT 5 * License: MIT
6 * Authors: Thomas Dixon 6 * Authors: Thomas Dixon
7 */ 7 */
8 8
9 module dcrypt.crypto.ciphers.RC4; 9 module dcrypt.crypto.ciphers.RC4;
20 ubyte[] state, 20 ubyte[] state,
21 workingKey; 21 workingKey;
22 ubyte x, y; 22 ubyte x, y;
23 } 23 }
24 24
25 this() {
26 state = new ubyte[256];
27 }
28
25 void init(bool encrypt, CipherParameters params) { 29 void init(bool encrypt, CipherParameters params) {
26 SymmetricKey keyParams = cast(SymmetricKey)params; 30 SymmetricKey keyParams = cast(SymmetricKey)params;
31
27 if (!keyParams) 32 if (!keyParams)
28 throw new InvalidParameterError( 33 throw new InvalidParameterError(
29 name()~": Invalid parameter passed to init"); 34 name()~": Invalid parameter passed to init");
35
30 if (keyParams.key.length < 0 || keyParams.key.length > 256) 36 if (keyParams.key.length < 0 || keyParams.key.length > 256)
31 throw new InvalidKeyError( 37 throw new InvalidKeyError(
32 name()~": Invalid key length (requires 1-256 bytes)"); 38 name()~": Invalid key length (requires 1-256 bytes)");
39
33 workingKey = keyParams.key; 40 workingKey = keyParams.key;
34 state = new ubyte[256];
35 setup(workingKey); 41 setup(workingKey);
36 _encrypt = true; 42
37 _initialized = true; 43 _encrypt = _initialized = true;
38 } 44 }
39 45
40 char[] name() { 46 char[] name() {
41 return "RC4"; 47 return "RC4";
42 } 48 }
43 49
44 ubyte returnByte(ubyte input) { 50 ubyte returnByte(ubyte input) {
51 if (!_initialized)
52 throw new NotInitializedError(name()~": Cipher not initialized");
53
45 y += state[++x]; 54 y += state[++x];
46 ubyte t = state[x]; 55 ubyte t = state[x];
47 state[x] = state[y]; 56 state[x] = state[y];
48 state[y] = t; 57 state[y] = t;
58
49 return (input^state[cast(ubyte)(state[x]+state[y])]); 59 return (input^state[cast(ubyte)(state[x]+state[y])]);
50 } 60 }
51 61
52 uint update(void[] input_, void[] output_) { 62 uint update(void[] input_, void[] output_) {
53 if (!_initialized) 63 if (!_initialized)
64 ubyte t = state[x]; 74 ubyte t = state[x];
65 state[x] = state[y]; 75 state[x] = state[y];
66 state[y] = t; 76 state[y] = t;
67 output[i] = input[i] ^ state[cast(ubyte)(state[x]+state[y])]; 77 output[i] = input[i] ^ state[cast(ubyte)(state[x]+state[y])];
68 } 78 }
79
69 return input.length; 80 return input.length;
70 } 81 }
71 82
72 void reset() { 83 void reset() {
73 setup(workingKey); 84 setup(workingKey);
83 x += key[i % key.length] + state[i]; 94 x += key[i % key.length] + state[i];
84 ubyte t = state[i]; 95 ubyte t = state[i];
85 state[i] = state[x]; 96 state[i] = state[x];
86 state[x] = t; 97 state[x] = t;
87 } 98 }
99
88 x = y = 0; 100 x = y = 0;
89 } 101 }
90 102
91 /** Some RC4 test vectors. */ 103 /** Some RC4 test vectors. */
92 version (UnitTest) { 104 version (UnitTest) {