libaaruformat 1.0
Aaru Data Preservation Suite - Format Library
Loading...
Searching...
No Matches
sha256.c
Go to the documentation of this file.
1/*
2 * Public domain / MIT SHA-256 implementation for libaaruformat.
3 *
4 * Reference: FIPS PUB 180-4.
5 */
6#include <stdbool.h>
7#include <stdint.h>
8#include <stdio.h>
9#include <string.h>
10
11#include "decls.h"
12#include "sha256.h"
13
14#ifndef AARU_LOCAL
15#define AARU_LOCAL static
16#endif
17
18#if defined(_MSC_VER)
19#define ROTR32(x, n) _rotr((x), (n))
20#else
21#define ROTR32(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
22#endif
23
24#define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
25#define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
26#define SIGMA0(x) (ROTR32((x), 2) ^ ROTR32((x), 13) ^ ROTR32((x), 22))
27#define SIGMA1(x) (ROTR32((x), 6) ^ ROTR32((x), 11) ^ ROTR32((x), 25))
28#define sigma0(x) (ROTR32((x), 7) ^ ROTR32((x), 18) ^ ((x) >> 3))
29#define sigma1(x) (ROTR32((x), 17) ^ ROTR32((x), 19) ^ ((x) >> 10))
30
31static const uint32_t K[64] = {
32 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
33 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
34 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
35 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
36 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
37 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
38 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
39 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
40
41AARU_LOCAL void sha256_transform(uint32_t state[8], const uint8_t block[64])
42{
43 uint32_t W[64];
44 for(int i = 0; i < 16; i++)
45 W[i] = ((uint32_t)block[i * 4] << 24) | ((uint32_t)block[i * 4 + 1] << 16) | ((uint32_t)block[i * 4 + 2] << 8) |
46 (uint32_t)block[i * 4 + 3];
47 for(int i = 16; i < 64; i++) W[i] = sigma1(W[i - 2]) + W[i - 7] + sigma0(W[i - 15]) + W[i - 16];
48
49 uint32_t a = state[0], b = state[1], c = state[2], d = state[3], e = state[4], f = state[5], g = state[6],
50 h = state[7];
51
52 for(int i = 0; i < 64; i++)
53 {
54 uint32_t T1 = h + SIGMA1(e) + Ch(e, f, g) + K[i] + W[i];
55 uint32_t T2 = SIGMA0(a) + Maj(a, b, c);
56 h = g;
57 g = f;
58 f = e;
59 e = d + T1;
60 d = c;
61 c = b;
62 b = a;
63 a = T1 + T2;
64 }
65
66 state[0] += a;
67 state[1] += b;
68 state[2] += c;
69 state[3] += d;
70 state[4] += e;
71 state[5] += f;
72 state[6] += g;
73 state[7] += h;
74}
75
77{
78 ctx->state[0] = 0x6a09e667U;
79 ctx->state[1] = 0xbb67ae85U;
80 ctx->state[2] = 0x3c6ef372U;
81 ctx->state[3] = 0xa54ff53aU;
82 ctx->state[4] = 0x510e527fU;
83 ctx->state[5] = 0x9b05688cU;
84 ctx->state[6] = 0x1f83d9abU;
85 ctx->state[7] = 0x5be0cd19U;
86 ctx->bitcount = 0ULL;
87 memset(ctx->buffer, 0, sizeof(ctx->buffer));
88}
89
90AARU_EXPORT void AARU_CALL aaruf_sha256_update(sha256_ctx *ctx, const void *data_ptr, unsigned long len)
91{
92 if(len == 0) return;
93 const uint8_t *data = (const uint8_t *)data_ptr;
94 uint32_t idx = (uint32_t)((ctx->bitcount >> 3) & 0x3F);
95 ctx->bitcount += ((uint64_t)len) << 3;
96 uint32_t space = 64 - idx;
97
98 if(len >= space)
99 {
100 memcpy(ctx->buffer + idx, data, space);
101 sha256_transform(ctx->state, ctx->buffer);
102 data += space;
103 len -= space;
104 while(len >= 64)
105 {
106 sha256_transform(ctx->state, data);
107 data += 64;
108 len -= 64;
109 }
110 idx = 0;
111 }
112 if(len > 0) memcpy(ctx->buffer + idx, data, len);
113}
114
116{
117 uint8_t pad[64];
118 memset(pad, 0, sizeof(pad));
119 pad[0] = 0x80;
120
121 uint8_t len_be[8];
122 uint64_t bits = ctx->bitcount;
123 for(int i = 0; i < 8; i++) len_be[7 - i] = (uint8_t)(bits >> (i * 8));
124
125 uint32_t idx = (uint32_t)((ctx->bitcount >> 3) & 0x3F);
126 uint32_t pad_len = (idx < 56) ? (56 - idx) : (120 - idx);
127
128 aaruf_sha256_update(ctx, pad, pad_len);
129 aaruf_sha256_update(ctx, len_be, 8);
130
131 for(int i = 0; i < 8; i++)
132 {
133 out[i * 4] = (uint8_t)(ctx->state[i] >> 24);
134 out[i * 4 + 1] = (uint8_t)(ctx->state[i] >> 16);
135 out[i * 4 + 2] = (uint8_t)(ctx->state[i] >> 8);
136 out[i * 4 + 3] = (uint8_t)(ctx->state[i]);
137 }
138 memset(ctx->buffer, 0, sizeof(ctx->buffer));
139}
140
141AARU_EXPORT void AARU_CALL aaruf_sha256_buffer(const void *data, unsigned long size, unsigned char *result)
142{
143 sha256_ctx ctx;
144 aaruf_sha256_init(&ctx);
145 aaruf_sha256_update(&ctx, data, size);
146 aaruf_sha256_final(&ctx, result);
147}
#define AARU_CALL
Definition decls.h:45
#define AARU_LOCAL
Definition decls.h:55
#define AARU_EXPORT
Definition decls.h:54
#define sigma1(x)
Definition sha256.c:29
void aaruf_sha256_init(sha256_ctx *ctx)
Definition sha256.c:76
void aaruf_sha256_final(sha256_ctx *ctx, unsigned char *out)
Definition sha256.c:115
static const uint32_t K[64]
Definition sha256.c:31
#define Maj(x, y, z)
Definition sha256.c:25
#define sigma0(x)
Definition sha256.c:28
void aaruf_sha256_update(sha256_ctx *ctx, const void *data_ptr, unsigned long len)
Definition sha256.c:90
void aaruf_sha256_buffer(const void *data, unsigned long size, unsigned char *result)
Definition sha256.c:141
#define SIGMA0(x)
Definition sha256.c:26
#define Ch(x, y, z)
Definition sha256.c:24
#define SIGMA1(x)
Definition sha256.c:27
static void sha256_transform(uint32_t state[8], const uint8_t block[64])
Definition sha256.c:41
uint64_t bitcount
Definition sha256.h:44
uint8_t buffer[64]
Definition sha256.h:45
uint32_t state[8]
Definition sha256.h:43