annotate dcrypt/crypto/ciphers/ChaCha.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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
25
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
1 /**
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
2 * This file is part of the dcrypt project.
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
3 *
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
4 * Copyright: Copyright (C) dcrypt contributors 2009. All rights reserved.
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
5 * License: MIT
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
6 * Authors: Thomas Dixon
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
7 */
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
8
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
9 module dcrypt.crypto.ciphers.ChaCha;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
10
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
11 import dcrypt.crypto.StreamCipher;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
12 import dcrypt.crypto.ciphers.Salsa20;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
13 import dcrypt.crypto.params.ParametersWithIV;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
14 import dcrypt.misc.ByteConverter;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
15 import dcrypt.misc.Bitwise;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
16
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
17 /** Implementation of ChaCha designed by Daniel J. Bernstein. */
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
18 class ChaCha : Salsa20
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
19 {
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
20 string name()
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
21 {
25
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
22 return "ChaCha";
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
23 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
24
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
25 this()
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
26 {
25
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
27 i0 = 12;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
28 i1 = 13;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
29 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
30
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
31 protected void keySetup()
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
32 {
25
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
33 uint offset;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
34 ubyte[] constants;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
35
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
36 state[4] = ByteConverter.LittleEndian.to!(uint)(workingKey[0..4]);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
37 state[5] = ByteConverter.LittleEndian.to!(uint)(workingKey[4..8]);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
38 state[6] = ByteConverter.LittleEndian.to!(uint)(workingKey[8..12]);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
39 state[7] = ByteConverter.LittleEndian.to!(uint)(workingKey[12..16]);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
40
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
41 if (workingKey.length == 32)
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
42 {
25
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
43 constants = sigma;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
44 offset = 16;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
45 } else
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
46 constants = tau;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
47
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
48 state[ 8] = ByteConverter.LittleEndian.to!(uint)(workingKey[offset..offset+4]);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
49 state[ 9] = ByteConverter.LittleEndian.to!(uint)(workingKey[offset+4..offset+8]);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
50 state[10] = ByteConverter.LittleEndian.to!(uint)(workingKey[offset+8..offset+12]);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
51 state[11] = ByteConverter.LittleEndian.to!(uint)(workingKey[offset+12..offset+16]);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
52 state[ 0] = ByteConverter.LittleEndian.to!(uint)(constants[0..4]);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
53 state[ 1] = ByteConverter.LittleEndian.to!(uint)(constants[4..8]);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
54 state[ 2] = ByteConverter.LittleEndian.to!(uint)(constants[8..12]);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
55 state[ 3] = ByteConverter.LittleEndian.to!(uint)(constants[12..16]);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
56 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
57
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
58 protected void ivSetup()
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
59 {
25
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
60 state[12] = state[13] = 0;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
61 state[14] = ByteConverter.LittleEndian.to!(uint)(workingIV[0..4]);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
62 state[15] = ByteConverter.LittleEndian.to!(uint)(workingIV[4..8]);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
63 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
64
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
65 protected void salsa20WordToByte(uint[] input, ref ubyte[] output)
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
66 {
25
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
67 uint[] x = new uint[16];
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
68 x[] = input;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
69
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
70 int i;
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
71 for (i = 0; i < 4; i++)
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
72 {
25
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
73 x[ 0] += x[ 4]; x[12] = Bitwise.rotateLeft(x[12]^x[ 0], 16);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
74 x[ 8] += x[12]; x[ 4] = Bitwise.rotateLeft(x[ 4]^x[ 8], 12);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
75 x[ 0] += x[ 4]; x[12] = Bitwise.rotateLeft(x[12]^x[ 0], 8);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
76 x[ 8] += x[12]; x[ 4] = Bitwise.rotateLeft(x[ 4]^x[ 8], 7);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
77 x[ 1] += x[ 5]; x[13] = Bitwise.rotateLeft(x[13]^x[ 1], 16);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
78 x[ 9] += x[13]; x[ 5] = Bitwise.rotateLeft(x[ 5]^x[ 9], 12);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
79 x[ 1] += x[ 5]; x[13] = Bitwise.rotateLeft(x[13]^x[ 1], 8);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
80 x[ 9] += x[13]; x[ 5] = Bitwise.rotateLeft(x[ 5]^x[ 9], 7);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
81 x[ 2] += x[ 6]; x[14] = Bitwise.rotateLeft(x[14]^x[ 2], 16);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
82 x[10] += x[14]; x[ 6] = Bitwise.rotateLeft(x[ 6]^x[10], 12);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
83 x[ 2] += x[ 6]; x[14] = Bitwise.rotateLeft(x[14]^x[ 2], 8);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
84 x[10] += x[14]; x[ 6] = Bitwise.rotateLeft(x[ 6]^x[10], 7);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
85 x[ 3] += x[ 7]; x[15] = Bitwise.rotateLeft(x[15]^x[ 3], 16);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
86 x[11] += x[15]; x[ 7] = Bitwise.rotateLeft(x[ 7]^x[11], 12);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
87 x[ 3] += x[ 7]; x[15] = Bitwise.rotateLeft(x[15]^x[ 3], 8);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
88 x[11] += x[15]; x[ 7] = Bitwise.rotateLeft(x[ 7]^x[11], 7);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
89 x[ 0] += x[ 5]; x[15] = Bitwise.rotateLeft(x[15]^x[ 0], 16);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
90 x[10] += x[15]; x[ 5] = Bitwise.rotateLeft(x[ 5]^x[10], 12);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
91 x[ 0] += x[ 5]; x[15] = Bitwise.rotateLeft(x[15]^x[ 0], 8);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
92 x[10] += x[15]; x[ 5] = Bitwise.rotateLeft(x[ 5]^x[10], 7);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
93 x[ 1] += x[ 6]; x[12] = Bitwise.rotateLeft(x[12]^x[ 1], 16);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
94 x[11] += x[12]; x[ 6] = Bitwise.rotateLeft(x[ 6]^x[11], 12);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
95 x[ 1] += x[ 6]; x[12] = Bitwise.rotateLeft(x[12]^x[ 1], 8);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
96 x[11] += x[12]; x[ 6] = Bitwise.rotateLeft(x[ 6]^x[11], 7);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
97 x[ 2] += x[ 7]; x[13] = Bitwise.rotateLeft(x[13]^x[ 2], 16);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
98 x[ 8] += x[13]; x[ 7] = Bitwise.rotateLeft(x[ 7]^x[ 8], 12);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
99 x[ 2] += x[ 7]; x[13] = Bitwise.rotateLeft(x[13]^x[ 2], 8);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
100 x[ 8] += x[13]; x[ 7] = Bitwise.rotateLeft(x[ 7]^x[ 8], 7);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
101 x[ 3] += x[ 4]; x[14] = Bitwise.rotateLeft(x[14]^x[ 3], 16);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
102 x[ 9] += x[14]; x[ 4] = Bitwise.rotateLeft(x[ 4]^x[ 9], 12);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
103 x[ 3] += x[ 4]; x[14] = Bitwise.rotateLeft(x[14]^x[ 3], 8);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
104 x[ 9] += x[14]; x[ 4] = Bitwise.rotateLeft(x[ 4]^x[ 9], 7);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
105 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
106
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
107 for (i = 0; i < 16; i++)
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
108 x[i] += input[i];
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
109
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
110 int j;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
111 for (i = j = 0; i < x.length; i++,j+=int.sizeof)
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
112 output[j..j+int.sizeof] = ByteConverter.LittleEndian.from!(uint)(x[i]);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
113 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
114
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
115 /** ChaCha test vectors */
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
116 debug (UnitTest)
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
117 {
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
118 unittest
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
119 {
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
120 static string[] test_keys = [
25
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
121 "80000000000000000000000000000000",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
122 "0053a6f94c9ff24598eb3e91e4378add",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
123 "00002000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
124 "00000000000000000000000000000000",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
125 "0f62b5085bae0154a7fa4da0f34699ec"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
126 "3f92e5388bde3184d72a7dd02376c91c"
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
127
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
128 ];
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
129
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
130 static string[] test_ivs = [
25
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
131 "0000000000000000",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
132 "0d74db42a91077de",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
133 "0000000000000000",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
134 "288ff65dc42b92f9"
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
135 ];
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
136
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
137 static string[] test_plaintexts = [
25
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
138 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
139 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
140 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
141 "00000000000000000000000000000000",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
142
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
143 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
144 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
145 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
146 "00000000000000000000000000000000",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
147
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
148 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
149 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
150 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
151 "00000000000000000000000000000000",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
152
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
153 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
154 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
155 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
156 "00000000000000000000000000000000"
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
157
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
158
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
159 ];
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
160
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
161 static string[] test_ciphertexts = [
25
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
162 "beb1e81e0f747e43ee51922b3e87fb38"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
163 "d0163907b4ed49336032ab78b67c2457"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
164 "9fe28f751bd3703e51d876c017faa435"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
165 "89e63593e03355a7d57b2366f30047c5",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
166
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
167 "509b267e7266355fa2dc0a25c023fce4"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
168 "7922d03dd9275423d7cb7118b2aedf22"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
169 "0568854bf47920d6fc0fd10526cfe7f9"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
170 "de472835afc73c916b849e91eee1f529",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
171
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
172 "653f4a18e3d27daf51f841a00b6c1a2b"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
173 "d2489852d4ae0711e1a4a32ad166fa6f"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
174 "881a2843238c7e17786ba5162bc019d5"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
175 "73849c167668510ada2f62b4ff31ad04",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
176
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
177 "db165814f66733b7a8e34d1ffc123427"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
178 "1256d3bf8d8da2166922e598acac70f4"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
179 "12b3fe35a94190ad0ae2e8ec62134819"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
180 "ab61addcccfe99d867ca3d73183fa3fd"
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
181 ];
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
182
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
183 ChaCha cc = new ChaCha();
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
184 ubyte[] buffer = new ubyte[64];
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
185 string result;
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
186 for (int i = 0; i < test_keys.length; i++)
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
187 {
25
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
188 SymmetricKey key = new SymmetricKey(ByteConverter.hexDecode(test_keys[i]));
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
189 ParametersWithIV params = new ParametersWithIV(key, ByteConverter.hexDecode(test_ivs[i]));
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
190
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
191 // Encryption
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
192 cc.init(true, params);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
193 cc.update(ByteConverter.hexDecode(test_plaintexts[i]), buffer);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
194 result = ByteConverter.hexEncode(buffer);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
195 assert(result == test_ciphertexts[i],
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
196 cc.name()~": ("~result~") != ("~test_ciphertexts[i]~")");
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
197
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
198 // Decryption
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
199 cc.init(false, params);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
200 cc.update(ByteConverter.hexDecode(test_ciphertexts[i]), buffer);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
201 result = ByteConverter.hexEncode(buffer);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
202 assert(result == test_plaintexts[i],
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
203 cc.name()~": ("~result~") != ("~test_plaintexts[i]~")");
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
204 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
205 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
206 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
207 }