Repository navigation
Expand file tree
/
Copy pathutil.cpp
More file actions
executable file
·62 lines (48 loc) · 1.42 KB
/
Copy pathutil.cpp
File metadata and controls
executable file
·62 lines (48 loc) · 1.42 KB
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
#include "util.h"
#include <iomanip>
#include <iostream>
#include <string.h>
using namespace std;
AESContext::AESContext() {
FILE* f = fopen("/dev/urandom", "r");
if (f == NULL) {
throw "unable to open /dev/urandom";
}
uint32_t bytesRead = fread(keyBytes, 1, AES_KEY_LEN, f);
if (bytesRead != AES_KEY_LEN) {
throw "failed to initialize AES key";
}
fclose(f);
AES_128_Key_Expansion(keyBytes, &key);
}
AESContext::AESContext(uint8_t* keyBuf) {
Init(keyBuf);
}
AESContext::~AESContext() {
memset(&key, 0, sizeof(key));
memset(keyBytes, 0, sizeof(keyBytes));
}
void AESContext::Init(uint8_t* keyBuf) {
memcpy(keyBytes, keyBuf, AES_KEY_LEN);
AES_128_Key_Expansion(keyBytes, &key);
}
void AESContext::GetBlock(block& output, uint64_t tag, uint64_t ind) const {
output = MAKE_BLOCK(tag, ind);
AES_ecb_encrypt_blk(&output, &key);
}
void AESContext::GetBlocks(block* buf, uint32_t nBlocks, uint64_t tag) const {
for (uint32_t i = 0; i < nBlocks; i++) {
buf[i] = MAKE_BLOCK(tag, i);
}
AES_ecb_encrypt_blks(buf, nBlocks, &key);
}
void AESContext::FillBuffer(uint8_t* buf, uint32_t len, uint64_t tag) const {
uint32_t nBlocks = len / sizeof(block);
uint32_t bytesRemaining = len % sizeof(block);
GetBlocks((block*) buf, nBlocks, tag);
if (bytesRemaining != 0) {
block lastBlock;
GetBlock(lastBlock, tag, nBlocks);
memcpy(buf + nBlocks * sizeof(block), &lastBlock, bytesRemaining);
}
}