juegos

Juegos de terminal de Pancho: Catan (TUI, GUI, web, servidor, PicoCalc), ajedrez, calculadora y minijuegos
git clone https://git.lu3dhn.xyz/juegos.git
Log | Files | Refs

sha256.c (3049B)


      1 /* sha256.c - SHA-256 (FIPS 180-4) en un solo bloque de memoria. */
      2 #include <string.h>
      3 #include "sha256.h"
      4 
      5 static const uint32_t K[64] = {
      6     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
      7     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
      8     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
      9     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
     10     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
     11     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
     12     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
     13     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
     14 };
     15 
     16 #define ROR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
     17 
     18 static void block(uint32_t h[8], const uint8_t *p)
     19 {
     20     uint32_t w[64];
     21     for (int i = 0; i < 16; i++)
     22         w[i] = (uint32_t)p[4 * i] << 24 | (uint32_t)p[4 * i + 1] << 16 | (uint32_t)p[4 * i + 2] << 8 | p[4 * i + 3];
     23     for (int i = 16; i < 64; i++) {
     24         uint32_t s0 = ROR(w[i - 15], 7) ^ ROR(w[i - 15], 18) ^ (w[i - 15] >> 3);
     25         uint32_t s1 = ROR(w[i - 2], 17) ^ ROR(w[i - 2], 19) ^ (w[i - 2] >> 10);
     26         w[i] = w[i - 16] + s0 + w[i - 7] + s1;
     27     }
     28     uint32_t a = h[0], b = h[1], c = h[2], d = h[3], e = h[4], f = h[5], g = h[6], k = h[7];
     29     for (int i = 0; i < 64; i++) {
     30         uint32_t t1 = k + (ROR(e, 6) ^ ROR(e, 11) ^ ROR(e, 25)) + ((e & f) ^ (~e & g)) + K[i] + w[i];
     31         uint32_t t2 = (ROR(a, 2) ^ ROR(a, 13) ^ ROR(a, 22)) + ((a & b) ^ (a & c) ^ (b & c));
     32         k = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2;
     33     }
     34     h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e; h[5] += f; h[6] += g; h[7] += k;
     35 }
     36 
     37 void sha256(const void *data, size_t len, uint8_t out[32])
     38 {
     39     uint32_t h[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
     40     const uint8_t *p = data;
     41     size_t n = len;
     42     for (; n >= 64; n -= 64, p += 64) block(h, p);
     43     uint8_t tail[128];
     44     memset(tail, 0, sizeof tail);
     45     memcpy(tail, p, n);
     46     tail[n] = 0x80;
     47     int tl = n + 9 > 64 ? 128 : 64;
     48     uint64_t bits = (uint64_t)len * 8;
     49     for (int i = 0; i < 8; i++) tail[tl - 1 - i] = (uint8_t)(bits >> (8 * i));
     50     block(h, tail);
     51     if (tl == 128) block(h, tail + 64);
     52     for (int i = 0; i < 8; i++) {
     53         out[4 * i] = (uint8_t)(h[i] >> 24); out[4 * i + 1] = (uint8_t)(h[i] >> 16);
     54         out[4 * i + 2] = (uint8_t)(h[i] >> 8); out[4 * i + 3] = (uint8_t)h[i];
     55     }
     56 }
     57 
     58 void sha256_hex(const void *data, size_t len, char out[65])
     59 {
     60     static const char HEX[] = "0123456789abcdef";
     61     uint8_t d[32];
     62     sha256(data, len, d);
     63     for (int i = 0; i < 32; i++) { out[2 * i] = HEX[d[i] >> 4]; out[2 * i + 1] = HEX[d[i] & 15]; }
     64     out[64] = 0;
     65 }