Webserver+3d print

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sha3_256.c Source File

sha3_256.c

Go to the documentation of this file.
00001 /**
00002  * @file sha3_256.c
00003  * @brief SHA3-256 hash function (SHA-3 with 256-bit output)
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneCrypto Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @section Description
00026  *
00027  * SHA-3 is a secure hash algorithm for computing a condensed representation
00028  * of an electronic message. Refer to FIPS 202 for more details
00029  *
00030  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00031  * @version 1.7.6
00032  **/
00033 
00034 //Switch to the appropriate trace level
00035 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
00036 
00037 //Dependencies
00038 #include <string.h>
00039 #include "crypto.h"
00040 #include "sha3_256.h"
00041 
00042 //Check crypto library configuration
00043 #if (SHA3_256_SUPPORT == ENABLED)
00044 
00045 //SHA3-256 object identifier (2.16.840.1.101.3.4.2.8)
00046 static const uint8_t sha3_256Oid[] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x08};
00047 
00048 //Common interface for hash algorithms
00049 const HashAlgo sha3_256HashAlgo =
00050 {
00051    "SHA3-256",
00052    sha3_256Oid,
00053    sizeof(sha3_256Oid),
00054    sizeof(Sha3_256Context),
00055    SHA3_256_BLOCK_SIZE,
00056    SHA3_256_DIGEST_SIZE,
00057    (HashAlgoCompute) sha3_256Compute,
00058    (HashAlgoInit) sha3_256Init,
00059    (HashAlgoUpdate) sha3_256Update,
00060    (HashAlgoFinal) sha3_256Final
00061 };
00062 
00063 
00064 /**
00065  * @brief Digest a message using SHA3-256
00066  * @param[in] data Pointer to the message being hashed
00067  * @param[in] length Length of the message
00068  * @param[out] digest Pointer to the calculated digest
00069  * @return Error code
00070  **/
00071 
00072 error_t sha3_256Compute(const void *data, size_t length, uint8_t *digest)
00073 {
00074    Sha3_256Context *context;
00075 
00076    //Allocate a memory buffer to hold the SHA3-256 context
00077    context = cryptoAllocMem(sizeof(Sha3_256Context));
00078    //Failed to allocate memory?
00079    if(context == NULL)
00080       return ERROR_OUT_OF_MEMORY;
00081 
00082    //Initialize the SHA3-256 context
00083    sha3_256Init(context);
00084    //Digest the message
00085    sha3_256Update(context, data, length);
00086    //Finalize the SHA3-256 message digest
00087    sha3_256Final(context, digest);
00088 
00089    //Free previously allocated memory
00090    cryptoFreeMem(context);
00091    //Successful processing
00092    return NO_ERROR;
00093 }
00094 
00095 
00096 /**
00097  * @brief Initialize SHA3-256 message digest context
00098  * @param[in] context Pointer to the SHA3-256 context to initialize
00099  **/
00100 
00101 void sha3_256Init(Sha3_256Context *context)
00102 {
00103    //The capacity of the sponge is twice the digest length
00104    keccakInit(context, 2 * 256);
00105 }
00106 
00107 
00108 /**
00109  * @brief Update the SHA3-256 context with a portion of the message being hashed
00110  * @param[in] context Pointer to the SHA3-256 context
00111  * @param[in] data Pointer to the buffer being hashed
00112  * @param[in] length Length of the buffer
00113  **/
00114 
00115 void sha3_256Update(Sha3_256Context *context, const void *data, size_t length)
00116 {
00117    //Absorb the input data
00118    keccakAbsorb(context, data, length);
00119 }
00120 
00121 
00122 /**
00123  * @brief Finish the SHA3-256 message digest
00124  * @param[in] context Pointer to the SHA3-256 context
00125  * @param[out] digest Calculated digest (optional parameter)
00126  **/
00127 
00128 void sha3_256Final(Sha3_256Context *context, uint8_t *digest)
00129 {
00130    //Finish absorbing phase (SHA-3 padding differs from Keccak)
00131    keccakFinal(context, 0x06);
00132    //Extract data from the squeezing phase
00133    keccakSqueeze(context, digest, SHA3_256_DIGEST_SIZE);
00134 }
00135 
00136 #endif
00137