Geralt
  • Introduction
  • Random data
  • Constant time
  • Secure memory
  • Encoding
  • Padding
  • Hashing
  • Message authentication
  • Password hashing
  • Key derivation
  • Authenticated encryption
    • Stream and file encryption
    • AEGIS-128L
    • AEGIS-256
    • ChaCha20-Poly1305
    • XChaCha20-Poly1305
  • Key exchange
  • Digital signatures
  • Advanced
    • Validation
    • Concat
    • ChaCha20
    • XChaCha20
    • HChaCha20
    • Poly1305
    • Ed25519 to X25519
Powered by GitBook
On this page
  • Purpose
  • Usage
  • Fill
  • GetPaddedLength
  • Pad
  • GetUnpaddedLength
  • Notes

Padding

Last updated 1 year ago

Purpose

The length of a ciphertext from a is equal to the length of the plaintext. In most cases, this is not considered an issue. However, hiding the length of a message can be desirable, and padding can be used to do this.

The amount of padding, determined by the block size, can either be deterministic or . Both have their .

Padding to a block size much smaller than the message length leaves the approximate unpadded length largely unprotected. can be used to limit leakage.

Padding should be applied to the plaintext before encryption and removed from the plaintext after decryption. The amount of padding does not need to be stored.

Usage

Fill

Fills a span with padding. This can then be with some data.

Padding.Fill(Span<byte> buffer)

Exceptions

buffer has a length of 0.

GetPaddedLength

Returns the required buffer size for Pad() based on the unpadded length and a block size (e.g. 16 bytes).

Padding.GetPaddedLength(int unpaddedLength, int blockSize)

Exceptions

unpaddedLength is less than 0.

blockSize is less than or equal to 0.

The amount of padding is too large.

Pad

Fills a span with the data padded up to the specified block size (e.g. a multiple of 16 bytes).

Padding.Pad(Span<byte> buffer, ReadOnlySpan<byte> data, int blockSize)

Exceptions

buffer has a length not equal to GetPaddedLength(data.Length, blockSize).

blockSize is less than or equal to 0.

GetUnpaddedLength

Returns the number of bytes to slice from the end of the padded data.

Padding.GetUnpaddedLength(ReadOnlySpan<byte> paddedData, int blockSize)

Exceptions

paddedData has a length of 0.

blockSize is less than or equal to 0.

Incorrect padding.

Notes

It is very difficult to hide that cryptography is being used. For example, even if padding is done appropriately and there are no plaintext headers, public keys are .

Using padding to hide the length of a password is NOT recommended. Instead, the password can be prehashed using or on the client before being sent to the server for password hashing.

stream cipher
ISO/IEC 7816-4
randomised
strengths and weaknesses
PADMÉ
manually concatenated
ArgumentOutOfRangeException
ArgumentOutOfRangeException
ArgumentOutOfRangeException
ArgumentOutOfRangeException
ArgumentOutOfRangeException
ArgumentOutOfRangeException
ArgumentOutOfRangeException
ArgumentOutOfRangeException
FormatException
X25519
distinguishable from random
BLAKE2b
Argon2id