Rev 16 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 16 | pmbaty | 1 | // sha512.c |
| 2 | |||
| 3 | |||
| 4 | // standard C includes |
||
| 5 | #include <stdio.h> |
||
| 6 | #include <stdlib.h> |
||
| 7 | #include <string.h> |
||
| 8 | |||
| 9 | // own includes |
||
| 10 | #include "sha512.h" |
||
| 11 | |||
| 12 | |||
| 13 | // bring TLS support to antique compilers |
||
| 14 | #ifndef thread_local |
||
| 15 | #ifdef _MSC_VER |
||
| 16 | #define thread_local __declspec(thread) // the thread_local keyword wasn't defined before C++11 and C23 |
||
| 17 | #else // a saner compiler |
||
| 18 | #define thread_local __thread // the thread_local keyword wasn't defined before C++11 and C23 |
||
| 19 | #endif // _MSC_VER |
||
| 20 | #endif // !thread_local |
||
| 21 | |||
| 22 | |||
| 23 | // compiler-specific glue |
||
| 24 | #ifdef _WIN32 |
||
| 25 | #ifndef __BYTE_ORDER__ |
||
| 26 | #define __ORDER_BIG_ENDIAN__ 4321 |
||
| 27 | #define __ORDER_LITTLE_ENDIAN__ 1234 |
||
| 28 | #define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ // all Windows machines are little endian |
||
| 29 | #endif // !__BYTE_ORDER__ |
||
| 30 | #ifdef _MSC_VER |
||
| 31 | #define __builtin_bswap64(x) _byteswap_uint64 ((unsigned long long) (x)) |
||
| 32 | #endif // _MSC_VER |
||
| 33 | #else // !WIN32, thus POSIX |
||
| 34 | #define sprintf_s(dst,siz,...) sprintf ((dst), __VA_ARGS__) |
||
| 35 | #endif // _WIN32 |
||
| 36 | |||
| 37 | |||
| 38 | static void sha512_private_transform (SHA512_CTX *context, const uint64_t *data) |
||
| 39 | { |
||
| 40 | // logical functions used in SHA-384 and SHA-512 |
||
| 41 | #define S64(b,x) (((x) >> (b)) | ((x) << (64 - (b)))) // 64-bit rotate right |
||
| 42 | #define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z))) |
||
| 43 | #define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) |
||
| 44 | #define Sigma0_512(x) (S64(28, (x)) ^ S64(34, (x)) ^ S64(39, (x))) |
||
| 45 | #define Sigma1_512(x) (S64(14, (x)) ^ S64(18, (x)) ^ S64(41, (x))) |
||
| 46 | #define sigma0_512(x) (S64( 1, (x)) ^ S64( 8, (x)) ^ ((x) >> 7)) |
||
| 47 | #define sigma1_512(x) (S64(19, (x)) ^ S64(61, (x)) ^ ((x) >> 6)) |
||
| 48 | |||
| 49 | // hash constant words K for SHA-384 and SHA-512 |
||
| 50 | static const uint64_t K512[80] = { |
||
| 51 | 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, |
||
| 52 | 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, |
||
| 53 | 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, |
||
| 54 | 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, |
||
| 55 | 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, |
||
| 56 | 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, |
||
| 57 | 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, |
||
| 58 | 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, |
||
| 59 | 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, |
||
| 60 | 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL |
||
| 61 | }; |
||
| 62 | |||
| 63 | uint64_t a, b, c, d, e, f, g, h, s0, s1; |
||
| 64 | uint64_t T1, T2, *W512 = (uint64_t *) context->buffer; |
||
| 65 | int j; |
||
| 66 | |||
| 67 | // initialize registers with the prev. intermediate value |
||
| 68 | a = context->state[0]; b = context->state[1]; c = context->state[2]; d = context->state[3]; e = context->state[4]; f = context->state[5]; g = context->state[6]; h = context->state[7]; |
||
| 69 | |||
| 70 | for (j = 0; j < 16; j++) |
||
| 71 | { |
||
| 72 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
||
| 73 | W512[j] = __builtin_bswap64 (*data); // convert to host byte order |
||
| 74 | #elif // __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
||
| 75 | W512[j] = *data; |
||
| 76 | #else // __BYTE_ORDER__ == ??? |
||
| 77 | #error Please port this SHA-512 code to your exotic endianness platform. What are you compiling this on? PDP? Honeywell? |
||
| 78 | #endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
||
| 79 | |||
| 80 | // apply the SHA-512 compression function to update a..h |
||
| 81 | T1 = h + Sigma1_512 (e) + Ch (e, f, g) + K512[j] + W512[j]; |
||
| 82 | T2 = Sigma0_512 (a) + Maj (a, b, c); |
||
| 83 | |||
| 84 | // update registers |
||
| 85 | h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2; |
||
| 86 | |||
| 87 | data++; |
||
| 88 | } |
||
| 89 | |||
| 90 | for (; j < 80; j++) |
||
| 91 | { |
||
| 92 | // part of the message block expansion |
||
| 93 | s0 = W512[(j + 1) & 0x0f]; |
||
| 94 | s0 = sigma0_512 (s0); |
||
| 95 | s1 = W512[(j + 14) & 0x0f]; |
||
| 96 | s1 = sigma1_512 (s1); |
||
| 97 | |||
| 98 | // apply the SHA-512 compression function to update a..h |
||
| 99 | T1 = h + Sigma1_512 (e) + Ch (e, f, g) + K512[j] + (W512[j & 0x0f] += s1 + W512[(j + 9) & 0x0f] + s0); |
||
| 100 | T2 = Sigma0_512 (a) + Maj (a, b, c); |
||
| 101 | |||
| 102 | // update registers |
||
| 103 | h = g; g = f; f = e; e = d + T1; d = c; c = b; b = a; a = T1 + T2; |
||
| 104 | } |
||
| 105 | |||
| 106 | // compute the current intermediate hash value |
||
| 107 | context->state[0] += a; context->state[1] += b; context->state[2] += c; context->state[3] += d; context->state[4] += e; context->state[5] += f; context->state[6] += g; context->state[7] += h; |
||
| 108 | |||
| 109 | // clean up |
||
| 110 | a = b = c = d = e = f = g = h = T1 = T2 = 0; |
||
| 111 | #undef sigma1_512 |
||
| 112 | #undef sigma0_512 |
||
| 113 | #undef Sigma1_512 |
||
| 114 | #undef Sigma0_512 |
||
| 115 | #undef Maj |
||
| 116 | #undef Ch |
||
| 117 | #undef S64 |
||
| 118 | return; |
||
| 119 | } |
||
| 120 | |||
| 121 | |||
| 122 | void SHA512_Init (SHA512_CTX *context) |
||
| 123 | { |
||
| 124 | // initial hash value H for SHA-512 |
||
| 125 | static const uint64_t sha512_initial_hash_value[8] = { |
||
| 126 | 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL, 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL, 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL, 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL |
||
| 127 | }; |
||
| 128 | |||
| 129 | memcpy (context->state, sha512_initial_hash_value, SHA512_DIGEST_LENGTH); |
||
| 130 | memset (context->buffer, 0, SHA512_BLOCK_LENGTH); |
||
| 131 | context->bitcount[0] = context->bitcount[1] = 0; |
||
| 132 | } |
||
| 133 | |||
| 134 | |||
| 135 | void SHA512_Update (SHA512_CTX *context, void *datain, size_t len) |
||
| 136 | { |
||
| 137 | #define ADDINC128(w,n) do { \ |
||
| 138 | (w)[0] += (uint64_t) (n); \ |
||
| 139 | if ((w)[0] < (n)) \ |
||
| 140 | (w)[1]++; \ |
||
| 141 | } while (0) // macro for incrementally adding the unsigned 64-bit integer n to the unsigned 128-bit integer (represented using a two-element array of 64-bit words |
||
| 142 | |||
| 143 | size_t freespace, usedspace; |
||
| 144 | const uint8_t *data = (const uint8_t *) datain; |
||
| 145 | |||
| 146 | if (len == 0) |
||
| 147 | return; // calling with empty data is valid - we do nothing |
||
| 148 | |||
| 149 | usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; |
||
| 150 | if (usedspace > 0) |
||
| 151 | { |
||
| 152 | // calculate how much free space is available in the buffer |
||
| 153 | freespace = SHA512_BLOCK_LENGTH - usedspace; |
||
| 154 | |||
| 155 | if (len >= freespace) |
||
| 156 | { |
||
| 157 | // fill the buffer completely and process it |
||
| 158 | memcpy (&context->buffer[usedspace], data, freespace); |
||
| 159 | ADDINC128 (context->bitcount, freespace << 3); |
||
| 160 | len -= freespace; |
||
| 161 | data += freespace; |
||
| 162 | sha512_private_transform (context, (uint64_t *) context->buffer); |
||
| 163 | } |
||
| 164 | else |
||
| 165 | { |
||
| 166 | // the buffer is not full yet |
||
| 167 | memcpy (&context->buffer[usedspace], data, len); |
||
| 168 | ADDINC128 (context->bitcount, len << 3); |
||
| 169 | |||
| 170 | // clean up |
||
| 171 | usedspace = freespace = 0; |
||
| 172 | return; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | while (len >= SHA512_BLOCK_LENGTH) |
||
| 177 | { |
||
| 178 | // process as many complete blocks as we can |
||
| 179 | sha512_private_transform (context, (uint64_t *) data); |
||
| 180 | ADDINC128 (context->bitcount, SHA512_BLOCK_LENGTH << 3); |
||
| 181 | len -= SHA512_BLOCK_LENGTH; |
||
| 182 | data += SHA512_BLOCK_LENGTH; |
||
| 183 | } |
||
| 184 | |||
| 185 | if (len > 0) |
||
| 186 | { |
||
| 187 | // save leftovers |
||
| 188 | memcpy (context->buffer, data, len); |
||
| 189 | ADDINC128 (context->bitcount, len << 3); |
||
| 190 | } |
||
| 191 | |||
| 192 | // clean up |
||
| 193 | usedspace = freespace = 0; |
||
| 194 | #undef ADDINC128 |
||
| 195 | return; |
||
| 196 | } |
||
| 197 | |||
| 198 | |||
| 199 | void SHA512_Final (uint8_t digest[SHA512_DIGEST_LENGTH], SHA512_CTX *context) |
||
| 200 | { |
||
| 201 | #define SHA512_SHORT_BLOCK_LENGTH (SHA512_BLOCK_LENGTH - 16) |
||
| 202 | |||
| 203 | size_t usedspace; |
||
| 204 | union { uint8_t *as_bytes; uint64_t *as_uint64s; } cast_var = { NULL }; |
||
| 205 | |||
| 206 | // if no digest buffer is passed, don't bother finalizing the computation |
||
| 207 | if (digest != NULL) |
||
| 208 | { |
||
| 209 | usedspace = (context->bitcount[0] >> 3) % SHA512_BLOCK_LENGTH; |
||
| 210 | |||
| 211 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
||
| 212 | context->bitcount[0] = __builtin_bswap64 (context->bitcount[0]); // convert from host byte order |
||
| 213 | context->bitcount[1] = __builtin_bswap64 (context->bitcount[1]); // convert from host byte order |
||
| 214 | #endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
||
| 215 | |||
| 216 | if (usedspace > 0) |
||
| 217 | { |
||
| 218 | // begin padding with a 1 bit |
||
| 219 | context->buffer[usedspace++] = 0x80; |
||
| 220 | |||
| 221 | if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) |
||
| 222 | memset (&context->buffer[usedspace], 0, SHA512_SHORT_BLOCK_LENGTH - usedspace); // set-up for the last transform |
||
| 223 | else |
||
| 224 | { |
||
| 225 | if (usedspace < SHA512_BLOCK_LENGTH) |
||
| 226 | memset (&context->buffer[usedspace], 0, SHA512_BLOCK_LENGTH - usedspace); |
||
| 227 | |||
| 228 | sha512_private_transform (context, (uint64_t *) context->buffer); // do second-to-last transform |
||
| 229 | memset (context->buffer, 0, SHA512_BLOCK_LENGTH - 2); // and set-up for the last transform |
||
| 230 | } |
||
| 231 | } |
||
| 232 | else // usedspace == 0 |
||
| 233 | { |
||
| 234 | memset (context->buffer, 0, SHA512_SHORT_BLOCK_LENGTH); // prepare for final transform |
||
| 235 | *context->buffer = 0x80; // begin padding with a 1 bit |
||
| 236 | } |
||
| 237 | |||
| 238 | // store the length of input data (in bits) |
||
| 239 | cast_var.as_bytes = context->buffer; |
||
| 240 | cast_var.as_uint64s[SHA512_SHORT_BLOCK_LENGTH / 8 + 0] = context->bitcount[1]; |
||
| 241 | cast_var.as_uint64s[SHA512_SHORT_BLOCK_LENGTH / 8 + 1] = context->bitcount[0]; |
||
| 242 | |||
| 243 | // final transform |
||
| 244 | sha512_private_transform (context, (uint64_t *) context->buffer); |
||
| 245 | |||
| 246 | // save the hash data for output |
||
| 247 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
||
| 248 | for (int j = 0; j < 8; j++) |
||
| 249 | context->state[j] = __builtin_bswap64 (context->state[j]); // convert to host byte order |
||
| 250 | #endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
||
| 251 | memcpy (digest, context->state, SHA512_DIGEST_LENGTH); |
||
| 252 | } |
||
| 253 | |||
| 254 | // zero out state data |
||
| 255 | memset (context, 0, sizeof (SHA512_CTX)); |
||
| 256 | #undef SHA512_SHORT_BLOCK_LENGTH |
||
| 257 | return; |
||
| 258 | } |
||
| 259 | |||
| 260 | |||
| 38 | pmbaty | 261 | const char *SHA512 (void *data, size_t data_len, uint8_t *digest_or_NULL) |
| 16 | pmbaty | 262 | { |
| 263 | // computes the SHA-512 hash of a block of data in one pass and write it to digest, or to a static buffer if NULL |
||
| 264 | // returns the STRING REPRESENTATION of digest in a statically-allocated string |
||
| 265 | |||
| 266 | static thread_local uint8_t static_digest[SHA512_DIGEST_LENGTH] = ""; |
||
| 267 | static thread_local char digest_as_string[2 * SHA512_DIGEST_LENGTH + 1] = ""; |
||
| 268 | |||
| 269 | SHA512_CTX ctx; |
||
| 270 | size_t byte_index; |
||
| 271 | |||
| 272 | SHA512_Init (&ctx); |
||
| 273 | SHA512_Update (&ctx, data, data_len); |
||
| 274 | if (digest_or_NULL == NULL) |
||
| 275 | digest_or_NULL = static_digest; |
||
| 276 | SHA512_Final (digest_or_NULL, &ctx); |
||
| 277 | |||
| 278 | for (byte_index = 0; byte_index < SHA512_DIGEST_LENGTH; byte_index++) |
||
| 279 | sprintf_s (&digest_as_string[2 * byte_index], 3, "%02x", digest_or_NULL[byte_index]); |
||
| 280 | return (digest_as_string); |
||
| 281 | } |