Embed:
(wiki syntax)
Show/hide line numbers
sha512_224.c
Go to the documentation of this file.
00001 /** 00002 * @file sha512_224.c 00003 * @brief SHA-512/224 (Secure Hash Algorithm) 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-512/224 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 "sha512_224.h" 00041 00042 //Check crypto library configuration 00043 #if (SHA512_224_SUPPORT == ENABLED) 00044 00045 //SHA-512/224 object identifier (2.16.840.1.101.3.4.2.5) 00046 static const uint8_t sha512_224Oid[] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x05}; 00047 00048 //Common interface for hash algorithms 00049 const HashAlgo sha512_224HashAlgo = 00050 { 00051 "SHA-512/224", 00052 sha512_224Oid, 00053 sizeof(sha512_224Oid), 00054 sizeof(Sha512_224Context), 00055 SHA512_224_BLOCK_SIZE, 00056 SHA512_224_DIGEST_SIZE, 00057 (HashAlgoCompute) sha512_224Compute, 00058 (HashAlgoInit) sha512_224Init, 00059 (HashAlgoUpdate) sha512_224Update, 00060 (HashAlgoFinal) sha512_224Final 00061 }; 00062 00063 00064 /** 00065 * @brief Digest a message using SHA-512/224 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 sha512_224Compute(const void *data, size_t length, uint8_t *digest) 00073 { 00074 //Allocate a memory buffer to hold the SHA-512/224 context 00075 Sha512_224Context *context = cryptoAllocMem(sizeof(Sha512_224Context)); 00076 //Failed to allocate memory? 00077 if(context == NULL) 00078 return ERROR_OUT_OF_MEMORY; 00079 00080 //Initialize the SHA-512/224 context 00081 sha512_224Init(context); 00082 //Digest the message 00083 sha512_224Update(context, data, length); 00084 //Finalize the SHA-512/224 message digest 00085 sha512_224Final(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-512/224 message digest context 00096 * @param[in] context Pointer to the SHA-512/224 context to initialize 00097 **/ 00098 00099 void sha512_224Init(Sha512_224Context *context) 00100 { 00101 //Set initial hash value 00102 context->h[0] = 0x8C3D37C819544DA2; 00103 context->h[1] = 0x73E1996689DCD4D6; 00104 context->h[2] = 0x1DFAB7AE32FF9C82; 00105 context->h[3] = 0x679DD514582F9FCF; 00106 context->h[4] = 0x0F6D2B697BD44DA8; 00107 context->h[5] = 0x77E36F7304C48942; 00108 context->h[6] = 0x3F9D85A86A1D36C8; 00109 context->h[7] = 0x1112E6AD91D692A1; 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-512/224 context with a portion of the message being hashed 00120 * @param[in] context Pointer to the SHA-512/224 context 00121 * @param[in] data Pointer to the buffer being hashed 00122 * @param[in] length Length of the buffer 00123 **/ 00124 00125 void sha512_224Update(Sha512_224Context *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-512/224 message digest 00134 * @param[in] context Pointer to the SHA-512/224 context 00135 * @param[out] digest Calculated digest (optional parameter) 00136 **/ 00137 00138 void sha512_224Final(Sha512_224Context *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, SHA512_224_DIGEST_SIZE); 00146 } 00147 00148 #endif 00149
Generated on Tue Jul 12 2022 17:10:16 by
