Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
sha224.c
00001 /** 00002 * @file sha224.c 00003 * @brief SHA-224 (Secure Hash Algorithm 224) 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-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 "sha224.h" 00041 00042 //Check crypto library configuration 00043 #if (SHA224_SUPPORT == ENABLED) 00044 00045 //SHA-224 object identifier (2.16.840.1.101.3.4.2.4) 00046 static const uint8_t sha224Oid[] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04}; 00047 00048 //Common interface for hash algorithms 00049 const HashAlgo sha224HashAlgo = 00050 { 00051 "SHA-224", 00052 sha224Oid, 00053 sizeof(sha224Oid), 00054 sizeof(Sha224Context), 00055 SHA224_BLOCK_SIZE, 00056 SHA224_DIGEST_SIZE, 00057 (HashAlgoCompute) sha224Compute, 00058 (HashAlgoInit) sha224Init, 00059 (HashAlgoUpdate) sha224Update, 00060 (HashAlgoFinal) sha224Final 00061 }; 00062 00063 00064 /** 00065 * @brief Digest a message using SHA-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 sha224Compute(const void *data, size_t length, uint8_t *digest) 00073 { 00074 //Allocate a memory buffer to hold the SHA-224 context 00075 Sha224Context *context = cryptoAllocMem(sizeof(Sha224Context)); 00076 //Failed to allocate memory? 00077 if(context == NULL) 00078 return ERROR_OUT_OF_MEMORY; 00079 00080 //Initialize the SHA-224 context 00081 sha224Init(context); 00082 //Digest the message 00083 sha224Update(context, data, length); 00084 //Finalize the SHA-224 message digest 00085 sha224Final(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-224 message digest context 00096 * @param[in] context Pointer to the SHA-224 context to initialize 00097 **/ 00098 00099 void sha224Init(Sha224Context *context) 00100 { 00101 //Set initial hash value 00102 context->h[0] = 0xC1059ED8; 00103 context->h[1] = 0x367CD507; 00104 context->h[2] = 0x3070DD17; 00105 context->h[3] = 0xF70E5939; 00106 context->h[4] = 0xFFC00B31; 00107 context->h[5] = 0x68581511; 00108 context->h[6] = 0x64F98FA7; 00109 context->h[7] = 0xBEFA4FA4; 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-224 context with a portion of the message being hashed 00120 * @param[in] context Pointer to the SHA-224 context 00121 * @param[in] data Pointer to the buffer being hashed 00122 * @param[in] length Length of the buffer 00123 **/ 00124 00125 void sha224Update(Sha224Context *context, const void *data, size_t length) 00126 { 00127 //The function is defined in the exact same manner as SHA-256 00128 sha256Update(context, data, length); 00129 } 00130 00131 00132 /** 00133 * @brief Finish the SHA-224 message digest 00134 * @param[in] context Pointer to the SHA-224 context 00135 * @param[out] digest Calculated digest (optional parameter) 00136 **/ 00137 00138 void sha224Final(Sha224Context *context, uint8_t *digest) 00139 { 00140 //The function is defined in the exact same manner as SHA-256 00141 sha256Final(context, NULL); 00142 00143 //Copy the resulting digest 00144 if(digest != NULL) 00145 memcpy(digest, context->digest, SHA224_DIGEST_SIZE); 00146 } 00147 00148 #endif 00149
Generated on Tue Jul 12 2022 17:10:16 by
1.7.2