Bars Icon

AEGIS Encryption with PHP Sodium Extension

08-01-2024 11:29 AM

AEGIS is an AES-based family of authenticated encryption algorithms that are significantly faster than ubiquitous algorithms such as AES-GCM and CHACHA20-POLY1305. The Sodium extension in PHP 8.4 supports AEGIS-128L and AEGIS-256 encryption algorithms if the Sodium extension is compiled with libsodium 1.0.19 or later.

The two encryption algorithms in the AEGIS family, AEGIS-128L and AEGIS-256, are 2-3 times faster than AES-GCM, and 3-4 times faster than the CHACHA20-POLY1305 algorithms. They leverage hardware AES acceleration on x86_64 and aarch64 (64-bit ARM architecture) CPU architectures.

The AEGIS paper provides detailed information about the inner workings of the algorithms.

AEGIS Availability on PHP

AEGIS family of encryption algorithms is available on PHP when:

PHP versions 8.4 and later and
Sodium extension compiled with libsodium 1.0.19 and later and
on x86_64 or aarch64 CPU architectures

1. AEGIS-128L

AEGIS-128L can theoretically encrypt data lengths below 2^64 bits, and uses a 128-bit key. It is the only symmetric encryption algorithm in Sodium extension that uses a 128-bit key, while the others use a 256-bit key.

It also takes a 128-bit nonce value, which must be provided to decrypt.

The following is an example of generating a key, and nonce, encrypting data with additional data, and decrypting it:

2. AEGIS-256

AEGIS-256 uses 256-bit keys, and can encrypt data lengths below 2^64 bits. With some exceptions, AEGIS-256 is about 20% more computationally intensive than AEGIS-128L.The following is an example of generating a key and a nonce and encrypting and decrypting a plain text message to a ciphertext and back using additional data: