1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
/*
* Copyright 2010 CurveDNS Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY CurveDNS Project ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CurveDNS Project OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of CurveDNS Project.
*
*/
/*
* $Id$
* $Author$
* $Date$
* $Revision$
*/
#include <time.h>
#include <sys/time.h>
#include "misc.h"
#include "ip.h"
// An open descriptor to /dev/urandom
int global_urandom_fd = -1;
static char *misc_getenv(const char *env, int mandatory) {
char *ptr;
ptr = getenv(env);
if (!ptr) {
if (mandatory) {
debug_log(DEBUG_FATAL, "the environment variable $%s must be set!\n", env);
}
return NULL;
}
return ptr;
}
// result = -1 - IP found in env, but not correct
// 0 - no IP found in env
// 1 - IP found in env and correct
int misc_getenv_ip(const char *env, int mandatory, anysin_t *result) {
char *ptr = misc_getenv(env, mandatory);
if (ptr) {
if (!ip_parse(result, ptr, "53"))
return -1;
return 1;
}
return 0;
}
int misc_getenv_int(const char *env, int mandatory, int *result) {
char *ptr = misc_getenv(env, mandatory);
if (ptr) {
*result = atoi(ptr);
return 1;
}
return 0;
}
int misc_getenv_double(const char *env, int mandatory, double *result) {
char *ptr = misc_getenv(env, mandatory);
if (ptr) {
*result = atof(ptr);
return 1;
}
return 0;
}
int misc_getenv_key(const char *env, int mandatory, uint8_t *result) {
char *ptr;
if (!(ptr = misc_getenv(env, mandatory))) {
return 0;
}
if (strlen(ptr) != 64) {
debug_log(DEBUG_FATAL, "key in $%s must be 64 bytes long\n", env);
return 0;
}
if (!misc_hex_decode(ptr, result)) {
debug_log(DEBUG_FATAL, "key in $%s appears to be invalid\n", env);
return 0;
}
return 1;
}
int misc_getenv_string(const char *env, int mandatory, char **result) {
char *ptr;
if (!(ptr = misc_getenv(env, mandatory))) {
return 0;
}
*result = ptr;
return 1;
}
int misc_char_hex(char in, uint8_t *out) {
if ((in >= '0') && (in <= '9')) {
*out = in - '0';
return 1;
} else if ((in >= 'a') && (in <= 'f')) {
*out = 10 + (in - 'a');
return 1;
} else if ((in >= 'A') && (in <= 'F')) {
*out = 10 + (in - 'A');
return 1;
} else {
return 0;
}
}
int misc_hex_char(uint8_t in, char *out) {
if (in < 10)
*out = in + '0';
else if (in < 16)
*out = (in - 10) + 'a';
else
return 0;
return 1;
}
int misc_hex_decode(const char *src, uint8_t *dst) {
uint8_t v1, v2;
while (*src) {
if (!misc_char_hex(*src++, &v1))
return 0;
if (!misc_char_hex(*src++, &v2))
return 0;
*dst++ = (v1 << 4) | v2;
}
return 1;
}
int misc_hex_encode(const uint8_t *src, int srclen, char *dst, int dstlen) {
int i = 0;
memset(dst, 0, dstlen);
if ((srclen * 2) < dstlen)
return 0;
while (i < srclen) {
if (!misc_hex_char(src[i] >> 4, dst))
return 0;
dst++;
if (!misc_hex_char(src[i] & 0xf, dst))
return 0;
dst++;
i++;
}
return 1;
}
/* All needed for cryptography random functions, taken from djbdns */
static uint32_t seed[32];
static uint32_t in[12];
static uint32_t out[8];
static int outleft = 0;
#define ROTATE(x,b) (((x) << (b)) | ((x) >> (32 - (b))))
#define MUSH(i,b) x = t[i] += (((x ^ seed[i]) + sum) ^ ROTATE(x,b));
static void surf(void) {
uint32_t t[12]; uint32_t x; uint32_t sum = 0;
int r; int i; int loop;
for (i = 0;i < 12;++i) t[i] = in[i] ^ seed[12 + i];
for (i = 0;i < 8;++i) out[i] = seed[24 + i];
x = t[11];
for (loop = 0;loop < 2;++loop) {
for (r = 0;r < 16;++r) {
sum += 0x9e3779b9;
MUSH(0,5) MUSH(1,7) MUSH(2,9) MUSH(3,13)
MUSH(4,5) MUSH(5,7) MUSH(6,9) MUSH(7,13)
MUSH(8,5) MUSH(9,7) MUSH(10,9) MUSH(11,13)
}
for (i = 0;i < 8;++i) out[i] ^= t[i + 4];
}
}
void misc_randombytes(uint8_t *x, unsigned long long xlen) {
int i;
while (xlen > 0) {
if (xlen < 1048576) i = xlen; else i = 1048576;
i = read(global_urandom_fd, x, i);
if (i < 1) {
sleep(1);
continue;
}
x += i;
xlen -= i;
}
}
int misc_crypto_random_init() {
global_urandom_fd = open("/dev/urandom", O_RDONLY);
if (global_urandom_fd < 0) {
perror("opening /dev/urandom failed");
return 0;
}
misc_randombytes((uint8_t *) in, sizeof(in));
return 1;
}
unsigned int misc_crypto_random(unsigned int n) {
if (!n) return 0;
if (!outleft) {
if (!++in[0]) if (!++in[1]) if (!++in[2]) ++in[3];
surf();
outleft = 8;
}
return out[--outleft] % n;
}
uint64_t misc_nanoseconds(void) {
struct timeval t;
gettimeofday(&t, (struct timezone *) 0);
return t.tv_sec * 1000000000LL + t.tv_usec * 1000LL;
}
static uint64_t noncecounter = 0;
static uint8_t noncemask[2] = {0xff, 0xff};
static uint8_t noncedata[2] = {0, 0};
void misc_crypto_nonce_init(char *ns) {
int i;
noncecounter = misc_nanoseconds();
if (!ns) ns = "";
i = 0;
while (i < 16) {
if (!ns[i]) break;
if (ns[i] != '0' && ns[i] != '1') break;
noncemask[i/8] = noncemask[i/8] * 2;
noncedata[i/8] = noncedata[i/8] * 2 + ns[i] - '0';
++i;
}
while (i < 16) {
noncemask[i/8] = noncemask[i/8] * 2 + 1;
noncedata[i/8] = noncedata[i/8] * 2;
++i;
}
}
void misc_crypto_nonce(uint8_t *nonce) {
uint64_t x;
// Do not decrease the counter if the clock jumps backwards !!
x = misc_nanoseconds();
if (noncecounter >= x) x = ++noncecounter;
noncecounter = x;
// Pack timestamp into machine-independent format
nonce[0] = x; x >>= 8;
nonce[1] = x; x >>= 8;
nonce[2] = x; x >>= 8;
nonce[3] = x; x >>= 8;
nonce[4] = x; x >>= 8;
nonce[5] = x; x >>= 8;
nonce[6] = x; x >>= 8;
nonce[7] = x;
for (x = 8; x < 12; ++x) {
nonce[x] = misc_crypto_random(256);
}
// nonce-separation part
for (x = 0; x < 2; ++x) {
nonce[x + 8] &= noncemask[x];
nonce[x + 8] += noncedata[x];
}
debug_log(DEBUG_DEBUG, "misc_crypto_nonce(): %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
nonce[0], nonce[1], nonce[2], nonce[3], nonce[4],nonce[5], nonce[6], nonce[7],
nonce[8], nonce[9], nonce[10], nonce[11]);
}
static const uint8_t kValues[] = {
99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,0,1,
2,3,4,5,6,7,8,9,99,99,99,99,99,99,99,99,10,11,12,99,13,14,15,99,16,17,18,
19,20,99,21,22,23,24,25,26,27,28,29,30,31,99,99,99,99,99,99,99,10,11,12,99,
13,14,15,99,16,17,18,19,20,99,21,22,23,24,25,26,27,28,29,30,31,99,99,99,99,99
};
// To Matthew Dempsky
// XXX: maybe faster?
int misc_base32_decode(uint8_t *output, unsigned int *ooutlen, const uint8_t *in, unsigned int inlen, int mode) {
unsigned int i = 0, j = 0;
unsigned int v = 0, bits = 0;
const unsigned outlen = *ooutlen;
while (j < inlen) {
if (in[j] & 0x80)
goto PROTO;
const uint8_t b = kValues[in[j++]];
if (b > 31)
goto PROTO;
v |= ((unsigned) b) << bits;
bits += 5;
if (bits >= 8) {
if (i >= outlen)
goto TOOBIG;
output[i++] = v;
bits -= 8;
v >>= 8;
}
}
if (mode) {
if (bits) {
if (i >= outlen)
goto TOOBIG;
output[i++] = v;
}
} else if (bits >= 5 || v)
goto PROTO;
*ooutlen = i;
return 1;
TOOBIG:
errno = E2BIG;
return 0;
PROTO:
errno = EPROTO;
return 0;
}
// To Matthew Dempsky
// XXX: maybe faster?
int misc_base32_encode(uint8_t *output, unsigned int *ooutlen, const uint8_t *in, unsigned int inlen) {
unsigned int i = 0, j = 0;
unsigned int v = 0, bits = 0;
const unsigned outlen = *ooutlen;
static const char kChars[] = "0123456789bcdfghjklmnpqrstuvwxyz";
while (j < inlen) {
v |= ((unsigned) in[j++]) << bits;
bits += 8;
while (bits >= 5) {
if (i >= outlen)
goto TOOBIG;
output[i++] = kChars[v & 31];
bits -= 5;
v >>= 5;
}
}
if (bits) {
if (i >= outlen)
goto TOOBIG;
output[i++] = kChars[v & 31];
bits -= 5;
v >>= 5;
}
*ooutlen = i;
return 1;
TOOBIG:
errno = E2BIG;
return 0;
}
|