Embedded SDK
Embedded SDK
lcx_hash.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 
39 #ifndef LCX_HASH_H
40 #define LCX_HASH_H
41 
42 #ifdef HAVE_HASH
43 
44 #include "cx_errors.h"
45 #include "lcx_wrappers.h"
46 #include "lcx_common.h"
47 #include <stdbool.h>
48 #include <stddef.h>
49 #include <stdint.h>
50 
52 enum cx_md_e {
53  CX_NONE = 0,
54  // 20 bytes
55  CX_RIPEMD160 = 1,
56  // 28 bytes
57  CX_SHA224 = 2,
58  // 32 bytes
59  CX_SHA256 = 3,
60  // 48 bytes
61  CX_SHA384 = 4,
62  // 64 bytes
63  CX_SHA512 = 5,
64  // 28,32,48,64 bytes
65  CX_KECCAK = 6,
66  // 28,32,48,64 bytes
67  CX_SHA3 = 7,
68  DEPRECATED_0 = 8,
69  CX_BLAKE2B = 9,
70  // any bytes
71  CX_SHAKE128 = 10,
72  // any bytes
73  CX_SHAKE256 = 11,
74  CX_SHA3_256 = 12,
75  CX_SHA3_512 = 13,
76 };
78 typedef enum cx_md_e cx_md_t;
79 
84 #define CX_HASH_MAX_BLOCK_COUNT 65535
85 
87 typedef struct cx_hash_header_s cx_hash_t;
88 
92 typedef struct {
93  cx_md_t md_type;
94  size_t output_size;
95  size_t block_size;
96  size_t ctx_size;
97  cx_err_t (*init_func)(cx_hash_t *ctx);
98  cx_err_t (*update_func)(cx_hash_t *ctx,
99  const uint8_t *data,
100  size_t len);
101  cx_err_t (*finish_func)(cx_hash_t *ctx, uint8_t *digest);
102  cx_err_t (*init_ex_func)(
103  cx_hash_t *ctx,
104  size_t output_size);
105  size_t (*output_size_func)(const cx_hash_t *ctx);
106 } cx_hash_info_t;
107 
111 struct cx_hash_header_s {
112  const cx_hash_info_t *info;
113  uint32_t counter;
114 };
115 
116 size_t cx_hash_get_size(const cx_hash_t *ctx);
117 
146 WARN_UNUSED_RESULT cx_err_t cx_hash_no_throw(cx_hash_t *hash,
147  uint32_t mode,
148  const uint8_t *in,
149  size_t len,
150  uint8_t *out,
151  size_t out_len);
152 
157 DEPRECATED static inline size_t cx_hash(cx_hash_t *hash,
158  uint32_t mode,
159  const unsigned char *in,
160  unsigned int len,
161  unsigned char *out,
162  unsigned int out_len)
163 {
164  CX_THROW(cx_hash_no_throw(hash, mode, in, len, out, out_len));
165  return cx_hash_get_size(hash);
166 }
167 
180 WARN_UNUSED_RESULT cx_err_t cx_hash_init(cx_hash_t *hash, cx_md_t hash_id);
181 
202 WARN_UNUSED_RESULT cx_err_t cx_hash_init_ex(cx_hash_t *hash, cx_md_t hash_id, size_t output_size);
203 
221 WARN_UNUSED_RESULT cx_err_t cx_hash_update(cx_hash_t *hash, const uint8_t *in, size_t in_len);
222 
236 WARN_UNUSED_RESULT cx_err_t cx_hash_final(cx_hash_t *hash, uint8_t *digest);
237 
238 #endif // HAVE_HASH
239 
240 #endif // LCX_HASH_H
Cryptography flags.
#define CX_THROW(call)
Definition: lcx_wrappers.h:15
unsigned char uint8_t
Definition: usbd_conf.h:53