-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaead.cpp
More file actions
75 lines (62 loc) · 2.57 KB
/
Copy pathaead.cpp
File metadata and controls
75 lines (62 loc) · 2.57 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
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "photon_beetle.hpp"
#include <cassert>
#include <iostream>
// Compile it with
//
// g++ -std=c++20 -Wall -O3 -march=native -I ./include example/aead.cpp
int
main()
{
// Rate bytes of Photon-Beetle-AEAD-32 ( default ).
// You may also make R = 16, to use Photon-Beetle-AEAD-128
constexpr size_t R = 4;
// either Photon-Beetle-AEAD-32 or Photon-Beetle-AEAD-128
static_assert((R == 4) || (R == 16));
constexpr size_t mlen = 64; // plain/ cipher text length in bytes
constexpr size_t dlen = 32; // associated data length in bytes
// acquire memory resources
uint8_t* key = static_cast<uint8_t*>(std::malloc(photon_beetle::KEY_LEN));
uint8_t* nonce = static_cast<uint8_t*>(std::malloc(photon_beetle::NONCE_LEN));
uint8_t* tag = static_cast<uint8_t*>(std::malloc(photon_beetle::TAG_LEN));
uint8_t* dat = static_cast<uint8_t*>(std::malloc(dlen));
uint8_t* txt = static_cast<uint8_t*>(std::malloc(mlen));
uint8_t* enc = static_cast<uint8_t*>(std::malloc(mlen));
uint8_t* dec = static_cast<uint8_t*>(std::malloc(mlen));
// generate random inputs i.e. key, nonce, associated data and text
photon_utils::random_data(key, photon_beetle::KEY_LEN);
photon_utils::random_data(nonce, photon_beetle::NONCE_LEN);
photon_utils::random_data(dat, dlen);
photon_utils::random_data(txt, mlen);
// clean to be written memory allocations
std::memset(tag, 0, photon_beetle::TAG_LEN);
std::memset(enc, 0, mlen);
std::memset(dec, 0, mlen);
bool f = false;
// encrypt plain text ( never encrypts associated data )
photon_beetle::encrypt<R>(key, nonce, dat, dlen, txt, enc, mlen, tag);
// decrypt back to plain text
f = photon_beetle::decrypt<R>(key, nonce, tag, dat, dlen, enc, dec, mlen);
// verify authenticity & integrity check !
assert(f);
// verify plain text & decrypted data ( byte-by-byte )
for (size_t i = 0; i < mlen; i++) {
assert((txt[i] ^ dec[i]) == 0);
}
using namespace photon_utils;
std::cout << "Key : " << to_hex(key, photon_beetle::KEY_LEN) << "\n";
std::cout << "Nonce : " << to_hex(nonce, photon_beetle::NONCE_LEN) << "\n";
std::cout << "Data : " << to_hex(dat, dlen) << "\n";
std::cout << "Text : " << to_hex(txt, mlen) << "\n";
std::cout << "Tag : " << to_hex(tag, photon_beetle::TAG_LEN) << "\n";
std::cout << "Cipher : " << to_hex(enc, mlen) << "\n";
std::cout << "Decrypted: " << to_hex(dec, mlen) << "\n";
// memory resources being released
std::free(key);
std::free(nonce);
std::free(tag);
std::free(dat);
std::free(txt);
std::free(enc);
std::free(dec);
return EXIT_SUCCESS;
}