Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Committer:
AzureIoTClient
Date:
Sat Jan 28 09:35:22 2017 -0800
Revision:
19:2e0811512ceb
Parent:
15:956c6d205aa7
1.1.6

Who changed what in which revision?

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