comparison dcrypt/crypto/hashes/SHA384.d @ 27:8b5eaf3c2979

Fixed error in hash message padding reported by Glenn Haecker.
author Thomas Dixon <reikon@reikon.us>
date Sat, 09 May 2009 23:29:20 -0400
parents 176c933827a8
children ad687db713a4
comparison
equal deleted inserted replaced
26:176c933827a8 27:8b5eaf3c2979
14 * Implementation of the US NSA's SHA-384. 14 * Implementation of the US NSA's SHA-384.
15 * 15 *
16 * Conforms: FIPS-180-2 16 * Conforms: FIPS-180-2
17 * References: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf 17 * References: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
18 */ 18 */
19 class SHA384 : SHA512 { 19 class SHA384 : SHA512
20 this (void[] input_=null) { 20 {
21 this (void[] input_=null)
22 {
21 reset(); 23 reset();
22 super(input_); 24 super(input_);
23 } 25 }
24 26
25 uint digestSize() { 27 uint digestSize()
28 {
26 return 48; 29 return 48;
27 } 30 }
28 31
29 char[] name() { 32 char[] name()
33 {
30 return "SHA384"; 34 return "SHA384";
31 } 35 }
32 36
33 ubyte[] digest() { 37 ubyte[] digest()
38 {
34 padMessage(MODE_SHA); 39 padMessage(MODE_SHA);
35 ubyte[] result = new ubyte[digestSize]; 40 ubyte[] result = new ubyte[digestSize];
36 41
37 result[0..8] = ByteConverter.BigEndian.from!(ulong)(h0); 42 result[0..8] = ByteConverter.BigEndian.from!(ulong)(h0);
38 result[8..16] = ByteConverter.BigEndian.from!(ulong)(h1); 43 result[8..16] = ByteConverter.BigEndian.from!(ulong)(h1);
43 48
44 reset(); 49 reset();
45 return result; 50 return result;
46 } 51 }
47 52
48 void reset() { 53 void reset()
54 {
49 super.reset(); 55 super.reset();
50 h0 = 0xcbbb9d5dc1059ed8u, 56 h0 = 0xcbbb9d5dc1059ed8u,
51 h1 = 0x629a292a367cd507u, 57 h1 = 0x629a292a367cd507u,
52 h2 = 0x9159015a3070dd17u, 58 h2 = 0x9159015a3070dd17u,
53 h3 = 0x152fecd8f70e5939u, 59 h3 = 0x152fecd8f70e5939u,
55 h5 = 0x8eb44a8768581511u, 61 h5 = 0x8eb44a8768581511u,
56 h6 = 0xdb0c2e0d64f98fa7u, 62 h6 = 0xdb0c2e0d64f98fa7u,
57 h7 = 0x47b5481dbefa4fa4u; 63 h7 = 0x47b5481dbefa4fa4u;
58 } 64 }
59 65
60 SHA384 copy() { 66 SHA384 copy()
67 {
61 SHA384 h = new SHA384(buffer[0..index]); 68 SHA384 h = new SHA384(buffer[0..index]);
62 h.bytes = bytes; 69 h.bytes = bytes;
63 h.h0 = h0; 70 h.h0 = h0;
64 h.h1 = h1; 71 h.h1 = h1;
65 h.h2 = h2; 72 h.h2 = h2;
69 h.h6 = h6; 76 h.h6 = h6;
70 h.h7 = h7; 77 h.h7 = h7;
71 return h; 78 return h;
72 } 79 }
73 80
74 debug (UnitTest) { 81 debug (UnitTest)
75 unittest { 82 {
83 unittest
84 {
76 static const char[][] test_inputs = [ 85 static const char[][] test_inputs = [
77 "", 86 "",
78 "abc", 87 "abc",
79 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"~ 88 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"~
80 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 89 "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
98 "9d0e1809716474cb086e834e310a4a1ced149e9c00f24852"~ 107 "9d0e1809716474cb086e834e310a4a1ced149e9c00f24852"~
99 "7972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985" 108 "7972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985"
100 ]; 109 ];
101 110
102 SHA384 h = new SHA384(); 111 SHA384 h = new SHA384();
103 foreach (uint i, char[] input; test_inputs) { 112 foreach (uint i, char[] input; test_inputs)
113 {
104 for (int j = 0; j < test_repeat[i]; j++) 114 for (int j = 0; j < test_repeat[i]; j++)
105 h.update(input); 115 h.update(input);
106 char[] digest = h.hexDigest(); 116 char[] digest = h.hexDigest();
107 assert(digest == test_results[i], 117 assert(digest == test_results[i],
108 h.name~": ("~digest~") != ("~test_results[i]~")"); 118 h.name~": ("~digest~") != ("~test_results[i]~")");