Concat

Purpose

Sometimes it is necessary to concatenate data together to form an input or output. This is represented via the || symbol in pseudocode.

For example, an AEAD ciphertext is usually a ciphertext concatenated with a tag.

Concatenation is often not as simple as it sounds. You MUST be careful to avoid canonicalization attacks.

This is important for hashing, message authentication (e.g. Encrypt-then-MAC), associated data in AEADs, and info in key derivation.

Two solutions are:

  1. Only ever concatenate fixed-length inputs. With two inputs, only one needs to be fixed-length. With three inputs, only two need to be fixed-length.

  2. For variable-length inputs, also concatenate the length of each input as a 64-bit unsigned integer (ulong) converted to bytes in little-endian. See this C# code example. This is done internally in AEADs like ChaCha20-Poly1305.

Usage

Concat

Fills a span with the concatenation of two spans.

Spans.Concat(Span<byte> buffer, ReadOnlySpan<byte> a, ReadOnlySpan<byte> b)

Exceptions

ArgumentOutOfRangeException

buffer has a length not equal to a.Length + b.Length.

ArgumentOutOfRangeException

a has a length not equal to buffer.Length - b.Length.

ArgumentOutOfRangeException

b has a length not equal to buffer.Length - a.Length.

Concat

Fills a span with the concatenation of three spans.

Spans.Concat(Span<byte> buffer, ReadOnlySpan<byte> a, ReadOnlySpan<byte> b, ReadOnlySpan<byte> c)

Exceptions

ArgumentOutOfRangeException

buffer has a length not equal to a.Length + b.Length + c.Length.

ArgumentOutOfRangeException

a has a length not equal to buffer.Length - b.Length - c.Length.

ArgumentOutOfRangeException

b has a length not equal to buffer.Length - a.Length - c.Length.

ArgumentOutOfRangeException

c has a length not equal to buffer.Length - a.Length - b.Length.

Concat

Fills a span with the concatenation of four spans.

Spans.Concat(Span<byte> buffer, ReadOnlySpan<byte> a, ReadOnlySpan<byte> b, ReadOnlySpan<byte> c, ReadOnlySpan<byte> d)

Exceptions

ArgumentOutOfRangeException

buffer has a length not equal to a.Length + b.Length + c.Length + d.Length.

ArgumentOutOfRangeException

a has a length not equal to buffer.Length - b.Length - c.Length - d.Length.

ArgumentOutOfRangeException

b has a length not equal to buffer.Length - a.Length - c.Length - d.Length.

ArgumentOutOfRangeException

c has a length not equal to buffer.Length - a.Length - b.Length - d.Length.

ArgumentOutOfRangeException

d has a length not equal to buffer.Length - a.Length - b.Length - c.Length.

Concat

Fills a span with the concatenation of five spans.

Spans.Concat(Span<byte> buffer, ReadOnlySpan<byte> a, ReadOnlySpan<byte> b, ReadOnlySpan<byte> c, ReadOnlySpan<byte> d, ReadOnlySpan<byte> e)

Exceptions

ArgumentOutOfRangeException

buffer has a length not equal to a.Length + b.Length + c.Length + d.Length + e.Length.

ArgumentOutOfRangeException

a has a length not equal to buffer.Length - b.Length - c.Length - d.Length - e.Length.

ArgumentOutOfRangeException

b has a length not equal to buffer.Length - a.Length - c.Length - d.Length - e.Length.

ArgumentOutOfRangeException

c has a length not equal to buffer.Length - a.Length - b.Length - d.Length - e.Length.

ArgumentOutOfRangeException

d has a length not equal to buffer.Length - a.Length - b.Length - c.Length - e.Length.

ArgumentOutOfRangeException

e has a length not equal to buffer.Length - a.Length - b.Length - c.Length - d.Length.

Concat

Fills a span with the concatenation of six spans.

Spans.Concat(Span<byte> buffer, ReadOnlySpan<byte> a, ReadOnlySpan<byte> b, ReadOnlySpan<byte> c, ReadOnlySpan<byte> d, ReadOnlySpan<byte> e, ReadOnlySpan<byte> f)

Exceptions

ArgumentOutOfRangeException

buffer has a length not equal to a.Length + b.Length + c.Length + d.Length + e.Length + f.Length.

ArgumentOutOfRangeException

a has a length not equal to buffer.Length - b.Length - c.Length - d.Length - e.Length - f.Length.

ArgumentOutOfRangeException

b has a length not equal to buffer.Length - a.Length - c.Length - d.Length - e.Length - f.Length.

ArgumentOutOfRangeException

c has a length not equal to buffer.Length - a.Length - b.Length - d.Length - e.Length - f.Length.

ArgumentOutOfRangeException

d has a length not equal to buffer.Length - a.Length - b.Length - c.Length - e.Length - f.Length.

ArgumentOutOfRangeException

e has a length not equal to buffer.Length - a.Length - b.Length - c.Length - d.Length - f.Length.

ArgumentOutOfRangeException

f has a length not equal to buffer.Length - a.Length - b.Length - c.Length - d.Length - e.Length.

Notes

There is unfortunately no Concat() function for arrays or spans in .NET, and there is not even a Span<T>.CopyTo(Span<T> destination, int index). This class fills that gap.

Last updated