4e180589058fc6401019e0bc3789e6af2ea6a8ea
[tinc] / src / gcrypt / digest.c
1 /*
2     digest.c -- Digest handling
3     Copyright (C) 2007 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         int 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, int *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, int *algo) {
51         int i;
52
53         for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) {
54                 if(nid == digesttable[i].nid) {
55                         *algo = digesttable[i].algo;
56                         return true;
57                 }
58         }
59
60         return false;
61 }
62
63 static bool digesttonid(int algo, int *nid) {
64         int i;
65
66         for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) {
67                 if(algo == digesttable[i].algo) {
68                         *nid = digesttable[i].nid;
69                         return true;
70                 }
71         }
72
73         return false;
74 }
75
76 static bool digest_open(digest_t *digest, int algo) {
77         if(!digesttonid(algo, &digest->nid)) {
78                 logger(LOG_DEBUG, "Digest %d has no corresponding nid!", algo);
79                 return false;
80         }
81
82         digest->len = gcry_md_get_algo_dlen(algo);
83
84         return true;
85 }
86
87 bool digest_open_by_name(digest_t *digest, const char *name) {
88         int algo;
89
90         if(!nametodigest(name, &algo)) {
91                 logger(LOG_DEBUG, "Unknown digest name '%s'!", name);
92                 return false;
93         }
94
95         return digest_open(digest, algo);
96 }
97
98 bool digest_open_by_nid(digest_t *digest, int nid) {
99         int algo;
100
101         if(!nidtodigest(nid, &algo)) {
102                 logger(LOG_DEBUG, "Unknown digest ID %d!", nid);
103                 return false;
104         }
105
106         return digest_open(digest, algo);
107 }
108
109 bool digest_open_sha1(digest_t *digest) {
110         return digest_open(digest, GCRY_MD_SHA1);
111 }
112
113 void digest_close(digest_t *digest) {
114 }
115
116 bool digest_create(digest_t *digest, const void *indata, size_t inlen, void *outdata) {
117         gcry_md_hash_buffer(digest->algo, outdata, indata, inlen);
118         return true;
119 }
120
121 bool digest_verify(digest_t *digest, const void *indata, size_t inlen, const void *cmpdata) {
122         char outdata[digest->len];
123
124         gcry_md_hash_buffer(digest->algo, outdata, indata, inlen);
125         return !memcmp(cmpdata, outdata, digest->len);
126 }
127
128 int digest_get_nid(const digest_t *digest) {
129         return digest->nid;
130 }
131
132 size_t digest_length(const digest_t *digest) {
133         return digest->len;
134 }
135
136 bool digest_active(const digest_t *digest) {
137         return digest->algo != GCRY_MD_NONE;
138 }