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
134
135
136
137
138
139
140
141
142
143
144
145
|
/*
* Copyright (C) 2009 Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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.
*/
#include "gcrypt_hasher.h"
#include <utils/debug.h>
#include <gcrypt.h>
typedef struct private_gcrypt_hasher_t private_gcrypt_hasher_t;
/**
* Private data of gcrypt_hasher_t
*/
struct private_gcrypt_hasher_t {
/**
* Public part of this class.
*/
gcrypt_hasher_t public;
/**
* gcrypt hasher context
*/
gcry_md_hd_t hd;
};
METHOD(hasher_t, get_hash_size, size_t,
private_gcrypt_hasher_t *this)
{
return gcry_md_get_algo_dlen(gcry_md_get_algo(this->hd));
}
METHOD(hasher_t, reset, bool,
private_gcrypt_hasher_t *this)
{
gcry_md_reset(this->hd);
return TRUE;
}
METHOD(hasher_t, get_hash, bool,
private_gcrypt_hasher_t *this, chunk_t chunk, u_int8_t *hash)
{
gcry_md_write(this->hd, chunk.ptr, chunk.len);
if (hash)
{
memcpy(hash, gcry_md_read(this->hd, 0), get_hash_size(this));
gcry_md_reset(this->hd);
}
return TRUE;
}
METHOD(hasher_t, allocate_hash, bool,
private_gcrypt_hasher_t *this, chunk_t chunk, chunk_t *hash)
{
if (hash)
{
*hash = chunk_alloc(get_hash_size(this));
return get_hash(this, chunk, hash->ptr);
}
return get_hash(this, chunk, NULL);
}
METHOD(hasher_t, destroy, void,
private_gcrypt_hasher_t *this)
{
gcry_md_close(this->hd);
free(this);
}
/*
* Described in header
*/
gcrypt_hasher_t *gcrypt_hasher_create(hash_algorithm_t algo)
{
private_gcrypt_hasher_t *this;
int gcrypt_alg;
gcry_error_t err;
switch (algo)
{
case HASH_MD2:
gcrypt_alg = GCRY_MD_MD2;
break;
case HASH_MD4:
gcrypt_alg = GCRY_MD_MD4;
break;
case HASH_MD5:
gcrypt_alg = GCRY_MD_MD5;
break;
case HASH_SHA1:
gcrypt_alg = GCRY_MD_SHA1;
break;
case HASH_SHA224:
gcrypt_alg = GCRY_MD_SHA224;
break;
case HASH_SHA256:
gcrypt_alg = GCRY_MD_SHA256;
break;
case HASH_SHA384:
gcrypt_alg = GCRY_MD_SHA384;
break;
case HASH_SHA512:
gcrypt_alg = GCRY_MD_SHA512;
break;
default:
return NULL;
}
INIT(this,
.public = {
.hasher = {
.get_hash = _get_hash,
.allocate_hash = _allocate_hash,
.get_hash_size = _get_hash_size,
.reset = _reset,
.destroy = _destroy,
},
},
);
err = gcry_md_open(&this->hd, gcrypt_alg, 0);
if (err)
{
DBG1(DBG_LIB, "grcy_md_open(%N) failed: %s",
hash_algorithm_names, algo, gpg_strerror(err));
free(this);
return NULL;
}
return &this->public;
}
|