annotate dcrypt/crypto/ciphers/ChaCha.d @ 26:176c933827a8

Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
author Thomas Dixon <reikon@reikon.us>
date Sun, 01 Mar 2009 13:06:48 -0500
parents 528676d20398
children 8b5eaf3c2979
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. */
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
18 class ChaCha : Salsa20 {
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
19 char[] name() {
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
20 return "ChaCha";
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
21 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
22
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
23 this() {
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
24 i0 = 12;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
25 i1 = 13;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
26 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
27
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
28 protected void keySetup() {
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
29 uint offset;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
30 ubyte[] constants;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
31
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
32 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
33 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
34 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
35 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
36
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
37 if (workingKey.length == 32) {
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
38 constants = sigma;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
39 offset = 16;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
40 } else
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
41 constants = tau;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
42
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
43 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
44 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
45 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
46 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
47 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
48 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
49 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
50 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
51 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
52
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
53 protected void ivSetup() {
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
54 state[12] = state[13] = 0;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
55 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
56 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
57 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
58
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
59 protected void salsa20WordToByte(uint[] input, ref ubyte[] output) {
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
60 uint[] x = new uint[16];
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
61 x[] = input;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
62
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
63 int i;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
64 for (i = 0; i < 4; i++) {
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
65 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
66 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
67 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
68 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
69 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
70 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
71 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
72 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
73 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
74 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
75 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
76 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
77 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
78 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
79 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
80 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
81 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
82 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
83 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
84 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
85 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
86 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
87 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
88 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
89 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
90 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
91 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
92 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
93 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
94 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
95 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
96 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
97 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
98
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
99 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
100 x[i] += input[i];
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
101
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
102 int j;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
103 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
104 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
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 /** ChaCha test vectors */
26
176c933827a8 Implemented MD4. Refactored MD5. Replaced all instances of 'version (UnitTest)' with 'debug (UnitTest)'.
Thomas Dixon <reikon@reikon.us>
parents: 25
diff changeset
108 debug (UnitTest) {
25
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
109 unittest {
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
110 static const char[][] test_keys = [
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
111 "80000000000000000000000000000000",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
112 "0053a6f94c9ff24598eb3e91e4378add",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
113 "00002000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
114 "00000000000000000000000000000000",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
115 "0f62b5085bae0154a7fa4da0f34699ec"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
116 "3f92e5388bde3184d72a7dd02376c91c"
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
117
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
118 ];
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
119
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
120 static const char[][] test_ivs = [
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
121 "0000000000000000",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
122 "0d74db42a91077de",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
123 "0000000000000000",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
124 "288ff65dc42b92f9"
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
125 ];
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
126
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
127 static const char[][] test_plaintexts = [
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
128 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
129 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
130 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
131 "00000000000000000000000000000000",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
132
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
133 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
134 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
135 "00000000000000000000000000000000"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
136 "00000000000000000000000000000000",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
137
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
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
149 ];
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
150
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
151 static const char[][] test_ciphertexts = [
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
152 "beb1e81e0f747e43ee51922b3e87fb38"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
153 "d0163907b4ed49336032ab78b67c2457"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
154 "9fe28f751bd3703e51d876c017faa435"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
155 "89e63593e03355a7d57b2366f30047c5",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
156
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
157 "509b267e7266355fa2dc0a25c023fce4"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
158 "7922d03dd9275423d7cb7118b2aedf22"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
159 "0568854bf47920d6fc0fd10526cfe7f9"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
160 "de472835afc73c916b849e91eee1f529",
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
161
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
162 "653f4a18e3d27daf51f841a00b6c1a2b"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
163 "d2489852d4ae0711e1a4a32ad166fa6f"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
164 "881a2843238c7e17786ba5162bc019d5"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
165 "73849c167668510ada2f62b4ff31ad04",
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 "db165814f66733b7a8e34d1ffc123427"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
168 "1256d3bf8d8da2166922e598acac70f4"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
169 "12b3fe35a94190ad0ae2e8ec62134819"~
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
170 "ab61addcccfe99d867ca3d73183fa3fd"
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
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
173 ChaCha cc = new ChaCha();
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
174 ubyte[] buffer = new ubyte[64];
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
175 char[] result;
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
176 for (int i = 0; i < test_keys.length; i++) {
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
177 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
178 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
179
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
180 // Encryption
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
181 cc.init(true, params);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
182 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
183 result = ByteConverter.hexEncode(buffer);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
184 assert(result == test_ciphertexts[i],
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
185 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
186
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
187 // Decryption
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
188 cc.init(false, params);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
189 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
190 result = ByteConverter.hexEncode(buffer);
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
191 assert(result == test_plaintexts[i],
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
192 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
193 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
194 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
195 }
528676d20398 Implemented ChaCha. Modified Salsa20 to simplify the implementation of ChaCha.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
196 }