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