c8d4b3149b36f54e7cf31b4e4ec30719dd310099
[tinc] / src / gcrypt / digest.c
1 /*
2     digest.c -- Digest handling
3     Copyright (C) 2007-2012 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 "logger.h"
24
25 static struct {
26         const char *name;
27         enum gcry_md_algos algo;
28         int nid;
29 } digesttable[] = {
30         {"none", GCRY_MD_NONE, 0},
31         {"sha1", GCRY_MD_SHA1, 64},
32         {"sha256", GCRY_MD_SHA256, 672},
33         {"sha384", GCRY_MD_SHA384, 673},
34         {"sha512", GCRY_MD_SHA512, 674},
35 };
36
37 static bool nametodigest(const char *name, enum gcry_md_algos *algo) {
38         int i;
39
40         for(i = 0; i < sizeof(digesttable) / sizeof(*digesttable); i++) {
41                 if(digesttable[i].name && !strcasecmp(name, digesttable[i].name)) {
42                         *algo = digesttable[i].algo;
43                         return true;
44                 }
45         }
46
47         return false;
48 }
49
50 static bool nidtodigest(int nid, enum gcry_md_algos *algo) {
51         for(int i = 0; i < sizeof(digesttable) / sizeof(*digesttable); i++) {
52                 if(nid == digesttable[i].nid) {
53                         *algo = digesttable[i].algo;
54                         return true;
55                 }
56         }
57
58         return false;
59 }
60
61 static bool digesttonid(enum gcry_md_algos algo, int *nid) {
62         for(int i = 0; i < sizeof(digesttable) / sizeof(*digesttable); i++) {
63                 if(algo == digesttable[i].algo) {
64                         *nid = digesttable[i].nid;
65                         return true;
66                 }
67         }
68
69         return false;
70 }
71
72 static bool digest_open(digest_t *digest, enum gcry_md_algos algo, size_t maclength) {
73         if(!digesttonid(algo, &digest->nid)) {
74                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Digest %d has no corresponding nid!", algo);
75                 return false;
76         }
77
78         unsigned int len = gcry_md_get_algo_dlen(algo);
79
80         if(maclength > len || maclength < 0) {
81                 digest->maclength = len;
82         } else {
83                 digest->maclength = maclength;
84         }
85
86         digest->algo = algo;
87         digest->hmac = NULL;
88
89         return true;
90 }
91
92 bool digest_open_by_name(digest_t *digest, const char *name, size_t maclength) {
93         enum gcry_md_algos algo;
94
95         if(!nametodigest(name, &algo)) {
96                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest name '%s'!", name);
97                 return false;
98         }
99
100         return digest_open(digest, algo, maclength);
101 }
102
103 bool digest_open_by_nid(digest_t *digest, int nid, size_t maclength) {
104         enum gcry_md_algos algo;
105
106         if(!nidtodigest(nid, &algo)) {
107                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest ID %d!", nid);
108                 return false;
109         }
110
111         return digest_open(digest, algo, maclength);
112 }
113
114 bool digest_open_sha1(digest_t *digest, size_t maclength) {
115         return digest_open(digest, GCRY_MD_SHA1, maclength);
116 }
117
118 void digest_close(digest_t *digest) {
119         if(digest->hmac) {
120                 gcry_md_close(digest->hmac);
121         }
122
123         memset(digest, 0, sizeof(*digest));
124 }
125
126 bool digest_set_key(digest_t *digest, const void *key, size_t len) {
127         if(!digest->hmac) {
128                 gcry_md_open(&digest->hmac, digest->algo, GCRY_MD_FLAG_HMAC);
129         }
130
131         if(!digest->hmac) {
132                 return false;
133         }
134
135         return !gcry_md_setkey(digest->hmac, key, len);
136 }
137
138 bool digest_create(digest_t *digest, const void *indata, size_t inlen, void *outdata) {
139         unsigned int len = gcry_md_get_algo_dlen(digest->algo);
140
141         if(digest->hmac) {
142                 uint8_t *tmpdata;
143                 gcry_md_reset(digest->hmac);
144                 gcry_md_write(digest->hmac, indata, inlen);
145                 tmpdata = gcry_md_read(digest->hmac, digest->algo);
146
147                 if(!tmpdata) {
148                         return false;
149                 }
150
151                 memcpy(outdata, tmpdata, digest->maclength);
152         } else {
153                 char tmpdata[len];
154                 gcry_md_hash_buffer(digest->algo, tmpdata, indata, inlen);
155                 memcpy(outdata, tmpdata, digest->maclength);
156         }
157
158         return true;
159 }
160
161 bool digest_verify(digest_t *digest, const void *indata, size_t inlen, const void *cmpdata) {
162         size_t len = digest->maclength;
163         uint8_t outdata[len];
164
165         return digest_create(digest, indata, inlen, outdata) && !memcmp(cmpdata, outdata, len);
166 }
167
168 int digest_get_nid(const digest_t *digest) {
169         if(!digest || !digest->nid) {
170                 return 0;
171         }
172
173         return digest->nid;
174 }
175
176 size_t digest_length(const digest_t *digest) {
177         if(!digest) {
178                 return 0;
179         }
180
181         return digest->maclength;
182 }
183
184 bool digest_active(const digest_t *digest) {
185         return digest && digest->algo != GCRY_MD_NONE;
186 }