Embedded SDK
Embedded SDK
lcx_sha256.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 
30 #ifndef LCX_SHA256_H
31 #define LCX_SHA256_H
32 
33 #if defined(HAVE_SHA256) || defined(HAVE_SHA224)
34 
35 #include "lcx_wrappers.h"
36 #include "lcx_hash.h"
37 #include <stddef.h>
38 #include <stdint.h>
39 
41 #define CX_SHA224_SIZE 28
42 
44 #define CX_SHA256_SIZE 32
45 
49 struct cx_sha256_s {
50  struct cx_hash_header_s header;
51  size_t blen;
52  uint8_t block[64];
53  uint8_t acc[8 * 4];
54 };
56 typedef struct cx_sha256_s cx_sha256_t;
57 
58 #if defined(HAVE_SHA224)
68 // No need to add WARN_UNUSED_RESULT to cx_sha224_init_no_throw(), it always returns CX_OK
69 cx_err_t cx_sha224_init_no_throw(cx_sha256_t *hash);
70 
79 static inline int cx_sha224_init(cx_sha256_t *hash)
80 {
81  cx_sha224_init_no_throw(hash);
82  return CX_SHA224;
83 }
84 
97 cx_err_t cx_sha224_hash_iovec(const cx_iovec_t *iovec,
98  size_t iovec_len,
99  uint8_t digest[static CX_SHA224_SIZE]);
100 
113 static inline cx_err_t cx_sha224_hash(const uint8_t *in,
114  size_t in_len,
115  uint8_t digest[static CX_SHA224_SIZE])
116 {
117  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
118 
119  return cx_sha224_hash_iovec(&iovec, 1, digest);
120 }
121 #endif // HAVE_SHA224
122 
132 // No need to add WARN_UNUSED_RESULT to cx_sha256_init_no_throw(), it always returns CX_OK
133 cx_err_t cx_sha256_init_no_throw(cx_sha256_t *hash);
134 
143 static inline int cx_sha256_init(cx_sha256_t *hash)
144 {
145  cx_sha256_init_no_throw(hash);
146  return CX_SHA256;
147 }
148 
161 cx_err_t cx_sha256_hash_iovec(const cx_iovec_t *iovec,
162  size_t iovec_len,
163  uint8_t digest[static CX_SHA256_SIZE]);
164 
177 static inline cx_err_t cx_sha256_hash(const uint8_t *in,
178  size_t in_len,
179  uint8_t digest[static CX_SHA256_SIZE])
180 {
181  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
182 
183  return cx_sha256_hash_iovec(&iovec, 1, digest);
184 }
185 
199 size_t cx_hash_sha256(const uint8_t *in, size_t len, uint8_t *out, size_t out_len);
200 
201 #endif // defined(HAVE_SHA256) || defined(HAVE_SHA224)
202 
203 #endif // LCX_SHA256_H
Hash functions.
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