2 digest.c -- Digest handling
3 Copyright (C) 2007-2016 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 "../xalloc.h"
24 #include <openssl/err.h>
25 #include <openssl/hmac.h>
28 #include "../digest.h"
29 #include "../logger.h"
31 static digest_t *digest_open(const EVP_MD *evp_md, int maclength) {
32 digest_t *digest = xzalloc(sizeof(*digest));
33 digest->digest = evp_md;
35 int digestlen = EVP_MD_size(digest->digest);
37 if(maclength > digestlen || maclength < 0) {
38 digest->maclength = digestlen;
40 digest->maclength = maclength;
46 digest_t *digest_open_by_name(const char *name, int maclength) {
47 const EVP_MD *evp_md = EVP_get_digestbyname(name);
50 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest name '%s'!", name);
54 return digest_open(evp_md, maclength);
57 digest_t *digest_open_by_nid(int nid, int maclength) {
58 const EVP_MD *evp_md = EVP_get_digestbynid(nid);
61 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest nid %d!", nid);
65 return digest_open(evp_md, maclength);
68 bool digest_set_key(digest_t *digest, const void *key, size_t len) {
69 digest->key = xrealloc(digest->key, len);
70 memcpy(digest->key, key, len);
71 digest->keylength = len;
75 void digest_close(digest_t *digest) {
84 bool digest_create(digest_t *digest, const void *indata, size_t inlen, void *outdata) {
85 size_t len = EVP_MD_size(digest->digest);
86 unsigned char tmpdata[len];
89 if(!HMAC(digest->digest, digest->key, digest->keylength, indata, inlen, tmpdata, NULL)) {
90 logger(DEBUG_ALWAYS, LOG_DEBUG, "Error creating digest: %s", ERR_error_string(ERR_get_error(), NULL));
94 EVP_MD_CTX *ctx = EVP_MD_CTX_create();
100 if(!EVP_DigestInit(ctx, digest->digest)
101 || !EVP_DigestUpdate(ctx, indata, inlen)
102 || !EVP_DigestFinal(ctx, tmpdata, NULL)) {
103 logger(DEBUG_ALWAYS, LOG_DEBUG, "Error creating digest: %s", ERR_error_string(ERR_get_error(), NULL));
104 EVP_MD_CTX_destroy(ctx);
108 EVP_MD_CTX_destroy(ctx);
111 memcpy(outdata, tmpdata, digest->maclength);
115 bool digest_verify(digest_t *digest, const void *indata, size_t inlen, const void *cmpdata) {
116 size_t len = digest->maclength;
117 unsigned char outdata[len];
119 return digest_create(digest, indata, inlen, outdata) && !memcmp(cmpdata, outdata, digest->maclength);
122 int digest_get_nid(const digest_t *digest) {
123 if(!digest || !digest->digest) {
127 return EVP_MD_type(digest->digest);
130 size_t digest_keylength(const digest_t *digest) {
131 if(!digest || !digest->digest) {
135 return EVP_MD_size(digest->digest);
138 size_t digest_length(const digest_t *digest) {
143 return digest->maclength;
146 bool digest_active(const digest_t *digest) {
147 return digest && digest->digest && EVP_MD_type(digest->digest) != 0;