annotate src/lib/sha3.h @ 23007:36e01285b5b8

lib: buffer - Improve header comment for buffer_insert() and buffer_delete().
author Stephan Bosch <stephan.bosch@dovecot.fi>
date Mon, 18 Mar 2019 00:52:37 +0100
parents c50031ebc182
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21070
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
1 /*
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
2 * FIPS 180-2 SHA-224/256/384/512 implementation
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
3 * Last update: 02/02/2007
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
4 * Issue date: 04/30/2005
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
5 *
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
6 * Copyright (C) 2005, 2007 Olivier Gay <olivier.gay@a3.epfl.ch>
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
7 * All rights reserved.
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
8 *
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
9 * Redistribution and use in source and binary forms, with or without
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
10 * modification, are permitted provided that the following conditions
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
11 * are met:
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
12 * 1. Redistributions of source code must retain the above copyright
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer.
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
14 * 2. Redistributions in binary form must reproduce the above copyright
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
15 * notice, this list of conditions and the following disclaimer in the
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
16 * documentation and/or other materials provided with the distribution.
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
17 * 3. Neither the name of the project nor the names of its contributors
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
18 * may be used to endorse or promote products derived from this software
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
19 * without specific prior written permission.
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
20 *
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
31 * SUCH DAMAGE.
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
32 */
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
33
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
34 #ifndef SHA3_H
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
35 #define SHA3_H
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
36
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
37 #include "hash-method.h"
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
38 #include "sha-common.h"
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
39
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
40 #define SHA3_KECCAK_SPONGE_WORDS \
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
41 (((1600)/8/*bits to byte*/)/sizeof(uint64_t))
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
42
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
43 struct sha3_ctx {
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
44 uint64_t saved; /* the portion of the input message that we
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
45 * didn't consume yet */
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
46 union { /* Keccak's state */
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
47 uint64_t s[SHA3_KECCAK_SPONGE_WORDS];
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
48 uint8_t sb[SHA3_KECCAK_SPONGE_WORDS * 8];
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
49 };
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
50 unsigned byteIndex; /* 0..7--the next byte after the set one
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
51 * (starts from 0; 0--none are buffered) */
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
52 unsigned wordIndex; /* 0..24--the next word to integrate input
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
53 * (starts from 0) */
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
54 unsigned capacityWords; /* the double size of the hash output in
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
55 * words (e.g. 16 for Keccak 512) */
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
56 };
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
57
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
58 void sha3_256_init(void *context);
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
59 void sha3_256_result(void *context,
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
60 unsigned char digest[STATIC_ARRAY SHA256_RESULTLEN]);
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
61 void sha3_256_get_digest(const void *data, size_t size,
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
62 unsigned char digest[STATIC_ARRAY SHA256_RESULTLEN]);
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
63
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
64 void sha3_512_init(void *context);
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
65 void sha3_512_result(void *context,
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
66 unsigned char digest[STATIC_ARRAY SHA512_RESULTLEN]);
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
67 void sha3_512_get_digest(const void *data, size_t size,
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
68 unsigned char digest[STATIC_ARRAY SHA512_RESULTLEN]);
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
69
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
70 void sha3_loop(void *context, const void *data, size_t len);
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
71
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
72 extern const struct hash_method hash_method_sha3_256;
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
73 extern const struct hash_method hash_method_sha3_512;
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
74
c50031ebc182 lib: Add SHA3 hashing methods for 256 and 512 bits
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
75 #endif