Webserver+3d print
cyclone_crypto/sha512.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 sha512.c |
Sergunb | 0:8918a71cdbe9 | 3 | * @brief SHA-512 (Secure Hash Algorithm 512) |
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-512 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 "sha512.h" |
Sergunb | 0:8918a71cdbe9 | 41 | |
Sergunb | 0:8918a71cdbe9 | 42 | //Check crypto library configuration |
Sergunb | 0:8918a71cdbe9 | 43 | #if (SHA384_SUPPORT == ENABLED || SHA512_SUPPORT == ENABLED || \ |
Sergunb | 0:8918a71cdbe9 | 44 | SHA512_224_SUPPORT == ENABLED || SHA512_256_SUPPORT == ENABLED) |
Sergunb | 0:8918a71cdbe9 | 45 | |
Sergunb | 0:8918a71cdbe9 | 46 | //Macro to access the workspace as a circular buffer |
Sergunb | 0:8918a71cdbe9 | 47 | #define W(t) w[(t) & 0x0F] |
Sergunb | 0:8918a71cdbe9 | 48 | |
Sergunb | 0:8918a71cdbe9 | 49 | //SHA-512 auxiliary functions |
Sergunb | 0:8918a71cdbe9 | 50 | #define CH(x, y, z) (((x) & (y)) | (~(x) & (z))) |
Sergunb | 0:8918a71cdbe9 | 51 | #define MAJ(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) |
Sergunb | 0:8918a71cdbe9 | 52 | #define SIGMA1(x) (ROR64(x, 28) ^ ROR64(x, 34) ^ ROR64(x, 39)) |
Sergunb | 0:8918a71cdbe9 | 53 | #define SIGMA2(x) (ROR64(x, 14) ^ ROR64(x, 18) ^ ROR64(x, 41)) |
Sergunb | 0:8918a71cdbe9 | 54 | #define SIGMA3(x) (ROR64(x, 1) ^ ROR64(x, 8) ^ SHR64(x, 7)) |
Sergunb | 0:8918a71cdbe9 | 55 | #define SIGMA4(x) (ROR64(x, 19) ^ ROR64(x, 61) ^ SHR64(x, 6)) |
Sergunb | 0:8918a71cdbe9 | 56 | |
Sergunb | 0:8918a71cdbe9 | 57 | //SHA-512 padding |
Sergunb | 0:8918a71cdbe9 | 58 | static const uint8_t padding[128] = |
Sergunb | 0:8918a71cdbe9 | 59 | { |
Sergunb | 0:8918a71cdbe9 | 60 | 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Sergunb | 0:8918a71cdbe9 | 61 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Sergunb | 0:8918a71cdbe9 | 62 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Sergunb | 0:8918a71cdbe9 | 63 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Sergunb | 0:8918a71cdbe9 | 64 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Sergunb | 0:8918a71cdbe9 | 65 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Sergunb | 0:8918a71cdbe9 | 66 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Sergunb | 0:8918a71cdbe9 | 67 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
Sergunb | 0:8918a71cdbe9 | 68 | }; |
Sergunb | 0:8918a71cdbe9 | 69 | |
Sergunb | 0:8918a71cdbe9 | 70 | //SHA-512 constants |
Sergunb | 0:8918a71cdbe9 | 71 | static const uint64_t k[80] = |
Sergunb | 0:8918a71cdbe9 | 72 | { |
Sergunb | 0:8918a71cdbe9 | 73 | 0x428A2F98D728AE22, 0x7137449123EF65CD, 0xB5C0FBCFEC4D3B2F, 0xE9B5DBA58189DBBC, |
Sergunb | 0:8918a71cdbe9 | 74 | 0x3956C25BF348B538, 0x59F111F1B605D019, 0x923F82A4AF194F9B, 0xAB1C5ED5DA6D8118, |
Sergunb | 0:8918a71cdbe9 | 75 | 0xD807AA98A3030242, 0x12835B0145706FBE, 0x243185BE4EE4B28C, 0x550C7DC3D5FFB4E2, |
Sergunb | 0:8918a71cdbe9 | 76 | 0x72BE5D74F27B896F, 0x80DEB1FE3B1696B1, 0x9BDC06A725C71235, 0xC19BF174CF692694, |
Sergunb | 0:8918a71cdbe9 | 77 | 0xE49B69C19EF14AD2, 0xEFBE4786384F25E3, 0x0FC19DC68B8CD5B5, 0x240CA1CC77AC9C65, |
Sergunb | 0:8918a71cdbe9 | 78 | 0x2DE92C6F592B0275, 0x4A7484AA6EA6E483, 0x5CB0A9DCBD41FBD4, 0x76F988DA831153B5, |
Sergunb | 0:8918a71cdbe9 | 79 | 0x983E5152EE66DFAB, 0xA831C66D2DB43210, 0xB00327C898FB213F, 0xBF597FC7BEEF0EE4, |
Sergunb | 0:8918a71cdbe9 | 80 | 0xC6E00BF33DA88FC2, 0xD5A79147930AA725, 0x06CA6351E003826F, 0x142929670A0E6E70, |
Sergunb | 0:8918a71cdbe9 | 81 | 0x27B70A8546D22FFC, 0x2E1B21385C26C926, 0x4D2C6DFC5AC42AED, 0x53380D139D95B3DF, |
Sergunb | 0:8918a71cdbe9 | 82 | 0x650A73548BAF63DE, 0x766A0ABB3C77B2A8, 0x81C2C92E47EDAEE6, 0x92722C851482353B, |
Sergunb | 0:8918a71cdbe9 | 83 | 0xA2BFE8A14CF10364, 0xA81A664BBC423001, 0xC24B8B70D0F89791, 0xC76C51A30654BE30, |
Sergunb | 0:8918a71cdbe9 | 84 | 0xD192E819D6EF5218, 0xD69906245565A910, 0xF40E35855771202A, 0x106AA07032BBD1B8, |
Sergunb | 0:8918a71cdbe9 | 85 | 0x19A4C116B8D2D0C8, 0x1E376C085141AB53, 0x2748774CDF8EEB99, 0x34B0BCB5E19B48A8, |
Sergunb | 0:8918a71cdbe9 | 86 | 0x391C0CB3C5C95A63, 0x4ED8AA4AE3418ACB, 0x5B9CCA4F7763E373, 0x682E6FF3D6B2B8A3, |
Sergunb | 0:8918a71cdbe9 | 87 | 0x748F82EE5DEFB2FC, 0x78A5636F43172F60, 0x84C87814A1F0AB72, 0x8CC702081A6439EC, |
Sergunb | 0:8918a71cdbe9 | 88 | 0x90BEFFFA23631E28, 0xA4506CEBDE82BDE9, 0xBEF9A3F7B2C67915, 0xC67178F2E372532B, |
Sergunb | 0:8918a71cdbe9 | 89 | 0xCA273ECEEA26619C, 0xD186B8C721C0C207, 0xEADA7DD6CDE0EB1E, 0xF57D4F7FEE6ED178, |
Sergunb | 0:8918a71cdbe9 | 90 | 0x06F067AA72176FBA, 0x0A637DC5A2C898A6, 0x113F9804BEF90DAE, 0x1B710B35131C471B, |
Sergunb | 0:8918a71cdbe9 | 91 | 0x28DB77F523047D84, 0x32CAAB7B40C72493, 0x3C9EBE0A15C9BEBC, 0x431D67C49C100D4C, |
Sergunb | 0:8918a71cdbe9 | 92 | 0x4CC5D4BECB3E42B6, 0x597F299CFC657E2A, 0x5FCB6FAB3AD6FAEC, 0x6C44198C4A475817 |
Sergunb | 0:8918a71cdbe9 | 93 | }; |
Sergunb | 0:8918a71cdbe9 | 94 | |
Sergunb | 0:8918a71cdbe9 | 95 | //SHA-512 object identifier (2.16.840.1.101.3.4.2.3) |
Sergunb | 0:8918a71cdbe9 | 96 | static const uint8_t sha512Oid[] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03}; |
Sergunb | 0:8918a71cdbe9 | 97 | |
Sergunb | 0:8918a71cdbe9 | 98 | //Common interface for hash algorithms |
Sergunb | 0:8918a71cdbe9 | 99 | const HashAlgo sha512HashAlgo = |
Sergunb | 0:8918a71cdbe9 | 100 | { |
Sergunb | 0:8918a71cdbe9 | 101 | "SHA-512", |
Sergunb | 0:8918a71cdbe9 | 102 | sha512Oid, |
Sergunb | 0:8918a71cdbe9 | 103 | sizeof(sha512Oid), |
Sergunb | 0:8918a71cdbe9 | 104 | sizeof(Sha512Context), |
Sergunb | 0:8918a71cdbe9 | 105 | SHA512_BLOCK_SIZE, |
Sergunb | 0:8918a71cdbe9 | 106 | SHA512_DIGEST_SIZE, |
Sergunb | 0:8918a71cdbe9 | 107 | (HashAlgoCompute) sha512Compute, |
Sergunb | 0:8918a71cdbe9 | 108 | (HashAlgoInit) sha512Init, |
Sergunb | 0:8918a71cdbe9 | 109 | (HashAlgoUpdate) sha512Update, |
Sergunb | 0:8918a71cdbe9 | 110 | (HashAlgoFinal) sha512Final |
Sergunb | 0:8918a71cdbe9 | 111 | }; |
Sergunb | 0:8918a71cdbe9 | 112 | |
Sergunb | 0:8918a71cdbe9 | 113 | |
Sergunb | 0:8918a71cdbe9 | 114 | /** |
Sergunb | 0:8918a71cdbe9 | 115 | * @brief Digest a message using SHA-512 |
Sergunb | 0:8918a71cdbe9 | 116 | * @param[in] data Pointer to the message being hashed |
Sergunb | 0:8918a71cdbe9 | 117 | * @param[in] length Length of the message |
Sergunb | 0:8918a71cdbe9 | 118 | * @param[out] digest Pointer to the calculated digest |
Sergunb | 0:8918a71cdbe9 | 119 | * @return Error code |
Sergunb | 0:8918a71cdbe9 | 120 | **/ |
Sergunb | 0:8918a71cdbe9 | 121 | |
Sergunb | 0:8918a71cdbe9 | 122 | error_t sha512Compute(const void *data, size_t length, uint8_t *digest) |
Sergunb | 0:8918a71cdbe9 | 123 | { |
Sergunb | 0:8918a71cdbe9 | 124 | //Allocate a memory buffer to hold the SHA-512 context |
Sergunb | 0:8918a71cdbe9 | 125 | Sha512Context *context = cryptoAllocMem(sizeof(Sha512Context)); |
Sergunb | 0:8918a71cdbe9 | 126 | //Failed to allocate memory? |
Sergunb | 0:8918a71cdbe9 | 127 | if(context == NULL) |
Sergunb | 0:8918a71cdbe9 | 128 | return ERROR_OUT_OF_MEMORY; |
Sergunb | 0:8918a71cdbe9 | 129 | |
Sergunb | 0:8918a71cdbe9 | 130 | //Initialize the SHA-512 context |
Sergunb | 0:8918a71cdbe9 | 131 | sha512Init(context); |
Sergunb | 0:8918a71cdbe9 | 132 | //Digest the message |
Sergunb | 0:8918a71cdbe9 | 133 | sha512Update(context, data, length); |
Sergunb | 0:8918a71cdbe9 | 134 | //Finalize the SHA-512 message digest |
Sergunb | 0:8918a71cdbe9 | 135 | sha512Final(context, digest); |
Sergunb | 0:8918a71cdbe9 | 136 | |
Sergunb | 0:8918a71cdbe9 | 137 | //Free previously allocated memory |
Sergunb | 0:8918a71cdbe9 | 138 | cryptoFreeMem(context); |
Sergunb | 0:8918a71cdbe9 | 139 | //Successful processing |
Sergunb | 0:8918a71cdbe9 | 140 | return NO_ERROR; |
Sergunb | 0:8918a71cdbe9 | 141 | } |
Sergunb | 0:8918a71cdbe9 | 142 | |
Sergunb | 0:8918a71cdbe9 | 143 | |
Sergunb | 0:8918a71cdbe9 | 144 | /** |
Sergunb | 0:8918a71cdbe9 | 145 | * @brief Initialize SHA-512 message digest context |
Sergunb | 0:8918a71cdbe9 | 146 | * @param[in] context Pointer to the SHA-512 context to initialize |
Sergunb | 0:8918a71cdbe9 | 147 | **/ |
Sergunb | 0:8918a71cdbe9 | 148 | |
Sergunb | 0:8918a71cdbe9 | 149 | void sha512Init(Sha512Context *context) |
Sergunb | 0:8918a71cdbe9 | 150 | { |
Sergunb | 0:8918a71cdbe9 | 151 | //Set initial hash value |
Sergunb | 0:8918a71cdbe9 | 152 | context->h[0] = 0x6A09E667F3BCC908; |
Sergunb | 0:8918a71cdbe9 | 153 | context->h[1] = 0xBB67AE8584CAA73B; |
Sergunb | 0:8918a71cdbe9 | 154 | context->h[2] = 0x3C6EF372FE94F82B; |
Sergunb | 0:8918a71cdbe9 | 155 | context->h[3] = 0xA54FF53A5F1D36F1; |
Sergunb | 0:8918a71cdbe9 | 156 | context->h[4] = 0x510E527FADE682D1; |
Sergunb | 0:8918a71cdbe9 | 157 | context->h[5] = 0x9B05688C2B3E6C1F; |
Sergunb | 0:8918a71cdbe9 | 158 | context->h[6] = 0x1F83D9ABFB41BD6B; |
Sergunb | 0:8918a71cdbe9 | 159 | context->h[7] = 0x5BE0CD19137E2179; |
Sergunb | 0:8918a71cdbe9 | 160 | |
Sergunb | 0:8918a71cdbe9 | 161 | //Number of bytes in the buffer |
Sergunb | 0:8918a71cdbe9 | 162 | context->size = 0; |
Sergunb | 0:8918a71cdbe9 | 163 | //Total length of the message |
Sergunb | 0:8918a71cdbe9 | 164 | context->totalSize = 0; |
Sergunb | 0:8918a71cdbe9 | 165 | } |
Sergunb | 0:8918a71cdbe9 | 166 | |
Sergunb | 0:8918a71cdbe9 | 167 | |
Sergunb | 0:8918a71cdbe9 | 168 | /** |
Sergunb | 0:8918a71cdbe9 | 169 | * @brief Update the SHA-512 context with a portion of the message being hashed |
Sergunb | 0:8918a71cdbe9 | 170 | * @param[in] context Pointer to the SHA-512 context |
Sergunb | 0:8918a71cdbe9 | 171 | * @param[in] data Pointer to the buffer being hashed |
Sergunb | 0:8918a71cdbe9 | 172 | * @param[in] length Length of the buffer |
Sergunb | 0:8918a71cdbe9 | 173 | **/ |
Sergunb | 0:8918a71cdbe9 | 174 | |
Sergunb | 0:8918a71cdbe9 | 175 | void sha512Update(Sha512Context *context, const void *data, size_t length) |
Sergunb | 0:8918a71cdbe9 | 176 | { |
Sergunb | 0:8918a71cdbe9 | 177 | size_t n; |
Sergunb | 0:8918a71cdbe9 | 178 | |
Sergunb | 0:8918a71cdbe9 | 179 | //Process the incoming data |
Sergunb | 0:8918a71cdbe9 | 180 | while(length > 0) |
Sergunb | 0:8918a71cdbe9 | 181 | { |
Sergunb | 0:8918a71cdbe9 | 182 | //The buffer can hold at most 128 bytes |
Sergunb | 0:8918a71cdbe9 | 183 | n = MIN(length, 128 - context->size); |
Sergunb | 0:8918a71cdbe9 | 184 | |
Sergunb | 0:8918a71cdbe9 | 185 | //Copy the data to the buffer |
Sergunb | 0:8918a71cdbe9 | 186 | memcpy(context->buffer + context->size, data, n); |
Sergunb | 0:8918a71cdbe9 | 187 | |
Sergunb | 0:8918a71cdbe9 | 188 | //Update the SHA-512 context |
Sergunb | 0:8918a71cdbe9 | 189 | context->size += n; |
Sergunb | 0:8918a71cdbe9 | 190 | context->totalSize += n; |
Sergunb | 0:8918a71cdbe9 | 191 | //Advance the data pointer |
Sergunb | 0:8918a71cdbe9 | 192 | data = (uint8_t *) data + n; |
Sergunb | 0:8918a71cdbe9 | 193 | //Remaining bytes to process |
Sergunb | 0:8918a71cdbe9 | 194 | length -= n; |
Sergunb | 0:8918a71cdbe9 | 195 | |
Sergunb | 0:8918a71cdbe9 | 196 | //Process message in 16-word blocks |
Sergunb | 0:8918a71cdbe9 | 197 | if(context->size == 128) |
Sergunb | 0:8918a71cdbe9 | 198 | { |
Sergunb | 0:8918a71cdbe9 | 199 | //Transform the 16-word block |
Sergunb | 0:8918a71cdbe9 | 200 | sha512ProcessBlock(context); |
Sergunb | 0:8918a71cdbe9 | 201 | //Empty the buffer |
Sergunb | 0:8918a71cdbe9 | 202 | context->size = 0; |
Sergunb | 0:8918a71cdbe9 | 203 | } |
Sergunb | 0:8918a71cdbe9 | 204 | } |
Sergunb | 0:8918a71cdbe9 | 205 | } |
Sergunb | 0:8918a71cdbe9 | 206 | |
Sergunb | 0:8918a71cdbe9 | 207 | |
Sergunb | 0:8918a71cdbe9 | 208 | /** |
Sergunb | 0:8918a71cdbe9 | 209 | * @brief Finish the SHA-512 message digest |
Sergunb | 0:8918a71cdbe9 | 210 | * @param[in] context Pointer to the SHA-512 context |
Sergunb | 0:8918a71cdbe9 | 211 | * @param[out] digest Calculated digest (optional parameter) |
Sergunb | 0:8918a71cdbe9 | 212 | **/ |
Sergunb | 0:8918a71cdbe9 | 213 | |
Sergunb | 0:8918a71cdbe9 | 214 | void sha512Final(Sha512Context *context, uint8_t *digest) |
Sergunb | 0:8918a71cdbe9 | 215 | { |
Sergunb | 0:8918a71cdbe9 | 216 | uint_t i; |
Sergunb | 0:8918a71cdbe9 | 217 | size_t paddingSize; |
Sergunb | 0:8918a71cdbe9 | 218 | uint64_t totalSize; |
Sergunb | 0:8918a71cdbe9 | 219 | |
Sergunb | 0:8918a71cdbe9 | 220 | //Length of the original message (before padding) |
Sergunb | 0:8918a71cdbe9 | 221 | totalSize = context->totalSize * 8; |
Sergunb | 0:8918a71cdbe9 | 222 | |
Sergunb | 0:8918a71cdbe9 | 223 | //Pad the message so that its length is congruent to 112 modulo 128 |
Sergunb | 0:8918a71cdbe9 | 224 | if(context->size < 112) |
Sergunb | 0:8918a71cdbe9 | 225 | paddingSize = 112 - context->size; |
Sergunb | 0:8918a71cdbe9 | 226 | else |
Sergunb | 0:8918a71cdbe9 | 227 | paddingSize = 128 + 112 - context->size; |
Sergunb | 0:8918a71cdbe9 | 228 | |
Sergunb | 0:8918a71cdbe9 | 229 | //Append padding |
Sergunb | 0:8918a71cdbe9 | 230 | sha512Update(context, padding, paddingSize); |
Sergunb | 0:8918a71cdbe9 | 231 | |
Sergunb | 0:8918a71cdbe9 | 232 | //Append the length of the original message |
Sergunb | 0:8918a71cdbe9 | 233 | context->w[14] = 0; |
Sergunb | 0:8918a71cdbe9 | 234 | context->w[15] = htobe64(totalSize); |
Sergunb | 0:8918a71cdbe9 | 235 | |
Sergunb | 0:8918a71cdbe9 | 236 | //Calculate the message digest |
Sergunb | 0:8918a71cdbe9 | 237 | sha512ProcessBlock(context); |
Sergunb | 0:8918a71cdbe9 | 238 | |
Sergunb | 0:8918a71cdbe9 | 239 | //Convert from host byte order to big-endian byte order |
Sergunb | 0:8918a71cdbe9 | 240 | for(i = 0; i < 8; i++) |
Sergunb | 0:8918a71cdbe9 | 241 | context->h[i] = htobe64(context->h[i]); |
Sergunb | 0:8918a71cdbe9 | 242 | |
Sergunb | 0:8918a71cdbe9 | 243 | //Copy the resulting digest |
Sergunb | 0:8918a71cdbe9 | 244 | if(digest != NULL) |
Sergunb | 0:8918a71cdbe9 | 245 | memcpy(digest, context->digest, SHA512_DIGEST_SIZE); |
Sergunb | 0:8918a71cdbe9 | 246 | } |
Sergunb | 0:8918a71cdbe9 | 247 | |
Sergunb | 0:8918a71cdbe9 | 248 | |
Sergunb | 0:8918a71cdbe9 | 249 | /** |
Sergunb | 0:8918a71cdbe9 | 250 | * @brief Process message in 16-word blocks |
Sergunb | 0:8918a71cdbe9 | 251 | * @param[in] context Pointer to the SHA-512 context |
Sergunb | 0:8918a71cdbe9 | 252 | **/ |
Sergunb | 0:8918a71cdbe9 | 253 | |
Sergunb | 0:8918a71cdbe9 | 254 | void sha512ProcessBlock(Sha512Context *context) |
Sergunb | 0:8918a71cdbe9 | 255 | { |
Sergunb | 0:8918a71cdbe9 | 256 | uint_t t; |
Sergunb | 0:8918a71cdbe9 | 257 | uint64_t temp1; |
Sergunb | 0:8918a71cdbe9 | 258 | uint64_t temp2; |
Sergunb | 0:8918a71cdbe9 | 259 | |
Sergunb | 0:8918a71cdbe9 | 260 | //Initialize the 8 working registers |
Sergunb | 0:8918a71cdbe9 | 261 | uint64_t a = context->h[0]; |
Sergunb | 0:8918a71cdbe9 | 262 | uint64_t b = context->h[1]; |
Sergunb | 0:8918a71cdbe9 | 263 | uint64_t c = context->h[2]; |
Sergunb | 0:8918a71cdbe9 | 264 | uint64_t d = context->h[3]; |
Sergunb | 0:8918a71cdbe9 | 265 | uint64_t e = context->h[4]; |
Sergunb | 0:8918a71cdbe9 | 266 | uint64_t f = context->h[5]; |
Sergunb | 0:8918a71cdbe9 | 267 | uint64_t g = context->h[6]; |
Sergunb | 0:8918a71cdbe9 | 268 | uint64_t h = context->h[7]; |
Sergunb | 0:8918a71cdbe9 | 269 | |
Sergunb | 0:8918a71cdbe9 | 270 | //Process message in 16-word blocks |
Sergunb | 0:8918a71cdbe9 | 271 | uint64_t *w = context->w; |
Sergunb | 0:8918a71cdbe9 | 272 | |
Sergunb | 0:8918a71cdbe9 | 273 | //Convert from big-endian byte order to host byte order |
Sergunb | 0:8918a71cdbe9 | 274 | for(t = 0; t < 16; t++) |
Sergunb | 0:8918a71cdbe9 | 275 | w[t] = betoh64(w[t]); |
Sergunb | 0:8918a71cdbe9 | 276 | |
Sergunb | 0:8918a71cdbe9 | 277 | //SHA-512 hash computation (alternate method) |
Sergunb | 0:8918a71cdbe9 | 278 | for(t = 0; t < 80; t++) |
Sergunb | 0:8918a71cdbe9 | 279 | { |
Sergunb | 0:8918a71cdbe9 | 280 | //Prepare the message schedule |
Sergunb | 0:8918a71cdbe9 | 281 | if(t >= 16) |
Sergunb | 0:8918a71cdbe9 | 282 | W(t) += SIGMA4(W(t + 14)) + W(t + 9) + SIGMA3(W(t + 1)); |
Sergunb | 0:8918a71cdbe9 | 283 | |
Sergunb | 0:8918a71cdbe9 | 284 | //Calculate T1 and T2 |
Sergunb | 0:8918a71cdbe9 | 285 | temp1 = h + SIGMA2(e) + CH(e, f, g) + k[t] + W(t); |
Sergunb | 0:8918a71cdbe9 | 286 | temp2 = SIGMA1(a) + MAJ(a, b, c); |
Sergunb | 0:8918a71cdbe9 | 287 | |
Sergunb | 0:8918a71cdbe9 | 288 | //Update the working registers |
Sergunb | 0:8918a71cdbe9 | 289 | h = g; |
Sergunb | 0:8918a71cdbe9 | 290 | g = f; |
Sergunb | 0:8918a71cdbe9 | 291 | f = e; |
Sergunb | 0:8918a71cdbe9 | 292 | e = d + temp1; |
Sergunb | 0:8918a71cdbe9 | 293 | d = c; |
Sergunb | 0:8918a71cdbe9 | 294 | c = b; |
Sergunb | 0:8918a71cdbe9 | 295 | b = a; |
Sergunb | 0:8918a71cdbe9 | 296 | a = temp1 + temp2; |
Sergunb | 0:8918a71cdbe9 | 297 | } |
Sergunb | 0:8918a71cdbe9 | 298 | |
Sergunb | 0:8918a71cdbe9 | 299 | //Update the hash value |
Sergunb | 0:8918a71cdbe9 | 300 | context->h[0] += a; |
Sergunb | 0:8918a71cdbe9 | 301 | context->h[1] += b; |
Sergunb | 0:8918a71cdbe9 | 302 | context->h[2] += c; |
Sergunb | 0:8918a71cdbe9 | 303 | context->h[3] += d; |
Sergunb | 0:8918a71cdbe9 | 304 | context->h[4] += e; |
Sergunb | 0:8918a71cdbe9 | 305 | context->h[5] += f; |
Sergunb | 0:8918a71cdbe9 | 306 | context->h[6] += g; |
Sergunb | 0:8918a71cdbe9 | 307 | context->h[7] += h; |
Sergunb | 0:8918a71cdbe9 | 308 | } |
Sergunb | 0:8918a71cdbe9 | 309 | |
Sergunb | 0:8918a71cdbe9 | 310 | #endif |
Sergunb | 0:8918a71cdbe9 | 311 |