Path: blob/main/sys/contrib/openzfs/module/icp/algs/edonr/edonr.c
48775 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Based on Edon-R implementation for SUPERCOP, based on NIST API.24* Copyright (c) 2009, 2010, Jørn Amundsen <[email protected]>25* Copyright (c) 2013 Saso Kiselkov, All rights reserved26* Copyright (c) 2023 Tino Reichardt <[email protected]>27*/2829#include <sys/zfs_context.h>30#include <sys/string.h>31#include <sys/edonr.h>3233/*34* We need 1196 byte stack for Q512() on i38635* - we define this pragma to make gcc happy36*/37#if defined(__GNUC__) && defined(_ILP32)38#pragma GCC diagnostic ignored "-Wframe-larger-than="39#endif4041/*42* Insert compiler memory barriers to reduce stack frame size.43*/44#define MEMORY_BARRIER asm volatile("" ::: "memory");4546#if defined(_ZFS_BIG_ENDIAN)47#define ld_swap64(s, d) (d = __builtin_bswap64(*(s)))48#define st_swap64(s, d) (*(d) = __builtin_bswap64(s))49#else50#define ld_swap64(s, d) (d = *(s))51#define st_swap64(s, d) (*(d) = s)52#endif5354#define hashState512(x) ((x)->pipe->p512)5556/* rotate shortcuts */57#define rotl64(x, n) (((x) << (n)) | ((x) >> (64 - (n))))5859/* EdonR512 initial double chaining pipe */60static const uint64_t i512p2[16] = {610x8081828384858687ull, 0x88898a8b8c8d8e8full,620x9091929394959697ull, 0x98999a9b9c9d9e9full,630xa0a1a2a3a4a5a6a7ull, 0xa8a9aaabacadaeafull,640xb0b1b2b3b4b5b6b7ull, 0xb8b9babbbcbdbebfull,650xc0c1c2c3c4c5c6c7ull, 0xc8c9cacbcccdcecfull,660xd0d1d2d3d4d5d6d7ull, 0xd8d9dadbdcdddedfull,670xe0e1e2e3e4e5e6e7ull, 0xe8e9eaebecedeeefull,680xf0f1f2f3f4f5f6f7ull, 0xf8f9fafbfcfdfeffull69};7071#define LS1_512(x0, x1, x2, x3, x4, x5, x6, x7) \72{ \73MEMORY_BARRIER \74z1 = x0 + x4, z2 = x1 + x7; z5 = z1 + z2; \75s0 = 0xaaaaaaaaaaaaaaaaull + z5 + x2; \76s1 = rotl64(z5 + x3, 5); \77s2 = rotl64(z5 + x6, 15); z3 = x2 + x3; \78s5 = rotl64(z1 + z3 + x5, 40); z4 = x5 + x6; \79s6 = rotl64(z2 + z4 + x0, 50); z6 = z3 + z4; \80s3 = rotl64(z6 + x7, 22); \81s4 = rotl64(z6 + x1, 31); \82s7 = rotl64(z6 + x4, 59); \83}8485#define LS2_512(y0, y1, y2, y3, y4, y5, y6, y7) \86{ \87z1 = y0 + y1, z2 = y2 + y5; z6 = z1 + z2; \88t0 = ~0xaaaaaaaaaaaaaaaaull + z6 + y7; \89t2 = rotl64(z6 + y3, 19); \90z3 = y3 + y4, z5 = z1 + z3; \91t1 = rotl64(z5 + y6, 10); \92t4 = rotl64(z5 + y5, 36); \93z4 = y6 + y7, z8 = z3 + z4; \94t3 = rotl64(z8 + y2, 29); \95t7 = rotl64(z8 + y0, 55); z7 = z2 + z4; \96t5 = rotl64(z7 + y4, 44); \97t6 = rotl64(z7 + y1, 48); \98}99100#define QEF_512(r0, r1, r2, r3, r4, r5, r6, r7) \101{ \102z1 = s0 ^ s4, z5 = t0 ^ t1; \103r0 = (z1 ^ s1) + (z5 ^ t5); z8 = t6 ^ t7; \104r1 = (z1 ^ s7) + (t2 ^ z8); z3 = s2 ^ s3; \105r7 = (z3 ^ s5) + (t4 ^ z8); z7 = t3 ^ t4; \106r3 = (z3 ^ s4) + (t0 ^ z7); z4 = s5 ^ s6; \107r5 = (s3 ^ z4) + (z7 ^ t6); z6 = t2 ^ t5; \108r6 = (s2 ^ z4) + (z6 ^ t7); z2 = s1 ^ s7; \109r4 = (s0 ^ z2) + (t1 ^ z6); \110r2 = (z2 ^ s6) + (z5 ^ t3); \111}112113static inline size_t114Q512(size_t bitlen, const uint64_t *data, uint64_t *p)115{116size_t bl;117118for (bl = bitlen; bl >= EdonR512_BLOCK_BITSIZE;119bl -= EdonR512_BLOCK_BITSIZE, data += 16) {120uint64_t q0, q1, q2, q3, q4, q5, q6, q7;121uint64_t p0, p1, p2, p3, p4, p5, p6, p7;122uint64_t s0, s1, s2, s3, s4, s5, s6, s7;123uint64_t t0, t1, t2, t3, t4, t5, t6, t7;124uint64_t z1, z2, z3, z4, z5, z6, z7, z8;125126#if defined(_ZFS_BIG_ENDIAN)127uint64_t swp0, swp1, swp2, swp3, swp4, swp5, swp6, swp7,128swp8, swp9, swp10, swp11, swp12, swp13, swp14, swp15;129#define d(j) swp##j130#define s64(j) ld_swap64((uint64_t *)data+j, swp##j)131s64(0);132s64(1);133s64(2);134s64(3);135s64(4);136s64(5);137s64(6);138s64(7);139s64(8);140s64(9);141s64(10);142s64(11);143s64(12);144s64(13);145s64(14);146s64(15);147#else148#define d(j) data[j]149#endif150151/* First row of quasigroup e-transformations */152LS1_512(d(15), d(14), d(13), d(12), d(11), d(10), d(9), d(8));153LS2_512(d(0), d(1), d(2), d(3), d(4), d(5), d(6), d(7));154QEF_512(p0, p1, p2, p3, p4, p5, p6, p7);155156LS1_512(p0, p1, p2, p3, p4, p5, p6, p7);157LS2_512(d(8), d(9), d(10), d(11), d(12), d(13), d(14), d(15));158QEF_512(q0, q1, q2, q3, q4, q5, q6, q7);159160/* Second row of quasigroup e-transformations */161LS1_512(p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);162LS2_512(p0, p1, p2, p3, p4, p5, p6, p7);163QEF_512(p0, p1, p2, p3, p4, p5, p6, p7);164165LS1_512(p0, p1, p2, p3, p4, p5, p6, p7);166LS2_512(q0, q1, q2, q3, q4, q5, q6, q7);167QEF_512(q0, q1, q2, q3, q4, q5, q6, q7);168169/* Third row of quasigroup e-transformations */170LS1_512(p0, p1, p2, p3, p4, p5, p6, p7);171LS2_512(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);172QEF_512(p0, p1, p2, p3, p4, p5, p6, p7);173174LS1_512(q0, q1, q2, q3, q4, q5, q6, q7);175LS2_512(p0, p1, p2, p3, p4, p5, p6, p7);176QEF_512(q0, q1, q2, q3, q4, q5, q6, q7);177178/* Fourth row of quasigroup e-transformations */179LS1_512(d(7), d(6), d(5), d(4), d(3), d(2), d(1), d(0));180LS2_512(p0, p1, p2, p3, p4, p5, p6, p7);181QEF_512(p0, p1, p2, p3, p4, p5, p6, p7);182183LS1_512(p0, p1, p2, p3, p4, p5, p6, p7);184LS2_512(q0, q1, q2, q3, q4, q5, q6, q7);185QEF_512(q0, q1, q2, q3, q4, q5, q6, q7);186187/* Edon-R tweak on the original SHA-3 Edon-R submission. */188p[0] ^= d(8) ^ p0;189p[1] ^= d(9) ^ p1;190p[2] ^= d(10) ^ p2;191p[3] ^= d(11) ^ p3;192p[4] ^= d(12) ^ p4;193p[5] ^= d(13) ^ p5;194p[6] ^= d(14) ^ p6;195p[7] ^= d(15) ^ p7;196p[8] ^= d(0) ^ q0;197p[9] ^= d(1) ^ q1;198p[10] ^= d(2) ^ q2;199p[11] ^= d(3) ^ q3;200p[12] ^= d(4) ^ q4;201p[13] ^= d(5) ^ q5;202p[14] ^= d(6) ^ q6;203p[15] ^= d(7) ^ q7;204}205206#undef s64207#undef d208return (bitlen - bl);209}210211void212EdonRInit(EdonRState *state)213{214state->bits_processed = 0;215state->unprocessed_bits = 0;216memcpy(hashState512(state)->DoublePipe, i512p2, sizeof (i512p2));217}218219void220EdonRUpdate(EdonRState *state, const uint8_t *data, size_t databitlen)221{222uint64_t *data64;223size_t bits_processed;224225if (state->unprocessed_bits > 0) {226/* LastBytes = databitlen / 8 */227int LastBytes = (int)databitlen >> 3;228229ASSERT(state->unprocessed_bits + databitlen <=230EdonR512_BLOCK_SIZE * 8);231232memcpy(hashState512(state)->LastPart233+ (state->unprocessed_bits >> 3), data, LastBytes);234state->unprocessed_bits += (int)databitlen;235databitlen = state->unprocessed_bits;236/* LINTED E_BAD_PTR_CAST_ALIGN */237data64 = (uint64_t *)hashState512(state)->LastPart;238} else239/* LINTED E_BAD_PTR_CAST_ALIGN */240data64 = (uint64_t *)data;241242bits_processed = Q512(databitlen, data64,243hashState512(state)->DoublePipe);244state->bits_processed += bits_processed;245databitlen -= bits_processed;246state->unprocessed_bits = (int)databitlen;247if (databitlen > 0) {248/* LastBytes = Ceil(databitlen / 8) */249int LastBytes = ((~(((-(int)databitlen) >> 3) & 0x03ff)) + 1) \250& 0x03ff;251252data64 += bits_processed >> 6; /* byte size update */253memmove(hashState512(state)->LastPart, data64, LastBytes);254}255}256257void258EdonRFinal(EdonRState *state, uint8_t *hashval)259{260uint64_t *data64, num_bits;261size_t databitlen;262int LastByte, PadOnePosition;263264num_bits = state->bits_processed + state->unprocessed_bits;265LastByte = (int)state->unprocessed_bits >> 3;266PadOnePosition = 7 - (state->unprocessed_bits & 0x07);267hashState512(state)->LastPart[LastByte] =268(hashState512(state)->LastPart[LastByte] \269& (0xff << (PadOnePosition + 1))) ^ (0x01 << PadOnePosition);270/* LINTED E_BAD_PTR_CAST_ALIGN */271data64 = (uint64_t *)hashState512(state)->LastPart;272273if (state->unprocessed_bits < 960) {274memset((hashState512(state)->LastPart) +275LastByte + 1, 0x00, EdonR512_BLOCK_SIZE - LastByte - 9);276databitlen = EdonR512_BLOCK_SIZE * 8;277#if defined(_ZFS_BIG_ENDIAN)278st_swap64(num_bits, data64 + 15);279#else280data64[15] = num_bits;281#endif282} else {283memset((hashState512(state)->LastPart) + LastByte + 1,2840x00, EdonR512_BLOCK_SIZE * 2 - LastByte - 9);285databitlen = EdonR512_BLOCK_SIZE * 16;286#if defined(_ZFS_BIG_ENDIAN)287st_swap64(num_bits, data64 + 31);288#else289data64[31] = num_bits;290#endif291}292293state->bits_processed += Q512(databitlen, data64,294hashState512(state)->DoublePipe);295296#if defined(_ZFS_BIG_ENDIAN)297data64 = (uint64_t *)hashval;298uint64_t *s64 = hashState512(state)->DoublePipe + 8;299int j;300301for (j = 0; j < EdonR512_DIGEST_SIZE >> 3; j++)302st_swap64(s64[j], data64 + j);303#else304memcpy(hashval, hashState512(state)->DoublePipe + 8,305EdonR512_DIGEST_SIZE);306#endif307}308309void310EdonRHash(const uint8_t *data, size_t databitlen, uint8_t *hashval)311{312EdonRState state;313314EdonRInit(&state);315EdonRUpdate(&state, data, databitlen);316EdonRFinal(&state, hashval);317}318319#ifdef _KERNEL320EXPORT_SYMBOL(EdonRInit);321EXPORT_SYMBOL(EdonRUpdate);322EXPORT_SYMBOL(EdonRHash);323EXPORT_SYMBOL(EdonRFinal);324#endif325326327