Path: blob/master/sha3/sph_shabal.c
1299 views
/* $Id: shabal.c 175 2010-05-07 16:03:20Z tp $ */1/*2* Shabal implementation.3*4* ==========================(LICENSE BEGIN)============================5*6* Copyright (c) 2007-2010 Projet RNRT SAPHIR7*8* Permission is hereby granted, free of charge, to any person obtaining9* a copy of this software and associated documentation files (the10* "Software"), to deal in the Software without restriction, including11* without limitation the rights to use, copy, modify, merge, publish,12* distribute, sublicense, and/or sell copies of the Software, and to13* permit persons to whom the Software is furnished to do so, subject to14* the following conditions:15*16* The above copyright notice and this permission notice shall be17* included in all copies or substantial portions of the Software.18*19* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,20* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF21* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.22* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY23* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,24* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE25* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.26*27* ===========================(LICENSE END)=============================28*29* @author Thomas Pornin <[email protected]>30*/3132#include <stddef.h>33#include <string.h>3435#include "sph_shabal.h"36#ifdef __cplusplus37extern "C"{38#endif3940#ifdef _MSC_VER41#pragma warning (disable: 4146)42#endif4344/*45* Part of this code was automatically generated (the part between46* the "BEGIN" and "END" markers).47*/4849#define sM 165051#define C32 SPH_C3252#define T32 SPH_T325354#define O1 1355#define O2 956#define O3 65758/*59* We copy the state into local variables, so that the compiler knows60* that it can optimize them at will.61*/6263/* BEGIN -- automatically generated code. */6465#define DECL_STATE \66sph_u32 A00, A01, A02, A03, A04, A05, A06, A07, \67A08, A09, A0A, A0B; \68sph_u32 B0, B1, B2, B3, B4, B5, B6, B7, \69B8, B9, BA, BB, BC, BD, BE, BF; \70sph_u32 C0, C1, C2, C3, C4, C5, C6, C7, \71C8, C9, CA, CB, CC, CD, CE, CF; \72sph_u32 M0, M1, M2, M3, M4, M5, M6, M7, \73M8, M9, MA, MB, MC, MD, ME, MF; \74sph_u32 Wlow, Whigh;7576#define READ_STATE(state) do { \77A00 = (state)->A[0]; \78A01 = (state)->A[1]; \79A02 = (state)->A[2]; \80A03 = (state)->A[3]; \81A04 = (state)->A[4]; \82A05 = (state)->A[5]; \83A06 = (state)->A[6]; \84A07 = (state)->A[7]; \85A08 = (state)->A[8]; \86A09 = (state)->A[9]; \87A0A = (state)->A[10]; \88A0B = (state)->A[11]; \89B0 = (state)->B[0]; \90B1 = (state)->B[1]; \91B2 = (state)->B[2]; \92B3 = (state)->B[3]; \93B4 = (state)->B[4]; \94B5 = (state)->B[5]; \95B6 = (state)->B[6]; \96B7 = (state)->B[7]; \97B8 = (state)->B[8]; \98B9 = (state)->B[9]; \99BA = (state)->B[10]; \100BB = (state)->B[11]; \101BC = (state)->B[12]; \102BD = (state)->B[13]; \103BE = (state)->B[14]; \104BF = (state)->B[15]; \105C0 = (state)->C[0]; \106C1 = (state)->C[1]; \107C2 = (state)->C[2]; \108C3 = (state)->C[3]; \109C4 = (state)->C[4]; \110C5 = (state)->C[5]; \111C6 = (state)->C[6]; \112C7 = (state)->C[7]; \113C8 = (state)->C[8]; \114C9 = (state)->C[9]; \115CA = (state)->C[10]; \116CB = (state)->C[11]; \117CC = (state)->C[12]; \118CD = (state)->C[13]; \119CE = (state)->C[14]; \120CF = (state)->C[15]; \121Wlow = (state)->Wlow; \122Whigh = (state)->Whigh; \123} while (0)124125#define WRITE_STATE(state) do { \126(state)->A[0] = A00; \127(state)->A[1] = A01; \128(state)->A[2] = A02; \129(state)->A[3] = A03; \130(state)->A[4] = A04; \131(state)->A[5] = A05; \132(state)->A[6] = A06; \133(state)->A[7] = A07; \134(state)->A[8] = A08; \135(state)->A[9] = A09; \136(state)->A[10] = A0A; \137(state)->A[11] = A0B; \138(state)->B[0] = B0; \139(state)->B[1] = B1; \140(state)->B[2] = B2; \141(state)->B[3] = B3; \142(state)->B[4] = B4; \143(state)->B[5] = B5; \144(state)->B[6] = B6; \145(state)->B[7] = B7; \146(state)->B[8] = B8; \147(state)->B[9] = B9; \148(state)->B[10] = BA; \149(state)->B[11] = BB; \150(state)->B[12] = BC; \151(state)->B[13] = BD; \152(state)->B[14] = BE; \153(state)->B[15] = BF; \154(state)->C[0] = C0; \155(state)->C[1] = C1; \156(state)->C[2] = C2; \157(state)->C[3] = C3; \158(state)->C[4] = C4; \159(state)->C[5] = C5; \160(state)->C[6] = C6; \161(state)->C[7] = C7; \162(state)->C[8] = C8; \163(state)->C[9] = C9; \164(state)->C[10] = CA; \165(state)->C[11] = CB; \166(state)->C[12] = CC; \167(state)->C[13] = CD; \168(state)->C[14] = CE; \169(state)->C[15] = CF; \170(state)->Wlow = Wlow; \171(state)->Whigh = Whigh; \172} while (0)173174#define DECODE_BLOCK do { \175M0 = sph_dec32le_aligned(buf + 0); \176M1 = sph_dec32le_aligned(buf + 4); \177M2 = sph_dec32le_aligned(buf + 8); \178M3 = sph_dec32le_aligned(buf + 12); \179M4 = sph_dec32le_aligned(buf + 16); \180M5 = sph_dec32le_aligned(buf + 20); \181M6 = sph_dec32le_aligned(buf + 24); \182M7 = sph_dec32le_aligned(buf + 28); \183M8 = sph_dec32le_aligned(buf + 32); \184M9 = sph_dec32le_aligned(buf + 36); \185MA = sph_dec32le_aligned(buf + 40); \186MB = sph_dec32le_aligned(buf + 44); \187MC = sph_dec32le_aligned(buf + 48); \188MD = sph_dec32le_aligned(buf + 52); \189ME = sph_dec32le_aligned(buf + 56); \190MF = sph_dec32le_aligned(buf + 60); \191} while (0)192193#define INPUT_BLOCK_ADD do { \194B0 = T32(B0 + M0); \195B1 = T32(B1 + M1); \196B2 = T32(B2 + M2); \197B3 = T32(B3 + M3); \198B4 = T32(B4 + M4); \199B5 = T32(B5 + M5); \200B6 = T32(B6 + M6); \201B7 = T32(B7 + M7); \202B8 = T32(B8 + M8); \203B9 = T32(B9 + M9); \204BA = T32(BA + MA); \205BB = T32(BB + MB); \206BC = T32(BC + MC); \207BD = T32(BD + MD); \208BE = T32(BE + ME); \209BF = T32(BF + MF); \210} while (0)211212#define INPUT_BLOCK_SUB do { \213C0 = T32(C0 - M0); \214C1 = T32(C1 - M1); \215C2 = T32(C2 - M2); \216C3 = T32(C3 - M3); \217C4 = T32(C4 - M4); \218C5 = T32(C5 - M5); \219C6 = T32(C6 - M6); \220C7 = T32(C7 - M7); \221C8 = T32(C8 - M8); \222C9 = T32(C9 - M9); \223CA = T32(CA - MA); \224CB = T32(CB - MB); \225CC = T32(CC - MC); \226CD = T32(CD - MD); \227CE = T32(CE - ME); \228CF = T32(CF - MF); \229} while (0)230231#define XOR_W do { \232A00 ^= Wlow; \233A01 ^= Whigh; \234} while (0)235236#define SWAP(v1, v2) do { \237sph_u32 tmp = (v1); \238(v1) = (v2); \239(v2) = tmp; \240} while (0)241242#define SWAP_BC do { \243SWAP(B0, C0); \244SWAP(B1, C1); \245SWAP(B2, C2); \246SWAP(B3, C3); \247SWAP(B4, C4); \248SWAP(B5, C5); \249SWAP(B6, C6); \250SWAP(B7, C7); \251SWAP(B8, C8); \252SWAP(B9, C9); \253SWAP(BA, CA); \254SWAP(BB, CB); \255SWAP(BC, CC); \256SWAP(BD, CD); \257SWAP(BE, CE); \258SWAP(BF, CF); \259} while (0)260261#define PERM_ELT(xa0, xa1, xb0, xb1, xb2, xb3, xc, xm) do { \262xa0 = T32((xa0 \263^ (((xa1 << 15) | (xa1 >> 17)) * 5U) \264^ xc) * 3U) \265^ xb1 ^ (xb2 & ~xb3) ^ xm; \266xb0 = T32(~(((xb0 << 1) | (xb0 >> 31)) ^ xa0)); \267} while (0)268269#define PERM_STEP_0 do { \270PERM_ELT(A00, A0B, B0, BD, B9, B6, C8, M0); \271PERM_ELT(A01, A00, B1, BE, BA, B7, C7, M1); \272PERM_ELT(A02, A01, B2, BF, BB, B8, C6, M2); \273PERM_ELT(A03, A02, B3, B0, BC, B9, C5, M3); \274PERM_ELT(A04, A03, B4, B1, BD, BA, C4, M4); \275PERM_ELT(A05, A04, B5, B2, BE, BB, C3, M5); \276PERM_ELT(A06, A05, B6, B3, BF, BC, C2, M6); \277PERM_ELT(A07, A06, B7, B4, B0, BD, C1, M7); \278PERM_ELT(A08, A07, B8, B5, B1, BE, C0, M8); \279PERM_ELT(A09, A08, B9, B6, B2, BF, CF, M9); \280PERM_ELT(A0A, A09, BA, B7, B3, B0, CE, MA); \281PERM_ELT(A0B, A0A, BB, B8, B4, B1, CD, MB); \282PERM_ELT(A00, A0B, BC, B9, B5, B2, CC, MC); \283PERM_ELT(A01, A00, BD, BA, B6, B3, CB, MD); \284PERM_ELT(A02, A01, BE, BB, B7, B4, CA, ME); \285PERM_ELT(A03, A02, BF, BC, B8, B5, C9, MF); \286} while (0)287288#define PERM_STEP_1 do { \289PERM_ELT(A04, A03, B0, BD, B9, B6, C8, M0); \290PERM_ELT(A05, A04, B1, BE, BA, B7, C7, M1); \291PERM_ELT(A06, A05, B2, BF, BB, B8, C6, M2); \292PERM_ELT(A07, A06, B3, B0, BC, B9, C5, M3); \293PERM_ELT(A08, A07, B4, B1, BD, BA, C4, M4); \294PERM_ELT(A09, A08, B5, B2, BE, BB, C3, M5); \295PERM_ELT(A0A, A09, B6, B3, BF, BC, C2, M6); \296PERM_ELT(A0B, A0A, B7, B4, B0, BD, C1, M7); \297PERM_ELT(A00, A0B, B8, B5, B1, BE, C0, M8); \298PERM_ELT(A01, A00, B9, B6, B2, BF, CF, M9); \299PERM_ELT(A02, A01, BA, B7, B3, B0, CE, MA); \300PERM_ELT(A03, A02, BB, B8, B4, B1, CD, MB); \301PERM_ELT(A04, A03, BC, B9, B5, B2, CC, MC); \302PERM_ELT(A05, A04, BD, BA, B6, B3, CB, MD); \303PERM_ELT(A06, A05, BE, BB, B7, B4, CA, ME); \304PERM_ELT(A07, A06, BF, BC, B8, B5, C9, MF); \305} while (0)306307#define PERM_STEP_2 do { \308PERM_ELT(A08, A07, B0, BD, B9, B6, C8, M0); \309PERM_ELT(A09, A08, B1, BE, BA, B7, C7, M1); \310PERM_ELT(A0A, A09, B2, BF, BB, B8, C6, M2); \311PERM_ELT(A0B, A0A, B3, B0, BC, B9, C5, M3); \312PERM_ELT(A00, A0B, B4, B1, BD, BA, C4, M4); \313PERM_ELT(A01, A00, B5, B2, BE, BB, C3, M5); \314PERM_ELT(A02, A01, B6, B3, BF, BC, C2, M6); \315PERM_ELT(A03, A02, B7, B4, B0, BD, C1, M7); \316PERM_ELT(A04, A03, B8, B5, B1, BE, C0, M8); \317PERM_ELT(A05, A04, B9, B6, B2, BF, CF, M9); \318PERM_ELT(A06, A05, BA, B7, B3, B0, CE, MA); \319PERM_ELT(A07, A06, BB, B8, B4, B1, CD, MB); \320PERM_ELT(A08, A07, BC, B9, B5, B2, CC, MC); \321PERM_ELT(A09, A08, BD, BA, B6, B3, CB, MD); \322PERM_ELT(A0A, A09, BE, BB, B7, B4, CA, ME); \323PERM_ELT(A0B, A0A, BF, BC, B8, B5, C9, MF); \324} while (0)325326#define APPLY_P do { \327B0 = T32(B0 << 17) | (B0 >> 15); \328B1 = T32(B1 << 17) | (B1 >> 15); \329B2 = T32(B2 << 17) | (B2 >> 15); \330B3 = T32(B3 << 17) | (B3 >> 15); \331B4 = T32(B4 << 17) | (B4 >> 15); \332B5 = T32(B5 << 17) | (B5 >> 15); \333B6 = T32(B6 << 17) | (B6 >> 15); \334B7 = T32(B7 << 17) | (B7 >> 15); \335B8 = T32(B8 << 17) | (B8 >> 15); \336B9 = T32(B9 << 17) | (B9 >> 15); \337BA = T32(BA << 17) | (BA >> 15); \338BB = T32(BB << 17) | (BB >> 15); \339BC = T32(BC << 17) | (BC >> 15); \340BD = T32(BD << 17) | (BD >> 15); \341BE = T32(BE << 17) | (BE >> 15); \342BF = T32(BF << 17) | (BF >> 15); \343PERM_STEP_0; \344PERM_STEP_1; \345PERM_STEP_2; \346A0B = T32(A0B + C6); \347A0A = T32(A0A + C5); \348A09 = T32(A09 + C4); \349A08 = T32(A08 + C3); \350A07 = T32(A07 + C2); \351A06 = T32(A06 + C1); \352A05 = T32(A05 + C0); \353A04 = T32(A04 + CF); \354A03 = T32(A03 + CE); \355A02 = T32(A02 + CD); \356A01 = T32(A01 + CC); \357A00 = T32(A00 + CB); \358A0B = T32(A0B + CA); \359A0A = T32(A0A + C9); \360A09 = T32(A09 + C8); \361A08 = T32(A08 + C7); \362A07 = T32(A07 + C6); \363A06 = T32(A06 + C5); \364A05 = T32(A05 + C4); \365A04 = T32(A04 + C3); \366A03 = T32(A03 + C2); \367A02 = T32(A02 + C1); \368A01 = T32(A01 + C0); \369A00 = T32(A00 + CF); \370A0B = T32(A0B + CE); \371A0A = T32(A0A + CD); \372A09 = T32(A09 + CC); \373A08 = T32(A08 + CB); \374A07 = T32(A07 + CA); \375A06 = T32(A06 + C9); \376A05 = T32(A05 + C8); \377A04 = T32(A04 + C7); \378A03 = T32(A03 + C6); \379A02 = T32(A02 + C5); \380A01 = T32(A01 + C4); \381A00 = T32(A00 + C3); \382} while (0)383384#define INCR_W do { \385if ((Wlow = T32(Wlow + 1)) == 0) \386Whigh = T32(Whigh + 1); \387} while (0)388389static const sph_u32 A_init_192[] = {390C32(0xFD749ED4), C32(0xB798E530), C32(0x33904B6F), C32(0x46BDA85E),391C32(0x076934B4), C32(0x454B4058), C32(0x77F74527), C32(0xFB4CF465),392C32(0x62931DA9), C32(0xE778C8DB), C32(0x22B3998E), C32(0xAC15CFB9)393};394395static const sph_u32 B_init_192[] = {396C32(0x58BCBAC4), C32(0xEC47A08E), C32(0xAEE933B2), C32(0xDFCBC824),397C32(0xA7944804), C32(0xBF65BDB0), C32(0x5A9D4502), C32(0x59979AF7),398C32(0xC5CEA54E), C32(0x4B6B8150), C32(0x16E71909), C32(0x7D632319),399C32(0x930573A0), C32(0xF34C63D1), C32(0xCAF914B4), C32(0xFDD6612C)400};401402static const sph_u32 C_init_192[] = {403C32(0x61550878), C32(0x89EF2B75), C32(0xA1660C46), C32(0x7EF3855B),404C32(0x7297B58C), C32(0x1BC67793), C32(0x7FB1C723), C32(0xB66FC640),405C32(0x1A48B71C), C32(0xF0976D17), C32(0x088CE80A), C32(0xA454EDF3),406C32(0x1C096BF4), C32(0xAC76224B), C32(0x5215781C), C32(0xCD5D2669)407};408409static const sph_u32 A_init_224[] = {410C32(0xA5201467), C32(0xA9B8D94A), C32(0xD4CED997), C32(0x68379D7B),411C32(0xA7FC73BA), C32(0xF1A2546B), C32(0x606782BF), C32(0xE0BCFD0F),412C32(0x2F25374E), C32(0x069A149F), C32(0x5E2DFF25), C32(0xFAECF061)413};414415static const sph_u32 B_init_224[] = {416C32(0xEC9905D8), C32(0xF21850CF), C32(0xC0A746C8), C32(0x21DAD498),417C32(0x35156EEB), C32(0x088C97F2), C32(0x26303E40), C32(0x8A2D4FB5),418C32(0xFEEE44B6), C32(0x8A1E9573), C32(0x7B81111A), C32(0xCBC139F0),419C32(0xA3513861), C32(0x1D2C362E), C32(0x918C580E), C32(0xB58E1B9C)420};421422static const sph_u32 C_init_224[] = {423C32(0xE4B573A1), C32(0x4C1A0880), C32(0x1E907C51), C32(0x04807EFD),424C32(0x3AD8CDE5), C32(0x16B21302), C32(0x02512C53), C32(0x2204CB18),425C32(0x99405F2D), C32(0xE5B648A1), C32(0x70AB1D43), C32(0xA10C25C2),426C32(0x16F1AC05), C32(0x38BBEB56), C32(0x9B01DC60), C32(0xB1096D83)427};428429static const sph_u32 A_init_256[] = {430C32(0x52F84552), C32(0xE54B7999), C32(0x2D8EE3EC), C32(0xB9645191),431C32(0xE0078B86), C32(0xBB7C44C9), C32(0xD2B5C1CA), C32(0xB0D2EB8C),432C32(0x14CE5A45), C32(0x22AF50DC), C32(0xEFFDBC6B), C32(0xEB21B74A)433};434435static const sph_u32 B_init_256[] = {436C32(0xB555C6EE), C32(0x3E710596), C32(0xA72A652F), C32(0x9301515F),437C32(0xDA28C1FA), C32(0x696FD868), C32(0x9CB6BF72), C32(0x0AFE4002),438C32(0xA6E03615), C32(0x5138C1D4), C32(0xBE216306), C32(0xB38B8890),439C32(0x3EA8B96B), C32(0x3299ACE4), C32(0x30924DD4), C32(0x55CB34A5)440};441442static const sph_u32 C_init_256[] = {443C32(0xB405F031), C32(0xC4233EBA), C32(0xB3733979), C32(0xC0DD9D55),444C32(0xC51C28AE), C32(0xA327B8E1), C32(0x56C56167), C32(0xED614433),445C32(0x88B59D60), C32(0x60E2CEBA), C32(0x758B4B8B), C32(0x83E82A7F),446C32(0xBC968828), C32(0xE6E00BF7), C32(0xBA839E55), C32(0x9B491C60)447};448449static const sph_u32 A_init_384[] = {450C32(0xC8FCA331), C32(0xE55C504E), C32(0x003EBF26), C32(0xBB6B8D83),451C32(0x7B0448C1), C32(0x41B82789), C32(0x0A7C9601), C32(0x8D659CFF),452C32(0xB6E2673E), C32(0xCA54C77B), C32(0x1460FD7E), C32(0x3FCB8F2D)453};454455static const sph_u32 B_init_384[] = {456C32(0x527291FC), C32(0x2A16455F), C32(0x78E627E5), C32(0x944F169F),457C32(0x1CA6F016), C32(0xA854EA25), C32(0x8DB98ABE), C32(0xF2C62641),458C32(0x30117DCB), C32(0xCF5C4309), C32(0x93711A25), C32(0xF9F671B8),459C32(0xB01D2116), C32(0x333F4B89), C32(0xB285D165), C32(0x86829B36)460};461462static const sph_u32 C_init_384[] = {463C32(0xF764B11A), C32(0x76172146), C32(0xCEF6934D), C32(0xC6D28399),464C32(0xFE095F61), C32(0x5E6018B4), C32(0x5048ECF5), C32(0x51353261),465C32(0x6E6E36DC), C32(0x63130DAD), C32(0xA9C69BD6), C32(0x1E90EA0C),466C32(0x7C35073B), C32(0x28D95E6D), C32(0xAA340E0D), C32(0xCB3DEE70)467};468469static const sph_u32 A_init_512[] = {470C32(0x20728DFD), C32(0x46C0BD53), C32(0xE782B699), C32(0x55304632),471C32(0x71B4EF90), C32(0x0EA9E82C), C32(0xDBB930F1), C32(0xFAD06B8B),472C32(0xBE0CAE40), C32(0x8BD14410), C32(0x76D2ADAC), C32(0x28ACAB7F)473};474475static const sph_u32 B_init_512[] = {476C32(0xC1099CB7), C32(0x07B385F3), C32(0xE7442C26), C32(0xCC8AD640),477C32(0xEB6F56C7), C32(0x1EA81AA9), C32(0x73B9D314), C32(0x1DE85D08),478C32(0x48910A5A), C32(0x893B22DB), C32(0xC5A0DF44), C32(0xBBC4324E),479C32(0x72D2F240), C32(0x75941D99), C32(0x6D8BDE82), C32(0xA1A7502B)480};481482static const sph_u32 C_init_512[] = {483C32(0xD9BF68D1), C32(0x58BAD750), C32(0x56028CB2), C32(0x8134F359),484C32(0xB5D469D8), C32(0x941A8CC2), C32(0x418B2A6E), C32(0x04052780),485C32(0x7F07D787), C32(0x5194358F), C32(0x3C60D665), C32(0xBE97D79A),486C32(0x950C3434), C32(0xAED9A06D), C32(0x2537DC8D), C32(0x7CDB5969)487};488489/* END -- automatically generated code. */490491static void492shabal_init(void *cc, unsigned size)493{494/*495* We have precomputed initial states for all the supported496* output bit lengths.497*/498const sph_u32 *A_init, *B_init, *C_init;499sph_shabal_context *sc;500501switch (size) {502case 192:503A_init = A_init_192;504B_init = B_init_192;505C_init = C_init_192;506break;507case 224:508A_init = A_init_224;509B_init = B_init_224;510C_init = C_init_224;511break;512case 256:513A_init = A_init_256;514B_init = B_init_256;515C_init = C_init_256;516break;517case 384:518A_init = A_init_384;519B_init = B_init_384;520C_init = C_init_384;521break;522case 512:523A_init = A_init_512;524B_init = B_init_512;525C_init = C_init_512;526break;527default:528return;529}530sc = cc;531memcpy(sc->A, A_init, sizeof sc->A);532memcpy(sc->B, B_init, sizeof sc->B);533memcpy(sc->C, C_init, sizeof sc->C);534sc->Wlow = 1;535sc->Whigh = 0;536sc->ptr = 0;537}538539static void540shabal_core(void *cc, const unsigned char *data, size_t len)541{542sph_shabal_context *sc;543unsigned char *buf;544size_t ptr;545DECL_STATE546547sc = cc;548buf = sc->buf;549ptr = sc->ptr;550551/*552* We do not want to copy the state to local variables if the553* amount of data is less than what is needed to complete the554* current block. Note that it is anyway suboptimal to call555* this method many times for small chunks of data.556*/557if (len < (sizeof sc->buf) - ptr) {558memcpy(buf + ptr, data, len);559ptr += len;560sc->ptr = ptr;561return;562}563564READ_STATE(sc);565while (len > 0) {566size_t clen;567568clen = (sizeof sc->buf) - ptr;569if (clen > len)570clen = len;571memcpy(buf + ptr, data, clen);572ptr += clen;573data += clen;574len -= clen;575if (ptr == sizeof sc->buf) {576DECODE_BLOCK;577INPUT_BLOCK_ADD;578XOR_W;579APPLY_P;580INPUT_BLOCK_SUB;581SWAP_BC;582INCR_W;583ptr = 0;584}585}586WRITE_STATE(sc);587sc->ptr = ptr;588}589590static void591shabal_close(void *cc, unsigned ub, unsigned n, void *dst, unsigned size_words)592{593sph_shabal_context *sc;594unsigned char *buf;595size_t ptr;596int i;597unsigned z;598union {599unsigned char tmp_out[64];600sph_u32 dummy;601} u;602size_t out_len;603DECL_STATE604605sc = cc;606buf = sc->buf;607ptr = sc->ptr;608z = 0x80 >> n;609buf[ptr] = ((ub & -z) | z) & 0xFF;610memset(buf + ptr + 1, 0, (sizeof sc->buf) - (ptr + 1));611READ_STATE(sc);612DECODE_BLOCK;613INPUT_BLOCK_ADD;614XOR_W;615APPLY_P;616for (i = 0; i < 3; i ++) {617SWAP_BC;618XOR_W;619APPLY_P;620}621622/*623* We just use our local variables; no need to go through624* the state structure. In order to share some code, we625* emit the relevant words into a temporary buffer, which626* we finally copy into the destination array.627*/628switch (size_words) {629case 16:630sph_enc32le_aligned(u.tmp_out + 0, B0);631sph_enc32le_aligned(u.tmp_out + 4, B1);632sph_enc32le_aligned(u.tmp_out + 8, B2);633sph_enc32le_aligned(u.tmp_out + 12, B3);634/* fall through */635case 12:636sph_enc32le_aligned(u.tmp_out + 16, B4);637sph_enc32le_aligned(u.tmp_out + 20, B5);638sph_enc32le_aligned(u.tmp_out + 24, B6);639sph_enc32le_aligned(u.tmp_out + 28, B7);640/* fall through */641case 8:642sph_enc32le_aligned(u.tmp_out + 32, B8);643/* fall through */644case 7:645sph_enc32le_aligned(u.tmp_out + 36, B9);646/* fall through */647case 6:648sph_enc32le_aligned(u.tmp_out + 40, BA);649sph_enc32le_aligned(u.tmp_out + 44, BB);650sph_enc32le_aligned(u.tmp_out + 48, BC);651sph_enc32le_aligned(u.tmp_out + 52, BD);652sph_enc32le_aligned(u.tmp_out + 56, BE);653sph_enc32le_aligned(u.tmp_out + 60, BF);654break;655default:656return;657}658out_len = size_words << 2;659memcpy(dst, u.tmp_out + (sizeof u.tmp_out) - out_len, out_len);660shabal_init(sc, size_words << 5);661}662#if 0663/* see sph_shabal.h */664void665sph_shabal192_init(void *cc)666{667shabal_init(cc, 192);668}669670/* see sph_shabal.h */671void672sph_shabal192(void *cc, const void *data, size_t len)673{674shabal_core(cc, data, len);675}676677/* see sph_shabal.h */678void679sph_shabal192_close(void *cc, void *dst)680{681shabal_close(cc, 0, 0, dst, 6);682}683684/* see sph_shabal.h */685void686sph_shabal192_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)687{688shabal_close(cc, ub, n, dst, 6);689}690691/* see sph_shabal.h */692void693sph_shabal224_init(void *cc)694{695shabal_init(cc, 224);696}697698/* see sph_shabal.h */699void700sph_shabal224(void *cc, const void *data, size_t len)701{702shabal_core(cc, data, len);703}704705/* see sph_shabal.h */706void707sph_shabal224_close(void *cc, void *dst)708{709shabal_close(cc, 0, 0, dst, 7);710}711712/* see sph_shabal.h */713void714sph_shabal224_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)715{716shabal_close(cc, ub, n, dst, 7);717}718719#endif720/* see sph_shabal.h */721void722sph_shabal256_init(void *cc)723{724shabal_init(cc, 256);725}726727/* see sph_shabal.h */728void729sph_shabal256(void *cc, const void *data, size_t len)730{731shabal_core(cc, data, len);732}733734/* see sph_shabal.h */735void736sph_shabal256_close(void *cc, void *dst)737{738shabal_close(cc, 0, 0, dst, 8);739}740741/* see sph_shabal.h */742void743sph_shabal256_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)744{745shabal_close(cc, ub, n, dst, 8);746}747748#if 0749/* see sph_shabal.h */750void751sph_shabal384_init(void *cc)752{753shabal_init(cc, 384);754}755756/* see sph_shabal.h */757void758sph_shabal384(void *cc, const void *data, size_t len)759{760shabal_core(cc, data, len);761}762763/* see sph_shabal.h */764void765sph_shabal384_close(void *cc, void *dst)766{767shabal_close(cc, 0, 0, dst, 12);768}769770/* see sph_shabal.h */771void772sph_shabal384_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)773{774shabal_close(cc, ub, n, dst, 12);775}776#endif777778/* see sph_shabal.h */779void780sph_shabal512_init(void *cc)781{782shabal_init(cc, 512);783}784785/* see sph_shabal.h */786void787sph_shabal512(void *cc, const void *data, size_t len)788{789shabal_core(cc, data, len);790}791792/* see sph_shabal.h */793void794sph_shabal512_close(void *cc, void *dst)795{796shabal_close(cc, 0, 0, dst, 16);797}798799/* see sph_shabal.h */800void801sph_shabal512_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst)802{803shabal_close(cc, ub, n, dst, 16);804}805#ifdef __cplusplus806}807#endif808809810