Embedded SDK
Embedded SDK
lcx_blake2.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  ********************************************************************************/
18 
27 #ifndef LCX_BLAKE2_H
28 #define LCX_BLAKE2_H
29 
30 #ifdef HAVE_BLAKE2
31 
32 #include "lcx_wrappers.h"
33 #include "lcx_hash.h"
34 #include <stddef.h>
35 #include <stdint.h>
36 
38 #define CX_BLAKE2B_256_SIZE 32
39 
41 #define CX_BLAKE2B_512_SIZE 64
42 
44 enum blake2b_constant {
45  BLAKE2B_BLOCKBYTES = 128,
46  BLAKE2B_OUTBYTES = 64,
47  BLAKE2B_KEYBYTES = 64,
48  BLAKE2B_SALTBYTES = 16,
49  BLAKE2B_PERSONALBYTES = 16
50 };
51 
53 struct blake2b_state__ {
54  uint64_t h[8];
55  uint64_t t[2];
56  uint64_t f[2];
57  uint8_t buf[BLAKE2B_BLOCKBYTES];
58  size_t buflen;
59  size_t outlen;
60  uint8_t last_node;
61 };
63 typedef struct blake2b_state__ blake2b_state;
64 
68 struct cx_blake2b_s {
69  struct cx_hash_header_s header;
70  size_t output_size;
71  struct blake2b_state__ ctx;
72 };
74 typedef struct cx_blake2b_s cx_blake2b_t;
75 
88 WARN_UNUSED_RESULT cx_err_t cx_blake2b_init_no_throw(cx_blake2b_t *hash, size_t out_len);
89 
94 DEPRECATED static inline int cx_blake2b_init(cx_blake2b_t *hash, unsigned int out_len)
95 {
96  CX_THROW(cx_blake2b_init_no_throw(hash, out_len));
97  return CX_BLAKE2B;
98 }
99 
112 cx_err_t cx_blake2b_256_hash_iovec(const cx_iovec_t *iovec,
113  size_t iovec_len,
114  uint8_t digest[static CX_BLAKE2B_256_SIZE]);
115 
128 static inline cx_err_t cx_blake2b_256_hash(const uint8_t *in,
129  size_t in_len,
130  uint8_t digest[static CX_BLAKE2B_256_SIZE])
131 {
132  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
133 
134  return cx_blake2b_256_hash_iovec(&iovec, 1, digest);
135 }
136 
149 cx_err_t cx_blake2b_512_hash_iovec(const cx_iovec_t *iovec,
150  size_t iovec_len,
151  uint8_t digest[static CX_BLAKE2B_512_SIZE]);
152 
165 static inline cx_err_t cx_blake2b_512_hash(const uint8_t *in,
166  size_t in_len,
167  uint8_t digest[static CX_BLAKE2B_512_SIZE])
168 {
169  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
170 
171  return cx_blake2b_512_hash_iovec(&iovec, 1, digest);
172 }
173 
195 WARN_UNUSED_RESULT cx_err_t cx_blake2b_init2_no_throw(cx_blake2b_t *hash,
196  size_t out_len,
197  uint8_t *salt,
198  size_t salt_len,
199  uint8_t *perso,
200  size_t perso_len);
201 
206 DEPRECATED static inline int cx_blake2b_init2(cx_blake2b_t *hash,
207  unsigned int out_len,
208  unsigned char *salt,
209  unsigned int salt_len,
210  unsigned char *perso,
211  unsigned int perso_len)
212 {
213  CX_THROW(cx_blake2b_init2_no_throw(hash, out_len, salt, salt_len, perso, perso_len));
214  return CX_BLAKE2B;
215 }
216 
217 #endif // HAVE_BLAKE2
218 
219 #endif // LCX_BLAKE2_H
Hash functions.
#define CX_THROW(call)
Definition: lcx_wrappers.h:15
Similar to libc iovec type.
Definition: lcx_common.h:66
const uint8_t * iov_base
Definition: lcx_common.h:67
unsigned char uint8_t
Definition: usbd_conf.h:53