Wipe (some) secrets from memory after use
[tinc] / src / gcrypt / digest.c
1 /*
2     digest.c -- Digest handling
3     Copyright (C) 2007-2022 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "../system.h"
21
22 #include "digest.h"
23 #include "../digest.h"
24 #include "../logger.h"
25
26 static struct {
27         const char *name;
28         md_algo_t algo;
29         nid_t nid;
30 } digesttable[] = {
31         {"none",   GCRY_MD_NONE,   0},
32         {"sha1",   GCRY_MD_SHA1,   64},
33         {"sha256", GCRY_MD_SHA256, 672},
34         {"sha384", GCRY_MD_SHA384, 673},
35         {"sha512", GCRY_MD_SHA512, 674},
36 };
37
38 static bool nametodigest(md_algo_t *algo, const char *name) {
39         for(size_t i = 0; i < sizeof(digesttable) / sizeof(*digesttable); i++) {
40                 if(digesttable[i].name && !strcasecmp(name, digesttable[i].name)) {
41                         *algo = digesttable[i].algo;
42                         return true;
43                 }
44         }
45
46         return false;
47 }
48
49 static bool nidtodigest(md_algo_t *algo, nid_t nid) {
50         for(size_t i = 0; i < sizeof(digesttable) / sizeof(*digesttable); i++) {
51                 if(nid == digesttable[i].nid) {
52                         *algo = digesttable[i].algo;
53                         return true;
54                 }
55         }
56
57         return false;
58 }
59
60 static bool digesttonid(nid_t *nid, md_algo_t algo) {
61         for(size_t i = 0; i < sizeof(digesttable) / sizeof(*digesttable); i++) {
62                 if(algo == digesttable[i].algo) {
63                         *nid = digesttable[i].nid;
64                         return true;
65                 }
66         }
67
68         return false;
69 }
70
71 static bool digest_open(digest_t *digest, md_algo_t algo, size_t maclength) {
72         if(!digesttonid(&digest->nid, algo)) {
73                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Digest %d has no corresponding nid!", algo);
74                 return false;
75         }
76
77         unsigned int len = gcry_md_get_algo_dlen(algo);
78
79         if(maclength > len) {
80                 digest->maclength = len;
81         } else {
82                 digest->maclength = maclength;
83         }
84
85         digest->algo = algo;
86         digest->hmac = NULL;
87
88         return true;
89 }
90
91 bool digest_open_by_name(digest_t *digest, const char *name, size_t maclength) {
92         md_algo_t algo;
93
94         if(!nametodigest(&algo, name)) {
95                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest name '%s'!", name);
96                 return false;
97         }
98
99         return digest_open(digest, algo, maclength);
100 }
101
102 bool digest_open_by_nid(digest_t *digest, nid_t nid, size_t maclength) {
103         md_algo_t algo;
104
105         if(!nidtodigest(&algo, nid)) {
106                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest ID %d!", nid);
107                 return false;
108         }
109
110         return digest_open(digest, algo, maclength);
111 }
112
113 void digest_close(digest_t *digest) {
114         if(!digest) {
115                 return;
116         }
117
118         if(digest->hmac) {
119                 gcry_md_close(digest->hmac);
120         }
121
122         memset(digest, 0, sizeof(*digest));
123 }
124
125 bool digest_set_key(digest_t *digest, const void *key, size_t len) {
126         if(!digest->hmac) {
127                 gcry_md_open(&digest->hmac, digest->algo, GCRY_MD_FLAG_HMAC);
128         }
129
130         if(!digest->hmac) {
131                 return false;
132         }
133
134         return !gcry_md_setkey(digest->hmac, key, len);
135 }
136
137 bool digest_create(digest_t *digest, const void *indata, size_t inlen, void *outdata) {
138         unsigned int len = gcry_md_get_algo_dlen(digest->algo);
139
140         if(digest->hmac) {
141                 uint8_t *tmpdata;
142                 gcry_md_reset(digest->hmac);
143                 gcry_md_write(digest->hmac, indata, inlen);
144                 tmpdata = gcry_md_read(digest->hmac, digest->algo);
145
146                 if(!tmpdata) {
147                         return false;
148                 }
149
150                 memcpy(outdata, tmpdata, digest->maclength);
151         } else {
152                 char tmpdata[len];
153                 gcry_md_hash_buffer(digest->algo, tmpdata, indata, inlen);
154                 memcpy(outdata, tmpdata, digest->maclength);
155         }
156
157         return true;
158 }
159
160 bool digest_verify(digest_t *digest, const void *indata, size_t inlen, const void *cmpdata) {
161         size_t len = digest->maclength;
162         uint8_t outdata[len];
163
164         return digest_create(digest, indata, inlen, outdata) && !memcmp(cmpdata, outdata, len);
165 }
166
167 nid_t digest_get_nid(const digest_t *digest) {
168         if(!digest || !digest->nid) {
169                 return 0;
170         }
171
172         return digest->nid;
173 }
174
175 size_t digest_length(const digest_t *digest) {
176         if(!digest) {
177                 return 0;
178         }
179
180         return digest->maclength;
181 }
182
183 bool digest_active(const digest_t *digest) {
184         return digest && digest->algo != GCRY_MD_NONE;
185 }