1 /* $OpenBSD: sha3_internal.h,v 1.14 2023/04/16 17:06:19 jsing Exp $ */ 2 /* 3 * The MIT License (MIT) 4 * 5 * Copyright (c) 2015 Markku-Juhani O. Saarinen 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a copy 8 * of this software and associated documentation files (the "Software"), to deal 9 * in the Software without restriction, including without limitation the rights 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 * copies of the Software, and to permit persons to whom the Software is 12 * furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in all 15 * copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 * SOFTWARE. 24 */ 25 26 #include <stddef.h> 27 #include <stdint.h> 28 29 #ifndef HEADER_SHA3_INTERNAL_H 30 #define HEADER_SHA3_INTERNAL_H 31 32 #define NID_sha3_224 1031 33 #define NID_sha3_256 1032 34 #define NID_sha3_384 1033 35 #define NID_sha3_512 1034 36 37 #define NID_RSA_SHA3_224 1049 38 #define NID_RSA_SHA3_256 1050 39 #define NID_RSA_SHA3_384 1051 40 #define NID_RSA_SHA3_512 1052 41 42 #define KECCAK_BIT_WIDTH 1600 43 #define KECCAK_BYTE_WIDTH (KECCAK_BIT_WIDTH / 8) 44 45 #define SHA3_224_BIT_LENGTH 224 46 #define SHA3_224_BITRATE (2 * SHA3_224_BIT_LENGTH) 47 #define SHA3_224_CAPACITY (KECCAK_BIT_WIDTH - SHA3_224_BITRATE) 48 #define SHA3_224_BLOCK_SIZE (SHA3_224_CAPACITY / 8) 49 #define SHA3_224_DIGEST_LENGTH (SHA3_224_BIT_LENGTH / 8) 50 51 #define SHA3_256_BIT_LENGTH 256 52 #define SHA3_256_BITRATE (2 * SHA3_256_BIT_LENGTH) 53 #define SHA3_256_CAPACITY (KECCAK_BIT_WIDTH - SHA3_256_BITRATE) 54 #define SHA3_256_BLOCK_SIZE (SHA3_256_CAPACITY / 8) 55 #define SHA3_256_DIGEST_LENGTH (SHA3_256_BIT_LENGTH / 8) 56 57 #define SHA3_384_BIT_LENGTH 384 58 #define SHA3_384_BITRATE (2 * SHA3_384_BIT_LENGTH) 59 #define SHA3_384_CAPACITY (KECCAK_BIT_WIDTH - SHA3_384_BITRATE) 60 #define SHA3_384_BLOCK_SIZE (SHA3_384_CAPACITY / 8) 61 #define SHA3_384_DIGEST_LENGTH (SHA3_384_BIT_LENGTH / 8) 62 63 #define SHA3_512_BIT_LENGTH 512 64 #define SHA3_512_BITRATE (2 * SHA3_512_BIT_LENGTH) 65 #define SHA3_512_CAPACITY (KECCAK_BIT_WIDTH - SHA3_512_BITRATE) 66 #define SHA3_512_BLOCK_SIZE (SHA3_512_CAPACITY / 8) 67 #define SHA3_512_DIGEST_LENGTH (SHA3_512_BIT_LENGTH / 8) 68 69 typedef struct sha3_ctx_st { 70 union { 71 uint8_t b[200]; /* State as 8 bit bytes. */ 72 uint64_t q[25]; /* State as 64 bit words. */ 73 } state; 74 size_t pt; 75 size_t rsize; 76 size_t mdlen; 77 } sha3_ctx; 78 79 int sha3_init(sha3_ctx *c, int mdlen); 80 int sha3_update(sha3_ctx *c, const void *data, size_t len); 81 int sha3_final(void *md, sha3_ctx *c); 82 83 /* SHAKE128 and SHAKE256 extensible-output functions. */ 84 #define shake128_init(c) sha3_init(c, 16) 85 #define shake256_init(c) sha3_init(c, 32) 86 #define shake_update sha3_update 87 88 void shake_xof(sha3_ctx *c); 89 void shake_out(sha3_ctx *c, void *out, size_t len); 90 91 #endif 92