Rust wrapper: add wolfssl_wolfcrypt::chacha20_poly1305 module

This commit is contained in:
Josh Holtrop
2025-12-31 14:18:13 -05:00
parent 80c1228a38
commit 9007d12d2a
5 changed files with 453 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/build.rs
EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/headers.h
EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/src/aes.rs
EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/src/blake2.rs
EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/src/chacha20_poly1305.rs
EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/src/cmac.rs
EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/src/curve25519.rs
EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/src/dh.rs
@@ -30,6 +31,7 @@ EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/src/sha.rs
EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/src/sys.rs
EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/tests/test_aes.rs
EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/tests/test_blake2.rs
EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/tests/test_chacha20_poly1305.rs
EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/tests/test_cmac.rs
EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/tests/test_curve25519.rs
EXTRA_DIST += wrapper/rust/wolfssl-wolfcrypt/tests/test_dh.rs

View File

@@ -130,6 +130,9 @@ fn scan_cfg() -> Result<()> {
check_cfg(&binding, "wc_InitBlake2b", "blake2b");
check_cfg(&binding, "wc_InitBlake2s", "blake2s");
/* chacha20_poly1305 */
check_cfg(&binding, "wc_ChaCha20Poly1305_Encrypt", "chacha20_poly1305");
/* cmac */
check_cfg(&binding, "wc_InitCmac", "cmac");

View File

@@ -0,0 +1,242 @@
/*
* Copyright (C) 2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/*!
This module provides a Rust wrapper for the wolfCrypt library's
ChaCha20-Poly1305 functionality.
*/
#![cfg(chacha20_poly1305)]
use crate::sys;
use std::mem::MaybeUninit;
pub struct ChaCha20Poly1305 {
wc_ccp: sys::ChaChaPoly_Aead,
}
impl ChaCha20Poly1305 {
pub const KEYSIZE: usize = sys::CHACHA20_POLY1305_AEAD_KEYSIZE as usize;
pub const IV_SIZE: usize = sys::CHACHA20_POLY1305_AEAD_IV_SIZE as usize;
pub const AUTH_TAG_SIZE: usize = sys::CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE as usize;
/// Decrypt an input message from `ciphertext` using the ChaCha20 stream
/// cipher into the `plaintext` output buffer. It also performs Poly-1305
/// authentication, comparing the given `auth_tag` to an authentication
/// generated with the `aad` (additional authentication data). If Err is
/// returned, the output data, `plaintext` is undefined. However, callers
/// must unconditionally zeroize the output buffer to guard against
/// leakage of cleartext data.
///
/// # Parameters
///
/// * `key`: Encryption key (must be 32 bytes).
/// * `iv`: Initialization Vector (must be 12 bytes).
/// * `aad`: Additional authenticated data (can be any length).
/// * `ciphertext`: Input buffer containing encrypted cipher text.
/// * `auth_tag`: Input buffer containing authentication tag (must be 16
/// bytes).
/// * `plaintext`: Output buffer containing decrypted plain text.
///
/// # Returns
///
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
/// library error code value.
pub fn decrypt(key: &[u8], iv: &[u8], aad: &[u8], ciphertext: &[u8],
auth_tag: &[u8], plaintext: &mut [u8]) -> Result<(), i32> {
if key.len() != Self::KEYSIZE {
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
}
if iv.len() != Self::IV_SIZE {
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
}
if auth_tag.len() != Self::AUTH_TAG_SIZE {
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
}
let aad_size = aad.len() as u32;
let ciphertext_size = ciphertext.len() as u32;
let rc = unsafe {
sys::wc_ChaCha20Poly1305_Decrypt(key.as_ptr(), iv.as_ptr(),
aad.as_ptr(), aad_size, ciphertext.as_ptr(),
ciphertext_size, auth_tag.as_ptr(), plaintext.as_mut_ptr())
};
if rc != 0 {
return Err(rc);
}
Ok(())
}
/// Encrypt an input message from `plaintext` using the ChaCha20 stream
/// cipher into the `ciphertext` output buffer performing Poly-1305
/// authentication on the cipher text and storing the generated
/// authentication tag in the `auth_tag` output buffer.
///
/// # Parameters
///
/// * `key`: Encryption key (must be 32 bytes).
/// * `iv`: Initialization Vector (must be 12 bytes).
/// * `aad`: Additional authenticated data (can be any length).
/// * `plaintext`: Input plain text to encrypt.
/// * `ciphertext`: Output buffer for encrypted cipher text.
/// * `auth_tag`: Output buffer for authentication tag (must be 16 bytes).
///
/// # Returns
///
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
/// library error code value.
pub fn encrypt(key: &[u8], iv: &[u8], aad: &[u8], plaintext: &[u8],
ciphertext: &mut [u8], auth_tag: &mut [u8]) -> Result<(), i32> {
if key.len() != Self::KEYSIZE {
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
}
if iv.len() != Self::IV_SIZE {
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
}
if auth_tag.len() != Self::AUTH_TAG_SIZE {
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
}
let aad_size = aad.len() as u32;
let plaintext_size = plaintext.len() as u32;
let rc = unsafe {
sys::wc_ChaCha20Poly1305_Encrypt(key.as_ptr(), iv.as_ptr(),
aad.as_ptr(), aad_size, plaintext.as_ptr(), plaintext_size,
ciphertext.as_mut_ptr(), auth_tag.as_mut_ptr())
};
if rc != 0 {
return Err(rc);
}
Ok(())
}
/// Create a new ChaCha20Poly1305 instance.
///
/// # Parameters
///
/// * `key`: Encryption key (must be 32 bytes).
/// * `iv`: Initialization Vector (must be 12 bytes).
/// * `encrypt`: Whether the instance will be used to encrypt (true) or
/// decrypt (false).
///
/// Returns either Ok(chacha20poly1305) on success or Err(e) containing the
/// wolfSSL library error code value.
pub fn new(key: &[u8], iv: &[u8], encrypt: bool) -> Result<Self, i32> {
if key.len() != Self::KEYSIZE {
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
}
if iv.len() != Self::IV_SIZE {
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
}
let mut wc_ccp: MaybeUninit<sys::ChaChaPoly_Aead> = MaybeUninit::uninit();
let rc = unsafe {
sys::wc_ChaCha20Poly1305_Init(wc_ccp.as_mut_ptr(), key.as_ptr(),
iv.as_ptr(), if encrypt {1} else {0})
};
if rc != 0 {
return Err(rc);
}
let wc_ccp = unsafe { wc_ccp.assume_init() };
let chacha20poly1305 = ChaCha20Poly1305 { wc_ccp };
Ok(chacha20poly1305)
}
/// Update AAD (additional authenticated data).
///
/// This function should be called before `update_data()`.
///
/// # Parameters
///
/// * `aad`: Additional authenticated data.
///
/// # Returns
///
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
/// library error code value.
pub fn update_aad(&mut self, aad: &[u8]) -> Result<(), i32> {
let aad_size = aad.len() as u32;
let rc = unsafe {
sys::wc_ChaCha20Poly1305_UpdateAad(&mut self.wc_ccp,
aad.as_ptr(), aad_size)
};
if rc != 0 {
return Err(rc);
}
Ok(())
}
/// Update data (add additional input data to decrypt or encrypt).
///
/// This function can be called multiple times. If AAD is used, the
/// `update_aad()` function must be called before this function. The
/// `finalize()` function should be called after adding all input data to
/// finalize the operation and compute the authentication tag.
///
/// # Parameters
///
/// * `din`: Additional input data to decrypt or encrypt.
/// * `dout`: Buffer in which to store output data (must be the same length
/// as the input buffer).
///
/// # Returns
///
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
/// library error code value.
pub fn update_data(&mut self, din: &[u8], dout: &mut [u8]) -> Result<(), i32> {
if din.len() != dout.len() {
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
}
let din_size = din.len() as u32;
let rc = unsafe {
sys::wc_ChaCha20Poly1305_UpdateData(&mut self.wc_ccp,
din.as_ptr(), dout.as_mut_ptr(), din_size)
};
if rc != 0 {
return Err(rc);
}
Ok(())
}
/// Finalize the decrypt/encrypt operation.
///
/// This function consumes the `ChaCha20Poly1305` instance. The
/// `update_data()` function must be called before calling this function to
/// add all input data.
///
/// # Parameters
///
/// * `auth_tag`: Output buffer for authentication tag (must be 16 bytes).
///
/// # Returns
///
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
/// library error code value.
pub fn finalize(mut self, auth_tag: &mut [u8]) -> Result<(), i32> {
if auth_tag.len() != Self::AUTH_TAG_SIZE {
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
}
let rc = unsafe {
sys::wc_ChaCha20Poly1305_Final(&mut self.wc_ccp,
auth_tag.as_mut_ptr())
};
if rc != 0 {
return Err(rc);
}
Ok(())
}
}

View File

@@ -23,6 +23,7 @@ pub mod sys;
pub mod aes;
pub mod blake2;
pub mod chacha20_poly1305;
pub mod cmac;
pub mod curve25519;
pub mod dh;

View File

@@ -0,0 +1,205 @@
#![cfg(chacha20_poly1305)]
use wolfssl_wolfcrypt::chacha20_poly1305::*;
#[test]
fn test_chacha20_poly1305_1() {
let key1 = [
0x80u8, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
];
let plaintext1 = [
0x4cu8, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
0x74, 0x2e
];
let iv1 = [
0x07u8, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
0x44, 0x45, 0x46, 0x47
];
let aad1 = [
0x50u8, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
0xc4, 0xc5, 0xc6, 0xc7
];
let cipher1 = [
0xd3u8, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
0x61, 0x16
];
let auth_tag_1 = [
0x1au8, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
];
/* Encrypt */
let mut ccp = ChaCha20Poly1305::new(&key1, &iv1, true).expect("Error with new()");
ccp.update_aad(&aad1).expect("Error with update_aad()");
let mut out_cipher1 = [0u8; 114];
ccp.update_data(&plaintext1, &mut out_cipher1).expect("Error with update_data()");
let mut out_auth_tag_1 = [0u8; ChaCha20Poly1305::AUTH_TAG_SIZE];
ccp.finalize(&mut out_auth_tag_1).expect("Error with finalize()");
assert_eq!(out_cipher1, cipher1);
assert_eq!(out_auth_tag_1, auth_tag_1);
/* Decrypt */
let mut ccp = ChaCha20Poly1305::new(&key1, &iv1, false).expect("Error with new()");
ccp.update_aad(&aad1).expect("Error with update_aad()");
let mut out_plaintext1 = [0u8; 114];
ccp.update_data(&cipher1, &mut out_plaintext1).expect("Error with update_data()");
let mut out_auth_tag_1 = [0u8; ChaCha20Poly1305::AUTH_TAG_SIZE];
ccp.finalize(&mut out_auth_tag_1).expect("Error with finalize()");
assert_eq!(out_plaintext1, plaintext1);
assert_eq!(out_auth_tag_1, auth_tag_1);
}
#[test]
fn test_chacha20_poly1305_2() {
let key2 = [
0x1cu8, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
];
let plaintext2 = [
0x49u8, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74,
0x2d, 0x44, 0x72, 0x61, 0x66, 0x74, 0x73, 0x20,
0x61, 0x72, 0x65, 0x20, 0x64, 0x72, 0x61, 0x66,
0x74, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
0x6e, 0x74, 0x73, 0x20, 0x76, 0x61, 0x6c, 0x69,
0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x61, 0x20,
0x6d, 0x61, 0x78, 0x69, 0x6d, 0x75, 0x6d, 0x20,
0x6f, 0x66, 0x20, 0x73, 0x69, 0x78, 0x20, 0x6d,
0x6f, 0x6e, 0x74, 0x68, 0x73, 0x20, 0x61, 0x6e,
0x64, 0x20, 0x6d, 0x61, 0x79, 0x20, 0x62, 0x65,
0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64,
0x2c, 0x20, 0x72, 0x65, 0x70, 0x6c, 0x61, 0x63,
0x65, 0x64, 0x2c, 0x20, 0x6f, 0x72, 0x20, 0x6f,
0x62, 0x73, 0x6f, 0x6c, 0x65, 0x74, 0x65, 0x64,
0x20, 0x62, 0x79, 0x20, 0x6f, 0x74, 0x68, 0x65,
0x72, 0x20, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65,
0x6e, 0x74, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
0x6e, 0x79, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x2e,
0x20, 0x49, 0x74, 0x20, 0x69, 0x73, 0x20, 0x69,
0x6e, 0x61, 0x70, 0x70, 0x72, 0x6f, 0x70, 0x72,
0x69, 0x61, 0x74, 0x65, 0x20, 0x74, 0x6f, 0x20,
0x75, 0x73, 0x65, 0x20, 0x49, 0x6e, 0x74, 0x65,
0x72, 0x6e, 0x65, 0x74, 0x2d, 0x44, 0x72, 0x61,
0x66, 0x74, 0x73, 0x20, 0x61, 0x73, 0x20, 0x72,
0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65,
0x20, 0x6d, 0x61, 0x74, 0x65, 0x72, 0x69, 0x61,
0x6c, 0x20, 0x6f, 0x72, 0x20, 0x74, 0x6f, 0x20,
0x63, 0x69, 0x74, 0x65, 0x20, 0x74, 0x68, 0x65,
0x6d, 0x20, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20,
0x74, 0x68, 0x61, 0x6e, 0x20, 0x61, 0x73, 0x20,
0x2f, 0xe2, 0x80, 0x9c, 0x77, 0x6f, 0x72, 0x6b,
0x20, 0x69, 0x6e, 0x20, 0x70, 0x72, 0x6f, 0x67,
0x72, 0x65, 0x73, 0x73, 0x2e, 0x2f, 0xe2, 0x80,
0x9d
];
let iv2 = [
0x00u8, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08
];
let aad2 = [
0xf3u8, 0x33, 0x88, 0x86, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x4e, 0x91
];
let cipher2 = [
0x64u8, 0xa0, 0x86, 0x15, 0x75, 0x86, 0x1a, 0xf4,
0x60, 0xf0, 0x62, 0xc7, 0x9b, 0xe6, 0x43, 0xbd,
0x5e, 0x80, 0x5c, 0xfd, 0x34, 0x5c, 0xf3, 0x89,
0xf1, 0x08, 0x67, 0x0a, 0xc7, 0x6c, 0x8c, 0xb2,
0x4c, 0x6c, 0xfc, 0x18, 0x75, 0x5d, 0x43, 0xee,
0xa0, 0x9e, 0xe9, 0x4e, 0x38, 0x2d, 0x26, 0xb0,
0xbd, 0xb7, 0xb7, 0x3c, 0x32, 0x1b, 0x01, 0x00,
0xd4, 0xf0, 0x3b, 0x7f, 0x35, 0x58, 0x94, 0xcf,
0x33, 0x2f, 0x83, 0x0e, 0x71, 0x0b, 0x97, 0xce,
0x98, 0xc8, 0xa8, 0x4a, 0xbd, 0x0b, 0x94, 0x81,
0x14, 0xad, 0x17, 0x6e, 0x00, 0x8d, 0x33, 0xbd,
0x60, 0xf9, 0x82, 0xb1, 0xff, 0x37, 0xc8, 0x55,
0x97, 0x97, 0xa0, 0x6e, 0xf4, 0xf0, 0xef, 0x61,
0xc1, 0x86, 0x32, 0x4e, 0x2b, 0x35, 0x06, 0x38,
0x36, 0x06, 0x90, 0x7b, 0x6a, 0x7c, 0x02, 0xb0,
0xf9, 0xf6, 0x15, 0x7b, 0x53, 0xc8, 0x67, 0xe4,
0xb9, 0x16, 0x6c, 0x76, 0x7b, 0x80, 0x4d, 0x46,
0xa5, 0x9b, 0x52, 0x16, 0xcd, 0xe7, 0xa4, 0xe9,
0x90, 0x40, 0xc5, 0xa4, 0x04, 0x33, 0x22, 0x5e,
0xe2, 0x82, 0xa1, 0xb0, 0xa0, 0x6c, 0x52, 0x3e,
0xaf, 0x45, 0x34, 0xd7, 0xf8, 0x3f, 0xa1, 0x15,
0x5b, 0x00, 0x47, 0x71, 0x8c, 0xbc, 0x54, 0x6a,
0x0d, 0x07, 0x2b, 0x04, 0xb3, 0x56, 0x4e, 0xea,
0x1b, 0x42, 0x22, 0x73, 0xf5, 0x48, 0x27, 0x1a,
0x0b, 0xb2, 0x31, 0x60, 0x53, 0xfa, 0x76, 0x99,
0x19, 0x55, 0xeb, 0xd6, 0x31, 0x59, 0x43, 0x4e,
0xce, 0xbb, 0x4e, 0x46, 0x6d, 0xae, 0x5a, 0x10,
0x73, 0xa6, 0x72, 0x76, 0x27, 0x09, 0x7a, 0x10,
0x49, 0xe6, 0x17, 0xd9, 0x1d, 0x36, 0x10, 0x94,
0xfa, 0x68, 0xf0, 0xff, 0x77, 0x98, 0x71, 0x30,
0x30, 0x5b, 0xea, 0xba, 0x2e, 0xda, 0x04, 0xdf,
0x99, 0x7b, 0x71, 0x4d, 0x6c, 0x6f, 0x2c, 0x29,
0xa6, 0xad, 0x5c, 0xb4, 0x02, 0x2b, 0x02, 0x70,
0x9b
];
let auth_tag_2 = [
0xeeu8, 0xad, 0x9d, 0x67, 0x89, 0x0c, 0xbb, 0x22,
0x39, 0x23, 0x36, 0xfe, 0xa1, 0x85, 0x1f, 0x38
];
/* Encrypt */
let mut ccp = ChaCha20Poly1305::new(&key2, &iv2, true).expect("Error with new()");
ccp.update_aad(&aad2).expect("Error with update_aad()");
let mut out_cipher2 = [0u8; 265];
ccp.update_data(&plaintext2[0..128], &mut out_cipher2[0..128]).expect("Error with update_data()");
ccp.update_data(&plaintext2[128..265], &mut out_cipher2[128..265]).expect("Error with update_data()");
let mut out_auth_tag_2 = [0u8; ChaCha20Poly1305::AUTH_TAG_SIZE];
ccp.finalize(&mut out_auth_tag_2).expect("Error with finalize()");
assert_eq!(out_cipher2, cipher2);
assert_eq!(out_auth_tag_2, auth_tag_2);
/* Decrypt */
let mut ccp = ChaCha20Poly1305::new(&key2, &iv2, false).expect("Error with new()");
ccp.update_aad(&aad2).expect("Error with update_aad()");
let mut out_plaintext2 = [0u8; 265];
ccp.update_data(&cipher2[0..128], &mut out_plaintext2[0..128]).expect("Error with update_data()");
ccp.update_data(&cipher2[128..265], &mut out_plaintext2[128..265]).expect("Error with update_data()");
let mut out_auth_tag_2 = [0u8; ChaCha20Poly1305::AUTH_TAG_SIZE];
ccp.finalize(&mut out_auth_tag_2).expect("Error with finalize()");
assert_eq!(out_plaintext2, plaintext2);
assert_eq!(out_auth_tag_2, auth_tag_2);
}