Embedded SDK
Embedded SDK
lcx_blake3.h
Go to the documentation of this file.
1 
2 /*******************************************************************************
3  * Ledger Nano S - Secure firmware
4  * (c) 2022 Ledger
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  ********************************************************************************/
33 #ifdef HAVE_BLAKE3
34 #ifndef LCX_BLAKE3_H
35 #define LCX_BLAKE3_H
36 
37 #include "lcx_hash.h"
38 #include "lcx_wrappers.h"
39 #include <stddef.h>
40 #include <stdint.h>
41 
42 // Not used within the source code but defines the stack depth.
43 // The stack depth is equal to log(BLAKE3_MAX_INPUT_LEN / BLAKE3_CHUNK_LEN)
44 // i.e. it is the maximal number of chunks that can be stored within the cv stack.
45 // The default input length is at most (2**64 - 1) but due to memory constraints
46 // it is set to (2**20 - 1)
47 #define BLAKE3_MAX_INPUT_LEN_POWER 20
48 #define BLAKE3_MAX_INPUT_LEN (1 << BLAKE3_MAX_INPUT_LEN_POWER)
49 
50 #define BLAKE3_BLOCK_LEN 64
51 #define BLAKE3_OUT_LEN 32
52 #define BLAKE3_MAX_DEPTH (BLAKE3_MAX_INPUT_LEN_POWER - 10)
53 #define BLAKE3_MAX_STACK_SIZE (BLAKE3_MAX_DEPTH + 1) * BLAKE3_OUT_LEN
54 #define BLAKE3_WORD_SIZE 32
55 #define BLAKE3_NB_OF_WORDS 8
56 
60 typedef struct {
61  uint32_t cv[BLAKE3_NB_OF_WORDS];
62  uint64_t t;
63  uint8_t buffer[BLAKE3_BLOCK_LEN];
64  uint8_t buffer_len;
65  uint8_t blocks_compressed;
66  uint8_t d;
67 } cx_blake3_state_t;
68 
72 typedef struct {
73  uint32_t key[BLAKE3_NB_OF_WORDS];
74  // the stack contains at most (BLAKE3_MAX_DEPTH + 1)
75  // 32-byte chaining values
76  uint8_t cv_stack[BLAKE3_MAX_STACK_SIZE];
77  uint8_t cv_stack_len;
78  // contains the 32-byte chaining value (CV)
79  // of the previous block,
80  // the 64-byte input buffer for the next block
81  // the counter t and the flags d
82  cx_blake3_state_t chunk;
83  bool is_init;
84 } cx_blake3_t;
85 
104 WARN_UNUSED_RESULT cx_err_t cx_blake3_init(cx_blake3_t *hash,
105  uint8_t mode,
106  const unsigned char *key,
107  const void *context,
108  unsigned int context_len);
109 
127 WARN_UNUSED_RESULT cx_err_t cx_blake3(cx_blake3_t *hash,
128  uint8_t mode,
129  const void *input,
130  size_t input_len,
131  uint8_t *out,
132  size_t out_len);
133 
145 WARN_UNUSED_RESULT cx_err_t cx_blake3_update(cx_blake3_t *hash,
146  const void *input,
147  size_t input_len);
148 
160 WARN_UNUSED_RESULT cx_err_t cx_blake3_final(cx_blake3_t *hash, uint8_t *output, size_t out_len);
161 
162 #endif // LCX_BLAKE3_H
163 #endif // HAVE_BLAKE3
Hash functions.
unsigned char uint8_t
Definition: usbd_conf.h:53