Home | History | Annotate | Download | only in crypto
      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 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 namespace crypto {
     10 
     11 namespace {
     12 
     13 // Test vectors are taken from Appendix B of
     14 // http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf
     15 
     16 static const uint8 kKey1[16] = {
     17   0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b,
     18   0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b, 0x2e,
     19 };
     20 
     21 static const uint8 kCiphertext2[] = {
     22   0x03, 0x88, 0xda, 0xce, 0x60, 0xb6, 0xa3, 0x92,
     23   0xf3, 0x28, 0xc2, 0xb9, 0x71, 0xb2, 0xfe, 0x78,
     24 };
     25 
     26 static const uint8 kKey3[16] = {
     27   0xb8, 0x3b, 0x53, 0x37, 0x08, 0xbf, 0x53, 0x5d,
     28   0x0a, 0xa6, 0xe5, 0x29, 0x80, 0xd5, 0x3b, 0x78,
     29 };
     30 
     31 static const uint8 kCiphertext3[] = {
     32   0x42, 0x83, 0x1e, 0xc2, 0x21, 0x77, 0x74, 0x24,
     33   0x4b, 0x72, 0x21, 0xb7, 0x84, 0xd0, 0xd4, 0x9c,
     34   0xe3, 0xaa, 0x21, 0x2f, 0x2c, 0x02, 0xa4, 0xe0,
     35   0x35, 0xc1, 0x7e, 0x23, 0x29, 0xac, 0xa1, 0x2e,
     36   0x21, 0xd5, 0x14, 0xb2, 0x54, 0x66, 0x93, 0x1c,
     37   0x7d, 0x8f, 0x6a, 0x5a, 0xac, 0x84, 0xaa, 0x05,
     38   0x1b, 0xa3, 0x0b, 0x39, 0x6a, 0x0a, 0xac, 0x97,
     39   0x3d, 0x58, 0xe0, 0x91, 0x47, 0x3f, 0x59, 0x85,
     40 };
     41 
     42 static const uint8 kAdditional4[] = {
     43   0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
     44   0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
     45   0xab, 0xad, 0xda, 0xd2,
     46 };
     47 
     48 struct TestCase {
     49   const uint8* key;
     50   const uint8* additional;
     51   unsigned additional_length;
     52   const uint8* ciphertext;
     53   unsigned ciphertext_length;
     54   const uint8 expected[16];
     55 };
     56 
     57 static const TestCase kTestCases[] = {
     58   {
     59     kKey1,
     60     NULL,
     61     0,
     62     NULL,
     63     0,
     64     {
     65       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     66       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     67     },
     68   },
     69   {
     70     kKey1,
     71     NULL,
     72     0,
     73     kCiphertext2,
     74     sizeof(kCiphertext2),
     75     {
     76       0xf3, 0x8c, 0xbb, 0x1a, 0xd6, 0x92, 0x23, 0xdc,
     77       0xc3, 0x45, 0x7a, 0xe5, 0xb6, 0xb0, 0xf8, 0x85,
     78     },
     79   },
     80   {
     81     kKey3,
     82     NULL,
     83     0,
     84     kCiphertext3,
     85     sizeof(kCiphertext3),
     86     {
     87       0x7f, 0x1b, 0x32, 0xb8, 0x1b, 0x82, 0x0d, 0x02,
     88       0x61, 0x4f, 0x88, 0x95, 0xac, 0x1d, 0x4e, 0xac,
     89     },
     90   },
     91   {
     92     kKey3,
     93     kAdditional4,
     94     sizeof(kAdditional4),
     95     kCiphertext3,
     96     sizeof(kCiphertext3) - 4,
     97     {
     98       0x69, 0x8e, 0x57, 0xf7, 0x0e, 0x6e, 0xcc, 0x7f,
     99       0xd9, 0x46, 0x3b, 0x72, 0x60, 0xa9, 0xae, 0x5f,
    100     },
    101   },
    102 };
    103 
    104 TEST(GaloisHash, TestCases) {
    105   uint8 out[16];
    106 
    107   for (size_t i = 0; i < arraysize(kTestCases); ++i) {
    108     const TestCase& test = kTestCases[i];
    109 
    110     GaloisHash hash(test.key);
    111     if (test.additional_length)
    112       hash.UpdateAdditional(test.additional, test.additional_length);
    113     if (test.ciphertext_length)
    114       hash.UpdateCiphertext(test.ciphertext, test.ciphertext_length);
    115     hash.Finish(out, sizeof(out));
    116     EXPECT_TRUE(0 == memcmp(out, test.expected, 16));
    117   }
    118 }
    119 
    120 TEST(GaloisHash, VaryLengths) {
    121   uint8 out[16];
    122 
    123   for (size_t chunk_size = 1; chunk_size < 16; chunk_size++) {
    124     for (size_t i = 0; i < arraysize(kTestCases); ++i) {
    125       const TestCase& test = kTestCases[i];
    126 
    127       GaloisHash hash(test.key);
    128       for (size_t i = 0; i < test.additional_length;) {
    129         size_t n = std::min(test.additional_length - i, chunk_size);
    130         hash.UpdateAdditional(test.additional + i, n);
    131         i += n;
    132       }
    133       for (size_t i = 0; i < test.ciphertext_length;) {
    134         size_t n = std::min(test.ciphertext_length - i, chunk_size);
    135         hash.UpdateCiphertext(test.ciphertext + i, n);
    136         i += n;
    137       }
    138       hash.Finish(out, sizeof(out));
    139       EXPECT_TRUE(0 == memcmp(out, test.expected, 16));
    140     }
    141   }
    142 }
    143 
    144 }  // namespace
    145 
    146 }  // namespace crypto
    147