Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface NTPClient iothub_amqp_transport iothub_client mbed-rtos mbed
Fork of iothub_client_sample_amqp by
usha.c
00001 // Copyright (c) Microsoft. All rights reserved. 00002 // Licensed under the MIT license. See LICENSE file in the project root for full license information. 00003 00004 /**************************** usha.c ****************************/ 00005 /******************** See RFC 4634 for details ******************/ 00006 /* 00007 * Description: 00008 * This file implements a unified interface to the SHA algorithms. 00009 */ 00010 00011 #include <stdlib.h> 00012 #ifdef _CRTDBG_MAP_ALLOC 00013 #include <crtdbg.h> 00014 #endif 00015 #include "azure_c_shared_utility/gballoc.h" 00016 00017 #include "azure_c_shared_utility/sha.h" 00018 00019 /* 00020 * USHAReset 00021 * 00022 * Description: 00023 * This function will initialize the SHA Context in preparation 00024 * for computing a new SHA message digest. 00025 * 00026 * Parameters: 00027 * context: [in/out] 00028 * The context to reset. 00029 * whichSha: [in] 00030 * Selects which SHA reset to call 00031 * 00032 * Returns: 00033 * sha Error Code. 00034 * 00035 */ 00036 int USHAReset(USHAContext *ctx, enum SHAversion whichSha) 00037 { 00038 if (ctx) { 00039 ctx->whichSha = whichSha; 00040 switch (whichSha) { 00041 case SHA1: return SHA1Reset((SHA1Context*)&ctx->ctx); 00042 case SHA224: return SHA224Reset((SHA224Context*)&ctx->ctx); 00043 case SHA256: return SHA256Reset((SHA256Context*)&ctx->ctx); 00044 case SHA384: return SHA384Reset((SHA384Context*)&ctx->ctx); 00045 case SHA512: return SHA512Reset((SHA512Context*)&ctx->ctx); 00046 default: return shaBadParam; 00047 } 00048 } 00049 else { 00050 return shaNull; 00051 } 00052 } 00053 00054 /* 00055 * USHAInput 00056 * 00057 * Description: 00058 * This function accepts an array of octets as the next portion 00059 * of the message. 00060 * 00061 * Parameters: 00062 * context: [in/out] 00063 * The SHA context to update 00064 * message_array: [in] 00065 * An array of characters representing the next portion of 00066 * the message. 00067 * length: [in] 00068 * The length of the message in message_array 00069 * 00070 * Returns: 00071 * sha Error Code. 00072 * 00073 */ 00074 int USHAInput(USHAContext *ctx, 00075 const uint8_t *bytes, unsigned int bytecount) 00076 { 00077 if (ctx) { 00078 switch (ctx->whichSha) { 00079 case SHA1: 00080 return SHA1Input((SHA1Context*)&ctx->ctx, bytes, bytecount); 00081 case SHA224: 00082 return SHA224Input((SHA224Context*)&ctx->ctx, bytes, 00083 bytecount); 00084 case SHA256: 00085 return SHA256Input((SHA256Context*)&ctx->ctx, bytes, 00086 bytecount); 00087 case SHA384: 00088 return SHA384Input((SHA384Context*)&ctx->ctx, bytes, 00089 bytecount); 00090 case SHA512: 00091 return SHA512Input((SHA512Context*)&ctx->ctx, bytes, 00092 bytecount); 00093 default: return shaBadParam; 00094 } 00095 } 00096 else { 00097 return shaNull; 00098 } 00099 } 00100 00101 /* 00102 * USHAFinalBits 00103 * 00104 * Description: 00105 * This function will add in any final bits of the message. 00106 * 00107 * Parameters: 00108 * context: [in/out] 00109 * The SHA context to update 00110 * message_bits: [in] 00111 * The final bits of the message, in the upper portion of the 00112 * byte. (Use 0b###00000 instead of 0b00000### to input the 00113 * three bits ###.) 00114 * length: [in] 00115 * The number of bits in message_bits, between 1 and 7. 00116 * 00117 * Returns: 00118 * sha Error Code. 00119 */ 00120 int USHAFinalBits(USHAContext *ctx, 00121 const uint8_t bits, unsigned int bitcount) 00122 { 00123 if (ctx) { 00124 switch (ctx->whichSha) { 00125 case SHA1: 00126 return SHA1FinalBits((SHA1Context*)&ctx->ctx, bits, bitcount); 00127 case SHA224: 00128 return SHA224FinalBits((SHA224Context*)&ctx->ctx, bits, 00129 bitcount); 00130 case SHA256: 00131 return SHA256FinalBits((SHA256Context*)&ctx->ctx, bits, 00132 bitcount); 00133 case SHA384: 00134 return SHA384FinalBits((SHA384Context*)&ctx->ctx, bits, 00135 bitcount); 00136 case SHA512: 00137 return SHA512FinalBits((SHA512Context*)&ctx->ctx, bits, 00138 bitcount); 00139 default: return shaBadParam; 00140 } 00141 } 00142 else { 00143 return shaNull; 00144 } 00145 } 00146 00147 /* 00148 * USHAResult 00149 * 00150 * Description: 00151 * This function will return the 160-bit message digest into the 00152 * Message_Digest array provided by the caller. 00153 * NOTE: The first octet of hash is stored in the 0th element, 00154 * the last octet of hash in the 19th element. 00155 * 00156 * Parameters: 00157 * context: [in/out] 00158 * The context to use to calculate the SHA-1 hash. 00159 * Message_Digest: [out] 00160 * Where the digest is returned. 00161 * 00162 * Returns: 00163 * sha Error Code. 00164 * 00165 */ 00166 int USHAResult(USHAContext *ctx, 00167 uint8_t Message_Digest[USHAMaxHashSize]) 00168 { 00169 if (ctx) { 00170 switch (ctx->whichSha) { 00171 case SHA1: 00172 return SHA1Result((SHA1Context*)&ctx->ctx, Message_Digest); 00173 case SHA224: 00174 return SHA224Result((SHA224Context*)&ctx->ctx, Message_Digest); 00175 case SHA256: 00176 return SHA256Result((SHA256Context*)&ctx->ctx, Message_Digest); 00177 case SHA384: 00178 return SHA384Result((SHA384Context*)&ctx->ctx, Message_Digest); 00179 case SHA512: 00180 return SHA512Result((SHA512Context*)&ctx->ctx, Message_Digest); 00181 default: return shaBadParam; 00182 } 00183 } 00184 else { 00185 return shaNull; 00186 } 00187 } 00188 00189 /* 00190 * USHABlockSize 00191 * 00192 * Description: 00193 * This function will return the blocksize for the given SHA 00194 * algorithm. 00195 * 00196 * Parameters: 00197 * whichSha: 00198 * which SHA algorithm to query 00199 * 00200 * Returns: 00201 * block size 00202 * 00203 */ 00204 int USHABlockSize(enum SHAversion whichSha) 00205 { 00206 switch (whichSha) { 00207 case SHA1: return SHA1_Message_Block_Size; 00208 case SHA224: return SHA224_Message_Block_Size; 00209 case SHA256: return SHA256_Message_Block_Size; 00210 case SHA384: return SHA384_Message_Block_Size; 00211 default: 00212 case SHA512: return SHA512_Message_Block_Size; 00213 } 00214 } 00215 00216 /* 00217 * USHAHashSize 00218 * 00219 * Description: 00220 * This function will return the hashsize for the given SHA 00221 * algorithm. 00222 * 00223 * Parameters: 00224 * whichSha: 00225 * which SHA algorithm to query 00226 * 00227 * Returns: 00228 * hash size 00229 * 00230 */ 00231 int USHAHashSize(enum SHAversion whichSha) 00232 { 00233 switch (whichSha) { 00234 case SHA1: return SHA1HashSize; 00235 case SHA224: return SHA224HashSize; 00236 case SHA256: return SHA256HashSize; 00237 case SHA384: return SHA384HashSize; 00238 default: 00239 case SHA512: return SHA512HashSize; 00240 } 00241 } 00242 00243 /* 00244 * USHAHashSizeBits 00245 * 00246 * Description: 00247 * This function will return the hashsize for the given SHA 00248 * algorithm, expressed in bits. 00249 * 00250 * Parameters: 00251 * whichSha: 00252 * which SHA algorithm to query 00253 * 00254 * Returns: 00255 * hash size in bits 00256 * 00257 */ 00258 int USHAHashSizeBits(enum SHAversion whichSha) 00259 { 00260 switch (whichSha) { 00261 case SHA1: return SHA1HashSizeBits; 00262 case SHA224: return SHA224HashSizeBits; 00263 case SHA256: return SHA256HashSizeBits; 00264 case SHA384: return SHA384HashSizeBits; 00265 default: 00266 case SHA512: return SHA512HashSizeBits; 00267 } 00268 } 00269
Generated on Tue Jul 12 2022 12:43:25 by
