Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 1 | /* |
| 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 Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 47 | /* |
| 48 | * List of supported SHA-3 families |
| 49 | */ |
| 50 | static 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 | |
| 58 | static 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 | |
| 67 | static 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 | |
| 73 | static 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. */ |
| 84 | static 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 | |
| 166 | void 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 | |
| 174 | void 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 | |
| 182 | void 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 | */ |
| 194 | int 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 | |
Pol Henarejos | 116411e | 2022-05-17 11:45:59 +0200 | [diff] [blame] | 206 | if( p == NULL || p->id == MBEDTLS_SHA3_NONE ) |
Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 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 | |
Pol Henarejos | 938b5ab | 2022-05-20 16:01:07 +0200 | [diff] [blame^] | 215 | memset( ctx->state, 0, sizeof( ctx->state ) ); |
| 216 | ctx->index = 0; |
| 217 | |
Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 218 | return( 0 ); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * SHA-3 process buffer |
| 223 | */ |
| 224 | int mbedtls_sha3_update( mbedtls_sha3_context *ctx, |
| 225 | const uint8_t *input, |
| 226 | size_t ilen ) |
| 227 | { |
| 228 | if( ctx == NULL ) |
| 229 | return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA ); |
| 230 | |
| 231 | if( ilen == 0 || input == NULL ) |
| 232 | return( 0 ); |
| 233 | |
| 234 | while( ilen-- > 0 ) |
| 235 | { |
| 236 | ABSORB( ctx, ctx->index, *input++ ); |
| 237 | if( ( ctx->index = ( ctx->index + 1) % ctx->max_block_size ) == 0 ) |
| 238 | keccak_f1600( ctx ); |
| 239 | } |
| 240 | |
| 241 | return( 0 ); |
| 242 | } |
| 243 | |
| 244 | int mbedtls_sha3_finish( mbedtls_sha3_context *ctx, |
| 245 | uint8_t *output, size_t olen ) |
| 246 | { |
Pol Henarejos | 85eeda0 | 2022-05-17 11:43:15 +0200 | [diff] [blame] | 247 | if( ctx == NULL || output == NULL ) |
Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 248 | return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA ); |
| 249 | |
Pol Henarejos | 1f3ae16 | 2022-05-17 12:53:30 +0200 | [diff] [blame] | 250 | /* Catch SHA-3 families, with fixed output length */ |
| 251 | if( ctx->olen > 0 ) |
| 252 | { |
| 253 | if ( ctx->olen > olen ) |
| 254 | return( MBEDTLS_ERR_SHA3_BAD_INPUT_DATA ); |
| 255 | olen = ctx->olen; |
| 256 | } |
Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 257 | |
| 258 | ABSORB( ctx, ctx->index, ctx->xor_byte ); |
| 259 | ABSORB( ctx, ctx->max_block_size - 1, 0x80 ); |
| 260 | keccak_f1600( ctx ); |
| 261 | ctx->index = 0; |
| 262 | |
| 263 | while( olen-- > 0 ) |
| 264 | { |
| 265 | *output++ = SQUEEZE( ctx, ctx->index ); |
| 266 | |
| 267 | if( ( ctx->index = ( ctx->index + 1) % ctx->max_block_size ) == 0 ) |
| 268 | keccak_f1600( ctx ); |
| 269 | } |
| 270 | |
| 271 | return( 0 ); |
| 272 | } |
| 273 | |
Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 274 | /* |
| 275 | * output = SHA3( input buffer ) |
| 276 | */ |
| 277 | int mbedtls_sha3( mbedtls_sha3_id id, const uint8_t *input, |
| 278 | size_t ilen, uint8_t *output, size_t olen ) |
| 279 | { |
| 280 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 281 | mbedtls_sha3_context ctx; |
| 282 | |
Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 283 | mbedtls_sha3_init( &ctx ); |
| 284 | |
Pol Henarejos | 85eeda0 | 2022-05-17 11:43:15 +0200 | [diff] [blame] | 285 | /* Sanity checks are performed in every mbedtls_sha3_xxx() */ |
Pol Henarejos | 0cd1f1c | 2022-05-09 01:04:15 +0200 | [diff] [blame] | 286 | if( ( ret = mbedtls_sha3_starts( &ctx, id ) ) != 0 ) |
| 287 | goto exit; |
| 288 | |
| 289 | if( ( ret = mbedtls_sha3_update( &ctx, input, ilen ) ) != 0 ) |
| 290 | goto exit; |
| 291 | |
| 292 | if( ( ret = mbedtls_sha3_finish( &ctx, output, olen ) ) != 0 ) |
| 293 | goto exit; |
| 294 | |
| 295 | exit: |
| 296 | mbedtls_sha3_free( &ctx ); |
| 297 | |
| 298 | return( ret ); |
| 299 | } |
| 300 | |
| 301 | #endif /* MBEDTLS_SHA3_C */ |