NAME

Noise::CipherState - Symmetric encryption layer for the Noise Protocol

SYNOPSIS

use Noise::CipherState;

my $state = Noise::CipherState->new( cipher => 'ChaChaPoly' );
$state->set_key($key);

my $ciphertext = $state->encrypt_with_ad( $ad, $plaintext );
my $original   = $state->decrypt_with_ad( $ad, $ciphertext );

DESCRIPTION

Noise::CipherState manages a symmetric key and a nonce. it provides authenticated encryption and decryption, automatically incrementing the nonce after each operation.

METHODS

new( %params )

Constructor.

Expected parameter:

cipher: Either 'ChaChaPoly' (default) or 'AESGCM'.

set_key( $key )

Sets the 32-byte symmetric key and resets the nonce to 0.

set_nonce( $nonce )

Manually sets the 64bit integer nonce.

encrypt_with_ad( $ad, $plaintext )

Encrypts $plaintext with associated data $ad. The nonce is incremented after encryption. Returns ciphertext + 16-byte MAC tag.

decrypt_with_ad($ad, $ciphertext)

Decrypts $ciphertext (including tag) with associated data $ad. The nonce is incremented after decryption. Dies on MAC mismatch.

rekey( )

Updates the internal key k using the Rekey function defined in Section 11.3 of the Noise specification.

k( )

Returns the 32 byte symmetric key currently stored in the CipherState. This key is used for all encryption and decryption operations. If no key is set (like at the beginning of a handshake), this returns undef.

n( )

Returns the current 64bit integer nonce. The nonce is a counter that ensures the same key is never used with the same initialization vector twice. In Noise, the nonce is incremented after every successful encryption or decryption and is reset to 0 whenever a new key is set via set_key( ).

has_key( )

Returns a boolean indicating whether a symmetric key is currently initialized. Encryption and decryption methods will pass through data unchanged if has_key( ) is false.

SEE ALSO

Noise::SymmetricState

AUTHOR

Sanko Robinson <sanko@cpan.org>

COPYRIGHT

Copyright (C) 2026 by Sanko Robinson.

This library is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.