/* $Id: sph_keccak.h 216 2010-06-08 09:46:57Z tp $ */1/**2* Keccak interface. This is the interface for Keccak with the3* recommended parameters for SHA-3, with output lengths 224, 256,4* 384 and 512 bits.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* @file sph_keccak.h32* @author Thomas Pornin <[email protected]>33*/3435#ifndef SPH_KECCAK_H__36#define SPH_KECCAK_H__3738#ifdef __cplusplus39extern "C"{40#endif4142#include <stddef.h>43#include "sph_types.h"4445/**46* Output size (in bits) for Keccak-224.47*/48#define SPH_SIZE_keccak224 2244950/**51* Output size (in bits) for Keccak-256.52*/53#define SPH_SIZE_keccak256 2565455/**56* Output size (in bits) for Keccak-384.57*/58#define SPH_SIZE_keccak384 3845960/**61* Output size (in bits) for Keccak-512.62*/63#define SPH_SIZE_keccak512 5126465/**66* This structure is a context for Keccak computations: it contains the67* intermediate values and some data from the last entered block. Once a68* Keccak computation has been performed, the context can be reused for69* another computation.70*71* The contents of this structure are private. A running Keccak computation72* can be cloned by copying the context (e.g. with a simple73* <code>memcpy()</code>).74*/75typedef struct {76#ifndef DOXYGEN_IGNORE77unsigned char buf[144]; /* first field, for alignment */78size_t ptr, lim;79union {80#if SPH_6481sph_u64 wide[25];82#endif83sph_u32 narrow[50];84} u;85#endif86} sph_keccak_context;8788/**89* Type for a Keccak-224 context (identical to the common context).90*/91typedef sph_keccak_context sph_keccak224_context;9293/**94* Type for a Keccak-256 context (identical to the common context).95*/96typedef sph_keccak_context sph_keccak256_context;9798/**99* Type for a Keccak-384 context (identical to the common context).100*/101typedef sph_keccak_context sph_keccak384_context;102103/**104* Type for a Keccak-512 context (identical to the common context).105*/106typedef sph_keccak_context sph_keccak512_context;107108/**109* Initialize a Keccak-224 context. This process performs no memory allocation.110*111* @param cc the Keccak-224 context (pointer to a112* <code>sph_keccak224_context</code>)113*/114void sph_keccak224_init(void *cc);115116/**117* Process some data bytes. It is acceptable that <code>len</code> is zero118* (in which case this function does nothing).119*120* @param cc the Keccak-224 context121* @param data the input data122* @param len the input data length (in bytes)123*/124void sph_keccak224(void *cc, const void *data, size_t len);125126/**127* Terminate the current Keccak-224 computation and output the result into128* the provided buffer. The destination buffer must be wide enough to129* accomodate the result (28 bytes). The context is automatically130* reinitialized.131*132* @param cc the Keccak-224 context133* @param dst the destination buffer134*/135void sph_keccak224_close(void *cc, void *dst);136137/**138* Add a few additional bits (0 to 7) to the current computation, then139* terminate it and output the result in the provided buffer, which must140* be wide enough to accomodate the result (28 bytes). If bit number i141* in <code>ub</code> has value 2^i, then the extra bits are those142* numbered 7 downto 8-n (this is the big-endian convention at the byte143* level). The context is automatically reinitialized.144*145* @param cc the Keccak-224 context146* @param ub the extra bits147* @param n the number of extra bits (0 to 7)148* @param dst the destination buffer149*/150void sph_keccak224_addbits_and_close(151void *cc, unsigned ub, unsigned n, void *dst);152153/**154* Initialize a Keccak-256 context. This process performs no memory allocation.155*156* @param cc the Keccak-256 context (pointer to a157* <code>sph_keccak256_context</code>)158*/159void sph_keccak256_init(void *cc);160161/**162* Process some data bytes. It is acceptable that <code>len</code> is zero163* (in which case this function does nothing).164*165* @param cc the Keccak-256 context166* @param data the input data167* @param len the input data length (in bytes)168*/169void sph_keccak256(void *cc, const void *data, size_t len);170171/**172* Terminate the current Keccak-256 computation and output the result into173* the provided buffer. The destination buffer must be wide enough to174* accomodate the result (32 bytes). The context is automatically175* reinitialized.176*177* @param cc the Keccak-256 context178* @param dst the destination buffer179*/180void sph_keccak256_close(void *cc, void *dst);181182/**183* Add a few additional bits (0 to 7) to the current computation, then184* terminate it and output the result in the provided buffer, which must185* be wide enough to accomodate the result (32 bytes). If bit number i186* in <code>ub</code> has value 2^i, then the extra bits are those187* numbered 7 downto 8-n (this is the big-endian convention at the byte188* level). The context is automatically reinitialized.189*190* @param cc the Keccak-256 context191* @param ub the extra bits192* @param n the number of extra bits (0 to 7)193* @param dst the destination buffer194*/195void sph_keccak256_addbits_and_close(196void *cc, unsigned ub, unsigned n, void *dst);197198/**199* Initialize a Keccak-384 context. This process performs no memory allocation.200*201* @param cc the Keccak-384 context (pointer to a202* <code>sph_keccak384_context</code>)203*/204void sph_keccak384_init(void *cc);205206/**207* Process some data bytes. It is acceptable that <code>len</code> is zero208* (in which case this function does nothing).209*210* @param cc the Keccak-384 context211* @param data the input data212* @param len the input data length (in bytes)213*/214void sph_keccak384(void *cc, const void *data, size_t len);215216/**217* Terminate the current Keccak-384 computation and output the result into218* the provided buffer. The destination buffer must be wide enough to219* accomodate the result (48 bytes). The context is automatically220* reinitialized.221*222* @param cc the Keccak-384 context223* @param dst the destination buffer224*/225void sph_keccak384_close(void *cc, void *dst);226227/**228* Add a few additional bits (0 to 7) to the current computation, then229* terminate it and output the result in the provided buffer, which must230* be wide enough to accomodate the result (48 bytes). If bit number i231* in <code>ub</code> has value 2^i, then the extra bits are those232* numbered 7 downto 8-n (this is the big-endian convention at the byte233* level). The context is automatically reinitialized.234*235* @param cc the Keccak-384 context236* @param ub the extra bits237* @param n the number of extra bits (0 to 7)238* @param dst the destination buffer239*/240void sph_keccak384_addbits_and_close(241void *cc, unsigned ub, unsigned n, void *dst);242243/**244* Initialize a Keccak-512 context. This process performs no memory allocation.245*246* @param cc the Keccak-512 context (pointer to a247* <code>sph_keccak512_context</code>)248*/249void sph_keccak512_init(void *cc);250251/**252* Process some data bytes. It is acceptable that <code>len</code> is zero253* (in which case this function does nothing).254*255* @param cc the Keccak-512 context256* @param data the input data257* @param len the input data length (in bytes)258*/259void sph_keccak512(void *cc, const void *data, size_t len);260261/**262* Terminate the current Keccak-512 computation and output the result into263* the provided buffer. The destination buffer must be wide enough to264* accomodate the result (64 bytes). The context is automatically265* reinitialized.266*267* @param cc the Keccak-512 context268* @param dst the destination buffer269*/270void sph_keccak512_close(void *cc, void *dst);271272/**273* Add a few additional bits (0 to 7) to the current computation, then274* terminate it and output the result in the provided buffer, which must275* be wide enough to accomodate the result (64 bytes). If bit number i276* in <code>ub</code> has value 2^i, then the extra bits are those277* numbered 7 downto 8-n (this is the big-endian convention at the byte278* level). The context is automatically reinitialized.279*280* @param cc the Keccak-512 context281* @param ub the extra bits282* @param n the number of extra bits (0 to 7)283* @param dst the destination buffer284*/285void sph_keccak512_addbits_and_close(286void *cc, unsigned ub, unsigned n, void *dst);287288#ifdef __cplusplus289}290#endif291292#endif293294295