Create wrappers for the cryptographic operations used in tinc.
[tinc] / src / 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
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19     $Id$
20 */
21
22 #include "system.h"
23
24 #include "digest.h"
25
26 static struct {
27         const char *name,
28         enum gcry_md_algos algo,
29         int 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 nidtodigest(int nid, enum gcry_md_algos *algo) {
39         int i;
40
41         for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) {
42                 if(nid == digesttable[i].nid) {
43                         *algo = digesttable[i].algo;
44                         return true;
45                 }
46         }
47
48         return false;
49 }
50
51 static bool digesttonid(enum gcry_md_algos algo, int *nid) {
52         int i;
53
54         for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) {
55                 if(algo == digesttable[i].algo) {
56                         *nid = digesttable[i].nid;
57                         return true;
58                 }
59         }
60
61         return false;
62 }
63
64 static bool digest_open(digest_t *digest, int algo) {
65         if(!digesttonid(algo, &digest->nid)) {
66                 logger(LOG_DEBUG< _("Digest %d has no corresponding nid!"), algo);
67                 return false;
68         }
69
70         digest->len = gcry_md_get_algo_dlen(algo);
71
72         return true;
73 }
74
75 bool digest_open_by_name(digest_t *digest, const char *name) {
76         int algo;
77
78         if(!nametodigest(name, &algo)) {
79                 logger(LOG_DEBUG, _("Unknown digest name '%s'!"), name);
80                 return false;
81         }
82
83         return digest_open(digest, algo);
84 }
85
86 bool digest_open_by_nid(digest_t *digest, int nid) {
87         int algo;
88
89         if(!nidtodigest(nid, &algo)) {
90                 logger(LOG_DEBUG, _("Unknown digest ID %d!"), nid);
91                 return false;
92         }
93
94         return digest_open(digest, algo, mode);
95 }
96
97 bool digest_open_sha1(digest_t *digest) {
98         return digest_open(digest, GCRY_MD_SHA1);
99 }
100
101 void digest_close(digest_t *digest) {
102 }
103
104 bool digest_create(digest_t *digest, void *indata, size_t inlen, void *outdata) {
105         gcry_error_t err;
106
107         if((err = gcry_md_hash_buffer(digest->algo, outdata, indata, inlen))) {
108                 logger(LOG_ERR, _("Error while creating digest!"));
109                 return false;
110         }
111
112         *outlen = digest->len;
113         return true;
114 }
115
116 bool digest_verify(digest_t *digest, void *indata, size_t inlen, void *cmpdata) {
117         gcry_error_t err;
118         char outdata[digest->len];
119
120         if((err = gcry_md_hash_buffer(digest->algo, outdata, indata, inlen))) {
121                 logger(LOG_ERR, _("Error while creating digest!"));
122                 return false;
123         }
124
125         return !memcmp(indata, outdata, digest->len);
126 }
127
128 int digest_get_nid(digest_t *digest) {
129         return digest->nid;
130 }
131