blake2.c
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 <openssl/type_check.h>
18 
19 #include "../internal.h"
20 
21 // https://tools.ietf.org/html/rfc7693#section-2.6
22 static const uint64_t kIV[8] = {
23  UINT64_C(0x6a09e667f3bcc908), UINT64_C(0xbb67ae8584caa73b),
24  UINT64_C(0x3c6ef372fe94f82b), UINT64_C(0xa54ff53a5f1d36f1),
25  UINT64_C(0x510e527fade682d1), UINT64_C(0x9b05688c2b3e6c1f),
26  UINT64_C(0x1f83d9abfb41bd6b), UINT64_C(0x5be0cd19137e2179),
27 };
28 
29 // https://tools.ietf.org/html/rfc7693#section-2.7
30 static const uint8_t kSigma[10 * 16] = {
31  // clang-format off
32  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
33  14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3,
34  11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4,
35  7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8,
36  9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13,
37  2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9,
38  12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11,
39  13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10,
40  6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5,
41  10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0,
42  // clang-format on
43 };
44 
45 // https://tools.ietf.org/html/rfc7693#section-3.1
46 static void blake2b_mix(uint64_t v[16], int a, int b, int c, int d, uint64_t x,
47  uint64_t y) {
48  v[a] = v[a] + v[b] + x;
49  v[d] = CRYPTO_rotr_u64(v[d] ^ v[a], 32);
50  v[c] = v[c] + v[d];
51  v[b] = CRYPTO_rotr_u64(v[b] ^ v[c], 24);
52  v[a] = v[a] + v[b] + y;
53  v[d] = CRYPTO_rotr_u64(v[d] ^ v[a], 16);
54  v[c] = v[c] + v[d];
55  v[b] = CRYPTO_rotr_u64(v[b] ^ v[c], 63);
56 }
57 
58 static void blake2b_transform(
59  BLAKE2B_CTX *b2b,
60  const uint64_t block_words[BLAKE2B_CBLOCK / sizeof(uint64_t)],
61  size_t num_bytes, int is_final_block) {
62  // https://tools.ietf.org/html/rfc7693#section-3.2
63  uint64_t v[16];
64  OPENSSL_STATIC_ASSERT(sizeof(v) == sizeof(b2b->h) + sizeof(kIV), "");
65  OPENSSL_memcpy(v, b2b->h, sizeof(b2b->h));
66  OPENSSL_memcpy(&v[8], kIV, sizeof(kIV));
67 
68  b2b->t_low += num_bytes;
69  if (b2b->t_low < num_bytes) {
70  b2b->t_high++;
71  }
72  v[12] ^= b2b->t_low;
73  v[13] ^= b2b->t_high;
74 
75  if (is_final_block) {
76  v[14] = ~v[14];
77  }
78 
79  for (int round = 0; round < 12; round++) {
80  const uint8_t *const s = &kSigma[16 * (round % 10)];
81  blake2b_mix(v, 0, 4, 8, 12, block_words[s[0]], block_words[s[1]]);
82  blake2b_mix(v, 1, 5, 9, 13, block_words[s[2]], block_words[s[3]]);
83  blake2b_mix(v, 2, 6, 10, 14, block_words[s[4]], block_words[s[5]]);
84  blake2b_mix(v, 3, 7, 11, 15, block_words[s[6]], block_words[s[7]]);
85  blake2b_mix(v, 0, 5, 10, 15, block_words[s[8]], block_words[s[9]]);
86  blake2b_mix(v, 1, 6, 11, 12, block_words[s[10]], block_words[s[11]]);
87  blake2b_mix(v, 2, 7, 8, 13, block_words[s[12]], block_words[s[13]]);
88  blake2b_mix(v, 3, 4, 9, 14, block_words[s[14]], block_words[s[15]]);
89  }
90 
91  for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(b2b->h); i++) {
92  b2b->h[i] ^= v[i];
93  b2b->h[i] ^= v[i + 8];
94  }
95 }
96 
98  OPENSSL_memset(b2b, 0, sizeof(BLAKE2B_CTX));
99 
100  OPENSSL_STATIC_ASSERT(sizeof(kIV) == sizeof(b2b->h), "");
101  OPENSSL_memcpy(&b2b->h, kIV, sizeof(kIV));
102 
103  // https://tools.ietf.org/html/rfc7693#section-2.5
104  b2b->h[0] ^= 0x01010000 | BLAKE2B256_DIGEST_LENGTH;
105 }
106 
107 void BLAKE2B256_Update(BLAKE2B_CTX *b2b, const void *in_data, size_t len) {
108  const uint8_t *data = (const uint8_t *)in_data;
109 
110  size_t todo = sizeof(b2b->block.bytes) - b2b->block_used;
111  if (todo > len) {
112  todo = len;
113  }
114  OPENSSL_memcpy(&b2b->block.bytes[b2b->block_used], data, todo);
115  b2b->block_used += todo;
116  data += todo;
117  len -= todo;
118 
119  if (!len) {
120  return;
121  }
122 
123  // More input remains therefore we must have filled |b2b->block|.
124  assert(b2b->block_used == BLAKE2B_CBLOCK);
126  /*is_final_block=*/0);
127  b2b->block_used = 0;
128 
129  while (len > BLAKE2B_CBLOCK) {
130  uint64_t block_words[BLAKE2B_CBLOCK / sizeof(uint64_t)];
131  OPENSSL_memcpy(block_words, data, sizeof(block_words));
132  blake2b_transform(b2b, block_words, BLAKE2B_CBLOCK, /*is_final_block=*/0);
133  data += BLAKE2B_CBLOCK;
134  len -= BLAKE2B_CBLOCK;
135  }
136 
138  b2b->block_used = len;
139 }
140 
142  OPENSSL_memset(&b2b->block.bytes[b2b->block_used], 0,
143  sizeof(b2b->block.bytes) - b2b->block_used);
144  blake2b_transform(b2b, b2b->block.words, b2b->block_used,
145  /*is_final_block=*/1);
146  OPENSSL_STATIC_ASSERT(BLAKE2B256_DIGEST_LENGTH <= sizeof(b2b->h), "");
148 }
149 
150 void BLAKE2B256(const uint8_t *data, size_t len,
156 }
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
ctx
Definition: benchmark-async.c:30
blake2b_transform
static void blake2b_transform(BLAKE2B_CTX *b2b, const uint64_t block_words[BLAKE2B_CBLOCK/sizeof(uint64_t)], size_t num_bytes, int is_final_block)
Definition: blake2.c:58
kIV
static const uint64_t kIV[8]
Definition: blake2.c:22
y
const double y
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3611
blake2b_state_st::t_high
uint64_t t_high
Definition: blake2.h:30
BLAKE2B256_Update
void BLAKE2B256_Update(BLAKE2B_CTX *b2b, const void *in_data, size_t len)
Definition: blake2.c:107
OPENSSL_ARRAY_SIZE
#define OPENSSL_ARRAY_SIZE(array)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:179
blake2b_mix
static void blake2b_mix(uint64_t v[16], int a, int b, int c, int d, uint64_t x, uint64_t y)
Definition: blake2.c:46
blake2.h
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
blake2b_state_st::bytes
uint8_t bytes[BLAKE2B_CBLOCK]
Definition: blake2.h:32
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
blake2b_state_st::words
uint64_t words[16]
Definition: blake2.h:33
blake2b_state_st
Definition: blake2.h:28
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
blake2b_state_st::h
uint64_t h[8]
Definition: blake2.h:29
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
round
static int round(int n)
Definition: bloaty/third_party/re2/util/benchmark.cc:91
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
BLAKE2B256_Init
void BLAKE2B256_Init(BLAKE2B_CTX *b2b)
Definition: blake2.c:97
blake2b_state_st::block
union blake2b_state_st::@362 block
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
BLAKE2B256_DIGEST_LENGTH
#define BLAKE2B256_DIGEST_LENGTH
Definition: blake2.h:25
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
d
static const fe d
Definition: curve25519_tables.h:19
UINT64_C
#define UINT64_C(val)
Definition: stdint-msvc2008.h:238
blake2b_state_st::t_low
uint64_t t_low
Definition: blake2.h:30
BLAKE2B256
void BLAKE2B256(const uint8_t *data, size_t len, uint8_t out[BLAKE2B256_DIGEST_LENGTH])
Definition: blake2.c:150
BLAKE2B_CBLOCK
#define BLAKE2B_CBLOCK
Definition: blake2.h:26
kSigma
static const uint8_t kSigma[10 *16]
Definition: blake2.c:30
CRYPTO_rotr_u64
static uint64_t CRYPTO_rotr_u64(uint64_t value, int shift)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:923
type_check.h
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
blake2b_state_st::block_used
size_t block_used
Definition: blake2.h:35
mkowners.todo
todo
Definition: mkowners.py:209
BLAKE2B256_Final
void BLAKE2B256_Final(uint8_t out[BLAKE2B256_DIGEST_LENGTH], BLAKE2B_CTX *b2b)
Definition: blake2.c:141
if
if(p->owned &&p->wrapped !=NULL)
Definition: call.c:42
OPENSSL_STATIC_ASSERT
#define OPENSSL_STATIC_ASSERT(cond, msg)
Definition: type_check.h:75
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


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