XU XUv0.16.0

#crypto/aes

import: #crypto/aes

counts: 1 functions, 9 methods, 1 types, 1 constants, 0 method sets

FIPS-197 AES (Advanced Encryption Standard: AES-128 / AES-192 / AES-256) block cipher. Modes and multi-block operations (CBC / CTR / GCM) live in #crypto/cipher.

Overview

#crypto/aes implements the standard AES (Rijndael) symmetric block cipher. It supports 128-bit, 192-bit, and 256-bit key sizes. The active implementation depends on the compiler target and backend:

Examples

1. Single-Block AES-128 Encryption & Decryption

XuExample
@main
#crypto/aes

fn main() int {
    // 16-byte key for AES-128
    key := "0123456789abcdef"
    c := aes.new_cipher(&key) >>> (err) {
        println("Cipher init error: " + err.msg())
        << 1
    }

    // Single block must be exactly 16 bytes
    block := "16_bytes_secret!"
    enc := c.encrypt(&block) >>> (err) {
        println("Encryption error: " + err.msg())
        << 1
    }

    dec := c.decrypt(&enc) >>> (err) {
        println("Decryption error: " + err.msg())
        << 1
    }
    assert(dec == block)
    println("Single-block roundtrip passed!")
    << 0
}

2. Variable-Length AES-CBC with PKCS#7 Padding via #crypto/cipher

XuExample
@main
#crypto/aes
#crypto/cipher

fn main() int {
    key := "my-secret-key-16" // 16-byte key
    iv := "1234567890123456"  // 16-byte initialization vector
    c := aes.new_cipher(&key) >>> (err) { << 1 }

    // Arbitrary length payload with automatic PKCS#7 padding
    msg := "Xu systems language provides supersonic native AES encryption."
    enc := cipher.encrypt_cbc_pkcs7(&c, &iv, &msg) >>> (err) { << 1 }
    dec := cipher.decrypt_cbc_pkcs7(&c, &iv, &enc) >>> (err) { << 1 }

    assert(dec == msg)
    println("CBC PKCS#7 decrypted successfully!")
    << 0
}

Declarations

const BLOCK_SIZE

XuAPI
pub const BLOCK_SIZE = 16

Purpose

block_size is the AES block size in bytes (16 bytes, 128 bits).

struct Cipher

XuAPI
pub struct Cipher {
    pub nr int
    pub rk []int
    pub sbox []int
    pub inv []int
    pub te []int
    pub td []int
    pub erk []int
    pub drk []int
    pub hw_accel bool
    pub rk_bytes string
    pub drk_bytes string
}

Purpose

Cipher represents an instantiated AES cipher with expanded key schedule. Supports AES-128 (10 rounds), AES-192 (12 rounds), and AES-256 (14 rounds). Hardware acceleration (AES-NI / ARMv8 Crypto) is indicated by hw_accel.

Public fields

nr int
field (int)
rk []int
field ([]int)
sbox []int
field ([]int)
inv []int
field ([]int)
te []int
field ([]int)
td []int
field ([]int)
erk []int
field ([]int)
drk []int
field ([]int)
hw_accel bool
field (bool)
rk_bytes string
field (string)
drk_bytes string
field (string)

new_cipher

XuAPI
pub fn new_cipher(key &string) Cipher

Purpose

new_cipher creates and returns a new Cipher instance from the given key. The key argument must be 16 bytes (AES-128), 24 bytes (AES-192), or 32 bytes (AES-256). Throws an error if the key length is not 16, 24, or 32 bytes. Throws on failure; handle the error or propagate it to the caller.

Parameters

key &string
key (read-only borrow; caller keeps the original; type &string)

Returns

Cipher — a value of this type

(&Cipher) block_size

XuAPI
pub fn (c &Cipher) block_size() int

Purpose

block_size returns the cipher's block size in bytes (always 16).

Parameters

c &Cipher
The Cipher instance used by this call (borrow).

Returns

int — a value of this type

(&Cipher) encrypt_hw_into

XuAPI
pub fn (c &Cipher) encrypt_hw_into(out $&string, src &string) int

Purpose

Operation on &Cipher: encrypt hw into. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
The Cipher instance used by this call (borrow).
out $&string
out (exclusive borrow; may modify the original; type $&string)
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

int — a value of this type

(&Cipher) encrypt_arr16

XuAPI
pub fn (c &Cipher) encrypt_arr16(dst $&[16]int, src &[16]int) int

Purpose

Operation on &Cipher: encrypt arr16.

Parameters

c &Cipher
The Cipher instance used by this call (borrow).
dst $&[16]int
destination (exclusive borrow; may modify the original; type $&[16]int)
src &[16]int
source (read-only borrow; caller keeps the original; type &[16]int)

Returns

int — a value of this type

(&Cipher) encrypt

XuAPI
pub fn (c &Cipher) encrypt(src &string) string

Purpose

encrypt encrypts a single 16-byte block from src and returns the 16-byte ciphertext string. Throws an error if src.len != 16. For multi-block or streaming encryption, use crypto/cipher. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
The Cipher instance used by this call (borrow).
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

(&Cipher) encrypt_block

XuAPI
pub fn (c &Cipher) encrypt_block(dst $&[]int, src &[]int) bool

Purpose

encrypt_block encrypts a single 16-byte block from src into dst in place. Throws an error if src.len != 16 or dst.len != 16. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
The Cipher instance used by this call (borrow).
dst $&[]int
destination (exclusive borrow; may modify the original; type $&[]int)
src &[]int
source (read-only borrow; caller keeps the original; type &[]int)

Returns

bool — true or false

(&Cipher) decrypt_hw_into

XuAPI
pub fn (c &Cipher) decrypt_hw_into(out $&string, src &string) int

Purpose

Operation on &Cipher: decrypt hw into. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
The Cipher instance used by this call (borrow).
out $&string
out (exclusive borrow; may modify the original; type $&string)
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

int — a value of this type

(&Cipher) decrypt_arr16

XuAPI
pub fn (c &Cipher) decrypt_arr16(dst $&[16]int, src &[16]int) int

Purpose

Operation on &Cipher: decrypt arr16.

Parameters

c &Cipher
The Cipher instance used by this call (borrow).
dst $&[16]int
destination (exclusive borrow; may modify the original; type $&[16]int)
src &[16]int
source (read-only borrow; caller keeps the original; type &[16]int)

Returns

int — a value of this type

(&Cipher) decrypt

XuAPI
pub fn (c &Cipher) decrypt(src &string) string

Purpose

decrypt decrypts a single 16-byte block from src and returns the 16-byte plaintext string. Throws an error if src.len != 16. For multi-block or streaming decryption, use crypto/cipher. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
The Cipher instance used by this call (borrow).
src &string
source (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

(&Cipher) decrypt_block

XuAPI
pub fn (c &Cipher) decrypt_block(dst $&[]int, src &[]int) bool

Purpose

decrypt_block decrypts a single 16-byte block from src into dst in place. Throws an error if src.len != 16 or dst.len != 16. Throws on failure; handle the error or propagate it to the caller.

Parameters

c &Cipher
The Cipher instance used by this call (borrow).
dst $&[]int
destination (exclusive borrow; may modify the original; type $&[]int)
src &[]int
source (read-only borrow; caller keeps the original; type &[]int)

Returns

bool — true or false