Azure IoT common library
Fork of azure_c_shared_utility by
sha1.c@15:956c6d205aa7, 2016-11-16 (annotated)
- 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?
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 | /**************************** sha1.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-1 algorithm produces a 160-bit message digest for a |
Azure.IoT Build | 0:fa2de1b79154 | 20 | * given data stream. It should take about 2**n steps to find a |
Azure.IoT Build | 0:fa2de1b79154 | 21 | * message with the same digest as a given message and |
Azure.IoT Build | 0:fa2de1b79154 | 22 | * 2**(n/2) to find any two messages with the same digest, |
Azure.IoT Build | 0:fa2de1b79154 | 23 | * 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-1 is defined in terms of 32-bit "words". This code |
Azure.IoT Build | 0:fa2de1b79154 | 29 | * uses <stdint.h> (included via "sha.h") to define 32 and 8 |
Azure.IoT Build | 0:fa2de1b79154 | 30 | * 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-1 is designed to work with messages less than 2^64 bits |
Azure.IoT Build | 0:fa2de1b79154 | 36 | * long. This implementation uses SHA1Input() to hash the bits |
Azure.IoT Build | 0:fa2de1b79154 | 37 | * that are a multiple of the size of an 8-bit character, and then |
Azure.IoT Build | 0:fa2de1b79154 | 38 | * uses SHA1FinalBits() to hash the final few bits of the input. |
Azure.IoT Build | 0:fa2de1b79154 | 39 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 40 | |
Azure.IoT Build | 0:fa2de1b79154 | 41 | #include <stdlib.h> |
Azure.IoT Build | 0:fa2de1b79154 | 42 | #ifdef _CRTDBG_MAP_ALLOC |
Azure.IoT Build | 0:fa2de1b79154 | 43 | #include <crtdbg.h> |
Azure.IoT Build | 0:fa2de1b79154 | 44 | #endif |
Azure.IoT Build | 0:fa2de1b79154 | 45 | #include "azure_c_shared_utility/gballoc.h" |
Azure.IoT Build | 0:fa2de1b79154 | 46 | |
Azure.IoT Build | 0:fa2de1b79154 | 47 | #include "azure_c_shared_utility/sha.h" |
Azure.IoT Build | 0:fa2de1b79154 | 48 | #include "azure_c_shared_utility/sha-private.h" |
Azure.IoT Build | 0:fa2de1b79154 | 49 | |
Azure.IoT Build | 0:fa2de1b79154 | 50 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 51 | * Define the SHA1 circular left shift macro |
Azure.IoT Build | 0:fa2de1b79154 | 52 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 53 | #define SHA1_ROTL(bits,word) \ |
Azure.IoT Build | 0:fa2de1b79154 | 54 | (((word) << (bits)) | ((word) >> (32-(bits)))) |
Azure.IoT Build | 0:fa2de1b79154 | 55 | |
Azure.IoT Build | 0:fa2de1b79154 | 56 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 57 | * add "length" to the length |
Azure.IoT Build | 0:fa2de1b79154 | 58 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 59 | #define SHA1AddLength(context, length) \ |
Azure.IoT Build | 0:fa2de1b79154 | 60 | (addTemp = (context)->Length_Low, \ |
Azure.IoT Build | 0:fa2de1b79154 | 61 | (context)->Corrupted = \ |
Azure.IoT Build | 0:fa2de1b79154 | 62 | (((context)->Length_Low += (length)) < addTemp) && \ |
Azure.IoT Build | 0:fa2de1b79154 | 63 | (++(context)->Length_High == 0) ? 1 : 0) |
Azure.IoT Build | 0:fa2de1b79154 | 64 | |
Azure.IoT Build | 0:fa2de1b79154 | 65 | /* Local Function Prototypes */ |
Azure.IoT Build | 0:fa2de1b79154 | 66 | static void SHA1Finalize(SHA1Context *context, uint8_t Pad_Byte); |
Azure.IoT Build | 0:fa2de1b79154 | 67 | static void SHA1PadMessage(SHA1Context *, uint8_t Pad_Byte); |
Azure.IoT Build | 0:fa2de1b79154 | 68 | static void SHA1ProcessMessageBlock(SHA1Context *); |
Azure.IoT Build | 0:fa2de1b79154 | 69 | |
Azure.IoT Build | 0:fa2de1b79154 | 70 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 71 | * SHA1Reset |
Azure.IoT Build | 0:fa2de1b79154 | 72 | * |
Azure.IoT Build | 0:fa2de1b79154 | 73 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 74 | * This function will initialize the SHA1Context in preparation |
Azure.IoT Build | 0:fa2de1b79154 | 75 | * for computing a new SHA1 message digest. |
Azure.IoT Build | 0:fa2de1b79154 | 76 | * |
Azure.IoT Build | 0:fa2de1b79154 | 77 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 78 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 79 | * The context to reset. |
Azure.IoT Build | 0:fa2de1b79154 | 80 | * |
Azure.IoT Build | 0:fa2de1b79154 | 81 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 82 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 83 | * |
Azure.IoT Build | 0:fa2de1b79154 | 84 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 85 | int SHA1Reset(SHA1Context *context) |
Azure.IoT Build | 0:fa2de1b79154 | 86 | { |
Azure.IoT Build | 0:fa2de1b79154 | 87 | if (!context) |
Azure.IoT Build | 0:fa2de1b79154 | 88 | return shaNull; |
Azure.IoT Build | 0:fa2de1b79154 | 89 | |
Azure.IoT Build | 0:fa2de1b79154 | 90 | context->Length_Low = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 91 | context->Length_High = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 92 | context->Message_Block_Index = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 93 | |
Azure.IoT Build | 0:fa2de1b79154 | 94 | |
Azure.IoT Build | 0:fa2de1b79154 | 95 | /* Initial Hash Values: FIPS-180-2 section 5.3.1 */ |
Azure.IoT Build | 0:fa2de1b79154 | 96 | context->Intermediate_Hash[0] = 0x67452301; |
Azure.IoT Build | 0:fa2de1b79154 | 97 | context->Intermediate_Hash[1] = 0xEFCDAB89; |
Azure.IoT Build | 0:fa2de1b79154 | 98 | context->Intermediate_Hash[2] = 0x98BADCFE; |
Azure.IoT Build | 0:fa2de1b79154 | 99 | context->Intermediate_Hash[3] = 0x10325476; |
Azure.IoT Build | 0:fa2de1b79154 | 100 | context->Intermediate_Hash[4] = 0xC3D2E1F0; |
Azure.IoT Build | 0:fa2de1b79154 | 101 | |
Azure.IoT Build | 0:fa2de1b79154 | 102 | context->Computed = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 103 | context->Corrupted = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 104 | |
Azure.IoT Build | 0:fa2de1b79154 | 105 | return shaSuccess; |
Azure.IoT Build | 0:fa2de1b79154 | 106 | } |
Azure.IoT Build | 0:fa2de1b79154 | 107 | |
Azure.IoT Build | 0:fa2de1b79154 | 108 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 109 | * SHA1Input |
Azure.IoT Build | 0:fa2de1b79154 | 110 | * |
Azure.IoT Build | 0:fa2de1b79154 | 111 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 112 | * This function accepts an array of octets as the next portion |
Azure.IoT Build | 0:fa2de1b79154 | 113 | * of the message. |
Azure.IoT Build | 0:fa2de1b79154 | 114 | * |
Azure.IoT Build | 0:fa2de1b79154 | 115 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 116 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 117 | * The SHA context to update |
Azure.IoT Build | 0:fa2de1b79154 | 118 | * message_array: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 119 | * An array of characters representing the next portion of |
Azure.IoT Build | 0:fa2de1b79154 | 120 | * the message. |
Azure.IoT Build | 0:fa2de1b79154 | 121 | * length: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 122 | * The length of the message in message_array |
Azure.IoT Build | 0:fa2de1b79154 | 123 | * |
Azure.IoT Build | 0:fa2de1b79154 | 124 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 125 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 126 | * |
Azure.IoT Build | 0:fa2de1b79154 | 127 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 128 | int SHA1Input(SHA1Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 129 | const uint8_t *message_array, unsigned length) |
Azure.IoT Build | 0:fa2de1b79154 | 130 | { |
AzureIoTClient | 15:956c6d205aa7 | 131 | uint32_t addTemp; |
Azure.IoT Build | 0:fa2de1b79154 | 132 | if (!length) |
Azure.IoT Build | 0:fa2de1b79154 | 133 | return shaSuccess; |
Azure.IoT Build | 0:fa2de1b79154 | 134 | |
Azure.IoT Build | 0:fa2de1b79154 | 135 | if (!context || !message_array) |
Azure.IoT Build | 0:fa2de1b79154 | 136 | return shaNull; |
Azure.IoT Build | 0:fa2de1b79154 | 137 | |
Azure.IoT Build | 0:fa2de1b79154 | 138 | if (context->Computed) { |
Azure.IoT Build | 0:fa2de1b79154 | 139 | context->Corrupted = shaStateError; |
Azure.IoT Build | 0:fa2de1b79154 | 140 | return shaStateError; |
Azure.IoT Build | 0:fa2de1b79154 | 141 | } |
Azure.IoT Build | 0:fa2de1b79154 | 142 | |
Azure.IoT Build | 0:fa2de1b79154 | 143 | if (context->Corrupted) |
Azure.IoT Build | 0:fa2de1b79154 | 144 | return context->Corrupted; |
Azure.IoT Build | 0:fa2de1b79154 | 145 | |
Azure.IoT Build | 0:fa2de1b79154 | 146 | while (length-- && !context->Corrupted) { |
Azure.IoT Build | 0:fa2de1b79154 | 147 | context->Message_Block[context->Message_Block_Index++] = |
Azure.IoT Build | 0:fa2de1b79154 | 148 | (*message_array & 0xFF); |
Azure.IoT Build | 0:fa2de1b79154 | 149 | |
Azure.IoT Build | 0:fa2de1b79154 | 150 | if (!SHA1AddLength(context, 8) && |
Azure.IoT Build | 0:fa2de1b79154 | 151 | (context->Message_Block_Index == SHA1_Message_Block_Size)) |
Azure.IoT Build | 0:fa2de1b79154 | 152 | SHA1ProcessMessageBlock(context); |
Azure.IoT Build | 0:fa2de1b79154 | 153 | |
Azure.IoT Build | 0:fa2de1b79154 | 154 | message_array++; |
Azure.IoT Build | 0:fa2de1b79154 | 155 | } |
Azure.IoT Build | 0:fa2de1b79154 | 156 | |
Azure.IoT Build | 0:fa2de1b79154 | 157 | return shaSuccess; |
Azure.IoT Build | 0:fa2de1b79154 | 158 | } |
Azure.IoT Build | 0:fa2de1b79154 | 159 | |
Azure.IoT Build | 0:fa2de1b79154 | 160 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 161 | * SHA1FinalBits |
Azure.IoT Build | 0:fa2de1b79154 | 162 | * |
Azure.IoT Build | 0:fa2de1b79154 | 163 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 164 | * This function will add in any final bits of the message. |
Azure.IoT Build | 0:fa2de1b79154 | 165 | * |
Azure.IoT Build | 0:fa2de1b79154 | 166 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 167 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 168 | * The SHA context to update |
Azure.IoT Build | 0:fa2de1b79154 | 169 | * message_bits: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 170 | * The final bits of the message, in the upper portion of the |
Azure.IoT Build | 0:fa2de1b79154 | 171 | * byte. (Use 0b###00000 instead of 0b00000### to input the |
Azure.IoT Build | 0:fa2de1b79154 | 172 | * three bits ###.) |
Azure.IoT Build | 0:fa2de1b79154 | 173 | * length: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 174 | * The number of bits in message_bits, between 1 and 7. |
Azure.IoT Build | 0:fa2de1b79154 | 175 | * |
Azure.IoT Build | 0:fa2de1b79154 | 176 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 177 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 178 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 179 | int SHA1FinalBits(SHA1Context *context, const uint8_t message_bits, |
Azure.IoT Build | 0:fa2de1b79154 | 180 | unsigned int length) |
Azure.IoT Build | 0:fa2de1b79154 | 181 | { |
AzureIoTClient | 15:956c6d205aa7 | 182 | uint32_t addTemp; |
AzureIoTClient | 15:956c6d205aa7 | 183 | |
Azure.IoT Build | 0:fa2de1b79154 | 184 | uint8_t masks[8] = { |
Azure.IoT Build | 0:fa2de1b79154 | 185 | /* 0 0b00000000 */ 0x00, /* 1 0b10000000 */ 0x80, |
Azure.IoT Build | 0:fa2de1b79154 | 186 | /* 2 0b11000000 */ 0xC0, /* 3 0b11100000 */ 0xE0, |
Azure.IoT Build | 0:fa2de1b79154 | 187 | /* 4 0b11110000 */ 0xF0, /* 5 0b11111000 */ 0xF8, |
Azure.IoT Build | 0:fa2de1b79154 | 188 | /* 6 0b11111100 */ 0xFC, /* 7 0b11111110 */ 0xFE |
Azure.IoT Build | 0:fa2de1b79154 | 189 | }; |
Azure.IoT Build | 0:fa2de1b79154 | 190 | uint8_t markbit[8] = { |
Azure.IoT Build | 0:fa2de1b79154 | 191 | /* 0 0b10000000 */ 0x80, /* 1 0b01000000 */ 0x40, |
Azure.IoT Build | 0:fa2de1b79154 | 192 | /* 2 0b00100000 */ 0x20, /* 3 0b00010000 */ 0x10, |
Azure.IoT Build | 0:fa2de1b79154 | 193 | /* 4 0b00001000 */ 0x08, /* 5 0b00000100 */ 0x04, |
Azure.IoT Build | 0:fa2de1b79154 | 194 | /* 6 0b00000010 */ 0x02, /* 7 0b00000001 */ 0x01 |
Azure.IoT Build | 0:fa2de1b79154 | 195 | }; |
Azure.IoT Build | 0:fa2de1b79154 | 196 | |
Azure.IoT Build | 0:fa2de1b79154 | 197 | if (!length) |
Azure.IoT Build | 0:fa2de1b79154 | 198 | return shaSuccess; |
Azure.IoT Build | 0:fa2de1b79154 | 199 | |
Azure.IoT Build | 0:fa2de1b79154 | 200 | if (!context) |
Azure.IoT Build | 0:fa2de1b79154 | 201 | return shaNull; |
Azure.IoT Build | 0:fa2de1b79154 | 202 | |
Azure.IoT Build | 0:fa2de1b79154 | 203 | if (context->Computed || (length >= 8) || (length == 0)) { |
Azure.IoT Build | 0:fa2de1b79154 | 204 | context->Corrupted = shaStateError; |
Azure.IoT Build | 0:fa2de1b79154 | 205 | return shaStateError; |
Azure.IoT Build | 0:fa2de1b79154 | 206 | } |
Azure.IoT Build | 0:fa2de1b79154 | 207 | |
Azure.IoT Build | 0:fa2de1b79154 | 208 | if (context->Corrupted) |
Azure.IoT Build | 0:fa2de1b79154 | 209 | return context->Corrupted; |
Azure.IoT Build | 0:fa2de1b79154 | 210 | |
Azure.IoT Build | 0:fa2de1b79154 | 211 | SHA1AddLength(context, length); |
Azure.IoT Build | 0:fa2de1b79154 | 212 | SHA1Finalize(context, |
Azure.IoT Build | 0:fa2de1b79154 | 213 | (uint8_t)((message_bits & masks[length]) | markbit[length])); |
Azure.IoT Build | 0:fa2de1b79154 | 214 | |
Azure.IoT Build | 0:fa2de1b79154 | 215 | return shaSuccess; |
Azure.IoT Build | 0:fa2de1b79154 | 216 | } |
Azure.IoT Build | 0:fa2de1b79154 | 217 | |
Azure.IoT Build | 0:fa2de1b79154 | 218 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 219 | * SHA1Result |
Azure.IoT Build | 0:fa2de1b79154 | 220 | * |
Azure.IoT Build | 0:fa2de1b79154 | 221 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 222 | * This function will return the 160-bit message digest into the |
Azure.IoT Build | 0:fa2de1b79154 | 223 | * Message_Digest array provided by the caller. |
Azure.IoT Build | 0:fa2de1b79154 | 224 | * NOTE: The first octet of hash is stored in the 0th element, |
Azure.IoT Build | 0:fa2de1b79154 | 225 | * the last octet of hash in the 19th element. |
Azure.IoT Build | 0:fa2de1b79154 | 226 | * |
Azure.IoT Build | 0:fa2de1b79154 | 227 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 228 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 229 | * The context to use to calculate the SHA-1 hash. |
Azure.IoT Build | 0:fa2de1b79154 | 230 | * Message_Digest: [out] |
Azure.IoT Build | 0:fa2de1b79154 | 231 | * Where the digest is returned. |
Azure.IoT Build | 0:fa2de1b79154 | 232 | * |
Azure.IoT Build | 0:fa2de1b79154 | 233 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 234 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 235 | * |
Azure.IoT Build | 0:fa2de1b79154 | 236 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 237 | int SHA1Result(SHA1Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 238 | uint8_t Message_Digest[SHA1HashSize]) |
Azure.IoT Build | 0:fa2de1b79154 | 239 | { |
Azure.IoT Build | 0:fa2de1b79154 | 240 | int i; |
Azure.IoT Build | 0:fa2de1b79154 | 241 | |
Azure.IoT Build | 0:fa2de1b79154 | 242 | if (!context || !Message_Digest) |
Azure.IoT Build | 0:fa2de1b79154 | 243 | return shaNull; |
Azure.IoT Build | 0:fa2de1b79154 | 244 | |
Azure.IoT Build | 0:fa2de1b79154 | 245 | if (context->Corrupted) |
Azure.IoT Build | 0:fa2de1b79154 | 246 | return context->Corrupted; |
Azure.IoT Build | 0:fa2de1b79154 | 247 | |
Azure.IoT Build | 0:fa2de1b79154 | 248 | if (!context->Computed) |
Azure.IoT Build | 0:fa2de1b79154 | 249 | SHA1Finalize(context, 0x80); |
Azure.IoT Build | 0:fa2de1b79154 | 250 | |
Azure.IoT Build | 0:fa2de1b79154 | 251 | for (i = 0; i < SHA1HashSize; ++i) |
Azure.IoT Build | 0:fa2de1b79154 | 252 | Message_Digest[i] = (uint8_t)(context->Intermediate_Hash[i >> 2] |
Azure.IoT Build | 0:fa2de1b79154 | 253 | >> 8 * (3 - (i & 0x03))); |
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 | * SHA1Finalize |
Azure.IoT Build | 0:fa2de1b79154 | 260 | * |
Azure.IoT Build | 0:fa2de1b79154 | 261 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 262 | * This helper function finishes off the digest calculations. |
Azure.IoT Build | 0:fa2de1b79154 | 263 | * |
Azure.IoT Build | 0:fa2de1b79154 | 264 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 265 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 266 | * The SHA context to update |
Azure.IoT Build | 0:fa2de1b79154 | 267 | * Pad_Byte: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 268 | * The last byte to add to the digest before the 0-padding |
Azure.IoT Build | 0:fa2de1b79154 | 269 | * and length. This will contain the last bits of the message |
Azure.IoT Build | 0:fa2de1b79154 | 270 | * followed by another single bit. If the message was an |
Azure.IoT Build | 0:fa2de1b79154 | 271 | * exact multiple of 8-bits long, Pad_Byte will be 0x80. |
Azure.IoT Build | 0:fa2de1b79154 | 272 | * |
Azure.IoT Build | 0:fa2de1b79154 | 273 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 274 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 275 | * |
Azure.IoT Build | 0:fa2de1b79154 | 276 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 277 | static void SHA1Finalize(SHA1Context *context, uint8_t Pad_Byte) |
Azure.IoT Build | 0:fa2de1b79154 | 278 | { |
Azure.IoT Build | 0:fa2de1b79154 | 279 | int i; |
Azure.IoT Build | 0:fa2de1b79154 | 280 | SHA1PadMessage(context, Pad_Byte); |
Azure.IoT Build | 0:fa2de1b79154 | 281 | /* message may be sensitive, clear it out */ |
Azure.IoT Build | 0:fa2de1b79154 | 282 | for (i = 0; i < SHA1_Message_Block_Size; ++i) |
Azure.IoT Build | 0:fa2de1b79154 | 283 | context->Message_Block[i] = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 284 | context->Length_Low = 0; /* and clear length */ |
Azure.IoT Build | 0:fa2de1b79154 | 285 | context->Length_High = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 286 | context->Computed = 1; |
Azure.IoT Build | 0:fa2de1b79154 | 287 | } |
Azure.IoT Build | 0:fa2de1b79154 | 288 | |
Azure.IoT Build | 0:fa2de1b79154 | 289 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 290 | * SHA1PadMessage |
Azure.IoT Build | 0:fa2de1b79154 | 291 | * |
Azure.IoT Build | 0:fa2de1b79154 | 292 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 293 | * According to the standard, the message must be padded to an |
Azure.IoT Build | 0:fa2de1b79154 | 294 | * even 512 bits. The first padding bit must be a '1'. The last |
Azure.IoT Build | 0:fa2de1b79154 | 295 | * 64 bits represent the length of the original message. All bits |
Azure.IoT Build | 0:fa2de1b79154 | 296 | * in between should be 0. This helper function will pad the |
Azure.IoT Build | 0:fa2de1b79154 | 297 | * message according to those rules by filling the Message_Block |
Azure.IoT Build | 0:fa2de1b79154 | 298 | * array accordingly. When it returns, it can be assumed that the |
Azure.IoT Build | 0:fa2de1b79154 | 299 | * message digest has been computed. |
Azure.IoT Build | 0:fa2de1b79154 | 300 | * |
Azure.IoT Build | 0:fa2de1b79154 | 301 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 302 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 303 | * The context to pad |
Azure.IoT Build | 0:fa2de1b79154 | 304 | * Pad_Byte: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 305 | * The last byte to add to the digest before the 0-padding |
Azure.IoT Build | 0:fa2de1b79154 | 306 | * and length. This will contain the last bits of the message |
Azure.IoT Build | 0:fa2de1b79154 | 307 | * followed by another single bit. If the message was an |
Azure.IoT Build | 0:fa2de1b79154 | 308 | * exact multiple of 8-bits long, Pad_Byte will be 0x80. |
Azure.IoT Build | 0:fa2de1b79154 | 309 | * |
Azure.IoT Build | 0:fa2de1b79154 | 310 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 311 | * Nothing. |
Azure.IoT Build | 0:fa2de1b79154 | 312 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 313 | static void SHA1PadMessage(SHA1Context *context, uint8_t Pad_Byte) |
Azure.IoT Build | 0:fa2de1b79154 | 314 | { |
Azure.IoT Build | 0:fa2de1b79154 | 315 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 316 | * Check to see if the current message block is too small to hold |
Azure.IoT Build | 0:fa2de1b79154 | 317 | * the initial padding bits and length. If so, we will pad the |
Azure.IoT Build | 0:fa2de1b79154 | 318 | * block, process it, and then continue padding into a second |
Azure.IoT Build | 0:fa2de1b79154 | 319 | * block. |
Azure.IoT Build | 0:fa2de1b79154 | 320 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 321 | if (context->Message_Block_Index >= (SHA1_Message_Block_Size - 8)) { |
Azure.IoT Build | 0:fa2de1b79154 | 322 | context->Message_Block[context->Message_Block_Index++] = Pad_Byte; |
Azure.IoT Build | 0:fa2de1b79154 | 323 | while (context->Message_Block_Index < SHA1_Message_Block_Size) |
Azure.IoT Build | 0:fa2de1b79154 | 324 | context->Message_Block[context->Message_Block_Index++] = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 325 | |
Azure.IoT Build | 0:fa2de1b79154 | 326 | SHA1ProcessMessageBlock(context); |
Azure.IoT Build | 0:fa2de1b79154 | 327 | } |
Azure.IoT Build | 0:fa2de1b79154 | 328 | else |
Azure.IoT Build | 0:fa2de1b79154 | 329 | context->Message_Block[context->Message_Block_Index++] = Pad_Byte; |
Azure.IoT Build | 0:fa2de1b79154 | 330 | |
Azure.IoT Build | 0:fa2de1b79154 | 331 | while (context->Message_Block_Index < (SHA1_Message_Block_Size - 8)) |
Azure.IoT Build | 0:fa2de1b79154 | 332 | context->Message_Block[context->Message_Block_Index++] = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 333 | |
Azure.IoT Build | 0:fa2de1b79154 | 334 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 335 | * Store the message length as the last 8 octets |
Azure.IoT Build | 0:fa2de1b79154 | 336 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 337 | context->Message_Block[56] = (uint8_t)(context->Length_High >> 24); |
Azure.IoT Build | 0:fa2de1b79154 | 338 | context->Message_Block[57] = (uint8_t)(context->Length_High >> 16); |
Azure.IoT Build | 0:fa2de1b79154 | 339 | |
Azure.IoT Build | 0:fa2de1b79154 | 340 | context->Message_Block[58] = (uint8_t)(context->Length_High >> 8); |
Azure.IoT Build | 0:fa2de1b79154 | 341 | context->Message_Block[59] = (uint8_t)(context->Length_High); |
Azure.IoT Build | 0:fa2de1b79154 | 342 | context->Message_Block[60] = (uint8_t)(context->Length_Low >> 24); |
Azure.IoT Build | 0:fa2de1b79154 | 343 | context->Message_Block[61] = (uint8_t)(context->Length_Low >> 16); |
Azure.IoT Build | 0:fa2de1b79154 | 344 | context->Message_Block[62] = (uint8_t)(context->Length_Low >> 8); |
Azure.IoT Build | 0:fa2de1b79154 | 345 | context->Message_Block[63] = (uint8_t)(context->Length_Low); |
Azure.IoT Build | 0:fa2de1b79154 | 346 | |
Azure.IoT Build | 0:fa2de1b79154 | 347 | SHA1ProcessMessageBlock(context); |
Azure.IoT Build | 0:fa2de1b79154 | 348 | } |
Azure.IoT Build | 0:fa2de1b79154 | 349 | |
Azure.IoT Build | 0:fa2de1b79154 | 350 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 351 | * SHA1ProcessMessageBlock |
Azure.IoT Build | 0:fa2de1b79154 | 352 | * |
Azure.IoT Build | 0:fa2de1b79154 | 353 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 354 | * This helper function will process the next 512 bits of the |
Azure.IoT Build | 0:fa2de1b79154 | 355 | * message stored in the Message_Block array. |
Azure.IoT Build | 0:fa2de1b79154 | 356 | * |
Azure.IoT Build | 0:fa2de1b79154 | 357 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 358 | * None. |
Azure.IoT Build | 0:fa2de1b79154 | 359 | * |
Azure.IoT Build | 0:fa2de1b79154 | 360 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 361 | * Nothing. |
Azure.IoT Build | 0:fa2de1b79154 | 362 | * |
Azure.IoT Build | 0:fa2de1b79154 | 363 | * Comments: |
Azure.IoT Build | 0:fa2de1b79154 | 364 | * Many of the variable names in this code, especially the |
Azure.IoT Build | 0:fa2de1b79154 | 365 | * single character names, were used because those were the |
Azure.IoT Build | 0:fa2de1b79154 | 366 | * names used in the publication. |
Azure.IoT Build | 0:fa2de1b79154 | 367 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 368 | static void SHA1ProcessMessageBlock(SHA1Context *context) |
Azure.IoT Build | 0:fa2de1b79154 | 369 | { |
Azure.IoT Build | 0:fa2de1b79154 | 370 | /* Constants defined in FIPS-180-2, section 4.2.1 */ |
Azure.IoT Build | 0:fa2de1b79154 | 371 | const uint32_t K[4] = { |
Azure.IoT Build | 0:fa2de1b79154 | 372 | 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6 |
Azure.IoT Build | 0:fa2de1b79154 | 373 | }; |
Azure.IoT Build | 0:fa2de1b79154 | 374 | int t; /* Loop counter */ |
Azure.IoT Build | 0:fa2de1b79154 | 375 | uint32_t temp; /* Temporary word value */ |
Azure.IoT Build | 0:fa2de1b79154 | 376 | uint32_t W[80]; /* Word sequence */ |
Azure.IoT Build | 0:fa2de1b79154 | 377 | uint32_t A, B, C, D, E; /* Word buffers */ |
Azure.IoT Build | 0:fa2de1b79154 | 378 | |
Azure.IoT Build | 0:fa2de1b79154 | 379 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 380 | * Initialize the first 16 words in the array W |
Azure.IoT Build | 0:fa2de1b79154 | 381 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 382 | for (t = 0; t < 16; t++) { |
Azure.IoT Build | 0:fa2de1b79154 | 383 | W[t] = ((uint32_t)context->Message_Block[t * 4]) << 24; |
Azure.IoT Build | 0:fa2de1b79154 | 384 | W[t] |= ((uint32_t)context->Message_Block[t * 4 + 1]) << 16; |
Azure.IoT Build | 0:fa2de1b79154 | 385 | W[t] |= ((uint32_t)context->Message_Block[t * 4 + 2]) << 8; |
Azure.IoT Build | 0:fa2de1b79154 | 386 | W[t] |= ((uint32_t)context->Message_Block[t * 4 + 3]); |
Azure.IoT Build | 0:fa2de1b79154 | 387 | } |
Azure.IoT Build | 0:fa2de1b79154 | 388 | |
Azure.IoT Build | 0:fa2de1b79154 | 389 | for (t = 16; t < 80; t++) |
Azure.IoT Build | 0:fa2de1b79154 | 390 | W[t] = SHA1_ROTL(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]); |
Azure.IoT Build | 0:fa2de1b79154 | 391 | |
Azure.IoT Build | 0:fa2de1b79154 | 392 | A = context->Intermediate_Hash[0]; |
Azure.IoT Build | 0:fa2de1b79154 | 393 | B = context->Intermediate_Hash[1]; |
Azure.IoT Build | 0:fa2de1b79154 | 394 | C = context->Intermediate_Hash[2]; |
Azure.IoT Build | 0:fa2de1b79154 | 395 | D = context->Intermediate_Hash[3]; |
Azure.IoT Build | 0:fa2de1b79154 | 396 | E = context->Intermediate_Hash[4]; |
Azure.IoT Build | 0:fa2de1b79154 | 397 | |
Azure.IoT Build | 0:fa2de1b79154 | 398 | for (t = 0; t < 20; t++) { |
Azure.IoT Build | 0:fa2de1b79154 | 399 | temp = SHA1_ROTL(5, A) + SHA_Ch(B, C, D) + E + W[t] + K[0]; |
Azure.IoT Build | 0:fa2de1b79154 | 400 | E = D; |
Azure.IoT Build | 0:fa2de1b79154 | 401 | D = C; |
Azure.IoT Build | 0:fa2de1b79154 | 402 | C = SHA1_ROTL(30, B); |
Azure.IoT Build | 0:fa2de1b79154 | 403 | B = A; |
Azure.IoT Build | 0:fa2de1b79154 | 404 | A = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 405 | } |
Azure.IoT Build | 0:fa2de1b79154 | 406 | |
Azure.IoT Build | 0:fa2de1b79154 | 407 | for (t = 20; t < 40; t++) { |
Azure.IoT Build | 0:fa2de1b79154 | 408 | temp = SHA1_ROTL(5, A) + SHA_Parity(B, C, D) + E + W[t] + K[1]; |
Azure.IoT Build | 0:fa2de1b79154 | 409 | E = D; |
Azure.IoT Build | 0:fa2de1b79154 | 410 | D = C; |
Azure.IoT Build | 0:fa2de1b79154 | 411 | C = SHA1_ROTL(30, B); |
Azure.IoT Build | 0:fa2de1b79154 | 412 | B = A; |
Azure.IoT Build | 0:fa2de1b79154 | 413 | A = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 414 | } |
Azure.IoT Build | 0:fa2de1b79154 | 415 | |
Azure.IoT Build | 0:fa2de1b79154 | 416 | for (t = 40; t < 60; t++) { |
Azure.IoT Build | 0:fa2de1b79154 | 417 | temp = SHA1_ROTL(5, A) + SHA_Maj(B, C, D) + E + W[t] + K[2]; |
Azure.IoT Build | 0:fa2de1b79154 | 418 | E = D; |
Azure.IoT Build | 0:fa2de1b79154 | 419 | D = C; |
Azure.IoT Build | 0:fa2de1b79154 | 420 | C = SHA1_ROTL(30, B); |
Azure.IoT Build | 0:fa2de1b79154 | 421 | B = A; |
Azure.IoT Build | 0:fa2de1b79154 | 422 | A = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 423 | } |
Azure.IoT Build | 0:fa2de1b79154 | 424 | |
Azure.IoT Build | 0:fa2de1b79154 | 425 | for (t = 60; t < 80; t++) { |
Azure.IoT Build | 0:fa2de1b79154 | 426 | temp = SHA1_ROTL(5, A) + SHA_Parity(B, C, D) + E + W[t] + K[3]; |
Azure.IoT Build | 0:fa2de1b79154 | 427 | E = D; |
Azure.IoT Build | 0:fa2de1b79154 | 428 | D = C; |
Azure.IoT Build | 0:fa2de1b79154 | 429 | C = SHA1_ROTL(30, B); |
Azure.IoT Build | 0:fa2de1b79154 | 430 | B = A; |
Azure.IoT Build | 0:fa2de1b79154 | 431 | A = temp; |
Azure.IoT Build | 0:fa2de1b79154 | 432 | } |
Azure.IoT Build | 0:fa2de1b79154 | 433 | |
Azure.IoT Build | 0:fa2de1b79154 | 434 | context->Intermediate_Hash[0] += A; |
Azure.IoT Build | 0:fa2de1b79154 | 435 | context->Intermediate_Hash[1] += B; |
Azure.IoT Build | 0:fa2de1b79154 | 436 | context->Intermediate_Hash[2] += C; |
Azure.IoT Build | 0:fa2de1b79154 | 437 | |
Azure.IoT Build | 0:fa2de1b79154 | 438 | context->Intermediate_Hash[3] += D; |
Azure.IoT Build | 0:fa2de1b79154 | 439 | context->Intermediate_Hash[4] += E; |
Azure.IoT Build | 0:fa2de1b79154 | 440 | |
Azure.IoT Build | 0:fa2de1b79154 | 441 | context->Message_Block_Index = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 442 | } |
Azure.IoT Build | 0:fa2de1b79154 | 443 |