aboutsummaryrefslogtreecommitdiffstats
path: root/src/framework/common/data_struct/sha256.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/framework/common/data_struct/sha256.c')
-rw-r--r--src/framework/common/data_struct/sha256.c146
1 files changed, 66 insertions, 80 deletions
diff --git a/src/framework/common/data_struct/sha256.c b/src/framework/common/data_struct/sha256.c
index 504b365..213eb4e 100644
--- a/src/framework/common/data_struct/sha256.c
+++ b/src/framework/common/data_struct/sha256.c
@@ -33,40 +33,40 @@ extern "C" {
/* *INDENT-ON* */
#endif
-#define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
-#define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))
+#define rotleft32(x,n) (((x) << n) | ((x) >> (32 - n)))
+#define rotright32(x,n) (((x) >> n) | ((x) << (32 - n)))
#if !defined(bswap_32)
-#define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00))
+#define bswap_32(x) ((rotright32((x), 24) & 0x00ff00ff) | (rotright32((x), 8) & 0xff00ff00))
#endif
#ifdef LITTLE_ENDIAN
-#define SWAP_BYTES
+#define SWAP_THE_BYTES
#else
-#undef SWAP_BYTES
+#undef SWAP_THE_BYTES
#endif
-#define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
-#define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y))))
+#define ch(a,b,c) ((c) ^ ((a) & ((b) ^ (c))))
+#define maj(a,b,c) (((a) & (b)) | ((c) & ((a) ^ (b))))
- /* round transforms for SHA256 and SHA512 compression functions */
+ /* round transforms for the SHA256 & SHA512 compression functions */
-#define vf(n,i) v[(n - i) & 7]
+#define vf(m,n) v[(m - n) & 7]
-#define hf(i) (p[i & 15] += \
- g_1(p[(i + 14) & 15]) + p[(i + 9) & 15] + g_0(p[(i + 1) & 15]))
+#define hf(n) (p[n & 15] += \
+ g_1(p[(n + 14) & 15]) + p[(n + 9) & 15] + g_0(p[(n + 1) & 15]))
-#define v_cycle(i,j) \
+#define v_cycle(m,n) \
{ \
- vf(7,i) += (j ? hf(i) : p[i]) + k_0[i+j] \
- + s_1(vf(4,i)) + ch(vf(4,i),vf(5,i),vf(6,i)); \
- vf(3,i) += vf(7,i); \
- vf(7,i) += s_0(vf(0,i))+ maj(vf(0,i),vf(1,i),vf(2,i)); \
+ vf(7,m) += (n ? hf(m) : p[m]) + k_0[m+n] \
+ + s_1(vf(4,m)) + ch(vf(4,m),vf(5,m),vf(6,m)); \
+ vf(3,m) += vf(7,m); \
+ vf(7,m) += s_0(vf(0,m))+ maj(vf(0,m),vf(1,m),vf(2,m)); \
}
-#define SHA256_MASK (SHA256_BLOCK_SIZE - 1)
+#define SHA256_MASK (SHA256_BLOCK_SIZE - 1) /* SHA256_MASK */
-#if defined(SWAP_BYTES)
+#if defined(SWAP_THE_BYTES)
#define bsw_32(p,n) \
{ \
u32 _i = (n); \
@@ -80,17 +80,17 @@ extern "C" {
#define bsw_32(p,n)
#endif
-#define s_0(x) (rotr32((x), 2) ^ rotr32((x), 13) ^ rotr32((x), 22))
-#define s_1(x) (rotr32((x), 6) ^ rotr32((x), 11) ^ rotr32((x), 25))
-#define g_0(x) (rotr32((x), 7) ^ rotr32((x), 18) ^ ((x) >> 3))
-#define g_1(x) (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10))
+#define s_0(x) (rotright32((x), 2) ^ rotright32((x), 13) ^ rotright32((x), 22))
+#define s_1(x) (rotright32((x), 6) ^ rotright32((x), 11) ^ rotright32((x), 25))
+#define g_0(x) (rotright32((x), 7) ^ rotright32((x), 18) ^ ((x) >> 3))
+#define g_1(x) (rotright32((x), 17) ^ rotright32((x), 19) ^ ((x) >> 10))
#define k_0 k256
-/* rotated SHA256 round definition. Rather than swapping variables as in */
-/* FIPS-180, different variables are 'rotated' on each round, returning */
-/* to their starting positions every eight rounds */
+/* rotated SHA256 round definition. Unlike swapping the variables as in */
+/* FIPS-180, different variables are being 'rotated' on each round, */
+/* returning to their starting positions every 8 rounds */
-#define q(n) v##n
+#define q(i) v##i
#define one_cycle(a,b,c,d,e,f,g,h,k,w) \
q(h) += s_1(q(e)) + ch(q(e), q(f), q(g)) + k + w; \
@@ -121,12 +121,6 @@ static const u32 k256[64] = {
022057577772, 024424066353, 027676321767, 030634274362,
};
-/* Compile 64 bytes of hash data into SHA256 digest value */
-/* NOTE: this routine assumes that the byte order in the */
-/* ctx->wbuf[] at this point is such that low address bytes */
-/* in the ORIGINAL byte stream will go into the high end of */
-/* words on BOTH big and little endian systems */
-
#define v_ v
#define ptr p
@@ -150,7 +144,7 @@ Sha256_compile__ (SHA256_CTX ctx[1])
/* macros defined above to this function i.e. v_ and ptr should not be removed */
/* v_cycle - for 0 to 15 */
- u32 j;
+ u32 i;
u32 *ptr = ctx->wbuf;
u32 v_[8];
@@ -161,25 +155,25 @@ Sha256_compile__ (SHA256_CTX ctx[1])
return;
}
- for (j = 0; j < 64; j += 16)
+ for (i = 0; i < 64; i += 16)
{
/*v_cycle operations from 0 to 15 */
- v_cycle (0, j);
- v_cycle (1, j);
- v_cycle (2, j);
- v_cycle (3, j);
- v_cycle (4, j);
- v_cycle (5, j);
- v_cycle (6, j);
- v_cycle (7, j);
- v_cycle (8, j);
- v_cycle (9, j);
- v_cycle (10, j);
- v_cycle (11, j);
- v_cycle (12, j);
- v_cycle (13, j);
- v_cycle (14, j);
- v_cycle (15, j);
+ v_cycle (0, i);
+ v_cycle (1, i);
+ v_cycle (2, i);
+ v_cycle (3, i);
+ v_cycle (4, i);
+ v_cycle (5, i);
+ v_cycle (6, i);
+ v_cycle (7, i);
+ v_cycle (8, i);
+ v_cycle (9, i);
+ v_cycle (10, i);
+ v_cycle (11, i);
+ v_cycle (12, i);
+ v_cycle (13, i);
+ v_cycle (14, i);
+ v_cycle (15, i);
}
/* update the context */
@@ -198,9 +192,6 @@ Sha256_compile__ (SHA256_CTX ctx[1])
#undef v_
#undef ptr
-/* SHA256 hash data in an array of bytes into hash buffer */
-/* and call the hash_compile function as required. */
-
/*===========================================================================*\
Function :Sha256_upd
Description :
@@ -214,40 +205,42 @@ Sha256_compile__ (SHA256_CTX ctx[1])
Note :
\*===========================================================================*/
void
-Sha256_upd (SHA256_CTX ctx[1], const u8 data[], size_t len)
+Sha256_upd (SHA256_CTX sha256_ctx[1], const u8 data[], size_t len)
{
- u32 pos = (u32) (ctx->count[0] & SHA256_MASK);
- u32 space = SHA256_BLOCK_SIZE - pos;
+ u32 pos = (u32) (sha256_ctx->count[0] & SHA256_MASK);
+ u32 capacity = SHA256_BLOCK_SIZE - pos;
const u8 *sp = data;
int ret;
- if ((ctx->count[0] += (u32) len) < len)
+ if ((sha256_ctx->count[0] += (u32) len) < len)
{
- ++(ctx->count[1]);
+ ++(sha256_ctx->count[1]);
}
- while (len >= space)
+ while (len >= capacity)
{
/* tranfer whole blocks while possible */
- ret = MEMCPY_S (((u8 *) ctx->wbuf) + pos, space, sp, space);
+ ret =
+ MEMCPY_S (((u8 *) sha256_ctx->wbuf) + pos, capacity, sp, capacity);
if (EOK != ret)
{
NSPOL_LOGERR ("MEMCPY_S failed");
return;
}
- sp += space;
- len -= space;
- space = SHA256_BLOCK_SIZE;
+ sp += capacity;
+ len -= capacity;
+ capacity = SHA256_BLOCK_SIZE;
pos = 0;
- bsw_32 (ctx->wbuf, SHA256_BLOCK_SIZE >> 2);
- Sha256_compile__ (ctx);
+ bsw_32 (sha256_ctx->wbuf, SHA256_BLOCK_SIZE >> 2);
+ Sha256_compile__ (sha256_ctx);
}
if (len != 0)
{
- ret = MEMCPY_S (((u8 *) ctx->wbuf) + pos, (u32) len, sp, (u32) len);
+ ret =
+ MEMCPY_S (((u8 *) sha256_ctx->wbuf) + pos, (u32) len, sp, (u32) len);
if (EOK != ret)
{
NSPOL_LOGERR ("MEMCPY_S failed");
@@ -258,7 +251,7 @@ Sha256_upd (SHA256_CTX ctx[1], const u8 data[], size_t len)
return;
}
-/* SHA256 Final padding and digest calculation */
+/* the Final padding and digest calculation of SHA256 */
/*===========================================================================*\
Function :SHA_fin1
@@ -284,16 +277,13 @@ SHA_fin1 (u8 hval[], SHA256_CTX ctx[1], const unsigned int hlen)
/*top of 32 bit words on BOTH big and little endian machines */
bsw_32 (ctx->wbuf, (i + 3) >> 2);
- /*we now need to mask valid bytes and add the padding which is */
+ /*now it is needed to mask valid bytes and add padding which is */
/*a single 1 bit and as many zero bits as necessary. Note that */
/*we can always add the first padding byte here because the */
/*buffer always has at least one empty slot */
ctx->wbuf[i >> 2] &= (u32) 0xffffff80 << 8 * (~i & 3);
ctx->wbuf[i >> 2] |= (u32) 0x00000080 << 8 * (~i & 3);
- /* we need 9 or more empty positions, one for the padding byte */
- /* (above) and eight for the length count. If there is not */
- /* enough space pad and empty the buffer */
if (i > SHA256_BLOCK_SIZE - 9)
{
if (i < 60)
@@ -316,16 +306,10 @@ SHA_fin1 (u8 hval[], SHA256_CTX ctx[1], const unsigned int hlen)
ctx->wbuf[i++] = 0;
}
- /* the following 32-bit length fields are assembled in the */
- /* wrong byte order on little endian machines but this is */
- /* corrected later since they are only ever used as 32-bit */
- /* word values. */
ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);
ctx->wbuf[15] = ctx->count[0] << 3;
Sha256_compile__ (ctx);
- /* extract the hash value as bytes in case the hash buffer is */
- /* mislaigned for 32-bit words */
for (i = 0; i < hlen; ++i)
{
hval[i] = (u8) (ctx->hash[i >> 2] >> (8 * (~i & 3)));
@@ -356,12 +340,14 @@ static const u32 g_i256[] = {
Note :
\*===========================================================================*/
void
-Sha256_set (SHA256_CTX ctx[1])
+Sha256_set (SHA256_CTX sha256_ctx[1])
{
int ret;
- ctx->count[0] = ctx->count[1] = 0;
+ sha256_ctx->count[0] = sha256_ctx->count[1] = 0;
- ret = MEMCPY_S (ctx->hash, sizeof (ctx->hash), g_i256, sizeof (g_i256));
+ ret =
+ MEMCPY_S (sha256_ctx->hash, sizeof (sha256_ctx->hash), g_i256,
+ sizeof (g_i256));
if (EOK != ret)
{
NSPOL_LOGERR ("MEMCPY_S failed");