core/crypto/aead
aead
Types
3Algorithm
Algorithm :: enum int {
Invalid = 0,
AES_GCM_128 = 1,
AES_GCM_192 = 2,
AES_GCM_256 = 3,
CHACHA20POLY1305 = 4,
XCHACHA20POLY1305 = 5,
AEGIS_128L = 6,
AEGIS_128L_256 = 7, // AEGIS-128L (256-bit tag)
AEGIS_256 = 8,
AEGIS_256_256 = 9, // AEGIS-256 (256-bit tag)
DEOXYS_II_256 = 10,
}SourceAlgorithm is the algorithm identifier associated with a given Context.
Context
Context :: struct {
_algo: Algorithm,
_impl: union {
aes.Context_GCM,
chacha20poly1305.Context,
aegis.Context,
deoxysii.Context,
},
}SourceContext is a concrete instantiation of a specific AEAD algorithm.
Implementation
Implementation :: union {
aes.Implementation,
chacha20.Implementation,
}SourceImplementation is an AEAD implementation. Most callers will not need to use this as the package will automatically select the most performant implementation available.
Constants
1MAX_TAG_SIZE
MAX_TAG_SIZE :: 32SourceMAX_TAG_SIZE is the maximum size tag that can be returned by any of the Algorithms supported via this package.
Variables
4ALGORITHM_NAMES
ALGORITHM_NAMES :: [11]string = [Algorithm]string {
.Invalid = "Invalid",
.AES_GCM_128 = "AES-GCM-128",
.AES_GCM_192 = "AES-GCM-192",
.AES_GCM_256 = "AES-GCM-256",
.CHACHA20POLY1305 = "chacha20poly1305",
.XCHACHA20POLY1305 = "xchacha20poly1305",
.AEGISSourceALGORITM_NAMES is the Algorithm to algorithm name string.
IV_SIZES
IV_SIZES :: [11]int = [Algorithm]int {
.Invalid = 0,
.AES_GCM_128 = aes.GCM_IV_SIZE,
.AES_GCM_192 = aes.GCM_IV_SIZE,
.AES_GCM_256 = aes.GCM_IV_SIZE,
.CHACHA20POLY1305 = chacha20poly1305.IV_SIZE,
.XCHACHA20POLY1305 = chacha20poly1305.XIV_SIZE,
SourceIV_SIZES is the Algorithm to initialization vector size in bytes.
Note: Some algorithms (such as AES-GCM) support variable IV sizes.
KEY_SIZES
KEY_SIZES :: [11]int = [Algorithm]int {
.Invalid = 0,
.AES_GCM_128 = aes.KEY_SIZE_128,
.AES_GCM_192 = aes.KEY_SIZE_192,
.AES_GCM_256 = aes.KEY_SIZE_256,
.CHACHA20POLY1305 = chacha20poly1305.KEY_SIZE,
.XCHACHA20POLY1305 = chacha20poly1305.KEY_SISourceKEY_SIZES is the Algorithm to key size in bytes.
TAG_SIZES
TAG_SIZES :: [11]int = [Algorithm]int {
.Invalid = 0,
.AES_GCM_128 = aes.GCM_TAG_SIZE,
.AES_GCM_192 = aes.GCM_TAG_SIZE,
.AES_GCM_256 = aes.GCM_TAG_SIZE,
.CHACHA20POLY1305 = chacha20poly1305.TAG_SIZE,
.XCHACHA20POLY1305 = chacha20poly1305.TAG_SISourceTAG_SIZES is the Algorithm to tag size in bytes.
Procedures
9algorithm
algorithm :: proc(ctx: ^Context) -> (Algorithm)Sourcealgorithm returns the Algorithm used by a Context instance.
init
init :: proc(ctx: ^Context, algorithm: Algorithm, key: []u8, impl: Implementation)Sourceinit initializes a Context with a specific AEAD Algorithm.
iv_size
iv_size :: proc(ctx: ^Context) -> (int)Sourceiv_size returns the IV size of a Context instance in bytes.
open_ctx
open_ctx :: proc(
ctx: ^Context,
dst: []u8,
iv: []u8,
aad: []u8,
ciphertext: []u8,
tag: []u8,
) -> (bool)Sourceopen_ctx authenticates the aad and ciphertext, and decrypts the ciphertext, with the provided Context, iv, and tag, and stores the output in dst, returning true if and only if (⟺) the authentication was successful. If authentication fails, the destination buffer will be zeroed.
dst and plaintext MUST alias exactly or not at all.
open_oneshot
open_oneshot :: proc(
algo: Algorithm,
dst: []u8,
key: []u8,
iv: []u8,
aad: []u8,
ciphertext: []u8,
tag: []u8,
impl: Implementation,
) -> (bool)Sourceopen authenticates the aad and ciphertext, and decrypts the ciphertext, with the provided algorithm, key, iv, and tag, and stores the output in dst, returning true if and only if (⟺) the authentication was successful. If authentication fails, the destination buffer will be zeroed.
dst and ciphertext MUST alias exactly or not at all.
reset
reset :: proc(ctx: ^Context)Sourcereset sanitizes the Context. The Context must be re-initialized to be used again.
seal_ctx
seal_ctx :: proc(
ctx: ^Context,
dst: []u8,
tag: []u8,
iv: []u8,
aad: []u8,
plaintext: []u8,
)Sourceseal_ctx encrypts the plaintext and authenticates the aad and ciphertext, with the provided Context and iv, stores the output in dst and tag.
dst and plaintext MUST alias exactly or not at all.
seal_oneshot
seal_oneshot :: proc(
algo: Algorithm,
dst: []u8,
tag: []u8,
key: []u8,
iv: []u8,
aad: []u8,
plaintext: []u8,
impl: Implementation,
)Sourceseal_oneshot encrypts the plaintext and authenticates the aad and ciphertext, with the provided algorithm, key, and iv, stores the output in dst and tag.
dst and plaintext MUST alias exactly or not at all.
tag_size
tag_size :: proc(ctx: ^Context) -> (int)Sourcetag_size returns the tag size of a Context instance in bytes.
Procedure Groups
2open
open :: proc{open_ctx, open_oneshot}Sourceseal
seal :: proc{seal_ctx, seal_oneshot}Source