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