Webserver+3d print
cyclone_crypto/sha224.c@0:8918a71cdbe9, 2017-02-04 (annotated)
- Committer:
- Sergunb
- Date:
- Sat Feb 04 18:15:49 2017 +0000
- Revision:
- 0:8918a71cdbe9
nothing else
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sergunb | 0:8918a71cdbe9 | 1 | /** |
Sergunb | 0:8918a71cdbe9 | 2 | * @file sha224.c |
Sergunb | 0:8918a71cdbe9 | 3 | * @brief SHA-224 (Secure Hash Algorithm 224) |
Sergunb | 0:8918a71cdbe9 | 4 | * |
Sergunb | 0:8918a71cdbe9 | 5 | * @section License |
Sergunb | 0:8918a71cdbe9 | 6 | * |
Sergunb | 0:8918a71cdbe9 | 7 | * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved. |
Sergunb | 0:8918a71cdbe9 | 8 | * |
Sergunb | 0:8918a71cdbe9 | 9 | * This file is part of CycloneCrypto Open. |
Sergunb | 0:8918a71cdbe9 | 10 | * |
Sergunb | 0:8918a71cdbe9 | 11 | * This program is free software; you can redistribute it and/or |
Sergunb | 0:8918a71cdbe9 | 12 | * modify it under the terms of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 13 | * as published by the Free Software Foundation; either version 2 |
Sergunb | 0:8918a71cdbe9 | 14 | * of the License, or (at your option) any later version. |
Sergunb | 0:8918a71cdbe9 | 15 | * |
Sergunb | 0:8918a71cdbe9 | 16 | * This program is distributed in the hope that it will be useful, |
Sergunb | 0:8918a71cdbe9 | 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Sergunb | 0:8918a71cdbe9 | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Sergunb | 0:8918a71cdbe9 | 19 | * GNU General Public License for more details. |
Sergunb | 0:8918a71cdbe9 | 20 | * |
Sergunb | 0:8918a71cdbe9 | 21 | * You should have received a copy of the GNU General Public License |
Sergunb | 0:8918a71cdbe9 | 22 | * along with this program; if not, write to the Free Software Foundation, |
Sergunb | 0:8918a71cdbe9 | 23 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
Sergunb | 0:8918a71cdbe9 | 24 | * |
Sergunb | 0:8918a71cdbe9 | 25 | * @section Description |
Sergunb | 0:8918a71cdbe9 | 26 | * |
Sergunb | 0:8918a71cdbe9 | 27 | * SHA-224 is a secure hash algorithm for computing a condensed representation |
Sergunb | 0:8918a71cdbe9 | 28 | * of an electronic message. Refer to FIPS 180-4 for more details |
Sergunb | 0:8918a71cdbe9 | 29 | * |
Sergunb | 0:8918a71cdbe9 | 30 | * @author Oryx Embedded SARL (www.oryx-embedded.com) |
Sergunb | 0:8918a71cdbe9 | 31 | * @version 1.7.6 |
Sergunb | 0:8918a71cdbe9 | 32 | **/ |
Sergunb | 0:8918a71cdbe9 | 33 | |
Sergunb | 0:8918a71cdbe9 | 34 | //Switch to the appropriate trace level |
Sergunb | 0:8918a71cdbe9 | 35 | #define TRACE_LEVEL CRYPTO_TRACE_LEVEL |
Sergunb | 0:8918a71cdbe9 | 36 | |
Sergunb | 0:8918a71cdbe9 | 37 | //Dependencies |
Sergunb | 0:8918a71cdbe9 | 38 | #include <string.h> |
Sergunb | 0:8918a71cdbe9 | 39 | #include "crypto.h" |
Sergunb | 0:8918a71cdbe9 | 40 | #include "sha224.h" |
Sergunb | 0:8918a71cdbe9 | 41 | |
Sergunb | 0:8918a71cdbe9 | 42 | //Check crypto library configuration |
Sergunb | 0:8918a71cdbe9 | 43 | #if (SHA224_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 44 | |
Sergunb | 0:8918a71cdbe9 | 45 | //SHA-224 object identifier (2.16.840.1.101.3.4.2.4) |
Sergunb | 0:8918a71cdbe9 | 46 | static const uint8_t sha224Oid[] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04}; |
Sergunb | 0:8918a71cdbe9 | 47 | |
Sergunb | 0:8918a71cdbe9 | 48 | //Common interface for hash algorithms |
Sergunb | 0:8918a71cdbe9 | 49 | const HashAlgo sha224HashAlgo = |
Sergunb | 0:8918a71cdbe9 | 50 | { |
Sergunb | 0:8918a71cdbe9 | 51 | "SHA-224", |
Sergunb | 0:8918a71cdbe9 | 52 | sha224Oid, |
Sergunb | 0:8918a71cdbe9 | 53 | sizeof(sha224Oid), |
Sergunb | 0:8918a71cdbe9 | 54 | sizeof(Sha224Context), |
Sergunb | 0:8918a71cdbe9 | 55 | SHA224_BLOCK_SIZE, |
Sergunb | 0:8918a71cdbe9 | 56 | SHA224_DIGEST_SIZE, |
Sergunb | 0:8918a71cdbe9 | 57 | (HashAlgoCompute) sha224Compute, |
Sergunb | 0:8918a71cdbe9 | 58 | (HashAlgoInit) sha224Init, |
Sergunb | 0:8918a71cdbe9 | 59 | (HashAlgoUpdate) sha224Update, |
Sergunb | 0:8918a71cdbe9 | 60 | (HashAlgoFinal) sha224Final |
Sergunb | 0:8918a71cdbe9 | 61 | }; |
Sergunb | 0:8918a71cdbe9 | 62 | |
Sergunb | 0:8918a71cdbe9 | 63 | |
Sergunb | 0:8918a71cdbe9 | 64 | /** |
Sergunb | 0:8918a71cdbe9 | 65 | * @brief Digest a message using SHA-224 |
Sergunb | 0:8918a71cdbe9 | 66 | * @param[in] data Pointer to the message being hashed |
Sergunb | 0:8918a71cdbe9 | 67 | * @param[in] length Length of the message |
Sergunb | 0:8918a71cdbe9 | 68 | * @param[out] digest Pointer to the calculated digest |
Sergunb | 0:8918a71cdbe9 | 69 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 70 | **/ |
Sergunb | 0:8918a71cdbe9 | 71 | |
Sergunb | 0:8918a71cdbe9 | 72 | error_t sha224Compute(const void *data, size_t length, uint8_t *digest) |
Sergunb | 0:8918a71cdbe9 | 73 | { |
Sergunb | 0:8918a71cdbe9 | 74 | //Allocate a memory buffer to hold the SHA-224 context |
Sergunb | 0:8918a71cdbe9 | 75 | Sha224Context *context = cryptoAllocMem(sizeof(Sha224Context)); |
Sergunb | 0:8918a71cdbe9 | 76 | //Failed to allocate memory? |
Sergunb | 0:8918a71cdbe9 | 77 | if(context == NULL) |
Sergunb | 0:8918a71cdbe9 | 78 | return ERROR_OUT_OF_MEMORY; |
Sergunb | 0:8918a71cdbe9 | 79 | |
Sergunb | 0:8918a71cdbe9 | 80 | //Initialize the SHA-224 context |
Sergunb | 0:8918a71cdbe9 | 81 | sha224Init(context); |
Sergunb | 0:8918a71cdbe9 | 82 | //Digest the message |
Sergunb | 0:8918a71cdbe9 | 83 | sha224Update(context, data, length); |
Sergunb | 0:8918a71cdbe9 | 84 | //Finalize the SHA-224 message digest |
Sergunb | 0:8918a71cdbe9 | 85 | sha224Final(context, digest); |
Sergunb | 0:8918a71cdbe9 | 86 | |
Sergunb | 0:8918a71cdbe9 | 87 | //Free previously allocated memory |
Sergunb | 0:8918a71cdbe9 | 88 | cryptoFreeMem(context); |
Sergunb | 0:8918a71cdbe9 | 89 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 90 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 91 | } |
Sergunb | 0:8918a71cdbe9 | 92 | |
Sergunb | 0:8918a71cdbe9 | 93 | |
Sergunb | 0:8918a71cdbe9 | 94 | /** |
Sergunb | 0:8918a71cdbe9 | 95 | * @brief Initialize SHA-224 message digest context |
Sergunb | 0:8918a71cdbe9 | 96 | * @param[in] context Pointer to the SHA-224 context to initialize |
Sergunb | 0:8918a71cdbe9 | 97 | **/ |
Sergunb | 0:8918a71cdbe9 | 98 | |
Sergunb | 0:8918a71cdbe9 | 99 | void sha224Init(Sha224Context *context) |
Sergunb | 0:8918a71cdbe9 | 100 | { |
Sergunb | 0:8918a71cdbe9 | 101 | //Set initial hash value |
Sergunb | 0:8918a71cdbe9 | 102 | context->h[0] = 0xC1059ED8; |
Sergunb | 0:8918a71cdbe9 | 103 | context->h[1] = 0x367CD507; |
Sergunb | 0:8918a71cdbe9 | 104 | context->h[2] = 0x3070DD17; |
Sergunb | 0:8918a71cdbe9 | 105 | context->h[3] = 0xF70E5939; |
Sergunb | 0:8918a71cdbe9 | 106 | context->h[4] = 0xFFC00B31; |
Sergunb | 0:8918a71cdbe9 | 107 | context->h[5] = 0x68581511; |
Sergunb | 0:8918a71cdbe9 | 108 | context->h[6] = 0x64F98FA7; |
Sergunb | 0:8918a71cdbe9 | 109 | context->h[7] = 0xBEFA4FA4; |
Sergunb | 0:8918a71cdbe9 | 110 | |
Sergunb | 0:8918a71cdbe9 | 111 | //Number of bytes in the buffer |
Sergunb | 0:8918a71cdbe9 | 112 | context->size = 0; |
Sergunb | 0:8918a71cdbe9 | 113 | //Total length of the message |
Sergunb | 0:8918a71cdbe9 | 114 | context->totalSize = 0; |
Sergunb | 0:8918a71cdbe9 | 115 | } |
Sergunb | 0:8918a71cdbe9 | 116 | |
Sergunb | 0:8918a71cdbe9 | 117 | |
Sergunb | 0:8918a71cdbe9 | 118 | /** |
Sergunb | 0:8918a71cdbe9 | 119 | * @brief Update the SHA-224 context with a portion of the message being hashed |
Sergunb | 0:8918a71cdbe9 | 120 | * @param[in] context Pointer to the SHA-224 context |
Sergunb | 0:8918a71cdbe9 | 121 | * @param[in] data Pointer to the buffer being hashed |
Sergunb | 0:8918a71cdbe9 | 122 | * @param[in] length Length of the buffer |
Sergunb | 0:8918a71cdbe9 | 123 | **/ |
Sergunb | 0:8918a71cdbe9 | 124 | |
Sergunb | 0:8918a71cdbe9 | 125 | void sha224Update(Sha224Context *context, const void *data, size_t length) |
Sergunb | 0:8918a71cdbe9 | 126 | { |
Sergunb | 0:8918a71cdbe9 | 127 | //The function is defined in the exact same manner as SHA-256 |
Sergunb | 0:8918a71cdbe9 | 128 | sha256Update(context, data, length); |
Sergunb | 0:8918a71cdbe9 | 129 | } |
Sergunb | 0:8918a71cdbe9 | 130 | |
Sergunb | 0:8918a71cdbe9 | 131 | |
Sergunb | 0:8918a71cdbe9 | 132 | /** |
Sergunb | 0:8918a71cdbe9 | 133 | * @brief Finish the SHA-224 message digest |
Sergunb | 0:8918a71cdbe9 | 134 | * @param[in] context Pointer to the SHA-224 context |
Sergunb | 0:8918a71cdbe9 | 135 | * @param[out] digest Calculated digest (optional parameter) |
Sergunb | 0:8918a71cdbe9 | 136 | **/ |
Sergunb | 0:8918a71cdbe9 | 137 | |
Sergunb | 0:8918a71cdbe9 | 138 | void sha224Final(Sha224Context *context, uint8_t *digest) |
Sergunb | 0:8918a71cdbe9 | 139 | { |
Sergunb | 0:8918a71cdbe9 | 140 | //The function is defined in the exact same manner as SHA-256 |
Sergunb | 0:8918a71cdbe9 | 141 | sha256Final(context, NULL); |
Sergunb | 0:8918a71cdbe9 | 142 | |
Sergunb | 0:8918a71cdbe9 | 143 | //Copy the resulting digest |
Sergunb | 0:8918a71cdbe9 | 144 | if(digest != NULL) |
Sergunb | 0:8918a71cdbe9 | 145 | memcpy(digest, context->digest, SHA224_DIGEST_SIZE); |
Sergunb | 0:8918a71cdbe9 | 146 | } |
Sergunb | 0:8918a71cdbe9 | 147 | |
Sergunb | 0:8918a71cdbe9 | 148 | #endif |
Sergunb | 0:8918a71cdbe9 | 149 |