71eaec7c5ffee9e01a26f849109dda2e3ec2d914
[tinc] / src / openssl / digest.c
1 /*
2     digest.c -- Digest handling
3     Copyright (C) 2007-2016 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 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.
18 */
19
20 #include "../system.h"
21
22 #include <openssl/err.h>
23 #include <openssl/hmac.h>
24
25 #include "digest.h"
26 #include "../digest.h"
27 #include "../logger.h"
28
29 static void digest_open(digest_t *digest, const EVP_MD *evp_md, size_t maclength) {
30         digest->digest = evp_md;
31
32         size_t digestlen = EVP_MD_size(digest->digest);
33
34         if(maclength == DIGEST_ALGO_SIZE || maclength > digestlen) {
35                 digest->maclength = digestlen;
36         } else {
37                 digest->maclength = maclength;
38         }
39 }
40
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);
43
44         if(!evp_md) {
45                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest name '%s'!", name);
46                 return false;
47         }
48
49         digest_open(digest, evp_md, maclength);
50         return true;
51 }
52
53 bool digest_open_by_nid(digest_t *digest, int nid, size_t maclength) {
54         const EVP_MD *evp_md = EVP_get_digestbynid(nid);
55
56         if(!evp_md) {
57                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest nid %d!", nid);
58                 return false;
59         }
60
61         digest_open(digest, evp_md, maclength);
62         return true;
63 }
64
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);
68
69         if(!digest->hmac_ctx) {
70                 abort();
71         }
72
73         return true;
74 }
75
76 void digest_close(digest_t *digest) {
77         if(!digest) {
78                 return;
79         }
80
81         if(digest->md_ctx) {
82                 EVP_MD_CTX_destroy(digest->md_ctx);
83         }
84
85         if(digest->hmac_ctx) {
86                 HMAC_CTX_free(digest->hmac_ctx);
87         }
88
89         memset(digest, 0, sizeof(*digest));
90 }
91
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];
95
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));
101                         return false;
102                 }
103         } else {
104                 if(!digest->md_ctx) {
105                         digest->md_ctx = EVP_MD_CTX_create();
106                 }
107
108                 if(!digest->md_ctx) {
109                         abort();
110                 }
111
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));
116                         return false;
117                 }
118         }
119
120         memcpy(outdata, tmpdata, digest->maclength);
121         return true;
122 }
123
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];
127
128         return digest_create(digest, indata, inlen, outdata) && !memcmp(cmpdata, outdata, digest->maclength);
129 }
130
131 int digest_get_nid(const digest_t *digest) {
132         if(!digest || !digest->digest) {
133                 return 0;
134         }
135
136         return EVP_MD_type(digest->digest);
137 }
138
139 size_t digest_keylength(const digest_t *digest) {
140         if(!digest || !digest->digest) {
141                 return 0;
142         }
143
144         return EVP_MD_size(digest->digest);
145 }
146
147 size_t digest_length(const digest_t *digest) {
148         if(!digest) {
149                 return 0;
150         }
151
152         return digest->maclength;
153 }
154
155 bool digest_active(const digest_t *digest) {
156         return digest && digest->digest && EVP_MD_type(digest->digest) != 0;
157 }