| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "crypto/ghash.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 5 #include <algorithm> | 10 #include <algorithm> |
| 6 | 11 |
| 7 #include "crypto/ghash.h" | 12 #include "base/macros.h" |
| 8 | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 14 |
| 11 namespace crypto { | 15 namespace crypto { |
| 12 | 16 |
| 13 namespace { | 17 namespace { |
| 14 | 18 |
| 15 // Test vectors are taken from Appendix B of | 19 // Test vectors are taken from Appendix B of |
| 16 // http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-re
vised-spec.pdf | 20 // http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-re
vised-spec.pdf |
| 17 | 21 |
| 18 static const uint8 kKey1[16] = { | 22 static const uint8_t kKey1[16] = { |
| 19 0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b, | 23 0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b, |
| 20 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b, 0x2e, | 24 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b, 0x2e, |
| 21 }; | 25 }; |
| 22 | 26 |
| 23 static const uint8 kCiphertext2[] = { | 27 static const uint8_t kCiphertext2[] = { |
| 24 0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92, | 28 0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92, |
| 25 0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe, 0x78, | 29 0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe, 0x78, |
| 26 }; | 30 }; |
| 27 | 31 |
| 28 static const uint8 kKey3[16] = { | 32 static const uint8_t kKey3[16] = { |
| 29 0xb8, 0x3b, 0x53, 0x37, 0x08, 0xbf, 0x53, 0x5d, | 33 0xb8, 0x3b, 0x53, 0x37, 0x08, 0xbf, 0x53, 0x5d, |
| 30 0x0a, 0xa6, 0xe5, 0x29, 0x80, 0xd5, 0x3b, 0x78, | 34 0x0a, 0xa6, 0xe5, 0x29, 0x80, 0xd5, 0x3b, 0x78, |
| 31 }; | 35 }; |
| 32 | 36 |
| 33 static const uint8 kCiphertext3[] = { | 37 static const uint8_t kCiphertext3[] = { |
| 34 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24, | 38 0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24, 0x4b, 0x72, 0x21, |
| 35 0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c, | 39 0xb7, 0x84, 0xd0, 0xd4, 0x9c, 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, |
| 36 0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0, | 40 0xa4, 0xe0, 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e, 0x21, |
| 37 0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e, | 41 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c, 0x7d, 0x8f, 0x6a, 0x5a, |
| 38 0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c, | 42 0xac, 0x84, 0xaa, 0x05, 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, |
| 39 0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05, | 43 0x97, 0x3d, 0x58, 0xe0, 0x91, 0x47, 0x3f, 0x59, 0x85, |
| 40 0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97, | |
| 41 0x3d, 0x58, 0xe0, 0x91, 0x47, 0x3f, 0x59, 0x85, | |
| 42 }; | 44 }; |
| 43 | 45 |
| 44 static const uint8 kAdditional4[] = { | 46 static const uint8_t kAdditional4[] = { |
| 45 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, | 47 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed, |
| 46 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, | 48 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef, 0xab, 0xad, 0xda, 0xd2, |
| 47 0xab, 0xad, 0xda, 0xd2, | |
| 48 }; | 49 }; |
| 49 | 50 |
| 50 struct TestCase { | 51 struct TestCase { |
| 51 const uint8* key; | 52 const uint8_t* key; |
| 52 const uint8* additional; | 53 const uint8_t* additional; |
| 53 unsigned additional_length; | 54 unsigned additional_length; |
| 54 const uint8* ciphertext; | 55 const uint8_t* ciphertext; |
| 55 unsigned ciphertext_length; | 56 unsigned ciphertext_length; |
| 56 const uint8 expected[16]; | 57 const uint8_t expected[16]; |
| 57 }; | 58 }; |
| 58 | 59 |
| 59 static const TestCase kTestCases[] = { | 60 static const TestCase kTestCases[] = { |
| 60 { | 61 { |
| 61 kKey1, | 62 kKey1, |
| 62 NULL, | 63 NULL, |
| 63 0, | 64 0, |
| 64 NULL, | 65 NULL, |
| 65 0, | 66 0, |
| 66 { | 67 { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 97 kCiphertext3, | 98 kCiphertext3, |
| 98 sizeof(kCiphertext3) - 4, | 99 sizeof(kCiphertext3) - 4, |
| 99 { | 100 { |
| 100 0x69, 0x8e, 0x57, 0xf7, 0x0e, 0x6e, 0xcc, 0x7f, | 101 0x69, 0x8e, 0x57, 0xf7, 0x0e, 0x6e, 0xcc, 0x7f, |
| 101 0xd9, 0x46, 0x3b, 0x72, 0x60, 0xa9, 0xae, 0x5f, | 102 0xd9, 0x46, 0x3b, 0x72, 0x60, 0xa9, 0xae, 0x5f, |
| 102 }, | 103 }, |
| 103 }, | 104 }, |
| 104 }; | 105 }; |
| 105 | 106 |
| 106 TEST(GaloisHash, TestCases) { | 107 TEST(GaloisHash, TestCases) { |
| 107 uint8 out[16]; | 108 uint8_t out[16]; |
| 108 | 109 |
| 109 for (size_t i = 0; i < arraysize(kTestCases); ++i) { | 110 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
| 110 const TestCase& test = kTestCases[i]; | 111 const TestCase& test = kTestCases[i]; |
| 111 | 112 |
| 112 GaloisHash hash(test.key); | 113 GaloisHash hash(test.key); |
| 113 if (test.additional_length) | 114 if (test.additional_length) |
| 114 hash.UpdateAdditional(test.additional, test.additional_length); | 115 hash.UpdateAdditional(test.additional, test.additional_length); |
| 115 if (test.ciphertext_length) | 116 if (test.ciphertext_length) |
| 116 hash.UpdateCiphertext(test.ciphertext, test.ciphertext_length); | 117 hash.UpdateCiphertext(test.ciphertext, test.ciphertext_length); |
| 117 hash.Finish(out, sizeof(out)); | 118 hash.Finish(out, sizeof(out)); |
| 118 EXPECT_TRUE(0 == memcmp(out, test.expected, 16)); | 119 EXPECT_TRUE(0 == memcmp(out, test.expected, 16)); |
| 119 } | 120 } |
| 120 } | 121 } |
| 121 | 122 |
| 122 TEST(GaloisHash, VaryLengths) { | 123 TEST(GaloisHash, VaryLengths) { |
| 123 uint8 out[16]; | 124 uint8_t out[16]; |
| 124 | 125 |
| 125 for (size_t chunk_size = 1; chunk_size < 16; chunk_size++) { | 126 for (size_t chunk_size = 1; chunk_size < 16; chunk_size++) { |
| 126 for (size_t i = 0; i < arraysize(kTestCases); ++i) { | 127 for (size_t i = 0; i < arraysize(kTestCases); ++i) { |
| 127 const TestCase& test = kTestCases[i]; | 128 const TestCase& test = kTestCases[i]; |
| 128 | 129 |
| 129 GaloisHash hash(test.key); | 130 GaloisHash hash(test.key); |
| 130 for (size_t i = 0; i < test.additional_length;) { | 131 for (size_t i = 0; i < test.additional_length;) { |
| 131 size_t n = std::min(test.additional_length - i, chunk_size); | 132 size_t n = std::min(test.additional_length - i, chunk_size); |
| 132 hash.UpdateAdditional(test.additional + i, n); | 133 hash.UpdateAdditional(test.additional + i, n); |
| 133 i += n; | 134 i += n; |
| 134 } | 135 } |
| 135 for (size_t i = 0; i < test.ciphertext_length;) { | 136 for (size_t i = 0; i < test.ciphertext_length;) { |
| 136 size_t n = std::min(test.ciphertext_length - i, chunk_size); | 137 size_t n = std::min(test.ciphertext_length - i, chunk_size); |
| 137 hash.UpdateCiphertext(test.ciphertext + i, n); | 138 hash.UpdateCiphertext(test.ciphertext + i, n); |
| 138 i += n; | 139 i += n; |
| 139 } | 140 } |
| 140 hash.Finish(out, sizeof(out)); | 141 hash.Finish(out, sizeof(out)); |
| 141 EXPECT_TRUE(0 == memcmp(out, test.expected, 16)); | 142 EXPECT_TRUE(0 == memcmp(out, test.expected, 16)); |
| 142 } | 143 } |
| 143 } | 144 } |
| 144 } | 145 } |
| 145 | 146 |
| 146 } // namespace | 147 } // namespace |
| 147 | 148 |
| 148 } // namespace crypto | 149 } // namespace crypto |
| OLD | NEW |