Skip to content

x.crypto.ascon #

ascon

ascon is an implementation of Ascon-Based Cryptography module implemented in pure V language. This module was mostly based on NIST Special Publication of 800 NIST SP 800-232 document. Its describes an Ascon-Based Lightweight Cryptography Standards for Constrained Devices thats provides Authenticated Encryption, Hash, and Extendable Output Functions. See the NIST.SP.800-232 Standard for more detail.

This module mostly implements all the features availables on the document. It currently implements:- Ascon-Hash256, Ascon-based hashing implementation that produces 256-bits output.

  • Ascon-XOF128, Ascon-based eXtendable Output Function (XOF) where the output size ofthe hash of the message can be selected by the user.- Ascon-CXOF128, a customized XOF that allows users to specify a customizationstring and choose the output size of the message hash.- Ascon-AEAD128, an Authenticated Encryption with Additional Data (AEAD) Scheme basedon Ascon-family crypto.

Constants #

const default_xof_size = 64

default_xof_size is the size of Ascon-XOF128 (and Ascon-CXOF128) checksum that has 512-bits length.

const max_hash_size = 4096 // in bytes

max_hash_size is a maximum size of Ascon-XOF128 (and Ascon-CXOF128) checksum output. Its very rare where checksum output bigger than 512-bits, So, we limiting it to prevent unconditional thing. This limitation was only occurs on this module, wee can change it later.

const tag_size = 16

tag_size is 128-bit size of Ascon-AEAD128 authentication tag

const nonce_size = 16

nonce_size is 128-bit size of Ascon-AEAD128 nonce

const key_size = 16

The constants for Ascon-AEAD128

key_size is 128-bit size of Ascon-AEAD128 key

fn cxof128 #

fn cxof128(msg []u8, size int, cs []u8) ![]u8

cxof128 creates an Ascon-CXOF128 checksum of msg with supplied size and custom string cs.

fn cxof128_64 #

fn cxof128_64(msg []u8, cs []u8) ![]u8

cxof128_64 creates a 64-bytes of Ascon-CXOF128 checksum of msg with supplied custom string in cs.

fn decrypt #

fn decrypt(key []u8, nonce []u8, ad []u8, ciphertext []u8) ![]u8

decrypt decrypts authenticated encrypted messages in ciphertext that encrypted under provided key and nonce with additional data in ad. It would check if authentication tag mas matching and return decrypted message if success or error on fails.

fn encrypt #

fn encrypt(key []u8, nonce []u8, ad []u8, msg []u8) ![]u8

encrypt encrypts the message under provided key and nonce and supplied additional data in ad. It returns an authenticated output of Ascon-AEAD128 ciphertext where authentication tag was stored within the end of ciphertext.

Note: The Ascon-AEAD128 key shall be kept secret,

fn new_aead128 #

fn new_aead128(key []u8) !&Aead128

new_aead128 creates a new Aead128 instance and initialized with supplied key.

fn new_cxof128 #

fn new_cxof128(size int, cs []u8) !&CXof128

new_cxof128 creates a new Ascon-CXOF128 instanace with cheksum size was set to size and custom string in cs. It returns error on fails.

fn new_hash256 #

fn new_hash256() &Hash256

new_hash256 creates a new Ascon-Hash256 instance.

fn new_xof128 #

fn new_xof128(size int) &Xof128

new_xof128 creates a new Ascon-XOF128 instance with provided size parameter.

fn sum256 #

fn sum256(msg_ []u8) []u8

sum256 creates an Ascon-Hash256 checksum for bytes on msg and produces a 256-bit hash.

fn xof128 #

fn xof128(msg []u8, size int) ![]u8

xof128 creates an Ascon-XOF128 checksum of msg with specified desired size of output.

fn xof128_64 #

fn xof128_64(msg []u8) ![]u8

xof128_64 creates a 64-bytes of Ascon-XOF128 checksum of msg.

struct Aead128 #

@[noinit]
struct Aead128 {
	State
mut:
	// 32-bytes of underlying key
	key [2]u64
}

Aead128 is an opaque provides an implementation of Ascon-AEAD128 from NIST.SP.800-232 standard. Its implements x.crypto.chacha20poly1305.AEAD interfaces.

fn (Aead128) nonce_size #

fn (c &Aead128) nonce_size() int

nonce_size returns the nonce size of Ascon-AEAD128 Aead128.

fn (Aead128) overhead #

fn (c &Aead128) overhead() int

overhead returns the maximum difference between the lengths of a plaintext and its ciphertext.

fn (Aead128) encrypt #

fn (mut c Aead128) encrypt(msg []u8, nonce []u8, ad []u8) ![]u8

