Use Ed25519 keys.
[oweals/tinc.git] / src / utils.c
1 /*
2     utils.c -- gathering of some stupid small functions
3     Copyright (C) 1999-2005 Ivo Timmermans
4                   2000-2013 Guus Sliepen <guus@tinc-vpn.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "../src/logger.h"
24 #include "utils.h"
25
26 static const char hexadecimals[] = "0123456789ABCDEF";
27 static const char base64_original[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
28 static const char base64_urlsafe[] =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
29 static const char base64_decode[256] = {
30         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
31         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
32         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63,
33         52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
34         -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
35         15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63,
36         -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
37         41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
38         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
39         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
40         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
42         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 };
47
48 static int charhex2bin(char c) {
49         if(isdigit(c))
50                 return c - '0';
51         else
52                 return toupper(c) - 'A' + 10;
53 }
54
55 int hex2bin(const char *src, char *dst, int length) {
56         int i;
57         for(i = 0; i < length && isxdigit(src[i * 2]) && isxdigit(src[i * 2 + 1]); i++)
58                 dst[i] = charhex2bin(src[i * 2]) * 16 + charhex2bin(src[i * 2 + 1]);
59         return i;
60 }
61
62 int bin2hex(const char *src, char *dst, int length) {
63         for(int i = length - 1; i >= 0; i--) {
64                 dst[i * 2 + 1] = hexadecimals[(unsigned char) src[i] & 15];
65                 dst[i * 2] = hexadecimals[(unsigned char) src[i] >> 4];
66         }
67         dst[length * 2] = 0;
68         return length * 2;
69 }
70
71 int b64decode(const char *src, char *dst, int length) {
72         int i;
73         uint32_t triplet = 0;
74         unsigned char *udst = (unsigned char *)dst;
75
76         for(i = 0; i < length / 3 * 4 && src[i]; i++) {
77                 triplet |= base64_decode[src[i] & 0xff] << (6 * (i & 3));
78                 if((i & 3) == 3) {
79                         if(triplet & 0xff000000U)
80                                 return 0;
81                         udst[0] = triplet & 0xff; triplet >>= 8;
82                         udst[1] = triplet & 0xff; triplet >>= 8;
83                         udst[2] = triplet;
84                         triplet = 0;
85                         udst += 3;
86                 }
87         }
88         if(triplet & 0xff000000U)
89                 return 0;
90         if((i & 3) == 3) {
91                 udst[0] = triplet & 0xff; triplet >>= 8;
92                 udst[1] = triplet & 0xff;
93                 return i / 4 * 3 + 2;
94         } else if((i & 3) == 2) {
95                 udst[0] = triplet & 0xff;
96                 return i / 4 * 3 + 1;
97         } else {
98                 return i / 4 * 3;
99         }
100 }
101
102 static int b64encode_internal(const char *src, char *dst, int length, const char *alphabet) {
103         uint32_t triplet;
104         const unsigned char *usrc = (unsigned char *)src;
105         int si = length / 3 * 3;
106         int di = length / 3 * 4;
107
108         switch(length % 3) {
109                 case 2:
110                         triplet = usrc[si] | usrc[si + 1] << 8;
111                         dst[di] = alphabet[triplet & 63]; triplet >>= 6;
112                         dst[di + 1] = alphabet[triplet & 63]; triplet >>= 6;
113                         dst[di + 2] = alphabet[triplet];
114                         dst[di + 3] = 0;
115                         length = di + 3;
116                         break;
117                 case 1:
118                         triplet = usrc[si];
119                         dst[di] = alphabet[triplet & 63]; triplet >>= 6;
120                         dst[di + 1] = alphabet[triplet];
121                         dst[di + 2] = 0;
122                         length = di + 2;
123                         break;
124                 default:
125                         dst[di] = 0;
126                         length = di;
127                         break;
128         }
129
130         while(si > 0) {
131                 di -= 4;
132                 si -= 3;
133                 triplet = usrc[si] | usrc[si + 1] << 8 | usrc[si + 2] << 16;
134                 dst[di] = alphabet[triplet & 63]; triplet >>= 6;
135                 dst[di + 1] = alphabet[triplet & 63]; triplet >>= 6;
136                 dst[di + 2] = alphabet[triplet & 63]; triplet >>= 6;
137                 dst[di + 3] = alphabet[triplet];
138         }
139
140         return length;
141 }
142
143 int b64encode(const char *src, char *dst, int length) {
144         return b64encode_internal(src, dst, length, base64_original);
145 }
146
147 int b64encode_urlsafe(const char *src, char *dst, int length) {
148         return b64encode_internal(src, dst, length, base64_urlsafe);
149 }
150
151 #if defined(HAVE_MINGW) || defined(HAVE_CYGWIN)
152 #ifdef HAVE_CYGWIN
153 #include <w32api/windows.h>
154 #endif
155
156 const char *winerror(int err) {
157         static char buf[1024], *ptr;
158
159         ptr = buf + sprintf(buf, "(%d) ", err);
160
161         if (!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
162                 NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), ptr, sizeof(buf) - (ptr - buf), NULL)) {
163                 strncpy(buf, "(unable to format errormessage)", sizeof(buf));
164         };
165
166         if((ptr = strchr(buf, '\r')))
167                 *ptr = '\0';
168
169         return buf;
170 }
171 #endif
172
173 unsigned int bitfield_to_int(const void *bitfield, size_t size) {
174         unsigned int value = 0;
175         if(size > sizeof value)
176                 size = sizeof value;
177         memcpy(&value, bitfield, size);
178         return value;
179 }