blob: d5db165fbc18732d4320bdb5fc65be6db12a7beb [file] [log] [blame]
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +02001/*
2 * FIPS-202 compliant SHA3 implementation
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19/*
20 * The SHA-3 Secure Hash Standard was published by NIST in 2015.
21 *
22 * https://nvlpubs.nist.gov/nistpubs/fips/nist.fips.202.pdf
23 */
24
25#include "common.h"
26
27#if defined(MBEDTLS_SHA3_C)
28
29#include "mbedtls/sha3.h"
30#include "mbedtls/platform_util.h"
31#include "mbedtls/error.h"
32
33#include <string.h>
34
35#if defined(MBEDTLS_SELF_TEST)
36#if defined(MBEDTLS_PLATFORM_C)
37#include "mbedtls/platform.h"
38#else
39#include <stdio.h>
40#include <stdlib.h>
41#define mbedtls_printf printf
42#define mbedtls_calloc calloc
43#define mbedtls_free free
44#endif /* MBEDTLS_PLATFORM_C */
45#endif /* MBEDTLS_SELF_TEST */
46
47#if !defined(MBEDTLS_SHA3_ALT)
48
49/*
50 * List of supported SHA-3 families
51 */
52static mbedtls_sha3_family_functions sha3_families[] = {
53 { MBEDTLS_SHA3_224, 1152, 224, 0x06 },
54 { MBEDTLS_SHA3_256, 1088, 256, 0x06 },
55 { MBEDTLS_SHA3_384, 832, 384, 0x06 },
56 { MBEDTLS_SHA3_512, 576, 512, 0x06 },
57 { MBEDTLS_SHA3_NONE, 0, 0, 0 }
58};
59
60static const uint64_t rc[24] = {
61 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000,
62 0x000000000000808b, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009,
63 0x000000000000008a, 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
64 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 0x8000000000008003,
65 0x8000000000008002, 0x8000000000000080, 0x000000000000800a, 0x800000008000000a,
66 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008,
67};
68
69static const uint8_t rho[24] = {
70 1, 62, 28, 27, 36, 44, 6, 55, 20,
71 3, 10, 43, 25, 39, 41, 45, 15,
72 21, 8, 18, 2, 61, 56, 14
73};
74
75static const uint8_t pi[24] = {
76 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,
77 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1,
78};
79
80#define ROT64( x , y ) ( ( ( x ) << ( y ) ) | ( ( x ) >> ( 64U - ( y ) ) ) )
81#define ABSORB( ctx, idx, v ) do { ctx->state[( idx ) >> 3] ^= ( ( uint64_t ) ( v ) ) << ( ( ( idx ) & 0x7 ) << 3 ); } while( 0 )
82#define SQUEEZE( ctx, idx ) ( ( uint8_t )( ctx->state[( idx ) >> 3] >> ( ( ( idx ) & 0x7 ) << 3 ) ) )
83#define SWAP( x, y ) do { uint64_t tmp = ( x ); ( x ) = ( y ); ( y ) = tmp; } while( 0 )
84
85/* The permutation function. */
86static void keccak_f1600(mbedtls_sha3_context *ctx)
87{
88 uint64_t lane[5];
89 uint64_t *s = ctx->state;
90 int i;
91
92 for( int round = 0; round < 24; round++ )
93 {
94 uint64_t t;
95
96 /* Theta */
97 lane[0] = s[0] ^ s[5] ^ s[10] ^ s[15] ^ s[20];
98 lane[1] = s[1] ^ s[6] ^ s[11] ^ s[16] ^ s[21];
99 lane[2] = s[2] ^ s[7] ^ s[12] ^ s[17] ^ s[22];
100 lane[3] = s[3] ^ s[8] ^ s[13] ^ s[18] ^ s[23];
101 lane[4] = s[4] ^ s[9] ^ s[14] ^ s[19] ^ s[24];
102
103 t = lane[4] ^ ROT64( lane[1], 1 );
104 s[0] ^= t; s[5] ^= t; s[10] ^= t; s[15] ^= t; s[20] ^= t;
105
106 t = lane[0] ^ ROT64( lane[2], 1 );
107 s[1] ^= t; s[6] ^= t; s[11] ^= t; s[16] ^= t; s[21] ^= t;
108
109 t = lane[1] ^ ROT64( lane[3], 1 );
110 s[2] ^= t; s[7] ^= t; s[12] ^= t; s[17] ^= t; s[22] ^= t;
111
112 t = lane[2] ^ ROT64( lane[4], 1 );
113 s[3] ^= t; s[8] ^= t; s[13] ^= t; s[18] ^= t; s[23] ^= t;
114
115 t = lane[3] ^ ROT64( lane[0], 1 );
116 s[4] ^= t; s[9] ^= t; s[14] ^= t; s[19] ^= t; s[24] ^= t;
117
118 /* Rho */
119 for( i = 1; i < 25; i++ )
120 s[i] = ROT64( s[i], rho[i-1] );
121
122 /* Pi */
123 t = s[1];
124 for( i = 0; i < 24; i++ )
125 SWAP( s[pi[i]], t );
126
127 /* Chi */
128 lane[0] = s[0]; lane[1] = s[1]; lane[2] = s[2]; lane[3] = s[3]; lane[4] = s[4];
129 s[0] ^= (~lane[1]) & lane[2];
130 s[1] ^= (~lane[2]) & lane[3];
131 s[2] ^= (~lane[3]) & lane[4];
132 s[3] ^= (~lane[4]) & lane[0];
133 s[4] ^= (~lane[0]) & lane[1];
134
135 lane[0] = s[5]; lane[1] = s[6]; lane[2] = s[7]; lane[3] = s[8]; lane[4] = s[9];
136 s[5] ^= (~lane[1]) & lane[2];
137 s[6] ^= (~lane[2]) & lane[3];
138 s[7] ^= (~lane[3]) & lane[4];
139 s[8] ^= (~lane[4]) & lane[0];
140 s[9] ^= (~lane[0]) & lane[1];
141
142 lane[0] = s[10]; lane[1] = s[11]; lane[2] = s[12]; lane[3] = s[13]; lane[4] = s[14];
143 s[10] ^= (~lane[1]) & lane[2];
144 s[11] ^= (~lane[2]) & lane[3];
145 s[12] ^= (~lane[3]) & lane[4];
146 s[13] ^= (~lane[4]) & lane[0];
147 s[14] ^= (~lane[0]) & lane[1];
148
149 lane[0] = s[15]; lane[1] = s[16]; lane[2] = s[17]; lane[3] = s[18]; lane[4] = s[19];
150 s[15] ^= (~lane[1]) & lane[2];
151 s[16] ^= (~lane[2]) & lane[3];
152 s[17] ^= (~lane[3]) & lane[4];
153 s[18] ^= (~lane[4]) & lane[0];
154 s[19] ^= (~lane[0]) & lane[1];
155
156 lane[0] = s[20]; lane[1] = s[21]; lane[2] = s[22]; lane[3] = s[23]; lane[4] = s[24];
157 s[20] ^= (~lane[1]) & lane[2];
158 s[21] ^= (~lane[2]) & lane[3];
159 s[22] ^= (~lane[3]) & lane[4];
160 s[23] ^= (~lane[4]) & lane[0];
161 s[24] ^= (~lane[0]) & lane[1];
162
163 /* Iota */
164 s[0] ^= rc[round];
165 }
166}
167
168void mbedtls_sha3_init( mbedtls_sha3_context *ctx )
169{
170 if( ctx == NULL )
171 return;
172
173 memset( ctx, 0, sizeof( mbedtls_sha3_context ) );
174}
175
176void mbedtls_sha3_free( mbedtls_sha3_context *ctx )
177{
178 if( ctx == NULL )
179 return;
180
181 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha3_context ) );
182}
183
184void mbedtls_sha3_clone( mbedtls_sha3_context *dst,
185 const mbedtls_sha3_context *src )
186{
187 if ( dst == NULL || src == NULL )
188 return;
189
190 *dst = *src;
191}
192
193/*
194 * SHA-3 context setup
195 */
196int mbedtls_sha3_starts( mbedtls_sha3_context *ctx, mbedtls_sha3_id id )
197{
198 mbedtls_sha3_family_functions *p = NULL;
199 if( ctx == NULL )
200 return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA );
201
202 for( p = sha3_families; p->id != MBEDTLS_SHA3_NONE; p++ )
203 {
204 if( p->id == id )
205 break;
206 }
207
208 if( p == NULL )
209 return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA );
210
211 ctx->id = id;
212 ctx->r = p->r;
213 ctx->olen = p->olen / 8;
214 ctx->xor_byte = p->xor_byte;
215 ctx->max_block_size = ctx->r / 8;
216
217 return( 0 );
218}
219
220/*
221 * SHA-3 process buffer
222 */
223int mbedtls_sha3_update( mbedtls_sha3_context *ctx,
224 const uint8_t *input,
225 size_t ilen )
226{
227 if( ctx == NULL )
228 return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA );
229
230 if( ilen == 0 || input == NULL )
231 return( 0 );
232
233 while( ilen-- > 0 )
234 {
235 ABSORB( ctx, ctx->index, *input++ );
236 if( ( ctx->index = ( ctx->index + 1) % ctx->max_block_size ) == 0 )
237 keccak_f1600( ctx );
238 }
239
240 return( 0 );
241}
242
243int mbedtls_sha3_finish( mbedtls_sha3_context *ctx,
244 uint8_t *output, size_t olen )
245{
246 if( ctx == NULL )
247 return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA );
248
249 if( olen == 0 )
250 return( 0 );
251
252 if( ctx->olen > 0 && ctx->olen != olen )
253 return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA );
254
255 ABSORB( ctx, ctx->index, ctx->xor_byte );
256 ABSORB( ctx, ctx->max_block_size - 1, 0x80 );
257 keccak_f1600( ctx );
258 ctx->index = 0;
259
260 while( olen-- > 0 )
261 {
262 *output++ = SQUEEZE( ctx, ctx->index );
263
264 if( ( ctx->index = ( ctx->index + 1) % ctx->max_block_size ) == 0 )
265 keccak_f1600( ctx );
266 }
267
268 return( 0 );
269}
270
271#endif /* !MBEDTLS_SHA3_ALT */
272
273/*
274 * output = SHA3( input buffer )
275 */
276int mbedtls_sha3( mbedtls_sha3_id id, const uint8_t *input,
277 size_t ilen, uint8_t *output, size_t olen )
278{
279 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
280 mbedtls_sha3_context ctx;
281
282 if( ilen != 0 && input == NULL )
283 return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA );
284
285 if( output == NULL )
286 return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA );
287
288 mbedtls_sha3_init( &ctx );
289
290 if( ( ret = mbedtls_sha3_starts( &ctx, id ) ) != 0 )
291 goto exit;
292
293 if( ( ret = mbedtls_sha3_update( &ctx, input, ilen ) ) != 0 )
294 goto exit;
295
296 if( ( ret = mbedtls_sha3_finish( &ctx, output, olen ) ) != 0 )
297 goto exit;
298
299exit:
300 mbedtls_sha3_free( &ctx );
301
302 return( ret );
303}
304
305#endif /* MBEDTLS_SHA3_C */