tinc: Add clean sources of 1.1pre10.
[tomato.git] / release / src / router / tinc / src / gcrypt / digest.c
blob4229b0c881865bda593b25463b5f7f862f64640b
1 /*
2 digest.c -- Digest handling
3 Copyright (C) 2007-2012 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
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.
20 #include "system.h"
22 #include "digest.h"
23 #include "logger.h"
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},
37 static bool nametodigest(const char *name, int *algo) {
38 int i;
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;
47 return false;
50 static bool nidtodigest(int nid, int *algo) {
51 int i;
53 for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) {
54 if(nid == digesttable[i].nid) {
55 *algo = digesttable[i].algo;
56 return true;
60 return false;
63 static bool digesttonid(int algo, int *nid) {
64 int i;
66 for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) {
67 if(algo == digesttable[i].algo) {
68 *nid = digesttable[i].nid;
69 return true;
73 return false;
76 static bool digest_open(digest_t *digest, int algo, int maclength) {
77 if(!digesttonid(algo, &digest->nid)) {
78 logger(DEBUG_ALWAYS, LOG_DEBUG, "Digest %d has no corresponding nid!", algo);
79 return false;
82 unsigned int len = gcry_md_get_algo_dlen(algo);
84 if(maclength > len || maclength < 0)
85 digest->maclength = len;
86 else
87 digest->maclength = maclength;
89 digest->algo = algo;
90 digest->hmac = NULL;
92 return true;
95 bool digest_open_by_name(digest_t *digest, const char *name, int maclength) {
96 int algo;
98 if(!nametodigest(name, &algo)) {
99 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest name '%s'!", name);
100 return false;
103 return digest_open(digest, algo, maclength);
106 bool digest_open_by_nid(digest_t *digest, int nid, int maclength) {
107 int algo;
109 if(!nidtodigest(nid, &algo)) {
110 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest ID %d!", nid);
111 return false;
114 return digest_open(digest, algo, maclength);
117 bool digest_open_sha1(digest_t *digest, int maclength) {
118 return digest_open(digest, GCRY_MD_SHA1, maclength);
121 void digest_close(digest_t *digest) {
122 if(digest->hmac)
123 gcry_md_close(digest->hmac);
124 digest->hmac = NULL;
127 bool digest_set_key(digest_t *digest, const void *key, size_t len) {
128 if(!digest->hmac)
129 gcry_md_open(&digest->hmac, digest->algo, GCRY_MD_FLAG_HMAC);
130 if(!digest->hmac)
131 return false;
133 return !gcry_md_setkey(digest->hmac, key, len);
136 bool digest_create(digest_t *digest, const void *indata, size_t inlen, void *outdata) {
137 unsigned int len = gcry_md_get_algo_dlen(digest->algo);
139 if(digest->hmac) {
140 char *tmpdata;
141 gcry_md_reset(digest->hmac);
142 gcry_md_write(digest->hmac, indata, inlen);
143 tmpdata = gcry_md_read(digest->hmac, digest->algo);
144 if(!tmpdata)
145 return false;
146 memcpy(outdata, tmpdata, digest->maclength);
147 } else {
148 char tmpdata[len];
149 gcry_md_hash_buffer(digest->algo, tmpdata, indata, inlen);
150 memcpy(outdata, tmpdata, digest->maclength);
153 return true;
156 bool digest_verify(digest_t *digest, const void *indata, size_t inlen, const void *cmpdata) {
157 unsigned int len = digest->maclength;
158 char outdata[len];
160 return digest_create(digest, indata, inlen, outdata) && !memcmp(cmpdata, outdata, len);
163 int digest_get_nid(const digest_t *digest) {
164 return digest->nid;
167 size_t digest_length(const digest_t *digest) {
168 return digest->maclength;
171 bool digest_active(const digest_t *digest) {
172 return digest->algo != GCRY_MD_NONE;