OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "crypto/ghash.h" | |
6 | |
7 | |
wtc
2012/10/19 21:35:22
Delete this line.
agl
2012/10/22 21:50:56
Done.
| |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 | |
10 using namespace crypto; | |
wtc
2012/10/19 21:35:22
We usually just put the tests inside the |crypto|
agl
2012/10/22 21:50:56
Done.
| |
11 | |
12 namespace { | |
13 | |
14 // Test vectors are taken from Appendix B of | |
15 // http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-sp ec.pdf | |
wtc
2012/10/19 21:35:22
There is a revised version of this file. Search fo
| |
16 | |
17 static const uint8 kKey1[16] = { | |
18 0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b, | |
19 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b, 0x2e, | |
20 }; | |
21 | |
22 static const uint8 kCiphertext2[] = { | |
23 0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92, | |
24 0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe, 0x78, | |
25 }; | |
26 | |
27 static const uint8 kKey3[16] = { | |
28 0xb8, 0x3b, 0x53, 0x37, 0x08, 0xbf, 0x53, 0x5d, | |
29 0x0a, 0xa6, 0xe5, 0x29, 0x80, 0xd5, 0x3b, 0x78, | |
30 }; | |
31 | |
32 static const uint8 kCiphertext3[] = { | |
33 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24, | |
34 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c, | |
35 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0, | |
36 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e, | |
37 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c, | |
38 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05, | |
39 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97, | |
40 0x3d, 0x58, 0xe0, 0x91, 0x47, 0x3f, 0x59, 0x85, | |
41 }; | |
42 | |
43 static const uint8 kAdditional4[] = { | |
44 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, | |
45 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, | |
46 0xab, 0xad, 0xda, 0xd2, | |
47 }; | |
48 | |
49 struct TestCase { | |
50 const uint8* key; | |
51 const uint8* additional; | |
52 unsigned additional_length; | |
53 const uint8* ciphertext; | |
54 unsigned ciphertext_length; | |
55 const uint8 expected[16]; | |
56 }; | |
57 | |
58 static const TestCase kTestCases[] = { | |
59 { | |
60 kKey1, | |
61 NULL, | |
62 0, | |
63 NULL, | |
64 0, | |
65 { | |
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
68 }, | |
69 }, | |
70 { | |
71 kKey1, | |
72 NULL, | |
73 0, | |
74 kCiphertext2, | |
75 sizeof(kCiphertext2), | |
76 { | |
77 0xf3, 0x8c, 0xbb, 0x1a, 0xd6, 0x92, 0x23, 0xdc, | |
78 0xc3, 0x45, 0x7a, 0xe5, 0xb6, 0xb0, 0xf8, 0x85, | |
79 }, | |
80 }, | |
81 { | |
82 kKey3, | |
83 NULL, | |
84 0, | |
85 kCiphertext3, | |
86 sizeof(kCiphertext3), | |
87 { | |
88 0x7f, 0x1b, 0x32, 0xb8, 0x1b, 0x82, 0x0d, 0x02, | |
89 0x61, 0x4f, 0x88, 0x95, 0xac, 0x1d, 0x4e, 0xac, | |
90 }, | |
91 }, | |
92 { | |
93 kKey3, | |
94 kAdditional4, | |
95 sizeof(kAdditional4), | |
96 kCiphertext3, | |
97 sizeof(kCiphertext3) - 4, | |
98 { | |
99 0x69, 0x8e, 0x57, 0xf7, 0x0e, 0x6e, 0xcc, 0x7f, | |
100 0xd9, 0x46, 0x3b, 0x72, 0x60, 0xa9, 0xae, 0x5f, | |
101 }, | |
102 }, | |
103 }; | |
104 | |
105 TEST(GaliosHash, TestCases) { | |
wtc
2012/10/19 21:35:22
Galios => Galois
agl
2012/10/22 21:50:56
Done. (Opps!)
| |
106 uint8 out[16]; | |
107 | |
108 for (size_t i = 0; i < arraysize(kTestCases); ++i) { | |
109 const TestCase& test = kTestCases[i]; | |
110 | |
111 GaliosHash hash(test.key); | |
112 if (test.additional_length) { | |
113 hash.UpdateAdditional(test.additional, test.additional_length); | |
114 } | |
115 if (test.ciphertext_length) { | |
116 hash.UpdateCiphertext(test.ciphertext, test.ciphertext_length); | |
117 } | |
118 hash.Digest(out); | |
119 EXPECT_TRUE(0 == memcmp(out, test.expected, 16)); | |
120 } | |
121 } | |
122 | |
123 TEST(GaliosHash, TestCasesByteAtATime) { | |
124 uint8 out[16]; | |
125 | |
126 for (size_t i = 0; i < arraysize(kTestCases); ++i) { | |
127 const TestCase& test = kTestCases[i]; | |
128 | |
129 GaliosHash hash(test.key); | |
130 for (size_t i = 0; i < test.additional_length; ++i) { | |
131 hash.UpdateAdditional(test.additional + i, 1); | |
132 } | |
133 for (size_t i = 0; i < test.ciphertext_length; ++i) { | |
134 hash.UpdateCiphertext(test.ciphertext + i, 1); | |
135 } | |
wtc
2012/10/19 21:35:22
Nit: omit the curly braces for one-line if or for
agl
2012/10/22 21:50:56
Done.
| |
136 hash.Digest(out); | |
137 EXPECT_TRUE(0 == memcmp(out, test.expected, 16)); | |
138 } | |
139 } | |
140 | |
141 } // anonymous namespace | |
wtc
2012/10/19 21:35:22
Nit: in the code example in the Style Guide, this
agl
2012/10/22 21:50:56
Done.
| |
OLD | NEW |