• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*############################################################################
2   # Copyright 2016-2017 Intel Corporation
3   #
4   # Licensed under the Apache License, Version 2.0 (the "License");
5   # you may not use this file except in compliance with the License.
6   # You may obtain a copy of the License at
7   #
8   #     http://www.apache.org/licenses/LICENSE-2.0
9   #
10   # Unless required by applicable law or agreed to in writing, software
11   # distributed under the License is distributed on an "AS IS" BASIS,
12   # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   # See the License for the specific language governing permissions and
14   # limitations under the License.
15   ############################################################################*/
16 
17 /*!
18  * \file
19  * \brief Hash unit tests.
20  */
21 
22 #include <cstring>
23 #include <limits>
24 #include "epid/common-testhelper/epid_gtest-testhelper.h"
25 #include "gtest/gtest.h"
26 
27 #include "epid/common-testhelper/errors-testhelper.h"
28 
29 extern "C" {
30 #include "epid/common/math/hash.h"
31 }
32 
33 /// compares Sha256Digest values
operator ==(Sha256Digest const & lhs,Sha256Digest const & rhs)34 bool operator==(Sha256Digest const& lhs, Sha256Digest const& rhs) {
35   return 0 == std::memcmp(&lhs, &rhs, sizeof(lhs));
36 }
37 
38 namespace {
39 
40 ///////////////////////////////////////////////////////////////////////
41 // SHA256
TEST(Hash,Sha256MessageDigestFailsGivenNullPtr)42 TEST(Hash, Sha256MessageDigestFailsGivenNullPtr) {
43   char msg[] = "abc";
44   Sha256Digest digest;
45 
46   EXPECT_EQ(kEpidBadArgErr,
47             Sha256MessageDigest(nullptr, sizeof(msg) - 1, &digest));
48   EXPECT_EQ(kEpidBadArgErr, Sha256MessageDigest(msg, sizeof(msg) - 1, nullptr));
49 }
50 
TEST(Hash,Sha256MessageDigestFailsGivenInvalidBufferSize)51 TEST(Hash, Sha256MessageDigestFailsGivenInvalidBufferSize) {
52   char msg[] = "abc";
53   Sha256Digest digest;
54 
55   EXPECT_EQ(
56       kEpidBadArgErr,
57       Sha256MessageDigest(msg, std::numeric_limits<size_t>::max(), &digest));
58 #if (SIZE_MAX >= 0x100000001)  // When size_t value allowed to be 0x100000001
59   EXPECT_EQ(kEpidBadArgErr, Sha256MessageDigest(msg, 0x100000001, &digest));
60 #endif
61 }
62 
TEST(Hash,Sha256MessageDigestComputesCorrectDigest)63 TEST(Hash, Sha256MessageDigestComputesCorrectDigest) {
64   // Test vectors here are taken from
65   // http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA256.pdf
66 
67   Sha256Digest digest;
68 
69   char msg_abc[] = "abc";
70   Sha256Digest digest_abc = {{0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
71                               0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
72                               0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
73                               0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD}};
74   EXPECT_EQ(kEpidNoErr,
75             Sha256MessageDigest(msg_abc, sizeof(msg_abc) - 1, &digest));
76   EXPECT_EQ(digest_abc, digest);
77 
78   char msg_long[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
79   Sha256Digest digest_long = {{0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
80                                0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
81                                0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
82                                0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1}};
83   EXPECT_EQ(kEpidNoErr,
84             Sha256MessageDigest(msg_long, sizeof(msg_long) - 1, &digest));
85   EXPECT_EQ(digest_long, digest);
86 }
87 
88 }  // namespace
89