Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/atlas/blob/master/sha256_clean.c # atlas aien-dev/atlas Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `atlas / sha256_clean.c` 107 lines ยท 3670 bytes Raw Blame History 1 #include 2 #include 3 4 static const uint32_t K[64] = { 5 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 6 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 7 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 8 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 9 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 10 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 11 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 12 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 13 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 14 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 15 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 16 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 17 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 18 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 19 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 20 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 21 }; 22 23 static inline uint32_t rotr(uint32_t x, int n) { 24 return (x >> n) | (x << (32 - n)); 25 } 26 27 static inline uint32_t bswap32(uint32_t x) { 28 return __builtin_bswap32(x); 29 } 30 31 void sha256_transform(uint32_t state[8], const uint8_t data[64]) { 32 uint32_t W[64]; 33 const uint32_t *d = (const uint32_t *)data; 34 for (int i = 0; i < 16; i++) { 35 W[i] = bswap32(d[i]); 36 } 37 for (int i = 16; i < 64; i++) { 38 uint32_t s0 = rotr(W[i-15], 7) ^ rotr(W[i-15], 18) ^ (W[i-15] >> 3); 39 uint32_t s1 = rotr(W[i-2], 17) ^ rotr(W[i-2], 19) ^ (W[i-2] >> 10); 40 W[i] = W[i-16] + s0 + W[i-7] + s1; 41 } 42 uint32_t a = state[0], b = state[1], c = state[2], d_var = state[3]; 43 uint32_t e = state[4], f = state[5], g = state[6], h = state[7]; 44 45 for (int i = 0; i < 64; i++) { 46 uint32_t S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25); 47 uint32_t ch = (e & f) ^ ((~e) & g); 48 uint32_t temp1 = h + S1 + ch + K[i] + W[i]; 49 uint32_t S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22); 50 uint32_t maj = (a & b) ^ (a & c) ^ (b & c); 51 uint32_t temp2 = S0 + maj; 52 53 h = g; 54 g = f; 55 f = e; 56 e = d_var + temp1; 57 d_var = c; 58 c = b; 59 b = a; 60 a = temp1 + temp2; 61 } 62 63 state[0] += a; state[1] += b; state[2] += c; state[3] += d_var; 64 state[4] += e; state[5] += f; state[6] += g; state[7] += h; 65 } 66 67 // General SHA-256 for any length with scratch buffer 68 void sha256_compute(const uint8_t *data, uint64_t len, uint8_t scratch_buf[128], uint32_t out_digest[8]) { 69 out_digest[0] = 0x6a09e667; 70 out_digest[1] = 0xbb67ae85; 71 out_digest[2] = 0x3c6ef372; 72 out_digest[3] = 0xa54ff53a; 73 out_digest[4] = 0x510e527f; 74 out_digest[5] = 0x9b05688c; 75 out_digest[6] = 0x1f83d9ab; 76 out_digest[7] = 0x5be0cd19; 77 78 uint64_t remaining = len; 79 const uint8_t *ptr = data; 80 while (remaining >= 64) { 81 sha256_transform(out_digest, ptr); 82 ptr += 64; 83 remaining -= 64; 84 } 85 86 // Pad in scratchpad 87 for (int i = 0; i < 128; i++) scratch_buf[i] = 0; 88 for (uint64_t i = 0; i < remaining; i++) scratch_buf[i] = ptr[i]; 89 scratch_buf[remaining] = 0x80; 90 91 int pad_blocks = (remaining >= 56) ? 2 : 1; 92 uint64_t total_bits = len * 8; 93 int len_offset = pad_blocks * 64 - 8; 94 for (int i = 0; i < 8; i++) { 95 scratch_buf[len_offset + i] = (total_bits >> ((7 - i) * 8)) & 0xff; 96 } 97 98 sha256_transform(out_digest, scratch_buf); 99 if (pad_blocks == 2) { 100 sha256_transform(out_digest, scratch_buf + 64); 101 } 102 } 103 104 // Specialization for 256-byte Physics payload 105 void sha256_256bytes(const uint8_t *payload, uint8_t *scratch_buf, uint32_t out_digest[8]) { 106 sha256_compute(payload, 256, scratch_buf, out_digest); 107 }