crypto/aes
lib/crypto/aes.vklib/crypto/aes.vk Pure-Veko constant-time algebraic AES-128 & AES-256 block cipher and NIST SP 800-38D AES-GCM AEAD. Avoids cache-timing S-box lookup tables by computing GF(2^8) inversion & affine transform algebraically. Zero capabilities required.
encrypt-block = fn [key bytes, blk bytes] bytes
Encrypts a single 16-byte block using AES-128 (16-byte key) or AES-256 (32-byte key).
Executable Example
use [aes = crypto/aes, hex = codec/hex]
k = hex/decode '000102030405060708090a0b0c0d0e0f'
p = hex/decode '00112233445566778899aabbccddeeff'
c = hex/encode (aes/encrypt-block k p)
if c != '69c4e0d86a7b0430d8cdb78070b4c55a' [ err 'aes-128-ecb vector mismatch' ] else [ .ok ]
gcm-encrypt = fn [key bytes, nonce bytes, plaintext bytes, aad bytes] bytes
Encrypts plaintext with AES-128-GCM (16-byte key) or AES-256-GCM (32-byte key),
12-byte nonce, and associated data aad, returning ciphertext || 16-byte tag.
Executable Example
use [aes = crypto/aes, hex = codec/hex]
k128 = bytes/fill 16 0
k256 = bytes/fill 32 0
iv = bytes/fill 12 0
empty = bytes/with-cap 0
t128 = hex/encode (aes/gcm-encrypt k128 iv empty empty)
t256 = hex/encode (aes/gcm-encrypt k256 iv empty empty)
if t128 != '58e2fccefa7e3061367f1d57a4e7455a' or t256 != '530f8afbc74536b9a963b4f1c4cb738b' [
err 'aes-gcm NIST vector mismatch'
] else [ .ok ]
gcm-decrypt = fn [key bytes, nonce bytes, ct_and_tag bytes, aad bytes] bytes
Authenticates and decrypts ct_and_tag (ciphertext || 16-byte tag) with AES-128-GCM or AES-256-GCM.
Returns err if authentication fails.
Executable Example
use [aes = crypto/aes]
k = bytes/fill 32 0x42
iv = bytes/fill 12 0x07
msg = bytes/from-str 'authenticated veko payload'
aad = bytes/from-str 'hdr-v1'
sealed = aes/gcm-encrypt k iv msg aad
pt = aes/gcm-decrypt k iv sealed aad
if pt != msg [ err 'aes-gcm roundtrip mismatch' ] else [ .ok ]