/src/nettle-with-mini-gmp/sha3.c
Line  | Count  | Source  | 
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  |  | #include "sha3-internal.h"  | 
43  |  |  | 
44  |  | #include "macros.h"  | 
45  |  | #include "md-internal.h"  | 
46  |  | #include "memxor.h"  | 
47  |  |  | 
48  |  | #if WORDS_BIGENDIAN  | 
49  |  | static void  | 
50  |  | sha3_xor_block (struct sha3_state *state, unsigned length, const uint8_t *data)  | 
51  |  | { | 
52  |  |   assert ( (length & 7) == 0);  | 
53  |  |   {     | 
54  |  |     uint64_t *p;  | 
55  |  |     for (p = state->a; length > 0; p++, length -= 8, data += 8)  | 
56  |  |       *p ^= LE_READ_UINT64 (data);  | 
57  |  |   }  | 
58  |  | }  | 
59  |  | #else /* !WORDS_BIGENDIAN */  | 
60  | 21.2k  | #define sha3_xor_block(state, length, data) memxor (state->a, data, length)  | 
61  |  | #endif  | 
62  |  |  | 
63  |  | static void  | 
64  |  | sha3_absorb (struct sha3_state *state, unsigned length, const uint8_t *data)  | 
65  | 20.8k  | { | 
66  | 20.8k  |   sha3_xor_block (state, length, data);  | 
67  | 20.8k  |   sha3_permute (state);  | 
68  | 20.8k  | }  | 
69  |  |  | 
70  |  | unsigned  | 
71  |  | _nettle_sha3_update (struct sha3_state *state,  | 
72  |  |          unsigned block_size, uint8_t *block,  | 
73  |  |          unsigned pos,  | 
74  |  |          size_t length, const uint8_t *data)  | 
75  | 22.7k  | { | 
76  | 22.7k  |   assert (pos < block_size);  | 
77  |  |  | 
78  | 22.7k  |   if (!length)  | 
79  | 20.6k  |     return pos;  | 
80  |  |  | 
81  | 2.09k  |   if (pos > 0)  | 
82  | 1.65k  |     { | 
83  | 1.65k  |       MD_FILL_OR_RETURN_INDEX (block_size, block, pos, length, data);  | 
84  | 326  |       sha3_absorb (state, block_size, block);  | 
85  | 326  |     }  | 
86  | 21.2k  |   for (; length >= block_size; length -= block_size, data += block_size)  | 
87  | 20.5k  |     sha3_absorb (state, block_size, data);  | 
88  |  |  | 
89  | 770  |   memcpy (block, data, length);  | 
90  | 770  |   return length;  | 
91  | 2.09k  | }  | 
92  |  |  | 
93  |  | void  | 
94  |  | _nettle_sha3_pad (struct sha3_state *state,  | 
95  |  |       unsigned block_size, uint8_t *block, unsigned pos, uint8_t magic)  | 
96  | 441  | { | 
97  | 441  |   assert (pos < block_size);  | 
98  | 441  |   block[pos++] = magic;  | 
99  |  |  | 
100  | 441  |   memset (block + pos, 0, block_size - pos);  | 
101  | 441  |   block[block_size - 1] |= 0x80;  | 
102  |  |  | 
103  | 441  |   sha3_xor_block (state, block_size, block);  | 
104  | 441  | }  |