0c655376492fc1fa1130833028316f034ae4fcde
[platform/upstream/nettle.git] / sha3.h
1 /* sha3.h
2
3    The sha3 hash function (aka Keccak).
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 #ifndef NETTLE_SHA3_H_INCLUDED
35 #define NETTLE_SHA3_H_INCLUDED
36
37 #include "nettle-types.h"
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 /* Name mangling */
44 #define sha3_permute nettle_sha3_permute
45 #define _sha3_update _nettle_sha3_update
46 #define _sha3_pad _nettle_sha3_pad
47 #define sha3_224_init nettle_sha3_224_init
48 #define sha3_224_update nettle_sha3_224_update
49 #define sha3_224_digest nettle_sha3_224_digest
50 #define sha3_256_init nettle_sha3_256_init
51 #define sha3_256_update nettle_sha3_256_update
52 #define sha3_256_digest nettle_sha3_256_digest
53 #define sha3_384_init nettle_sha3_384_init
54 #define sha3_384_update nettle_sha3_384_update
55 #define sha3_384_digest nettle_sha3_384_digest
56 #define sha3_512_init nettle_sha3_512_init
57 #define sha3_512_update nettle_sha3_512_update
58 #define sha3_512_digest nettle_sha3_512_digest
59
60 /* Indicates that SHA3 is the NIST FIPS 202 version. */
61 #define NETTLE_SHA3_FIPS202 1
62
63 /* The sha3 state is a 5x5 matrix of 64-bit words. In the notation of
64    Keccak description, S[x,y] is element x + 5*y, so if x is
65    interpreted as the row index and y the column index, it is stored
66    in column-major order. */
67 #define SHA3_STATE_LENGTH 25
68
69 /* The "width" is 1600 bits or 200 octets */
70 struct sha3_state
71 {
72   uint64_t a[SHA3_STATE_LENGTH];
73 };
74
75 void
76 sha3_permute (struct sha3_state *state);
77
78 unsigned
79 _sha3_update (struct sha3_state *state,
80               unsigned block_size, uint8_t *block,
81               unsigned pos,
82               size_t length, const uint8_t *data);
83 void
84 _sha3_pad (struct sha3_state *state,
85            unsigned block_size, uint8_t *block, unsigned pos);
86
87 /* The "capacity" is set to 2*(digest size), 512 bits or 64 octets.
88    The "rate" is the width - capacity, or width - 2 * (digest
89    size). */
90
91 #define SHA3_224_DIGEST_SIZE 28
92 #define SHA3_224_BLOCK_SIZE 144
93
94 #define SHA3_256_DIGEST_SIZE 32
95 #define SHA3_256_BLOCK_SIZE 136
96
97 #define SHA3_384_DIGEST_SIZE 48
98 #define SHA3_384_BLOCK_SIZE 104
99
100 #define SHA3_512_DIGEST_SIZE 64
101 #define SHA3_512_BLOCK_SIZE 72
102
103 /* For backwards compatibility */
104 #define SHA3_224_DATA_SIZE SHA3_224_BLOCK_SIZE
105 #define SHA3_256_DATA_SIZE SHA3_256_BLOCK_SIZE
106 #define SHA3_384_DATA_SIZE SHA3_384_BLOCK_SIZE
107 #define SHA3_512_DATA_SIZE SHA3_512_BLOCK_SIZE
108
109 struct sha3_224_ctx
110 {
111   struct sha3_state state;
112   unsigned index;
113   uint8_t block[SHA3_224_BLOCK_SIZE];
114 };
115
116 void
117 sha3_224_init (struct sha3_224_ctx *ctx);
118
119 void
120 sha3_224_update (struct sha3_224_ctx *ctx,
121                  size_t length,
122                  const uint8_t *data);
123
124 void
125 sha3_224_digest(struct sha3_224_ctx *ctx,
126                 size_t length,
127                 uint8_t *digest);
128
129 struct sha3_256_ctx
130 {
131   struct sha3_state state;
132   unsigned index;
133   uint8_t block[SHA3_256_BLOCK_SIZE];
134 };
135
136 void
137 sha3_256_init (struct sha3_256_ctx *ctx);
138
139 void
140 sha3_256_update (struct sha3_256_ctx *ctx,
141                  size_t length,
142                  const uint8_t *data);
143
144 void
145 sha3_256_digest(struct sha3_256_ctx *ctx,
146                 size_t length,
147                 uint8_t *digest);
148
149 struct sha3_384_ctx
150 {
151   struct sha3_state state;
152   unsigned index;
153   uint8_t block[SHA3_384_BLOCK_SIZE];
154 };
155
156 void
157 sha3_384_init (struct sha3_384_ctx *ctx);
158
159 void
160 sha3_384_update (struct sha3_384_ctx *ctx,
161                  size_t length,
162                  const uint8_t *data);
163
164 void
165 sha3_384_digest(struct sha3_384_ctx *ctx,
166                 size_t length,
167                 uint8_t *digest);
168
169 struct sha3_512_ctx
170 {
171   struct sha3_state state;
172   unsigned index;
173   uint8_t block[SHA3_512_BLOCK_SIZE];
174 };
175
176 void
177 sha3_512_init (struct sha3_512_ctx *ctx);
178
179 void
180 sha3_512_update (struct sha3_512_ctx *ctx,
181                  size_t length,
182                  const uint8_t *data);
183
184 void
185 sha3_512_digest(struct sha3_512_ctx *ctx,
186                 size_t length,
187                 uint8_t *digest);
188
189 #ifdef __cplusplus
190 }
191 #endif
192
193 #endif /* NETTLE_SHA3_H_INCLUDED */