Azure IoT common library

Fork of azure_c_shared_utility by Azure IoT

Committer:
Azure.IoT Build
Date:
Fri Apr 08 12:01:36 2016 -0700
Revision:
0:fa2de1b79154
Child:
19:2e0811512ceb
1.0.4

Who changed what in which revision?

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