Path: blob/master/sha3/haval_helper.c
1299 views
/* $Id: haval_helper.c 218 2010-06-08 17:06:34Z tp $ */1/*2* Helper code, included (three times !) by HAVAL implementation.3*4* TODO: try to merge this with md_helper.c.5*6* ==========================(LICENSE BEGIN)============================7*8* Copyright (c) 2007-2010 Projet RNRT SAPHIR9*10* Permission is hereby granted, free of charge, to any person obtaining11* a copy of this software and associated documentation files (the12* "Software"), to deal in the Software without restriction, including13* without limitation the rights to use, copy, modify, merge, publish,14* distribute, sublicense, and/or sell copies of the Software, and to15* permit persons to whom the Software is furnished to do so, subject to16* the following conditions:17*18* The above copyright notice and this permission notice shall be19* included in all copies or substantial portions of the Software.20*21* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,22* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF23* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.24* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY25* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,26* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE27* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.28*29* ===========================(LICENSE END)=============================30*31* @author Thomas Pornin <[email protected]>32*/3334#undef SPH_XCAT35#define SPH_XCAT(a, b) SPH_XCAT_(a, b)36#undef SPH_XCAT_37#define SPH_XCAT_(a, b) a ## b3839static void40#ifdef SPH_UPTR41SPH_XCAT(SPH_XCAT(haval, PASSES), _short)42#else43SPH_XCAT(haval, PASSES)44#endif45(sph_haval_context *sc, const void *data, size_t len)46{47unsigned current;4849#if SPH_6450current = (unsigned)sc->count & 127U;51#else52current = (unsigned)sc->count_low & 127U;53#endif54while (len > 0) {55unsigned clen;56#if !SPH_6457sph_u32 clow, clow2;58#endif5960clen = 128U - current;61if (clen > len)62clen = (unsigned) len;63memcpy(sc->buf + current, data, clen);64data = (const unsigned char *)data + clen;65current += clen;66len -= clen;67if (current == 128U) {68DSTATE;69IN_PREPARE(sc->buf);7071RSTATE;72SPH_XCAT(CORE, PASSES)(INW);73WSTATE;74current = 0;75}76#if SPH_6477sc->count += clen;78#else79clow = sc->count_low;80clow2 = SPH_T32(clow + clen);81sc->count_low = clow2;82if (clow2 < clow)83sc->count_high ++;84#endif85}86}8788#ifdef SPH_UPTR89static void90SPH_XCAT(haval, PASSES)(sph_haval_context *sc, const void *data, size_t len)91{92unsigned current;93size_t orig_len;94#if !SPH_6495sph_u32 clow, clow2;96#endif97DSTATE;9899if (len < 256U) {100SPH_XCAT(SPH_XCAT(haval, PASSES), _short)(sc, data, len);101return;102}103#if SPH_64104current = (unsigned)sc->count & 127U;105#else106current = (unsigned)sc->count_low & 127U;107#endif108if (current > 0) {109unsigned clen;110111clen = 128U - current;112SPH_XCAT(SPH_XCAT(haval, PASSES), _short)(sc, data, clen);113data = (const unsigned char *)data + clen;114len -= clen;115}116#if !SPH_UNALIGNED117if (((SPH_UPTR)data & 3U) != 0) {118SPH_XCAT(SPH_XCAT(haval, PASSES), _short)(sc, data, len);119return;120}121#endif122orig_len = len;123RSTATE;124while (len >= 128U) {125IN_PREPARE(data);126127SPH_XCAT(CORE, PASSES)(INW);128data = (const unsigned char *)data + 128U;129len -= 128U;130}131WSTATE;132if (len > 0)133memcpy(sc->buf, data, len);134#if SPH_64135sc->count += (sph_u64)orig_len;136#else137clow = sc->count_low;138clow2 = SPH_T32(clow + orig_len);139sc->count_low = clow2;140if (clow2 < clow)141sc->count_high ++;142orig_len >>= 12;143orig_len >>= 10;144orig_len >>= 10;145sc->count_high += orig_len;146#endif147}148#endif149150static void151SPH_XCAT(SPH_XCAT(haval, PASSES), _close)(sph_haval_context *sc,152unsigned ub, unsigned n, void *dst)153{154unsigned current;155DSTATE;156157#if SPH_64158current = (unsigned)sc->count & 127U;159#else160current = (unsigned)sc->count_low & 127U;161#endif162sc->buf[current ++] = (0x01 << n) | ((ub & 0xFF) >> (8 - n));163RSTATE;164if (current > 118U) {165memset(sc->buf + current, 0, 128U - current);166167do {168IN_PREPARE(sc->buf);169170SPH_XCAT(CORE, PASSES)(INW);171} while (0);172current = 0;173}174memset(sc->buf + current, 0, 118U - current);175sc->buf[118] = 0x01 | (PASSES << 3);176sc->buf[119] = sc->olen << 3;177#if SPH_64178sph_enc64le_aligned(sc->buf + 120, SPH_T64(sc->count << 3));179#else180sph_enc32le_aligned(sc->buf + 120, SPH_T32(sc->count_low << 3));181sph_enc32le_aligned(sc->buf + 124,182SPH_T32((sc->count_high << 3) | (sc->count_low >> 29)));183#endif184do {185IN_PREPARE(sc->buf);186187SPH_XCAT(CORE, PASSES)(INW);188} while (0);189190WSTATE;191haval_out(sc, dst);192haval_init(sc, sc->olen, sc->passes);193}194195196197