| 24 | #define SIG1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ ((x) >> 10)) |
| 25 | |
| 26 | static void sha256_transform(cbm_sha256_ctx *c, const uint8_t *data) { |
| 27 | uint32_t m[64]; |
| 28 | for (int i = 0, j = 0; i < 16; i++, j += 4) { |
| 29 | m[i] = ((uint32_t)data[j] << 24) | ((uint32_t)data[j + 1] << 16) | |
| 30 | ((uint32_t)data[j + 2] << 8) | (uint32_t)data[j + 3]; |
| 31 | } |
| 32 | for (int i = 16; i < 64; i++) { |
| 33 | m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16]; |
| 34 | } |
| 35 | |
| 36 | uint32_t a = c->state[0], b = c->state[1], cc = c->state[2], d = c->state[3]; |
| 37 | uint32_t e = c->state[4], f = c->state[5], g = c->state[6], h = c->state[7]; |
| 38 | |
| 39 | for (int i = 0; i < 64; i++) { |
| 40 | uint32_t t1 = h + EP1(e) + CH(e, f, g) + K[i] + m[i]; |
| 41 | uint32_t t2 = EP0(a) + MAJ(a, b, cc); |
| 42 | h = g; |
| 43 | g = f; |
| 44 | f = e; |
| 45 | e = d + t1; |
| 46 | d = cc; |
| 47 | cc = b; |
| 48 | b = a; |
| 49 | a = t1 + t2; |
| 50 | } |
| 51 | |
| 52 | c->state[0] += a; |
| 53 | c->state[1] += b; |
| 54 | c->state[2] += cc; |
| 55 | c->state[3] += d; |
| 56 | c->state[4] += e; |
| 57 | c->state[5] += f; |
| 58 | c->state[6] += g; |
| 59 | c->state[7] += h; |
| 60 | } |
| 61 | |
| 62 | void cbm_sha256_init(cbm_sha256_ctx *c) { |
| 63 | c->bitlen = 0; |
no outgoing calls
no test coverage detected