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