Remove unused '#include's.
[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 #include "../xalloc.h"
22
23 #include <openssl/err.h>
24 #include <openssl/hmac.h>
25
26 #include "digest.h"
27 #include "../digest.h"
28 #include "../logger.h"
29
30 static digest_t *digest_open(const EVP_MD *evp_md, int maclength) {
31         digest_t *digest = xzalloc(sizeof(*digest));
32         digest->digest = evp_md;
33
34         int digestlen = EVP_MD_size(digest->digest);
35
36         if(maclength > digestlen || maclength < 0) {
37                 digest->maclength = digestlen;
38         } else {
39                 digest->maclength = maclength;
40         }
41
42         return digest;
43 }
44
45 digest_t *digest_open_by_name(const char *name, int maclength) {
46         const EVP_MD *evp_md = EVP_get_digestbyname(name);
47
48         if(!evp_md) {
49                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest name '%s'!", name);
50                 return false;
51         }
52
53         return digest_open(evp_md, maclength);
54 }
55
56 digest_t *digest_open_by_nid(int nid, int maclength) {
57         const EVP_MD *evp_md = EVP_get_digestbynid(nid);
58
59         if(!evp_md) {
60                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Unknown digest nid %d!", nid);
61                 return false;
62         }
63
64         return digest_open(evp_md, maclength);
65 }
66
67 bool digest_set_key(digest_t *digest, const void *key, size_t len) {
68         digest->hmac_ctx = HMAC_CTX_new();
69         HMAC_Init_ex(digest->hmac_ctx, key, len, digest->digest, NULL);
70
71         if(!digest->hmac_ctx) {
72                 abort();
73         }
74
75         return true;
76 }
77
78 void digest_close(digest_t *digest) {
79         if(!digest) {
80                 return;
81         }
82
83         if(digest->md_ctx) {
84                 EVP_MD_CTX_destroy(digest->md_ctx);
85         }
86
87         if(digest->hmac_ctx) {
88                 HMAC_CTX_free(digest->hmac_ctx);
89         }
90
91         free(digest);
92 }
93
94 bool digest_create(digest_t *digest, const void *indata, size_t inlen, void *outdata) {
95         size_t len = EVP_MD_size(digest->digest);
96         unsigned char tmpdata[len];
97
98         if(digest->hmac_ctx) {
99                 if(!HMAC_Init_ex(digest->hmac_ctx, NULL, 0, NULL, NULL)
100                                 || !HMAC_Update(digest->hmac_ctx, indata, inlen)
101                                 || !HMAC_Final(digest->hmac_ctx, tmpdata, NULL)) {
102                         logger(DEBUG_ALWAYS, LOG_DEBUG, "Error creating digest: %s", ERR_error_string(ERR_get_error(), NULL));
103                         return false;
104                 }
105         } else {
106                 if(!digest->md_ctx) {
107                         digest->md_ctx = EVP_MD_CTX_create();
108                 }
109
110                 if(!digest->md_ctx) {
111                         abort();
112                 }
113
114                 if(!EVP_DigestInit(digest->md_ctx, digest->digest)
115                                 || !EVP_DigestUpdate(digest->md_ctx, indata, inlen)
116                                 || !EVP_DigestFinal(digest->md_ctx, tmpdata, NULL)) {
117                         logger(DEBUG_ALWAYS, LOG_DEBUG, "Error creating digest: %s", ERR_error_string(ERR_get_error(), NULL));
118                         return false;
119                 }
120         }
121
122         memcpy(outdata, tmpdata, digest->maclength);
123         return true;
124 }
125
126 bool digest_verify(digest_t *digest, const void *indata, size_t inlen, const void *cmpdata) {
127         size_t len = digest->maclength;
128         unsigned char outdata[len];
129
130         return digest_create(digest, indata, inlen, outdata) && !memcmp(cmpdata, outdata, digest->maclength);
131 }
132
133 int digest_get_nid(const digest_t *digest) {
134         if(!digest || !digest->digest) {
135                 return 0;
136         }
137
138         return EVP_MD_type(digest->digest);
139 }
140
141 size_t digest_keylength(const digest_t *digest) {
142         if(!digest || !digest->digest) {
143                 return 0;
144         }
145
146         return EVP_MD_size(digest->digest);
147 }
148
149 size_t digest_length(const digest_t *digest) {
150         if(!digest) {
151                 return 0;
152         }
153
154         return digest->maclength;
155 }
156
157 bool digest_active(const digest_t *digest) {
158         return digest && digest->digest && EVP_MD_type(digest->digest) != 0;
159 }