/******************************************************************************* | |
* Author : author | |
* Version : V1.0 | |
* Date : 2021-07-27 | |
* Description : util_sha256.c | |
********************************************************************************/ | |
/********************************* Include File ********************************/ | |
#include <stdlib.h> | |
#include <string.h> | |
#include "util_sha256.h" | |
/********************************* Macro Definition ****************************/ | |
#define SHR(x,n) ((x & 0xFFFFFFFF) >> n) | |
#define ROTR(x,n) (SHR(x,n) | (x << (32 - n))) | |
#define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3)) | |
#define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10)) | |
#define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22)) | |
#define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25)) | |
#define F0(x,y,z) ((x & y) | (z & (x | y))) | |
#define F1(x,y,z) (z ^ (x & (y ^ z))) | |
#define R(t) (M[t] = S1(M[t-2]) + M[t-7] + S0(M[t-15]) + M[t-16]) | |
#define P(a,b,c,d,e,f,g,h,x,K) \ | |
{ \ | |
tep = h + S3(e) + F1(e,f,g) + K + x; \ | |
tep1 = S2(a) + F0(a,b,c); \ | |
d += tep; h = tep + tep1; \ | |
} | |
#ifndef GET_UINT32_BE | |
#define GET_UINT32_BE(n,b,i) \ | |
do { \ | |
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \ | |
| ( (uint32_t) (b)[(i) + 1] << 16 ) \ | |
| ( (uint32_t) (b)[(i) + 2] << 8 ) \ | |
| ( (uint32_t) (b)[(i) + 3] ); \ | |
} while( 0 ) | |
#endif | |
#ifndef PUT_UINT32_BE | |
#define PUT_UINT32_BE(n,b,i) \ | |
do { \ | |
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \ | |
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \ | |
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \ | |
(b)[(i) + 3] = (unsigned char) ( (n) ); \ | |
} while( 0 ) | |
#endif | |
/********************************* Type Definition *****************************/ | |
/********************************* Variables ***********************************/ | |
static const uint32_t K[] = { | |
0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, | |
0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, | |
0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, | |
0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, | |
0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, | |
0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, | |
0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, | |
0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967, | |
0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, | |
0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, | |
0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, | |
0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, | |
0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, | |
0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, | |
0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, | |
0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2, | |
}; | |
/********************************* Function ************************************/ | |
void utils_sha256_init(uni_sha256_s *tex) | |
{ | |
memset(tex, 0, sizeof(uni_sha256_s)); | |
} | |
void utils_sha256_starts(uni_sha256_s *tex) | |
{ | |
int is224 = 0; | |
tex->total[0] = 0; | |
tex->total[1] = 0; | |
if (is224 == 0) { | |
tex->state[0] = 0x6A09E667; | |
tex->state[1] = 0xBB67AE85; | |
tex->state[2] = 0x3C6EF372; | |
tex->state[3] = 0xA54FF53A; | |
tex->state[4] = 0x510E527F; | |
tex->state[5] = 0x9B05688C; | |
tex->state[6] = 0x1F83D9AB; | |
tex->state[7] = 0x5BE0CD19; | |
} | |
tex->type = is224; | |
} | |
void utils_sha256_process(uni_sha256_s *tex, const unsigned char data[64]) | |
{ | |
unsigned int i; | |
uint32_t B[8],M[64]; | |
uint32_t tep,tep1; | |
for (i = 0; i < 8; i++) { | |
B[i] = tex->state[i]; | |
} | |
for (i = 0; i < 64; i++) { | |
if (i < 16) { | |
GET_UINT32_BE(M[i], data, 4 * i); | |
} else { | |
R(i); | |
} | |
P(B[0], B[1], B[2], B[3], B[4], B[5], B[6], B[7], M[i], K[i]); | |
tep = B[7]; | |
B[7] = B[6]; | |
B[6] = B[5]; | |
B[5] = B[4]; | |
B[4] = B[3]; | |
B[3] = B[2]; | |
B[2] = B[1]; | |
B[1] = B[0]; | |
B[0] = tep; | |
} | |
for (i = 0; i < 8; i++) { | |
tex->state[i] += B[i]; | |
} | |
} | |
void utils_sha256_update(uni_sha256_s *tex, const unsigned char *in, uint32_t in_len) | |
{ | |
uint32_t left; | |
size_t cov; | |
if (in_len == 0) { | |
return; | |
} | |
left = tex->total[0] & 0x3F; | |
cov = 64 - left; | |
tex->total[0] += (uint32_t) in_len; | |
tex->total[0] &= 0xFFFFFFFF; | |
if (tex->total[0] < (uint32_t) in_len) { | |
tex->total[1]++; | |
} | |
if (left && in_len >= cov) { | |
memcpy((void *)(tex->buffer + left), in, cov); | |
utils_sha256_process(tex, tex->buffer); | |
in += cov; | |
in_len -= cov; | |
left = 0; | |
} | |
while (in_len >= 64) { | |
utils_sha256_process(tex, in); | |
in += 64; | |
in_len -= 64; | |
} | |
if (in_len > 0) { | |
memcpy((void *)(tex->buffer + left), in, in_len); | |
} | |
} | |
static const unsigned char sha256_filling[64] = { | |
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | |
}; | |
void utils_sha256_finish(uni_sha256_s *tex, uint8_t out[32]) | |
{ | |
uint32_t last, padn; | |
uint32_t high, low; | |
unsigned char msglen[8]; | |
high = (tex->total[0] >> 29) | |
| (tex->total[1] << 3); | |
low = (tex->total[0] << 3); | |
PUT_UINT32_BE(high, msglen, 0); | |
PUT_UINT32_BE(low, msglen, 4); | |
last = tex->total[0] & 0x3F; | |
padn = (last < 56) ? (56 - last) : (120 - last); | |
utils_sha256_update(tex, sha256_filling, padn); | |
utils_sha256_update(tex, msglen, 8); | |
PUT_UINT32_BE(tex->state[0], out, 0); | |
PUT_UINT32_BE(tex->state[1], out, 4); | |
PUT_UINT32_BE(tex->state[2], out, 8); | |
PUT_UINT32_BE(tex->state[3], out, 12); | |
PUT_UINT32_BE(tex->state[4], out, 16); | |
PUT_UINT32_BE(tex->state[5], out, 20); | |
PUT_UINT32_BE(tex->state[6], out, 24); | |
if (tex->type == 0) { | |
PUT_UINT32_BE(tex->state[7], out, 28); | |
} | |
} | |