summaryrefslogtreecommitdiffstats
path: root/hash_nettle.c
blob: 2c3501d8e97ee97c2d19b479596aeec1d9abc6cc (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
/*
  chronyd/chronyc - Programs for keeping computer clocks accurate.

 **********************************************************************
 * Copyright (C) Miroslav Lichvar  2018
 * 
 * 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 the nettle library.

  */

#include "config.h"

#include "sysincl.h"

#include <nettle/nettle-meta.h>

#include "hash.h"
#include "memory.h"

struct hash {
  const char *name;
  const char *int_name;
  const struct nettle_hash *nettle_hash;
  void *context;
};

static struct hash hashes[] = {
  { "MD5", "md5", NULL, NULL },
  { "RMD160", "ripemd160", NULL, NULL },
  { "SHA1", "sha1", NULL, NULL },
  { "SHA256", "sha256", NULL, NULL },
  { "SHA384", "sha384", NULL, NULL },
  { "SHA512", "sha512", NULL, NULL },
  { "SHA3-224", "sha3_224", NULL, NULL },
  { "SHA3-256", "sha3_256", NULL, NULL },
  { "SHA3-384", "sha3_384", NULL, NULL },
  { "SHA3-512", "sha3_512", NULL, NULL },
  { NULL, NULL, NULL, NULL }
};

int
HSH_GetHashId(const char *name)
{
  int id, nid;

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

  if (!hashes[id].name)
    return -1;

  if (hashes[id].context)
    return id;

  for (nid = 0; nettle_hashes[nid]; nid++) {
    if (!strcmp(hashes[id].int_name, nettle_hashes[nid]->name))
      break;
  }

  if (!nettle_hashes[nid] || !nettle_hashes[nid]->context_size || !nettle_hashes[nid]->init)
    return -1;

  hashes[id].nettle_hash = nettle_hashes[nid];
  hashes[id].context = Malloc(hashes[id].nettle_hash->context_size);

  return id;
}

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)
{
  const struct nettle_hash *hash;
  void *context;

  hash = hashes[id].nettle_hash;
  context = hashes[id].context;

  if (out_len > hash->digest_size)
    out_len = hash->digest_size;

  hash->init(context);
  hash->update(context, in1_len, in1);
  if (in2)
    hash->update(context, in2_len, in2);
  hash->digest(context, out_len, out);

  return out_len;
}

void
HSH_Finalise(void)
{
  int i;

  for (i = 0; hashes[i].name; i++) {
    if (hashes[i].context)
      Free(hashes[i].context);
  }
}