-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathxof_aes.cpp
More file actions
51 lines (41 loc) · 1.04 KB
/
xof_aes.cpp
File metadata and controls
51 lines (41 loc) · 1.04 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
#include <string.h>
#include <openssl/conf.h>
#include <openssl/evp.h>
#include "xof_aes.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b))
using namespace std;
void XoAES::init()
{
ctx_ = EVP_CIPHER_CTX_new();
memset(zero_, 0, RATE_IN_BYTE);
pos_ = 0;
}
void XoAES::reset()
{
EVP_CIPHER_CTX_reset(ctx_);
pos_ = 0;
}
void XoAES::absorb_once(const uint8_t *in, size_t inlen)
{
int sz;
alignas(32) uint8_t iv[16] = {0,};
memcpy(iv, in+16, MIN(16, inlen-16));
EVP_EncryptInit_ex(ctx_, EVP_aes_128_ctr(), NULL, in, iv);
EVP_CIPHER_CTX_set_padding(ctx_, 0);
pos_ = RATE_IN_BYTE;
}
void XoAES::squeeze(uint8_t *out, size_t outlen)
{
int sz;
while (outlen) {
if (pos_ == RATE_IN_BYTE) {
EVP_EncryptUpdate(ctx_, buf_, &sz, zero_, RATE_IN_BYTE);
pos_ = 0;
}
unsigned int squeeze_size = MIN(RATE_IN_BYTE - pos_, outlen);
memcpy(out, buf_+pos_, squeeze_size);
outlen -= squeeze_size;
out += squeeze_size;
pos_ += squeeze_size;
}
}