1 // Copyright 2021 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
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