]> git.ipfire.org Git - thirdparty/nettle.git/blob - sha3-shake.c
Update config.guess and config.sub to 2024-01-01 versions.
[thirdparty/nettle.git] / sha3-shake.c
1 /* sha3-shake.c
2
3 Copyright (C) 2017, 2024 Daiki Ueno
4 Copyright (C) 2017 Red Hat, Inc.
5 Copyright (C) 2024 Niels Möller
6
7 This file is part of GNU Nettle.
8
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
11
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
15
16 or
17
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
21
22 or both in parallel, as here.
23
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
28
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
32 */
33
34 #if HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <assert.h>
39 #include <string.h>
40
41 #include "sha3.h"
42 #include "sha3-internal.h"
43
44 #include "nettle-write.h"
45
46 void
47 _nettle_sha3_shake (struct sha3_state *state,
48 unsigned block_size, uint8_t *block,
49 unsigned index,
50 size_t length, uint8_t *dst)
51 {
52 _sha3_pad_shake (state, block_size, block, index);
53
54 while (length > block_size)
55 {
56 sha3_permute (state);
57 _nettle_write_le64 (block_size, dst, state->a);
58 length -= block_size;
59 dst += block_size;
60 }
61
62 sha3_permute (state);
63 _nettle_write_le64 (length, dst, state->a);
64 }
65
66 unsigned
67 _nettle_sha3_shake_output (struct sha3_state *state,
68 unsigned block_size, uint8_t *block,
69 unsigned index,
70 size_t length, uint8_t *dst)
71 {
72 unsigned left;
73
74 /* We use one's complement of the index value to indicate SHAKE is
75 initialized. */
76 if (index < block_size)
77 {
78 /* This is the first call of _shake_output. */
79 _sha3_pad_shake (state, block_size, block, index);
80 /* Point at the end of block to trigger fill in of the buffer. */
81 index = block_size;
82 }
83 else
84 index = ~index;
85
86 assert (index <= block_size);
87
88 /* Write remaining data from the buffer. */
89 left = block_size - index;
90 if (length <= left)
91 {
92 memcpy (dst, block + index, length);
93 return ~(index + length);
94 }
95 else
96 {
97 memcpy (dst, block + index, left);
98 length -= left;
99 dst += left;
100 }
101
102 /* Write full blocks. */
103 while (length > block_size)
104 {
105 sha3_permute (state);
106 _nettle_write_le64 (block_size, dst, state->a);
107 length -= block_size;
108 dst += block_size;
109 }
110
111 sha3_permute (state);
112 /* Fill in the buffer for next call. */
113 _nettle_write_le64 (block_size, block, state->a);
114 memcpy (dst, block, length);
115 return ~length;
116 }