xref: /freebsd/crypto/libecc/include/libecc/hash/sha256.h (revision f0865ec9906d5a18fa2a3b61381f22ce16e606ad)
1 /*
2  *  Copyright (C) 2017 - This file is part of libecc project
3  *
4  *  Authors:
5  *      Ryad BENADJILA <ryadbenadjila@gmail.com>
6  *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
7  *      Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
8  *
9  *  Contributors:
10  *      Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
11  *      Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
12  *
13  *  This software is licensed under a dual BSD and GPL v2 license.
14  *  See LICENSE file at the root folder of the project.
15  */
16 #include <libecc/lib_ecc_config.h>
17 #ifdef WITH_HASH_SHA256
18 
19 #ifndef __SHA256_H__
20 #define __SHA256_H__
21 
22 #include <libecc/words/words.h>
23 #include <libecc/utils/utils.h>
24 #include <libecc/hash/sha2.h>
25 
26 #define SHA256_STATE_SIZE   8
27 #define SHA256_BLOCK_SIZE   64
28 #define SHA256_DIGEST_SIZE  32
29 #define SHA256_DIGEST_SIZE_BITS  256
30 
31 /* Compute max hash digest and block sizes */
32 #ifndef MAX_DIGEST_SIZE
33 #define MAX_DIGEST_SIZE	0
34 #endif
35 #if (MAX_DIGEST_SIZE < SHA256_DIGEST_SIZE)
36 #undef MAX_DIGEST_SIZE
37 #define MAX_DIGEST_SIZE SHA256_DIGEST_SIZE
38 #endif
39 
40 #ifndef MAX_DIGEST_SIZE_BITS
41 #define MAX_DIGEST_SIZE_BITS	0
42 #endif
43 #if (MAX_DIGEST_SIZE_BITS < SHA256_DIGEST_SIZE_BITS)
44 #undef MAX_DIGEST_SIZE_BITS
45 #define MAX_DIGEST_SIZE_BITS SHA256_DIGEST_SIZE_BITS
46 #endif
47 
48 #ifndef MAX_BLOCK_SIZE
49 #define MAX_BLOCK_SIZE  0
50 #endif
51 #if (MAX_BLOCK_SIZE < SHA256_BLOCK_SIZE)
52 #undef MAX_BLOCK_SIZE
53 #define MAX_BLOCK_SIZE SHA256_BLOCK_SIZE
54 #endif
55 
56 #define SHA256_HASH_MAGIC ((word_t)(0x11299a2b32098412ULL))
57 #define SHA256_HASH_CHECK_INITIALIZED(A, ret, err) \
58 	MUST_HAVE((((void *)(A)) != NULL) && ((A)->magic == SHA256_HASH_MAGIC), ret, err)
59 
60 typedef struct {
61 	/* Number of bytes processed */
62 	u64 sha256_total;
63 	/* Internal state */
64 	u32 sha256_state[SHA256_STATE_SIZE];
65 	/* Internal buffer to handle updates in a block */
66 	u8 sha256_buffer[SHA256_BLOCK_SIZE];
67 	/* Initialization magic value */
68 	word_t magic;
69 } sha256_context;
70 
71 ATTRIBUTE_WARN_UNUSED_RET int sha256_init(sha256_context *ctx);
72 ATTRIBUTE_WARN_UNUSED_RET int sha256_update(sha256_context *ctx, const u8 *input, u32 ilen);
73 ATTRIBUTE_WARN_UNUSED_RET int sha256_final(sha256_context *ctx, u8 output[SHA256_DIGEST_SIZE]);
74 ATTRIBUTE_WARN_UNUSED_RET int sha256_scattered(const u8 **inputs, const u32 *ilens,
75 		     u8 output[SHA256_DIGEST_SIZE]);
76 ATTRIBUTE_WARN_UNUSED_RET int sha256(const u8 *input, u32 ilen, u8 output[SHA256_DIGEST_SIZE]);
77 
78 #endif /* __SHA256_H__ */
79 #endif /* WITH_HASH_SHA256 */
80