blob: 705f48c32c1b31ae088d2fca861d062cd2002db4 [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
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +020047/*
48 * List of supported SHA-3 families
49 */
50static mbedtls_sha3_family_functions sha3_families[] = {
51 { MBEDTLS_SHA3_224, 1152, 224, 0x06 },
52 { MBEDTLS_SHA3_256, 1088, 256, 0x06 },
53 { MBEDTLS_SHA3_384, 832, 384, 0x06 },
54 { MBEDTLS_SHA3_512, 576, 512, 0x06 },
55 { MBEDTLS_SHA3_NONE, 0, 0, 0 }
56};
57
58static const uint64_t rc[24] = {
59 0x0000000000000001, 0x0000000000008082, 0x800000000000808a, 0x8000000080008000,
60 0x000000000000808b, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009,
61 0x000000000000008a, 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
62 0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 0x8000000000008003,
63 0x8000000000008002, 0x8000000000000080, 0x000000000000800a, 0x800000008000000a,
64 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008,
65};
66
67static const uint8_t rho[24] = {
68 1, 62, 28, 27, 36, 44, 6, 55, 20,
69 3, 10, 43, 25, 39, 41, 45, 15,
70 21, 8, 18, 2, 61, 56, 14
71};
72
73static const uint8_t pi[24] = {
74 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4,
75 15, 23, 19, 13, 12, 2, 20, 14, 22, 9, 6, 1,
76};
77
78#define ROT64( x , y ) ( ( ( x ) << ( y ) ) | ( ( x ) >> ( 64U - ( y ) ) ) )
79#define ABSORB( ctx, idx, v ) do { ctx->state[( idx ) >> 3] ^= ( ( uint64_t ) ( v ) ) << ( ( ( idx ) & 0x7 ) << 3 ); } while( 0 )
80#define SQUEEZE( ctx, idx ) ( ( uint8_t )( ctx->state[( idx ) >> 3] >> ( ( ( idx ) & 0x7 ) << 3 ) ) )
81#define SWAP( x, y ) do { uint64_t tmp = ( x ); ( x ) = ( y ); ( y ) = tmp; } while( 0 )
82
83/* The permutation function. */
84static void keccak_f1600(mbedtls_sha3_context *ctx)
85{
86 uint64_t lane[5];
87 uint64_t *s = ctx->state;
88 int i;
89
90 for( int round = 0; round < 24; round++ )
91 {
92 uint64_t t;
93
94 /* Theta */
95 lane[0] = s[0] ^ s[5] ^ s[10] ^ s[15] ^ s[20];
96 lane[1] = s[1] ^ s[6] ^ s[11] ^ s[16] ^ s[21];
97 lane[2] = s[2] ^ s[7] ^ s[12] ^ s[17] ^ s[22];
98 lane[3] = s[3] ^ s[8] ^ s[13] ^ s[18] ^ s[23];
99 lane[4] = s[4] ^ s[9] ^ s[14] ^ s[19] ^ s[24];
100
101 t = lane[4] ^ ROT64( lane[1], 1 );
102 s[0] ^= t; s[5] ^= t; s[10] ^= t; s[15] ^= t; s[20] ^= t;
103
104 t = lane[0] ^ ROT64( lane[2], 1 );
105 s[1] ^= t; s[6] ^= t; s[11] ^= t; s[16] ^= t; s[21] ^= t;
106
107 t = lane[1] ^ ROT64( lane[3], 1 );
108 s[2] ^= t; s[7] ^= t; s[12] ^= t; s[17] ^= t; s[22] ^= t;
109
110 t = lane[2] ^ ROT64( lane[4], 1 );
111 s[3] ^= t; s[8] ^= t; s[13] ^= t; s[18] ^= t; s[23] ^= t;
112
113 t = lane[3] ^ ROT64( lane[0], 1 );
114 s[4] ^= t; s[9] ^= t; s[14] ^= t; s[19] ^= t; s[24] ^= t;
115
116 /* Rho */
117 for( i = 1; i < 25; i++ )
118 s[i] = ROT64( s[i], rho[i-1] );
119
120 /* Pi */
121 t = s[1];
122 for( i = 0; i < 24; i++ )
123 SWAP( s[pi[i]], t );
124
125 /* Chi */
126 lane[0] = s[0]; lane[1] = s[1]; lane[2] = s[2]; lane[3] = s[3]; lane[4] = s[4];
127 s[0] ^= (~lane[1]) & lane[2];
128 s[1] ^= (~lane[2]) & lane[3];
129 s[2] ^= (~lane[3]) & lane[4];
130 s[3] ^= (~lane[4]) & lane[0];
131 s[4] ^= (~lane[0]) & lane[1];
132
133 lane[0] = s[5]; lane[1] = s[6]; lane[2] = s[7]; lane[3] = s[8]; lane[4] = s[9];
134 s[5] ^= (~lane[1]) & lane[2];
135 s[6] ^= (~lane[2]) & lane[3];
136 s[7] ^= (~lane[3]) & lane[4];
137 s[8] ^= (~lane[4]) & lane[0];
138 s[9] ^= (~lane[0]) & lane[1];
139
140 lane[0] = s[10]; lane[1] = s[11]; lane[2] = s[12]; lane[3] = s[13]; lane[4] = s[14];
141 s[10] ^= (~lane[1]) & lane[2];
142 s[11] ^= (~lane[2]) & lane[3];
143 s[12] ^= (~lane[3]) & lane[4];
144 s[13] ^= (~lane[4]) & lane[0];
145 s[14] ^= (~lane[0]) & lane[1];
146
147 lane[0] = s[15]; lane[1] = s[16]; lane[2] = s[17]; lane[3] = s[18]; lane[4] = s[19];
148 s[15] ^= (~lane[1]) & lane[2];
149 s[16] ^= (~lane[2]) & lane[3];
150 s[17] ^= (~lane[3]) & lane[4];
151 s[18] ^= (~lane[4]) & lane[0];
152 s[19] ^= (~lane[0]) & lane[1];
153
154 lane[0] = s[20]; lane[1] = s[21]; lane[2] = s[22]; lane[3] = s[23]; lane[4] = s[24];
155 s[20] ^= (~lane[1]) & lane[2];
156 s[21] ^= (~lane[2]) & lane[3];
157 s[22] ^= (~lane[3]) & lane[4];
158 s[23] ^= (~lane[4]) & lane[0];
159 s[24] ^= (~lane[0]) & lane[1];
160
161 /* Iota */
162 s[0] ^= rc[round];
163 }
164}
165
166void mbedtls_sha3_init( mbedtls_sha3_context *ctx )
167{
168 if( ctx == NULL )
169 return;
170
171 memset( ctx, 0, sizeof( mbedtls_sha3_context ) );
172}
173
174void mbedtls_sha3_free( mbedtls_sha3_context *ctx )
175{
176 if( ctx == NULL )
177 return;
178
179 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha3_context ) );
180}
181
182void mbedtls_sha3_clone( mbedtls_sha3_context *dst,
183 const mbedtls_sha3_context *src )
184{
185 if ( dst == NULL || src == NULL )
186 return;
187
188 *dst = *src;
189}
190
191/*
192 * SHA-3 context setup
193 */
194int mbedtls_sha3_starts( mbedtls_sha3_context *ctx, mbedtls_sha3_id id )
195{
196 mbedtls_sha3_family_functions *p = NULL;
197 if( ctx == NULL )
198 return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA );
199
200 for( p = sha3_families; p->id != MBEDTLS_SHA3_NONE; p++ )
201 {
202 if( p->id == id )
203 break;
204 }
205
206 if( p == NULL )
207 return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA );
208
209 ctx->id = id;
210 ctx->r = p->r;
211 ctx->olen = p->olen / 8;
212 ctx->xor_byte = p->xor_byte;
213 ctx->max_block_size = ctx->r / 8;
214
215 return( 0 );
216}
217
218/*
219 * SHA-3 process buffer
220 */
221int mbedtls_sha3_update( mbedtls_sha3_context *ctx,
222 const uint8_t *input,
223 size_t ilen )
224{
225 if( ctx == NULL )
226 return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA );
227
228 if( ilen == 0 || input == NULL )
229 return( 0 );
230
231 while( ilen-- > 0 )
232 {
233 ABSORB( ctx, ctx->index, *input++ );
234 if( ( ctx->index = ( ctx->index + 1) % ctx->max_block_size ) == 0 )
235 keccak_f1600( ctx );
236 }
237
238 return( 0 );
239}
240
241int mbedtls_sha3_finish( mbedtls_sha3_context *ctx,
242 uint8_t *output, size_t olen )
243{
Pol Henarejos85eeda02022-05-17 11:43:15 +0200244 if( ctx == NULL || output == NULL )
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +0200245 return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA );
246
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +0200247 if( ctx->olen > 0 && ctx->olen != olen )
248 return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA );
249
250 ABSORB( ctx, ctx->index, ctx->xor_byte );
251 ABSORB( ctx, ctx->max_block_size - 1, 0x80 );
252 keccak_f1600( ctx );
253 ctx->index = 0;
254
255 while( olen-- > 0 )
256 {
257 *output++ = SQUEEZE( ctx, ctx->index );
258
259 if( ( ctx->index = ( ctx->index + 1) % ctx->max_block_size ) == 0 )
260 keccak_f1600( ctx );
261 }
262
263 return( 0 );
264}
265
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +0200266/*
267 * output = SHA3( input buffer )
268 */
269int mbedtls_sha3( mbedtls_sha3_id id, const uint8_t *input,
270 size_t ilen, uint8_t *output, size_t olen )
271{
272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
273 mbedtls_sha3_context ctx;
274
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +0200275 mbedtls_sha3_init( &ctx );
276
Pol Henarejos85eeda02022-05-17 11:43:15 +0200277 /* Sanity checks are performed in every mbedtls_sha3_xxx() */
Pol Henarejos0cd1f1c2022-05-09 01:04:15 +0200278 if( ( ret = mbedtls_sha3_starts( &ctx, id ) ) != 0 )
279 goto exit;
280
281 if( ( ret = mbedtls_sha3_update( &ctx, input, ilen ) ) != 0 )
282 goto exit;
283
284 if( ( ret = mbedtls_sha3_finish( &ctx, output, olen ) ) != 0 )
285 goto exit;
286
287exit:
288 mbedtls_sha3_free( &ctx );
289
290 return( ret );
291}
292
293#endif /* MBEDTLS_SHA3_C */