Line  | Count  | Source (jump to first uncovered line)  | 
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  | 0  | { | 
52  | 0  |   _sha3_pad_shake (state, block_size, block, index);  | 
53  |  | 
  | 
54  | 0  |   while (length > block_size)  | 
55  | 0  |     { | 
56  | 0  |       sha3_permute (state);  | 
57  | 0  |       _nettle_write_le64 (block_size, dst, state->a);  | 
58  | 0  |       length -= block_size;  | 
59  | 0  |       dst += block_size;  | 
60  | 0  |     }  | 
61  |  | 
  | 
62  | 0  |   sha3_permute (state);  | 
63  | 0  |   _nettle_write_le64 (length, dst, state->a);  | 
64  | 0  | }  | 
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  | 0  | { | 
72  | 0  |   unsigned left;  | 
73  |  |  | 
74  |  |   /* We use one's complement of the index value to indicate SHAKE is  | 
75  |  |      initialized. */  | 
76  | 0  |   if (index < block_size)  | 
77  | 0  |     { | 
78  |  |       /* This is the first call of _shake_output.  */  | 
79  | 0  |       _sha3_pad_shake (state, block_size, block, index);  | 
80  |  |       /* Point at the end of block to trigger fill in of the buffer.  */  | 
81  | 0  |       index = block_size;  | 
82  | 0  |     }  | 
83  | 0  |   else  | 
84  | 0  |     index = ~index;  | 
85  |  | 
  | 
86  | 0  |   assert (index <= block_size);  | 
87  |  |  | 
88  |  |   /* Write remaining data from the buffer.  */  | 
89  | 0  |   left = block_size - index;  | 
90  | 0  |   if (length <= left)  | 
91  | 0  |     { | 
92  | 0  |       memcpy (dst, block + index, length);  | 
93  | 0  |       return ~(index + length);  | 
94  | 0  |     }  | 
95  | 0  |   else  | 
96  | 0  |     { | 
97  | 0  |       memcpy (dst, block + index, left);  | 
98  | 0  |       length -= left;  | 
99  | 0  |       dst += left;  | 
100  | 0  |     }  | 
101  |  |  | 
102  |  |   /* Write full blocks.  */  | 
103  | 0  |   while (length > block_size)  | 
104  | 0  |     { | 
105  | 0  |       sha3_permute (state);  | 
106  | 0  |       _nettle_write_le64 (block_size, dst, state->a);  | 
107  | 0  |       length -= block_size;  | 
108  | 0  |       dst += block_size;  | 
109  | 0  |     }  | 
110  |  | 
  | 
111  | 0  |   sha3_permute (state);  | 
112  |  |   /* Fill in the buffer for next call.  */  | 
113  | 0  |   _nettle_write_le64 (block_size, block, state->a);  | 
114  | 0  |   memcpy (dst, block, length);  | 
115  | 0  |   return ~length;  | 
116  | 0  | }  |