Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
cx_blake3_ref.h
Go to the documentation of this file.
1#ifdef HAVE_BLAKE3
2
3#ifndef CX_BLAKE3_REF_H
4#define CX_BLAKE3_REF_H
5
6#include "cx_blake3.h"
7#include <stddef.h>
8#include <stdint.h>
9#include <string.h>
10
11#define INLINE static inline __attribute__((always_inline))
12
13INLINE uint32_t load32(const void *src)
14{
15#if defined(NATIVE_LITTLE_ENDIAN)
16 uint32_t w;
17 memcpy(&w, src, sizeof w);
18 return w;
19#else
20 const uint8_t *p = (const uint8_t *) src;
21 return ((uint32_t) (p[0]) << 0) | ((uint32_t) (p[1]) << 8) | ((uint32_t) (p[2]) << 16)
22 | ((uint32_t) (p[3]) << 24);
23#endif
24}
25
26INLINE uint32_t rotr32(uint32_t w, uint32_t c)
27{
28 return (w >> c) | (w << (32 - c));
29}
30
31INLINE void load_key_words(const uint8_t key[BLAKE3_KEY_LEN], uint32_t key_words[8])
32{
33 key_words[0] = load32(&key[0 * 4]);
34 key_words[1] = load32(&key[1 * 4]);
35 key_words[2] = load32(&key[2 * 4]);
36 key_words[3] = load32(&key[3 * 4]);
37 key_words[4] = load32(&key[4 * 4]);
38 key_words[5] = load32(&key[5 * 4]);
39 key_words[6] = load32(&key[6 * 4]);
40 key_words[7] = load32(&key[7 * 4]);
41}
42
43INLINE void store32(void *dst, uint32_t w)
44{
45#if defined(NATIVE_LITTLE_ENDIAN)
46 memcpy(dst, &w, sizeof w);
47#else
48 uint8_t *p = (uint8_t *) dst;
49 p[0] = (uint8_t) (w >> 0);
50 p[1] = (uint8_t) (w >> 8);
51 p[2] = (uint8_t) (w >> 16);
52 p[3] = (uint8_t) (w >> 24);
53#endif
54}
55
56INLINE void store_cv_words(uint8_t bytes_out[32], uint32_t cv_words[8])
57{
58 store32(&bytes_out[0 * 4], cv_words[0]);
59 store32(&bytes_out[1 * 4], cv_words[1]);
60 store32(&bytes_out[2 * 4], cv_words[2]);
61 store32(&bytes_out[3 * 4], cv_words[3]);
62 store32(&bytes_out[4 * 4], cv_words[4]);
63 store32(&bytes_out[5 * 4], cv_words[5]);
64 store32(&bytes_out[6 * 4], cv_words[6]);
65 store32(&bytes_out[7 * 4], cv_words[7]);
66}
67
68static unsigned int highest_one(uint64_t x)
69{
70 unsigned int c = 0;
71 if (x & 0xffffffff00000000ULL) {
72 x >>= 32;
73 c += 32;
74 }
75 if (x & 0x00000000ffff0000ULL) {
76 x >>= 16;
77 c += 16;
78 }
79 if (x & 0x000000000000ff00ULL) {
80 x >>= 8;
81 c += 8;
82 }
83 if (x & 0x00000000000000f0ULL) {
84 x >>= 4;
85 c += 4;
86 }
87 if (x & 0x000000000000000cULL) {
88 x >>= 2;
89 c += 2;
90 }
91 if (x & 0x0000000000000002ULL) {
92 c += 1;
93 }
94
95 return c;
96}
97
98// The Hamming weight of x, i.e. the number of 1 in the binary representation
99INLINE unsigned int hw(uint64_t x)
100{
101 unsigned int count = 0;
102 while (x) {
103 count += 1;
104 x &= x - 1;
105 }
106 return count;
107}
108
120void blake3_state_init(cx_blake3_state_t *chunk_state, const uint32_t *key, uint8_t flags);
121
131void blake3_state_update(cx_blake3_state_t *chunk_state, const uint8_t *input, size_t input_len);
132
143void blake3_state_reset(cx_blake3_state_t *chunk_state,
144 const uint32_t *key,
145 uint64_t chunk_counter);
146
161cx_blake3_state_out_t blake3_state_output(const cx_blake3_state_t *chunk_state);
162
174void blake3_output_chain(const cx_blake3_state_out_t *out, uint8_t *cv);
175
193void blake3_compress_subtree_to_parent(const uint8_t *input,
194 size_t input_len,
195 const uint32_t *key,
196 uint64_t chunk_counter,
197 uint8_t flags,
198 uint8_t *out);
199
207void blake3_hasher_merge_cv(cx_blake3_t *hash, uint64_t total_len);
208
218void blake3_hasher_push_cv(cx_blake3_t *hash, uint8_t *new_cv, uint64_t chunk_counter);
219
230void blake3_output_root_bytes(const cx_blake3_state_out_t *chunk_out, uint8_t *out, size_t out_len);
231
244void blake3_init_ctx(cx_blake3_t *hash, const uint32_t *key, uint8_t mode);
245
246#endif // CX_BLAKE3_REF_H
247#endif // HAVE_BLAKE3
unsigned char uint8_t
Definition usbd_conf.h:53