xref: /aosp_15_r20/external/boringssl/src/crypto/blake2/blake2_test.cc (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Copyright (c) 2021, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <openssl/blake2.h>
16 
17 #include <gtest/gtest.h>
18 
19 #include "../test/file_test.h"
20 #include "../test/test_util.h"
21 
TEST(BLAKE2B256Test,ABC)22 TEST(BLAKE2B256Test, ABC) {
23   // https://tools.ietf.org/html/rfc7693#appendix-A, except updated for the
24   // 256-bit hash output.
25   const uint8_t kExpected[] = {
26       0xbd, 0xdd, 0x81, 0x3c, 0x63, 0x42, 0x39, 0x72, 0x31, 0x71, 0xef,
27       0x3f, 0xee, 0x98, 0x57, 0x9b, 0x94, 0x96, 0x4e, 0x3b, 0xb1, 0xcb,
28       0x3e, 0x42, 0x72, 0x62, 0xc8, 0xc0, 0x68, 0xd5, 0x23, 0x19,
29   };
30 
31   uint8_t digest[BLAKE2B256_DIGEST_LENGTH];
32   BLAKE2B256((const uint8_t *)"abc", 3, digest);
33   EXPECT_EQ(Bytes(kExpected), Bytes(digest));
34 }
35 
TEST(BLAKE2B256Test,TestVectors)36 TEST(BLAKE2B256Test, TestVectors) {
37   FileTestGTest("crypto/blake2/blake2b256_tests.txt", [](FileTest *t) {
38     std::vector<uint8_t> msg, expected;
39     ASSERT_TRUE(t->GetBytes(&msg, "IN"));
40     ASSERT_TRUE(t->GetBytes(&expected, "HASH"));
41 
42     uint8_t digest[BLAKE2B256_DIGEST_LENGTH];
43     BLAKE2B256(msg.data(), msg.size(), digest);
44     EXPECT_EQ(Bytes(digest), Bytes(expected)) << msg.size();
45 
46     OPENSSL_memset(digest, 0, sizeof(digest));
47     BLAKE2B_CTX b2b;
48     BLAKE2B256_Init(&b2b);
49     for (uint8_t b : msg) {
50       BLAKE2B256_Update(&b2b, &b, 1);
51     }
52     BLAKE2B256_Final(digest, &b2b);
53     EXPECT_EQ(Bytes(digest), Bytes(expected)) << msg.size();
54   });
55 }
56