f20a97559e23763623353843c9f7d382f7709c98
[tinc] / src / openssl / digest.c
1 /*
2     digest.c -- Digest handling
3     Copyright (C) 2007 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 "utils.h"
22 #include "xalloc.h"
23
24 #include <openssl/err.h>
25 #include <openssl/hmac.h>
26
27 #include "digest.h"
28 #include "logger.h"
29
30 static void set_maclength(digest_t *digest, int maclength) {
31         int digestlen = EVP_MD_size(digest->digest);
32
33         if(maclength > digestlen || maclength < 0)
34                 digest->maclength = digestlen;
35         else
36                 digest->maclength = maclength;
37 }
38
39 bool digest_open_by_name(digest_t *digest, const char *name, int maclength) {
40         digest->digest = EVP_get_digestbyname(name);
41         digest->key = NULL;
42
43         if(!digest->digest) {
44                 logger(LOG_DEBUG, "Unknown digest name '%s'!", name);
45                 return false;
46         }
47
48         set_maclength(digest, maclength);
49         return true;
50 }
51
52 bool digest_open_by_nid(digest_t *digest, int nid, int maclength) {
53         digest->digest = EVP_get_digestbynid(nid);
54         digest->key = NULL;
55
56         if(!digest->digest) {
57                 logger(LOG_DEBUG, "Unknown digest nid %d!", nid);
58                 return false;
59         }
60
61         set_maclength(digest, maclength);
62         return true;
63 }
64
65 bool digest_open_sha1(digest_t *digest, int maclength) {
66         digest->digest = EVP_sha1();
67         digest->key = NULL;
68
69         set_maclength(digest, maclength);
70         return true;
71 }
72
73 bool digest_set_key(digest_t *digest, const void *key, size_t len) {
74         digest->key = xrealloc(digest->key, len);
75         memcpy(digest->key, key, len);
76         digest->keylength = len;
77 }
78
79 void digest_close(digest_t *digest) {
80         if(digest->key)
81                 free(digest->key);
82         digest->key = NULL;
83 }
84
85 bool digest_create(digest_t *digest, const void *indata, size_t inlen, void *outdata) {
86         size_t len = EVP_MD_size(digest->digest);
87         unsigned char tmpdata[len];
88
89         if(digest->key) {
90                 HMAC(digest->digest, digest->key, digest->keylength, indata, inlen, tmpdata, NULL);
91         } else {
92                 EVP_MD_CTX ctx;
93
94                 if(!EVP_DigestInit(&ctx, digest->digest)
95                                 || !EVP_DigestUpdate(&ctx, indata, inlen)
96                                 || !EVP_DigestFinal(&ctx, tmpdata, NULL)) {
97                         logger(LOG_DEBUG, "Error creating digest: %s", ERR_error_string(ERR_get_error(), NULL));
98                         return false;
99                 }
100         }
101
102         memcpy(outdata, tmpdata, digest->maclength);
103         return true;
104 }
105
106 bool digest_verify(digest_t *digest, const void *indata, size_t inlen, const void *cmpdata) {
107         size_t len = digest->maclength;
108         unsigned char outdata[len];
109
110         return digest_create(digest, indata, inlen, outdata) && !memcmp(cmpdata, outdata, digest->maclength);
111 }
112
113 int digest_get_nid(const digest_t *digest) {
114         return digest->digest ? digest->digest->type : 0;
115 }
116
117 size_t digest_length(const digest_t *digest) {
118         return digest->maclength;
119 }
120
121 bool digest_active(const digest_t *digest) {
122         return digest->digest && digest->digest->type != 0;
123 }