1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <string.h>
26
27 #include <gtest/gtest.h>
28
29 #include <libavb/avb_sha.h>
30
31 #include "avb_unittest_util.h"
32
33 namespace avb {
34
35 /* These smoke tests are intended to check that the cryptographic operations
36 * conform to the AVB interface and not to check the correctness of the
37 * cryptograhpy.
38 */
39
TEST(CryptoOpsTest,Sha256)40 TEST(CryptoOpsTest, Sha256) {
41 AvbSHA256Ctx ctx;
42
43 /* Compare with
44 *
45 * $ echo -n foobar |sha256sum
46 * c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2 -
47 */
48 avb_sha256_init(&ctx);
49 avb_sha256_update(&ctx, (const uint8_t*)"foobar", 6);
50 EXPECT_EQ("c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2",
51 mem_to_hexstring(avb_sha256_final(&ctx), AVB_SHA256_DIGEST_SIZE));
52 }
53
54 // Disabled for now because it takes ~30 seconds to run.
TEST(CryptoOpsTest,DISABLED_Sha256Large)55 TEST(CryptoOpsTest, DISABLED_Sha256Large) {
56 AvbSHA256Ctx ctx;
57
58 /* Also check we this works with greater than 4GiB input. Compare with
59 *
60 * $ dd if=/dev/zero bs=1048576 count=4097 |sha256sum
61 * 829816e339ff597ec3ada4c30fc840d3f2298444169d242952a54bcf3fcd7747 -
62 */
63 const size_t kMebibyte = 1048576;
64 uint8_t* megabuf;
65 megabuf = new uint8_t[kMebibyte];
66 memset((char*)megabuf, '\0', kMebibyte);
67 avb_sha256_init(&ctx);
68 for (size_t n = 0; n < 4097; n++) {
69 avb_sha256_update(&ctx, megabuf, kMebibyte);
70 }
71 EXPECT_EQ("829816e339ff597ec3ada4c30fc840d3f2298444169d242952a54bcf3fcd7747",
72 mem_to_hexstring(avb_sha256_final(&ctx), AVB_SHA256_DIGEST_SIZE));
73 delete[] megabuf;
74 }
75
TEST(CryptoOpsTest,Sha512)76 TEST(CryptoOpsTest, Sha512) {
77 AvbSHA512Ctx ctx;
78
79 /* Compare with
80 *
81 * $ echo -n foobar |sha512sum
82 * 0a50261ebd1a390fed2bf326f2673c145582a6342d523204973d0219337f81616a8069b012587cf5635f6925f1b56c360230c19b273500ee013e030601bf2425
83 * -
84 */
85 avb_sha512_init(&ctx);
86 avb_sha512_update(&ctx, (const uint8_t*)"foobar", 6);
87 EXPECT_EQ(
88 "0a50261ebd1a390fed2bf326f2673c145582a6342d523204973d0219337f81616a8069b0"
89 "12587cf5635f6925f1b56c360230c19b273500ee013e030601bf2425",
90 mem_to_hexstring(avb_sha512_final(&ctx), AVB_SHA512_DIGEST_SIZE));
91 }
92
93 // Disabled for now because it takes ~30 seconds to run.
TEST(CryptoOpsTest,DISABLED_Sha512Large)94 TEST(CryptoOpsTest, DISABLED_Sha512Large) {
95 AvbSHA512Ctx ctx;
96
97 /* Also check we this works with greater than 4GiB input. Compare with
98 *
99 * $ dd if=/dev/zero bs=1048576 count=4097 |sha512sum
100 * eac1685671cc2060315888746de072398116c0c83b7ee9463f0576e11bfdea9cdd5ddbf291fb3ffc4ee8a1b459c798d9fb9b50b7845e2871c4b1402470aaf4c0
101 * -
102 */
103 const size_t kMebibyte = 1048576;
104 uint8_t* megabuf;
105 megabuf = new uint8_t[kMebibyte];
106 memset((char*)megabuf, '\0', kMebibyte);
107 avb_sha512_init(&ctx);
108 for (size_t n = 0; n < 4097; n++) {
109 avb_sha512_update(&ctx, megabuf, kMebibyte);
110 }
111 EXPECT_EQ(
112 "eac1685671cc2060315888746de072398116c0c83b7ee9463f0576e11bfdea9cdd5ddbf2"
113 "91fb3ffc4ee8a1b459c798d9fb9b50b7845e2871c4b1402470aaf4c0",
114 mem_to_hexstring(avb_sha512_final(&ctx), AVB_SHA512_DIGEST_SIZE));
115 delete[] megabuf;
116 }
117
118 } // namespace avb
119