WIP. send a large constant string twice a second, in order to test out the transport with something indicative of our required load.
Dependencies: FXOS8700CQ NTPClient azure_umqtt_c iothub_mqtt_transport mbed-rtos mbed wolfSSL Socket lwip-eth lwip-sys lwip
Fork of FXOS8700CQ_To_Azure_IoT by
azure_c_shared_utility/usha.c@7:0d1a0fe537dc, 2017-01-05 (annotated)
- Committer:
- julianhigginson
- Date:
- Thu Jan 05 23:40:24 2017 +0000
- Revision:
- 7:0d1a0fe537dc
- Parent:
- 3:c0556ff7b8e3
modified dummy message for minimal data transport
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 3:c0556ff7b8e3 | 1 | // Copyright (c) Microsoft. All rights reserved. |
markrad | 3:c0556ff7b8e3 | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
markrad | 3:c0556ff7b8e3 | 3 | |
markrad | 3:c0556ff7b8e3 | 4 | /**************************** usha.c ****************************/ |
markrad | 3:c0556ff7b8e3 | 5 | /******************** See RFC 4634 for details ******************/ |
markrad | 3:c0556ff7b8e3 | 6 | /* |
markrad | 3:c0556ff7b8e3 | 7 | * Description: |
markrad | 3:c0556ff7b8e3 | 8 | * This file implements a unified interface to the SHA algorithms. |
markrad | 3:c0556ff7b8e3 | 9 | */ |
markrad | 3:c0556ff7b8e3 | 10 | |
markrad | 3:c0556ff7b8e3 | 11 | #include <stdlib.h> |
markrad | 3:c0556ff7b8e3 | 12 | #ifdef _CRTDBG_MAP_ALLOC |
markrad | 3:c0556ff7b8e3 | 13 | #include <crtdbg.h> |
markrad | 3:c0556ff7b8e3 | 14 | #endif |
markrad | 3:c0556ff7b8e3 | 15 | #include "azure_c_shared_utility/gballoc.h" |
markrad | 3:c0556ff7b8e3 | 16 | |
markrad | 3:c0556ff7b8e3 | 17 | #include "azure_c_shared_utility/sha.h" |
markrad | 3:c0556ff7b8e3 | 18 | |
markrad | 3:c0556ff7b8e3 | 19 | /* |
markrad | 3:c0556ff7b8e3 | 20 | * USHAReset |
markrad | 3:c0556ff7b8e3 | 21 | * |
markrad | 3:c0556ff7b8e3 | 22 | * Description: |
markrad | 3:c0556ff7b8e3 | 23 | * This function will initialize the SHA Context in preparation |
markrad | 3:c0556ff7b8e3 | 24 | * for computing a new SHA message digest. |
markrad | 3:c0556ff7b8e3 | 25 | * |
markrad | 3:c0556ff7b8e3 | 26 | * Parameters: |
markrad | 3:c0556ff7b8e3 | 27 | * context: [in/out] |
markrad | 3:c0556ff7b8e3 | 28 | * The context to reset. |
markrad | 3:c0556ff7b8e3 | 29 | * whichSha: [in] |
markrad | 3:c0556ff7b8e3 | 30 | * Selects which SHA reset to call |
markrad | 3:c0556ff7b8e3 | 31 | * |
markrad | 3:c0556ff7b8e3 | 32 | * Returns: |
markrad | 3:c0556ff7b8e3 | 33 | * sha Error Code. |
markrad | 3:c0556ff7b8e3 | 34 | * |
markrad | 3:c0556ff7b8e3 | 35 | */ |
markrad | 3:c0556ff7b8e3 | 36 | int USHAReset(USHAContext *ctx, enum SHAversion whichSha) |
markrad | 3:c0556ff7b8e3 | 37 | { |
markrad | 3:c0556ff7b8e3 | 38 | if (ctx) { |
markrad | 3:c0556ff7b8e3 | 39 | ctx->whichSha = whichSha; |
markrad | 3:c0556ff7b8e3 | 40 | switch (whichSha) { |
markrad | 3:c0556ff7b8e3 | 41 | case SHA1: return SHA1Reset((SHA1Context*)&ctx->ctx); |
markrad | 3:c0556ff7b8e3 | 42 | case SHA224: return SHA224Reset((SHA224Context*)&ctx->ctx); |
markrad | 3:c0556ff7b8e3 | 43 | case SHA256: return SHA256Reset((SHA256Context*)&ctx->ctx); |
markrad | 3:c0556ff7b8e3 | 44 | case SHA384: return SHA384Reset((SHA384Context*)&ctx->ctx); |
markrad | 3:c0556ff7b8e3 | 45 | case SHA512: return SHA512Reset((SHA512Context*)&ctx->ctx); |
markrad | 3:c0556ff7b8e3 | 46 | default: return shaBadParam; |
markrad | 3:c0556ff7b8e3 | 47 | } |
markrad | 3:c0556ff7b8e3 | 48 | } |
markrad | 3:c0556ff7b8e3 | 49 | else { |
markrad | 3:c0556ff7b8e3 | 50 | return shaNull; |
markrad | 3:c0556ff7b8e3 | 51 | } |
markrad | 3:c0556ff7b8e3 | 52 | } |
markrad | 3:c0556ff7b8e3 | 53 | |
markrad | 3:c0556ff7b8e3 | 54 | /* |
markrad | 3:c0556ff7b8e3 | 55 | * USHAInput |
markrad | 3:c0556ff7b8e3 | 56 | * |
markrad | 3:c0556ff7b8e3 | 57 | * Description: |
markrad | 3:c0556ff7b8e3 | 58 | * This function accepts an array of octets as the next portion |
markrad | 3:c0556ff7b8e3 | 59 | * of the message. |
markrad | 3:c0556ff7b8e3 | 60 | * |
markrad | 3:c0556ff7b8e3 | 61 | * Parameters: |
markrad | 3:c0556ff7b8e3 | 62 | * context: [in/out] |
markrad | 3:c0556ff7b8e3 | 63 | * The SHA context to update |
markrad | 3:c0556ff7b8e3 | 64 | * message_array: [in] |
markrad | 3:c0556ff7b8e3 | 65 | * An array of characters representing the next portion of |
markrad | 3:c0556ff7b8e3 | 66 | * the message. |
markrad | 3:c0556ff7b8e3 | 67 | * length: [in] |
markrad | 3:c0556ff7b8e3 | 68 | * The length of the message in message_array |
markrad | 3:c0556ff7b8e3 | 69 | * |
markrad | 3:c0556ff7b8e3 | 70 | * Returns: |
markrad | 3:c0556ff7b8e3 | 71 | * sha Error Code. |
markrad | 3:c0556ff7b8e3 | 72 | * |
markrad | 3:c0556ff7b8e3 | 73 | */ |
markrad | 3:c0556ff7b8e3 | 74 | int USHAInput(USHAContext *ctx, |
markrad | 3:c0556ff7b8e3 | 75 | const uint8_t *bytes, unsigned int bytecount) |
markrad | 3:c0556ff7b8e3 | 76 | { |
markrad | 3:c0556ff7b8e3 | 77 | if (ctx) { |
markrad | 3:c0556ff7b8e3 | 78 | switch (ctx->whichSha) { |
markrad | 3:c0556ff7b8e3 | 79 | case SHA1: |
markrad | 3:c0556ff7b8e3 | 80 | return SHA1Input((SHA1Context*)&ctx->ctx, bytes, bytecount); |
markrad | 3:c0556ff7b8e3 | 81 | case SHA224: |
markrad | 3:c0556ff7b8e3 | 82 | return SHA224Input((SHA224Context*)&ctx->ctx, bytes, |
markrad | 3:c0556ff7b8e3 | 83 | bytecount); |
markrad | 3:c0556ff7b8e3 | 84 | case SHA256: |
markrad | 3:c0556ff7b8e3 | 85 | return SHA256Input((SHA256Context*)&ctx->ctx, bytes, |
markrad | 3:c0556ff7b8e3 | 86 | bytecount); |
markrad | 3:c0556ff7b8e3 | 87 | case SHA384: |
markrad | 3:c0556ff7b8e3 | 88 | return SHA384Input((SHA384Context*)&ctx->ctx, bytes, |
markrad | 3:c0556ff7b8e3 | 89 | bytecount); |
markrad | 3:c0556ff7b8e3 | 90 | case SHA512: |
markrad | 3:c0556ff7b8e3 | 91 | return SHA512Input((SHA512Context*)&ctx->ctx, bytes, |
markrad | 3:c0556ff7b8e3 | 92 | bytecount); |
markrad | 3:c0556ff7b8e3 | 93 | default: return shaBadParam; |
markrad | 3:c0556ff7b8e3 | 94 | } |
markrad | 3:c0556ff7b8e3 | 95 | } |
markrad | 3:c0556ff7b8e3 | 96 | else { |
markrad | 3:c0556ff7b8e3 | 97 | return shaNull; |
markrad | 3:c0556ff7b8e3 | 98 | } |
markrad | 3:c0556ff7b8e3 | 99 | } |
markrad | 3:c0556ff7b8e3 | 100 | |
markrad | 3:c0556ff7b8e3 | 101 | /* |
markrad | 3:c0556ff7b8e3 | 102 | * USHAFinalBits |
markrad | 3:c0556ff7b8e3 | 103 | * |
markrad | 3:c0556ff7b8e3 | 104 | * Description: |
markrad | 3:c0556ff7b8e3 | 105 | * This function will add in any final bits of the message. |
markrad | 3:c0556ff7b8e3 | 106 | * |
markrad | 3:c0556ff7b8e3 | 107 | * Parameters: |
markrad | 3:c0556ff7b8e3 | 108 | * context: [in/out] |
markrad | 3:c0556ff7b8e3 | 109 | * The SHA context to update |
markrad | 3:c0556ff7b8e3 | 110 | * message_bits: [in] |
markrad | 3:c0556ff7b8e3 | 111 | * The final bits of the message, in the upper portion of the |
markrad | 3:c0556ff7b8e3 | 112 | * byte. (Use 0b###00000 instead of 0b00000### to input the |
markrad | 3:c0556ff7b8e3 | 113 | * three bits ###.) |
markrad | 3:c0556ff7b8e3 | 114 | * length: [in] |
markrad | 3:c0556ff7b8e3 | 115 | * The number of bits in message_bits, between 1 and 7. |
markrad | 3:c0556ff7b8e3 | 116 | * |
markrad | 3:c0556ff7b8e3 | 117 | * Returns: |
markrad | 3:c0556ff7b8e3 | 118 | * sha Error Code. |
markrad | 3:c0556ff7b8e3 | 119 | */ |
markrad | 3:c0556ff7b8e3 | 120 | int USHAFinalBits(USHAContext *ctx, |
markrad | 3:c0556ff7b8e3 | 121 | const uint8_t bits, unsigned int bitcount) |
markrad | 3:c0556ff7b8e3 | 122 | { |
markrad | 3:c0556ff7b8e3 | 123 | if (ctx) { |
markrad | 3:c0556ff7b8e3 | 124 | switch (ctx->whichSha) { |
markrad | 3:c0556ff7b8e3 | 125 | case SHA1: |
markrad | 3:c0556ff7b8e3 | 126 | return SHA1FinalBits((SHA1Context*)&ctx->ctx, bits, bitcount); |
markrad | 3:c0556ff7b8e3 | 127 | case SHA224: |
markrad | 3:c0556ff7b8e3 | 128 | return SHA224FinalBits((SHA224Context*)&ctx->ctx, bits, |
markrad | 3:c0556ff7b8e3 | 129 | bitcount); |
markrad | 3:c0556ff7b8e3 | 130 | case SHA256: |
markrad | 3:c0556ff7b8e3 | 131 | return SHA256FinalBits((SHA256Context*)&ctx->ctx, bits, |
markrad | 3:c0556ff7b8e3 | 132 | bitcount); |
markrad | 3:c0556ff7b8e3 | 133 | case SHA384: |
markrad | 3:c0556ff7b8e3 | 134 | return SHA384FinalBits((SHA384Context*)&ctx->ctx, bits, |
markrad | 3:c0556ff7b8e3 | 135 | bitcount); |
markrad | 3:c0556ff7b8e3 | 136 | case SHA512: |
markrad | 3:c0556ff7b8e3 | 137 | return SHA512FinalBits((SHA512Context*)&ctx->ctx, bits, |
markrad | 3:c0556ff7b8e3 | 138 | bitcount); |
markrad | 3:c0556ff7b8e3 | 139 | default: return shaBadParam; |
markrad | 3:c0556ff7b8e3 | 140 | } |
markrad | 3:c0556ff7b8e3 | 141 | } |
markrad | 3:c0556ff7b8e3 | 142 | else { |
markrad | 3:c0556ff7b8e3 | 143 | return shaNull; |
markrad | 3:c0556ff7b8e3 | 144 | } |
markrad | 3:c0556ff7b8e3 | 145 | } |
markrad | 3:c0556ff7b8e3 | 146 | |
markrad | 3:c0556ff7b8e3 | 147 | /* |
markrad | 3:c0556ff7b8e3 | 148 | * USHAResult |
markrad | 3:c0556ff7b8e3 | 149 | * |
markrad | 3:c0556ff7b8e3 | 150 | * Description: |
markrad | 3:c0556ff7b8e3 | 151 | * This function will return the 160-bit message digest into the |
markrad | 3:c0556ff7b8e3 | 152 | * Message_Digest array provided by the caller. |
markrad | 3:c0556ff7b8e3 | 153 | * NOTE: The first octet of hash is stored in the 0th element, |
markrad | 3:c0556ff7b8e3 | 154 | * the last octet of hash in the 19th element. |
markrad | 3:c0556ff7b8e3 | 155 | * |
markrad | 3:c0556ff7b8e3 | 156 | * Parameters: |
markrad | 3:c0556ff7b8e3 | 157 | * context: [in/out] |
markrad | 3:c0556ff7b8e3 | 158 | * The context to use to calculate the SHA-1 hash. |
markrad | 3:c0556ff7b8e3 | 159 | * Message_Digest: [out] |
markrad | 3:c0556ff7b8e3 | 160 | * Where the digest is returned. |
markrad | 3:c0556ff7b8e3 | 161 | * |
markrad | 3:c0556ff7b8e3 | 162 | * Returns: |
markrad | 3:c0556ff7b8e3 | 163 | * sha Error Code. |
markrad | 3:c0556ff7b8e3 | 164 | * |
markrad | 3:c0556ff7b8e3 | 165 | */ |
markrad | 3:c0556ff7b8e3 | 166 | int USHAResult(USHAContext *ctx, |
markrad | 3:c0556ff7b8e3 | 167 | uint8_t Message_Digest[USHAMaxHashSize]) |
markrad | 3:c0556ff7b8e3 | 168 | { |
markrad | 3:c0556ff7b8e3 | 169 | if (ctx) { |
markrad | 3:c0556ff7b8e3 | 170 | switch (ctx->whichSha) { |
markrad | 3:c0556ff7b8e3 | 171 | case SHA1: |
markrad | 3:c0556ff7b8e3 | 172 | return SHA1Result((SHA1Context*)&ctx->ctx, Message_Digest); |
markrad | 3:c0556ff7b8e3 | 173 | case SHA224: |
markrad | 3:c0556ff7b8e3 | 174 | return SHA224Result((SHA224Context*)&ctx->ctx, Message_Digest); |
markrad | 3:c0556ff7b8e3 | 175 | case SHA256: |
markrad | 3:c0556ff7b8e3 | 176 | return SHA256Result((SHA256Context*)&ctx->ctx, Message_Digest); |
markrad | 3:c0556ff7b8e3 | 177 | case SHA384: |
markrad | 3:c0556ff7b8e3 | 178 | return SHA384Result((SHA384Context*)&ctx->ctx, Message_Digest); |
markrad | 3:c0556ff7b8e3 | 179 | case SHA512: |
markrad | 3:c0556ff7b8e3 | 180 | return SHA512Result((SHA512Context*)&ctx->ctx, Message_Digest); |
markrad | 3:c0556ff7b8e3 | 181 | default: return shaBadParam; |
markrad | 3:c0556ff7b8e3 | 182 | } |
markrad | 3:c0556ff7b8e3 | 183 | } |
markrad | 3:c0556ff7b8e3 | 184 | else { |
markrad | 3:c0556ff7b8e3 | 185 | return shaNull; |
markrad | 3:c0556ff7b8e3 | 186 | } |
markrad | 3:c0556ff7b8e3 | 187 | } |
markrad | 3:c0556ff7b8e3 | 188 | |
markrad | 3:c0556ff7b8e3 | 189 | /* |
markrad | 3:c0556ff7b8e3 | 190 | * USHABlockSize |
markrad | 3:c0556ff7b8e3 | 191 | * |
markrad | 3:c0556ff7b8e3 | 192 | * Description: |
markrad | 3:c0556ff7b8e3 | 193 | * This function will return the blocksize for the given SHA |
markrad | 3:c0556ff7b8e3 | 194 | * algorithm. |
markrad | 3:c0556ff7b8e3 | 195 | * |
markrad | 3:c0556ff7b8e3 | 196 | * Parameters: |
markrad | 3:c0556ff7b8e3 | 197 | * whichSha: |
markrad | 3:c0556ff7b8e3 | 198 | * which SHA algorithm to query |
markrad | 3:c0556ff7b8e3 | 199 | * |
markrad | 3:c0556ff7b8e3 | 200 | * Returns: |
markrad | 3:c0556ff7b8e3 | 201 | * block size |
markrad | 3:c0556ff7b8e3 | 202 | * |
markrad | 3:c0556ff7b8e3 | 203 | */ |
markrad | 3:c0556ff7b8e3 | 204 | int USHABlockSize(enum SHAversion whichSha) |
markrad | 3:c0556ff7b8e3 | 205 | { |
markrad | 3:c0556ff7b8e3 | 206 | switch (whichSha) { |
markrad | 3:c0556ff7b8e3 | 207 | case SHA1: return SHA1_Message_Block_Size; |
markrad | 3:c0556ff7b8e3 | 208 | case SHA224: return SHA224_Message_Block_Size; |
markrad | 3:c0556ff7b8e3 | 209 | case SHA256: return SHA256_Message_Block_Size; |
markrad | 3:c0556ff7b8e3 | 210 | case SHA384: return SHA384_Message_Block_Size; |
markrad | 3:c0556ff7b8e3 | 211 | default: |
markrad | 3:c0556ff7b8e3 | 212 | case SHA512: return SHA512_Message_Block_Size; |
markrad | 3:c0556ff7b8e3 | 213 | } |
markrad | 3:c0556ff7b8e3 | 214 | } |
markrad | 3:c0556ff7b8e3 | 215 | |
markrad | 3:c0556ff7b8e3 | 216 | /* |
markrad | 3:c0556ff7b8e3 | 217 | * USHAHashSize |
markrad | 3:c0556ff7b8e3 | 218 | * |
markrad | 3:c0556ff7b8e3 | 219 | * Description: |
markrad | 3:c0556ff7b8e3 | 220 | * This function will return the hashsize for the given SHA |
markrad | 3:c0556ff7b8e3 | 221 | * algorithm. |
markrad | 3:c0556ff7b8e3 | 222 | * |
markrad | 3:c0556ff7b8e3 | 223 | * Parameters: |
markrad | 3:c0556ff7b8e3 | 224 | * whichSha: |
markrad | 3:c0556ff7b8e3 | 225 | * which SHA algorithm to query |
markrad | 3:c0556ff7b8e3 | 226 | * |
markrad | 3:c0556ff7b8e3 | 227 | * Returns: |
markrad | 3:c0556ff7b8e3 | 228 | * hash size |
markrad | 3:c0556ff7b8e3 | 229 | * |
markrad | 3:c0556ff7b8e3 | 230 | */ |
markrad | 3:c0556ff7b8e3 | 231 | int USHAHashSize(enum SHAversion whichSha) |
markrad | 3:c0556ff7b8e3 | 232 | { |
markrad | 3:c0556ff7b8e3 | 233 | switch (whichSha) { |
markrad | 3:c0556ff7b8e3 | 234 | case SHA1: return SHA1HashSize; |
markrad | 3:c0556ff7b8e3 | 235 | case SHA224: return SHA224HashSize; |
markrad | 3:c0556ff7b8e3 | 236 | case SHA256: return SHA256HashSize; |
markrad | 3:c0556ff7b8e3 | 237 | case SHA384: return SHA384HashSize; |
markrad | 3:c0556ff7b8e3 | 238 | default: |
markrad | 3:c0556ff7b8e3 | 239 | case SHA512: return SHA512HashSize; |
markrad | 3:c0556ff7b8e3 | 240 | } |
markrad | 3:c0556ff7b8e3 | 241 | } |
markrad | 3:c0556ff7b8e3 | 242 | |
markrad | 3:c0556ff7b8e3 | 243 | /* |
markrad | 3:c0556ff7b8e3 | 244 | * USHAHashSizeBits |
markrad | 3:c0556ff7b8e3 | 245 | * |
markrad | 3:c0556ff7b8e3 | 246 | * Description: |
markrad | 3:c0556ff7b8e3 | 247 | * This function will return the hashsize for the given SHA |
markrad | 3:c0556ff7b8e3 | 248 | * algorithm, expressed in bits. |
markrad | 3:c0556ff7b8e3 | 249 | * |
markrad | 3:c0556ff7b8e3 | 250 | * Parameters: |
markrad | 3:c0556ff7b8e3 | 251 | * whichSha: |
markrad | 3:c0556ff7b8e3 | 252 | * which SHA algorithm to query |
markrad | 3:c0556ff7b8e3 | 253 | * |
markrad | 3:c0556ff7b8e3 | 254 | * Returns: |
markrad | 3:c0556ff7b8e3 | 255 | * hash size in bits |
markrad | 3:c0556ff7b8e3 | 256 | * |
markrad | 3:c0556ff7b8e3 | 257 | */ |
markrad | 3:c0556ff7b8e3 | 258 | int USHAHashSizeBits(enum SHAversion whichSha) |
markrad | 3:c0556ff7b8e3 | 259 | { |
markrad | 3:c0556ff7b8e3 | 260 | switch (whichSha) { |
markrad | 3:c0556ff7b8e3 | 261 | case SHA1: return SHA1HashSizeBits; |
markrad | 3:c0556ff7b8e3 | 262 | case SHA224: return SHA224HashSizeBits; |
markrad | 3:c0556ff7b8e3 | 263 | case SHA256: return SHA256HashSizeBits; |
markrad | 3:c0556ff7b8e3 | 264 | case SHA384: return SHA384HashSizeBits; |
markrad | 3:c0556ff7b8e3 | 265 | default: |
markrad | 3:c0556ff7b8e3 | 266 | case SHA512: return SHA512HashSizeBits; |
markrad | 3:c0556ff7b8e3 | 267 | } |
markrad | 3:c0556ff7b8e3 | 268 | } |
markrad | 3:c0556ff7b8e3 | 269 |