Skip to content

Commit b99c334

Browse files
authored
Adds FIPS 180-4 512/t variants (flutter#75)
* Adds FIPS 180-4 512/t variants 512/224 and 512/256 are variants specified in the FIPS 180-4 and commonly available in other packages.
1 parent cbd056f commit b99c334

File tree

6 files changed

+211
-30
lines changed

6 files changed

+211
-30
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
## 2.2.0-nullsafety-dev
23

34
Pre-release for the null safety migration of this package.
@@ -9,6 +10,8 @@ This release will be pinned to only allow pre-release sdk versions starting from
910
2.10.0-2.0.dev, which is the first version where this package will appear in the
1011
null safety allow list.
1112

13+
* AddsSHA-2 512/224 and SHA-2 512/256 from FIPS 180-4
14+
1215
## 2.1.5
1316

1417
* Improve example and package description to address package site maintenance

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ The following hashing algorithms are supported:
99
* SHA-256
1010
* SHA-384
1111
* SHA-512
12+
* SHA-512/224
13+
* SHA-512/256
1214
* MD5
1315
* HMAC (i.e. HMAC-MD5, HMAC-SHA1, HMAC-SHA256)
1416

lib/src/sha512.dart

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,33 @@ import 'hash.dart';
1010
import 'sha512_fastsinks.dart' if (dart.library.js) 'sha512_slowsinks.dart';
1111
import 'utils.dart';
1212

13-
/// An instance of [Sha2Sha384].
13+
/// A reusable instance of [Sha384].
1414
///
15-
/// This instance provides convenient access to the [Sha384][rfc] hash function.
15+
/// This instance provides convenient and canonical access to the
16+
/// [Sha384][rfc] hash functionality.
1617
///
1718
/// [rfc]: http://tools.ietf.org/html/rfc6234
18-
final sha384 = Sha384._();
19+
const sha384 = Sha384._();
1920

20-
/// An instance of [Sha2Sha512].
21+
/// A reusable instance of [Sha512].
2122
///
22-
/// This instance provides convenient access to the [Sha512][rfc] hash function.
23+
/// This instance provides convenient and canonical access to the
24+
/// [Sha512][rfc] hash functionality.
2325
///
2426
/// [rfc]: http://tools.ietf.org/html/rfc6234
25-
final sha512 = Sha512._();
27+
const sha512 = Sha512._();
28+
29+
/// A reusable, canonical instance of the [Sha512/224][FIPS] [Hash]
30+
/// functionality.
31+
///
32+
/// [FIPS]: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
33+
const sha512224 = _Sha512224();
34+
35+
/// A reusable, canonical instance of the [Sha512/256][FIPS] [Hash]
36+
/// functionality.
37+
///
38+
/// [FIPS]: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
39+
const sha512256 = _Sha512256();
2640

2741
/// An implementation of the [SHA-384][rfc] hash function.
2842
///
@@ -34,7 +48,7 @@ class Sha384 extends Hash {
3448
@override
3549
final int blockSize = 32 * bytesPerWord;
3650

37-
Sha384._();
51+
const Sha384._();
3852

3953
Sha384 newInstance() => Sha384._();
4054

@@ -49,13 +63,49 @@ class Sha384 extends Hash {
4963
///
5064
/// Note that it's almost always easier to use [sha512] rather than creating a
5165
/// new instance.
52-
class Sha512 extends Sha384 {
53-
Sha512._() : super._();
54-
66+
class Sha512 extends Hash {
5567
@override
68+
final int blockSize = 32 * bytesPerWord;
69+
70+
const Sha512._();
71+
5672
Sha512 newInstance() => Sha512._();
5773

5874
@override
5975
ByteConversionSink startChunkedConversion(Sink<Digest> sink) =>
6076
ByteConversionSink.from(Sha512Sink(sink));
6177
}
78+
79+
/// An implementation of the [SHA-512/224][FIPS] hash function.
80+
///
81+
/// [FIPS]: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
82+
///
83+
/// Note that it's almost always easier to use [sha512224] rather than creating
84+
/// a new instance.
85+
class _Sha512224 extends Hash {
86+
@override
87+
final int blockSize = 32 * bytesPerWord;
88+
89+
const _Sha512224();
90+
91+
@override
92+
ByteConversionSink startChunkedConversion(Sink<Digest> sink) =>
93+
ByteConversionSink.from(Sha512224Sink(sink));
94+
}
95+
96+
/// An implementation of the [SHA-512/256][FIPS] hash function.
97+
///
98+
/// [FIPS]: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf
99+
///
100+
/// Note that it's almost always easier to use [sha512256] rather than creating
101+
/// a new instance.
102+
class _Sha512256 extends Hash {
103+
@override
104+
final int blockSize = 32 * bytesPerWord;
105+
106+
const _Sha512256();
107+
108+
@override
109+
ByteConversionSink startChunkedConversion(Sink<Digest> sink) =>
110+
ByteConversionSink.from(Sha512256Sink(sink));
111+
}

lib/src/sha512_fastsinks.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,54 @@ class Sha512Sink extends _Sha64BitSink {
144144
]));
145145
}
146146

147+
/// The concrete implementation of [Sha512/224].
148+
///
149+
/// This is separate so that it can extend [HashSink] without leaking additional
150+
/// public members.
151+
class Sha512224Sink extends _Sha64BitSink {
152+
@override
153+
final digestBytes = 7;
154+
155+
Sha512224Sink(Sink<Digest> sink)
156+
: super(
157+
sink,
158+
Uint64List.fromList([
159+
// FIPS 180-4, Section 5.3.6.1
160+
0x8c3d37c819544da2,
161+
0x73e1996689dcd4d6,
162+
0x1dfab7ae32ff9c82,
163+
0x679dd514582f9fcf,
164+
0x0f6d2b697bd44da8,
165+
0x77e36f7304c48942,
166+
0x3f9d85a86a1d36c8,
167+
0x1112e6ad91d692a1,
168+
]));
169+
}
170+
171+
/// The concrete implementation of [Sha512/256].
172+
///
173+
/// This is separate so that it can extend [HashSink] without leaking additional
174+
/// public members.
175+
class Sha512256Sink extends _Sha64BitSink {
176+
@override
177+
final digestBytes = 8;
178+
179+
Sha512256Sink(Sink<Digest> sink)
180+
: super(
181+
sink,
182+
Uint64List.fromList([
183+
// FIPS 180-4, Section 5.3.6.2
184+
0x22312194fc2bf72c,
185+
0x9f555fa3c84c64c2,
186+
0x2393b86b6f53b151,
187+
0x963877195940eabd,
188+
0x96283ee2a88effe3,
189+
0xbe5e1e2553863992,
190+
0x2b0199fc2c85b8aa,
191+
0x0eb72ddc81c52ca2,
192+
]));
193+
}
194+
147195
final _noise64 = Uint64List.fromList([
148196
0x428a2f98d728ae22,
149197
0x7137449123ef65cd,

lib/src/sha512_slowsinks.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,51 @@ class Sha512Sink extends _Sha64BitSink {
327327
0x5be0cd19, 0x137e2179,
328328
]));
329329
}
330+
331+
/// The concrete implementation of [Sha512/224].
332+
///
333+
/// This is separate so that it can extend [HashSink] without leaking additional
334+
/// public members.
335+
class Sha512224Sink extends _Sha64BitSink {
336+
@override
337+
final digestBytes = 7;
338+
339+
Sha512224Sink(Sink<Digest> sink)
340+
: super(
341+
sink,
342+
Uint32List.fromList([
343+
// FIPS 180-4, Section 5.3.6.1
344+
0x8c3d37c8, 0x19544da2,
345+
0x73e19966, 0x89dcd4d6,
346+
0x1dfab7ae, 0x32ff9c82,
347+
0x679dd514, 0x582f9fcf,
348+
0x0f6d2b69, 0x7bd44da8,
349+
0x77e36f73, 0x04c48942,
350+
0x3f9d85a8, 0x6a1d36c8,
351+
0x1112e6ad, 0x91d692a1,
352+
]));
353+
}
354+
355+
/// The concrete implementation of [Sha512/256].
356+
///
357+
/// This is separate so that it can extend [HashSink] without leaking additional
358+
/// public members.
359+
class Sha512256Sink extends _Sha64BitSink {
360+
@override
361+
final digestBytes = 8;
362+
363+
Sha512256Sink(Sink<Digest> sink)
364+
: super(
365+
sink,
366+
Uint32List.fromList([
367+
// FIPS 180-4, Section 5.3.6.2
368+
0x22312194, 0xfc2bf72c,
369+
0x9f555fa3, 0xc84c64c2,
370+
0x2393b86b, 0x6f53b151,
371+
0x96387719, 0x5940eabd,
372+
0x96283ee2, 0xa88effe3,
373+
0xbe5e1e25, 0x53863992,
374+
0x2b0199fc, 0x2c85b8aa,
375+
0x0eb72ddc, 0x81c52ca2,
376+
]));
377+
}

