blake2_test.cc
Go to the documentation of this file.
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 
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 
32  BLAKE2B256((const uint8_t *)"abc", 3, digest);
33  EXPECT_EQ(Bytes(kExpected), Bytes(digest));
34 }
35 
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 
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 }
Bytes
Definition: boringssl-with-bazel/src/crypto/test/test_util.h:38
BLAKE2B256_Init
#define BLAKE2B256_Init
Definition: boringssl_prefix_symbols.h:875
blake2.h
BLAKE2B256_Update
#define BLAKE2B256_Update
Definition: boringssl_prefix_symbols.h:876
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
blake2b_state_st
Definition: blake2.h:28
FileTestGTest
void FileTestGTest(const char *path, std::function< void(FileTest *)> run_test)
Definition: file_test_gtest.cc:68
FileTest
Definition: file_test.h:90
BLAKE2B256_DIGEST_LENGTH
#define BLAKE2B256_DIGEST_LENGTH
Definition: blake2.h:25
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
TEST
TEST(BLAKE2B256Test, ABC)
Definition: blake2_test.cc:22
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
BLAKE2B256_Final
#define BLAKE2B256_Final
Definition: boringssl_prefix_symbols.h:874
BLAKE2B256
#define BLAKE2B256
Definition: boringssl_prefix_symbols.h:873


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:39