Skip to content

Commit

Permalink
chacha20-poly1305 internals: Use InputTooLongError for return types.
Browse files Browse the repository at this point in the history
  • Loading branch information
briansmith committed Jan 1, 2025
1 parent 555d598 commit 21de773
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/aead/algorithm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

use crate::{constant_time, cpu, error, hkdf};
use crate::{
constant_time, cpu,
error::{self, InputTooLongError},
hkdf,
};
use core::ops::RangeFrom;

use super::{
Expand Down Expand Up @@ -246,6 +250,7 @@ fn chacha20_poly1305_seal(
_ => unreachable!(),
};
chacha20_poly1305::seal(key, nonce, aad, in_out, cpu_features)
.map_err(error::erase::<InputTooLongError>)
}

fn chacha20_poly1305_open(
Expand All @@ -262,4 +267,5 @@ fn chacha20_poly1305_open(
};
let in_out = Overlapping::new(in_out, src).map_err(error::erase::<SrcIndexError>)?;
chacha20_poly1305::open(key, nonce, aad, in_out, cpu_features)
.map_err(error::erase::<InputTooLongError>)
}
11 changes: 6 additions & 5 deletions src/aead/chacha20_poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use super::{
poly1305, Aad, Nonce, Tag,
};
use crate::{
cpu, error,
cpu,
error::InputTooLongError,
polyfill::{u64_from_usize, usize_from_u64_saturated},
};

Expand All @@ -43,11 +44,11 @@ pub(super) fn seal(
aad: Aad<&[u8]>,
in_out: &mut [u8],
cpu_features: cpu::Features,
) -> Result<Tag, error::Unspecified> {
) -> Result<Tag, InputTooLongError> {
let Key(chacha20_key) = key;

if in_out.len() > MAX_IN_OUT_LEN {
return Err(error::Unspecified);
return Err(InputTooLongError::new(in_out.len()));

Check warning on line 51 in src/aead/chacha20_poly1305.rs

View check run for this annotation

Codecov / codecov/patch

src/aead/chacha20_poly1305.rs#L51

Added line #L51 was not covered by tests
}
/// RFC 8439 Section 2.8 says the maximum AAD length is 2**64 - 1, which is
/// never larger than usize::MAX, so we don't need an explicit length
Expand Down Expand Up @@ -127,11 +128,11 @@ pub(super) fn open(
aad: Aad<&[u8]>,
in_out: Overlapping<'_>,
cpu_features: cpu::Features,
) -> Result<Tag, error::Unspecified> {
) -> Result<Tag, InputTooLongError> {
let Key(chacha20_key) = key;

if in_out.len() > MAX_IN_OUT_LEN {
return Err(error::Unspecified);
return Err(InputTooLongError::new(in_out.len()));

Check warning on line 135 in src/aead/chacha20_poly1305.rs

View check run for this annotation

Codecov / codecov/patch

src/aead/chacha20_poly1305.rs#L135

Added line #L135 was not covered by tests
}
// RFC 8439 Section 2.8 says the maximum AAD length is 2**64 - 1, which is
// never larger than usize::MAX, so we don't need an explicit length
Expand Down

0 comments on commit 21de773

Please sign in to comment.