Skip to content

Commit 524c6af

Browse files
committedJul 23, 2019
Add seal_in_place_separate_tag and `seal_in_place_append_tag.
1 parent 467b157 commit 524c6af

File tree

2 files changed

+112
-36
lines changed

2 files changed

+112
-36
lines changed
 

‎src/aead.rs

+104-34
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,44 @@ impl<N: NonceSequence> core::fmt::Debug for SealingKey<N> {
268268
}
269269

270270
impl<N: NonceSequence> SealingKey<N> {
271+
/// Deprecated. Renamed to [`seal_in_place_append_tag()`].
272+
#[deprecated(note = "Renamed to `seal_in_place_append_tag`.")]
273+
#[inline]
274+
pub fn seal_in_place<A, InOut>(
275+
&mut self,
276+
aad: Aad<A>,
277+
in_out: &mut InOut,
278+
) -> Result<(), error::Unspecified>
279+
where
280+
A: AsRef<[u8]>,
281+
InOut: AsMut<[u8]> + for<'in_out> Extend<&'in_out u8>,
282+
{
283+
self.seal_in_place_append_tag(aad, in_out)
284+
}
285+
286+
/// Encrypts and signs (“seals”) data in place, appending the tag to the
287+
/// resulting ciphertext.
288+
///
289+
/// `key.seal_in_place_append_tag(aad, in_out)` is equivalent to:
290+
///
291+
/// ```skip
292+
/// key.seal_in_place_separate_tag(aad, in_out.as_mut())
293+
/// .map(|tag| in_out.extend(tag.as_ref()))
294+
/// ```
295+
#[inline]
296+
pub fn seal_in_place_append_tag<A, InOut>(
297+
&mut self,
298+
aad: Aad<A>,
299+
in_out: &mut InOut,
300+
) -> Result<(), error::Unspecified>
301+
where
302+
A: AsRef<[u8]>,
303+
InOut: AsMut<[u8]> + for<'in_out> Extend<&'in_out u8>,
304+
{
305+
self.seal_in_place_separate_tag(aad, in_out.as_mut())
306+
.map(|tag| in_out.extend(tag.as_ref()))
307+
}
308+
271309
/// Encrypts and signs (“seals”) data in place.
272310
///
273311
/// `nonce` must be unique for every use of the key to seal data.
@@ -278,49 +316,42 @@ impl<N: NonceSequence> SealingKey<N> {
278316
/// If there is no AAD then use `Aad::empty()`.
279317
///
280318
/// The plaintext is given as the input value of `in_out`. `seal_in_place()`
281-
/// will overwrite the plaintext with the ciphertext and then append the tag
282-
/// using `in_out.extend()`; the tag will be `self.algorithm.tag_len()` bytes
283-
/// long. Common types for `InOut` are `Vec<u8>` or `VecDeque<u8>` from the
284-
/// standard library, or `BytesMut` from the `bytes` crate.
319+
/// will overwrite the plaintext with the ciphertext and return the tag.
320+
/// For most protocols, the caller must append the tag to the ciphertext.
321+
/// The tag will be `self.algorithm.tag_len()` bytes long.
285322
#[inline]
286-
pub fn seal_in_place<A, InOut>(
323+
pub fn seal_in_place_separate_tag<A>(
287324
&mut self,
288325
aad: Aad<A>,
289-
in_out: &mut InOut,
290-
) -> Result<(), error::Unspecified>
326+
in_out: &mut [u8],
327+
) -> Result<Tag, error::Unspecified>
291328
where
292329
A: AsRef<[u8]>,
293-
InOut: AsMut<[u8]> + for<'in_out> Extend<&'in_out u8>,
294330
{
295-
seal_in_place_(&self.key, self.nonce_sequence.advance()?, aad, in_out)
331+
seal_in_place_separate_tag_(
332+
&self.key,
333+
self.nonce_sequence.advance()?,
334+
Aad::from(aad.0.as_ref()),
335+
in_out,
336+
)
296337
}
297338
}
298339

