1 /* @(#)sha3.h	1.6 16/10/26 2015-2016 J. Schilling */
2 /* sha3.h */
3 /*
4  * SHA3 hash code taken from
5  * https://github.com/rhash/RHash/tree/master/librhash
6  *
7  * Portions Copyright (c) 2015-2016 J. Schilling
8  */
9 #ifndef	_SCHILY_SHA3_H
10 #define	_SCHILY_SHA3_H
11 
12 #ifndef	_SCHILY_MCONFIG_H
13 #include <schily/mconfig.h>
14 #endif
15 #include <schily/types.h>
16 #include <schily/stdint.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #ifdef	HAVE_LONGLONG
23 
24 #define	sha3_224_hash_size	28
25 #define	sha3_256_hash_size	32
26 #define	sha3_384_hash_size	48
27 #define	sha3_512_hash_size	64
28 #define	sha3_max_permutation_size 25
29 #define	sha3_max_rate_in_qwords	24
30 
31 #define	SHA3_224_DIGEST_LENGTH		sha3_224_hash_size
32 #define	SHA3_224_DIGEST_STRING_LENGTH	(SHA3_224_DIGEST_LENGTH * 2 + 1)
33 #define	SHA3_256_DIGEST_LENGTH		sha3_256_hash_size
34 #define	SHA3_256_DIGEST_STRING_LENGTH	(SHA3_256_DIGEST_LENGTH * 2 + 1)
35 #define	SHA3_384_DIGEST_LENGTH		sha3_384_hash_size
36 #define	SHA3_384_DIGEST_STRING_LENGTH	(SHA3_384_DIGEST_LENGTH * 2 + 1)
37 #define	SHA3_512_DIGEST_LENGTH		sha3_512_hash_size
38 #define	SHA3_512_DIGEST_STRING_LENGTH	(SHA3_512_DIGEST_LENGTH * 2 + 1)
39 
40 /*
41  * SHA3 Algorithm context.
42  */
43 typedef struct sha3_ctx
44 {
45 	/* 1600 bits algorithm hashing state */
46 	UInt64_t hash[sha3_max_permutation_size];
47 	/* 1536-bit buffer for leftovers */
48 	UInt64_t message[sha3_max_rate_in_qwords];
49 	/* count of bytes in the message[] buffer */
50 	unsigned rest;
51 	/* size of a message block processed at once */
52 	unsigned block_size;
53 } sha3_ctx, SHA3_CTX;
54 
55 /* methods for calculating the hash function */
56 
57 void rhash_sha3_224_init __PR((sha3_ctx *ctx));
58 void rhash_sha3_256_init __PR((sha3_ctx *ctx));
59 void rhash_sha3_384_init __PR((sha3_ctx *ctx));
60 void rhash_sha3_512_init __PR((sha3_ctx *ctx));
61 void rhash_sha3_update __PR((sha3_ctx *ctx,
62 				const unsigned char *msg,
63 				size_t size));
64 void rhash_sha3_final __PR((sha3_ctx *ctx, unsigned char *result));
65 
66 void SHA3_224_Init	__PR((SHA3_CTX *ctx));
67 void SHA3_256_Init	__PR((SHA3_CTX *ctx));
68 void SHA3_384_Init	__PR((SHA3_CTX *ctx));
69 void SHA3_512_Init	__PR((SHA3_CTX *ctx));
70 void SHA3_Update	__PR((SHA3_CTX *ctx,
71 				const unsigned char *msg,
72 				size_t size));
73 void SHA3_Final		__PR((unsigned char *result, SHA3_CTX *ctx));
74 
75 #ifdef USE_KECCAK
76 #define	rhash_keccak_224_init	rhash_sha3_224_init
77 #define	rhash_keccak_256_init	rhash_sha3_256_init
78 #define	rhash_keccak_384_init	rhash_sha3_384_init
79 #define	rhash_keccak_512_init	rhash_sha3_512_init
80 #define	rhash_keccak_update	rhash_sha3_update
81 void rhash_keccak_final	__PR((sha3_ctx *ctx, unsigned char *result));
82 #endif
83 
84 #endif	/* HAVE_LONGLONG */
85 
86 #ifdef __cplusplus
87 }
88 #endif
89 
90 #endif /* _SCHILY_SHA3_H */
91