luajitos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

SHA512.c (6695B)


      1 /*
      2  * SHA-512 Implementation
      3  * Uses 64-bit operations, optimized with compiler intrinsics
      4  */
      5 
      6 #include <stdio.h>
      7 #include <stdlib.h>
      8 #include <string.h>
      9 #include <stdint.h>
     10 
     11 // SHA-512 context
     12 typedef struct {
     13     uint64_t state[8];
     14     uint64_t count[2];
     15     uint8_t buffer[128];
     16 } sha512_context;
     17 
     18 // SHA-512 constants
     19 static const uint64_t K[80] = {
     20     0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL,
     21     0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL,
     22     0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL,
     23     0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL,
     24     0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL,
     25     0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL,
     26     0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL,
     27     0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL,
     28     0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL,
     29     0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL,
     30     0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL,
     31     0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL,
     32     0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL,
     33     0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL,
     34     0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL,
     35     0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL,
     36     0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL,
     37     0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, 0x113f9804bef90daeULL, 0x1b710b35131c471bULL,
     38     0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL,
     39     0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
     40 };
     41 
     42 // Rotate right for 64-bit
     43 #define ROTR64(x, n) (((x) >> (n)) | ((x) << (64 - (n))))
     44 #define SHR64(x, n) ((x) >> (n))
     45 
     46 // SHA-512 functions
     47 #define CH64(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
     48 #define MAJ64(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
     49 #define SIGMA0_512(x) (ROTR64(x, 28) ^ ROTR64(x, 34) ^ ROTR64(x, 39))
     50 #define SIGMA1_512(x) (ROTR64(x, 14) ^ ROTR64(x, 18) ^ ROTR64(x, 41))
     51 #define sigma0_512(x) (ROTR64(x, 1) ^ ROTR64(x, 8) ^ SHR64(x, 7))
     52 #define sigma1_512(x) (ROTR64(x, 19) ^ ROTR64(x, 61) ^ SHR64(x, 6))
     53 
     54 // SHA-512 transform
     55 static void sha512_transform(uint64_t state[8], const uint8_t block[128]) {
     56     uint64_t W[80];
     57     uint64_t a, b, c, d, e, f, g, h, t1, t2;
     58     int i;
     59 
     60     // Prepare message schedule
     61     for (i = 0; i < 16; i++) {
     62         W[i] = ((uint64_t)block[i * 8] << 56) |
     63                ((uint64_t)block[i * 8 + 1] << 48) |
     64                ((uint64_t)block[i * 8 + 2] << 40) |
     65                ((uint64_t)block[i * 8 + 3] << 32) |
     66                ((uint64_t)block[i * 8 + 4] << 24) |
     67                ((uint64_t)block[i * 8 + 5] << 16) |
     68                ((uint64_t)block[i * 8 + 6] << 8) |
     69                ((uint64_t)block[i * 8 + 7]);
     70     }
     71 
     72     for (i = 16; i < 80; i++) {
     73         W[i] = sigma1_512(W[i - 2]) + W[i - 7] + sigma0_512(W[i - 15]) + W[i - 16];
     74     }
     75 
     76     // Initialize working variables
     77     a = state[0]; b = state[1]; c = state[2]; d = state[3];
     78     e = state[4]; f = state[5]; g = state[6]; h = state[7];
     79 
     80     // Main loop
     81     for (i = 0; i < 80; i++) {
     82         t1 = h + SIGMA1_512(e) + CH64(e, f, g) + K[i] + W[i];
     83         t2 = SIGMA0_512(a) + MAJ64(a, b, c);
     84         h = g; g = f; f = e; e = d + t1;
     85         d = c; c = b; b = a; a = t1 + t2;
     86     }
     87 
     88     // Update state
     89     state[0] += a; state[1] += b; state[2] += c; state[3] += d;
     90     state[4] += e; state[5] += f; state[6] += g; state[7] += h;
     91 }
     92 
     93 // Initialize SHA-512 context
     94 void sha512_init(sha512_context *ctx) {
     95     ctx->state[0] = 0x6a09e667f3bcc908ULL;
     96     ctx->state[1] = 0xbb67ae8584caa73bULL;
     97     ctx->state[2] = 0x3c6ef372fe94f82bULL;
     98     ctx->state[3] = 0xa54ff53a5f1d36f1ULL;
     99     ctx->state[4] = 0x510e527fade682d1ULL;
    100     ctx->state[5] = 0x9b05688c2b3e6c1fULL;
    101     ctx->state[6] = 0x1f83d9abfb41bd6bULL;
    102     ctx->state[7] = 0x5be0cd19137e2179ULL;
    103     ctx->count[0] = 0;
    104     ctx->count[1] = 0;
    105 }
    106 
    107 // Update SHA-512 with data
    108 void sha512_update(sha512_context *ctx, const uint8_t *data, size_t len) {
    109     size_t i, index, part_len;
    110 
    111     index = (size_t)((ctx->count[0] >> 3) & 0x7F);
    112 
    113     uint64_t bitlen = (uint64_t)len << 3;
    114     if ((ctx->count[0] += bitlen) < bitlen) {
    115         ctx->count[1]++;
    116     }
    117     ctx->count[1] += ((uint64_t)len >> 61);
    118 
    119     part_len = 128 - index;
    120 
    121     if (len >= part_len) {
    122         memcpy(&ctx->buffer[index], data, part_len);
    123         sha512_transform(ctx->state, ctx->buffer);
    124 
    125         for (i = part_len; i + 127 < len; i += 128) {
    126             sha512_transform(ctx->state, &data[i]);
    127         }
    128         index = 0;
    129     } else {
    130         i = 0;
    131     }
    132 
    133     memcpy(&ctx->buffer[index], &data[i], len - i);
    134 }
    135 
    136 // Finalize SHA-512 and produce digest
    137 void sha512_final(sha512_context *ctx, uint8_t digest[64]) {
    138     uint8_t bits[16];
    139     size_t index, pad_len;
    140 
    141     // Encode bit count
    142     for (int i = 0; i < 8; i++) {
    143         bits[15 - i] = (uint8_t)(ctx->count[0] >> (i * 8));
    144         bits[7 - i] = (uint8_t)(ctx->count[1] >> (i * 8));
    145     }
    146 
    147     // Pad
    148     index = (size_t)((ctx->count[0] >> 3) & 0x7F);
    149     pad_len = (index < 112) ? (112 - index) : (240 - index);
    150 
    151     uint8_t padding[128];
    152     padding[0] = 0x80;
    153     memset(padding + 1, 0, pad_len - 1);
    154 
    155     sha512_update(ctx, padding, pad_len);
    156     sha512_update(ctx, bits, 16);
    157 
    158     // Store digest
    159     for (int i = 0; i < 8; i++) {
    160         digest[i * 8] = (uint8_t)(ctx->state[i] >> 56);
    161         digest[i * 8 + 1] = (uint8_t)(ctx->state[i] >> 48);
    162         digest[i * 8 + 2] = (uint8_t)(ctx->state[i] >> 40);
    163         digest[i * 8 + 3] = (uint8_t)(ctx->state[i] >> 32);
    164         digest[i * 8 + 4] = (uint8_t)(ctx->state[i] >> 24);
    165         digest[i * 8 + 5] = (uint8_t)(ctx->state[i] >> 16);
    166         digest[i * 8 + 6] = (uint8_t)(ctx->state[i] >> 8);
    167         digest[i * 8 + 7] = (uint8_t)(ctx->state[i]);
    168     }
    169 }
    170 
    171 // One-shot SHA-512
    172 void sha512(const uint8_t *data, size_t len, uint8_t digest[64]) {
    173     sha512_context ctx;
    174     sha512_init(&ctx);
    175     sha512_update(&ctx, data, len);
    176     sha512_final(&ctx, digest);
    177 }