299340
#[inline]
300-
fn seal_in_place_<A: AsRef<[u8]>, InOut: AsMut<[u8]> + for<'in_out> Extend<&'in_out u8>>(
341+
fn seal_in_place_separate_tag_(
301342
key: &UnboundKey,
302343
nonce: Nonce,
303-
Aad(aad): Aad<A>,
304-
in_out: &mut InOut,
305-
) -> Result<(), error::Unspecified> {
306-
fn seal_in_place(
307-
key: &UnboundKey,
308-
nonce: Nonce,
309-
aad: Aad<&[u8]>,
310-
in_out: &mut [u8],
311-
) -> Result<Tag, error::Unspecified> {
312-
check_per_nonce_max_bytes(key.algorithm, in_out.len())?;
313-
Ok((key.algorithm.seal)(
314-
&key.inner,
315-
nonce,
316-
aad,
317-
in_out,
318-
key.cpu_features,
319-
))
320-
}
321-
let Tag(tag) = seal_in_place(key, nonce, Aad::from(aad.as_ref()), in_out.as_mut())?;
322-
in_out.extend(tag.as_ref());
323-
Ok(())
344+
aad: Aad<&[u8]>,
345+
in_out: &mut [u8],
346+
) -> Result<Tag, error::Unspecified> {
347+
check_per_nonce_max_bytes(key.algorithm, in_out.len())?;
348+
Ok((key.algorithm.seal)(
349+
&key.inner,
350+
nonce,
351+
aad,
352+
in_out,
353+
key.cpu_features,
354+
))
324355
}
325356

326357
/// The additionally authenticated data (AAD) for an opening or sealing
@@ -450,7 +481,8 @@ impl LessSafeKey {
450481
open_within_(&self.key, nonce, aad, in_out, ciphertext_and_tag)
451482
}
452483

453-
/// Like [`SealingKey::seal_in_place()`], except it accepts an arbitrary nonce.
484+
/// Deprecated. Renamed to [`seal_in_place_append_tag()`].
485+
#[deprecated(note = "Renamed to `seal_in_place_append_tag`.")]
454486
#[inline]
455487
pub fn seal_in_place<A, InOut>(
456488
&self,
@@ -462,7 +494,39 @@ impl LessSafeKey {
462494
A: AsRef<[u8]>,
463495
InOut: AsMut<[u8]> + for<'in_out> Extend<&'in_out u8>,
464496
{
465-
seal_in_place_(&self.key, nonce, aad, in_out)
497+
self.seal_in_place_append_tag(nonce, aad, in_out)
498+
}
499+
500+
/// Like [`SealingKey::seal_in_place_append_tag()`], except it accepts an
501+
/// arbitrary nonce.
502+
#[inline]
503+
pub fn seal_in_place_append_tag<A, InOut>(
504+
&self,
505+
nonce: Nonce,
506+
aad: Aad<A>,
507+
in_out: &mut InOut,
508+
) -> Result<(), error::Unspecified>
509+
where
510+
A: AsRef<[u8]>,
511+
InOut: AsMut<[u8]> + for<'in_out> Extend<&'in_out u8>,
512+
{
513+
self.seal_in_place_separate_tag(nonce, aad, in_out.as_mut())
514+
.map(|tag| in_out.extend(tag.as_ref()))
515+
}
516+
517+
/// Like `SealingKey::seal_in_place_separate_tag()`, except it accepts an
518+
/// arbitrary nonce.
519+
#[inline]
520+
pub fn seal_in_place_separate_tag<A>(
521+
&self,
522+
nonce: Nonce,
523+
aad: Aad<A>,
524+
in_out: &mut [u8],
525+
) -> Result<Tag, error::Unspecified>
526+
where
527+
A: AsRef<[u8]>,
528+
{
529+
seal_in_place_separate_tag_(&self.key, nonce, Aad::from(aad.0.as_ref()), in_out)
466530
}
467531

468532
/// The key's AEAD algorithm.
@@ -557,7 +621,13 @@ impl Eq for Algorithm {}
557621
/// An authentication tag.
558622
#[must_use]
559623
#[repr(C)]
560-
struct Tag(Block);
624+
pub struct Tag(Block);
625+
626+
impl AsRef<[u8]> for Tag {
627+
fn as_ref(&self) -> &[u8] {
628+
self.0.as_ref()
629+
}
630+
}
561631

562632
const MAX_KEY_LEN: usize = 32;
563633

‎tests/aead_tests.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ fn seal_with_key(
248248
in_out: &mut Vec<u8>,
249249
) -> Result<(), error::Unspecified> {
250250
let mut s_key: aead::SealingKey<OneNonceSequence> = make_key(algorithm, key, nonce);
251-
s_key.seal_in_place(aad, in_out)
251+
s_key.seal_in_place_append_tag(aad, in_out)
252252
}
253253

254254
fn open_with_key<'a>(
@@ -271,7 +271,7 @@ fn seal_with_less_safe_key(
271271
in_out: &mut Vec<u8>,
272272
) -> Result<(), error::Unspecified> {
273273
let key = make_less_safe_key(algorithm, key);
274-
key.seal_in_place(nonce, aad, in_out)
274+
key.seal_in_place_append_tag(nonce, aad, in_out)
275275
}
276276

277277
fn open_with_less_safe_key<'a>(
@@ -377,6 +377,12 @@ fn aead_chacha20_poly1305_openssh() {
377377
);
378378
}
379379

380+
#[test]
381+
fn test_tag_traits() {
382+
test::compile_time_assert_send::<aead::Tag>();
383+
test::compile_time_assert_sync::<aead::Tag>();
384+
}
385+
380386
#[test]
381387
fn test_aead_key_debug() {
382388
let key_bytes = [0; 32];

0 commit comments

Comments
 (0)
Please sign in to comment.