sha2: Write final hash directly to output buffer
This avoids having the last output in internal memory that's not wiped. References #2388.
This commit is contained in:
@@ -226,7 +226,7 @@ static void sha256_write(private_sha256_hasher_t *ctx,
|
|||||||
/**
|
/**
|
||||||
* finalize SHA256 hash
|
* finalize SHA256 hash
|
||||||
*/
|
*/
|
||||||
static void sha256_final(private_sha256_hasher_t *ctx)
|
static void sha256_final(private_sha256_hasher_t *ctx, u_char *buf, size_t len)
|
||||||
{
|
{
|
||||||
register int j;
|
register int j;
|
||||||
uint64_t bitLength;
|
uint64_t bitLength;
|
||||||
@@ -255,8 +255,7 @@ static void sha256_final(private_sha256_hasher_t *ctx)
|
|||||||
ctx->sha_out[63] = bitLength;
|
ctx->sha_out[63] = bitLength;
|
||||||
sha256_transform(ctx, &ctx->sha_out[0]);
|
sha256_transform(ctx, &ctx->sha_out[0]);
|
||||||
|
|
||||||
/* return results in ctx->sha_out[0...31] */
|
datap = buf;
|
||||||
datap = &ctx->sha_out[0];
|
|
||||||
j = 0;
|
j = 0;
|
||||||
do {
|
do {
|
||||||
i = ctx->sha_H[j];
|
i = ctx->sha_H[j];
|
||||||
@@ -265,7 +264,7 @@ static void sha256_final(private_sha256_hasher_t *ctx)
|
|||||||
datap[2] = i >> 8;
|
datap[2] = i >> 8;
|
||||||
datap[3] = i;
|
datap[3] = i;
|
||||||
datap += 4;
|
datap += 4;
|
||||||
} while(++j < 8);
|
} while(++j < len / 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* update macros for SHA512 */
|
/* update macros for SHA512 */
|
||||||
@@ -371,7 +370,7 @@ static void sha512_write(private_sha512_hasher_t *ctx,
|
|||||||
/**
|
/**
|
||||||
* Finalize a SHA384/SHA512 hash
|
* Finalize a SHA384/SHA512 hash
|
||||||
*/
|
*/
|
||||||
static void sha512_final(private_sha512_hasher_t *ctx)
|
static void sha512_final(private_sha512_hasher_t *ctx, u_char *buf, size_t len)
|
||||||
{
|
{
|
||||||
register int j;
|
register int j;
|
||||||
uint64_t bitLength, bitLengthMSB;
|
uint64_t bitLength, bitLengthMSB;
|
||||||
@@ -409,8 +408,7 @@ static void sha512_final(private_sha512_hasher_t *ctx)
|
|||||||
ctx->sha_out[127] = bitLength;
|
ctx->sha_out[127] = bitLength;
|
||||||
sha512_transform(ctx, &ctx->sha_out[0]);
|
sha512_transform(ctx, &ctx->sha_out[0]);
|
||||||
|
|
||||||
/* return results in ctx->sha_out[0...63] */
|
datap = buf;
|
||||||
datap = &ctx->sha_out[0];
|
|
||||||
j = 0;
|
j = 0;
|
||||||
do {
|
do {
|
||||||
i = ctx->sha_H[j];
|
i = ctx->sha_H[j];
|
||||||
@@ -423,7 +421,7 @@ static void sha512_final(private_sha512_hasher_t *ctx)
|
|||||||
datap[6] = i >> 8;
|
datap[6] = i >> 8;
|
||||||
datap[7] = i;
|
datap[7] = i;
|
||||||
datap += 8;
|
datap += 8;
|
||||||
} while(++j < 8);
|
} while(++j < len / 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
METHOD(hasher_t, reset224, bool,
|
METHOD(hasher_t, reset224, bool,
|
||||||
@@ -432,7 +430,6 @@ METHOD(hasher_t, reset224, bool,
|
|||||||
memcpy(&this->sha_H[0], &sha224_hashInit[0], sizeof(this->sha_H));
|
memcpy(&this->sha_H[0], &sha224_hashInit[0], sizeof(this->sha_H));
|
||||||
this->sha_blocks = 0;
|
this->sha_blocks = 0;
|
||||||
this->sha_bufCnt = 0;
|
this->sha_bufCnt = 0;
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -442,7 +439,6 @@ METHOD(hasher_t, reset256, bool,
|
|||||||
memcpy(&this->sha_H[0], &sha256_hashInit[0], sizeof(this->sha_H));
|
memcpy(&this->sha_H[0], &sha256_hashInit[0], sizeof(this->sha_H));
|
||||||
this->sha_blocks = 0;
|
this->sha_blocks = 0;
|
||||||
this->sha_bufCnt = 0;
|
this->sha_bufCnt = 0;
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -453,7 +449,6 @@ METHOD(hasher_t, reset384, bool,
|
|||||||
this->sha_blocks = 0;
|
this->sha_blocks = 0;
|
||||||
this->sha_blocksMSB = 0;
|
this->sha_blocksMSB = 0;
|
||||||
this->sha_bufCnt = 0;
|
this->sha_bufCnt = 0;
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -464,7 +459,6 @@ METHOD(hasher_t, reset512, bool,
|
|||||||
this->sha_blocks = 0;
|
this->sha_blocks = 0;
|
||||||
this->sha_blocksMSB = 0;
|
this->sha_blocksMSB = 0;
|
||||||
this->sha_bufCnt = 0;
|
this->sha_bufCnt = 0;
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -474,8 +468,7 @@ METHOD(hasher_t, get_hash224, bool,
|
|||||||
sha256_write(this, chunk.ptr, chunk.len);
|
sha256_write(this, chunk.ptr, chunk.len);
|
||||||
if (buffer != NULL)
|
if (buffer != NULL)
|
||||||
{
|
{
|
||||||
sha256_final(this);
|
sha256_final(this, buffer, HASH_SIZE_SHA224);
|
||||||
memcpy(buffer, this->sha_out, HASH_SIZE_SHA224);
|
|
||||||
reset224(this);
|
reset224(this);
|
||||||
}
|
}
|
||||||
return TRUE;
|
return TRUE;
|
||||||
@@ -487,8 +480,7 @@ METHOD(hasher_t, get_hash256, bool,
|
|||||||
sha256_write(this, chunk.ptr, chunk.len);
|
sha256_write(this, chunk.ptr, chunk.len);
|
||||||
if (buffer != NULL)
|
if (buffer != NULL)
|
||||||
{
|
{
|
||||||
sha256_final(this);
|
sha256_final(this, buffer, HASH_SIZE_SHA256);
|
||||||
memcpy(buffer, this->sha_out, HASH_SIZE_SHA256);
|
|
||||||
reset256(this);
|
reset256(this);
|
||||||
}
|
}
|
||||||
return TRUE;
|
return TRUE;
|
||||||
@@ -500,8 +492,7 @@ METHOD(hasher_t, get_hash384, bool,
|
|||||||
sha512_write(this, chunk.ptr, chunk.len);
|
sha512_write(this, chunk.ptr, chunk.len);
|
||||||
if (buffer != NULL)
|
if (buffer != NULL)
|
||||||
{
|
{
|
||||||
sha512_final(this);
|
sha512_final(this, buffer, HASH_SIZE_SHA384);
|
||||||
memcpy(buffer, this->sha_out, HASH_SIZE_SHA384);
|
|
||||||
reset384(this);
|
reset384(this);
|
||||||
}
|
}
|
||||||
return TRUE;
|
return TRUE;
|
||||||
@@ -513,8 +504,7 @@ METHOD(hasher_t, get_hash512, bool,
|
|||||||
sha512_write(this, chunk.ptr, chunk.len);
|
sha512_write(this, chunk.ptr, chunk.len);
|
||||||
if (buffer != NULL)
|
if (buffer != NULL)
|
||||||
{
|
{
|
||||||
sha512_final(this);
|
sha512_final(this, buffer, HASH_SIZE_SHA512);
|
||||||
memcpy(buffer, this->sha_out, HASH_SIZE_SHA512);
|
|
||||||
reset512(this);
|
reset512(this);
|
||||||
}
|
}
|
||||||
return TRUE;
|
return TRUE;
|
||||||
@@ -523,69 +513,49 @@ METHOD(hasher_t, get_hash512, bool,
|
|||||||
METHOD(hasher_t, allocate_hash224, bool,
|
METHOD(hasher_t, allocate_hash224, bool,
|
||||||
private_sha256_hasher_t *this, chunk_t chunk, chunk_t *hash)
|
private_sha256_hasher_t *this, chunk_t chunk, chunk_t *hash)
|
||||||
{
|
{
|
||||||
chunk_t allocated_hash;
|
chunk_t allocated_hash = chunk_empty;
|
||||||
|
|
||||||
sha256_write(this, chunk.ptr, chunk.len);
|
if (hash)
|
||||||
if (hash != NULL)
|
|
||||||
{
|
{
|
||||||
sha256_final(this);
|
*hash = allocated_hash = chunk_alloc(HASH_SIZE_SHA224);
|
||||||
allocated_hash = chunk_alloc(HASH_SIZE_SHA224);
|
|
||||||
memcpy(allocated_hash.ptr, this->sha_out, HASH_SIZE_SHA224);
|
|
||||||
reset224(this);
|
|
||||||
*hash = allocated_hash;
|
|
||||||
}
|
}
|
||||||
return TRUE;
|
return get_hash224(this, chunk, allocated_hash.ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
METHOD(hasher_t, allocate_hash256, bool,
|
METHOD(hasher_t, allocate_hash256, bool,
|
||||||
private_sha256_hasher_t *this, chunk_t chunk, chunk_t *hash)
|
private_sha256_hasher_t *this, chunk_t chunk, chunk_t *hash)
|
||||||
{
|
{
|
||||||
chunk_t allocated_hash;
|
chunk_t allocated_hash = chunk_empty;
|
||||||
|
|
||||||
sha256_write(this, chunk.ptr, chunk.len);
|
if (hash)
|
||||||
if (hash != NULL)
|
|
||||||
{
|
{
|
||||||
sha256_final(this);
|
*hash = allocated_hash = chunk_alloc(HASH_SIZE_SHA256);
|
||||||
allocated_hash = chunk_alloc(HASH_SIZE_SHA256);
|
|
||||||
memcpy(allocated_hash.ptr, this->sha_out, HASH_SIZE_SHA256);
|
|
||||||
reset256(this);
|
|
||||||
*hash = allocated_hash;
|
|
||||||
}
|
}
|
||||||
return TRUE;
|
return get_hash256(this, chunk, allocated_hash.ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
METHOD(hasher_t, allocate_hash384, bool,
|
METHOD(hasher_t, allocate_hash384, bool,
|
||||||
private_sha512_hasher_t *this, chunk_t chunk, chunk_t *hash)
|
private_sha512_hasher_t *this, chunk_t chunk, chunk_t *hash)
|
||||||
{
|
{
|
||||||
chunk_t allocated_hash;
|
chunk_t allocated_hash = chunk_empty;
|
||||||
|
|
||||||
sha512_write(this, chunk.ptr, chunk.len);
|
if (hash)
|
||||||
if (hash != NULL)
|
|
||||||
{
|
{
|
||||||
sha512_final(this);
|
*hash = allocated_hash = chunk_alloc(HASH_SIZE_SHA384);
|
||||||
allocated_hash = chunk_alloc(HASH_SIZE_SHA384);
|
|
||||||
memcpy(allocated_hash.ptr, this->sha_out, HASH_SIZE_SHA384);
|
|
||||||
reset384(this);
|
|
||||||
*hash = allocated_hash;
|
|
||||||
}
|
}
|
||||||
return TRUE;
|
return get_hash384(this, chunk, allocated_hash.ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
METHOD(hasher_t, allocate_hash512, bool,
|
METHOD(hasher_t, allocate_hash512, bool,
|
||||||
private_sha512_hasher_t *this, chunk_t chunk, chunk_t *hash)
|
private_sha512_hasher_t *this, chunk_t chunk, chunk_t *hash)
|
||||||
{
|
{
|
||||||
chunk_t allocated_hash;
|
chunk_t allocated_hash = chunk_empty;
|
||||||
|
|
||||||
sha512_write(this, chunk.ptr, chunk.len);
|
if (hash)
|
||||||
if (hash != NULL)
|
|
||||||
{
|
{
|
||||||
sha512_final(this);
|
*hash = allocated_hash = chunk_alloc(HASH_SIZE_SHA512);
|
||||||
allocated_hash = chunk_alloc(HASH_SIZE_SHA512);
|
|
||||||
memcpy(allocated_hash.ptr, this->sha_out, HASH_SIZE_SHA512);
|
|
||||||
reset512(this);
|
|
||||||
*hash = allocated_hash;
|
|
||||||
}
|
}
|
||||||
return TRUE;
|
return get_hash512(this, chunk, allocated_hash.ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
METHOD(hasher_t, get_hash_size224, size_t,
|
METHOD(hasher_t, get_hash_size224, size_t,
|
||||||
|
|||||||
Reference in New Issue
Block a user