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.hashes.SHA512;
|
|
10
|
|
11 public import dcrypt.crypto.Hash;
|
|
12
|
|
13 /** Implementation of SHA-512. */
|
|
14 class SHA512 : Hash {
|
|
15 private const ulong[] K = [
|
|
16 0x428a2f98d728ae22u, 0x7137449123ef65cdu, 0xb5c0fbcfec4d3b2fu, 0xe9b5dba58189dbbcu,
|
|
17 0x3956c25bf348b538u, 0x59f111f1b605d019u, 0x923f82a4af194f9bu, 0xab1c5ed5da6d8118u,
|
|
18 0xd807aa98a3030242u, 0x12835b0145706fbeu, 0x243185be4ee4b28cu, 0x550c7dc3d5ffb4e2u,
|
|
19 0x72be5d74f27b896fu, 0x80deb1fe3b1696b1u, 0x9bdc06a725c71235u, 0xc19bf174cf692694u,
|
|
20 0xe49b69c19ef14ad2u, 0xefbe4786384f25e3u, 0x0fc19dc68b8cd5b5u, 0x240ca1cc77ac9c65u,
|
|
21 0x2de92c6f592b0275u, 0x4a7484aa6ea6e483u, 0x5cb0a9dcbd41fbd4u, 0x76f988da831153b5u,
|
|
22 0x983e5152ee66dfabu, 0xa831c66d2db43210u, 0xb00327c898fb213fu, 0xbf597fc7beef0ee4u,
|
|
23 0xc6e00bf33da88fc2u, 0xd5a79147930aa725u, 0x06ca6351e003826fu, 0x142929670a0e6e70u,
|
|
24 0x27b70a8546d22ffcu, 0x2e1b21385c26c926u, 0x4d2c6dfc5ac42aedu, 0x53380d139d95b3dfu,
|
|
25 0x650a73548baf63deu, 0x766a0abb3c77b2a8u, 0x81c2c92e47edaee6u, 0x92722c851482353bu,
|
|
26 0xa2bfe8a14cf10364u, 0xa81a664bbc423001u, 0xc24b8b70d0f89791u, 0xc76c51a30654be30u,
|
|
27 0xd192e819d6ef5218u, 0xd69906245565a910u, 0xf40e35855771202au, 0x106aa07032bbd1b8u,
|
|
28 0x19a4c116b8d2d0c8u, 0x1e376c085141ab53u, 0x2748774cdf8eeb99u, 0x34b0bcb5e19b48a8u,
|
|
29 0x391c0cb3c5c95a63u, 0x4ed8aa4ae3418acbu, 0x5b9cca4f7763e373u, 0x682e6ff3d6b2b8a3u,
|
|
30 0x748f82ee5defb2fcu, 0x78a5636f43172f60u, 0x84c87814a1f0ab72u, 0x8cc702081a6439ecu,
|
|
31 0x90befffa23631e28u, 0xa4506cebde82bde9u, 0xbef9a3f7b2c67915u, 0xc67178f2e372532bu,
|
|
32 0xca273eceea26619cu, 0xd186b8c721c0c207u, 0xeada7dd6cde0eb1eu, 0xf57d4f7fee6ed178u,
|
|
33 0x06f067aa72176fbau, 0x0a637dc5a2c898a6u, 0x113f9804bef90daeu, 0x1b710b35131c471bu,
|
|
34 0x28db77f523047d84u, 0x32caab7b40c72493u, 0x3c9ebe0a15c9bebcu, 0x431d67c49c100d4cu,
|
|
35 0x4cc5d4becb3e42b6u, 0x597f299cfc657e2au, 0x5fcb6fab3ad6faecu, 0x6c44198c4a475817u
|
|
36 ];
|
|
37 protected ulong h0, h1, h2, h3, h4, h5, h6, h7;
|
|
38
|
|
39 this (void[] input_=null) {
|
|
40 reset();
|
|
41 super(input_);
|
|
42 }
|
|
43
|
|
44 uint blockSize() {
|
|
45 return 128;
|
|
46 }
|
|
47
|
|
48 uint digestSize() {
|
|
49 return 64;
|
|
50 }
|
|
51
|
|
52 char[] name() {
|
|
53 return "SHA512";
|
|
54 }
|
|
55
|
|
56 void transform(ubyte[] input) {
|
|
57 ulong[] w = new ulong[80];
|
|
58
|
|
59 for (int i = 0, j = 0; i < 16; i++,j+=8)
|
|
60 w[i] = Util.ubytesToUlongBig(input, j);
|
|
61
|
|
62 for (int i = 16; i < 80; i++)
|
|
63 w[i] = theta1(w[i-2]) + w[i-7] + theta0(w[i-15]) + w[i-16];
|
|
64
|
|
65 ulong a = h0,
|
|
66 b = h1,
|
|
67 c = h2,
|
|
68 d = h3,
|
|
69 e = h4,
|
|
70 f = h5,
|
|
71 g = h6,
|
|
72 h = h7;
|
|
73
|
|
74 for (int i = 0; i < 80; i++) {
|
|
75 ulong t1 = h + sum1(e) + ch(e,f,g) + K[i] + w[i],
|
|
76 t2 = sum0(a) + maj(a,b,c);
|
|
77 h = g;
|
|
78 g = f;
|
|
79 f = e;
|
|
80 e = d + t1;
|
|
81 d = c;
|
|
82 c = b;
|
|
83 b = a;
|
|
84 a = t1 + t2;
|
|
85 }
|
|
86
|
|
87 h0 += a;
|
|
88 h1 += b;
|
|
89 h2 += c;
|
|
90 h3 += d;
|
|
91 h4 += e;
|
|
92 h5 += f;
|
|
93 h6 += g;
|
|
94 h7 += h;
|
|
95 }
|
|
96
|
|
97 private ulong ch(ulong x, ulong y, ulong z) {
|
|
98 return (x&y)^(~x&z);
|
|
99 }
|
|
100
|
|
101 private ulong maj(ulong x, ulong y, ulong z) {
|
|
102 return (x&y)^(x&z)^(y&z);
|
|
103 }
|
|
104
|
|
105 private ulong sum0(ulong x) {
|
|
106 return (Util.rotateRight64(x,28)^
|
|
107 Util.rotateRight64(x,34)^
|
|
108 Util.rotateRight64(x,39));
|
|
109 }
|
|
110
|
|
111 private ulong sum1(ulong x) {
|
|
112 return (Util.rotateRight64(x,14)^
|
|
113 Util.rotateRight64(x,18)^
|
|
114 Util.rotateRight64(x,41));
|
|
115 }
|
|
116
|
|
117 private ulong theta0(ulong x) {
|
|
118 return Util.rotateRight64(x,1)^Util.rotateRight64(x,8)^(x >> 7);
|
|
119 }
|
|
120
|
|
121 private ulong theta1(ulong x) {
|
|
122 return Util.rotateRight64(x,19)^Util.rotateRight64(x,61)^(x >> 6);
|
|
123 }
|
|
124
|
|
125 ubyte[] digest() {
|
|
126 padMessage(MODE_SHA);
|
|
127 ubyte[] result = new ubyte[digestSize];
|
|
128
|
|
129 Util.ulongToUbytesBig(h0, result, 0);
|
|
130 Util.ulongToUbytesBig(h1, result, 8);
|
|
131 Util.ulongToUbytesBig(h2, result, 16);
|
|
132 Util.ulongToUbytesBig(h3, result, 24);
|
|
133 Util.ulongToUbytesBig(h4, result, 32);
|
|
134 Util.ulongToUbytesBig(h5, result, 40);
|
|
135 Util.ulongToUbytesBig(h6, result, 48);
|
|
136 Util.ulongToUbytesBig(h7, result, 56);
|
|
137
|
|
138 reset();
|
|
139 return result;
|
|
140 }
|
|
141
|
|
142 void reset() {
|
|
143 super.reset();
|
|
144 h0 = 0x6a09e667f3bcc908u;
|
|
145 h1 = 0xbb67ae8584caa73bu;
|
|
146 h2 = 0x3c6ef372fe94f82bu;
|
|
147 h3 = 0xa54ff53a5f1d36f1u;
|
|
148 h4 = 0x510e527fade682d1u;
|
|
149 h5 = 0x9b05688c2b3e6c1fu;
|
|
150 h6 = 0x1f83d9abfb41bd6bu;
|
|
151 h7 = 0x5be0cd19137e2179u;
|
|
152 }
|
|
153
|
|
154 version (UnitTest) {
|
|
155 unittest {
|
|
156 static const char[][] test_inputs = [
|
|
157 "",
|
|
158 "abc",
|
|
159 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"~
|
|
160 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
|
|
161 "a"
|
|
162 ];
|
|
163
|
|
164 static const int[] test_repeat = [
|
|
165 1, 1, 1, 1000000
|
|
166 ];
|
|
167
|
|
168 static const char[][] test_results = [
|
|
169 "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce"~
|
|
170 "47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e",
|
|
171
|
|
172 "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"~
|
|
173 "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
|
|
174
|
|
175 "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"~
|
|
176 "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909",
|
|
177
|
|
178 "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"~
|
|
179 "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"
|
|
180 ];
|
|
181
|
|
182 SHA512 h = new SHA512();
|
|
183 foreach (uint i, char[] input; test_inputs) {
|
|
184 for (int j = 0; j < test_repeat[i]; j++)
|
|
185 h.update(input);
|
|
186 char[] digest = h.hexDigest();
|
|
187 assert(digest == test_results[i],
|
|
188 h.name()~": ("~digest~") != ("~test_results[i]~")");
|
|
189 }
|
|
190 }
|
|
191 }
|
|
192 }
|