test/sha_monte_test.dart

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import 'package:crypto/crypto.dart';
55

66
import 'utils.dart';
77

8+
// See https://csrc.nist.gov/Projects/cryptographic-algorithm-validation-program/Secure-Hashing
9+
810
void main() {
911
group('Monte Vectors', () {
1012
monteTest(
@@ -32,27 +34,55 @@ void main() {
3234
],
3335
);
3436
monteTest(
35-
'sha384',
36-
sha384,
37-
'edff07255c71b54a9beae52cdfa083569a08be89949cbba73ddc8acf429359ca5e5be7a673633ca0d9709848f522a9df',
38-
[
39-
'e81b86c49a38feddfd185f71ca7da6732a053ed4a2640d52d27f53f9f76422650b0e93645301ac99f8295d6f820f1035',
40-
'1d6bd21713bffd50946a10c39a7742d740e8f271f0c8f643d4c95375094fd9bf29d89ee61a76053f22e44a4b058a64ed',
41-
'425167b66ae965bd7d68515b54ebfa16f33d2bdb2147a4eac515a75224cd19cea564d692017d2a1c41c1a3f68bb5a209',
42-
'9e7477ffd4baad1fcca035f4687b35ed47a57832fb27d131eb8018fcb41edf4d5e25874466d2e2d61ae3accdfc7aa364',
43-
'd7b4d4e779ca70c8d065630db1f9128ee43b4bde08a81bce13d48659b6ef47b6cfc802af6d8756f6cd43c709bb445bab',
44-
]);
37+
'sha384',
38+
sha384,
39+
'edff07255c71b54a9beae52cdfa083569a08be89949cbba73ddc8acf429359ca5e5be7a673633ca0d9709848f522a9df',
40+
[
41+
'e81b86c49a38feddfd185f71ca7da6732a053ed4a2640d52d27f53f9f76422650b0e93645301ac99f8295d6f820f1035',
42+
'1d6bd21713bffd50946a10c39a7742d740e8f271f0c8f643d4c95375094fd9bf29d89ee61a76053f22e44a4b058a64ed',
43+
'425167b66ae965bd7d68515b54ebfa16f33d2bdb2147a4eac515a75224cd19cea564d692017d2a1c41c1a3f68bb5a209',
44+
'9e7477ffd4baad1fcca035f4687b35ed47a57832fb27d131eb8018fcb41edf4d5e25874466d2e2d61ae3accdfc7aa364',
45+
'd7b4d4e779ca70c8d065630db1f9128ee43b4bde08a81bce13d48659b6ef47b6cfc802af6d8756f6cd43c709bb445bab',
46+
],
47+
);
48+
monteTest(
49+
'sha512',
50+
sha512,
51+
'5c337de5caf35d18ed90b5cddfce001ca1b8ee8602f367e7c24ccca6f893802fb1aca7a3dae32dcd60800a59959bc540d63237876b799229ae71a2526fbc52cd',
52+
[
53+
'ada69add0071b794463c8806a177326735fa624b68ab7bcab2388b9276c036e4eaaff87333e83c81c0bca0359d4aeebcbcfd314c0630e0c2af68c1fb19cc470e',
54+
'ef219b37c24ae507a2b2b26d1add51b31fb5327eb8c3b19b882fe38049433dbeccd63b3d5b99ba2398920bcefb8aca98cd28a1ee5d2aaf139ce58a15d71b06b4',
55+
'c3d5087a62db0e5c6f5755c417f69037308cbce0e54519ea5be8171496cc6d18023ba15768153cfd74c7e7dc103227e9eed4b0f82233362b2a7b1a2cbcda9daf',
56+
'bb3a58f71148116e377505461d65d6c89906481fedfbcfe481b7aa8ceb977d252b3fe21bfff6e7fbf7575ceecf5936bd635e1cf52698c36ef6908ddbd5b6ae05',
57+
'b68f0cd2d63566b3934a50666dec6d62ca1db98e49d7733084c1f86d91a8a08c756fa7ece815e20930dd7cb66351bad8c087c2f94e8757cb98e7f4b86b21a8a8',
58+
],
59+
);
60+
4561
monteTest(
46-
'sha512',
47-
sha512,
48-
'5c337de5caf35d18ed90b5cddfce001ca1b8ee8602f367e7c24ccca6f893802fb1aca7a3dae32dcd60800a59959bc540d63237876b799229ae71a2526fbc52cd',
49-
[
50-
'ada69add0071b794463c8806a177326735fa624b68ab7bcab2388b9276c036e4eaaff87333e83c81c0bca0359d4aeebcbcfd314c0630e0c2af68c1fb19cc470e',
51-
'ef219b37c24ae507a2b2b26d1add51b31fb5327eb8c3b19b882fe38049433dbeccd63b3d5b99ba2398920bcefb8aca98cd28a1ee5d2aaf139ce58a15d71b06b4',
52-
'c3d5087a62db0e5c6f5755c417f69037308cbce0e54519ea5be8171496cc6d18023ba15768153cfd74c7e7dc103227e9eed4b0f82233362b2a7b1a2cbcda9daf',
53-
'bb3a58f71148116e377505461d65d6c89906481fedfbcfe481b7aa8ceb977d252b3fe21bfff6e7fbf7575ceecf5936bd635e1cf52698c36ef6908ddbd5b6ae05',
54-
'b68f0cd2d63566b3934a50666dec6d62ca1db98e49d7733084c1f86d91a8a08c756fa7ece815e20930dd7cb66351bad8c087c2f94e8757cb98e7f4b86b21a8a8',
55-
]);
62+
'sha512/224',
63+
sha512224,
64+
'2e325bf8c98c0be54493d04c329e706343aebe4968fdd33b37da9c0a',
65+
[
66+
'9ee006873962aa0842d636c759646a4ef4b65bcbebcc35430b20f7f4',
67+
'87726eda4570734b396f4c253146ecb9770b8591739240f02a4f2a02',
68+
'7be0871653db5fa514b4ec1a0363df004657155575b0383bc9fdec35',
69+
'7a794a3a1ae255e67ffbf688a05b6aba7f231cebec64b4fc75092d49',
70+
'aaf5d4ecaf9426149821b15821b41c49e3900c0fc91664fb294216ea',
71+
],
72+
);
73+
74+
monteTest(
75+
'sha512/256',
76+
sha512256,
77+
'f41ece2613e4573915696b5adcd51ca328be3bf566a9ca99c9ceb0279c1cb0a7',
78+
[
79+
'b1d97a6536896aa01098fb2b9e15d8692621c84077051fc1f70a8a48baa6dfaf',
80+
'a008d2c5adce31a95b30397ac691d8606c6769a47b801441ba3afb7f727c8a9c',
81+
'8eb896cb2b309db019121eb72564b89c1a59f74d4e2f2f6773c87b98c1997d77',
82+
'ac71b694438cc300dde0f6f9f548d2304e2bdb6ea45e2b305af5fb3e4ec27761',
83+
'd47cca4ae027778fc285bc78fb2a9c1cc7cde498267c35157e86b05fc58e698d',
84+
],
85+
);
5686
});
5787
}
5888

0 commit comments

Comments
 (0)