/* $Id: sph_blake.h 252 2011-06-07 17:55:14Z tp $ */1/**2* BLAKE interface. BLAKE is a family of functions which differ by their3* output size; this implementation defines BLAKE for output sizes 224,4* 256, 384 and 512 bits. This implementation conforms to the "third5* round" specification.6*7* ==========================(LICENSE BEGIN)============================8*9* Copyright (c) 2007-2010 Projet RNRT SAPHIR10*11* Permission is hereby granted, free of charge, to any person obtaining12* a copy of this software and associated documentation files (the13* "Software"), to deal in the Software without restriction, including14* without limitation the rights to use, copy, modify, merge, publish,15* distribute, sublicense, and/or sell copies of the Software, and to16* permit persons to whom the Software is furnished to do so, subject to17* the following conditions:18*19* The above copyright notice and this permission notice shall be20* included in all copies or substantial portions of the Software.21*22* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,23* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF24* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.25* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY26* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,27* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE28* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.29*30* ===========================(LICENSE END)=============================31*32* @file sph_blake.h33* @author Thomas Pornin <[email protected]>34*/3536#ifndef SPH_BLAKE_H__37#define SPH_BLAKE_H__3839#ifdef __cplusplus40extern "C"{41#endif4243#include <stddef.h>44#include "sph_types.h"4546/**47* Output size (in bits) for BLAKE-224.48*/49#define SPH_SIZE_blake224 2245051/**52* Output size (in bits) for BLAKE-256.53*/54#define SPH_SIZE_blake256 2565556#if SPH_645758/**59* Output size (in bits) for BLAKE-384.60*/61#define SPH_SIZE_blake384 3846263/**64* Output size (in bits) for BLAKE-512.65*/66#define SPH_SIZE_blake512 5126768#endif6970/**71* This structure is a context for BLAKE-224 and BLAKE-256 computations:72* it contains the intermediate values and some data from the last73* entered block. Once a BLAKE computation has been performed, the74* context can be reused for another computation.75*76* The contents of this structure are private. A running BLAKE77* computation can be cloned by copying the context (e.g. with a simple78* <code>memcpy()</code>).79*/80typedef struct {81#ifndef DOXYGEN_IGNORE82unsigned char buf[64]; /* first field, for alignment */83size_t ptr;84sph_u32 H[8];85sph_u32 S[4];86sph_u32 T0, T1;87#endif88} sph_blake_small_context;8990/**91* This structure is a context for BLAKE-224 computations. It is92* identical to the common <code>sph_blake_small_context</code>.93*/94typedef sph_blake_small_context sph_blake224_context;9596/**97* This structure is a context for BLAKE-256 computations. It is98* identical to the common <code>sph_blake_small_context</code>.99*/100typedef sph_blake_small_context sph_blake256_context;101102#if SPH_64103104/**105* This structure is a context for BLAKE-384 and BLAKE-512 computations:106* it contains the intermediate values and some data from the last107* entered block. Once a BLAKE computation has been performed, the108* context can be reused for another computation.109*110* The contents of this structure are private. A running BLAKE111* computation can be cloned by copying the context (e.g. with a simple112* <code>memcpy()</code>).113*/114typedef struct {115#ifndef DOXYGEN_IGNORE116unsigned char buf[128]; /* first field, for alignment */117size_t ptr;118sph_u64 H[8];119sph_u64 S[4];120sph_u64 T0, T1;121#endif122} sph_blake_big_context;123124/**125* This structure is a context for BLAKE-384 computations. It is126* identical to the common <code>sph_blake_small_context</code>.127*/128typedef sph_blake_big_context sph_blake384_context;129130/**131* This structure is a context for BLAKE-512 computations. It is132* identical to the common <code>sph_blake_small_context</code>.133*/134typedef sph_blake_big_context sph_blake512_context;135136#endif137138/**139* Initialize a BLAKE-224 context. This process performs no memory allocation.140*141* @param cc the BLAKE-224 context (pointer to a142* <code>sph_blake224_context</code>)143*/144void sph_blake224_init(void *cc);145146/**147* Process some data bytes. It is acceptable that <code>len</code> is zero148* (in which case this function does nothing).149*150* @param cc the BLAKE-224 context151* @param data the input data152* @param len the input data length (in bytes)153*/154void sph_blake224(void *cc, const void *data, size_t len);155156/**157* Terminate the current BLAKE-224 computation and output the result into158* the provided buffer. The destination buffer must be wide enough to159* accomodate the result (28 bytes). The context is automatically160* reinitialized.161*162* @param cc the BLAKE-224 context163* @param dst the destination buffer164*/165void sph_blake224_close(void *cc, void *dst);166167/**168* Add a few additional bits (0 to 7) to the current computation, then169* terminate it and output the result in the provided buffer, which must170* be wide enough to accomodate the result (28 bytes). If bit number i171* in <code>ub</code> has value 2^i, then the extra bits are those172* numbered 7 downto 8-n (this is the big-endian convention at the byte173* level). The context is automatically reinitialized.174*175* @param cc the BLAKE-224 context176* @param ub the extra bits177* @param n the number of extra bits (0 to 7)178* @param dst the destination buffer179*/180void sph_blake224_addbits_and_close(181void *cc, unsigned ub, unsigned n, void *dst);182183/**184* Initialize a BLAKE-256 context. This process performs no memory allocation.185*186* @param cc the BLAKE-256 context (pointer to a187* <code>sph_blake256_context</code>)188*/189void sph_blake256_init(void *cc);190191/**192* Process some data bytes. It is acceptable that <code>len</code> is zero193* (in which case this function does nothing).194*195* @param cc the BLAKE-256 context196* @param data the input data197* @param len the input data length (in bytes)198*/199void sph_blake256(void *cc, const void *data, size_t len);200201/**202* Terminate the current BLAKE-256 computation and output the result into203* the provided buffer. The destination buffer must be wide enough to204* accomodate the result (32 bytes). The context is automatically205* reinitialized.206*207* @param cc the BLAKE-256 context208* @param dst the destination buffer209*/210void sph_blake256_close(void *cc, void *dst);211212/**213* Add a few additional bits (0 to 7) to the current computation, then214* terminate it and output the result in the provided buffer, which must215* be wide enough to accomodate the result (32 bytes). If bit number i216* in <code>ub</code> has value 2^i, then the extra bits are those217* numbered 7 downto 8-n (this is the big-endian convention at the byte218* level). The context is automatically reinitialized.219*220* @param cc the BLAKE-256 context221* @param ub the extra bits222* @param n the number of extra bits (0 to 7)223* @param dst the destination buffer224*/225void sph_blake256_addbits_and_close(226void *cc, unsigned ub, unsigned n, void *dst);227228#if SPH_64229230/**231* Initialize a BLAKE-384 context. This process performs no memory allocation.232*233* @param cc the BLAKE-384 context (pointer to a234* <code>sph_blake384_context</code>)235*/236void sph_blake384_init(void *cc);237238/**239* Process some data bytes. It is acceptable that <code>len</code> is zero240* (in which case this function does nothing).241*242* @param cc the BLAKE-384 context243* @param data the input data244* @param len the input data length (in bytes)245*/246void sph_blake384(void *cc, const void *data, size_t len);247248/**249* Terminate the current BLAKE-384 computation and output the result into250* the provided buffer. The destination buffer must be wide enough to251* accomodate the result (48 bytes). The context is automatically252* reinitialized.253*254* @param cc the BLAKE-384 context255* @param dst the destination buffer256*/257void sph_blake384_close(void *cc, void *dst);258259/**260* Add a few additional bits (0 to 7) to the current computation, then261* terminate it and output the result in the provided buffer, which must262* be wide enough to accomodate the result (48 bytes). If bit number i263* in <code>ub</code> has value 2^i, then the extra bits are those264* numbered 7 downto 8-n (this is the big-endian convention at the byte265* level). The context is automatically reinitialized.266*267* @param cc the BLAKE-384 context268* @param ub the extra bits269* @param n the number of extra bits (0 to 7)270* @param dst the destination buffer271*/272void sph_blake384_addbits_and_close(273void *cc, unsigned ub, unsigned n, void *dst);274275/**276* Initialize a BLAKE-512 context. This process performs no memory allocation.277*278* @param cc the BLAKE-512 context (pointer to a279* <code>sph_blake512_context</code>)280*/281void sph_blake512_init(void *cc);282283/**284* Process some data bytes. It is acceptable that <code>len</code> is zero285* (in which case this function does nothing).286*287* @param cc the BLAKE-512 context288* @param data the input data289* @param len the input data length (in bytes)290*/291void sph_blake512(void *cc, const void *data, size_t len);292293/**294* Terminate the current BLAKE-512 computation and output the result into295* the provided buffer. The destination buffer must be wide enough to296* accomodate the result (64 bytes). The context is automatically297* reinitialized.298*299* @param cc the BLAKE-512 context300* @param dst the destination buffer301*/302void sph_blake512_close(void *cc, void *dst);303304/**305* Add a few additional bits (0 to 7) to the current computation, then306* terminate it and output the result in the provided buffer, which must307* be wide enough to accomodate the result (64 bytes). If bit number i308* in <code>ub</code> has value 2^i, then the extra bits are those309* numbered 7 downto 8-n (this is the big-endian convention at the byte310* level). The context is automatically reinitialized.311*312* @param cc the BLAKE-512 context313* @param ub the extra bits314* @param n the number of extra bits (0 to 7)315* @param dst the destination buffer316*/317void sph_blake512_addbits_and_close(318void *cc, unsigned ub, unsigned n, void *dst);319320#endif321322#ifdef __cplusplus323}324#endif325326#endif327328329