/* $Id: sph_sha2.h 216 2010-06-08 09:46:57Z tp $ */1/**2* SHA-224, SHA-256, SHA-384 and SHA-512 interface.3*4* SHA-256 has been published in FIPS 180-2, now amended with a change5* notice to include SHA-224 as well (which is a simple variation on6* SHA-256). SHA-384 and SHA-512 are also defined in FIPS 180-2. FIPS7* standards can be found at:8* http://csrc.nist.gov/publications/fips/9*10* ==========================(LICENSE BEGIN)============================11*12* Copyright (c) 2007-2010 Projet RNRT SAPHIR13*14* Permission is hereby granted, free of charge, to any person obtaining15* a copy of this software and associated documentation files (the16* "Software"), to deal in the Software without restriction, including17* without limitation the rights to use, copy, modify, merge, publish,18* distribute, sublicense, and/or sell copies of the Software, and to19* permit persons to whom the Software is furnished to do so, subject to20* the following conditions:21*22* The above copyright notice and this permission notice shall be23* included in all copies or substantial portions of the Software.24*25* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,26* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF27* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.28* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY29* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,30* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE31* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.32*33* ===========================(LICENSE END)=============================34*35* @file sph_sha2.h36* @author Thomas Pornin <[email protected]>37*/3839#ifndef SPH_SHA2_H__40#define SPH_SHA2_H__4142#include <stddef.h>43#include "sph_types.h"4445/**46* Output size (in bits) for SHA-224.47*/48#define SPH_SIZE_sha224 2244950/**51* Output size (in bits) for SHA-256.52*/53#define SPH_SIZE_sha256 2565455/**56* This structure is a context for SHA-224 computations: it contains the57* intermediate values and some data from the last entered block. Once58* a SHA-224 computation has been performed, the context can be reused for59* another computation.60*61* The contents of this structure are private. A running SHA-224 computation62* can be cloned by copying the context (e.g. with a simple63* <code>memcpy()</code>).64*/65typedef struct {66#ifndef DOXYGEN_IGNORE67unsigned char buf[64]; /* first field, for alignment */68sph_u32 val[8];69#if SPH_6470sph_u64 count;71#else72sph_u32 count_high, count_low;73#endif74#endif75} sph_sha224_context;7677/**78* This structure is a context for SHA-256 computations. It is identical79* to the SHA-224 context. However, a context is initialized for SHA-22480* <strong>or</strong> SHA-256, but not both (the internal IV is not the81* same).82*/83typedef sph_sha224_context sph_sha256_context;8485/**86* Initialize a SHA-224 context. This process performs no memory allocation.87*88* @param cc the SHA-224 context (pointer to89* a <code>sph_sha224_context</code>)90*/91void sph_sha224_init(void *cc);9293/**94* Process some data bytes. It is acceptable that <code>len</code> is zero95* (in which case this function does nothing).96*97* @param cc the SHA-224 context98* @param data the input data99* @param len the input data length (in bytes)100*/101void sph_sha224(void *cc, const void *data, size_t len);102103/**104* Terminate the current SHA-224 computation and output the result into the105* provided buffer. The destination buffer must be wide enough to106* accomodate the result (28 bytes). The context is automatically107* reinitialized.108*109* @param cc the SHA-224 context110* @param dst the destination buffer111*/112void sph_sha224_close(void *cc, void *dst);113114/**115* Add a few additional bits (0 to 7) to the current computation, then116* terminate it and output the result in the provided buffer, which must117* be wide enough to accomodate the result (28 bytes). If bit number i118* in <code>ub</code> has value 2^i, then the extra bits are those119* numbered 7 downto 8-n (this is the big-endian convention at the byte120* level). The context is automatically reinitialized.121*122* @param cc the SHA-224 context123* @param ub the extra bits124* @param n the number of extra bits (0 to 7)125* @param dst the destination buffer126*/127void sph_sha224_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst);128129/**130* Apply the SHA-224 compression function on the provided data. The131* <code>msg</code> parameter contains the 16 32-bit input blocks,132* as numerical values (hence after the big-endian decoding). The133* <code>val</code> parameter contains the 8 32-bit input blocks for134* the compression function; the output is written in place in this135* array.136*137* @param msg the message block (16 values)138* @param val the function 256-bit input and output139*/140void sph_sha224_comp(const sph_u32 msg[16], sph_u32 val[8]);141142/**143* Initialize a SHA-256 context. This process performs no memory allocation.144*145* @param cc the SHA-256 context (pointer to146* a <code>sph_sha256_context</code>)147*/148void sph_sha256_init(void *cc);149150#ifdef DOXYGEN_IGNORE151/**152* Process some data bytes, for SHA-256. This function is identical to153* <code>sha_224()</code>154*155* @param cc the SHA-224 context156* @param data the input data157* @param len the input data length (in bytes)158*/159void sph_sha256(void *cc, const void *data, size_t len);160#endif161162#ifndef DOXYGEN_IGNORE163#define sph_sha256 sph_sha224164#endif165166/**167* Terminate the current SHA-256 computation and output the result into the168* provided buffer. The destination buffer must be wide enough to169* accomodate the result (32 bytes). The context is automatically170* reinitialized.171*172* @param cc the SHA-256 context173* @param dst the destination buffer174*/175void sph_sha256_close(void *cc, void *dst);176177/**178* Add a few additional bits (0 to 7) to the current computation, then179* terminate it and output the result in the provided buffer, which must180* be wide enough to accomodate the result (32 bytes). If bit number i181* in <code>ub</code> has value 2^i, then the extra bits are those182* numbered 7 downto 8-n (this is the big-endian convention at the byte183* level). The context is automatically reinitialized.184*185* @param cc the SHA-256 context186* @param ub the extra bits187* @param n the number of extra bits (0 to 7)188* @param dst the destination buffer189*/190void sph_sha256_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst);191192#ifdef DOXYGEN_IGNORE193/**194* Apply the SHA-256 compression function on the provided data. This195* function is identical to <code>sha224_comp()</code>.196*197* @param msg the message block (16 values)198* @param val the function 256-bit input and output199*/200void sph_sha256_comp(const sph_u32 msg[16], sph_u32 val[8]);201#endif202203#ifndef DOXYGEN_IGNORE204#define sph_sha256_comp sph_sha224_comp205#endif206207#if SPH_64208209/**210* Output size (in bits) for SHA-384.211*/212#define SPH_SIZE_sha384 384213214/**215* Output size (in bits) for SHA-512.216*/217#define SPH_SIZE_sha512 512218219/**220* This structure is a context for SHA-384 computations: it contains the221* intermediate values and some data from the last entered block. Once222* a SHA-384 computation has been performed, the context can be reused for223* another computation.224*225* The contents of this structure are private. A running SHA-384 computation226* can be cloned by copying the context (e.g. with a simple227* <code>memcpy()</code>).228*/229typedef struct {230#ifndef DOXYGEN_IGNORE231unsigned char buf[128]; /* first field, for alignment */232sph_u64 val[8];233sph_u64 count;234#endif235} sph_sha384_context;236237/**238* Initialize a SHA-384 context. This process performs no memory allocation.239*240* @param cc the SHA-384 context (pointer to241* a <code>sph_sha384_context</code>)242*/243void sph_sha384_init(void *cc);244245/**246* Process some data bytes. It is acceptable that <code>len</code> is zero247* (in which case this function does nothing).248*249* @param cc the SHA-384 context250* @param data the input data251* @param len the input data length (in bytes)252*/253void sph_sha384(void *cc, const void *data, size_t len);254255/**256* Terminate the current SHA-384 computation and output the result into the257* provided buffer. The destination buffer must be wide enough to258* accomodate the result (48 bytes). The context is automatically259* reinitialized.260*261* @param cc the SHA-384 context262* @param dst the destination buffer263*/264void sph_sha384_close(void *cc, void *dst);265266/**267* Add a few additional bits (0 to 7) to the current computation, then268* terminate it and output the result in the provided buffer, which must269* be wide enough to accomodate the result (48 bytes). If bit number i270* in <code>ub</code> has value 2^i, then the extra bits are those271* numbered 7 downto 8-n (this is the big-endian convention at the byte272* level). The context is automatically reinitialized.273*274* @param cc the SHA-384 context275* @param ub the extra bits276* @param n the number of extra bits (0 to 7)277* @param dst the destination buffer278*/279void sph_sha384_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst);280281/**282* Apply the SHA-384 compression function on the provided data. The283* <code>msg</code> parameter contains the 16 64-bit input blocks,284* as numerical values (hence after the big-endian decoding). The285* <code>val</code> parameter contains the 8 64-bit input blocks for286* the compression function; the output is written in place in this287* array.288*289* @param msg the message block (16 values)290* @param val the function 512-bit input and output291*/292void sph_sha384_comp(const sph_u64 msg[16], sph_u64 val[8]);293294/**295* This structure is a context for SHA-512 computations. It is identical296* to the SHA-384 context. However, a context is initialized for SHA-384297* <strong>or</strong> SHA-512, but not both (the internal IV is not the298* same).299*/300typedef sph_sha384_context sph_sha512_context;301302/**303* Initialize a SHA-512 context. This process performs no memory allocation.304*305* @param cc the SHA-512 context (pointer to306* a <code>sph_sha512_context</code>)307*/308void sph_sha512_init(void *cc);309310#ifdef DOXYGEN_IGNORE311/**312* Process some data bytes, for SHA-512. This function is identical to313* <code>sph_sha384()</code>.314*315* @param cc the SHA-384 context316* @param data the input data317* @param len the input data length (in bytes)318*/319void sph_sha512(void *cc, const void *data, size_t len);320#endif321322#ifndef DOXYGEN_IGNORE323#define sph_sha512 sph_sha384324#endif325326/**327* Terminate the current SHA-512 computation and output the result into the328* provided buffer. The destination buffer must be wide enough to329* accomodate the result (64 bytes). The context is automatically330* reinitialized.331*332* @param cc the SHA-512 context333* @param dst the destination buffer334*/335void sph_sha512_close(void *cc, void *dst);336337/**338* Add a few additional bits (0 to 7) to the current computation, then339* terminate it and output the result in the provided buffer, which must340* be wide enough to accomodate the result (64 bytes). If bit number i341* in <code>ub</code> has value 2^i, then the extra bits are those342* numbered 7 downto 8-n (this is the big-endian convention at the byte343* level). The context is automatically reinitialized.344*345* @param cc the SHA-512 context346* @param ub the extra bits347* @param n the number of extra bits (0 to 7)348* @param dst the destination buffer349*/350void sph_sha512_addbits_and_close(void *cc, unsigned ub, unsigned n, void *dst);351352#ifdef DOXYGEN_IGNORE353/**354* Apply the SHA-512 compression function. This function is identical to355* <code>sph_sha384_comp()</code>.356*357* @param msg the message block (16 values)358* @param val the function 512-bit input and output359*/360void sph_sha512_comp(const sph_u64 msg[16], sph_u64 val[8]);361#endif362363#ifndef DOXYGEN_IGNORE364#define sph_sha512_comp sph_sha384_comp365#endif366367#endif368369#endif370371372