Skip to content

Commit

Permalink
Reduce overhead on generic hash write function
Browse files Browse the repository at this point in the history
* cipher/hash-common.c (_gcry_md_block_write): Remove recursive
function call; Use buf_cpy for copying buffers; Burn stack only once.
--

Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
  • Loading branch information
jkivilin committed Mar 23, 2019
1 parent f8d14df commit e76617c
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions cipher/hash-common.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#endif

#include "g10lib.h"
#include "bufhelp.h"
#include "hash-common.h"


Expand Down Expand Up @@ -121,47 +122,64 @@ _gcry_md_block_write (void *context, const void *inbuf_arg, size_t inlen)
const unsigned char *inbuf = inbuf_arg;
gcry_md_block_ctx_t *hd = context;
unsigned int stack_burn = 0;
unsigned int nburn;
const unsigned int blocksize = hd->blocksize;
size_t inblocks;
size_t copylen;

if (sizeof(hd->buf) < blocksize)
BUG();

if (!hd->bwrite)
return;

if (hd->count == blocksize) /* Flush the buffer. */
while (hd->count)
{
stack_burn = hd->bwrite (hd, hd->buf, 1);
_gcry_burn_stack (stack_burn);
stack_burn = 0;
hd->count = 0;
if (!++hd->nblocks)
hd->nblocks_high++;
}
if (!inbuf)
return;
if (hd->count == blocksize) /* Flush the buffer. */
{
nburn = hd->bwrite (hd, hd->buf, 1);
stack_burn = nburn > stack_burn ? nburn : stack_burn;
hd->count = 0;
if (!++hd->nblocks)
hd->nblocks_high++;
}
else
{
copylen = inlen;
if (copylen > blocksize - hd->count)
copylen = blocksize - hd->count;

if (hd->count)
{
for (; inlen && hd->count < blocksize; inlen--)
hd->buf[hd->count++] = *inbuf++;
_gcry_md_block_write (hd, NULL, 0);
if (!inlen)
return;
if (copylen == 0)
break;

buf_cpy (&hd->buf[hd->count], inbuf, copylen);
hd->count += copylen;
inbuf += copylen;
inlen -= copylen;
}
}

if (inlen == 0)
return;

if (inlen >= blocksize)
{
inblocks = inlen / blocksize;
stack_burn = hd->bwrite (hd, inbuf, inblocks);
nburn = hd->bwrite (hd, inbuf, inblocks);
stack_burn = nburn > stack_burn ? nburn : stack_burn;
hd->count = 0;
hd->nblocks_high += (hd->nblocks + inblocks < inblocks);
hd->nblocks += inblocks;
inlen -= inblocks * blocksize;
inbuf += inblocks * blocksize;
}
_gcry_burn_stack (stack_burn);
for (; inlen && hd->count < blocksize; inlen--)
hd->buf[hd->count++] = *inbuf++;

if (inlen)
{
buf_cpy (hd->buf, inbuf, inlen);
hd->count = inlen;
}

if (stack_burn > 0)
_gcry_burn_stack (stack_burn);
}

0 comments on commit e76617c

Please sign in to comment.