Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Wed Nov 16 21:38:39 2016 -0800
Revision:
15:956c6d205aa7
Parent:
0:fa2de1b79154
Child:
19:2e0811512ceb
1.0.10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 0:fa2de1b79154 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 0:fa2de1b79154 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 0:fa2de1b79154 3
Azure.IoT Build 0:fa2de1b79154 4 /*************************** sha224-256.c ***************************/
Azure.IoT Build 0:fa2de1b79154 5 /********************* See RFC 4634 for details *********************/
Azure.IoT Build 0:fa2de1b79154 6 /*
Azure.IoT Build 0:fa2de1b79154 7 * Description:
Azure.IoT Build 0:fa2de1b79154 8 * This file implements the Secure Hash Signature Standard
Azure.IoT Build 0:fa2de1b79154 9 * algorithms as defined in the National Institute of Standards
Azure.IoT Build 0:fa2de1b79154 10 * and Technology Federal Information Processing Standards
Azure.IoT Build 0:fa2de1b79154 11 * Publication (FIPS PUB) 180-1 published on April 17, 1995, 180-2
Azure.IoT Build 0:fa2de1b79154 12 * published on August 1, 2002, and the FIPS PUB 180-2 Change
Azure.IoT Build 0:fa2de1b79154 13 * Notice published on February 28, 2004.
Azure.IoT Build 0:fa2de1b79154 14 *
Azure.IoT Build 0:fa2de1b79154 15 * A combined document showing all algorithms is available at
Azure.IoT Build 0:fa2de1b79154 16 * http://csrc.nist.gov/publications/fips/
Azure.IoT Build 0:fa2de1b79154 17 * fips180-2/fips180-2withchangenotice.pdf
Azure.IoT Build 0:fa2de1b79154 18 *
Azure.IoT Build 0:fa2de1b79154 19 * The SHA-224 and SHA-256 algorithms produce 224-bit and 256-bit
Azure.IoT Build 0:fa2de1b79154 20 * message digests for a given data stream. It should take about
Azure.IoT Build 0:fa2de1b79154 21 * 2**n steps to find a message with the same digest as a given
Azure.IoT Build 0:fa2de1b79154 22 * message and 2**(n/2) to find any two messages with the same
Azure.IoT Build 0:fa2de1b79154 23 * digest, when n is the digest size in bits. Therefore, this
Azure.IoT Build 0:fa2de1b79154 24 * algorithm can serve as a means of providing a
Azure.IoT Build 0:fa2de1b79154 25 * "fingerprint" for a message.
Azure.IoT Build 0:fa2de1b79154 26 *
Azure.IoT Build 0:fa2de1b79154 27 * Portability Issues:
Azure.IoT Build 0:fa2de1b79154 28 * SHA-224 and SHA-256 are defined in terms of 32-bit "words".
Azure.IoT Build 0:fa2de1b79154 29 * This code uses <stdint.h> (included via "sha.h") to define 32
Azure.IoT Build 0:fa2de1b79154 30 * and 8 bit unsigned integer types. If your C compiler does not
Azure.IoT Build 0:fa2de1b79154 31 * support 32 bit unsigned integers, this code is not
Azure.IoT Build 0:fa2de1b79154 32 * appropriate.
Azure.IoT Build 0:fa2de1b79154 33 *
Azure.IoT Build 0:fa2de1b79154 34 * Caveats:
Azure.IoT Build 0:fa2de1b79154 35 * SHA-224 and SHA-256 are designed to work with messages less
Azure.IoT Build 0:fa2de1b79154 36 * than 2^64 bits long. This implementation uses SHA224/256Input()
Azure.IoT Build 0:fa2de1b79154 37 * to hash the bits that are a multiple of the size of an 8-bit
Azure.IoT Build 0:fa2de1b79154 38 * character, and then uses SHA224/256FinalBits() to hash the
Azure.IoT Build 0:fa2de1b79154 39 * final few bits of the input.
Azure.IoT Build 0:fa2de1b79154 40 */
Azure.IoT Build 0:fa2de1b79154 41
Azure.IoT Build 0:fa2de1b79154 42 #include <stdlib.h>
Azure.IoT Build 0:fa2de1b79154 43 #ifdef _CRTDBG_MAP_ALLOC
Azure.IoT Build 0:fa2de1b79154 44 #include <crtdbg.h>
Azure.IoT Build 0:fa2de1b79154 45 #endif
Azure.IoT Build 0:fa2de1b79154 46 #include "azure_c_shared_utility/gballoc.h"
Azure.IoT Build 0:fa2de1b79154 47
Azure.IoT Build 0:fa2de1b79154 48 #include "azure_c_shared_utility/sha.h"
Azure.IoT Build 0:fa2de1b79154 49 #include "azure_c_shared_utility/sha-private.h"
Azure.IoT Build 0:fa2de1b79154 50 /* Define the SHA shift, rotate left and rotate right macro */
Azure.IoT Build 0:fa2de1b79154 51 #define SHA256_SHR(bits,word) ((word) >> (bits))
Azure.IoT Build 0:fa2de1b79154 52 #define SHA256_ROTL(bits,word) \
Azure.IoT Build 0:fa2de1b79154 53 (((word) << (bits)) | ((word) >> (32-(bits))))
Azure.IoT Build 0:fa2de1b79154 54 #define SHA256_ROTR(bits,word) \
Azure.IoT Build 0:fa2de1b79154 55 (((word) >> (bits)) | ((word) << (32-(bits))))
Azure.IoT Build 0:fa2de1b79154 56
Azure.IoT Build 0:fa2de1b79154 57 /* Define the SHA SIGMA and sigma macros */
Azure.IoT Build 0:fa2de1b79154 58 #define SHA256_SIGMA0(word) \
Azure.IoT Build 0:fa2de1b79154 59 (SHA256_ROTR( 2,word) ^ SHA256_ROTR(13,word) ^ SHA256_ROTR(22,word))
Azure.IoT Build 0:fa2de1b79154 60 #define SHA256_SIGMA1(word) \
Azure.IoT Build 0:fa2de1b79154 61 (SHA256_ROTR( 6,word) ^ SHA256_ROTR(11,word) ^ SHA256_ROTR(25,word))
Azure.IoT Build 0:fa2de1b79154 62 #define SHA256_sigma0(word) \
Azure.IoT Build 0:fa2de1b79154 63 (SHA256_ROTR( 7,word) ^ SHA256_ROTR(18,word) ^ SHA256_SHR( 3,word))
Azure.IoT Build 0:fa2de1b79154 64 #define SHA256_sigma1(word) \
Azure.IoT Build 0:fa2de1b79154 65 (SHA256_ROTR(17,word) ^ SHA256_ROTR(19,word) ^ SHA256_SHR(10,word))
Azure.IoT Build 0:fa2de1b79154 66
Azure.IoT Build 0:fa2de1b79154 67 /*
Azure.IoT Build 0:fa2de1b79154 68 * add "length" to the length
Azure.IoT Build 0:fa2de1b79154 69 */
Azure.IoT Build 0:fa2de1b79154 70 #define SHA224_256AddLength(context, length) \
Azure.IoT Build 0:fa2de1b79154 71 (addTemp = (context)->Length_Low, (context)->Corrupted = \
Azure.IoT Build 0:fa2de1b79154 72 (((context)->Length_Low += (length)) < addTemp) && \
Azure.IoT Build 0:fa2de1b79154 73 (++(context)->Length_High == 0) ? 1 : 0)
Azure.IoT Build 0:fa2de1b79154 74
Azure.IoT Build 0:fa2de1b79154 75 /* Local Function Prototypes */
Azure.IoT Build 0:fa2de1b79154 76 static void SHA224_256Finalize(SHA256Context *context,
Azure.IoT Build 0:fa2de1b79154 77 uint8_t Pad_Byte);
Azure.IoT Build 0:fa2de1b79154 78 static void SHA224_256PadMessage(SHA256Context *context,
Azure.IoT Build 0:fa2de1b79154 79 uint8_t Pad_Byte);
Azure.IoT Build 0:fa2de1b79154 80 static void SHA224_256ProcessMessageBlock(SHA256Context *context);
Azure.IoT Build 0:fa2de1b79154 81 static int SHA224_256Reset(SHA256Context *context, uint32_t *H0);
Azure.IoT Build 0:fa2de1b79154 82 static int SHA224_256ResultN(SHA256Context *context,
Azure.IoT Build 0:fa2de1b79154 83 uint8_t Message_Digest[], int HashSize);
Azure.IoT Build 0:fa2de1b79154 84
Azure.IoT Build 0:fa2de1b79154 85 /* Initial Hash Values: FIPS-180-2 Change Notice 1 */
Azure.IoT Build 0:fa2de1b79154 86 static uint32_t SHA224_H0[SHA256HashSize / 4] = {
Azure.IoT Build 0:fa2de1b79154 87 0xC1059ED8, 0x367CD507, 0x3070DD17, 0xF70E5939,
Azure.IoT Build 0:fa2de1b79154 88 0xFFC00B31, 0x68581511, 0x64F98FA7, 0xBEFA4FA4
Azure.IoT Build 0:fa2de1b79154 89 };
Azure.IoT Build 0:fa2de1b79154 90
Azure.IoT Build 0:fa2de1b79154 91 /* Initial Hash Values: FIPS-180-2 section 5.3.2 */
Azure.IoT Build 0:fa2de1b79154 92 static uint32_t SHA256_H0[SHA256HashSize / 4] = {
Azure.IoT Build 0:fa2de1b79154 93 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
Azure.IoT Build 0:fa2de1b79154 94 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19
Azure.IoT Build 0:fa2de1b79154 95 };
Azure.IoT Build 0:fa2de1b79154 96
Azure.IoT Build 0:fa2de1b79154 97 /*
Azure.IoT Build 0:fa2de1b79154 98 * SHA224Reset
Azure.IoT Build 0:fa2de1b79154 99 *
Azure.IoT Build 0:fa2de1b79154 100 * Description:
Azure.IoT Build 0:fa2de1b79154 101 * This function will initialize the SHA384Context in preparation
Azure.IoT Build 0:fa2de1b79154 102 * for computing a new SHA224 message digest.
Azure.IoT Build 0:fa2de1b79154 103 *
Azure.IoT Build 0:fa2de1b79154 104 * Parameters:
Azure.IoT Build 0:fa2de1b79154 105 * context: [in/out]
Azure.IoT Build 0:fa2de1b79154 106 * The context to reset.
Azure.IoT Build 0:fa2de1b79154 107 *
Azure.IoT Build 0:fa2de1b79154 108 * Returns:
Azure.IoT Build 0:fa2de1b79154 109 * sha Error Code.
Azure.IoT Build 0:fa2de1b79154 110 */
Azure.IoT Build 0:fa2de1b79154 111 int SHA224Reset(SHA224Context *context)
Azure.IoT Build 0:fa2de1b79154 112 {
Azure.IoT Build 0:fa2de1b79154 113 return SHA224_256Reset(context, SHA224_H0);
Azure.IoT Build 0:fa2de1b79154 114 }
Azure.IoT Build 0:fa2de1b79154 115
Azure.IoT Build 0:fa2de1b79154 116 /*
Azure.IoT Build 0:fa2de1b79154 117 * SHA224Input
Azure.IoT Build 0:fa2de1b79154 118 *
Azure.IoT Build 0:fa2de1b79154 119 * Description:
Azure.IoT Build 0:fa2de1b79154 120 * This function accepts an array of octets as the next portion
Azure.IoT Build 0:fa2de1b79154 121 * of the message.
Azure.IoT Build 0:fa2de1b79154 122 *
Azure.IoT Build 0:fa2de1b79154 123 * Parameters:
Azure.IoT Build 0:fa2de1b79154 124 * context: [in/out]
Azure.IoT Build 0:fa2de1b79154 125 * The SHA context to update
Azure.IoT Build 0:fa2de1b79154 126 * message_array: [in]
Azure.IoT Build 0:fa2de1b79154 127 * An array of characters representing the next portion of
Azure.IoT Build 0:fa2de1b79154 128 * the message.
Azure.IoT Build 0:fa2de1b79154 129 * length: [in]
Azure.IoT Build 0:fa2de1b79154 130 * The length of the message in message_array
Azure.IoT Build 0:fa2de1b79154 131 *
Azure.IoT Build 0:fa2de1b79154 132 * Returns:
Azure.IoT Build 0:fa2de1b79154 133 * sha Error Code.
Azure.IoT Build 0:fa2de1b79154 134 *
Azure.IoT Build 0:fa2de1b79154 135 */
Azure.IoT Build 0:fa2de1b79154 136 int SHA224Input(SHA224Context *context, const uint8_t *message_array,
Azure.IoT Build 0:fa2de1b79154 137 unsigned int length)
Azure.IoT Build 0:fa2de1b79154 138 {
Azure.IoT Build 0:fa2de1b79154 139 return SHA256Input(context, message_array, length);
Azure.IoT Build 0:fa2de1b79154 140 }
Azure.IoT Build 0:fa2de1b79154 141
Azure.IoT Build 0:fa2de1b79154 142 /*
Azure.IoT Build 0:fa2de1b79154 143 * SHA224FinalBits
Azure.IoT Build 0:fa2de1b79154 144 *
Azure.IoT Build 0:fa2de1b79154 145 * Description:
Azure.IoT Build 0:fa2de1b79154 146 * This function will add in any final bits of the message.
Azure.IoT Build 0:fa2de1b79154 147 *
Azure.IoT Build 0:fa2de1b79154 148 * Parameters:
Azure.IoT Build 0:fa2de1b79154 149 * context: [in/out]
Azure.IoT Build 0:fa2de1b79154 150 * The SHA context to update
Azure.IoT Build 0:fa2de1b79154 151 * message_bits: [in]
Azure.IoT Build 0:fa2de1b79154 152 * The final bits of the message, in the upper portion of the
Azure.IoT Build 0:fa2de1b79154 153 * byte. (Use 0b###00000 instead of 0b00000### to input the
Azure.IoT Build 0:fa2de1b79154 154 * three bits ###.)
Azure.IoT Build 0:fa2de1b79154 155 * length: [in]
Azure.IoT Build 0:fa2de1b79154 156 * The number of bits in message_bits, between 1 and 7.
Azure.IoT Build 0:fa2de1b79154 157 *
Azure.IoT Build 0:fa2de1b79154 158 * Returns:
Azure.IoT Build 0:fa2de1b79154 159 * sha Error Code.
Azure.IoT Build 0:fa2de1b79154 160 */
Azure.IoT Build 0:fa2de1b79154 161 int SHA224FinalBits(SHA224Context *context,
Azure.IoT Build 0:fa2de1b79154 162 const uint8_t message_bits, unsigned int length)
Azure.IoT Build 0:fa2de1b79154 163 {
Azure.IoT Build 0:fa2de1b79154 164 return SHA256FinalBits(context, message_bits, length);
Azure.IoT Build 0:fa2de1b79154 165 }
Azure.IoT Build 0:fa2de1b79154 166
Azure.IoT Build 0:fa2de1b79154 167 /*
Azure.IoT Build 0:fa2de1b79154 168 * SHA224Result
Azure.IoT Build 0:fa2de1b79154 169 *
Azure.IoT Build 0:fa2de1b79154 170 * Description:
Azure.IoT Build 0:fa2de1b79154 171 * This function will return the 224-bit message
Azure.IoT Build 0:fa2de1b79154 172 * digest into the Message_Digest array provided by the caller.
Azure.IoT Build 0:fa2de1b79154 173 * NOTE: The first octet of hash is stored in the 0th element,
Azure.IoT Build 0:fa2de1b79154 174 * the last octet of hash in the 28th element.
Azure.IoT Build 0:fa2de1b79154 175 *
Azure.IoT Build 0:fa2de1b79154 176 * Parameters:
Azure.IoT Build 0:fa2de1b79154 177 * context: [in/out]
Azure.IoT Build 0:fa2de1b79154 178 * The context to use to calculate the SHA hash.
Azure.IoT Build 0:fa2de1b79154 179 * Message_Digest: [out]
Azure.IoT Build 0:fa2de1b79154 180 * Where the digest is returned.
Azure.IoT Build 0:fa2de1b79154 181 *
Azure.IoT Build 0:fa2de1b79154 182 * Returns:
Azure.IoT Build 0:fa2de1b79154 183 * sha Error Code.
Azure.IoT Build 0:fa2de1b79154 184 */
Azure.IoT Build 0:fa2de1b79154 185 int SHA224Result(SHA224Context *context,
Azure.IoT Build 0:fa2de1b79154 186 uint8_t Message_Digest[SHA224HashSize])
Azure.IoT Build 0:fa2de1b79154 187 {
Azure.IoT Build 0:fa2de1b79154 188 return SHA224_256ResultN(context, Message_Digest, SHA224HashSize);
Azure.IoT Build 0:fa2de1b79154 189 }
Azure.IoT Build 0:fa2de1b79154 190
Azure.IoT Build 0:fa2de1b79154 191 /*
Azure.IoT Build 0:fa2de1b79154 192 * SHA256Reset
Azure.IoT Build 0:fa2de1b79154 193 *
Azure.IoT Build 0:fa2de1b79154 194 * Description:
Azure.IoT Build 0:fa2de1b79154 195 * This function will initialize the SHA256Context in preparation
Azure.IoT Build 0:fa2de1b79154 196 * for computing a new SHA256 message digest.
Azure.IoT Build 0:fa2de1b79154 197 *
Azure.IoT Build 0:fa2de1b79154 198 * Parameters:
Azure.IoT Build 0:fa2de1b79154 199 * context: [in/out]
Azure.IoT Build 0:fa2de1b79154 200 * The context to reset.
Azure.IoT Build 0:fa2de1b79154 201 *
Azure.IoT Build 0:fa2de1b79154 202 * Returns:
Azure.IoT Build 0:fa2de1b79154 203 * sha Error Code.
Azure.IoT Build 0:fa2de1b79154 204 */
Azure.IoT Build 0:fa2de1b79154 205 int SHA256Reset(SHA256Context *context)
Azure.IoT Build 0:fa2de1b79154 206 {
Azure.IoT Build 0:fa2de1b79154 207 return SHA224_256Reset(context, SHA256_H0);
Azure.IoT Build 0:fa2de1b79154 208 }
Azure.IoT Build 0:fa2de1b79154 209
Azure.IoT Build 0:fa2de1b79154 210 /*
Azure.IoT Build 0:fa2de1b79154 211 * SHA256Input
Azure.IoT Build 0:fa2de1b79154 212 *
Azure.IoT Build 0:fa2de1b79154 213 * Description:
Azure.IoT Build 0:fa2de1b79154 214 * This function accepts an array of octets as the next portion
Azure.IoT Build 0:fa2de1b79154 215 * of the message.
Azure.IoT Build 0:fa2de1b79154 216 *
Azure.IoT Build 0:fa2de1b79154 217 * Parameters:
Azure.IoT Build 0:fa2de1b79154 218 * context: [in/out]
Azure.IoT Build 0:fa2de1b79154 219 * The SHA context to update
Azure.IoT Build 0:fa2de1b79154 220 * message_array: [in]
Azure.IoT Build 0:fa2de1b79154 221 * An array of characters representing the next portion of
Azure.IoT Build 0:fa2de1b79154 222 * the message.
Azure.IoT Build 0:fa2de1b79154 223 * length: [in]
Azure.IoT Build 0:fa2de1b79154 224 * The length of the message in message_array
Azure.IoT Build 0:fa2de1b79154 225 *
Azure.IoT Build 0:fa2de1b79154 226 * Returns:
Azure.IoT Build 0:fa2de1b79154 227 * sha Error Code.
Azure.IoT Build 0:fa2de1b79154 228 */
Azure.IoT Build 0:fa2de1b79154 229 int SHA256Input(SHA256Context *context, const uint8_t *message_array,
Azure.IoT Build 0:fa2de1b79154 230 unsigned int length)
Azure.IoT Build 0:fa2de1b79154 231 {
AzureIoTClient 15:956c6d205aa7 232 uint32_t addTemp;
Azure.IoT Build 0:fa2de1b79154 233 if (!length)
Azure.IoT Build 0:fa2de1b79154 234 return shaSuccess;
Azure.IoT Build 0:fa2de1b79154 235
Azure.IoT Build 0:fa2de1b79154 236 if (!context || !message_array)
Azure.IoT Build 0:fa2de1b79154 237 return shaNull;
Azure.IoT Build 0:fa2de1b79154 238
Azure.IoT Build 0:fa2de1b79154 239 if (context->Computed) {
Azure.IoT Build 0:fa2de1b79154 240 context->Corrupted = shaStateError;
Azure.IoT Build 0:fa2de1b79154 241 return shaStateError;
Azure.IoT Build 0:fa2de1b79154 242 }
Azure.IoT Build 0:fa2de1b79154 243
Azure.IoT Build 0:fa2de1b79154 244 if (context->Corrupted)
Azure.IoT Build 0:fa2de1b79154 245 return context->Corrupted;
Azure.IoT Build 0:fa2de1b79154 246
Azure.IoT Build 0:fa2de1b79154 247 while (length-- && !context->Corrupted) {
Azure.IoT Build 0:fa2de1b79154 248 context->Message_Block[context->Message_Block_Index++] =
Azure.IoT Build 0:fa2de1b79154 249 (*message_array & 0xFF);
Azure.IoT Build 0:fa2de1b79154 250
Azure.IoT Build 0:fa2de1b79154 251 if (!SHA224_256AddLength(context, 8) &&
Azure.IoT Build 0:fa2de1b79154 252 (context->Message_Block_Index == SHA256_Message_Block_Size))
Azure.IoT Build 0:fa2de1b79154 253 SHA224_256ProcessMessageBlock(context);
Azure.IoT Build 0:fa2de1b79154 254
Azure.IoT Build 0:fa2de1b79154 255 message_array++;
Azure.IoT Build 0:fa2de1b79154 256 }
Azure.IoT Build 0:fa2de1b79154 257
Azure.IoT Build 0:fa2de1b79154 258 return shaSuccess;
Azure.IoT Build 0:fa2de1b79154 259
Azure.IoT Build 0:fa2de1b79154 260 }
Azure.IoT Build 0:fa2de1b79154 261
Azure.IoT Build 0:fa2de1b79154 262 /*
Azure.IoT Build 0:fa2de1b79154 263 * SHA256FinalBits
Azure.IoT Build 0:fa2de1b79154 264 *
Azure.IoT Build 0:fa2de1b79154 265 * Description:
Azure.IoT Build 0:fa2de1b79154 266 * This function will add in any final bits of the message.
Azure.IoT Build 0:fa2de1b79154 267 *
Azure.IoT Build 0:fa2de1b79154 268 * Parameters:
Azure.IoT Build 0:fa2de1b79154 269 * context: [in/out]
Azure.IoT Build 0:fa2de1b79154 270 * The SHA context to update
Azure.IoT Build 0:fa2de1b79154 271 * message_bits: [in]
Azure.IoT Build 0:fa2de1b79154 272 * The final bits of the message, in the upper portion of the
Azure.IoT Build 0:fa2de1b79154 273 * byte. (Use 0b###00000 instead of 0b00000### to input the
Azure.IoT Build 0:fa2de1b79154 274 * three bits ###.)
Azure.IoT Build 0:fa2de1b79154 275 * length: [in]
Azure.IoT Build 0:fa2de1b79154 276 * The number of bits in message_bits, between 1 and 7.
Azure.IoT Build 0:fa2de1b79154 277 *
Azure.IoT Build 0:fa2de1b79154 278 * Returns:
Azure.IoT Build 0:fa2de1b79154 279 * sha Error Code.
Azure.IoT Build 0:fa2de1b79154 280 */
Azure.IoT Build 0:fa2de1b79154 281 int SHA256FinalBits(SHA256Context *context,
Azure.IoT Build 0:fa2de1b79154 282 const uint8_t message_bits, unsigned int length)
Azure.IoT Build 0:fa2de1b79154 283 {
AzureIoTClient 15:956c6d205aa7 284 uint32_t addTemp;
Azure.IoT Build 0:fa2de1b79154 285 uint8_t masks[8] = {
Azure.IoT Build 0:fa2de1b79154 286 /* 0 0b00000000 */ 0x00, /* 1 0b10000000 */ 0x80,
Azure.IoT Build 0:fa2de1b79154 287 /* 2 0b11000000 */ 0xC0, /* 3 0b11100000 */ 0xE0,
Azure.IoT Build 0:fa2de1b79154 288 /* 4 0b11110000 */ 0xF0, /* 5 0b11111000 */ 0xF8,
Azure.IoT Build 0:fa2de1b79154 289 /* 6 0b11111100 */ 0xFC, /* 7 0b11111110 */ 0xFE
Azure.IoT Build 0:fa2de1b79154 290 };
Azure.IoT Build 0:fa2de1b79154 291
Azure.IoT Build 0:fa2de1b79154 292 uint8_t markbit[8] = {
Azure.IoT Build 0:fa2de1b79154 293 /* 0 0b10000000 */ 0x80, /* 1 0b01000000 */ 0x40,
Azure.IoT Build 0:fa2de1b79154 294 /* 2 0b00100000 */ 0x20, /* 3 0b00010000 */ 0x10,
Azure.IoT Build 0:fa2de1b79154 295 /* 4 0b00001000 */ 0x08, /* 5 0b00000100 */ 0x04,
Azure.IoT Build 0:fa2de1b79154 296 /* 6 0b00000010 */ 0x02, /* 7 0b00000001 */ 0x01
Azure.IoT Build 0:fa2de1b79154 297 };
Azure.IoT Build 0:fa2de1b79154 298
Azure.IoT Build 0:fa2de1b79154 299 if (!length)
Azure.IoT Build 0:fa2de1b79154 300 return shaSuccess;
Azure.IoT Build 0:fa2de1b79154 301
Azure.IoT Build 0:fa2de1b79154 302 if (!context)
Azure.IoT Build 0:fa2de1b79154 303 return shaNull;
Azure.IoT Build 0:fa2de1b79154 304
Azure.IoT Build 0:fa2de1b79154 305 if ((context->Computed) || (length >= 8) || (length == 0)) {
Azure.IoT Build 0:fa2de1b79154 306 context->Corrupted = shaStateError;
Azure.IoT Build 0:fa2de1b79154 307 return shaStateError;
Azure.IoT Build 0:fa2de1b79154 308 }
Azure.IoT Build 0:fa2de1b79154 309
Azure.IoT Build 0:fa2de1b79154 310 if (context->Corrupted)
Azure.IoT Build 0:fa2de1b79154 311 return context->Corrupted;
Azure.IoT Build 0:fa2de1b79154 312
Azure.IoT Build 0:fa2de1b79154 313 SHA224_256AddLength(context, length);
Azure.IoT Build 0:fa2de1b79154 314 SHA224_256Finalize(context, (uint8_t)
Azure.IoT Build 0:fa2de1b79154 315 ((message_bits & masks[length]) | markbit[length]));
Azure.IoT Build 0:fa2de1b79154 316
Azure.IoT Build 0:fa2de1b79154 317 return shaSuccess;
Azure.IoT Build 0:fa2de1b79154 318 }
Azure.IoT Build 0:fa2de1b79154 319
Azure.IoT Build 0:fa2de1b79154 320 /*
Azure.IoT Build 0:fa2de1b79154 321 * SHA256Result
Azure.IoT Build 0:fa2de1b79154 322 *
Azure.IoT Build 0:fa2de1b79154 323 * Description:
Azure.IoT Build 0:fa2de1b79154 324 * This function will return the 256-bit message
Azure.IoT Build 0:fa2de1b79154 325 * digest into the Message_Digest array provided by the caller.
Azure.IoT Build 0:fa2de1b79154 326 * NOTE: The first octet of hash is stored in the 0th element,
Azure.IoT Build 0:fa2de1b79154 327 * the last octet of hash in the 32nd element.
Azure.IoT Build 0:fa2de1b79154 328 *
Azure.IoT Build 0:fa2de1b79154 329 * Parameters:
Azure.IoT Build 0:fa2de1b79154 330 * context: [in/out]
Azure.IoT Build 0:fa2de1b79154 331 * The context to use to calculate the SHA hash.
Azure.IoT Build 0:fa2de1b79154 332 * Message_Digest: [out]
Azure.IoT Build 0:fa2de1b79154 333 * Where the digest is returned.
Azure.IoT Build 0:fa2de1b79154 334 *
Azure.IoT Build 0:fa2de1b79154 335 * Returns:
Azure.IoT Build 0:fa2de1b79154 336 * sha Error Code.
Azure.IoT Build 0:fa2de1b79154 337 */
Azure.IoT Build 0:fa2de1b79154 338 int SHA256Result(SHA256Context *context, uint8_t Message_Digest[])
Azure.IoT Build 0:fa2de1b79154 339 {
Azure.IoT Build 0:fa2de1b79154 340 return SHA224_256ResultN(context, Message_Digest, SHA256HashSize);
Azure.IoT Build 0:fa2de1b79154 341 }
Azure.IoT Build 0:fa2de1b79154 342
Azure.IoT Build 0:fa2de1b79154 343 /*
Azure.IoT Build 0:fa2de1b79154 344 * SHA224_256Finalize
Azure.IoT Build 0:fa2de1b79154 345 *
Azure.IoT Build 0:fa2de1b79154 346 * Description:
Azure.IoT Build 0:fa2de1b79154 347 * This helper function finishes off the digest calculations.
Azure.IoT Build 0:fa2de1b79154 348 *
Azure.IoT Build 0:fa2de1b79154 349 * Parameters:
Azure.IoT Build 0:fa2de1b79154 350 * context: [in/out]
Azure.IoT Build 0:fa2de1b79154 351 * The SHA context to update
Azure.IoT Build 0:fa2de1b79154 352 * Pad_Byte: [in]
Azure.IoT Build 0:fa2de1b79154 353 * The last byte to add to the digest before the 0-padding
Azure.IoT Build 0:fa2de1b79154 354 * and length. This will contain the last bits of the message
Azure.IoT Build 0:fa2de1b79154 355 * followed by another single bit. If the message was an
Azure.IoT Build 0:fa2de1b79154 356 * exact multiple of 8-bits long, Pad_Byte will be 0x80.
Azure.IoT Build 0:fa2de1b79154 357 *
Azure.IoT Build 0:fa2de1b79154 358 * Returns:
Azure.IoT Build 0:fa2de1b79154 359 * sha Error Code.
Azure.IoT Build 0:fa2de1b79154 360 */
Azure.IoT Build 0:fa2de1b79154 361 static void SHA224_256Finalize(SHA256Context *context,
Azure.IoT Build 0:fa2de1b79154 362 uint8_t Pad_Byte)
Azure.IoT Build 0:fa2de1b79154 363 {
Azure.IoT Build 0:fa2de1b79154 364 int i;
Azure.IoT Build 0:fa2de1b79154 365 SHA224_256PadMessage(context, Pad_Byte);
Azure.IoT Build 0:fa2de1b79154 366 /* message may be sensitive, so clear it out */
Azure.IoT Build 0:fa2de1b79154 367 for (i = 0; i < SHA256_Message_Block_Size; ++i)
Azure.IoT Build 0:fa2de1b79154 368 context->Message_Block[i] = 0;
Azure.IoT Build 0:fa2de1b79154 369 context->Length_Low = 0; /* and clear length */
Azure.IoT Build 0:fa2de1b79154 370 context->Length_High = 0;
Azure.IoT Build 0:fa2de1b79154 371 context->Computed = 1;
Azure.IoT Build 0:fa2de1b79154 372 }
Azure.IoT Build 0:fa2de1b79154 373
Azure.IoT Build 0:fa2de1b79154 374 /*
Azure.IoT Build 0:fa2de1b79154 375 * SHA224_256PadMessage
Azure.IoT Build 0:fa2de1b79154 376 *
Azure.IoT Build 0:fa2de1b79154 377 * Description:
Azure.IoT Build 0:fa2de1b79154 378 * According to the standard, the message must be padded to an
Azure.IoT Build 0:fa2de1b79154 379 * even 512 bits. The first padding bit must be a '1'. The
Azure.IoT Build 0:fa2de1b79154 380 * last 64 bits represent the length of the original message.
Azure.IoT Build 0:fa2de1b79154 381 * All bits in between should be 0. This helper function will pad
Azure.IoT Build 0:fa2de1b79154 382 * the message according to those rules by filling the
Azure.IoT Build 0:fa2de1b79154 383 * Message_Block array accordingly. When it returns, it can be
Azure.IoT Build 0:fa2de1b79154 384 * assumed that the message digest has been computed.
Azure.IoT Build 0:fa2de1b79154 385 *
Azure.IoT Build 0:fa2de1b79154 386 * Parameters:
Azure.IoT Build 0:fa2de1b79154 387 * context: [in/out]
Azure.IoT Build 0:fa2de1b79154 388 * The context to pad
Azure.IoT Build 0:fa2de1b79154 389 * Pad_Byte: [in]
Azure.IoT Build 0:fa2de1b79154 390 * The last byte to add to the digest before the 0-padding
Azure.IoT Build 0:fa2de1b79154 391 * and length. This will contain the last bits of the message
Azure.IoT Build 0:fa2de1b79154 392 * followed by another single bit. If the message was an
Azure.IoT Build 0:fa2de1b79154 393 * exact multiple of 8-bits long, Pad_Byte will be 0x80.
Azure.IoT Build 0:fa2de1b79154 394 *
Azure.IoT Build 0:fa2de1b79154 395 * Returns:
Azure.IoT Build 0:fa2de1b79154 396 * Nothing.
Azure.IoT Build 0:fa2de1b79154 397 */
Azure.IoT Build 0:fa2de1b79154 398 static void SHA224_256PadMessage(SHA256Context *context,
Azure.IoT Build 0:fa2de1b79154 399 uint8_t Pad_Byte)
Azure.IoT Build 0:fa2de1b79154 400 {
Azure.IoT Build 0:fa2de1b79154 401 /*
Azure.IoT Build 0:fa2de1b79154 402 * Check to see if the current message block is too small to hold
Azure.IoT Build 0:fa2de1b79154 403 * the initial padding bits and length. If so, we will pad the
Azure.IoT Build 0:fa2de1b79154 404 * block, process it, and then continue padding into a second
Azure.IoT Build 0:fa2de1b79154 405 * block.
Azure.IoT Build 0:fa2de1b79154 406 */
Azure.IoT Build 0:fa2de1b79154 407 if (context->Message_Block_Index >= (SHA256_Message_Block_Size - 8)) {
Azure.IoT Build 0:fa2de1b79154 408 context->Message_Block[context->Message_Block_Index++] = Pad_Byte;
Azure.IoT Build 0:fa2de1b79154 409 while (context->Message_Block_Index < SHA256_Message_Block_Size)
Azure.IoT Build 0:fa2de1b79154 410 context->Message_Block[context->Message_Block_Index++] = 0;
Azure.IoT Build 0:fa2de1b79154 411 SHA224_256ProcessMessageBlock(context);
Azure.IoT Build 0:fa2de1b79154 412 }
Azure.IoT Build 0:fa2de1b79154 413 else
Azure.IoT Build 0:fa2de1b79154 414 context->Message_Block[context->Message_Block_Index++] = Pad_Byte;
Azure.IoT Build 0:fa2de1b79154 415
Azure.IoT Build 0:fa2de1b79154 416 while (context->Message_Block_Index < (SHA256_Message_Block_Size - 8))
Azure.IoT Build 0:fa2de1b79154 417 context->Message_Block[context->Message_Block_Index++] = 0;
Azure.IoT Build 0:fa2de1b79154 418
Azure.IoT Build 0:fa2de1b79154 419 /*
Azure.IoT Build 0:fa2de1b79154 420 * Store the message length as the last 8 octets
Azure.IoT Build 0:fa2de1b79154 421 */
Azure.IoT Build 0:fa2de1b79154 422 context->Message_Block[56] = (uint8_t)(context->Length_High >> 24);
Azure.IoT Build 0:fa2de1b79154 423 context->Message_Block[57] = (uint8_t)(context->Length_High >> 16);
Azure.IoT Build 0:fa2de1b79154 424 context->Message_Block[58] = (uint8_t)(context->Length_High >> 8);
Azure.IoT Build 0:fa2de1b79154 425 context->Message_Block[59] = (uint8_t)(context->Length_High);
Azure.IoT Build 0:fa2de1b79154 426 context->Message_Block[60] = (uint8_t)(context->Length_Low >> 24);
Azure.IoT Build 0:fa2de1b79154 427 context->Message_Block[61] = (uint8_t)(context->Length_Low >> 16);
Azure.IoT Build 0:fa2de1b79154 428 context->Message_Block[62] = (uint8_t)(context->Length_Low >> 8);
Azure.IoT Build 0:fa2de1b79154 429 context->Message_Block[63] = (uint8_t)(context->Length_Low);
Azure.IoT Build 0:fa2de1b79154 430
Azure.IoT Build 0:fa2de1b79154 431 SHA224_256ProcessMessageBlock(context);
Azure.IoT Build 0:fa2de1b79154 432 }
Azure.IoT Build 0:fa2de1b79154 433
Azure.IoT Build 0:fa2de1b79154 434 /*
Azure.IoT Build 0:fa2de1b79154 435 * SHA224_256ProcessMessageBlock
Azure.IoT Build 0:fa2de1b79154 436 *
Azure.IoT Build 0:fa2de1b79154 437 * Description:
Azure.IoT Build 0:fa2de1b79154 438 * This function will process the next 512 bits of the message
Azure.IoT Build 0:fa2de1b79154 439 * stored in the Message_Block array.
Azure.IoT Build 0:fa2de1b79154 440 *
Azure.IoT Build 0:fa2de1b79154 441 * Parameters:
Azure.IoT Build 0:fa2de1b79154 442 * context: [in/out]
Azure.IoT Build 0:fa2de1b79154 443 * The SHA context to update
Azure.IoT Build 0:fa2de1b79154 444 *
Azure.IoT Build 0:fa2de1b79154 445 * Returns:
Azure.IoT Build 0:fa2de1b79154 446 * Nothing.
Azure.IoT Build 0:fa2de1b79154 447 *
Azure.IoT Build 0:fa2de1b79154 448 * Comments:
Azure.IoT Build 0:fa2de1b79154 449 * Many of the variable names in this code, especially the
Azure.IoT Build 0:fa2de1b79154 450 * single character names, were used because those were the
Azure.IoT Build 0:fa2de1b79154 451 * names used in the publication.
Azure.IoT Build 0:fa2de1b79154 452 */
Azure.IoT Build 0:fa2de1b79154 453 static void SHA224_256ProcessMessageBlock(SHA256Context *context)
Azure.IoT Build 0:fa2de1b79154 454 {
Azure.IoT Build 0:fa2de1b79154 455 /* Constants defined in FIPS-180-2, section 4.2.2 */
Azure.IoT Build 0:fa2de1b79154 456 static const uint32_t K[64] = {
Azure.IoT Build 0:fa2de1b79154 457 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b,
Azure.IoT Build 0:fa2de1b79154 458 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01,
Azure.IoT Build 0:fa2de1b79154 459 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7,
Azure.IoT Build 0:fa2de1b79154 460 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
Azure.IoT Build 0:fa2de1b79154 461 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152,
Azure.IoT Build 0:fa2de1b79154 462 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
Azure.IoT Build 0:fa2de1b79154 463 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc,
Azure.IoT Build 0:fa2de1b79154 464 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
Azure.IoT Build 0:fa2de1b79154 465 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819,
Azure.IoT Build 0:fa2de1b79154 466 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08,
Azure.IoT Build 0:fa2de1b79154 467 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f,
Azure.IoT Build 0:fa2de1b79154 468 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
Azure.IoT Build 0:fa2de1b79154 469 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
Azure.IoT Build 0:fa2de1b79154 470 };
Azure.IoT Build 0:fa2de1b79154 471 int t, t4; /* Loop counter */
Azure.IoT Build 0:fa2de1b79154 472 uint32_t temp1, temp2; /* Temporary word value */
Azure.IoT Build 0:fa2de1b79154 473 uint32_t W[64]; /* Word sequence */
Azure.IoT Build 0:fa2de1b79154 474 uint32_t A, B, C, D, E, F, G, H; /* Word buffers */
Azure.IoT Build 0:fa2de1b79154 475
Azure.IoT Build 0:fa2de1b79154 476 /*
Azure.IoT Build 0:fa2de1b79154 477 * Initialize the first 16 words in the array W
Azure.IoT Build 0:fa2de1b79154 478 */
Azure.IoT Build 0:fa2de1b79154 479 for (t = t4 = 0; t < 16; t++, t4 += 4)
Azure.IoT Build 0:fa2de1b79154 480 W[t] = (((uint32_t)context->Message_Block[t4]) << 24) |
Azure.IoT Build 0:fa2de1b79154 481 (((uint32_t)context->Message_Block[t4 + 1]) << 16) |
Azure.IoT Build 0:fa2de1b79154 482 (((uint32_t)context->Message_Block[t4 + 2]) << 8) |
Azure.IoT Build 0:fa2de1b79154 483 (((uint32_t)context->Message_Block[t4 + 3]));
Azure.IoT Build 0:fa2de1b79154 484
Azure.IoT Build 0:fa2de1b79154 485 for (t = 16; t < 64; t++)
Azure.IoT Build 0:fa2de1b79154 486 W[t] = SHA256_sigma1(W[t - 2]) + W[t - 7] +
Azure.IoT Build 0:fa2de1b79154 487 SHA256_sigma0(W[t - 15]) + W[t - 16];
Azure.IoT Build 0:fa2de1b79154 488
Azure.IoT Build 0:fa2de1b79154 489 A = context->Intermediate_Hash[0];
Azure.IoT Build 0:fa2de1b79154 490 B = context->Intermediate_Hash[1];
Azure.IoT Build 0:fa2de1b79154 491 C = context->Intermediate_Hash[2];
Azure.IoT Build 0:fa2de1b79154 492 D = context->Intermediate_Hash[3];
Azure.IoT Build 0:fa2de1b79154 493 E = context->Intermediate_Hash[4];
Azure.IoT Build 0:fa2de1b79154 494 F = context->Intermediate_Hash[5];
Azure.IoT Build 0:fa2de1b79154 495 G = context->Intermediate_Hash[6];
Azure.IoT Build 0:fa2de1b79154 496 H = context->Intermediate_Hash[7];
Azure.IoT Build 0:fa2de1b79154 497
Azure.IoT Build 0:fa2de1b79154 498 for (t = 0; t < 64; t++) {
Azure.IoT Build 0:fa2de1b79154 499 temp1 = H + SHA256_SIGMA1(E) + SHA_Ch(E, F, G) + K[t] + W[t];
Azure.IoT Build 0:fa2de1b79154 500 temp2 = SHA256_SIGMA0(A) + SHA_Maj(A, B, C);
Azure.IoT Build 0:fa2de1b79154 501 H = G;
Azure.IoT Build 0:fa2de1b79154 502 G = F;
Azure.IoT Build 0:fa2de1b79154 503 F = E;
Azure.IoT Build 0:fa2de1b79154 504 E = D + temp1;
Azure.IoT Build 0:fa2de1b79154 505 D = C;
Azure.IoT Build 0:fa2de1b79154 506 C = B;
Azure.IoT Build 0:fa2de1b79154 507 B = A;
Azure.IoT Build 0:fa2de1b79154 508 A = temp1 + temp2;
Azure.IoT Build 0:fa2de1b79154 509 }
Azure.IoT Build 0:fa2de1b79154 510
Azure.IoT Build 0:fa2de1b79154 511 context->Intermediate_Hash[0] += A;
Azure.IoT Build 0:fa2de1b79154 512 context->Intermediate_Hash[1] += B;
Azure.IoT Build 0:fa2de1b79154 513 context->Intermediate_Hash[2] += C;
Azure.IoT Build 0:fa2de1b79154 514 context->Intermediate_Hash[3] += D;
Azure.IoT Build 0:fa2de1b79154 515 context->Intermediate_Hash[4] += E;
Azure.IoT Build 0:fa2de1b79154 516 context->Intermediate_Hash[5] += F;
Azure.IoT Build 0:fa2de1b79154 517 context->Intermediate_Hash[6] += G;
Azure.IoT Build 0:fa2de1b79154 518 context->Intermediate_Hash[7] += H;
Azure.IoT Build 0:fa2de1b79154 519
Azure.IoT Build 0:fa2de1b79154 520 context->Message_Block_Index = 0;
Azure.IoT Build 0:fa2de1b79154 521 }
Azure.IoT Build 0:fa2de1b79154 522
Azure.IoT Build 0:fa2de1b79154 523 /*
Azure.IoT Build 0:fa2de1b79154 524 * SHA224_256Reset
Azure.IoT Build 0:fa2de1b79154 525 *
Azure.IoT Build 0:fa2de1b79154 526 * Description:
Azure.IoT Build 0:fa2de1b79154 527 * This helper function will initialize the SHA256Context in
Azure.IoT Build 0:fa2de1b79154 528 * preparation for computing a new SHA256 message digest.
Azure.IoT Build 0:fa2de1b79154 529 *
Azure.IoT Build 0:fa2de1b79154 530 * Parameters:
Azure.IoT Build 0:fa2de1b79154 531 * context: [in/out]
Azure.IoT Build 0:fa2de1b79154 532 * The context to reset.
Azure.IoT Build 0:fa2de1b79154 533 * H0
Azure.IoT Build 0:fa2de1b79154 534 * The initial hash value to use.
Azure.IoT Build 0:fa2de1b79154 535 *
Azure.IoT Build 0:fa2de1b79154 536 * Returns:
Azure.IoT Build 0:fa2de1b79154 537 * sha Error Code.
Azure.IoT Build 0:fa2de1b79154 538 */
Azure.IoT Build 0:fa2de1b79154 539 static int SHA224_256Reset(SHA256Context *context, uint32_t *H0)
Azure.IoT Build 0:fa2de1b79154 540 {
Azure.IoT Build 0:fa2de1b79154 541 if (!context)
Azure.IoT Build 0:fa2de1b79154 542 return shaNull;
Azure.IoT Build 0:fa2de1b79154 543
Azure.IoT Build 0:fa2de1b79154 544 context->Length_Low = 0;
Azure.IoT Build 0:fa2de1b79154 545 context->Length_High = 0;
Azure.IoT Build 0:fa2de1b79154 546 context->Message_Block_Index = 0;
Azure.IoT Build 0:fa2de1b79154 547
Azure.IoT Build 0:fa2de1b79154 548 context->Intermediate_Hash[0] = H0[0];
Azure.IoT Build 0:fa2de1b79154 549 context->Intermediate_Hash[1] = H0[1];
Azure.IoT Build 0:fa2de1b79154 550 context->Intermediate_Hash[2] = H0[2];
Azure.IoT Build 0:fa2de1b79154 551 context->Intermediate_Hash[3] = H0[3];
Azure.IoT Build 0:fa2de1b79154 552 context->Intermediate_Hash[4] = H0[4];
Azure.IoT Build 0:fa2de1b79154 553 context->Intermediate_Hash[5] = H0[5];
Azure.IoT Build 0:fa2de1b79154 554 context->Intermediate_Hash[6] = H0[6];
Azure.IoT Build 0:fa2de1b79154 555 context->Intermediate_Hash[7] = H0[7];
Azure.IoT Build 0:fa2de1b79154 556
Azure.IoT Build 0:fa2de1b79154 557 context->Computed = 0;
Azure.IoT Build 0:fa2de1b79154 558 context->Corrupted = 0;
Azure.IoT Build 0:fa2de1b79154 559
Azure.IoT Build 0:fa2de1b79154 560 return shaSuccess;
Azure.IoT Build 0:fa2de1b79154 561 }
Azure.IoT Build 0:fa2de1b79154 562
Azure.IoT Build 0:fa2de1b79154 563 /*
Azure.IoT Build 0:fa2de1b79154 564 * SHA224_256ResultN
Azure.IoT Build 0:fa2de1b79154 565 *
Azure.IoT Build 0:fa2de1b79154 566 * Description:
Azure.IoT Build 0:fa2de1b79154 567 * This helper function will return the 224-bit or 256-bit message
Azure.IoT Build 0:fa2de1b79154 568 * digest into the Message_Digest array provided by the caller.
Azure.IoT Build 0:fa2de1b79154 569 * NOTE: The first octet of hash is stored in the 0th element,
Azure.IoT Build 0:fa2de1b79154 570 * the last octet of hash in the 28th/32nd element.
Azure.IoT Build 0:fa2de1b79154 571 *
Azure.IoT Build 0:fa2de1b79154 572 * Parameters:
Azure.IoT Build 0:fa2de1b79154 573 * context: [in/out]
Azure.IoT Build 0:fa2de1b79154 574 * The context to use to calculate the SHA hash.
Azure.IoT Build 0:fa2de1b79154 575 * Message_Digest: [out]
Azure.IoT Build 0:fa2de1b79154 576 * Where the digest is returned.
Azure.IoT Build 0:fa2de1b79154 577 * HashSize: [in]
Azure.IoT Build 0:fa2de1b79154 578 * The size of the hash, either 28 or 32.
Azure.IoT Build 0:fa2de1b79154 579 *
Azure.IoT Build 0:fa2de1b79154 580 * Returns:
Azure.IoT Build 0:fa2de1b79154 581 * sha Error Code.
Azure.IoT Build 0:fa2de1b79154 582 */
Azure.IoT Build 0:fa2de1b79154 583 static int SHA224_256ResultN(SHA256Context *context,
Azure.IoT Build 0:fa2de1b79154 584 uint8_t Message_Digest[], int HashSize)
Azure.IoT Build 0:fa2de1b79154 585 {
Azure.IoT Build 0:fa2de1b79154 586 int i;
Azure.IoT Build 0:fa2de1b79154 587
Azure.IoT Build 0:fa2de1b79154 588 if (!context || !Message_Digest)
Azure.IoT Build 0:fa2de1b79154 589 return shaNull;
Azure.IoT Build 0:fa2de1b79154 590
Azure.IoT Build 0:fa2de1b79154 591 if (context->Corrupted)
Azure.IoT Build 0:fa2de1b79154 592 return context->Corrupted;
Azure.IoT Build 0:fa2de1b79154 593
Azure.IoT Build 0:fa2de1b79154 594 if (!context->Computed)
Azure.IoT Build 0:fa2de1b79154 595 SHA224_256Finalize(context, 0x80);
Azure.IoT Build 0:fa2de1b79154 596
Azure.IoT Build 0:fa2de1b79154 597 for (i = 0; i < HashSize; ++i)
Azure.IoT Build 0:fa2de1b79154 598 Message_Digest[i] = (uint8_t)
Azure.IoT Build 0:fa2de1b79154 599 (context->Intermediate_Hash[i >> 2] >> 8 * (3 - (i & 0x03)));
Azure.IoT Build 0:fa2de1b79154 600
Azure.IoT Build 0:fa2de1b79154 601 return shaSuccess;
Azure.IoT Build 0:fa2de1b79154 602 }
Azure.IoT Build 0:fa2de1b79154 603