Changeset 444 for trunk/src


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

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

Location:
trunk/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/cutest_sh_tiger0.c

    r248 r444  
    88
    99#include "sh_tiger.h"
     10#include "sh_checksum.h"
    1011
    1112#if defined(HAVE_PTHREAD) && defined(SH_STEALTH)
     
    135136  char * expected;
    136137  char hashbuf[KEYBUF_SIZE];
     138  char hexdigest[SHA256_DIGEST_STRING_LENGTH];
    137139  UINT64  length;
    138140
     
    197199  actual = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
    198200  expected = "2FE65D1D995B8F8BC8B13F798C07E7E935A787ED00000000";
     201  CuAssertStrEquals(tc, expected, actual);
     202
     203  result = sl_close(rval_open);
     204  CuAssertTrue(tc, result == 0);
     205
     206  result = sh_tiger_hashtype("SHA256");
     207  CuAssertTrue(tc, result == 0);
     208
     209  rval_open = sl_open_fastread (__FILE__, __LINE__, "cutest_foo", SL_YESPRIV);
     210  CuAssertTrue(tc, rval_open >= 0);
     211
     212  /* same result as gpg --print-md SHA256
     213   */
     214  length = TIGER_NOLIM;
     215  {
     216    char * tmp = sh_tiger_generic_hash("cutest_foo", rval_open, &length, 0, hashbuf, sizeof(hashbuf));
     217    actual = SHA256_Base2Hex(tmp, hexdigest);
     218  }
     219  expected = "235790848f95e96b2c627f1bf58a2b8c05c535ada8c0a3326aac34ce1391ad40";
    199220  CuAssertStrEquals(tc, expected, actual);
    200221
  • trunk/src/cutest_sh_utils.c

    r167 r444  
    139139
    140140  ret = sh_util_base64_enc (out, inp0, strlen((char*)inp0));
    141   CuAssertIntEquals(tc, ret, 0);
     141  CuAssertIntEquals(tc, 0, ret);
    142142  CuAssertStrEquals(tc, "", (char*)out);
    143143  ret = sh_util_base64_dec (ou2, out, strlen((char*)out));
  • trunk/src/sh_error.c

    r440 r444  
    867867}
    868868#endif
    869  
     869
     870#if defined(SH_WITH_CLIENT) || defined(SH_STANDALONE)
     871#include "sh_checksum.h"
     872static char * sh_error_replace(const char * msg)
     873{
     874  char * ret   = NULL;
     875
     876  if (sh_tiger_get_hashtype () == SH_SHA256)
     877    {
     878      char * store = NULL;
     879
     880#ifdef SH_USE_XML
     881      char c_end  = '"';
     882      char * str  = _("chksum_old=\"");
     883      char * str2 = _("chksum_new=\"");
     884#else
     885      char c_end  = '>';
     886      char * str  = _("chksum_old=<");
     887      char * str2 = _("chksum_new=<");
     888#endif
     889
     890      ret = SHA256_ReplaceBaseByHex(msg, str, c_end);
     891
     892      if (ret) {
     893        store = ret;
     894        ret   = SHA256_ReplaceBaseByHex(ret, str2, c_end);
     895        if (ret)
     896          SH_FREE(store);
     897        else
     898          ret = store;
     899      } else {
     900        ret   = SHA256_ReplaceBaseByHex(msg, str2, c_end);
     901      }
     902    }
     903  return ret;
     904}
     905static void sh_replace_free(char * msg)
     906{
     907  if (msg)
     908    SH_FREE(msg);
     909  return;
     910}
     911#else
     912static char * sh_error_replace(const char * msg) { (void) msg; return NULL; }
     913static void sh_replace_free(char * msg) { (void) msg; return; }
     914#endif
    870915
    871916/**********************************************************
     
    910955#endif
    911956
     957  char   * hexmsg = NULL;
     958
    912959  static int    own_block = 0;
    913960
     
    10831130  own_block = 0;
    10841131
     1132  hexmsg = sh_error_replace(lmsg->msg);
    10851133
    10861134  /* Log to stderr.
     
    11031151           *  Reports first error after failure. Always tries.
    11041152           */
    1105           (void) sh_log_console (lmsg->msg);
     1153          (void) sh_log_console (hexmsg ? hexmsg : lmsg->msg);
    11061154          print_block = 0;
    11071155        }
     
    11311179               * Ignores errors. Always tries.
    11321180               */
    1133               (void) sh_log_syslog (lmsg->severity, lmsg->msg);
     1181              (void) sh_log_syslog (lmsg->severity, hexmsg ? hexmsg : lmsg->msg);
    11341182              syslog_block = 0;
    11351183            }
     
    11531201               *  Reports first error after failure. Always tries.
    11541202               */
    1155               (void) sh_ext_execute ('l', 'o', 'g', lmsg->msg, 0);
     1203              (void) sh_ext_execute ('l', 'o', 'g', hexmsg ? hexmsg : lmsg->msg, 0);
    11561204              external_block = 0;
    11571205            }
     
    12701318              BREAKEXIT(sh_nmail_msg);
    12711319              if ( (severity & SH_ERR_FATAL) == 0)
    1272                 retval = sh_nmail_pushstack (severity, lmsg->msg, NULL);
     1320                retval = sh_nmail_pushstack (severity, hexmsg ? hexmsg : lmsg->msg, NULL);
    12731321              else
    1274                 retval = sh_nmail_msg (severity, lmsg->msg, NULL);
     1322                retval = sh_nmail_msg (severity, hexmsg ? hexmsg : lmsg->msg, NULL);
    12751323
    12761324              mail_block = 0;
     
    13061354#if defined(HAVE_LIBPRELUDE) && defined(SH_WITH_SERVER)
    13071355              (void) sh_prelude_alert (severity, (int) class,
    1308                                        lmsg->msg, lmsg->status, msg_id,
     1356                                       hexmsg ? hexmsg : lmsg->msg, lmsg->status, msg_id,
    13091357                                       local_inet_peer_ip);
    13101358#else
    13111359              (void) sh_prelude_alert (severity, (int) class,
    1312                                        lmsg->msg, lmsg->status, msg_id,
     1360                                       hexmsg ? hexmsg : lmsg->msg, lmsg->status, msg_id,
    13131361                                       NULL);
    13141362#endif
     
    13511399                }
    13521400#else
    1353               (void) sh_log_file (lmsg->msg, NULL);
     1401              (void) sh_log_file (hexmsg ? hexmsg : lmsg->msg, NULL);
    13541402#endif
    13551403              /* sh_log_file (lmsg->msg); */
     
    13661414  if (lmsg->msg)
    13671415    SH_FREE( lmsg->msg );
     1416  sh_replace_free(hexmsg);
    13681417
    13691418  memset ( lmsg, (int) '\0', sizeof(struct _log_t) );
  • trunk/src/sh_hash.c

    r425 r444  
    3434#include <sys/stat.h>
    3535#include <unistd.h>
     36#include <ctype.h>
    3637
    3738#ifdef MAJOR_IN_MKDEV
     
    39733974}
    39743975
    3975 
     3976int isHexKey(char * s)
     3977{
     3978  int i;
     3979 
     3980  for (i = 0; i < KEY_LEN; ++i)
     3981    {
     3982      if (*s)
     3983        {
     3984          if ((*s >= '0' && *s <= '9') ||
     3985              (*s >= 'A' && *s <= 'F') ||
     3986              (*s >= 'a' && *s <= 'f'))
     3987            {
     3988              ++s;
     3989              continue;
     3990            }
     3991        }
     3992      return S_FALSE;
     3993    }
     3994  return S_TRUE;
     3995}
    39763996 
     3997#include "sh_checksum.h"
     3998
     3999static char * KEYBUFtolower (char * s, char * result)
     4000{
     4001  char * r = result;
     4002  if (s)
     4003    {
     4004      for (; *s; ++s)
     4005        {
     4006          *r = tolower((unsigned char) *s); ++r;
     4007        }
     4008      *r = '\0';
     4009    }
     4010  return result;
     4011}
     4012
    39774013void sh_hash_list_db_entry_full_detail (sh_file_t * p)
    39784014{
     
    39804016  char * esc;
    39814017  char   str[81];
     4018  char   hexdigest[SHA256_DIGEST_STRING_LENGTH];
     4019  char   keybuffer[KEYBUF_SIZE];
    39824020
    39834021  if (ListWithDelimiter == S_TRUE)
     
    40204058  if (ListWithDelimiter == S_TRUE)
    40214059    putchar(',');
    4022   printf( _(" %s"), p->theFile.checksum);
     4060
     4061  if (isHexKey(p->theFile.checksum))
     4062    printf( _(" %s"), KEYBUFtolower(p->theFile.checksum, keybuffer));
     4063  else
     4064    printf( _(" %s"), SHA256_Base2Hex(p->theFile.checksum, hexdigest));
    40234065  if (ListWithDelimiter == S_TRUE)
    40244066    putchar(',');
  • 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);
  • trunk/src/sh_utils.c

    r382 r444  
    22392239  if (bto64[0] != '\0')
    22402240    {
    2241       if (instr && *instr)
     2241      if (instr /* && *instr *//* need to handle binary data */)
    22422242        {
    22432243          if (lin == 0)
    22442244            lin = strlen((const char *)instr);
    22452245
    2246           do {
    2247             ll = 0;
    2248 
    2249             if (len < lin)
    2250               { a = *instr; ++instr; ++len; ++ll; }
    2251             else
    2252               { a = 0; }
    2253             if (len < lin)
    2254               { b = *instr; ++instr; ++len; ++ll; }
    2255             else
    2256               { b = 0; }
    2257             if (len < lin)
    2258               { c = *instr; ++instr; ++len; ++ll; }
    2259             else
    2260               { c = 0; }
    2261 
    2262             *out = bto64[ a >> 2 ];
    2263             ++j; ++out;
    2264             *out = bto64[ ((a & 0x03) << 4) | ((b & 0xf0) >> 4) ];
    2265             ++j; ++out;
    2266             *out = (unsigned char) (ll > 1 ? bto64[ ((b & 0x0f) << 2) | ((c & 0xc0) >> 6) ] : '?');
    2267             ++j; ++out;
    2268             *out = (unsigned char) (ll > 2 ? bto64[ c & 0x3f ] : '?');
    2269             ++j; ++out;
    2270           } while (len < lin);
     2246          if (lin > 0)
     2247            {
     2248              do {
     2249                ll = 0;
     2250               
     2251                if (len < lin)
     2252                  { a = *instr; ++instr; ++len; ++ll; }
     2253                else
     2254                  { a = 0; }
     2255                if (len < lin)
     2256                  { b = *instr; ++instr; ++len; ++ll; }
     2257                else
     2258                  { b = 0; }
     2259                if (len < lin)
     2260                  { c = *instr; ++instr; ++len; ++ll; }
     2261                else
     2262                  { c = 0; }
     2263               
     2264                *out = bto64[ a >> 2 ];
     2265                ++j; ++out;
     2266                *out = bto64[ ((a & 0x03) << 4) | ((b & 0xf0) >> 4) ];
     2267                ++j; ++out;
     2268                *out = (unsigned char) (ll > 1 ? bto64[ ((b & 0x0f) << 2) | ((c & 0xc0) >> 6) ] : '?');
     2269                ++j; ++out;
     2270                *out = (unsigned char) (ll > 2 ? bto64[ c & 0x3f ] : '?');
     2271                ++j; ++out;
     2272              } while (len < lin);
     2273            }
    22712274        }
    22722275      *out = '\0';
Note: See TracChangeset for help on using the changeset viewer.