annotate dcrypt/crypto/hashes/SHA224.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
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
1 /**
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
2 * This file is part of the dcrypt project.
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
3 *
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
4 * Copyright: Copyright (C) dcrypt contributors 2008. All rights reserved.
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
5 * License: MIT
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
6 * Authors: Thomas Dixon
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
7 */
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
8
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
9 module dcrypt.crypto.hashes.SHA224;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
10
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
11 import dcrypt.crypto.hashes.SHA256;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
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-224.
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_changenotice.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 */
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
19 class SHA224 : SHA256
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
20 {
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
21 this (void[] input_=null)
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
22 {
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
23 reset();
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
24 super(input_);
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
25 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
26
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
27 uint digestSize()
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
28 {
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
29 return 28;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
30 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
31
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
32 string name()
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
33 {
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
34 return "SHA224";
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
35 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
36
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
37 ubyte[] digest()
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
38 {
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
39 padMessage(MODE_SHA);
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
40
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
41 ubyte[] result = new ubyte[digestSize];
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
42
23
4589f8c5eb3c Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents: 6
diff changeset
43 result[0..4] = ByteConverter.BigEndian.from!(uint)(h0);
4589f8c5eb3c Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents: 6
diff changeset
44 result[4..8] = ByteConverter.BigEndian.from!(uint)(h1);
4589f8c5eb3c Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents: 6
diff changeset
45 result[8..12] = ByteConverter.BigEndian.from!(uint)(h2);
4589f8c5eb3c Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents: 6
diff changeset
46 result[12..16] = ByteConverter.BigEndian.from!(uint)(h3);
4589f8c5eb3c Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents: 6
diff changeset
47 result[16..20] = ByteConverter.BigEndian.from!(uint)(h4);
4589f8c5eb3c Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents: 6
diff changeset
48 result[20..24] = ByteConverter.BigEndian.from!(uint)(h5);
4589f8c5eb3c Replaced dcrypt.crypto.Util with dcrypt.misc.Bitwise and dcrypt.misc.ByteConverter. Altered all dependent files to reflect changes.
Thomas Dixon <reikon@reikon.us>
parents: 6
diff changeset
49 result[24..28] = ByteConverter.BigEndian.from!(uint)(h6);
2
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
50
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
51 reset();
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
52 return result;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
53 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
54
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
55 void reset()
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
56 {
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
57 super.reset();
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
58 h0 = 0xc1059ed8u;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
59 h1 = 0x367cd507u;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
60 h2 = 0x3070dd17u;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
61 h3 = 0xf70e5939u;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
62 h4 = 0xffc00b31u;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
63 h5 = 0x68581511u;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
64 h6 = 0x64f98fa7u;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
65 h7 = 0xbefa4fa4u;
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
66 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
67
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
68 SHA224 copy()
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
69 {
2
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
70 SHA224 h = new SHA224(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
71 h.bytes = bytes;
2
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
72 h.h0 = h0;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
73 h.h1 = h1;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
74 h.h2 = h2;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
75 h.h3 = h3;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
76 h.h4 = h4;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
77 h.h5 = h5;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
78 h.h6 = h6;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
79 h.h7 = h7;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
80 return h;
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
81 }
71aae178f89a Added copy() to hash functions. Modified some code style.
Thomas Dixon <reikon@reikon.us>
parents: 0
diff changeset
82
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
83 debug (UnitTest)
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
84 {
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
85 unittest
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
86 {
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
87 static string[] test_inputs = [
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
88 "",
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
89 "abc",
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
90 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
91 "a"
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
92 ];
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
93
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
94 static int[] test_repeat = [
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
95 1, 1, 1, 1000000
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
96 ];
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
97
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
98 static string[] test_results = [
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
99 "d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f",
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
100 "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7",
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
101 "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525",
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
102 "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67"
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
103 ];
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
104
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
105 SHA224 h = new SHA224();
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
106 foreach (uint i, string input; test_inputs)
27
8b5eaf3c2979 Fixed error in hash message padding reported by Glenn Haecker.
Thomas Dixon <reikon@reikon.us>
parents: 26
diff changeset
107 {
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
108 for (int j = 0; j < test_repeat[i]; j++)
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
109 h.update(input);
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
110 string digest = h.hexDigest();
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
111 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
112 h.name~": ("~digest~") != ("~test_results[i]~")");
0
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
113 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
114 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
115 }
0e08791a1418 Initial import.
Thomas Dixon <reikon@reikon.us>
parents:
diff changeset
116 }