osl-0.2.0.
[osl.git] / sha256.c
1 #include <inttypes.h>
2 #include <arpa/inet.h>
3 #include <string.h>
4
5 #define blk_SHA256_BLKSIZE 64
6
7 struct blk_SHA256_CTX {
8 uint32_t state[8];
9 uint64_t size;
10 uint32_t offset;
11 uint8_t buf[blk_SHA256_BLKSIZE];
12 };
13
14 static void blk_SHA256_Init(struct blk_SHA256_CTX *ctx)
15 {
16 ctx->offset = 0;
17 ctx->size = 0;
18 ctx->state[0] = 0x6a09e667ul;
19 ctx->state[1] = 0xbb67ae85ul;
20 ctx->state[2] = 0x3c6ef372ul;
21 ctx->state[3] = 0xa54ff53aul;
22 ctx->state[4] = 0x510e527ful;
23 ctx->state[5] = 0x9b05688cul;
24 ctx->state[6] = 0x1f83d9abul;
25 ctx->state[7] = 0x5be0cd19ul;
26 }
27
28 static inline uint32_t ror(uint32_t x, unsigned n)
29 {
30 return (x >> n) | (x << (32 - n));
31 }
32
33 static inline uint32_t ch(uint32_t x, uint32_t y, uint32_t z)
34 {
35 return z ^ (x & (y ^ z));
36 }
37
38 static inline uint32_t maj(uint32_t x, uint32_t y, uint32_t z)
39 {
40 return ((x | y) & z) | (x & y);
41 }
42
43 static inline uint32_t sigma0(uint32_t x)
44 {
45 return ror(x, 2) ^ ror(x, 13) ^ ror(x, 22);
46 }
47
48 static inline uint32_t sigma1(uint32_t x)
49 {
50 return ror(x, 6) ^ ror(x, 11) ^ ror(x, 25);
51 }
52
53 static inline uint32_t gamma0(uint32_t x)
54 {
55 return ror(x, 7) ^ ror(x, 18) ^ (x >> 3);
56 }
57
58 static inline uint32_t gamma1(uint32_t x)
59 {
60 return ror(x, 17) ^ ror(x, 19) ^ (x >> 10);
61 }
62
63 #define get_be32(p) ntohl(*(unsigned int *)(p))
64
65 static void blk_SHA256_Transform(struct blk_SHA256_CTX *ctx, const unsigned char *buf)
66 {
67
68 uint32_t S[8], W[64], t0, t1;
69 int i;
70
71 /* copy state into S */
72 for (i = 0; i < 8; i++)
73 S[i] = ctx->state[i];
74
75 /* copy the state into 512-bits into W[0..15] */
76 for (i = 0; i < 16; i++, buf += sizeof(uint32_t))
77 W[i] = get_be32(buf);
78
79 /* fill W[16..63] */
80 for (i = 16; i < 64; i++)
81 W[i] = gamma1(W[i - 2]) + W[i - 7] + gamma0(W[i - 15]) + W[i - 16];
82
83 #define RND(a,b,c,d,e,f,g,h,i,ki) \
84 t0 = h + sigma1(e) + ch(e, f, g) + ki + W[i]; \
85 t1 = sigma0(a) + maj(a, b, c); \
86 d += t0; \
87 h = t0 + t1;
88
89 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
90 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
91 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
92 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
93 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
94 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
95 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
96 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
97 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
98 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
99 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
100 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
101 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
102 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
103 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
104 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
105 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
106 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
107 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
108 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
109 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
110 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
111 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
112 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
113 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
114 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
115 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
116 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
117 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
118 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
119 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
120 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
121 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
122 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
123 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
124 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
125 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
126 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
127 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
128 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
129 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
130 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
131 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
132 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
133 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
134 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
135 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
136 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
137 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
138 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
139 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
140 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
141 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
142 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
143 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
144 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
145 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
146 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
147 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
148 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
149 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
150 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
151 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
152 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
153
154 for (i = 0; i < 8; i++)
155 ctx->state[i] += S[i];
156 }
157
158 static void blk_SHA256_Update(struct blk_SHA256_CTX *ctx, const void *data, size_t len)
159 {
160 unsigned int len_buf = ctx->size & 63;
161
162 ctx->size += len;
163
164 /* Read the data into buf and process blocks as they get full */
165 if (len_buf) {
166 unsigned int left = 64 - len_buf;
167 if (len < left)
168 left = len;
169 memcpy(len_buf + ctx->buf, data, left);
170 len_buf = (len_buf + left) & 63;
171 len -= left;
172 data = ((const char *)data + left);
173 if (len_buf)
174 return;
175 blk_SHA256_Transform(ctx, ctx->buf);
176 }
177 while (len >= 64) {
178 blk_SHA256_Transform(ctx, data);
179 data = ((const char *)data + 64);
180 len -= 64;
181 }
182 if (len)
183 memcpy(ctx->buf, data, len);
184 }
185
186 #define put_be32(p, v) do { *(unsigned int *)(p) = htonl(v); } while (0)
187
188 void blk_SHA256_Final(unsigned char *digest, struct blk_SHA256_CTX *ctx)
189 {
190 static const unsigned char pad[64] = { 0x80 };
191 unsigned int padlen[2];
192 int i;
193
194 /* Pad with a binary 1 (ie 0x80), then zeroes, then length */
195 padlen[0] = htonl((uint32_t)(ctx->size >> 29));
196 padlen[1] = htonl((uint32_t)(ctx->size << 3));
197
198 i = ctx->size & 63;
199 blk_SHA256_Update(ctx, pad, 1 + (63 & (55 - i)));
200 blk_SHA256_Update(ctx, padlen, 8);
201
202 /* copy output */
203 for (i = 0; i < 5; i++, digest += sizeof(uint32_t))
204 put_be32(digest, ctx->state[i]);
205 }
206
207 /**
208 * Compute the hash value for osl version 3 tables.
209 *
210 * \param data Pointer to the data to compute the hash value from.
211 * \param len The length of \a data in bytes.
212 * \param result must point to an area at least 20 bytes large.
213 */
214 void sha256_hash(const char *data, unsigned long len, unsigned char *result)
215 {
216 struct blk_SHA256_CTX c;
217
218 blk_SHA256_Init(&c);
219 blk_SHA256_Update(&c, data, len);
220 blk_SHA256_Final(result, &c);
221 }