Changeset 444 for trunk/src/sh_tiger0.c


Ignore:
Timestamp:
Oct 31, 2013, 11:31:47 PM (11 years ago)
Author:
katerina
Message:

Support for sha2-256 checksum (ticket #348).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_tiger0.c

    r440 r444  
    13861386/*@+type@*/
    13871387
     1388#include "sh_checksum.h"
     1389
     1390#define SH_VAR_SHA1   0
     1391#define SH_VAR_SHA256 1
     1392
    13881393/* Compute SHA1 message digest for bytes read from STREAM.  The
    13891394   resulting message digest number will be written into the 16 bytes
    13901395   beginning at RESBLOCK.  */
    1391 static int sha1_stream(char * filename, void *resblock,
    1392                        UINT64 * Length, int timeout, SL_TICKET fd)
     1396static int SHA_stream(char * filename, void *resblock,
     1397                      UINT64 * Length, int timeout, SL_TICKET fd, int variant)
    13931398{
    13941399  /* Important: BLOCKSIZE must be a multiple of 64.  */
    13951400  static const int BLOCKSIZE = 4096;
    13961401  struct sha_ctx ctx;
     1402  SHA256_CTX ctx_sha2;
    13971403  char * buffer = SH_ALLOC(4168); /* BLOCKSIZE + 72 AIX compiler chokes */
    13981404  off_t sum = 0;
     
    14101416
    14111417  /* Initialize the computation context.  */
    1412   (void) sha_init(&ctx);
     1418  if (variant == SH_VAR_SHA256)
     1419    (void) SHA256_Init(&ctx_sha2);
     1420  else
     1421    (void) sha_init(&ctx);
    14131422
    14141423  if (SL_ISERROR (fd))
     
    15061515       BLOCKSIZE % 64 == 0
    15071516    */
    1508     sha_update(&ctx, (sha_word8*) buffer, (sha_word32) BLOCKSIZE);
     1517    if (variant == SH_VAR_SHA256)
     1518      SHA256_Update(&ctx_sha2, (sha2_byte*) buffer, (size_t) BLOCKSIZE);
     1519    else
     1520      sha_update(&ctx, (sha_word8*) buffer, (sha_word32) BLOCKSIZE);
    15091521    sh.statistics.bytes_hashed += BLOCKSIZE;
    15101522
     
    15281540  if (sum > 0)
    15291541    {
    1530       sha_update(&ctx, (sha_word8*) buffer, (sha_word32) sum);
     1542      if (variant == SH_VAR_SHA256)
     1543        SHA256_Update(&ctx_sha2, (sha2_byte*) buffer, (size_t) sum);
     1544      else
     1545        sha_update(&ctx, (sha_word8*) buffer, (sha_word32) sum);
    15311546      sh.statistics.bytes_hashed += sum;
    15321547    }
    15331548
    1534   sha_final (&ctx);
    1535 
    15361549  /* Construct result in desired memory.  */
    1537   sha_digest (&ctx, resblock);
     1550  if (variant == SH_VAR_SHA256)
     1551    {
     1552      SHA256_End(&ctx_sha2, resblock);
     1553    }
     1554  else
     1555    {
     1556      sha_final (&ctx);
     1557      sha_digest (&ctx, resblock);
     1558    }
     1559
    15381560  *Length = bcount;
    15391561  SH_FREE(buffer);
     
    15501572  unsigned char sha1buffer[20];
    15511573
    1552   (void) sha1_stream (filename, sha1buffer, Length, timeout, what);
     1574  (void) SHA_stream (filename, sha1buffer, Length, timeout, what, SH_VAR_SHA1);
    15531575
    15541576  /*@-bufferoverflowhigh -usedef@*/
     
    15651587}
    15661588
     1589static char * sh_tiger_sha256_hash  (char * filename, TigerType what,
     1590                                     UINT64 * Length, int timeout,
     1591                                     char * out, size_t len)
     1592{
     1593  char outbuf[KEYBUF_SIZE];
     1594
     1595  (void) SHA_stream (filename, outbuf, Length, timeout, what, SH_VAR_SHA256);
     1596
     1597  sl_strlcpy(out, outbuf, len);
     1598  return out;
     1599}
     1600
    15671601/* ifdef USE_SHA1 */
    15681602#endif
    15691603
    1570 static int hash_type = 0;
     1604static int hash_type = SH_TIGER192;
    15711605
    15721606int sh_tiger_get_hashtype ()
     
    15851619
    15861620  if (0 == strcmp(c, _("TIGER192")))
    1587     hash_type = 0;
     1621    hash_type = SH_TIGER192;
    15881622#ifdef USE_SHA1
    15891623  else if (0 == strcmp(c, _("SHA1")))   
    1590     hash_type = 1;
     1624    hash_type = SH_SHA1;
    15911625#endif
    15921626#ifdef USE_MD5
    15931627  else if (0 == strcmp(c, _("MD5")))   
    1594     hash_type = 2;
     1628    hash_type = SH_MD5;
     1629#endif
     1630#ifdef USE_SHA1
     1631  else if (0 == strcmp(c, _("SHA256")))   
     1632    hash_type = SH_SHA256;
    15951633#endif
    15961634  else
     
    16181656{
    16191657#ifdef USE_SHA1
    1620   if (hash_type == 1)
     1658  if (hash_type == SH_SHA1)
    16211659    return sh_tiger_sha1_hash    (filename, what, Length, timeout, out, len);
    16221660#endif
    16231661#ifdef USE_MD5
    1624   if (hash_type == 2)
     1662  if (hash_type == SH_MD5)
    16251663    return sh_tiger_md5_hash     (filename, what, Length, timeout, out, len);
     1664#endif
     1665#ifdef USE_SHA1
     1666  if (hash_type == SH_SHA256)
     1667    return sh_tiger_sha256_hash  (filename, what, Length, timeout, out, len);
    16261668#endif
    16271669  return sh_tiger_hash_internal  (filename, what, Length, timeout, out, len);
Note: See TracChangeset for help on using the changeset viewer.