Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
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
52enum 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};
77
78#define SHA256_BLOCK_SIZE 64
79#define SHA512_BLOCK_SIZE 128
80
82typedef enum cx_md_e cx_md_t;
83
88#define CX_HASH_MAX_BLOCK_COUNT 65535
89
91typedef struct cx_hash_header_s cx_hash_t;
92
96typedef struct {
97 cx_md_t md_type;
98 size_t output_size;
99 size_t block_size;
100 size_t ctx_size;
101 cx_err_t (*init_func)(cx_hash_t *ctx);
102 cx_err_t (*update_func)(cx_hash_t *ctx,
103 const uint8_t *data,
104 size_t len);
105 cx_err_t (*finish_func)(cx_hash_t *ctx, uint8_t *digest);
106 cx_err_t (*init_ex_func)(
107 cx_hash_t *ctx,
108 size_t output_size);
109 size_t (*output_size_func)(const cx_hash_t *ctx);
110} cx_hash_info_t;
111
115struct cx_hash_header_s {
116 const cx_hash_info_t *info;
117 uint32_t counter;
118};
119
120size_t cx_hash_get_size(const cx_hash_t *ctx);
121
150WARN_UNUSED_RESULT cx_err_t cx_hash_no_throw(cx_hash_t *hash,
151 uint32_t mode,
152 const uint8_t *in,
153 size_t len,
154 uint8_t *out,
155 size_t out_len);
156
161DEPRECATED static inline size_t cx_hash(cx_hash_t *hash,
162 uint32_t mode,
163 const unsigned char *in,
164 unsigned int len,
165 unsigned char *out,
166 unsigned int out_len)
167{
168 CX_THROW(cx_hash_no_throw(hash, mode, in, len, out, out_len));
169 return cx_hash_get_size(hash);
170}
171
184WARN_UNUSED_RESULT cx_err_t cx_hash_init(cx_hash_t *hash, cx_md_t hash_id);
185
206WARN_UNUSED_RESULT cx_err_t cx_hash_init_ex(cx_hash_t *hash, cx_md_t hash_id, size_t output_size);
207
225WARN_UNUSED_RESULT cx_err_t cx_hash_update(cx_hash_t *hash, const uint8_t *in, size_t in_len);
226
240WARN_UNUSED_RESULT cx_err_t cx_hash_final(cx_hash_t *hash, uint8_t *digest);
241
248const cx_hash_info_t *cx_hash_get_info(cx_md_t md_type);
249
261void cx_hkdf_extract(const cx_md_t hash_id,
262 const unsigned char *ikm,
263 unsigned int ikm_len,
264 unsigned char *salt,
265 unsigned int salt_len,
266 unsigned char *prk);
267
279void cx_hkdf_expand(const cx_md_t hash_id,
280 const unsigned char *prk,
281 unsigned int prk_len,
282 unsigned char *info,
283 unsigned int info_len,
284 unsigned char *okm,
285 unsigned int okm_len);
286
287#endif // HAVE_HASH
288
289#endif // LCX_HASH_H
Cryptography flags.
#define CX_THROW(call)