Embedded SDK
Embedded SDK
lcx_sha3.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 
29 #ifndef LCX_SHA3_H
30 #define LCX_SHA3_H
31 
32 #ifdef HAVE_SHA3
33 
34 #include "lcx_common.h"
35 #include "lcx_hash.h"
36 #include <stddef.h>
37 
39 #define CX_SHA3_224_SIZE 28
40 
42 #define CX_SHA3_256_SIZE 32
43 
45 #define CX_SHA3_384_SIZE 48
46 
48 #define CX_SHA3_512_SIZE 64
49 
51 #define CX_KECCAK_224_SIZE 28
52 
54 #define CX_KECCAK_256_SIZE 32
55 
57 #define CX_KECCAK_384_SIZE 48
58 
60 #define CX_KECCAK_512_SIZE 64
61 
65 struct cx_sha3_s {
66  struct cx_hash_header_s header;
67  size_t output_size;
68  size_t block_size;
69  size_t blen;
70  uint8_t block[200];
71  uint64bits_t acc[25];
72 };
74 typedef struct cx_sha3_s cx_sha3_t;
75 
94 WARN_UNUSED_RESULT cx_err_t cx_sha3_init_no_throw(cx_sha3_t *hash, size_t size);
95 
100 DEPRECATED static inline int cx_sha3_init(cx_sha3_t *hash, size_t size)
101 {
102  CX_THROW(cx_sha3_init_no_throw(hash, size));
103  return CX_SHA3;
104 }
105 
118 cx_err_t cx_sha3_224_hash_iovec(const cx_iovec_t *iovec,
119  size_t iovec_len,
120  uint8_t digest[static CX_SHA3_224_SIZE]);
121 
134 static inline cx_err_t cx_sha3_224_hash(const uint8_t *in,
135  size_t in_len,
136  uint8_t digest[static CX_SHA3_224_SIZE])
137 {
138  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
139 
140  return cx_sha3_224_hash_iovec(&iovec, 1, digest);
141 }
142 
155 cx_err_t cx_sha3_256_hash_iovec(const cx_iovec_t *iovec,
156  size_t iovec_len,
157  uint8_t digest[static CX_SHA3_256_SIZE]);
158 
171 static inline cx_err_t cx_sha3_256_hash(const uint8_t *in,
172  size_t in_len,
173  uint8_t digest[static CX_SHA3_256_SIZE])
174 {
175  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
176 
177  return cx_sha3_256_hash_iovec(&iovec, 1, digest);
178 }
179 
192 cx_err_t cx_sha3_384_hash_iovec(const cx_iovec_t *iovec,
193  size_t iovec_len,
194  uint8_t digest[static CX_SHA3_384_SIZE]);
195 
208 static inline cx_err_t cx_sha3_384_hash(const uint8_t *in,
209  size_t in_len,
210  uint8_t digest[static CX_SHA3_384_SIZE])
211 {
212  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
213 
214  return cx_sha3_384_hash_iovec(&iovec, 1, digest);
215 }
216 
229 cx_err_t cx_sha3_512_hash_iovec(const cx_iovec_t *iovec,
230  size_t iovec_len,
231  uint8_t digest[static CX_SHA3_512_SIZE]);
232 
245 static inline cx_err_t cx_sha3_512_hash(const uint8_t *in,
246  size_t in_len,
247  uint8_t digest[static CX_SHA3_512_SIZE])
248 {
249  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
250 
251  return cx_sha3_512_hash_iovec(&iovec, 1, digest);
252 }
253 
273 WARN_UNUSED_RESULT cx_err_t cx_keccak_init_no_throw(cx_sha3_t *hash, size_t size);
274 
279 DEPRECATED static inline int cx_keccak_init(cx_sha3_t *hash, size_t size)
280 {
281  CX_THROW(cx_keccak_init_no_throw(hash, size));
282  return CX_KECCAK;
283 }
284 
297 cx_err_t cx_keccak_224_hash_iovec(const cx_iovec_t *iovec,
298  size_t iovec_len,
299  uint8_t digest[static CX_KECCAK_224_SIZE]);
300 
313 static inline cx_err_t cx_keccak_224_hash(const uint8_t *in,
314  size_t in_len,
315  uint8_t digest[static CX_KECCAK_224_SIZE])
316 {
317  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
318 
319  return cx_keccak_224_hash_iovec(&iovec, 1, digest);
320 }
321 
334 cx_err_t cx_keccak_256_hash_iovec(const cx_iovec_t *iovec,
335  size_t iovec_len,
336  uint8_t digest[static CX_KECCAK_256_SIZE]);
337 
350 static inline cx_err_t cx_keccak_256_hash(const uint8_t *in,
351  size_t in_len,
352  uint8_t digest[static CX_KECCAK_256_SIZE])
353 {
354  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
355 
356  return cx_keccak_256_hash_iovec(&iovec, 1, digest);
357 }
358 
371 cx_err_t cx_keccak_384_hash_iovec(const cx_iovec_t *iovec,
372  size_t iovec_len,
373  uint8_t digest[static CX_KECCAK_384_SIZE]);
374 
387 static inline cx_err_t cx_keccak_384_hash(const uint8_t *in,
388  size_t in_len,
389  uint8_t digest[static CX_KECCAK_384_SIZE])
390 {
391  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
392 
393  return cx_keccak_384_hash_iovec(&iovec, 1, digest);
394 }
395 
408 cx_err_t cx_keccak_512_hash_iovec(const cx_iovec_t *iovec,
409  size_t iovec_len,
410  uint8_t digest[static CX_KECCAK_512_SIZE]);
411 
424 static inline cx_err_t cx_keccak_512_hash(const uint8_t *in,
425  size_t in_len,
426  uint8_t digest[static CX_KECCAK_512_SIZE])
427 {
428  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
429 
430  return cx_keccak_512_hash_iovec(&iovec, 1, digest);
431 }
432 
451 WARN_UNUSED_RESULT cx_err_t cx_shake128_init_no_throw(cx_sha3_t *hash, size_t out_size);
452 
457 DEPRECATED static inline int cx_shake128_init(cx_sha3_t *hash, unsigned int out_size)
458 {
459  CX_THROW(cx_shake128_init_no_throw(hash, out_size));
460  return CX_SHAKE128;
461 }
462 
477 cx_err_t cx_shake128_hash_iovec(const cx_iovec_t *iovec,
478  size_t iovec_len,
479  uint8_t *digest,
480  size_t out_length);
481 
496 static inline cx_err_t cx_shake128_hash(const uint8_t *in,
497  size_t in_len,
498  uint8_t *digest,
499  size_t out_length)
500 {
501  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
502 
503  return cx_shake128_hash_iovec(&iovec, 1, digest, out_length);
504 }
505 
524 WARN_UNUSED_RESULT cx_err_t cx_shake256_init_no_throw(cx_sha3_t *hash, size_t out_size);
525 
530 DEPRECATED static inline int cx_shake256_init(cx_sha3_t *hash, unsigned int out_length)
531 {
532  CX_THROW(cx_shake256_init_no_throw(hash, out_length));
533  return CX_SHAKE256;
534 }
549 cx_err_t cx_shake256_hash_iovec(const cx_iovec_t *iovec,
550  size_t iovec_len,
551  uint8_t *digest,
552  size_t out_length);
553 
568 static inline cx_err_t cx_shake256_hash(const uint8_t *in,
569  size_t in_len,
570  uint8_t *digest,
571  size_t out_length)
572 {
573  const cx_iovec_t iovec = {.iov_base = in, .iov_len = in_len};
574 
575  return cx_shake256_hash_iovec(&iovec, 1, digest, out_length);
576 }
577 
598 WARN_UNUSED_RESULT cx_err_t cx_sha3_xof_init_no_throw(cx_sha3_t *hash,
599  size_t size,
600  size_t out_length);
601 
606 DEPRECATED static inline int cx_sha3_xof_init(cx_sha3_t *hash,
607  unsigned int size,
608  unsigned int out_length)
609 {
610  CX_THROW(cx_sha3_xof_init_no_throw(hash, size, out_length));
611 
612  if (size == 128) {
613  return CX_SHAKE128;
614  }
615  else {
616  return CX_SHAKE256;
617  }
618 }
619 
620 #endif // HAVE_SHA3
621 
622 #endif // LCX_SHA3_H
Cryptography flags.
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
64-bit types, native or by-hands, depending on target and/or compiler support.
Definition: lcx_common.h:49
unsigned char uint8_t
Definition: usbd_conf.h:53