Azure IoT common library
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
sha384-512.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 | /*************************** sha384-512.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-384 and SHA-512 algorithms produce 384-bit and 512-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-384 and SHA-512 are defined in terms of 64-bit "words", |
Azure.IoT Build | 0:fa2de1b79154 | 29 | * but if USE_32BIT_ONLY is #defined, this code is implemented in |
Azure.IoT Build | 0:fa2de1b79154 | 30 | * terms of 32-bit "words". This code uses <stdint.h> (included |
Azure.IoT Build | 0:fa2de1b79154 | 31 | * via "sha.h") to define the 64, 32 and 8 bit unsigned integer |
Azure.IoT Build | 0:fa2de1b79154 | 32 | * types. If your C compiler does not support 64 bit unsigned |
Azure.IoT Build | 0:fa2de1b79154 | 33 | * integers, and you do not #define USE_32BIT_ONLY, this code is |
Azure.IoT Build | 0:fa2de1b79154 | 34 | * not appropriate. |
Azure.IoT Build | 0:fa2de1b79154 | 35 | * |
Azure.IoT Build | 0:fa2de1b79154 | 36 | * Caveats: |
Azure.IoT Build | 0:fa2de1b79154 | 37 | * SHA-384 and SHA-512 are designed to work with messages less |
Azure.IoT Build | 0:fa2de1b79154 | 38 | * than 2^128 bits long. This implementation uses |
Azure.IoT Build | 0:fa2de1b79154 | 39 | * SHA384/512Input() to hash the bits that are a multiple of the |
Azure.IoT Build | 0:fa2de1b79154 | 40 | * size of an 8-bit character, and then uses SHA384/256FinalBits() |
Azure.IoT Build | 0:fa2de1b79154 | 41 | * to hash the final few bits of the input. |
Azure.IoT Build | 0:fa2de1b79154 | 42 | * |
Azure.IoT Build | 0:fa2de1b79154 | 43 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 44 | |
Azure.IoT Build | 0:fa2de1b79154 | 45 | #include <stdlib.h> |
Azure.IoT Build | 0:fa2de1b79154 | 46 | #ifdef _CRTDBG_MAP_ALLOC |
Azure.IoT Build | 0:fa2de1b79154 | 47 | #include <crtdbg.h> |
Azure.IoT Build | 0:fa2de1b79154 | 48 | #endif |
Azure.IoT Build | 0:fa2de1b79154 | 49 | #include "azure_c_shared_utility/gballoc.h" |
Azure.IoT Build | 0:fa2de1b79154 | 50 | |
Azure.IoT Build | 0:fa2de1b79154 | 51 | #include "azure_c_shared_utility/sha.h" |
Azure.IoT Build | 0:fa2de1b79154 | 52 | #include "azure_c_shared_utility/sha-private.h" |
Azure.IoT Build | 0:fa2de1b79154 | 53 | |
Azure.IoT Build | 0:fa2de1b79154 | 54 | #ifdef USE_32BIT_ONLY |
AzureIoTClient | 15:956c6d205aa7 | 55 | #error IoTHubClient does not support USE_32BIT_ONLY flag |
Azure.IoT Build | 0:fa2de1b79154 | 56 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 57 | * Define 64-bit arithmetic in terms of 32-bit arithmetic. |
Azure.IoT Build | 0:fa2de1b79154 | 58 | * Each 64-bit number is represented in a 2-word array. |
Azure.IoT Build | 0:fa2de1b79154 | 59 | * All macros are defined such that the result is the last parameter. |
Azure.IoT Build | 0:fa2de1b79154 | 60 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 61 | |
Azure.IoT Build | 0:fa2de1b79154 | 62 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 63 | * Define shift, rotate left and rotate right functions |
Azure.IoT Build | 0:fa2de1b79154 | 64 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 65 | #define SHA512_SHR(bits, word, ret) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 66 | /* (((uint64_t)((word))) >> (bits)) */ \ |
Azure.IoT Build | 0:fa2de1b79154 | 67 | (ret)[0] = (((bits) < 32) && ((bits) >= 0)) ? \ |
Azure.IoT Build | 0:fa2de1b79154 | 68 | ((word)[0] >> (bits)) : 0, \ |
Azure.IoT Build | 0:fa2de1b79154 | 69 | (ret)[1] = ((bits) > 32) ? ((word)[0] >> ((bits) - 32)) : \ |
Azure.IoT Build | 0:fa2de1b79154 | 70 | ((bits) == 32) ? (word)[0] : \ |
Azure.IoT Build | 0:fa2de1b79154 | 71 | ((bits) >= 0) ? \ |
Azure.IoT Build | 0:fa2de1b79154 | 72 | (((word)[0] << (32 - (bits))) | \ |
Azure.IoT Build | 0:fa2de1b79154 | 73 | ((word)[1] >> (bits))) : 0 ) |
Azure.IoT Build | 0:fa2de1b79154 | 74 | |
Azure.IoT Build | 0:fa2de1b79154 | 75 | #define SHA512_SHL(bits, word, ret) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 76 | /* (((uint64_t)(word)) << (bits)) */ \ |
Azure.IoT Build | 0:fa2de1b79154 | 77 | (ret)[0] = ((bits) > 32) ? ((word)[1] << ((bits) - 32)) : \ |
Azure.IoT Build | 0:fa2de1b79154 | 78 | ((bits) == 32) ? (word)[1] : \ |
Azure.IoT Build | 0:fa2de1b79154 | 79 | ((bits) >= 0) ? \ |
Azure.IoT Build | 0:fa2de1b79154 | 80 | (((word)[0] << (bits)) | \ |
Azure.IoT Build | 0:fa2de1b79154 | 81 | ((word)[1] >> (32 - (bits)))) : \ |
Azure.IoT Build | 0:fa2de1b79154 | 82 | 0, \ |
Azure.IoT Build | 0:fa2de1b79154 | 83 | (ret)[1] = (((bits) < 32) && ((bits) >= 0)) ? \ |
Azure.IoT Build | 0:fa2de1b79154 | 84 | ((word)[1] << (bits)) : 0) |
Azure.IoT Build | 0:fa2de1b79154 | 85 | |
Azure.IoT Build | 0:fa2de1b79154 | 86 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 87 | * Define 64-bit OR |
Azure.IoT Build | 0:fa2de1b79154 | 88 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 89 | #define SHA512_OR(word1, word2, ret) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 90 | (ret)[0] = (word1)[0] | (word2)[0], \ |
Azure.IoT Build | 0:fa2de1b79154 | 91 | (ret)[1] = (word1)[1] | (word2)[1] ) |
Azure.IoT Build | 0:fa2de1b79154 | 92 | |
Azure.IoT Build | 0:fa2de1b79154 | 93 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 94 | * Define 64-bit XOR |
Azure.IoT Build | 0:fa2de1b79154 | 95 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 96 | #define SHA512_XOR(word1, word2, ret) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 97 | (ret)[0] = (word1)[0] ^ (word2)[0], \ |
Azure.IoT Build | 0:fa2de1b79154 | 98 | (ret)[1] = (word1)[1] ^ (word2)[1] ) |
Azure.IoT Build | 0:fa2de1b79154 | 99 | |
Azure.IoT Build | 0:fa2de1b79154 | 100 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 101 | * Define 64-bit AND |
Azure.IoT Build | 0:fa2de1b79154 | 102 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 103 | #define SHA512_AND(word1, word2, ret) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 104 | (ret)[0] = (word1)[0] & (word2)[0], \ |
Azure.IoT Build | 0:fa2de1b79154 | 105 | (ret)[1] = (word1)[1] & (word2)[1] ) |
Azure.IoT Build | 0:fa2de1b79154 | 106 | |
Azure.IoT Build | 0:fa2de1b79154 | 107 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 108 | * Define 64-bit TILDA |
Azure.IoT Build | 0:fa2de1b79154 | 109 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 110 | #define SHA512_TILDA(word, ret) \ |
Azure.IoT Build | 0:fa2de1b79154 | 111 | ( (ret)[0] = ~(word)[0], (ret)[1] = ~(word)[1] ) |
Azure.IoT Build | 0:fa2de1b79154 | 112 | |
Azure.IoT Build | 0:fa2de1b79154 | 113 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 114 | * Define 64-bit ADD |
Azure.IoT Build | 0:fa2de1b79154 | 115 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 116 | #define SHA512_ADD(word1, word2, ret) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 117 | (ret)[1] = (word1)[1], (ret)[1] += (word2)[1], \ |
Azure.IoT Build | 0:fa2de1b79154 | 118 | (ret)[0] = (word1)[0] + (word2)[0] + ((ret)[1] < (word1)[1]) ) |
Azure.IoT Build | 0:fa2de1b79154 | 119 | |
Azure.IoT Build | 0:fa2de1b79154 | 120 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 121 | * Add the 4word value in word2 to word1. |
Azure.IoT Build | 0:fa2de1b79154 | 122 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 123 | static uint32_t ADDTO4_temp, ADDTO4_temp2; |
Azure.IoT Build | 0:fa2de1b79154 | 124 | #define SHA512_ADDTO4(word1, word2) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 125 | ADDTO4_temp = (word1)[3], \ |
Azure.IoT Build | 0:fa2de1b79154 | 126 | (word1)[3] += (word2)[3], \ |
Azure.IoT Build | 0:fa2de1b79154 | 127 | ADDTO4_temp2 = (word1)[2], \ |
Azure.IoT Build | 0:fa2de1b79154 | 128 | (word1)[2] += (word2)[2] + ((word1)[3] < ADDTO4_temp), \ |
Azure.IoT Build | 0:fa2de1b79154 | 129 | ADDTO4_temp = (word1)[1], \ |
Azure.IoT Build | 0:fa2de1b79154 | 130 | (word1)[1] += (word2)[1] + ((word1)[2] < ADDTO4_temp2), \ |
Azure.IoT Build | 0:fa2de1b79154 | 131 | (word1)[0] += (word2)[0] + ((word1)[1] < ADDTO4_temp)) |
Azure.IoT Build | 0:fa2de1b79154 | 132 | |
Azure.IoT Build | 0:fa2de1b79154 | 133 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 134 | * Add the 2word value in word2 to word1. |
Azure.IoT Build | 0:fa2de1b79154 | 135 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 136 | static uint32_t ADDTO2_temp; |
Azure.IoT Build | 0:fa2de1b79154 | 137 | #define SHA512_ADDTO2(word1, word2) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 138 | ADDTO2_temp = (word1)[1], \ |
Azure.IoT Build | 0:fa2de1b79154 | 139 | (word1)[1] += (word2)[1], \ |
Azure.IoT Build | 0:fa2de1b79154 | 140 | (word1)[0] += (word2)[0] + ((word1)[1] < ADDTO2_temp) ) |
Azure.IoT Build | 0:fa2de1b79154 | 141 | |
Azure.IoT Build | 0:fa2de1b79154 | 142 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 143 | * SHA rotate ((word >> bits) | (word << (64-bits))) |
Azure.IoT Build | 0:fa2de1b79154 | 144 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 145 | static uint32_t ROTR_temp1[2], ROTR_temp2[2]; |
Azure.IoT Build | 0:fa2de1b79154 | 146 | #define SHA512_ROTR(bits, word, ret) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 147 | SHA512_SHR((bits), (word), ROTR_temp1), \ |
Azure.IoT Build | 0:fa2de1b79154 | 148 | SHA512_SHL(64-(bits), (word), ROTR_temp2), \ |
Azure.IoT Build | 0:fa2de1b79154 | 149 | SHA512_OR(ROTR_temp1, ROTR_temp2, (ret)) ) |
Azure.IoT Build | 0:fa2de1b79154 | 150 | |
Azure.IoT Build | 0:fa2de1b79154 | 151 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 152 | * Define the SHA SIGMA and sigma macros |
Azure.IoT Build | 0:fa2de1b79154 | 153 | * SHA512_ROTR(28,word) ^ SHA512_ROTR(34,word) ^ SHA512_ROTR(39,word) |
Azure.IoT Build | 0:fa2de1b79154 | 154 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 155 | static uint32_t SIGMA0_temp1[2], SIGMA0_temp2[2], |
Azure.IoT Build | 0:fa2de1b79154 | 156 | SIGMA0_temp3[2], SIGMA0_temp4[2]; |
Azure.IoT Build | 0:fa2de1b79154 | 157 | #define SHA512_SIGMA0(word, ret) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 158 | SHA512_ROTR(28, (word), SIGMA0_temp1), \ |
Azure.IoT Build | 0:fa2de1b79154 | 159 | SHA512_ROTR(34, (word), SIGMA0_temp2), \ |
Azure.IoT Build | 0:fa2de1b79154 | 160 | SHA512_ROTR(39, (word), SIGMA0_temp3), \ |
Azure.IoT Build | 0:fa2de1b79154 | 161 | SHA512_XOR(SIGMA0_temp2, SIGMA0_temp3, SIGMA0_temp4), \ |
Azure.IoT Build | 0:fa2de1b79154 | 162 | SHA512_XOR(SIGMA0_temp1, SIGMA0_temp4, (ret)) ) |
Azure.IoT Build | 0:fa2de1b79154 | 163 | |
Azure.IoT Build | 0:fa2de1b79154 | 164 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 165 | * SHA512_ROTR(14,word) ^ SHA512_ROTR(18,word) ^ SHA512_ROTR(41,word) |
Azure.IoT Build | 0:fa2de1b79154 | 166 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 167 | static uint32_t SIGMA1_temp1[2], SIGMA1_temp2[2], |
Azure.IoT Build | 0:fa2de1b79154 | 168 | SIGMA1_temp3[2], SIGMA1_temp4[2]; |
Azure.IoT Build | 0:fa2de1b79154 | 169 | #define SHA512_SIGMA1(word, ret) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 170 | SHA512_ROTR(14, (word), SIGMA1_temp1), \ |
Azure.IoT Build | 0:fa2de1b79154 | 171 | SHA512_ROTR(18, (word), SIGMA1_temp2), \ |
Azure.IoT Build | 0:fa2de1b79154 | 172 | SHA512_ROTR(41, (word), SIGMA1_temp3), \ |
Azure.IoT Build | 0:fa2de1b79154 | 173 | SHA512_XOR(SIGMA1_temp2, SIGMA1_temp3, SIGMA1_temp4), \ |
Azure.IoT Build | 0:fa2de1b79154 | 174 | SHA512_XOR(SIGMA1_temp1, SIGMA1_temp4, (ret)) ) |
Azure.IoT Build | 0:fa2de1b79154 | 175 | |
Azure.IoT Build | 0:fa2de1b79154 | 176 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 177 | * (SHA512_ROTR( 1,word) ^ SHA512_ROTR( 8,word) ^ SHA512_SHR( 7,word)) |
Azure.IoT Build | 0:fa2de1b79154 | 178 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 179 | static uint32_t sigma0_temp1[2], sigma0_temp2[2], |
Azure.IoT Build | 0:fa2de1b79154 | 180 | sigma0_temp3[2], sigma0_temp4[2]; |
Azure.IoT Build | 0:fa2de1b79154 | 181 | #define SHA512_sigma0(word, ret) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 182 | SHA512_ROTR( 1, (word), sigma0_temp1), \ |
Azure.IoT Build | 0:fa2de1b79154 | 183 | SHA512_ROTR( 8, (word), sigma0_temp2), \ |
Azure.IoT Build | 0:fa2de1b79154 | 184 | SHA512_SHR( 7, (word), sigma0_temp3), \ |
Azure.IoT Build | 0:fa2de1b79154 | 185 | SHA512_XOR(sigma0_temp2, sigma0_temp3, sigma0_temp4), \ |
Azure.IoT Build | 0:fa2de1b79154 | 186 | SHA512_XOR(sigma0_temp1, sigma0_temp4, (ret)) ) |
Azure.IoT Build | 0:fa2de1b79154 | 187 | |
Azure.IoT Build | 0:fa2de1b79154 | 188 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 189 | * (SHA512_ROTR(19,word) ^ SHA512_ROTR(61,word) ^ SHA512_SHR( 6,word)) |
Azure.IoT Build | 0:fa2de1b79154 | 190 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 191 | static uint32_t sigma1_temp1[2], sigma1_temp2[2], |
Azure.IoT Build | 0:fa2de1b79154 | 192 | sigma1_temp3[2], sigma1_temp4[2]; |
Azure.IoT Build | 0:fa2de1b79154 | 193 | #define SHA512_sigma1(word, ret) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 194 | SHA512_ROTR(19, (word), sigma1_temp1), \ |
Azure.IoT Build | 0:fa2de1b79154 | 195 | SHA512_ROTR(61, (word), sigma1_temp2), \ |
Azure.IoT Build | 0:fa2de1b79154 | 196 | SHA512_SHR( 6, (word), sigma1_temp3), \ |
Azure.IoT Build | 0:fa2de1b79154 | 197 | SHA512_XOR(sigma1_temp2, sigma1_temp3, sigma1_temp4), \ |
Azure.IoT Build | 0:fa2de1b79154 | 198 | SHA512_XOR(sigma1_temp1, sigma1_temp4, (ret)) ) |
Azure.IoT Build | 0:fa2de1b79154 | 199 | |
Azure.IoT Build | 0:fa2de1b79154 | 200 | #undef SHA_Ch |
Azure.IoT Build | 0:fa2de1b79154 | 201 | #undef SHA_Maj |
Azure.IoT Build | 0:fa2de1b79154 | 202 | |
Azure.IoT Build | 0:fa2de1b79154 | 203 | #ifndef USE_MODIFIED_MACROS |
Azure.IoT Build | 0:fa2de1b79154 | 204 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 205 | * These definitions are the ones used in FIPS-180-2, section 4.1.3 |
Azure.IoT Build | 0:fa2de1b79154 | 206 | * Ch(x,y,z) ((x & y) ^ (~x & z)) |
Azure.IoT Build | 0:fa2de1b79154 | 207 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 208 | static uint32_t Ch_temp1[2], Ch_temp2[2], Ch_temp3[2]; |
Azure.IoT Build | 0:fa2de1b79154 | 209 | #define SHA_Ch(x, y, z, ret) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 210 | SHA512_AND(x, y, Ch_temp1), \ |
Azure.IoT Build | 0:fa2de1b79154 | 211 | SHA512_TILDA(x, Ch_temp2), \ |
Azure.IoT Build | 0:fa2de1b79154 | 212 | SHA512_AND(Ch_temp2, z, Ch_temp3), \ |
Azure.IoT Build | 0:fa2de1b79154 | 213 | SHA512_XOR(Ch_temp1, Ch_temp3, (ret)) ) |
Azure.IoT Build | 0:fa2de1b79154 | 214 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 215 | * Maj(x,y,z) (((x)&(y)) ^ ((x)&(z)) ^ ((y)&(z))) |
Azure.IoT Build | 0:fa2de1b79154 | 216 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 217 | static uint32_t Maj_temp1[2], Maj_temp2[2], |
Azure.IoT Build | 0:fa2de1b79154 | 218 | Maj_temp3[2], Maj_temp4[2]; |
Azure.IoT Build | 0:fa2de1b79154 | 219 | #define SHA_Maj(x, y, z, ret) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 220 | SHA512_AND(x, y, Maj_temp1), \ |
Azure.IoT Build | 0:fa2de1b79154 | 221 | SHA512_AND(x, z, Maj_temp2), \ |
Azure.IoT Build | 0:fa2de1b79154 | 222 | SHA512_AND(y, z, Maj_temp3), \ |
Azure.IoT Build | 0:fa2de1b79154 | 223 | SHA512_XOR(Maj_temp2, Maj_temp3, Maj_temp4), \ |
Azure.IoT Build | 0:fa2de1b79154 | 224 | SHA512_XOR(Maj_temp1, Maj_temp4, (ret)) ) |
Azure.IoT Build | 0:fa2de1b79154 | 225 | |
Azure.IoT Build | 0:fa2de1b79154 | 226 | #else /* !USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 227 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 228 | * These definitions are potentially faster equivalents for the ones |
Azure.IoT Build | 0:fa2de1b79154 | 229 | * used in FIPS-180-2, section 4.1.3. |
Azure.IoT Build | 0:fa2de1b79154 | 230 | * ((x & y) ^ (~x & z)) becomes |
Azure.IoT Build | 0:fa2de1b79154 | 231 | * ((x & (y ^ z)) ^ z) |
Azure.IoT Build | 0:fa2de1b79154 | 232 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 233 | #define SHA_Ch(x, y, z, ret) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 234 | (ret)[0] = (((x)[0] & ((y)[0] ^ (z)[0])) ^ (z)[0]), \ |
Azure.IoT Build | 0:fa2de1b79154 | 235 | (ret)[1] = (((x)[1] & ((y)[1] ^ (z)[1])) ^ (z)[1]) ) |
Azure.IoT Build | 0:fa2de1b79154 | 236 | |
Azure.IoT Build | 0:fa2de1b79154 | 237 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 238 | * ((x & y) ^ (x & z) ^ (y & z)) becomes |
Azure.IoT Build | 0:fa2de1b79154 | 239 | * ((x & (y | z)) | (y & z)) |
Azure.IoT Build | 0:fa2de1b79154 | 240 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 241 | #define SHA_Maj(x, y, z, ret) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 242 | ret[0] = (((x)[0] & ((y)[0] | (z)[0])) | ((y)[0] & (z)[0])), \ |
Azure.IoT Build | 0:fa2de1b79154 | 243 | ret[1] = (((x)[1] & ((y)[1] | (z)[1])) | ((y)[1] & (z)[1])) ) |
Azure.IoT Build | 0:fa2de1b79154 | 244 | #endif /* USE_MODIFIED_MACROS */ |
Azure.IoT Build | 0:fa2de1b79154 | 245 | |
Azure.IoT Build | 0:fa2de1b79154 | 246 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 247 | * add "length" to the length |
Azure.IoT Build | 0:fa2de1b79154 | 248 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 249 | static uint32_t addTemp[4] = { 0, 0, 0, 0 }; |
Azure.IoT Build | 0:fa2de1b79154 | 250 | #define SHA384_512AddLength(context, length) ( \ |
Azure.IoT Build | 0:fa2de1b79154 | 251 | addTemp[3] = (length), SHA512_ADDTO4((context)->Length, addTemp), \ |
Azure.IoT Build | 0:fa2de1b79154 | 252 | (context)->Corrupted = (((context)->Length[3] == 0) && \ |
Azure.IoT Build | 0:fa2de1b79154 | 253 | ((context)->Length[2] == 0) && ((context)->Length[1] == 0) && \ |
Azure.IoT Build | 0:fa2de1b79154 | 254 | ((context)->Length[0] < 8)) ? 1 : 0 ) |
Azure.IoT Build | 0:fa2de1b79154 | 255 | |
Azure.IoT Build | 0:fa2de1b79154 | 256 | /* Local Function Prototypes */ |
Azure.IoT Build | 0:fa2de1b79154 | 257 | static void SHA384_512Finalize(SHA512Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 258 | uint8_t Pad_Byte); |
Azure.IoT Build | 0:fa2de1b79154 | 259 | static void SHA384_512PadMessage(SHA512Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 260 | uint8_t Pad_Byte); |
Azure.IoT Build | 0:fa2de1b79154 | 261 | static void SHA384_512ProcessMessageBlock(SHA512Context *context); |
Azure.IoT Build | 0:fa2de1b79154 | 262 | static int SHA384_512Reset(SHA512Context *context, uint32_t H0[]); |
Azure.IoT Build | 0:fa2de1b79154 | 263 | static int SHA384_512ResultN(SHA512Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 264 | uint8_t Message_Digest[], int HashSize); |
Azure.IoT Build | 0:fa2de1b79154 | 265 | |
Azure.IoT Build | 0:fa2de1b79154 | 266 | /* Initial Hash Values: FIPS-180-2 sections 5.3.3 and 5.3.4 */ |
Azure.IoT Build | 0:fa2de1b79154 | 267 | static uint32_t SHA384_H0[SHA512HashSize / 4] = { |
Azure.IoT Build | 0:fa2de1b79154 | 268 | 0xCBBB9D5D, 0xC1059ED8, 0x629A292A, 0x367CD507, 0x9159015A, |
Azure.IoT Build | 0:fa2de1b79154 | 269 | 0x3070DD17, 0x152FECD8, 0xF70E5939, 0x67332667, 0xFFC00B31, |
Azure.IoT Build | 0:fa2de1b79154 | 270 | 0x8EB44A87, 0x68581511, 0xDB0C2E0D, 0x64F98FA7, 0x47B5481D, |
Azure.IoT Build | 0:fa2de1b79154 | 271 | 0xBEFA4FA4 |
Azure.IoT Build | 0:fa2de1b79154 | 272 | }; |
Azure.IoT Build | 0:fa2de1b79154 | 273 | |
Azure.IoT Build | 0:fa2de1b79154 | 274 | static uint32_t SHA512_H0[SHA512HashSize / 4] = { |
Azure.IoT Build | 0:fa2de1b79154 | 275 | 0x6A09E667, 0xF3BCC908, 0xBB67AE85, 0x84CAA73B, 0x3C6EF372, |
Azure.IoT Build | 0:fa2de1b79154 | 276 | 0xFE94F82B, 0xA54FF53A, 0x5F1D36F1, 0x510E527F, 0xADE682D1, |
Azure.IoT Build | 0:fa2de1b79154 | 277 | 0x9B05688C, 0x2B3E6C1F, 0x1F83D9AB, 0xFB41BD6B, 0x5BE0CD19, |
Azure.IoT Build | 0:fa2de1b79154 | 278 | 0x137E2179 |
Azure.IoT Build | 0:fa2de1b79154 | 279 | }; |
Azure.IoT Build | 0:fa2de1b79154 | 280 | |
Azure.IoT Build | 0:fa2de1b79154 | 281 | #else /* !USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 282 | |
Azure.IoT Build | 0:fa2de1b79154 | 283 | /* Define the SHA shift, rotate left and rotate right macro */ |
Azure.IoT Build | 0:fa2de1b79154 | 284 | #define SHA512_SHR(bits,word) (((uint64_t)(word)) >> (bits)) |
Azure.IoT Build | 0:fa2de1b79154 | 285 | #define SHA512_ROTR(bits,word) ((((uint64_t)(word)) >> (bits)) | \ |
Azure.IoT Build | 0:fa2de1b79154 | 286 | (((uint64_t)(word)) << (64-(bits)))) |
Azure.IoT Build | 0:fa2de1b79154 | 287 | |
Azure.IoT Build | 0:fa2de1b79154 | 288 | /* Define the SHA SIGMA and sigma macros */ |
Azure.IoT Build | 0:fa2de1b79154 | 289 | #define SHA512_SIGMA0(word) \ |
Azure.IoT Build | 0:fa2de1b79154 | 290 | (SHA512_ROTR(28,word) ^ SHA512_ROTR(34,word) ^ SHA512_ROTR(39,word)) |
Azure.IoT Build | 0:fa2de1b79154 | 291 | #define SHA512_SIGMA1(word) \ |
Azure.IoT Build | 0:fa2de1b79154 | 292 | (SHA512_ROTR(14,word) ^ SHA512_ROTR(18,word) ^ SHA512_ROTR(41,word)) |
Azure.IoT Build | 0:fa2de1b79154 | 293 | #define SHA512_sigma0(word) \ |
Azure.IoT Build | 0:fa2de1b79154 | 294 | (SHA512_ROTR( 1,word) ^ SHA512_ROTR( 8,word) ^ SHA512_SHR( 7,word)) |
Azure.IoT Build | 0:fa2de1b79154 | 295 | #define SHA512_sigma1(word) \ |
Azure.IoT Build | 0:fa2de1b79154 | 296 | (SHA512_ROTR(19,word) ^ SHA512_ROTR(61,word) ^ SHA512_SHR( 6,word)) |
Azure.IoT Build | 0:fa2de1b79154 | 297 | |
Azure.IoT Build | 0:fa2de1b79154 | 298 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 299 | * add "length" to the length |
Azure.IoT Build | 0:fa2de1b79154 | 300 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 301 | #define SHA384_512AddLength(context, length) \ |
Azure.IoT Build | 0:fa2de1b79154 | 302 | (addTemp = context->Length_Low, context->Corrupted = \ |
Azure.IoT Build | 0:fa2de1b79154 | 303 | ((context->Length_Low += length) < addTemp) && \ |
Azure.IoT Build | 0:fa2de1b79154 | 304 | (++context->Length_High == 0) ? 1 : 0) |
Azure.IoT Build | 0:fa2de1b79154 | 305 | |
Azure.IoT Build | 0:fa2de1b79154 | 306 | /* Local Function Prototypes */ |
Azure.IoT Build | 0:fa2de1b79154 | 307 | static void SHA384_512Finalize(SHA512Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 308 | uint8_t Pad_Byte); |
Azure.IoT Build | 0:fa2de1b79154 | 309 | static void SHA384_512PadMessage(SHA512Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 310 | uint8_t Pad_Byte); |
Azure.IoT Build | 0:fa2de1b79154 | 311 | static void SHA384_512ProcessMessageBlock(SHA512Context *context); |
Azure.IoT Build | 0:fa2de1b79154 | 312 | static int SHA384_512Reset(SHA512Context *context, uint64_t H0[]); |
Azure.IoT Build | 0:fa2de1b79154 | 313 | static int SHA384_512ResultN(SHA512Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 314 | uint8_t Message_Digest[], int HashSize); |
Azure.IoT Build | 0:fa2de1b79154 | 315 | |
Azure.IoT Build | 0:fa2de1b79154 | 316 | /* Initial Hash Values: FIPS-180-2 sections 5.3.3 and 5.3.4 */ |
Azure.IoT Build | 0:fa2de1b79154 | 317 | static uint64_t SHA384_H0[] = { |
Azure.IoT Build | 0:fa2de1b79154 | 318 | 0xCBBB9D5DC1059ED8ull, 0x629A292A367CD507ull, 0x9159015A3070DD17ull, |
Azure.IoT Build | 0:fa2de1b79154 | 319 | 0x152FECD8F70E5939ull, 0x67332667FFC00B31ull, 0x8EB44A8768581511ull, |
Azure.IoT Build | 0:fa2de1b79154 | 320 | 0xDB0C2E0D64F98FA7ull, 0x47B5481DBEFA4FA4ull |
Azure.IoT Build | 0:fa2de1b79154 | 321 | }; |
Azure.IoT Build | 0:fa2de1b79154 | 322 | static uint64_t SHA512_H0[] = { |
Azure.IoT Build | 0:fa2de1b79154 | 323 | 0x6A09E667F3BCC908ull, 0xBB67AE8584CAA73Bull, 0x3C6EF372FE94F82Bull, |
Azure.IoT Build | 0:fa2de1b79154 | 324 | 0xA54FF53A5F1D36F1ull, 0x510E527FADE682D1ull, 0x9B05688C2B3E6C1Full, |
Azure.IoT Build | 0:fa2de1b79154 | 325 | 0x1F83D9ABFB41BD6Bull, 0x5BE0CD19137E2179ull |
Azure.IoT Build | 0:fa2de1b79154 | 326 | }; |
Azure.IoT Build | 0:fa2de1b79154 | 327 | |
Azure.IoT Build | 0:fa2de1b79154 | 328 | #endif /* USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 329 | |
Azure.IoT Build | 0:fa2de1b79154 | 330 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 331 | * SHA384Reset |
Azure.IoT Build | 0:fa2de1b79154 | 332 | * |
Azure.IoT Build | 0:fa2de1b79154 | 333 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 334 | * This function will initialize the SHA384Context in preparation |
Azure.IoT Build | 0:fa2de1b79154 | 335 | * for computing a new SHA384 message digest. |
Azure.IoT Build | 0:fa2de1b79154 | 336 | * |
Azure.IoT Build | 0:fa2de1b79154 | 337 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 338 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 339 | * The context to reset. |
Azure.IoT Build | 0:fa2de1b79154 | 340 | * |
Azure.IoT Build | 0:fa2de1b79154 | 341 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 342 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 343 | * |
Azure.IoT Build | 0:fa2de1b79154 | 344 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 345 | int SHA384Reset(SHA384Context *context) |
Azure.IoT Build | 0:fa2de1b79154 | 346 | { |
Azure.IoT Build | 0:fa2de1b79154 | 347 | return SHA384_512Reset(context, SHA384_H0); |
Azure.IoT Build | 0:fa2de1b79154 | 348 | } |
Azure.IoT Build | 0:fa2de1b79154 | 349 | |
Azure.IoT Build | 0:fa2de1b79154 | 350 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 351 | * SHA384Input |
Azure.IoT Build | 0:fa2de1b79154 | 352 | * |
Azure.IoT Build | 0:fa2de1b79154 | 353 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 354 | * This function accepts an array of octets as the next portion |
Azure.IoT Build | 0:fa2de1b79154 | 355 | * of the message. |
Azure.IoT Build | 0:fa2de1b79154 | 356 | * |
Azure.IoT Build | 0:fa2de1b79154 | 357 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 358 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 359 | * The SHA context to update |
Azure.IoT Build | 0:fa2de1b79154 | 360 | * message_array: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 361 | * An array of characters representing the next portion of |
Azure.IoT Build | 0:fa2de1b79154 | 362 | * the message. |
Azure.IoT Build | 0:fa2de1b79154 | 363 | * length: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 364 | * The length of the message in message_array |
Azure.IoT Build | 0:fa2de1b79154 | 365 | * |
Azure.IoT Build | 0:fa2de1b79154 | 366 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 367 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 368 | * |
Azure.IoT Build | 0:fa2de1b79154 | 369 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 370 | int SHA384Input(SHA384Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 371 | const uint8_t *message_array, unsigned int length) |
Azure.IoT Build | 0:fa2de1b79154 | 372 | { |
Azure.IoT Build | 0:fa2de1b79154 | 373 | return SHA512Input(context, message_array, length); |
Azure.IoT Build | 0:fa2de1b79154 | 374 | } |
Azure.IoT Build | 0:fa2de1b79154 | 375 | |
Azure.IoT Build | 0:fa2de1b79154 | 376 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 377 | * SHA384FinalBits |
Azure.IoT Build | 0:fa2de1b79154 | 378 | * |
Azure.IoT Build | 0:fa2de1b79154 | 379 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 380 | * This function will add in any final bits of the message. |
Azure.IoT Build | 0:fa2de1b79154 | 381 | * |
Azure.IoT Build | 0:fa2de1b79154 | 382 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 383 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 384 | * The SHA context to update |
Azure.IoT Build | 0:fa2de1b79154 | 385 | * message_bits: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 386 | * The final bits of the message, in the upper portion of the |
Azure.IoT Build | 0:fa2de1b79154 | 387 | * byte. (Use 0b###00000 instead of 0b00000### to input the |
Azure.IoT Build | 0:fa2de1b79154 | 388 | * three bits ###.) |
Azure.IoT Build | 0:fa2de1b79154 | 389 | * length: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 390 | * The number of bits in message_bits, between 1 and 7. |
Azure.IoT Build | 0:fa2de1b79154 | 391 | * |
Azure.IoT Build | 0:fa2de1b79154 | 392 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 393 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 394 | * |
Azure.IoT Build | 0:fa2de1b79154 | 395 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 396 | int SHA384FinalBits(SHA384Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 397 | const uint8_t message_bits, unsigned int length) |
Azure.IoT Build | 0:fa2de1b79154 | 398 | { |
Azure.IoT Build | 0:fa2de1b79154 | 399 | return SHA512FinalBits(context, message_bits, length); |
Azure.IoT Build | 0:fa2de1b79154 | 400 | } |
Azure.IoT Build | 0:fa2de1b79154 | 401 | |
Azure.IoT Build | 0:fa2de1b79154 | 402 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 403 | * SHA384Result |
Azure.IoT Build | 0:fa2de1b79154 | 404 | * |
Azure.IoT Build | 0:fa2de1b79154 | 405 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 406 | * This function will return the 384-bit message |
Azure.IoT Build | 0:fa2de1b79154 | 407 | * digest into the Message_Digest array provided by the caller. |
Azure.IoT Build | 0:fa2de1b79154 | 408 | * NOTE: The first octet of hash is stored in the 0th element, |
Azure.IoT Build | 0:fa2de1b79154 | 409 | * the last octet of hash in the 48th element. |
Azure.IoT Build | 0:fa2de1b79154 | 410 | * |
Azure.IoT Build | 0:fa2de1b79154 | 411 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 412 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 413 | * The context to use to calculate the SHA hash. |
Azure.IoT Build | 0:fa2de1b79154 | 414 | * Message_Digest: [out] |
Azure.IoT Build | 0:fa2de1b79154 | 415 | * Where the digest is returned. |
Azure.IoT Build | 0:fa2de1b79154 | 416 | * |
Azure.IoT Build | 0:fa2de1b79154 | 417 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 418 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 419 | * |
Azure.IoT Build | 0:fa2de1b79154 | 420 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 421 | int SHA384Result(SHA384Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 422 | uint8_t Message_Digest[SHA384HashSize]) |
Azure.IoT Build | 0:fa2de1b79154 | 423 | { |
Azure.IoT Build | 0:fa2de1b79154 | 424 | return SHA384_512ResultN(context, Message_Digest, SHA384HashSize); |
Azure.IoT Build | 0:fa2de1b79154 | 425 | } |
Azure.IoT Build | 0:fa2de1b79154 | 426 | |
Azure.IoT Build | 0:fa2de1b79154 | 427 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 428 | * SHA512Reset |
Azure.IoT Build | 0:fa2de1b79154 | 429 | * |
Azure.IoT Build | 0:fa2de1b79154 | 430 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 431 | * This function will initialize the SHA512Context in preparation |
Azure.IoT Build | 0:fa2de1b79154 | 432 | * for computing a new SHA512 message digest. |
Azure.IoT Build | 0:fa2de1b79154 | 433 | * |
Azure.IoT Build | 0:fa2de1b79154 | 434 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 435 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 436 | * The context to reset. |
Azure.IoT Build | 0:fa2de1b79154 | 437 | * |
Azure.IoT Build | 0:fa2de1b79154 | 438 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 439 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 440 | * |
Azure.IoT Build | 0:fa2de1b79154 | 441 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 442 | int SHA512Reset(SHA512Context *context) |
Azure.IoT Build | 0:fa2de1b79154 | 443 | { |
Azure.IoT Build | 0:fa2de1b79154 | 444 | return SHA384_512Reset(context, SHA512_H0); |
Azure.IoT Build | 0:fa2de1b79154 | 445 | } |
Azure.IoT Build | 0:fa2de1b79154 | 446 | |
Azure.IoT Build | 0:fa2de1b79154 | 447 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 448 | * SHA512Input |
Azure.IoT Build | 0:fa2de1b79154 | 449 | * |
Azure.IoT Build | 0:fa2de1b79154 | 450 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 451 | * This function accepts an array of octets as the next portion |
Azure.IoT Build | 0:fa2de1b79154 | 452 | * of the message. |
Azure.IoT Build | 0:fa2de1b79154 | 453 | * |
Azure.IoT Build | 0:fa2de1b79154 | 454 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 455 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 456 | * The SHA context to update |
Azure.IoT Build | 0:fa2de1b79154 | 457 | * message_array: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 458 | * An array of characters representing the next portion of |
Azure.IoT Build | 0:fa2de1b79154 | 459 | * the message. |
Azure.IoT Build | 0:fa2de1b79154 | 460 | * length: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 461 | * The length of the message in message_array |
Azure.IoT Build | 0:fa2de1b79154 | 462 | * |
Azure.IoT Build | 0:fa2de1b79154 | 463 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 464 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 465 | * |
Azure.IoT Build | 0:fa2de1b79154 | 466 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 467 | int SHA512Input(SHA512Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 468 | const uint8_t *message_array, |
Azure.IoT Build | 0:fa2de1b79154 | 469 | unsigned int length) |
Azure.IoT Build | 0:fa2de1b79154 | 470 | { |
AzureIoTClient | 15:956c6d205aa7 | 471 | uint64_t addTemp; |
Azure.IoT Build | 0:fa2de1b79154 | 472 | if (!length) |
Azure.IoT Build | 0:fa2de1b79154 | 473 | return shaSuccess; |
Azure.IoT Build | 0:fa2de1b79154 | 474 | |
Azure.IoT Build | 0:fa2de1b79154 | 475 | if (!context || !message_array) |
Azure.IoT Build | 0:fa2de1b79154 | 476 | return shaNull; |
Azure.IoT Build | 0:fa2de1b79154 | 477 | |
Azure.IoT Build | 0:fa2de1b79154 | 478 | if (context->Computed) { |
Azure.IoT Build | 0:fa2de1b79154 | 479 | context->Corrupted = shaStateError; |
Azure.IoT Build | 0:fa2de1b79154 | 480 | return shaStateError; |
Azure.IoT Build | 0:fa2de1b79154 | 481 | } |
Azure.IoT Build | 0:fa2de1b79154 | 482 | |
Azure.IoT Build | 0:fa2de1b79154 | 483 | if (context->Corrupted) |
Azure.IoT Build | 0:fa2de1b79154 | 484 | return context->Corrupted; |
Azure.IoT Build | 0:fa2de1b79154 | 485 | |
Azure.IoT Build | 0:fa2de1b79154 | 486 | while (length-- && !context->Corrupted) { |
Azure.IoT Build | 0:fa2de1b79154 | 487 | context->Message_Block[context->Message_Block_Index++] = |
Azure.IoT Build | 0:fa2de1b79154 | 488 | (*message_array & 0xFF); |
Azure.IoT Build | 0:fa2de1b79154 | 489 | |
Azure.IoT Build | 0:fa2de1b79154 | 490 | if (!SHA384_512AddLength(context, 8) && |
Azure.IoT Build | 0:fa2de1b79154 | 491 | (context->Message_Block_Index == SHA512_Message_Block_Size)) |
Azure.IoT Build | 0:fa2de1b79154 | 492 | SHA384_512ProcessMessageBlock(context); |
Azure.IoT Build | 0:fa2de1b79154 | 493 | |
Azure.IoT Build | 0:fa2de1b79154 | 494 | message_array++; |
Azure.IoT Build | 0:fa2de1b79154 | 495 | } |
Azure.IoT Build | 0:fa2de1b79154 | 496 | |
Azure.IoT Build | 0:fa2de1b79154 | 497 | return shaSuccess; |
Azure.IoT Build | 0:fa2de1b79154 | 498 | } |
Azure.IoT Build | 0:fa2de1b79154 | 499 | |
Azure.IoT Build | 0:fa2de1b79154 | 500 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 501 | * SHA512FinalBits |
Azure.IoT Build | 0:fa2de1b79154 | 502 | * |
Azure.IoT Build | 0:fa2de1b79154 | 503 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 504 | * This function will add in any final bits of the message. |
Azure.IoT Build | 0:fa2de1b79154 | 505 | * |
Azure.IoT Build | 0:fa2de1b79154 | 506 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 507 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 508 | * The SHA context to update |
Azure.IoT Build | 0:fa2de1b79154 | 509 | * message_bits: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 510 | * The final bits of the message, in the upper portion of the |
Azure.IoT Build | 0:fa2de1b79154 | 511 | * byte. (Use 0b###00000 instead of 0b00000### to input the |
Azure.IoT Build | 0:fa2de1b79154 | 512 | * three bits ###.) |
Azure.IoT Build | 0:fa2de1b79154 | 513 | * length: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 514 | * The number of bits in message_bits, between 1 and 7. |
Azure.IoT Build | 0:fa2de1b79154 | 515 | * |
Azure.IoT Build | 0:fa2de1b79154 | 516 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 517 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 518 | * |
Azure.IoT Build | 0:fa2de1b79154 | 519 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 520 | int SHA512FinalBits(SHA512Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 521 | const uint8_t message_bits, unsigned int length) |
Azure.IoT Build | 0:fa2de1b79154 | 522 | { |
AzureIoTClient | 15:956c6d205aa7 | 523 | uint64_t addTemp; |
Azure.IoT Build | 0:fa2de1b79154 | 524 | uint8_t masks[8] = { |
Azure.IoT Build | 0:fa2de1b79154 | 525 | /* 0 0b00000000 */ 0x00, /* 1 0b10000000 */ 0x80, |
Azure.IoT Build | 0:fa2de1b79154 | 526 | /* 2 0b11000000 */ 0xC0, /* 3 0b11100000 */ 0xE0, |
Azure.IoT Build | 0:fa2de1b79154 | 527 | /* 4 0b11110000 */ 0xF0, /* 5 0b11111000 */ 0xF8, |
Azure.IoT Build | 0:fa2de1b79154 | 528 | /* 6 0b11111100 */ 0xFC, /* 7 0b11111110 */ 0xFE |
Azure.IoT Build | 0:fa2de1b79154 | 529 | }; |
Azure.IoT Build | 0:fa2de1b79154 | 530 | uint8_t markbit[8] = { |
Azure.IoT Build | 0:fa2de1b79154 | 531 | /* 0 0b10000000 */ 0x80, /* 1 0b01000000 */ 0x40, |
Azure.IoT Build | 0:fa2de1b79154 | 532 | /* 2 0b00100000 */ 0x20, /* 3 0b00010000 */ 0x10, |
Azure.IoT Build | 0:fa2de1b79154 | 533 | /* 4 0b00001000 */ 0x08, /* 5 0b00000100 */ 0x04, |
Azure.IoT Build | 0:fa2de1b79154 | 534 | /* 6 0b00000010 */ 0x02, /* 7 0b00000001 */ 0x01 |
Azure.IoT Build | 0:fa2de1b79154 | 535 | }; |
Azure.IoT Build | 0:fa2de1b79154 | 536 | |
Azure.IoT Build | 0:fa2de1b79154 | 537 | if (!length) |
Azure.IoT Build | 0:fa2de1b79154 | 538 | return shaSuccess; |
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 | if ((context->Computed) || (length >= 8) || (length == 0)) { |
Azure.IoT Build | 0:fa2de1b79154 | 544 | context->Corrupted = shaStateError; |
Azure.IoT Build | 0:fa2de1b79154 | 545 | return shaStateError; |
Azure.IoT Build | 0:fa2de1b79154 | 546 | } |
Azure.IoT Build | 0:fa2de1b79154 | 547 | |
Azure.IoT Build | 0:fa2de1b79154 | 548 | if (context->Corrupted) |
Azure.IoT Build | 0:fa2de1b79154 | 549 | return context->Corrupted; |
Azure.IoT Build | 0:fa2de1b79154 | 550 | |
Azure.IoT Build | 0:fa2de1b79154 | 551 | SHA384_512AddLength(context, length); |
Azure.IoT Build | 0:fa2de1b79154 | 552 | SHA384_512Finalize(context, (uint8_t) |
Azure.IoT Build | 0:fa2de1b79154 | 553 | ((message_bits & masks[length]) | markbit[length])); |
Azure.IoT Build | 0:fa2de1b79154 | 554 | |
Azure.IoT Build | 0:fa2de1b79154 | 555 | return shaSuccess; |
Azure.IoT Build | 0:fa2de1b79154 | 556 | } |
Azure.IoT Build | 0:fa2de1b79154 | 557 | |
Azure.IoT Build | 0:fa2de1b79154 | 558 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 559 | * SHA384_512Finalize |
Azure.IoT Build | 0:fa2de1b79154 | 560 | * |
Azure.IoT Build | 0:fa2de1b79154 | 561 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 562 | * This helper function finishes off the digest calculations. |
Azure.IoT Build | 0:fa2de1b79154 | 563 | * |
Azure.IoT Build | 0:fa2de1b79154 | 564 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 565 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 566 | * The SHA context to update |
Azure.IoT Build | 0:fa2de1b79154 | 567 | * Pad_Byte: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 568 | * The last byte to add to the digest before the 0-padding |
Azure.IoT Build | 0:fa2de1b79154 | 569 | * and length. This will contain the last bits of the message |
Azure.IoT Build | 0:fa2de1b79154 | 570 | * followed by another single bit. If the message was an |
Azure.IoT Build | 0:fa2de1b79154 | 571 | * exact multiple of 8-bits long, Pad_Byte will be 0x80. |
Azure.IoT Build | 0:fa2de1b79154 | 572 | * |
Azure.IoT Build | 0:fa2de1b79154 | 573 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 574 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 575 | * |
Azure.IoT Build | 0:fa2de1b79154 | 576 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 577 | static void SHA384_512Finalize(SHA512Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 578 | uint8_t Pad_Byte) |
Azure.IoT Build | 0:fa2de1b79154 | 579 | { |
Azure.IoT Build | 0:fa2de1b79154 | 580 | int_least16_t i; |
Azure.IoT Build | 0:fa2de1b79154 | 581 | SHA384_512PadMessage(context, Pad_Byte); |
Azure.IoT Build | 0:fa2de1b79154 | 582 | /* message may be sensitive, clear it out */ |
Azure.IoT Build | 0:fa2de1b79154 | 583 | for (i = 0; i < SHA512_Message_Block_Size; ++i) |
Azure.IoT Build | 0:fa2de1b79154 | 584 | context->Message_Block[i] = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 585 | #ifdef USE_32BIT_ONLY /* and clear length */ |
Azure.IoT Build | 0:fa2de1b79154 | 586 | context->Length[0] = context->Length[1] = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 587 | context->Length[2] = context->Length[3] = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 588 | #else /* !USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 589 | context->Length_Low = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 590 | context->Length_High = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 591 | #endif /* USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 592 | context->Computed = 1; |
Azure.IoT Build | 0:fa2de1b79154 | 593 | } |
Azure.IoT Build | 0:fa2de1b79154 | 594 | |
Azure.IoT Build | 0:fa2de1b79154 | 595 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 596 | * SHA512Result |
Azure.IoT Build | 0:fa2de1b79154 | 597 | * |
Azure.IoT Build | 0:fa2de1b79154 | 598 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 599 | * This function will return the 512-bit message |
Azure.IoT Build | 0:fa2de1b79154 | 600 | * digest into the Message_Digest array provided by the caller. |
Azure.IoT Build | 0:fa2de1b79154 | 601 | * NOTE: The first octet of hash is stored in the 0th element, |
Azure.IoT Build | 0:fa2de1b79154 | 602 | * the last octet of hash in the 64th element. |
Azure.IoT Build | 0:fa2de1b79154 | 603 | * |
Azure.IoT Build | 0:fa2de1b79154 | 604 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 605 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 606 | * The context to use to calculate the SHA hash. |
Azure.IoT Build | 0:fa2de1b79154 | 607 | * Message_Digest: [out] |
Azure.IoT Build | 0:fa2de1b79154 | 608 | * Where the digest is returned. |
Azure.IoT Build | 0:fa2de1b79154 | 609 | * |
Azure.IoT Build | 0:fa2de1b79154 | 610 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 611 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 612 | * |
Azure.IoT Build | 0:fa2de1b79154 | 613 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 614 | int SHA512Result(SHA512Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 615 | uint8_t Message_Digest[SHA512HashSize]) |
Azure.IoT Build | 0:fa2de1b79154 | 616 | { |
Azure.IoT Build | 0:fa2de1b79154 | 617 | return SHA384_512ResultN(context, Message_Digest, SHA512HashSize); |
Azure.IoT Build | 0:fa2de1b79154 | 618 | } |
Azure.IoT Build | 0:fa2de1b79154 | 619 | |
Azure.IoT Build | 0:fa2de1b79154 | 620 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 621 | * SHA384_512PadMessage |
Azure.IoT Build | 0:fa2de1b79154 | 622 | * |
Azure.IoT Build | 0:fa2de1b79154 | 623 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 624 | * According to the standard, the message must be padded to an |
Azure.IoT Build | 0:fa2de1b79154 | 625 | * even 1024 bits. The first padding bit must be a '1'. The |
Azure.IoT Build | 0:fa2de1b79154 | 626 | * last 128 bits represent the length of the original message. |
Azure.IoT Build | 0:fa2de1b79154 | 627 | * All bits in between should be 0. This helper function will |
Azure.IoT Build | 0:fa2de1b79154 | 628 | * pad the message according to those rules by filling the |
Azure.IoT Build | 0:fa2de1b79154 | 629 | * Message_Block array accordingly. When it returns, it can be |
Azure.IoT Build | 0:fa2de1b79154 | 630 | * assumed that the message digest has been computed. |
Azure.IoT Build | 0:fa2de1b79154 | 631 | * |
Azure.IoT Build | 0:fa2de1b79154 | 632 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 633 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 634 | * The context to pad |
Azure.IoT Build | 0:fa2de1b79154 | 635 | * Pad_Byte: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 636 | * The last byte to add to the digest before the 0-padding |
Azure.IoT Build | 0:fa2de1b79154 | 637 | * and length. This will contain the last bits of the message |
Azure.IoT Build | 0:fa2de1b79154 | 638 | * followed by another single bit. If the message was an |
Azure.IoT Build | 0:fa2de1b79154 | 639 | * exact multiple of 8-bits long, Pad_Byte will be 0x80. |
Azure.IoT Build | 0:fa2de1b79154 | 640 | * |
Azure.IoT Build | 0:fa2de1b79154 | 641 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 642 | * Nothing. |
Azure.IoT Build | 0:fa2de1b79154 | 643 | * |
Azure.IoT Build | 0:fa2de1b79154 | 644 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 645 | static void SHA384_512PadMessage(SHA512Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 646 | uint8_t Pad_Byte) |
Azure.IoT Build | 0:fa2de1b79154 | 647 | { |
Azure.IoT Build | 0:fa2de1b79154 | 648 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 649 | * Check to see if the current message block is too small to hold |
Azure.IoT Build | 0:fa2de1b79154 | 650 | * the initial padding bits and length. If so, we will pad the |
Azure.IoT Build | 0:fa2de1b79154 | 651 | * block, process it, and then continue padding into a second |
Azure.IoT Build | 0:fa2de1b79154 | 652 | * block. |
Azure.IoT Build | 0:fa2de1b79154 | 653 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 654 | if (context->Message_Block_Index >= (SHA512_Message_Block_Size - 16)) { |
Azure.IoT Build | 0:fa2de1b79154 | 655 | context->Message_Block[context->Message_Block_Index++] = Pad_Byte; |
Azure.IoT Build | 0:fa2de1b79154 | 656 | while (context->Message_Block_Index < SHA512_Message_Block_Size) |
Azure.IoT Build | 0:fa2de1b79154 | 657 | context->Message_Block[context->Message_Block_Index++] = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 658 | |
Azure.IoT Build | 0:fa2de1b79154 | 659 | SHA384_512ProcessMessageBlock(context); |
Azure.IoT Build | 0:fa2de1b79154 | 660 | } |
Azure.IoT Build | 0:fa2de1b79154 | 661 | else |
Azure.IoT Build | 0:fa2de1b79154 | 662 | context->Message_Block[context->Message_Block_Index++] = Pad_Byte; |
Azure.IoT Build | 0:fa2de1b79154 | 663 | |
Azure.IoT Build | 0:fa2de1b79154 | 664 | while (context->Message_Block_Index < (SHA512_Message_Block_Size - 16)) |
Azure.IoT Build | 0:fa2de1b79154 | 665 | context->Message_Block[context->Message_Block_Index++] = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 666 | |
Azure.IoT Build | 0:fa2de1b79154 | 667 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 668 | * Store the message length as the last 16 octets |
Azure.IoT Build | 0:fa2de1b79154 | 669 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 670 | #ifdef USE_32BIT_ONLY |
Azure.IoT Build | 0:fa2de1b79154 | 671 | context->Message_Block[112] = (uint8_t)(context->Length[0] >> 24); |
Azure.IoT Build | 0:fa2de1b79154 | 672 | context->Message_Block[113] = (uint8_t)(context->Length[0] >> 16); |
Azure.IoT Build | 0:fa2de1b79154 | 673 | context->Message_Block[114] = (uint8_t)(context->Length[0] >> 8); |
Azure.IoT Build | 0:fa2de1b79154 | 674 | context->Message_Block[115] = (uint8_t)(context->Length[0]); |
Azure.IoT Build | 0:fa2de1b79154 | 675 | context->Message_Block[116] = (uint8_t)(context->Length[1] >> 24); |
Azure.IoT Build | 0:fa2de1b79154 | 676 | context->Message_Block[117] = (uint8_t)(context->Length[1] >> 16); |
Azure.IoT Build | 0:fa2de1b79154 | 677 | context->Message_Block[118] = (uint8_t)(context->Length[1] >> 8); |
Azure.IoT Build | 0:fa2de1b79154 | 678 | context->Message_Block[119] = (uint8_t)(context->Length[1]); |
Azure.IoT Build | 0:fa2de1b79154 | 679 | |
Azure.IoT Build | 0:fa2de1b79154 | 680 | context->Message_Block[120] = (uint8_t)(context->Length[2] >> 24); |
Azure.IoT Build | 0:fa2de1b79154 | 681 | context->Message_Block[121] = (uint8_t)(context->Length[2] >> 16); |
Azure.IoT Build | 0:fa2de1b79154 | 682 | context->Message_Block[122] = (uint8_t)(context->Length[2] >> 8); |
Azure.IoT Build | 0:fa2de1b79154 | 683 | context->Message_Block[123] = (uint8_t)(context->Length[2]); |
Azure.IoT Build | 0:fa2de1b79154 | 684 | context->Message_Block[124] = (uint8_t)(context->Length[3] >> 24); |
Azure.IoT Build | 0:fa2de1b79154 | 685 | context->Message_Block[125] = (uint8_t)(context->Length[3] >> 16); |
Azure.IoT Build | 0:fa2de1b79154 | 686 | context->Message_Block[126] = (uint8_t)(context->Length[3] >> 8); |
Azure.IoT Build | 0:fa2de1b79154 | 687 | context->Message_Block[127] = (uint8_t)(context->Length[3]); |
Azure.IoT Build | 0:fa2de1b79154 | 688 | #else /* !USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 689 | context->Message_Block[112] = (uint8_t)(context->Length_High >> 56); |
Azure.IoT Build | 0:fa2de1b79154 | 690 | context->Message_Block[113] = (uint8_t)(context->Length_High >> 48); |
Azure.IoT Build | 0:fa2de1b79154 | 691 | context->Message_Block[114] = (uint8_t)(context->Length_High >> 40); |
Azure.IoT Build | 0:fa2de1b79154 | 692 | context->Message_Block[115] = (uint8_t)(context->Length_High >> 32); |
Azure.IoT Build | 0:fa2de1b79154 | 693 | context->Message_Block[116] = (uint8_t)(context->Length_High >> 24); |
Azure.IoT Build | 0:fa2de1b79154 | 694 | context->Message_Block[117] = (uint8_t)(context->Length_High >> 16); |
Azure.IoT Build | 0:fa2de1b79154 | 695 | context->Message_Block[118] = (uint8_t)(context->Length_High >> 8); |
Azure.IoT Build | 0:fa2de1b79154 | 696 | context->Message_Block[119] = (uint8_t)(context->Length_High); |
Azure.IoT Build | 0:fa2de1b79154 | 697 | |
Azure.IoT Build | 0:fa2de1b79154 | 698 | context->Message_Block[120] = (uint8_t)(context->Length_Low >> 56); |
Azure.IoT Build | 0:fa2de1b79154 | 699 | context->Message_Block[121] = (uint8_t)(context->Length_Low >> 48); |
Azure.IoT Build | 0:fa2de1b79154 | 700 | context->Message_Block[122] = (uint8_t)(context->Length_Low >> 40); |
Azure.IoT Build | 0:fa2de1b79154 | 701 | context->Message_Block[123] = (uint8_t)(context->Length_Low >> 32); |
Azure.IoT Build | 0:fa2de1b79154 | 702 | context->Message_Block[124] = (uint8_t)(context->Length_Low >> 24); |
Azure.IoT Build | 0:fa2de1b79154 | 703 | context->Message_Block[125] = (uint8_t)(context->Length_Low >> 16); |
Azure.IoT Build | 0:fa2de1b79154 | 704 | context->Message_Block[126] = (uint8_t)(context->Length_Low >> 8); |
Azure.IoT Build | 0:fa2de1b79154 | 705 | context->Message_Block[127] = (uint8_t)(context->Length_Low); |
Azure.IoT Build | 0:fa2de1b79154 | 706 | #endif /* USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 707 | |
Azure.IoT Build | 0:fa2de1b79154 | 708 | SHA384_512ProcessMessageBlock(context); |
Azure.IoT Build | 0:fa2de1b79154 | 709 | } |
Azure.IoT Build | 0:fa2de1b79154 | 710 | |
Azure.IoT Build | 0:fa2de1b79154 | 711 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 712 | * SHA384_512ProcessMessageBlock |
Azure.IoT Build | 0:fa2de1b79154 | 713 | * |
Azure.IoT Build | 0:fa2de1b79154 | 714 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 715 | * This helper function will process the next 1024 bits of the |
Azure.IoT Build | 0:fa2de1b79154 | 716 | * message stored in the Message_Block array. |
Azure.IoT Build | 0:fa2de1b79154 | 717 | * |
Azure.IoT Build | 0:fa2de1b79154 | 718 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 719 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 720 | * The SHA context to update |
Azure.IoT Build | 0:fa2de1b79154 | 721 | * |
Azure.IoT Build | 0:fa2de1b79154 | 722 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 723 | * Nothing. |
Azure.IoT Build | 0:fa2de1b79154 | 724 | * |
Azure.IoT Build | 0:fa2de1b79154 | 725 | * Comments: |
Azure.IoT Build | 0:fa2de1b79154 | 726 | * Many of the variable names in this code, especially the |
Azure.IoT Build | 0:fa2de1b79154 | 727 | * single character names, were used because those were the |
Azure.IoT Build | 0:fa2de1b79154 | 728 | * names used in the publication. |
Azure.IoT Build | 0:fa2de1b79154 | 729 | * |
Azure.IoT Build | 0:fa2de1b79154 | 730 | * |
Azure.IoT Build | 0:fa2de1b79154 | 731 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 732 | static void SHA384_512ProcessMessageBlock(SHA512Context *context) |
Azure.IoT Build | 0:fa2de1b79154 | 733 | { |
Azure.IoT Build | 0:fa2de1b79154 | 734 | /* Constants defined in FIPS-180-2, section 4.2.3 */ |
Azure.IoT Build | 0:fa2de1b79154 | 735 | #ifdef USE_32BIT_ONLY |
Azure.IoT Build | 0:fa2de1b79154 | 736 | static const uint32_t K[80 * 2] = { |
Azure.IoT Build | 0:fa2de1b79154 | 737 | 0x428A2F98, 0xD728AE22, 0x71374491, 0x23EF65CD, 0xB5C0FBCF, |
Azure.IoT Build | 0:fa2de1b79154 | 738 | 0xEC4D3B2F, 0xE9B5DBA5, 0x8189DBBC, 0x3956C25B, 0xF348B538, |
Azure.IoT Build | 0:fa2de1b79154 | 739 | 0x59F111F1, 0xB605D019, 0x923F82A4, 0xAF194F9B, 0xAB1C5ED5, |
Azure.IoT Build | 0:fa2de1b79154 | 740 | 0xDA6D8118, 0xD807AA98, 0xA3030242, 0x12835B01, 0x45706FBE, |
Azure.IoT Build | 0:fa2de1b79154 | 741 | 0x243185BE, 0x4EE4B28C, 0x550C7DC3, 0xD5FFB4E2, 0x72BE5D74, |
Azure.IoT Build | 0:fa2de1b79154 | 742 | 0xF27B896F, 0x80DEB1FE, 0x3B1696B1, 0x9BDC06A7, 0x25C71235, |
Azure.IoT Build | 0:fa2de1b79154 | 743 | 0xC19BF174, 0xCF692694, 0xE49B69C1, 0x9EF14AD2, 0xEFBE4786, |
Azure.IoT Build | 0:fa2de1b79154 | 744 | 0x384F25E3, 0x0FC19DC6, 0x8B8CD5B5, 0x240CA1CC, 0x77AC9C65, |
Azure.IoT Build | 0:fa2de1b79154 | 745 | 0x2DE92C6F, 0x592B0275, 0x4A7484AA, 0x6EA6E483, 0x5CB0A9DC, |
Azure.IoT Build | 0:fa2de1b79154 | 746 | 0xBD41FBD4, 0x76F988DA, 0x831153B5, 0x983E5152, 0xEE66DFAB, |
Azure.IoT Build | 0:fa2de1b79154 | 747 | 0xA831C66D, 0x2DB43210, 0xB00327C8, 0x98FB213F, 0xBF597FC7, |
Azure.IoT Build | 0:fa2de1b79154 | 748 | 0xBEEF0EE4, 0xC6E00BF3, 0x3DA88FC2, 0xD5A79147, 0x930AA725, |
Azure.IoT Build | 0:fa2de1b79154 | 749 | 0x06CA6351, 0xE003826F, 0x14292967, 0x0A0E6E70, 0x27B70A85, |
Azure.IoT Build | 0:fa2de1b79154 | 750 | 0x46D22FFC, 0x2E1B2138, 0x5C26C926, 0x4D2C6DFC, 0x5AC42AED, |
Azure.IoT Build | 0:fa2de1b79154 | 751 | 0x53380D13, 0x9D95B3DF, 0x650A7354, 0x8BAF63DE, 0x766A0ABB, |
Azure.IoT Build | 0:fa2de1b79154 | 752 | 0x3C77B2A8, 0x81C2C92E, 0x47EDAEE6, 0x92722C85, 0x1482353B, |
Azure.IoT Build | 0:fa2de1b79154 | 753 | 0xA2BFE8A1, 0x4CF10364, 0xA81A664B, 0xBC423001, 0xC24B8B70, |
Azure.IoT Build | 0:fa2de1b79154 | 754 | 0xD0F89791, 0xC76C51A3, 0x0654BE30, 0xD192E819, 0xD6EF5218, |
Azure.IoT Build | 0:fa2de1b79154 | 755 | 0xD6990624, 0x5565A910, 0xF40E3585, 0x5771202A, 0x106AA070, |
Azure.IoT Build | 0:fa2de1b79154 | 756 | 0x32BBD1B8, 0x19A4C116, 0xB8D2D0C8, 0x1E376C08, 0x5141AB53, |
Azure.IoT Build | 0:fa2de1b79154 | 757 | 0x2748774C, 0xDF8EEB99, 0x34B0BCB5, 0xE19B48A8, 0x391C0CB3, |
Azure.IoT Build | 0:fa2de1b79154 | 758 | 0xC5C95A63, 0x4ED8AA4A, 0xE3418ACB, 0x5B9CCA4F, 0x7763E373, |
Azure.IoT Build | 0:fa2de1b79154 | 759 | 0x682E6FF3, 0xD6B2B8A3, 0x748F82EE, 0x5DEFB2FC, 0x78A5636F, |
Azure.IoT Build | 0:fa2de1b79154 | 760 | 0x43172F60, 0x84C87814, 0xA1F0AB72, 0x8CC70208, 0x1A6439EC, |
Azure.IoT Build | 0:fa2de1b79154 | 761 | 0x90BEFFFA, 0x23631E28, 0xA4506CEB, 0xDE82BDE9, 0xBEF9A3F7, |
Azure.IoT Build | 0:fa2de1b79154 | 762 | 0xB2C67915, 0xC67178F2, 0xE372532B, 0xCA273ECE, 0xEA26619C, |
Azure.IoT Build | 0:fa2de1b79154 | 763 | 0xD186B8C7, 0x21C0C207, 0xEADA7DD6, 0xCDE0EB1E, 0xF57D4F7F, |
Azure.IoT Build | 0:fa2de1b79154 | 764 | 0xEE6ED178, 0x06F067AA, 0x72176FBA, 0x0A637DC5, 0xA2C898A6, |
Azure.IoT Build | 0:fa2de1b79154 | 765 | 0x113F9804, 0xBEF90DAE, 0x1B710B35, 0x131C471B, 0x28DB77F5, |
Azure.IoT Build | 0:fa2de1b79154 | 766 | 0x23047D84, 0x32CAAB7B, 0x40C72493, 0x3C9EBE0A, 0x15C9BEBC, |
Azure.IoT Build | 0:fa2de1b79154 | 767 | 0x431D67C4, 0x9C100D4C, 0x4CC5D4BE, 0xCB3E42B6, 0x597F299C, |
Azure.IoT Build | 0:fa2de1b79154 | 768 | 0xFC657E2A, 0x5FCB6FAB, 0x3AD6FAEC, 0x6C44198C, 0x4A475817 |
Azure.IoT Build | 0:fa2de1b79154 | 769 | }; |
Azure.IoT Build | 0:fa2de1b79154 | 770 | int t, t2, t8; /* Loop counter */ |
Azure.IoT Build | 0:fa2de1b79154 | 771 | uint32_t temp1[2], temp2[2], /* Temporary word values */ |
Azure.IoT Build | 0:fa2de1b79154 | 772 | temp3[2], temp4[2], temp5[2]; |
Azure.IoT Build | 0:fa2de1b79154 | 773 | uint32_t W[2 * 80]; /* Word sequence */ |
Azure.IoT Build | 0:fa2de1b79154 | 774 | uint32_t A[2], B[2], C[2], D[2], /* Word buffers */ |
Azure.IoT Build | 0:fa2de1b79154 | 775 | E[2], F[2], G[2], H[2]; |
Azure.IoT Build | 0:fa2de1b79154 | 776 | |
Azure.IoT Build | 0:fa2de1b79154 | 777 | /* Initialize the first 16 words in the array W */ |
Azure.IoT Build | 0:fa2de1b79154 | 778 | for (t = t2 = t8 = 0; t < 16; t++, t8 += 8) { |
Azure.IoT Build | 0:fa2de1b79154 | 779 | W[t2++] = ((((uint32_t)context->Message_Block[t8])) << 24) | |
Azure.IoT Build | 0:fa2de1b79154 | 780 | ((((uint32_t)context->Message_Block[t8 + 1])) << 16) | |
Azure.IoT Build | 0:fa2de1b79154 | 781 | ((((uint32_t)context->Message_Block[t8 + 2])) << 8) | |
Azure.IoT Build | 0:fa2de1b79154 | 782 | ((((uint32_t)context->Message_Block[t8 + 3]))); |
Azure.IoT Build | 0:fa2de1b79154 | 783 | W[t2++] = ((((uint32_t)context->Message_Block[t8 + 4])) << 24) | |
Azure.IoT Build | 0:fa2de1b79154 | 784 | ((((uint32_t)context->Message_Block[t8 + 5])) << 16) | |
Azure.IoT Build | 0:fa2de1b79154 | 785 | ((((uint32_t)context->Message_Block[t8 + 6])) << 8) | |
Azure.IoT Build | 0:fa2de1b79154 | 786 | ((((uint32_t)context->Message_Block[t8 + 7]))); |
Azure.IoT Build | 0:fa2de1b79154 | 787 | } |
Azure.IoT Build | 0:fa2de1b79154 | 788 | |
Azure.IoT Build | 0:fa2de1b79154 | 789 | for (t = 16; t < 80; t++, t2 += 2) { |
Azure.IoT Build | 0:fa2de1b79154 | 790 | /* W[t] = SHA512_sigma1(W[t-2]) + W[t-7] + |
Azure.IoT Build | 0:fa2de1b79154 | 791 | SHA512_sigma0(W[t-15]) + W[t-16]; */ |
Azure.IoT Build | 0:fa2de1b79154 | 792 | uint32_t *Wt2 = &W[t2 - 2 * 2]; |
Azure.IoT Build | 0:fa2de1b79154 | 793 | uint32_t *Wt7 = &W[t2 - 7 * 2]; |
Azure.IoT Build | 0:fa2de1b79154 | 794 | uint32_t *Wt15 = &W[t2 - 15 * 2]; |
Azure.IoT Build | 0:fa2de1b79154 | 795 | uint32_t *Wt16 = &W[t2 - 16 * 2]; |
Azure.IoT Build | 0:fa2de1b79154 | 796 | SHA512_sigma1(Wt2, temp1); |
Azure.IoT Build | 0:fa2de1b79154 | 797 | SHA512_ADD(temp1, Wt7, temp2); |
Azure.IoT Build | 0:fa2de1b79154 | 798 | SHA512_sigma0(Wt15, temp1); |
Azure.IoT Build | 0:fa2de1b79154 | 799 | SHA512_ADD(temp1, Wt16, temp3); |
Azure.IoT Build | 0:fa2de1b79154 | 800 | SHA512_ADD(temp2, temp3, &W[t2]); |
Azure.IoT Build | 0:fa2de1b79154 | 801 | } |
Azure.IoT Build | 0:fa2de1b79154 | 802 | |
Azure.IoT Build | 0:fa2de1b79154 | 803 | A[0] = context->Intermediate_Hash[0]; |
Azure.IoT Build | 0:fa2de1b79154 | 804 | A[1] = context->Intermediate_Hash[1]; |
Azure.IoT Build | 0:fa2de1b79154 | 805 | B[0] = context->Intermediate_Hash[2]; |
Azure.IoT Build | 0:fa2de1b79154 | 806 | B[1] = context->Intermediate_Hash[3]; |
Azure.IoT Build | 0:fa2de1b79154 | 807 | C[0] = context->Intermediate_Hash[4]; |
Azure.IoT Build | 0:fa2de1b79154 | 808 | C[1] = context->Intermediate_Hash[5]; |
Azure.IoT Build | 0:fa2de1b79154 | 809 | D[0] = context->Intermediate_Hash[6]; |
Azure.IoT Build | 0:fa2de1b79154 | 810 | D[1] = context->Intermediate_Hash[7]; |
Azure.IoT Build | 0:fa2de1b79154 | 811 | E[0] = context->Intermediate_Hash[8]; |
Azure.IoT Build | 0:fa2de1b79154 | 812 | E[1] = context->Intermediate_Hash[9]; |
Azure.IoT Build | 0:fa2de1b79154 | 813 | F[0] = context->Intermediate_Hash[10]; |
Azure.IoT Build | 0:fa2de1b79154 | 814 | F[1] = context->Intermediate_Hash[11]; |
Azure.IoT Build | 0:fa2de1b79154 | 815 | G[0] = context->Intermediate_Hash[12]; |
Azure.IoT Build | 0:fa2de1b79154 | 816 | G[1] = context->Intermediate_Hash[13]; |
Azure.IoT Build | 0:fa2de1b79154 | 817 | H[0] = context->Intermediate_Hash[14]; |
Azure.IoT Build | 0:fa2de1b79154 | 818 | H[1] = context->Intermediate_Hash[15]; |
Azure.IoT Build | 0:fa2de1b79154 | 819 | |
Azure.IoT Build | 0:fa2de1b79154 | 820 | for (t = t2 = 0; t < 80; t++, t2 += 2) { |
Azure.IoT Build | 0:fa2de1b79154 | 821 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 822 | * temp1 = H + SHA512_SIGMA1(E) + SHA_Ch(E,F,G) + K[t] + W[t]; |
Azure.IoT Build | 0:fa2de1b79154 | 823 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 824 | SHA512_SIGMA1(E, temp1); |
Azure.IoT Build | 0:fa2de1b79154 | 825 | SHA512_ADD(H, temp1, temp2); |
Azure.IoT Build | 0:fa2de1b79154 | 826 | SHA_Ch(E, F, G, temp3); |
Azure.IoT Build | 0:fa2de1b79154 | 827 | SHA512_ADD(temp2, temp3, temp4); |
Azure.IoT Build | 0:fa2de1b79154 | 828 | SHA512_ADD(&K[t2], &W[t2], temp5); |
Azure.IoT Build | 0:fa2de1b79154 | 829 | SHA512_ADD(temp4, temp5, temp1); |
Azure.IoT Build | 0:fa2de1b79154 | 830 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 831 | * temp2 = SHA512_SIGMA0(A) + SHA_Maj(A,B,C); |
Azure.IoT Build | 0:fa2de1b79154 | 832 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 833 | SHA512_SIGMA0(A, temp3); |
Azure.IoT Build | 0:fa2de1b79154 | 834 | SHA_Maj(A, B, C, temp4); |
Azure.IoT Build | 0:fa2de1b79154 | 835 | SHA512_ADD(temp3, temp4, temp2); |
Azure.IoT Build | 0:fa2de1b79154 | 836 | H[0] = G[0]; H[1] = G[1]; |
Azure.IoT Build | 0:fa2de1b79154 | 837 | G[0] = F[0]; G[1] = F[1]; |
Azure.IoT Build | 0:fa2de1b79154 | 838 | F[0] = E[0]; F[1] = E[1]; |
Azure.IoT Build | 0:fa2de1b79154 | 839 | SHA512_ADD(D, temp1, E); |
Azure.IoT Build | 0:fa2de1b79154 | 840 | D[0] = C[0]; D[1] = C[1]; |
Azure.IoT Build | 0:fa2de1b79154 | 841 | C[0] = B[0]; C[1] = B[1]; |
Azure.IoT Build | 0:fa2de1b79154 | 842 | B[0] = A[0]; B[1] = A[1]; |
Azure.IoT Build | 0:fa2de1b79154 | 843 | SHA512_ADD(temp1, temp2, A); |
Azure.IoT Build | 0:fa2de1b79154 | 844 | } |
Azure.IoT Build | 0:fa2de1b79154 | 845 | |
Azure.IoT Build | 0:fa2de1b79154 | 846 | SHA512_ADDTO2(&context->Intermediate_Hash[0], A); |
Azure.IoT Build | 0:fa2de1b79154 | 847 | SHA512_ADDTO2(&context->Intermediate_Hash[2], B); |
Azure.IoT Build | 0:fa2de1b79154 | 848 | SHA512_ADDTO2(&context->Intermediate_Hash[4], C); |
Azure.IoT Build | 0:fa2de1b79154 | 849 | SHA512_ADDTO2(&context->Intermediate_Hash[6], D); |
Azure.IoT Build | 0:fa2de1b79154 | 850 | SHA512_ADDTO2(&context->Intermediate_Hash[8], E); |
Azure.IoT Build | 0:fa2de1b79154 | 851 | SHA512_ADDTO2(&context->Intermediate_Hash[10], F); |
Azure.IoT Build | 0:fa2de1b79154 | 852 | SHA512_ADDTO2(&context->Intermediate_Hash[12], G); |
Azure.IoT Build | 0:fa2de1b79154 | 853 | SHA512_ADDTO2(&context->Intermediate_Hash[14], H); |
Azure.IoT Build | 0:fa2de1b79154 | 854 | |
Azure.IoT Build | 0:fa2de1b79154 | 855 | #else /* !USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 856 | static const uint64_t K[80] = { |
Azure.IoT Build | 0:fa2de1b79154 | 857 | 0x428A2F98D728AE22ull, 0x7137449123EF65CDull, 0xB5C0FBCFEC4D3B2Full, |
Azure.IoT Build | 0:fa2de1b79154 | 858 | 0xE9B5DBA58189DBBCull, 0x3956C25BF348B538ull, 0x59F111F1B605D019ull, |
Azure.IoT Build | 0:fa2de1b79154 | 859 | 0x923F82A4AF194F9Bull, 0xAB1C5ED5DA6D8118ull, 0xD807AA98A3030242ull, |
Azure.IoT Build | 0:fa2de1b79154 | 860 | 0x12835B0145706FBEull, 0x243185BE4EE4B28Cull, 0x550C7DC3D5FFB4E2ull, |
Azure.IoT Build | 0:fa2de1b79154 | 861 | 0x72BE5D74F27B896Full, 0x80DEB1FE3B1696B1ull, 0x9BDC06A725C71235ull, |
Azure.IoT Build | 0:fa2de1b79154 | 862 | 0xC19BF174CF692694ull, 0xE49B69C19EF14AD2ull, 0xEFBE4786384F25E3ull, |
Azure.IoT Build | 0:fa2de1b79154 | 863 | 0x0FC19DC68B8CD5B5ull, 0x240CA1CC77AC9C65ull, 0x2DE92C6F592B0275ull, |
Azure.IoT Build | 0:fa2de1b79154 | 864 | 0x4A7484AA6EA6E483ull, 0x5CB0A9DCBD41FBD4ull, 0x76F988DA831153B5ull, |
Azure.IoT Build | 0:fa2de1b79154 | 865 | 0x983E5152EE66DFABull, 0xA831C66D2DB43210ull, 0xB00327C898FB213Full, |
Azure.IoT Build | 0:fa2de1b79154 | 866 | 0xBF597FC7BEEF0EE4ull, 0xC6E00BF33DA88FC2ull, 0xD5A79147930AA725ull, |
Azure.IoT Build | 0:fa2de1b79154 | 867 | 0x06CA6351E003826Full, 0x142929670A0E6E70ull, 0x27B70A8546D22FFCull, |
Azure.IoT Build | 0:fa2de1b79154 | 868 | 0x2E1B21385C26C926ull, 0x4D2C6DFC5AC42AEDull, 0x53380D139D95B3DFull, |
Azure.IoT Build | 0:fa2de1b79154 | 869 | 0x650A73548BAF63DEull, 0x766A0ABB3C77B2A8ull, 0x81C2C92E47EDAEE6ull, |
Azure.IoT Build | 0:fa2de1b79154 | 870 | 0x92722C851482353Bull, 0xA2BFE8A14CF10364ull, 0xA81A664BBC423001ull, |
Azure.IoT Build | 0:fa2de1b79154 | 871 | 0xC24B8B70D0F89791ull, 0xC76C51A30654BE30ull, 0xD192E819D6EF5218ull, |
Azure.IoT Build | 0:fa2de1b79154 | 872 | 0xD69906245565A910ull, 0xF40E35855771202Aull, 0x106AA07032BBD1B8ull, |
Azure.IoT Build | 0:fa2de1b79154 | 873 | 0x19A4C116B8D2D0C8ull, 0x1E376C085141AB53ull, 0x2748774CDF8EEB99ull, |
Azure.IoT Build | 0:fa2de1b79154 | 874 | 0x34B0BCB5E19B48A8ull, 0x391C0CB3C5C95A63ull, 0x4ED8AA4AE3418ACBull, |
Azure.IoT Build | 0:fa2de1b79154 | 875 | 0x5B9CCA4F7763E373ull, 0x682E6FF3D6B2B8A3ull, 0x748F82EE5DEFB2FCull, |
Azure.IoT Build | 0:fa2de1b79154 | 876 | 0x78A5636F43172F60ull, 0x84C87814A1F0AB72ull, 0x8CC702081A6439ECull, |
Azure.IoT Build | 0:fa2de1b79154 | 877 | 0x90BEFFFA23631E28ull, 0xA4506CEBDE82BDE9ull, 0xBEF9A3F7B2C67915ull, |
Azure.IoT Build | 0:fa2de1b79154 | 878 | 0xC67178F2E372532Bull, 0xCA273ECEEA26619Cull, 0xD186B8C721C0C207ull, |
Azure.IoT Build | 0:fa2de1b79154 | 879 | 0xEADA7DD6CDE0EB1Eull, 0xF57D4F7FEE6ED178ull, 0x06F067AA72176FBAull, |
Azure.IoT Build | 0:fa2de1b79154 | 880 | 0x0A637DC5A2C898A6ull, 0x113F9804BEF90DAEull, 0x1B710B35131C471Bull, |
Azure.IoT Build | 0:fa2de1b79154 | 881 | 0x28DB77F523047D84ull, 0x32CAAB7B40C72493ull, 0x3C9EBE0A15C9BEBCull, |
Azure.IoT Build | 0:fa2de1b79154 | 882 | 0x431D67C49C100D4Cull, 0x4CC5D4BECB3E42B6ull, 0x597F299CFC657E2Aull, |
Azure.IoT Build | 0:fa2de1b79154 | 883 | 0x5FCB6FAB3AD6FAECull, 0x6C44198C4A475817ull |
Azure.IoT Build | 0:fa2de1b79154 | 884 | }; |
Azure.IoT Build | 0:fa2de1b79154 | 885 | int t, t8; /* Loop counter */ |
Azure.IoT Build | 0:fa2de1b79154 | 886 | uint64_t temp1, temp2; /* Temporary word value */ |
Azure.IoT Build | 0:fa2de1b79154 | 887 | uint64_t W[80]; /* Word sequence */ |
Azure.IoT Build | 0:fa2de1b79154 | 888 | uint64_t A, B, C, D, E, F, G, H; /* Word buffers */ |
Azure.IoT Build | 0:fa2de1b79154 | 889 | |
Azure.IoT Build | 0:fa2de1b79154 | 890 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 891 | * Initialize the first 16 words in the array W |
Azure.IoT Build | 0:fa2de1b79154 | 892 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 893 | for (t = t8 = 0; t < 16; t++, t8 += 8) |
Azure.IoT Build | 0:fa2de1b79154 | 894 | W[t] = ((uint64_t)(context->Message_Block[t8]) << 56) | |
Azure.IoT Build | 0:fa2de1b79154 | 895 | ((uint64_t)(context->Message_Block[t8 + 1]) << 48) | |
Azure.IoT Build | 0:fa2de1b79154 | 896 | ((uint64_t)(context->Message_Block[t8 + 2]) << 40) | |
Azure.IoT Build | 0:fa2de1b79154 | 897 | ((uint64_t)(context->Message_Block[t8 + 3]) << 32) | |
Azure.IoT Build | 0:fa2de1b79154 | 898 | ((uint64_t)(context->Message_Block[t8 + 4]) << 24) | |
Azure.IoT Build | 0:fa2de1b79154 | 899 | ((uint64_t)(context->Message_Block[t8 + 5]) << 16) | |
Azure.IoT Build | 0:fa2de1b79154 | 900 | ((uint64_t)(context->Message_Block[t8 + 6]) << 8) | |
Azure.IoT Build | 0:fa2de1b79154 | 901 | ((uint64_t)(context->Message_Block[t8 + 7])); |
Azure.IoT Build | 0:fa2de1b79154 | 902 | |
Azure.IoT Build | 0:fa2de1b79154 | 903 | for (t = 16; t < 80; t++) |
Azure.IoT Build | 0:fa2de1b79154 | 904 | W[t] = SHA512_sigma1(W[t - 2]) + W[t - 7] + |
Azure.IoT Build | 0:fa2de1b79154 | 905 | SHA512_sigma0(W[t - 15]) + W[t - 16]; |
Azure.IoT Build | 0:fa2de1b79154 | 906 | |
Azure.IoT Build | 0:fa2de1b79154 | 907 | A = context->Intermediate_Hash[0]; |
Azure.IoT Build | 0:fa2de1b79154 | 908 | B = context->Intermediate_Hash[1]; |
Azure.IoT Build | 0:fa2de1b79154 | 909 | C = context->Intermediate_Hash[2]; |
Azure.IoT Build | 0:fa2de1b79154 | 910 | D = context->Intermediate_Hash[3]; |
Azure.IoT Build | 0:fa2de1b79154 | 911 | E = context->Intermediate_Hash[4]; |
Azure.IoT Build | 0:fa2de1b79154 | 912 | F = context->Intermediate_Hash[5]; |
Azure.IoT Build | 0:fa2de1b79154 | 913 | G = context->Intermediate_Hash[6]; |
Azure.IoT Build | 0:fa2de1b79154 | 914 | H = context->Intermediate_Hash[7]; |
Azure.IoT Build | 0:fa2de1b79154 | 915 | |
Azure.IoT Build | 0:fa2de1b79154 | 916 | for (t = 0; t < 80; t++) { |
Azure.IoT Build | 0:fa2de1b79154 | 917 | temp1 = H + SHA512_SIGMA1(E) + SHA_Ch(E, F, G) + K[t] + W[t]; |
Azure.IoT Build | 0:fa2de1b79154 | 918 | temp2 = SHA512_SIGMA0(A) + SHA_Maj(A, B, C); |
Azure.IoT Build | 0:fa2de1b79154 | 919 | H = G; |
Azure.IoT Build | 0:fa2de1b79154 | 920 | G = F; |
Azure.IoT Build | 0:fa2de1b79154 | 921 | F = E; |
Azure.IoT Build | 0:fa2de1b79154 | 922 | E = D + temp1; |
Azure.IoT Build | 0:fa2de1b79154 | 923 | D = C; |
Azure.IoT Build | 0:fa2de1b79154 | 924 | C = B; |
Azure.IoT Build | 0:fa2de1b79154 | 925 | B = A; |
Azure.IoT Build | 0:fa2de1b79154 | 926 | A = temp1 + temp2; |
Azure.IoT Build | 0:fa2de1b79154 | 927 | } |
Azure.IoT Build | 0:fa2de1b79154 | 928 | |
Azure.IoT Build | 0:fa2de1b79154 | 929 | context->Intermediate_Hash[0] += A; |
Azure.IoT Build | 0:fa2de1b79154 | 930 | context->Intermediate_Hash[1] += B; |
Azure.IoT Build | 0:fa2de1b79154 | 931 | context->Intermediate_Hash[2] += C; |
Azure.IoT Build | 0:fa2de1b79154 | 932 | context->Intermediate_Hash[3] += D; |
Azure.IoT Build | 0:fa2de1b79154 | 933 | context->Intermediate_Hash[4] += E; |
Azure.IoT Build | 0:fa2de1b79154 | 934 | context->Intermediate_Hash[5] += F; |
Azure.IoT Build | 0:fa2de1b79154 | 935 | context->Intermediate_Hash[6] += G; |
Azure.IoT Build | 0:fa2de1b79154 | 936 | context->Intermediate_Hash[7] += H; |
Azure.IoT Build | 0:fa2de1b79154 | 937 | #endif /* USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 938 | |
Azure.IoT Build | 0:fa2de1b79154 | 939 | context->Message_Block_Index = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 940 | } |
Azure.IoT Build | 0:fa2de1b79154 | 941 | |
Azure.IoT Build | 0:fa2de1b79154 | 942 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 943 | * SHA384_512Reset |
Azure.IoT Build | 0:fa2de1b79154 | 944 | * |
Azure.IoT Build | 0:fa2de1b79154 | 945 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 946 | * This helper function will initialize the SHA512Context in |
Azure.IoT Build | 0:fa2de1b79154 | 947 | * preparation for computing a new SHA384 or SHA512 message |
Azure.IoT Build | 0:fa2de1b79154 | 948 | * digest. |
Azure.IoT Build | 0:fa2de1b79154 | 949 | * |
Azure.IoT Build | 0:fa2de1b79154 | 950 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 951 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 952 | * The context to reset. |
Azure.IoT Build | 0:fa2de1b79154 | 953 | * H0 |
Azure.IoT Build | 0:fa2de1b79154 | 954 | * The initial hash value to use. |
Azure.IoT Build | 0:fa2de1b79154 | 955 | * |
Azure.IoT Build | 0:fa2de1b79154 | 956 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 957 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 958 | * |
Azure.IoT Build | 0:fa2de1b79154 | 959 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 960 | #ifdef USE_32BIT_ONLY |
Azure.IoT Build | 0:fa2de1b79154 | 961 | static int SHA384_512Reset(SHA512Context *context, uint32_t H0[]) |
Azure.IoT Build | 0:fa2de1b79154 | 962 | #else /* !USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 963 | static int SHA384_512Reset(SHA512Context *context, uint64_t H0[]) |
Azure.IoT Build | 0:fa2de1b79154 | 964 | #endif /* USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 965 | { |
Azure.IoT Build | 0:fa2de1b79154 | 966 | int i; |
Azure.IoT Build | 0:fa2de1b79154 | 967 | if (!context) |
Azure.IoT Build | 0:fa2de1b79154 | 968 | return shaNull; |
Azure.IoT Build | 0:fa2de1b79154 | 969 | |
Azure.IoT Build | 0:fa2de1b79154 | 970 | context->Message_Block_Index = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 971 | |
Azure.IoT Build | 0:fa2de1b79154 | 972 | #ifdef USE_32BIT_ONLY |
Azure.IoT Build | 0:fa2de1b79154 | 973 | context->Length[0] = context->Length[1] = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 974 | context->Length[2] = context->Length[3] = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 975 | |
Azure.IoT Build | 0:fa2de1b79154 | 976 | for (i = 0; i < SHA512HashSize / 4; i++) |
Azure.IoT Build | 0:fa2de1b79154 | 977 | context->Intermediate_Hash[i] = H0[i]; |
Azure.IoT Build | 0:fa2de1b79154 | 978 | #else /* !USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 979 | context->Length_High = context->Length_Low = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 980 | |
Azure.IoT Build | 0:fa2de1b79154 | 981 | for (i = 0; i < SHA512HashSize / 8; i++) |
Azure.IoT Build | 0:fa2de1b79154 | 982 | context->Intermediate_Hash[i] = H0[i]; |
Azure.IoT Build | 0:fa2de1b79154 | 983 | #endif /* USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 984 | |
Azure.IoT Build | 0:fa2de1b79154 | 985 | context->Computed = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 986 | context->Corrupted = 0; |
Azure.IoT Build | 0:fa2de1b79154 | 987 | |
Azure.IoT Build | 0:fa2de1b79154 | 988 | return shaSuccess; |
Azure.IoT Build | 0:fa2de1b79154 | 989 | } |
Azure.IoT Build | 0:fa2de1b79154 | 990 | |
Azure.IoT Build | 0:fa2de1b79154 | 991 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 992 | * SHA384_512ResultN |
Azure.IoT Build | 0:fa2de1b79154 | 993 | * |
Azure.IoT Build | 0:fa2de1b79154 | 994 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 995 | * This helper function will return the 384-bit or 512-bit message |
Azure.IoT Build | 0:fa2de1b79154 | 996 | * digest into the Message_Digest array provided by the caller. |
Azure.IoT Build | 0:fa2de1b79154 | 997 | * NOTE: The first octet of hash is stored in the 0th element, |
Azure.IoT Build | 0:fa2de1b79154 | 998 | * the last octet of hash in the 48th/64th element. |
Azure.IoT Build | 0:fa2de1b79154 | 999 | * |
Azure.IoT Build | 0:fa2de1b79154 | 1000 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 1001 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 1002 | * The context to use to calculate the SHA hash. |
Azure.IoT Build | 0:fa2de1b79154 | 1003 | * Message_Digest: [out] |
Azure.IoT Build | 0:fa2de1b79154 | 1004 | * Where the digest is returned. |
Azure.IoT Build | 0:fa2de1b79154 | 1005 | * HashSize: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 1006 | * The size of the hash, either 48 or 64. |
Azure.IoT Build | 0:fa2de1b79154 | 1007 | * |
Azure.IoT Build | 0:fa2de1b79154 | 1008 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 1009 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 1010 | * |
Azure.IoT Build | 0:fa2de1b79154 | 1011 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 1012 | static int SHA384_512ResultN(SHA512Context *context, |
Azure.IoT Build | 0:fa2de1b79154 | 1013 | uint8_t Message_Digest[], int HashSize) |
Azure.IoT Build | 0:fa2de1b79154 | 1014 | { |
Azure.IoT Build | 0:fa2de1b79154 | 1015 | int i; |
Azure.IoT Build | 0:fa2de1b79154 | 1016 | |
Azure.IoT Build | 0:fa2de1b79154 | 1017 | #ifdef USE_32BIT_ONLY |
Azure.IoT Build | 0:fa2de1b79154 | 1018 | int i2; |
Azure.IoT Build | 0:fa2de1b79154 | 1019 | #endif /* USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 1020 | |
Azure.IoT Build | 0:fa2de1b79154 | 1021 | if (!context || !Message_Digest) |
Azure.IoT Build | 0:fa2de1b79154 | 1022 | return shaNull; |
Azure.IoT Build | 0:fa2de1b79154 | 1023 | |
Azure.IoT Build | 0:fa2de1b79154 | 1024 | if (context->Corrupted) |
Azure.IoT Build | 0:fa2de1b79154 | 1025 | return context->Corrupted; |
Azure.IoT Build | 0:fa2de1b79154 | 1026 | |
Azure.IoT Build | 0:fa2de1b79154 | 1027 | if (!context->Computed) |
Azure.IoT Build | 0:fa2de1b79154 | 1028 | SHA384_512Finalize(context, 0x80); |
Azure.IoT Build | 0:fa2de1b79154 | 1029 | |
Azure.IoT Build | 0:fa2de1b79154 | 1030 | #ifdef USE_32BIT_ONLY |
Azure.IoT Build | 0:fa2de1b79154 | 1031 | for (i = i2 = 0; i < HashSize;) { |
Azure.IoT Build | 0:fa2de1b79154 | 1032 | Message_Digest[i++] = (uint8_t)(context->Intermediate_Hash[i2] >> 24); |
Azure.IoT Build | 0:fa2de1b79154 | 1033 | Message_Digest[i++] = (uint8_t)(context->Intermediate_Hash[i2] >> 16); |
Azure.IoT Build | 0:fa2de1b79154 | 1034 | Message_Digest[i++] = (uint8_t)(context->Intermediate_Hash[i2] >> 8); |
Azure.IoT Build | 0:fa2de1b79154 | 1035 | Message_Digest[i++] = (uint8_t)(context->Intermediate_Hash[i2++]); |
Azure.IoT Build | 0:fa2de1b79154 | 1036 | Message_Digest[i++] = (uint8_t)(context->Intermediate_Hash[i2] >> 24); |
Azure.IoT Build | 0:fa2de1b79154 | 1037 | Message_Digest[i++] = (uint8_t)(context->Intermediate_Hash[i2] >> 16); |
Azure.IoT Build | 0:fa2de1b79154 | 1038 | Message_Digest[i++] = (uint8_t)(context->Intermediate_Hash[i2] >> 8); |
Azure.IoT Build | 0:fa2de1b79154 | 1039 | Message_Digest[i++] = (uint8_t)(context->Intermediate_Hash[i2++]); |
Azure.IoT Build | 0:fa2de1b79154 | 1040 | } |
Azure.IoT Build | 0:fa2de1b79154 | 1041 | #else /* !USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 1042 | for (i = 0; i < HashSize; ++i) |
Azure.IoT Build | 0:fa2de1b79154 | 1043 | Message_Digest[i] = (uint8_t) |
Azure.IoT Build | 0:fa2de1b79154 | 1044 | (context->Intermediate_Hash[i >> 3] >> 8 * (7 - (i % 8))); |
Azure.IoT Build | 0:fa2de1b79154 | 1045 | #endif /* USE_32BIT_ONLY */ |
Azure.IoT Build | 0:fa2de1b79154 | 1046 | |
Azure.IoT Build | 0:fa2de1b79154 | 1047 | return shaSuccess; |
Azure.IoT Build | 0:fa2de1b79154 | 1048 | } |
Azure.IoT Build | 0:fa2de1b79154 | 1049 |