encrypt encrypts the message under provided key and nonce and supplied additional data in ad. It returns an authenticated output of Ascon-AEAD128 ciphertext where authentication tag was stored within the end of ciphertext.

fn (Aead128) decrypt #

fn (mut c Aead128) decrypt(ciphertext []u8, nonce []u8, ad []u8) ![]u8

decrypt decrypts the ciphertext and validates authentication tag with provided nonce and additional data ad. It returns error on fails or tag unmatching.

struct CXof128 #

@[noinit]
struct CXof128 {
	Digest
mut:
	// Customization string
	cs []u8
	// checksum size, for fixed-output
	size int = default_xof_size
}

CXof128 is an opaque provides an implementation of Ascon-CXOF128 from NIST.SP.800-232 standard. Its implements hash.Hash interface with checksum-size stored on instance creation.

fn (CXof128) size #

fn (x &CXof128) size() int

size returns an underlying size of CXof128 checksum in fixed-sized manner. Internally, its return underlying size stored on current CXof128 instance.

fn (CXof128) block_size #

fn (x &CXof128) block_size() int

block_size returns an underlying CXof128 block size operates on, ie, 8-bytes

fn (CXof128) write #

fn (mut x CXof128) write(msg []u8) !int

write writes out the content of message and updates internal CXof128 state.

fn (CXof128) sum #

fn (mut x CXof128) sum(msg []u8) []u8

sum returns an Ascon-CXOF128 checksum of the bytes in msg. Its produces x.size of checksum bytes. If you want to more extendible output, use .read call instead.

fn (CXof128) read #

fn (mut x CXof128) read(mut dst []u8) !int

read tries to read dst.len bytes of hash output from current CXof128 state and stored into dst.

fn (CXof128) reset #

fn (mut x CXof128) reset()

reset resets internal CXof128 state into default initialized state.

fn (CXof128) free #

unsafe
fn (mut x CXof128) free()

free releases out the resources taken by the x. Dont use x after .free call.

struct Hash256 #

@[noinit]
struct Hash256 {
	Digest
}

Hash256 is an opaque provides an implementation of Ascon-Hash256 from NIST.SP.800-232 standard. Its implements hash.Hash interface.

fn (Hash256) size #

fn (h &Hash256) size() int

size returns an underlying size of Hash256 checksum, ie, 32-bytes

fn (Hash256) block_size #

fn (h &Hash256) block_size() int

block_size returns an underlying Hash256 block size operates on, ie, 8-bytes

fn (Hash256) reset #

fn (mut h Hash256) reset()

reset resets and reinit internal Hash256 state into default initialized state.

fn (Hash256) free #

unsafe
fn (mut h Hash256) free()

free releases out the resources taken by the h. Dont use x after .free call.

fn (Hash256) write #

fn (mut h Hash256) write(msg []u8) !int

write writes out the content of message and updates internal Hash256 state.

fn (Hash256) sum #

fn (mut h Hash256) sum(data []u8) []u8

sum returns an Ascon-Hash256 checksum of the bytes in data.

struct Xof128 #

@[noinit]
struct Xof128 {
	Digest
mut:
	// The size of Xof128 cheksum, when you dont specify it
	size int = default_xof_size
}

Xof128 is an opaque provides an implementation of Ascon-XOF128 from NIST.SP.800-232 standard. Its implements hash.Hash interface with checksum size stored on instance creation.

fn (Xof128) size #

fn (x &Xof128) size() int

size returns an underlying size of Xof128 checksum in fixed-sized manner. Internally, its return underlying size stored on current Xof128 instance.

fn (Xof128) block_size #

fn (x &Xof128) block_size() int

block_size returns an underlying Xof128 block size operates on, ie, 8-bytes

fn (Xof128) write #

fn (mut x Xof128) write(msg []u8) !int

write writes out the content of message and updates internal Xof128 state.

fn (Xof128) sum #

fn (mut x Xof128) sum(msg []u8) []u8

sum returns an Ascon-XOF128 checksum of the bytes in msg. Its produces x.size of checksum bytes. If you want to more extendible output, use .read call instead.

fn (Xof128) read #

fn (mut x Xof128) read(mut dst []u8) !int

read tries to read dst.len bytes of hash output from current Xof128 state and stored into dst.

Note: 1 ≤ dst.len ≤ max_hash_size.

fn (Xof128) reset #

fn (mut x Xof128) reset()

reset resets internal Xof128 state into default initialized state.

fn (Xof128) free #

unsafe
fn (mut x Xof128) free()

free releases out the resources taken by the x. Dont use x after .free call.