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