Skip to content

Commit

Permalink
add some comment
Browse files Browse the repository at this point in the history
  • Loading branch information
piggypiggy committed Jan 10, 2021
1 parent ea6c015 commit e75ca01
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
11 changes: 10 additions & 1 deletion sm3.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,30 @@ int sm3_update(SM3_CTX *ctx, const u8* data, size_t datalen)
{
size_t n, left;

/* number of bytes in ctx->buf */
n = (ctx->bits >> 3) & 0x3fU;
ctx->bits += (datalen << 3);

ctx->bits += (datalen << 3);
left = SM3_BLOCK_SIZE - n;
if (datalen < left) {
memcpy(ctx->buf + n, data, datalen);
return SM3_OK;
} else {
/* concatenate ctx->buf and data to make up one block */
memcpy(ctx->buf + n, data, left);
sm3_compress_neon(ctx->digest, ctx->buf, 1);
data += left;
datalen -= left;
}

/* compress the remaining data */
n = (datalen >> 6);
sm3_compress_neon(ctx->digest, data, n);

data += (SM3_BLOCK_SIZE*n);
datalen &= 0x3fU;

/* coppy the last few bytes to ctx->buf */
memcpy(ctx->buf, data, datalen);
return SM3_OK;
}
Expand All @@ -128,7 +132,10 @@ int sm3_final(u8 *digest, SM3_CTX *ctx)
u32 tdigest[8];
u32 *pdigest;

/* number of bytes in ctx->buf */
n = (ctx->bits >> 3) & 0x3fU;

/* copy ctx */
memcpy(tbuf, ctx->buf, n);
tdigest[0] = ctx->digest[0];
tdigest[1] = ctx->digest[1];
Expand All @@ -150,6 +157,8 @@ int sm3_final(u8 *digest, SM3_CTX *ctx)

*(u64*)(&tbuf[56]) = to_be64(ctx->bits);
sm3_compress_neon(tdigest, tbuf, 1);

/* big endian */
for (i = 0; i < 8; i++)
pdigest[i] = to_be32(tdigest[i]);

Expand Down
6 changes: 3 additions & 3 deletions sm3.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ extern "C" {
#endif

typedef struct {
u8 buf[SM3_BLOCK_SIZE];
u32 digest[8];
size_t bits;
u8 buf[SM3_BLOCK_SIZE]; // hold last few bytes that have not been processed
u32 digest[8]; // ...
size_t bits; // number of bits compressed
} SM3_CTX;

PIGGY_EXPORT const char* sm3_get_impl_name();
Expand Down
1 change: 1 addition & 0 deletions sm3_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef struct {
char *hash;
} SM3_TEST_VECTOR;

/* you can add more test vectors here :) */
static SM3_TEST_VECTOR sm3_test_vec[] =
{
/* 1 */
Expand Down

0 comments on commit e75ca01

Please sign in to comment.