Webserver+3d print

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sha384.c Source File

sha384.c

Go to the documentation of this file.
00001 /**
00002  * @file sha384.c
00003  * @brief SHA-384 (Secure Hash Algorithm 384)
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-384 is a secure hash algorithm for computing a condensed representation
00028  * of an electronic message. Refer to FIPS 180-4 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 "sha384.h"
00041 
00042 //Check crypto library configuration
00043 #if (SHA384_SUPPORT == ENABLED)
00044 
00045 //SHA-384 object identifier (2.16.840.1.101.3.4.2.2)
00046 static const uint8_t sha384Oid[] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02};
00047 
00048 //Common interface for hash algorithms
00049 const HashAlgo sha384HashAlgo =
00050 {
00051    "SHA-384",
00052    sha384Oid,
00053    sizeof(sha384Oid),
00054    sizeof(Sha384Context),
00055    SHA384_BLOCK_SIZE,
00056    SHA384_DIGEST_SIZE,
00057    (HashAlgoCompute) sha384Compute,
00058    (HashAlgoInit) sha384Init,
00059    (HashAlgoUpdate) sha384Update,
00060    (HashAlgoFinal) sha384Final
00061 };
00062 
00063 
00064 /**
00065  * @brief Digest a message using SHA-384
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 sha384Compute(const void *data, size_t length, uint8_t *digest)
00073 {
00074    //Allocate a memory buffer to hold the SHA-384 context
00075    Sha384Context *context = cryptoAllocMem(sizeof(Sha384Context));
00076    //Failed to allocate memory?
00077    if(context == NULL)
00078       return ERROR_OUT_OF_MEMORY;
00079 
00080    //Initialize the SHA-384 context
00081    sha384Init(context);
00082    //Digest the message
00083    sha384Update(context, data, length);
00084    //Finalize the SHA-384 message digest
00085    sha384Final(context, digest);
00086 
00087    //Free previously allocated memory
00088    cryptoFreeMem(context);
00089    //Successful processing
00090    return NO_ERROR;
00091 }
00092 
00093 
00094 /**
00095  * @brief Initialize SHA-384 message digest context
00096  * @param[in] context Pointer to the SHA-384 context to initialize
00097  **/
00098 
00099 void sha384Init(Sha384Context *context)
00100 {
00101    //Set initial hash value
00102    context->h[0] = 0xCBBB9D5DC1059ED8;
00103    context->h[1] = 0x629A292A367CD507;
00104    context->h[2] = 0x9159015A3070DD17;
00105    context->h[3] = 0x152FECD8F70E5939;
00106    context->h[4] = 0x67332667FFC00B31;
00107    context->h[5] = 0x8EB44A8768581511;
00108    context->h[6] = 0xDB0C2E0D64F98FA7;
00109    context->h[7] = 0x47B5481DBEFA4FA4;
00110 
00111    //Number of bytes in the buffer
00112    context->size = 0;
00113    //Total length of the message
00114    context->totalSize = 0;
00115 }
00116 
00117 
00118 /**
00119  * @brief Update the SHA-384 context with a portion of the message being hashed
00120  * @param[in] context Pointer to the SHA-384 context
00121  * @param[in] data Pointer to the buffer being hashed
00122  * @param[in] length Length of the buffer
00123  **/
00124 
00125 void sha384Update(Sha384Context *context, const void *data, size_t length)
00126 {
00127    //The function is defined in the exact same manner as SHA-512
00128    sha512Update(context, data, length);
00129 }
00130 
00131 
00132 /**
00133  * @brief Finish the SHA-384 message digest
00134  * @param[in] context Pointer to the SHA-384 context
00135  * @param[out] digest Calculated digest (optional parameter)
00136  **/
00137 
00138 void sha384Final(Sha384Context *context, uint8_t *digest)
00139 {
00140    //The function is defined in the exact same manner as SHA-512
00141    sha512Final(context, NULL);
00142 
00143    //Copy the resulting digest
00144    if(digest != NULL)
00145       memcpy(digest, context->digest, SHA384_DIGEST_SIZE);
00146 }
00147 
00148 #endif
00149