Embedded SDK
Embedded SDK
lcx_sha512.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_SHA512_H
31 #define LCX_SHA512_H
32 
33 #if defined(HAVE_SHA384) || defined(HAVE_SHA512)
34 
36 #define CX_SHA384_SIZE 48
38 #define CX_SHA512_SIZE 64
39 
43 struct cx_sha512_s {
44  struct cx_hash_header_s header;
45  size_t blen;
46  uint8_t block[128];
47  uint8_t acc[8 * 8];
48 };
50 typedef struct cx_sha512_s cx_sha512_t;
51 
61 // No need to add WARN_UNUSED_RESULT to cx_sha384_init_no_throw(), it always returns CX_OK
62 cx_err_t cx_sha384_init_no_throw(cx_sha512_t *hash);
63 
72 static inline int cx_sha384_init(cx_sha512_t *hash)
73 {
74  cx_sha384_init_no_throw(hash);
75  return CX_SHA384;
76 }
77 
90 cx_err_t cx_sha384_hash_iovec(const cx_iovec_t *iovec,
91  size_t iovec_len,
92  uint8_t digest[static CX_SHA384_SIZE]);
93 
106 static inline cx_err_t cx_sha384_hash(const uint8_t *in,
107  size_t in_len,
108  uint8_t digest[static CX_SHA384_SIZE])
109 {
110  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
111 
112  return cx_sha384_hash_iovec(&iovec, 1, digest);
113 }
114 
124 // No need to add WARN_UNUSED_RESULT to cx_sha512_init_no_throw(), it always returns CX_OK
125 cx_err_t cx_sha512_init_no_throw(cx_sha512_t *hash);
126 
135 static inline int cx_sha512_init(cx_sha512_t *hash)
136 {
137  cx_sha512_init_no_throw(hash);
138  return CX_SHA512;
139 }
140 
153 cx_err_t cx_sha512_hash_iovec(const cx_iovec_t *iovec,
154  size_t iovec_len,
155  uint8_t digest[static CX_SHA512_SIZE]);
156 
169 static inline cx_err_t cx_sha512_hash(const uint8_t *in,
170  size_t in_len,
171  uint8_t digest[static CX_SHA512_SIZE])
172 {
173  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
174 
175  return cx_sha512_hash_iovec(&iovec, 1, digest);
176 }
177 
190 size_t cx_hash_sha512(const uint8_t *in, size_t in_len, uint8_t *out, size_t out_len);
191 
192 #endif // defined(HAVE_SHA384) || defined(HAVE_SHA512)
193 
194 #endif // LCX_SHA512_H
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