Don't assume sa.sa_family is a short int.
[tinc] / src / openssl / digest.c
1 /*
2     digest.c -- Digest handling
3     Copyright (C) 2007-2013 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 "../digest.h"
29 #include "../logger.h"
30
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;
34
35         int digestlen = EVP_MD_size(digest->digest);
36
37         if(maclength > digestlen || maclength < 0)
38                 digest->maclength = digestlen;
39         else
40                 digest->maclength = maclength;
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 digest_t *digest_open_sha1(int maclength) {
68         return digest_open(EVP_sha1(), maclength);
69 }
70
71 bool digest_set_key(digest_t *digest, const void *key, size_t len) {
72         digest->key = xrealloc(digest->key, len);
73         memcpy(digest->key, key, len);
74         digest->keylength = len;
75         return true;
76 }
77
78 void digest_close(digest_t *digest) {
79         if(!digest)
80                 return;
81
82         free(digest->key);
83         free(digest);
84 }
85
86 bool digest_create(digest_t *digest, const void *indata, size_t inlen, void *outdata) {
87         size_t len = EVP_MD_size(digest->digest);
88         unsigned char tmpdata[len];
89
90         if(digest->key) {
91                 if(!HMAC(digest->digest, digest->key, digest->keylength, indata, inlen, tmpdata, NULL)) {
92                         logger(DEBUG_ALWAYS, LOG_DEBUG, "Error creating digest: %s", ERR_error_string(ERR_get_error(), NULL));
93                         return false;
94                 }
95         } else {
96                 EVP_MD_CTX ctx;
97
98                 if(!EVP_DigestInit(&ctx, digest->digest)
99                                 || !EVP_DigestUpdate(&ctx, indata, inlen)
100                                 || !EVP_DigestFinal(&ctx, tmpdata, NULL)) {
101                         logger(DEBUG_ALWAYS, LOG_DEBUG, "Error creating digest: %s", ERR_error_string(ERR_get_error(), NULL));
102                         return false;
103                 }
104         }
105
106         memcpy(outdata, tmpdata, digest->maclength);
107         return true;
108 }
109
110 bool digest_verify(digest_t *digest, const void *indata, size_t inlen, const void *cmpdata) {
111         size_t len = digest->maclength;
112         unsigned char outdata[len];
113
114         return digest_create(digest, indata, inlen, outdata) && !memcmp(cmpdata, outdata, digest->maclength);
115 }
116
117 int digest_get_nid(const digest_t *digest) {
118         if(!digest || !digest->digest)
119                 return 0;
120
121         return digest->digest->type;
122 }
123
124 size_t digest_keylength(const digest_t *digest) {
125         if(!digest || !digest->digest)
126                 return 0;
127
128         return digest->digest->md_size;
129 }
130
131 size_t digest_length(const digest_t *digest) {
132         if(!digest)
133                 return 0;
134
135         return digest->maclength;
136 }
137
138 bool digest_active(const digest_t *digest) {
139         return digest && digest->digest && digest->digest->type != 0;
140 }