-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxoodyak_aead.cpp
57 lines (47 loc) · 1.88 KB
/
xoodyak_aead.cpp
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
#include "xoodyak.hpp"
#include <cassert>
#include <iostream>
// Compile it with
// g++ -std=c++20 -Wall -Wextra -O3 -I ./include example/xoodyak_aead.cpp
int
main()
{
constexpr size_t knt_len = 16ul;
constexpr size_t ad_len = 32ul;
constexpr size_t ct_len = 64ul;
uint8_t* key = static_cast<uint8_t*>(std::malloc(knt_len));
uint8_t* nonce = static_cast<uint8_t*>(std::malloc(knt_len));
uint8_t* tag = static_cast<uint8_t*>(std::malloc(knt_len));
uint8_t* data = static_cast<uint8_t*>(std::malloc(ad_len));
uint8_t* txt = static_cast<uint8_t*>(std::malloc(ct_len));
uint8_t* enc = static_cast<uint8_t*>(std::malloc(ct_len));
uint8_t* dec = static_cast<uint8_t*>(std::malloc(ct_len));
xoodyak_utils::random_data(key, knt_len);
xoodyak_utils::random_data(nonce, knt_len);
xoodyak_utils::random_data(data, ad_len);
xoodyak_utils::random_data(txt, ct_len);
bool f = false;
xoodyak::encrypt(key, nonce, data, ad_len, txt, enc, ct_len, tag);
f = xoodyak::decrypt(key, nonce, tag, data, ad_len, enc, dec, ct_len);
assert(f);
for (size_t i = 0; i < ct_len; i++) {
assert((txt[i] ^ dec[i]) == 0u);
}
using namespace xoodyak_utils;
std::cout << "Xoodyak AEAD" << std::endl << std::endl;
std::cout << "Key : " << to_hex(key, knt_len) << std::endl;
std::cout << "Nonce : " << to_hex(nonce, knt_len) << std::endl;
std::cout << "Associated Data : " << to_hex(data, ad_len) << std::endl;
std::cout << "Plain Text : " << to_hex(txt, ct_len) << std::endl;
std::cout << "Authentication Tag : " << to_hex(tag, knt_len) << std::endl;
std::cout << "Encrypted Text : " << to_hex(enc, ct_len) << std::endl;
std::cout << "Decrypted Text : " << to_hex(dec, ct_len) << std::endl;
std::free(key);
std::free(nonce);
std::free(tag);
std::free(data);
std::free(txt);
std::free(enc);
std::free(dec);
return EXIT_SUCCESS;
}