-
Notifications
You must be signed in to change notification settings - Fork 0
/
anonymization.c
94 lines (79 loc) · 2.29 KB
/
anonymization.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
#include "anonymization.h"
#include <assert.h>
#include <endian.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sha1.h"
#include "util.h"
static uint8_t seed[ANONYMIZATION_SEED_LEN];
static char seed_hex_digest[SHA_DIGEST_LENGTH * 2 + 1];
static int initialized = 0;
/* Anonymize a buffer of given length. Places the resulting digest into the
* provided digest buffer, which must be at least ANONYMIZATION_DIGEST_LENGTH
* bytes long. */
static void anonymization_process(const uint8_t* const data,
const int len,
unsigned char* const digest)
{
assert(initialized);
sha1_hmac(seed, ANONYMIZATION_SEED_LEN, data, len, digest);
}
static int init_hex_seed_digest()
{
unsigned char seed_digest[SHA_DIGEST_LENGTH];
anonymization_process(seed, ANONYMIZATION_SEED_LEN, seed_digest);
const char* hex_digest = buffer_to_hex(seed_digest, SHA_DIGEST_LENGTH);
if (!hex_digest) {
return -1;
}
memcpy(seed_hex_digest, hex_digest, sizeof(seed_hex_digest));
seed_hex_digest[sizeof(seed_hex_digest) - 1] = '\0';
return 0;
}
int anonymization_init()
{
FILE* handle = fopen(ANONYMIZATION_SEED_FILE, "rb");
if (!handle) {
perror("Error opening seed file");
return -1;
}
if (fread(seed, 1, ANONYMIZATION_SEED_LEN, handle) < ANONYMIZATION_SEED_LEN) {
perror("Error reading seed file");
fclose(handle);
return -1;
}
initialized = 1;
if (init_hex_seed_digest()) {
initialized = 0;
return -1;
}
return 0;
}
#define MAC_UPPER_MASK 0xffffff000000
#define MAC_LOWER_MASK 0x000000ffffff
inline int anonymize_mac(uint8_t mac[ETH_ALEN], uint8_t digest[ETH_ALEN])
{
unsigned char mac_digest[ANONYMIZATION_DIGEST_LENGTH];
anonymization_process(mac, ETH_ALEN, mac_digest);
memcpy(digest, mac_digest, ETH_ALEN);
memcpy(digest, mac, ETH_ALEN / 2);
return 0;
}
inline int anonymize_ip(uint32_t address, uint64_t* digest)
{
unsigned char address_digest[ANONYMIZATION_DIGEST_LENGTH];
anonymization_process((unsigned char*)&address,
sizeof(address),
address_digest);
*digest = *(uint64_t*)address_digest;
return 0;
}
int anonymization_write_update(gzFile handle)
{
if (!gzprintf(handle, "%s\n\n", seed_hex_digest)) {
perror("Error writing update");
return -1;
}
return 0;
}