-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature-gnutls.c
154 lines (120 loc) · 3.64 KB
/
signature-gnutls.c
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
146
147
148
149
150
151
152
153
154
/* --*- c -*--
* Copyright (C) 2012 Enrico Scholz <[email protected]>
*
* 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; version 3 of the License.
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
#include "signature.h"
#include "util.h"
#if GNUTLS_VERSION_NUMBER >= 0x02090a
struct signature_gnutls_digest {
struct signature_algorithm alg;
gnutls_hash_hd_t dig;
gnutls_digest_algorithm_t digalg;
size_t digest_len;
unsigned char digest[];
};
#define togalg(_alg) \
container_of(_alg, struct signature_gnutls_digest, alg)
static bool signature_gnutls_reset(struct signature_algorithm *alg)
{
struct signature_gnutls_digest *galg = togalg(alg);
gnutls_hash_deinit(galg->dig, galg->digest);
if (gnutls_hash_init(&galg->dig, galg->digalg) < 0)
abort();
return true;
}
static bool signature_gnutls_update(struct signature_algorithm *alg,
void const *data, size_t len)
{
struct signature_gnutls_digest *galg = togalg(alg);
if (gnutls_hash(galg->dig, data, len) < 0)
return false;
return true;
}
static bool signature_gnutls_finish(struct signature_algorithm *alg,
void const **dst, size_t *dst_len)
{
struct signature_gnutls_digest *galg = togalg(alg);
gnutls_hash_output(galg->dig, galg->digest);
*dst = galg->digest;
*dst_len = galg->digest_len;
return true;
}
static void signature_gnutls_free(struct signature_algorithm *alg)
{
struct signature_gnutls_digest *galg = togalg(alg);
gnutls_hash_deinit(galg->dig, galg->digest);
free(galg);
}
struct signature_algorithm *signature_gnutls_create(gnutls_digest_algorithm_t digalg)
{
struct signature_gnutls_digest *galg;
size_t digest_len = gnutls_hash_get_len(digalg);
galg = calloc(1, sizeof *galg + digest_len);
if (!galg) {
perror("malloc(<gnutls-digest>)");
goto err;
}
if (gnutls_hash_init(&galg->dig, digalg) < 0) {
fprintf(stderr, "failed to create gnutls digest %d\n", digalg);
goto err;
}
galg->digest_len = digest_len;
galg->digalg = digalg;
galg->alg.strength = 10;
galg->alg.reset = signature_gnutls_reset;
galg->alg.update = signature_gnutls_update;
galg->alg.finish = signature_gnutls_finish;
galg->alg.free = signature_gnutls_free;
return &galg->alg;
err:
if (galg) {
if (digest_len > 0)
gnutls_hash_deinit(galg->dig, galg->digest);
free(galg);
}
return NULL;
}
struct signature_algorithm * signature_algorithm_md5_create(void)
{
return signature_gnutls_create(GNUTLS_DIG_MD5);
}
struct signature_algorithm * signature_algorithm_sha1_create(void)
{
return signature_gnutls_create(GNUTLS_DIG_SHA1);
}
struct signature_algorithm * signature_algorithm_sha256_create(void)
{
return signature_gnutls_create(GNUTLS_DIG_SHA256);
}
struct signature_algorithm * signature_algorithm_sha512_create(void)
{
return signature_gnutls_create(GNUTLS_DIG_SHA512);
}
#endif
static void __attribute__((__constructor__)) signature_algorithm_gnutls_init(void)
{
gnutls_global_init();
}
static void __attribute__((__destructor__)) signature_algorithm_gnutls_deinit(void)
{
gnutls_global_deinit();
}