Azure IoT common library
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
usha.c@49:6bb8b9a66642, 2018-10-04 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Thu Oct 04 09:17:16 2018 -0700
- Revision:
- 49:6bb8b9a66642
- Parent:
- 19:2e0811512ceb
1.2.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 | /**************************** usha.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 a unified interface to the SHA algorithms. |
Azure.IoT Build | 0:fa2de1b79154 | 9 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 10 | |
Azure.IoT Build | 0:fa2de1b79154 | 11 | #include "azure_c_shared_utility/sha.h" |
Azure.IoT Build | 0:fa2de1b79154 | 12 | |
Azure.IoT Build | 0:fa2de1b79154 | 13 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 14 | * USHAReset |
Azure.IoT Build | 0:fa2de1b79154 | 15 | * |
Azure.IoT Build | 0:fa2de1b79154 | 16 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 17 | * This function will initialize the SHA Context in preparation |
Azure.IoT Build | 0:fa2de1b79154 | 18 | * for computing a new SHA message digest. |
Azure.IoT Build | 0:fa2de1b79154 | 19 | * |
Azure.IoT Build | 0:fa2de1b79154 | 20 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 21 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 22 | * The context to reset. |
Azure.IoT Build | 0:fa2de1b79154 | 23 | * whichSha: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 24 | * Selects which SHA reset to call |
Azure.IoT Build | 0:fa2de1b79154 | 25 | * |
Azure.IoT Build | 0:fa2de1b79154 | 26 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 27 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 28 | * |
Azure.IoT Build | 0:fa2de1b79154 | 29 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 30 | int USHAReset(USHAContext *ctx, enum SHAversion whichSha) |
Azure.IoT Build | 0:fa2de1b79154 | 31 | { |
Azure.IoT Build | 0:fa2de1b79154 | 32 | if (ctx) { |
Azure.IoT Build | 0:fa2de1b79154 | 33 | ctx->whichSha = whichSha; |
Azure.IoT Build | 0:fa2de1b79154 | 34 | switch (whichSha) { |
Azure.IoT Build | 0:fa2de1b79154 | 35 | case SHA1: return SHA1Reset((SHA1Context*)&ctx->ctx); |
Azure.IoT Build | 0:fa2de1b79154 | 36 | case SHA224: return SHA224Reset((SHA224Context*)&ctx->ctx); |
Azure.IoT Build | 0:fa2de1b79154 | 37 | case SHA256: return SHA256Reset((SHA256Context*)&ctx->ctx); |
Azure.IoT Build | 0:fa2de1b79154 | 38 | case SHA384: return SHA384Reset((SHA384Context*)&ctx->ctx); |
Azure.IoT Build | 0:fa2de1b79154 | 39 | case SHA512: return SHA512Reset((SHA512Context*)&ctx->ctx); |
Azure.IoT Build | 0:fa2de1b79154 | 40 | default: return shaBadParam; |
Azure.IoT Build | 0:fa2de1b79154 | 41 | } |
Azure.IoT Build | 0:fa2de1b79154 | 42 | } |
Azure.IoT Build | 0:fa2de1b79154 | 43 | else { |
Azure.IoT Build | 0:fa2de1b79154 | 44 | return shaNull; |
Azure.IoT Build | 0:fa2de1b79154 | 45 | } |
Azure.IoT Build | 0:fa2de1b79154 | 46 | } |
Azure.IoT Build | 0:fa2de1b79154 | 47 | |
Azure.IoT Build | 0:fa2de1b79154 | 48 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 49 | * USHAInput |
Azure.IoT Build | 0:fa2de1b79154 | 50 | * |
Azure.IoT Build | 0:fa2de1b79154 | 51 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 52 | * This function accepts an array of octets as the next portion |
Azure.IoT Build | 0:fa2de1b79154 | 53 | * of the message. |
Azure.IoT Build | 0:fa2de1b79154 | 54 | * |
Azure.IoT Build | 0:fa2de1b79154 | 55 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 56 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 57 | * The SHA context to update |
Azure.IoT Build | 0:fa2de1b79154 | 58 | * message_array: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 59 | * An array of characters representing the next portion of |
Azure.IoT Build | 0:fa2de1b79154 | 60 | * the message. |
Azure.IoT Build | 0:fa2de1b79154 | 61 | * length: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 62 | * The length of the message in message_array |
Azure.IoT Build | 0:fa2de1b79154 | 63 | * |
Azure.IoT Build | 0:fa2de1b79154 | 64 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 65 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 66 | * |
Azure.IoT Build | 0:fa2de1b79154 | 67 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 68 | int USHAInput(USHAContext *ctx, |
Azure.IoT Build | 0:fa2de1b79154 | 69 | const uint8_t *bytes, unsigned int bytecount) |
Azure.IoT Build | 0:fa2de1b79154 | 70 | { |
Azure.IoT Build | 0:fa2de1b79154 | 71 | if (ctx) { |
Azure.IoT Build | 0:fa2de1b79154 | 72 | switch (ctx->whichSha) { |
Azure.IoT Build | 0:fa2de1b79154 | 73 | case SHA1: |
Azure.IoT Build | 0:fa2de1b79154 | 74 | return SHA1Input((SHA1Context*)&ctx->ctx, bytes, bytecount); |
Azure.IoT Build | 0:fa2de1b79154 | 75 | case SHA224: |
Azure.IoT Build | 0:fa2de1b79154 | 76 | return SHA224Input((SHA224Context*)&ctx->ctx, bytes, |
Azure.IoT Build | 0:fa2de1b79154 | 77 | bytecount); |
Azure.IoT Build | 0:fa2de1b79154 | 78 | case SHA256: |
Azure.IoT Build | 0:fa2de1b79154 | 79 | return SHA256Input((SHA256Context*)&ctx->ctx, bytes, |
Azure.IoT Build | 0:fa2de1b79154 | 80 | bytecount); |
Azure.IoT Build | 0:fa2de1b79154 | 81 | case SHA384: |
Azure.IoT Build | 0:fa2de1b79154 | 82 | return SHA384Input((SHA384Context*)&ctx->ctx, bytes, |
Azure.IoT Build | 0:fa2de1b79154 | 83 | bytecount); |
Azure.IoT Build | 0:fa2de1b79154 | 84 | case SHA512: |
Azure.IoT Build | 0:fa2de1b79154 | 85 | return SHA512Input((SHA512Context*)&ctx->ctx, bytes, |
Azure.IoT Build | 0:fa2de1b79154 | 86 | bytecount); |
Azure.IoT Build | 0:fa2de1b79154 | 87 | default: return shaBadParam; |
Azure.IoT Build | 0:fa2de1b79154 | 88 | } |
Azure.IoT Build | 0:fa2de1b79154 | 89 | } |
Azure.IoT Build | 0:fa2de1b79154 | 90 | else { |
Azure.IoT Build | 0:fa2de1b79154 | 91 | return shaNull; |
Azure.IoT Build | 0:fa2de1b79154 | 92 | } |
Azure.IoT Build | 0:fa2de1b79154 | 93 | } |
Azure.IoT Build | 0:fa2de1b79154 | 94 | |
Azure.IoT Build | 0:fa2de1b79154 | 95 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 96 | * USHAFinalBits |
Azure.IoT Build | 0:fa2de1b79154 | 97 | * |
Azure.IoT Build | 0:fa2de1b79154 | 98 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 99 | * This function will add in any final bits of the message. |
Azure.IoT Build | 0:fa2de1b79154 | 100 | * |
Azure.IoT Build | 0:fa2de1b79154 | 101 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 102 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 103 | * The SHA context to update |
Azure.IoT Build | 0:fa2de1b79154 | 104 | * message_bits: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 105 | * The final bits of the message, in the upper portion of the |
Azure.IoT Build | 0:fa2de1b79154 | 106 | * byte. (Use 0b###00000 instead of 0b00000### to input the |
Azure.IoT Build | 0:fa2de1b79154 | 107 | * three bits ###.) |
Azure.IoT Build | 0:fa2de1b79154 | 108 | * length: [in] |
Azure.IoT Build | 0:fa2de1b79154 | 109 | * The number of bits in message_bits, between 1 and 7. |
Azure.IoT Build | 0:fa2de1b79154 | 110 | * |
Azure.IoT Build | 0:fa2de1b79154 | 111 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 112 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 113 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 114 | int USHAFinalBits(USHAContext *ctx, |
Azure.IoT Build | 0:fa2de1b79154 | 115 | const uint8_t bits, unsigned int bitcount) |
Azure.IoT Build | 0:fa2de1b79154 | 116 | { |
Azure.IoT Build | 0:fa2de1b79154 | 117 | if (ctx) { |
Azure.IoT Build | 0:fa2de1b79154 | 118 | switch (ctx->whichSha) { |
Azure.IoT Build | 0:fa2de1b79154 | 119 | case SHA1: |
Azure.IoT Build | 0:fa2de1b79154 | 120 | return SHA1FinalBits((SHA1Context*)&ctx->ctx, bits, bitcount); |
Azure.IoT Build | 0:fa2de1b79154 | 121 | case SHA224: |
Azure.IoT Build | 0:fa2de1b79154 | 122 | return SHA224FinalBits((SHA224Context*)&ctx->ctx, bits, |
Azure.IoT Build | 0:fa2de1b79154 | 123 | bitcount); |
Azure.IoT Build | 0:fa2de1b79154 | 124 | case SHA256: |
Azure.IoT Build | 0:fa2de1b79154 | 125 | return SHA256FinalBits((SHA256Context*)&ctx->ctx, bits, |
Azure.IoT Build | 0:fa2de1b79154 | 126 | bitcount); |
Azure.IoT Build | 0:fa2de1b79154 | 127 | case SHA384: |
Azure.IoT Build | 0:fa2de1b79154 | 128 | return SHA384FinalBits((SHA384Context*)&ctx->ctx, bits, |
Azure.IoT Build | 0:fa2de1b79154 | 129 | bitcount); |
Azure.IoT Build | 0:fa2de1b79154 | 130 | case SHA512: |
Azure.IoT Build | 0:fa2de1b79154 | 131 | return SHA512FinalBits((SHA512Context*)&ctx->ctx, bits, |
Azure.IoT Build | 0:fa2de1b79154 | 132 | bitcount); |
Azure.IoT Build | 0:fa2de1b79154 | 133 | default: return shaBadParam; |
Azure.IoT Build | 0:fa2de1b79154 | 134 | } |
Azure.IoT Build | 0:fa2de1b79154 | 135 | } |
Azure.IoT Build | 0:fa2de1b79154 | 136 | else { |
Azure.IoT Build | 0:fa2de1b79154 | 137 | return shaNull; |
Azure.IoT Build | 0:fa2de1b79154 | 138 | } |
Azure.IoT Build | 0:fa2de1b79154 | 139 | } |
Azure.IoT Build | 0:fa2de1b79154 | 140 | |
Azure.IoT Build | 0:fa2de1b79154 | 141 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 142 | * USHAResult |
Azure.IoT Build | 0:fa2de1b79154 | 143 | * |
Azure.IoT Build | 0:fa2de1b79154 | 144 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 145 | * This function will return the 160-bit message digest into the |
Azure.IoT Build | 0:fa2de1b79154 | 146 | * Message_Digest array provided by the caller. |
Azure.IoT Build | 0:fa2de1b79154 | 147 | * NOTE: The first octet of hash is stored in the 0th element, |
Azure.IoT Build | 0:fa2de1b79154 | 148 | * the last octet of hash in the 19th element. |
Azure.IoT Build | 0:fa2de1b79154 | 149 | * |
Azure.IoT Build | 0:fa2de1b79154 | 150 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 151 | * context: [in/out] |
Azure.IoT Build | 0:fa2de1b79154 | 152 | * The context to use to calculate the SHA-1 hash. |
Azure.IoT Build | 0:fa2de1b79154 | 153 | * Message_Digest: [out] |
Azure.IoT Build | 0:fa2de1b79154 | 154 | * Where the digest is returned. |
Azure.IoT Build | 0:fa2de1b79154 | 155 | * |
Azure.IoT Build | 0:fa2de1b79154 | 156 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 157 | * sha Error Code. |
Azure.IoT Build | 0:fa2de1b79154 | 158 | * |
Azure.IoT Build | 0:fa2de1b79154 | 159 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 160 | int USHAResult(USHAContext *ctx, |
Azure.IoT Build | 0:fa2de1b79154 | 161 | uint8_t Message_Digest[USHAMaxHashSize]) |
Azure.IoT Build | 0:fa2de1b79154 | 162 | { |
Azure.IoT Build | 0:fa2de1b79154 | 163 | if (ctx) { |
Azure.IoT Build | 0:fa2de1b79154 | 164 | switch (ctx->whichSha) { |
Azure.IoT Build | 0:fa2de1b79154 | 165 | case SHA1: |
Azure.IoT Build | 0:fa2de1b79154 | 166 | return SHA1Result((SHA1Context*)&ctx->ctx, Message_Digest); |
Azure.IoT Build | 0:fa2de1b79154 | 167 | case SHA224: |
Azure.IoT Build | 0:fa2de1b79154 | 168 | return SHA224Result((SHA224Context*)&ctx->ctx, Message_Digest); |
Azure.IoT Build | 0:fa2de1b79154 | 169 | case SHA256: |
Azure.IoT Build | 0:fa2de1b79154 | 170 | return SHA256Result((SHA256Context*)&ctx->ctx, Message_Digest); |
Azure.IoT Build | 0:fa2de1b79154 | 171 | case SHA384: |
Azure.IoT Build | 0:fa2de1b79154 | 172 | return SHA384Result((SHA384Context*)&ctx->ctx, Message_Digest); |
Azure.IoT Build | 0:fa2de1b79154 | 173 | case SHA512: |
Azure.IoT Build | 0:fa2de1b79154 | 174 | return SHA512Result((SHA512Context*)&ctx->ctx, Message_Digest); |
Azure.IoT Build | 0:fa2de1b79154 | 175 | default: return shaBadParam; |
Azure.IoT Build | 0:fa2de1b79154 | 176 | } |
Azure.IoT Build | 0:fa2de1b79154 | 177 | } |
Azure.IoT Build | 0:fa2de1b79154 | 178 | else { |
Azure.IoT Build | 0:fa2de1b79154 | 179 | return shaNull; |
Azure.IoT Build | 0:fa2de1b79154 | 180 | } |
Azure.IoT Build | 0:fa2de1b79154 | 181 | } |
Azure.IoT Build | 0:fa2de1b79154 | 182 | |
Azure.IoT Build | 0:fa2de1b79154 | 183 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 184 | * USHABlockSize |
Azure.IoT Build | 0:fa2de1b79154 | 185 | * |
Azure.IoT Build | 0:fa2de1b79154 | 186 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 187 | * This function will return the blocksize for the given SHA |
Azure.IoT Build | 0:fa2de1b79154 | 188 | * algorithm. |
Azure.IoT Build | 0:fa2de1b79154 | 189 | * |
Azure.IoT Build | 0:fa2de1b79154 | 190 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 191 | * whichSha: |
Azure.IoT Build | 0:fa2de1b79154 | 192 | * which SHA algorithm to query |
Azure.IoT Build | 0:fa2de1b79154 | 193 | * |
Azure.IoT Build | 0:fa2de1b79154 | 194 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 195 | * block size |
Azure.IoT Build | 0:fa2de1b79154 | 196 | * |
Azure.IoT Build | 0:fa2de1b79154 | 197 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 198 | int USHABlockSize(enum SHAversion whichSha) |
Azure.IoT Build | 0:fa2de1b79154 | 199 | { |
Azure.IoT Build | 0:fa2de1b79154 | 200 | switch (whichSha) { |
Azure.IoT Build | 0:fa2de1b79154 | 201 | case SHA1: return SHA1_Message_Block_Size; |
Azure.IoT Build | 0:fa2de1b79154 | 202 | case SHA224: return SHA224_Message_Block_Size; |
Azure.IoT Build | 0:fa2de1b79154 | 203 | case SHA256: return SHA256_Message_Block_Size; |
Azure.IoT Build | 0:fa2de1b79154 | 204 | case SHA384: return SHA384_Message_Block_Size; |
Azure.IoT Build | 0:fa2de1b79154 | 205 | default: |
Azure.IoT Build | 0:fa2de1b79154 | 206 | case SHA512: return SHA512_Message_Block_Size; |
Azure.IoT Build | 0:fa2de1b79154 | 207 | } |
Azure.IoT Build | 0:fa2de1b79154 | 208 | } |
Azure.IoT Build | 0:fa2de1b79154 | 209 | |
Azure.IoT Build | 0:fa2de1b79154 | 210 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 211 | * USHAHashSize |
Azure.IoT Build | 0:fa2de1b79154 | 212 | * |
Azure.IoT Build | 0:fa2de1b79154 | 213 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 214 | * This function will return the hashsize for the given SHA |
Azure.IoT Build | 0:fa2de1b79154 | 215 | * algorithm. |
Azure.IoT Build | 0:fa2de1b79154 | 216 | * |
Azure.IoT Build | 0:fa2de1b79154 | 217 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 218 | * whichSha: |
Azure.IoT Build | 0:fa2de1b79154 | 219 | * which SHA algorithm to query |
Azure.IoT Build | 0:fa2de1b79154 | 220 | * |
Azure.IoT Build | 0:fa2de1b79154 | 221 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 222 | * hash size |
Azure.IoT Build | 0:fa2de1b79154 | 223 | * |
Azure.IoT Build | 0:fa2de1b79154 | 224 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 225 | int USHAHashSize(enum SHAversion whichSha) |
Azure.IoT Build | 0:fa2de1b79154 | 226 | { |
Azure.IoT Build | 0:fa2de1b79154 | 227 | switch (whichSha) { |
Azure.IoT Build | 0:fa2de1b79154 | 228 | case SHA1: return SHA1HashSize; |
Azure.IoT Build | 0:fa2de1b79154 | 229 | case SHA224: return SHA224HashSize; |
Azure.IoT Build | 0:fa2de1b79154 | 230 | case SHA256: return SHA256HashSize; |
Azure.IoT Build | 0:fa2de1b79154 | 231 | case SHA384: return SHA384HashSize; |
Azure.IoT Build | 0:fa2de1b79154 | 232 | default: |
Azure.IoT Build | 0:fa2de1b79154 | 233 | case SHA512: return SHA512HashSize; |
Azure.IoT Build | 0:fa2de1b79154 | 234 | } |
Azure.IoT Build | 0:fa2de1b79154 | 235 | } |
Azure.IoT Build | 0:fa2de1b79154 | 236 | |
Azure.IoT Build | 0:fa2de1b79154 | 237 | /* |
Azure.IoT Build | 0:fa2de1b79154 | 238 | * USHAHashSizeBits |
Azure.IoT Build | 0:fa2de1b79154 | 239 | * |
Azure.IoT Build | 0:fa2de1b79154 | 240 | * Description: |
Azure.IoT Build | 0:fa2de1b79154 | 241 | * This function will return the hashsize for the given SHA |
Azure.IoT Build | 0:fa2de1b79154 | 242 | * algorithm, expressed in bits. |
Azure.IoT Build | 0:fa2de1b79154 | 243 | * |
Azure.IoT Build | 0:fa2de1b79154 | 244 | * Parameters: |
Azure.IoT Build | 0:fa2de1b79154 | 245 | * whichSha: |
Azure.IoT Build | 0:fa2de1b79154 | 246 | * which SHA algorithm to query |
Azure.IoT Build | 0:fa2de1b79154 | 247 | * |
Azure.IoT Build | 0:fa2de1b79154 | 248 | * Returns: |
Azure.IoT Build | 0:fa2de1b79154 | 249 | * hash size in bits |
Azure.IoT Build | 0:fa2de1b79154 | 250 | * |
Azure.IoT Build | 0:fa2de1b79154 | 251 | */ |
Azure.IoT Build | 0:fa2de1b79154 | 252 | int USHAHashSizeBits(enum SHAversion whichSha) |
Azure.IoT Build | 0:fa2de1b79154 | 253 | { |
Azure.IoT Build | 0:fa2de1b79154 | 254 | switch (whichSha) { |
Azure.IoT Build | 0:fa2de1b79154 | 255 | case SHA1: return SHA1HashSizeBits; |
Azure.IoT Build | 0:fa2de1b79154 | 256 | case SHA224: return SHA224HashSizeBits; |
Azure.IoT Build | 0:fa2de1b79154 | 257 | case SHA256: return SHA256HashSizeBits; |
Azure.IoT Build | 0:fa2de1b79154 | 258 | case SHA384: return SHA384HashSizeBits; |
Azure.IoT Build | 0:fa2de1b79154 | 259 | default: |
Azure.IoT Build | 0:fa2de1b79154 | 260 | case SHA512: return SHA512HashSizeBits; |
Azure.IoT Build | 0:fa2de1b79154 | 261 | } |
Azure.IoT Build | 0:fa2de1b79154 | 262 | } |
Azure.IoT Build | 0:fa2de1b79154 | 263 |