-
Notifications
You must be signed in to change notification settings - Fork 12
/
extractlsb.c
212 lines (172 loc) · 4.25 KB
/
extractlsb.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
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
/*
* Copyright (C) 2019, Stephan Mueller <[email protected]>
*
* License: see LICENSE file in root directory
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
* WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 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 NOT ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
/*
* The tool extracts the 4 or 8 LSB of the high-res time stamp and
* concatenates them to form a binary data stream.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#define BITS_PER_SAMPLE 64
/*
Extract bits from sample based on significant bit mask
*/
static unsigned char extract(uint64_t sample, uint64_t mask)
{
unsigned char byte = 0;
int i, j = 0;
for (i = 0; i < BITS_PER_SAMPLE && mask; i++) {
if (mask & 1) {
byte |= (sample & 1) << j;
j++;
}
mask >>= 1;
sample >>= 1;
}
return (byte);
}
/*
Convert mask in hexadecimal format to binary
*/
static int hextolong(char *p_strmask, uint64_t *p_mask)
{
uint64_t mask = 0;
int count = 0;
while (*p_strmask) {
count++;
mask <<= 4;
if ((*p_strmask >= '0') && (*p_strmask <= '9'))
mask |= *p_strmask - '0';
else if ((*p_strmask >= 'A') && (*p_strmask <= 'F'))
mask |= *p_strmask - 'A' + 10;
else if ((*p_strmask >= '0') && (*p_strmask <= '9'))
mask |= *p_strmask - 'a' + 10;
else
return -1;
p_strmask++;
}
if (count > 16)
return(-1);
*p_mask = mask;
return(0);
}
/*
Count the number of bits on
*/
static int bitcount(uint64_t mask)
{
int i, j = 0;
for (i = 0; i < BITS_PER_SAMPLE && mask; i++) {
if (mask & 1) {
j++;
}
mask >>= 1;
}
return (j);
}
/*
Print 64 bits of long word masking with '-' those not matching value
*/
static char *printbits(uint64_t sample, int value)
{
static char buf[BITS_PER_SAMPLE + 9];
char *p_buf = buf + sizeof(buf) - 1;
int i;
*p_buf-- = '\0';
for (i = 0; i < BITS_PER_SAMPLE; i++) {
if (i % 8 == 0)
*p_buf-- = ' ';
if ((sample & 1) ^ value)
*p_buf = '-';
else
*p_buf = '0' + value;
p_buf--;
sample >>= 1;
}
return buf;
}
int main(int argc, char *argv[])
{
FILE *f = NULL;
char buf[64];
int fd = -1;
uint32_t count;
uint32_t i = 0;
uint64_t mask;
uint64_t unchanged0s, unchanged1s;
int rc;
if (argc != 5) {
printf("Usage: %s inputfile outfile maxevents mask\n", argv[0]);
return 1;
}
f = fopen(argv[1], "r");
if (!f) {
printf("File %s cannot be opened for read\n", argv[1]);
return 1;
}
fd = open(argv[2], O_CREAT|O_WRONLY|O_EXCL, 0777);
if (fd < 0) {
printf("File %s cannot be opened for write\n", argv[2]);
fclose(f);
return 1;
}
count = strtoul(argv[3], NULL, 10);
rc = hextolong(argv[4], &mask);
if (rc) {
printf("Mask value is incorrect [%s], use up to 16 hexadecimal characters", argv[4]);
return 1;
}
if (bitcount(mask) > 8) {
printf("SP800-90B tool only supports up to 8 bits. Check the mask value");
return 1;
}
unchanged0s = 0;
unchanged1s = ~0;
while (fgets(buf, sizeof(buf), f)) {
uint64_t sample;
unsigned char val;
char *saveptr = NULL;
char *res = NULL;
i++;
res = strtok_r(buf, " ", &saveptr);
if (!res) {
printf("strtok_r error\n");
return 1;
}
sample = strtoul(res, NULL, 10);
unchanged0s |= sample;
unchanged1s &= sample;
val = extract(sample, mask);
write(fd, &val, sizeof(val));
if (i >= count)
break;
}
printf("Processed %d items from %s samples with mask [0x%016llx] significant bits [%d]\n", i, argv[0], (unsigned long long)mask, bitcount(mask));
printf("Constant 0s in sample: \n%s\n", printbits(unchanged0s, 0));
printf("Constant 1s in sample: \n%s\n", printbits(unchanged1s, 1));
fclose(f);
close(fd);
return 0;
}