aboutsummaryrefslogtreecommitdiff
path: root/test/avb_crypto_ops_unittest.cc
blob: 94141c7573ff5414f23d85319201d6829350bb18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <string.h>

#include <gtest/gtest.h>

#include <libavb/avb_sha.h>

#include "avb_unittest_util.h"

namespace avb {

/* These smoke tests are intended to check that the cryptographic operations
 * conform to the AVB interface and not to check the correctness of the
 * cryptograhpy.
 */

TEST(CryptoOpsTest, Sha256) {
  AvbSHA256Ctx ctx;

  /* Compare with
   *
   * $ echo -n foobar |sha256sum
   * c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2 -
   */
  avb_sha256_init(&ctx);
  avb_sha256_update(&ctx, (const uint8_t*)"foobar", 6);
  EXPECT_EQ("c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2",
            mem_to_hexstring(avb_sha256_final(&ctx), AVB_SHA256_DIGEST_SIZE));
}

// Disabled for now because it takes ~30 seconds to run.
TEST(CryptoOpsTest, DISABLED_Sha256Large) {
  AvbSHA256Ctx ctx;

  /* Also check we this works with greater than 4GiB input. Compare with
   *
   * $ dd if=/dev/zero bs=1048576 count=4097 |sha256sum
   * 829816e339ff597ec3ada4c30fc840d3f2298444169d242952a54bcf3fcd7747 -
   */
  const size_t kMebibyte = 1048576;
  uint8_t* megabuf;
  megabuf = new uint8_t[kMebibyte];
  memset((char*)megabuf, '\0', kMebibyte);
  avb_sha256_init(&ctx);
  for (size_t n = 0; n < 4097; n++) {
    avb_sha256_update(&ctx, megabuf, kMebibyte);
  }
  EXPECT_EQ("829816e339ff597ec3ada4c30fc840d3f2298444169d242952a54bcf3fcd7747",
            mem_to_hexstring(avb_sha256_final(&ctx), AVB_SHA256_DIGEST_SIZE));
  delete[] megabuf;
}

TEST(CryptoOpsTest, Sha512) {
  AvbSHA512Ctx ctx;

  /* Compare with
   *
   * $ echo -n foobar |sha512sum
   * 0a50261ebd1a390fed2bf326f2673c145582a6342d523204973d0219337f81616a8069b012587cf5635f6925f1b56c360230c19b273500ee013e030601bf2425
   * -
   */
  avb_sha512_init(&ctx);
  avb_sha512_update(&ctx, (const uint8_t*)"foobar", 6);
  EXPECT_EQ(
      "0a50261ebd1a390fed2bf326f2673c145582a6342d523204973d0219337f81616a8069b0"
      "12587cf5635f6925f1b56c360230c19b273500ee013e030601bf2425",
      mem_to_hexstring(avb_sha512_final(&ctx), AVB_SHA512_DIGEST_SIZE));
}

// Disabled for now because it takes ~30 seconds to run.
TEST(CryptoOpsTest, DISABLED_Sha512Large) {
  AvbSHA512Ctx ctx;

  /* Also check we this works with greater than 4GiB input. Compare with
   *
   * $ dd if=/dev/zero bs=1048576 count=4097 |sha512sum
   * eac1685671cc2060315888746de072398116c0c83b7ee9463f0576e11bfdea9cdd5ddbf291fb3ffc4ee8a1b459c798d9fb9b50b7845e2871c4b1402470aaf4c0
   * -
   */
  const size_t kMebibyte = 1048576;
  uint8_t* megabuf;
  megabuf = new uint8_t[kMebibyte];
  memset((char*)megabuf, '\0', kMebibyte);
  avb_sha512_init(&ctx);
  for (size_t n = 0; n < 4097; n++) {
    avb_sha512_update(&ctx, megabuf, kMebibyte);
  }
  EXPECT_EQ(
      "eac1685671cc2060315888746de072398116c0c83b7ee9463f0576e11bfdea9cdd5ddbf2"
      "91fb3ffc4ee8a1b459c798d9fb9b50b7845e2871c4b1402470aaf4c0",
      mem_to_hexstring(avb_sha512_final(&ctx), AVB_SHA512_DIGEST_SIZE));
  delete[] megabuf;
}

}  // namespace avb