summaryrefslogtreecommitdiff
path: root/hash_tomcrypt.c
blob: 51b63d9b7841d7d6f9ab7ae760fba5e98f2e143f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
  chronyd/chronyc - Programs for keeping computer clocks accurate.

 **********************************************************************
 * Copyright (C) Miroslav Lichvar  2012
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 **********************************************************************

  =======================================================================

  Routines implementing crypto hashing using tomcrypt library.

  */

#include <tomcrypt.h>

#include "config.h"
#include "hash.h"
#include "util.h"

struct hash {
  const char *name;
  const char *int_name;
  const struct ltc_hash_descriptor *desc;
};

static const struct hash hashes[] = {
  { "MD5", "md5", &md5_desc },
#ifdef LTC_RIPEMD128
  { "RMD128", "rmd128", &rmd128_desc },
#endif
#ifdef LTC_RIPEMD160
  { "RMD160", "rmd160", &rmd160_desc },
#endif
#ifdef LTC_RIPEMD256
  { "RMD256", "rmd256", &rmd256_desc },
#endif
#ifdef LTC_RIPEMD320
  { "RMD320", "rmd320", &rmd320_desc },
#endif
#ifdef LTC_SHA1
  { "SHA1", "sha1", &sha1_desc },
#endif
#ifdef LTC_SHA256
  { "SHA256", "sha256", &sha256_desc },
#endif
#ifdef LTC_SHA384
  { "SHA384", "sha384", &sha384_desc },
#endif
#ifdef LTC_SHA512
  { "SHA512", "sha512", &sha512_desc },
#endif
#ifdef LTC_SHA3
  { "SHA3-224", "sha3-224", &sha3_224_desc },
  { "SHA3-256", "sha3-256", &sha3_256_desc },
  { "SHA3-384", "sha3-384", &sha3_384_desc },
  { "SHA3-512", "sha3-512", &sha3_512_desc },
#endif
#ifdef LTC_TIGER
  { "TIGER", "tiger", &tiger_desc },
#endif
#ifdef LTC_WHIRLPOOL
  { "WHIRLPOOL", "whirlpool", &whirlpool_desc },
#endif
  { NULL, NULL, NULL }
};

int
HSH_GetHashId(const char *name)
{
  int i, h;

  for (i = 0; hashes[i].name; i++) {
    if (!strcmp(name, hashes[i].name))
      break;
  }

  if (!hashes[i].name)
    return -1; /* not found */

  h = find_hash(hashes[i].int_name);
  if (h >= 0)
    return h; /* already registered */
  
  /* register and try again */
  register_hash(hashes[i].desc);

  return find_hash(hashes[i].int_name);
}

unsigned int
HSH_Hash(int id, const unsigned char *in1, unsigned int in1_len,
    const unsigned char *in2, unsigned int in2_len,
    unsigned char *out, unsigned int out_len)
{
  unsigned char buf[MAX_HASH_LENGTH];
  unsigned long len;
  int r;

  len = sizeof (buf);
  if (in2)
    r = hash_memory_multi(id, buf, &len,
                          in1, (unsigned long)in1_len,
                          in2, (unsigned long)in2_len, NULL, 0);
  else
    r = hash_memory(id, in1, in1_len, buf, &len);

  if (r != CRYPT_OK)
    return 0;

  len = MIN(len, out_len);
  memcpy(out, buf, len);

  return len;
}

void
HSH_Finalise(void)
{
}