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