Skip to content

Commit

Permalink
Add examples demonstrating how to use Ascon AEAD, Hash and Xof API
Browse files Browse the repository at this point in the history
Signed-off-by: Anjan Roy <[email protected]>
  • Loading branch information
itzmeanjan committed Dec 3, 2024
1 parent 3cbf186 commit 70f4f41
Show file tree
Hide file tree
Showing 16 changed files with 148 additions and 435 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ BUILD_DIR := build

include tests/test.mk
include benches/bench.mk
include examples/example.mk

$(SUBTLE_INC_DIR):
git submodule update --init subtle
Expand Down
50 changes: 0 additions & 50 deletions examples/ascon128_aead.cpp

This file was deleted.

50 changes: 0 additions & 50 deletions examples/ascon128a_aead.cpp

This file was deleted.

50 changes: 0 additions & 50 deletions examples/ascon80pq_aead.cpp

This file was deleted.

41 changes: 41 additions & 0 deletions examples/ascon_aead128.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "ascon/aead/ascon_aead128.hpp"
#include "example_helper.hpp"
#include <array>
#include <cassert>
#include <iostream>

int
main()
{
constexpr size_t plaintext_byte_len = 64;
constexpr size_t associated_data_byte_len = 32;

std::array<uint8_t, ascon_aead128::KEY_BYTE_LEN> key{};
std::array<uint8_t, ascon_aead128::NONCE_BYTE_LEN> nonce{};
std::array<uint8_t, ascon_aead128::TAG_BYTE_LEN> tag{};
std::vector<uint8_t> associated_data(associated_data_byte_len);
std::vector<uint8_t> plain_text(plaintext_byte_len);
std::vector<uint8_t> cipher_text(plaintext_byte_len);
std::vector<uint8_t> deciphered_text(plaintext_byte_len);

generate_random_data<uint8_t>(key);
generate_random_data<uint8_t>(nonce);
generate_random_data<uint8_t>(associated_data);
generate_random_data<uint8_t>(plain_text);

ascon_aead128::encrypt(key, nonce, associated_data, plain_text, cipher_text, tag);
const bool is_decrypted = ascon_aead128::decrypt(key, nonce, associated_data, cipher_text, deciphered_text, tag);

assert(is_decrypted);
assert(std::ranges::equal(plain_text, deciphered_text));

std::cout << "Ascon-AEAD128\n\n";
std::cout << "Key :\t" << bytes_to_hex_string(key) << "\n";
std::cout << "Nonce :\t" << bytes_to_hex_string(nonce) << "\n";
std::cout << "Data :\t" << bytes_to_hex_string(associated_data) << "\n";
std::cout << "Text :\t" << bytes_to_hex_string(plain_text) << "\n";
std::cout << "Encrypted :\t" << bytes_to_hex_string(cipher_text) << "\n";
std::cout << "Decrypted :\t" << bytes_to_hex_string(deciphered_text) << "\n";

return EXIT_SUCCESS;
}
30 changes: 0 additions & 30 deletions examples/ascon_hash.cpp

This file was deleted.

28 changes: 28 additions & 0 deletions examples/ascon_hash256.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "ascon/hashes/ascon_hash256.hpp"
#include "example_helper.hpp"
#include <array>
#include <cassert>
#include <iostream>
#include <vector>

int
main()
{
constexpr size_t msg_byte_len = 64;

std::vector<uint8_t> msg(msg_byte_len);
std::array<uint8_t, ascon_hash256::DIGEST_BYTE_LEN> digest{};

generate_random_data<uint8_t>(msg);

ascon_hash256::ascon_hash256_t hasher;
assert(hasher.absorb(msg));
assert(hasher.finalize());
assert(hasher.digest(digest));

std::cout << "Ascon-Hash256\n\n";
std::cout << "Message :\t" << bytes_to_hex_string(msg) << "\n";
std::cout << "Digest :\t" << bytes_to_hex_string(digest) << "\n";

return EXIT_SUCCESS;
}
30 changes: 0 additions & 30 deletions examples/ascon_hasha.cpp

This file was deleted.

65 changes: 0 additions & 65 deletions examples/ascon_mac.cpp

This file was deleted.

Loading

0 comments on commit 70f4f41

Please sign in to comment.