-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
153 lines (120 loc) · 2.72 KB
/
utils.c
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
/*
* utils - various helper functions for sfet
*
* Written by Philipp Lay <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <libgen.h>
#include "utils.h"
#ifdef HAVE_GETRANDOM
#include <sys/syscall.h>
#include <linux/random.h>
#define getrandom(buf, len, flags) syscall(SYS_getrandom, (buf), (len), (flags))
#else
#define GRND_NONBLOCK 1
#define GRND_RANDOM 2
/*
* getrandom - very simple emulation of linux syscall until i get the
* real thing.
*/
int
getrandom(void *buf, size_t buflen, unsigned int flags)
{
const char *devname = (flags & GRND_RANDOM) ? "/dev/random" : "/dev/urandom";
int devflag = 0;
int dev;
int rval;
if (flags & GRND_NONBLOCK)
devflag |= O_NONBLOCK;
dev = open(devname, devflag);
if (dev == -1)
return -1;
rval = read(dev, buf, buflen);
close(dev);
return rval;
}
#endif
int
secrand(void *buf, size_t len)
{
uint8_t *ptr = (uint8_t *)buf;
int n;
int rc;
if (len > 256)
return -1;
/* try to read all random data without blocking */
#ifdef USE_DEV_RANDOM
n = getrandom(ptr, len, GRND_RANDOM|GRND_NONBLOCK);
#else
n = getrandom(ptr, len, GRND_NONBLOCK);
#endif
if (n == -1)
return -1;
if (n == len)
return 0;
/*
* we did'nt get everything on the first try... inform user
* and read more random now WITH blocking
*/
fprintf(stderr, "waiting for random... ");
while (n < len) {
rc = getrandom(ptr+n, len-n, GRND_RANDOM);
if (rc == -1) {
fprintf(stderr, "error\n");
return -1;
}
n += rc;
}
fprintf(stderr, "done\n");
return 0;
}
int
ctiseq(const void *s1, const void *s2, size_t n)
{
uint8_t *p1 = (uint8_t *)s1;
uint8_t *p2 = (uint8_t *)s2;
int mask = 0;
while (n-- > 0)
mask |= *p1++ ^ *p2++;
return ((mask-1) >> 8) & 1;
}
int
exists(const char *path)
{
struct stat sb;
return (stat(path, &sb) == -1) ? 0 : 1;
}
void
store_be64(uint8_t *p, uint64_t x)
{
p[0] = (x >> 56) & 0xff;
p[1] = (x >> 48) & 0xff;
p[2] = (x >> 40) & 0xff;
p[3] = (x >> 32) & 0xff;
p[4] = (x >> 24) & 0xff;
p[5] = (x >> 16) & 0xff;
p[6] = (x >> 8) & 0xff;
p[7] = (x >> 0) & 0xff;
}
uint64_t
load_be64(const uint8_t *p)
{
return ((uint64_t)p[0] << 56) | ((uint64_t)p[1] << 48) |
((uint64_t)p[2] << 40) | ((uint64_t)p[3] << 32) |
((uint64_t)p[4] << 24) | ((uint64_t)p[5] << 16) |
((uint64_t)p[6] << 8) | ((uint64_t)p[7]);
}