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 <openssl/err.h>
23 #include <openssl/hmac.h>
26 #include "../digest.h"
27 #include "../logger.h"
29 static void digest_open(digest_t *digest, const EVP_MD *evp_md, size_t maclength) {
30 digest->digest = evp_md;
32 size_t digestlen = EVP_MD_size(digest->digest);
34 if(maclength == DIGEST_ALGO_SIZE || maclength > digestlen) {
35 digest->maclength = digestlen;
37 digest->maclength = maclength;
41 bool digest_open_by_name(digest_t *digest, const char *name, size_t maclength) {
42 const EVP_MD *evp_md = EVP_get_digestbyname(name);
45 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest name '%s'!", name);
49 digest_open(digest, evp_md, maclength);
53 bool digest_open_by_nid(digest_t *digest, int nid, size_t maclength) {
54 const EVP_MD *evp_md = EVP_get_digestbynid(nid);
57 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest nid %d!", nid);
61 digest_open(digest, evp_md, maclength);
65 bool digest_set_key(digest_t *digest, const void *key, size_t len) {
66 digest->hmac_ctx = HMAC_CTX_new();
67 HMAC_Init_ex(digest->hmac_ctx, key, (int)len, digest->digest, NULL);
69 if(!digest->hmac_ctx) {
76 void digest_close(digest_t *digest) {
82 EVP_MD_CTX_destroy(digest->md_ctx);
85 if(digest->hmac_ctx) {
86 HMAC_CTX_free(digest->hmac_ctx);
89 memset(digest, 0, sizeof(*digest));
92 bool digest_create(digest_t *digest, const void *indata, size_t inlen, void *outdata) {
93 size_t len = EVP_MD_size(digest->digest);
94 unsigned char tmpdata[len];
96 if(digest->hmac_ctx) {
97 if(!HMAC_Init_ex(digest->hmac_ctx, NULL, 0, NULL, NULL)
98 || !HMAC_Update(digest->hmac_ctx, indata, inlen)
99 || !HMAC_Final(digest->hmac_ctx, tmpdata, NULL)) {
100 logger(DEBUG_ALWAYS, LOG_DEBUG, "Error creating digest: %s", ERR_error_string(ERR_get_error(), NULL));
104 if(!digest->md_ctx) {
105 digest->md_ctx = EVP_MD_CTX_create();
108 if(!digest->md_ctx) {
112 if(!EVP_DigestInit(digest->md_ctx, digest->digest)
113 || !EVP_DigestUpdate(digest->md_ctx, indata, inlen)
114 || !EVP_DigestFinal(digest->md_ctx, tmpdata, NULL)) {
115 logger(DEBUG_ALWAYS, LOG_DEBUG, "Error creating digest: %s", ERR_error_string(ERR_get_error(), NULL));
120 memcpy(outdata, tmpdata, digest->maclength);
124 bool digest_verify(digest_t *digest, const void *indata, size_t inlen, const void *cmpdata) {
125 size_t len = digest->maclength;
126 unsigned char outdata[len];
128 return digest_create(digest, indata, inlen, outdata) && !memcmp(cmpdata, outdata, digest->maclength);
131 int digest_get_nid(const digest_t *digest) {
132 if(!digest || !digest->digest) {
136 return EVP_MD_type(digest->digest);
139 size_t digest_keylength(const digest_t *digest) {
140 if(!digest || !digest->digest) {
144 return EVP_MD_size(digest->digest);
147 size_t digest_length(const digest_t *digest) {
152 return digest->maclength;
155 bool digest_active(const digest_t *digest) {
156 return digest && digest->digest && EVP_MD_type(digest->digest) != 0;