Blame hash_tomcrypt.c

Packit 96c956
/*
Packit 96c956
  chronyd/chronyc - Programs for keeping computer clocks accurate.
Packit 96c956
Packit 96c956
 **********************************************************************
Packit 96c956
 * Copyright (C) Miroslav Lichvar  2012, 2018
Packit 96c956
 * 
Packit 96c956
 * This program is free software; you can redistribute it and/or modify
Packit 96c956
 * it under the terms of version 2 of the GNU General Public License as
Packit 96c956
 * published by the Free Software Foundation.
Packit 96c956
 * 
Packit 96c956
 * This program is distributed in the hope that it will be useful, but
Packit 96c956
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 96c956
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 96c956
 * General Public License for more details.
Packit 96c956
 * 
Packit 96c956
 * You should have received a copy of the GNU General Public License along
Packit 96c956
 * with this program; if not, write to the Free Software Foundation, Inc.,
Packit 96c956
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Packit 96c956
 * 
Packit 96c956
 **********************************************************************
Packit 96c956
Packit 96c956
  =======================================================================
Packit 96c956
Packit 96c956
  Routines implementing crypto hashing using tomcrypt library.
Packit 96c956
Packit 96c956
  */
Packit 96c956
Packit 96c956
#include <tomcrypt.h>
Packit 96c956
Packit 96c956
#include "config.h"
Packit 96c956
#include "hash.h"
Packit 96c956
#include "util.h"
Packit 96c956
Packit 96c956
struct hash {
Packit 96c956
  const char *name;
Packit 96c956
  const char *int_name;
Packit 96c956
  const struct ltc_hash_descriptor *desc;
Packit 96c956
};
Packit 96c956
Packit 96c956
static const struct hash hashes[] = {
Packit 96c956
  { "MD5", "md5", &md5_desc },
Packit 96c956
#ifdef LTC_RIPEMD128
Packit 96c956
  { "RMD128", "rmd128", &rmd128_desc },
Packit 96c956
#endif
Packit 96c956
#ifdef LTC_RIPEMD160
Packit 96c956
  { "RMD160", "rmd160", &rmd160_desc },
Packit 96c956
#endif
Packit 96c956
#ifdef LTC_RIPEMD256
Packit 96c956
  { "RMD256", "rmd256", &rmd256_desc },
Packit 96c956
#endif
Packit 96c956
#ifdef LTC_RIPEMD320
Packit 96c956
  { "RMD320", "rmd320", &rmd320_desc },
Packit 96c956
#endif
Packit 96c956
#ifdef LTC_SHA1
Packit 96c956
  { "SHA1", "sha1", &sha1_desc },
Packit 96c956
#endif
Packit 96c956
#ifdef LTC_SHA256
Packit 96c956
  { "SHA256", "sha256", &sha256_desc },
Packit 96c956
#endif
Packit 96c956
#ifdef LTC_SHA384
Packit 96c956
  { "SHA384", "sha384", &sha384_desc },
Packit 96c956
#endif
Packit 96c956
#ifdef LTC_SHA512
Packit 96c956
  { "SHA512", "sha512", &sha512_desc },
Packit 96c956
#endif
Packit 96c956
#ifdef LTC_SHA3
Packit 96c956
  { "SHA3-224", "sha3-224", &sha3_224_desc },
Packit 96c956
  { "SHA3-256", "sha3-256", &sha3_256_desc },
Packit 96c956
  { "SHA3-384", "sha3-384", &sha3_384_desc },
Packit 96c956
  { "SHA3-512", "sha3-512", &sha3_512_desc },
Packit 96c956
#endif
Packit 96c956
#ifdef LTC_TIGER
Packit 96c956
  { "TIGER", "tiger", &tiger_desc },
Packit 96c956
#endif
Packit 96c956
#ifdef LTC_WHIRLPOOL
Packit 96c956
  { "WHIRLPOOL", "whirlpool", &whirlpool_desc },
Packit 96c956
#endif
Packit 96c956
  { NULL, NULL, NULL }
Packit 96c956
};
Packit 96c956
Packit 96c956
int
Packit 96c956
HSH_GetHashId(const char *name)
Packit 96c956
{
Packit 96c956
  int i, h;
Packit 96c956
Packit 96c956
  for (i = 0; hashes[i].name; i++) {
Packit 96c956
    if (!strcmp(name, hashes[i].name))
Packit 96c956
      break;
Packit 96c956
  }
Packit 96c956
Packit 96c956
  if (!hashes[i].name)
Packit 96c956
    return -1; /* not found */
Packit 96c956
Packit 96c956
  h = find_hash(hashes[i].int_name);
Packit 96c956
  if (h >= 0)
Packit 96c956
    return h; /* already registered */
Packit 96c956
  
Packit 96c956
  /* register and try again */
Packit 96c956
  register_hash(hashes[i].desc);
Packit 96c956
Packit 96c956
  return find_hash(hashes[i].int_name);
Packit 96c956
}
Packit 96c956
Packit 96c956
unsigned int
Packit 96c956
HSH_Hash(int id, const unsigned char *in1, unsigned int in1_len,
Packit 96c956
    const unsigned char *in2, unsigned int in2_len,
Packit 96c956
    unsigned char *out, unsigned int out_len)
Packit 96c956
{
Packit 96c956
  unsigned char buf[MAX_HASH_LENGTH];
Packit 96c956
  unsigned long len;
Packit 96c956
  int r;
Packit 96c956
Packit 96c956
  len = sizeof (buf);
Packit 96c956
  if (in2)
Packit 96c956
    r = hash_memory_multi(id, buf, &len,
Packit 96c956
                          in1, (unsigned long)in1_len,
Packit 96c956
                          in2, (unsigned long)in2_len, NULL, 0);
Packit 96c956
  else
Packit 96c956
    r = hash_memory(id, in1, in1_len, buf, &len;;
Packit 96c956
Packit 96c956
  if (r != CRYPT_OK)
Packit 96c956
    return 0;
Packit 96c956
Packit 96c956
  len = MIN(len, out_len);
Packit 96c956
  memcpy(out, buf, len);
Packit 96c956
Packit 96c956
  return len;
Packit 96c956
}
Packit 96c956
Packit 96c956
void
Packit 96c956
HSH_Finalise(void)
Packit 96c956
{
Packit 96c956
}