Skip to main content
Version: dev

Ciphers

aes128

Given a plaintext as an array of bytes, returns the corresponding aes128 ciphertext (CBC mode). Input padding is automatically performed using PKCS#7, so that the output length is input.len() + (16 - input.len() % 16).

aes128
// Given a plaintext as an array of bytes, returns the corresponding aes128 ciphertext (CBC mode). Input padding is performed using PKCS#7, so that the output length is `input.len() + (16 - input.len() % 16)`.
pub fn aes128_encrypt<let N: u32>(
input: [u8; N],
iv: [u8; 16],
key: [u8; 16],
) -> [u8; N + 16 - N % 16] {
let padding_length = (16 - N % 16) as u8;
let mut padded_input: [u8; N + 16 - N % 16] = [0; N + 16 - N % 16];
for i in 0..N {
padded_input[i] = input[i];
}
for i in N..N + 16 - N % 16 {
padded_input[i] = padding_length;
}
let output = aes128_encrypt_padded_input(padded_input, iv, key);
output
}

#[foreign(aes128_encrypt)]
fn aes128_encrypt_padded_input<let N: u32>(input: [u8; N], iv: [u8; 16], key: [u8; 16]) -> [u8; N] {}
Source code: noir_stdlib/src/aes128.nr#L1-L23
fn main() {
let input: [u8; 4] = [0, 12, 3, 15]; // Random bytes, will be padded to 16 bytes.
let iv: [u8; 16] = [0; 16]; // Initialization vector
let key: [u8; 16] = [0; 16]; // AES key
let ciphertext = std::aes128::aes128_encrypt(input, iv, key); // In this case, the output length will be 16 bytes.
}

This is a black box function. Read this section to learn more about black box functions in Noir.