Fix accidental use of C99 for loop.
[gd/nettle] / sha3.c
1 /* sha3.c
2
3    The sha3 hash function.
4
5    Copyright (C) 2012 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
43 #include "macros.h"
44 #include "memxor.h"
45
46 static void
47 sha3_absorb (struct sha3_state *state, unsigned length, const uint8_t *data)
48 {
49   assert ( (length & 7) == 0);
50 #if WORDS_BIGENDIAN
51   {    
52     uint64_t *p;
53     for (p = state->a; length > 0; p++, length -= 8, data += 8)
54       *p ^= LE_READ_UINT64 (data);
55   }
56 #else /* !WORDS_BIGENDIAN */
57   memxor (state->a, data, length);
58 #endif
59
60   sha3_permute (state);
61 }
62
63 unsigned
64 _sha3_update (struct sha3_state *state,
65               unsigned block_size, uint8_t *block,
66               unsigned pos,
67               size_t length, const uint8_t *data)
68 {
69   if (pos > 0)
70     {
71       unsigned left = block_size - pos;
72       if (length < left)
73         {
74           memcpy (block + pos, data, length);
75           return pos + length;
76         }
77       else
78         {
79           memcpy (block + pos, data, left);
80           data += left;
81           length -= left;
82           sha3_absorb (state, block_size, block);
83         }
84     }
85   for (; length >= block_size; length -= block_size, data += block_size)
86     sha3_absorb (state, block_size, data);
87
88   memcpy (block, data, length);
89   return length;
90 }
91
92 void
93 _sha3_pad (struct sha3_state *state,
94            unsigned block_size, uint8_t *block, unsigned pos)
95 {
96   assert (pos < block_size);
97   block[pos++] = 6;
98
99   memset (block + pos, 0, block_size - pos);
100   block[block_size - 1] |= 0x80;
101
102   sha3_absorb (state, block_size, block);  
103 }