2 digest.c -- Digest handling
3 Copyright (C) 2007 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.
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) {
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;
50 static bool nidtodigest(int nid, int *algo) {
53 for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) {
54 if(nid == digesttable[i].nid) {
55 *algo = digesttable[i].algo;
63 static bool digesttonid(int algo, int *nid) {
66 for(i = 0; i < sizeof digesttable / sizeof *digesttable; i++) {
67 if(algo == digesttable[i].algo) {
68 *nid = digesttable[i].nid;
76 static bool digest_open(digest_t *digest, int algo, int maclength) {
77 if(!digesttonid(algo, &digest->nid)) {
78 logger(LOG_DEBUG, "Digest %d has no corresponding nid!", algo);
82 unsigned int len = gcry_md_get_algo_dlen(algo);
84 if(maclength > len || maclength < 0)
85 digest->maclength = len;
87 digest->maclength = maclength;
94 bool digest_open_by_name(digest_t *digest, const char *name, int maclength) {
97 if(!nametodigest(name, &algo)) {
98 logger(LOG_DEBUG, "Unknown digest name '%s'!", name);
102 return digest_open(digest, algo, maclength);
105 bool digest_open_by_nid(digest_t *digest, int nid, int maclength) {
108 if(!nidtodigest(nid, &algo)) {
109 logger(LOG_DEBUG, "Unknown digest ID %d!", nid);
113 return digest_open(digest, algo, maclength);
116 bool digest_open_sha1(digest_t *digest, int maclength) {
117 return digest_open(digest, GCRY_MD_SHA1, maclength);
120 void digest_close(digest_t *digest) {
123 bool digest_create(digest_t *digest, const void *indata, size_t inlen, void *outdata) {
124 unsigned int len = gcry_md_get_algo_dlen(digest->algo);
127 gcry_md_hash_buffer(digest->algo, tmpdata, indata, inlen);
128 memcpy(outdata, tmpdata, digest->maclength);
133 bool digest_verify(digest_t *digest, const void *indata, size_t inlen, const void *cmpdata) {
134 unsigned int len = gcry_md_get_algo_dlen(digest->algo);
137 gcry_md_hash_buffer(digest->algo, outdata, indata, inlen);
138 return !memcmp(cmpdata, outdata, digest->maclength);
141 int digest_get_nid(const digest_t *digest) {
145 size_t digest_length(const digest_t *digest) {
146 return digest->maclength;
149 bool digest_active(const digest_t *digest) {
150 return digest->algo != GCRY_MD_NONE;