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/secure_hash.h"
      6 
      7 #include "base/basictypes.h"
      8 #include "base/memory/scoped_ptr.h"
      9 #include "base/pickle.h"
     10 #include "crypto/sha2.h"
     11 #include "testing/gtest/include/gtest/gtest.h"
     12 
     13 TEST(SecureHashTest, TestUpdate) {
     14   // Example B.3 from FIPS 180-2: long message.
     15   std::string input3(500000, 'a');  // 'a' repeated half a million times
     16   int expected3[] = { 0xcd, 0xc7, 0x6e, 0x5c,
     17                       0x99, 0x14, 0xfb, 0x92,
     18                       0x81, 0xa1, 0xc7, 0xe2,
     19                       0x84, 0xd7, 0x3e, 0x67,
     20                       0xf1, 0x80, 0x9a, 0x48,
     21                       0xa4, 0x97, 0x20, 0x0e,
     22                       0x04, 0x6d, 0x39, 0xcc,
     23                       0xc7, 0x11, 0x2c, 0xd0 };
     24 
     25   uint8 output3[crypto::kSHA256Length];
     26 
     27   scoped_ptr<crypto::SecureHash> ctx(crypto::SecureHash::Create(
     28       crypto::SecureHash::SHA256));
     29   ctx->Update(input3.data(), input3.size());
     30   ctx->Update(input3.data(), input3.size());
     31 
     32   ctx->Finish(output3, sizeof(output3));
     33   for (size_t i = 0; i < crypto::kSHA256Length; i++)
     34     EXPECT_EQ(expected3[i], static_cast<int>(output3[i]));
     35 }
     36 
     37 // Save the crypto state mid-stream, and create another instance with the
     38 // saved state.  Then feed the same data afterwards to both.
     39 // When done, both should have the same hash value.
     40 TEST(SecureHashTest, TestSerialization) {
     41   std::string input1(10001, 'a');  // 'a' repeated 10001 times
     42   std::string input2(10001, 'b');  // 'b' repeated 10001 times
     43   std::string input3(10001, 'c');  // 'c' repeated 10001 times
     44   std::string input4(10001, 'd');  // 'd' repeated 10001 times
     45   std::string input5(10001, 'e');  // 'e' repeated 10001 times
     46 
     47   uint8 output1[crypto::kSHA256Length];
     48   uint8 output2[crypto::kSHA256Length];
     49 
     50   scoped_ptr<crypto::SecureHash> ctx1(crypto::SecureHash::Create(
     51       crypto::SecureHash::SHA256));
     52   scoped_ptr<crypto::SecureHash> ctx2(crypto::SecureHash::Create(
     53       crypto::SecureHash::SHA256));
     54   Pickle pickle;
     55   ctx1->Update(input1.data(), input1.size());
     56   ctx1->Update(input2.data(), input2.size());
     57   ctx1->Update(input3.data(), input3.size());
     58 
     59   EXPECT_TRUE(ctx1->Serialize(&pickle));
     60   ctx1->Update(input4.data(), input4.size());
     61   ctx1->Update(input5.data(), input5.size());
     62 
     63   ctx1->Finish(output1, sizeof(output1));
     64 
     65   PickleIterator data_iterator(pickle);
     66   EXPECT_TRUE(ctx2->Deserialize(&data_iterator));
     67   ctx2->Update(input4.data(), input4.size());
     68   ctx2->Update(input5.data(), input5.size());
     69 
     70   ctx2->Finish(output2, sizeof(output2));
     71 
     72   EXPECT_EQ(0, memcmp(output1, output2, crypto::kSHA256Length));
     73 }
     74