PDFium Delphi Component Docs

FPdfAes unit

Unit: FPdfAes
Pure-Pascal AES-128 and AES-256 block cipher with key expansion, single-block encrypt/decrypt, and AES-256-CBC stream wrappers used by the PDF encryption handlers

Syntax

TAesBlock = array[0..15] of Byte;
TAes128Key = array[0..15] of Byte;
TAes128ExpandedKey = array[0..175] of Byte;
TAes256Key = array[0..31] of Byte;
TAes256ExpandedKey = array[0..239] of Byte;

procedure Aes128KeyExpansion(const Key: TAes128Key; var Expanded: TAes128ExpandedKey);
procedure Aes128EncryptBlock(const Expanded: TAes128ExpandedKey;
  const Input: TAesBlock; var Output: TAesBlock);
procedure Aes256KeyExpansion(const Key: TAes256Key; var Expanded: TAes256ExpandedKey);
procedure Aes256EncryptBlock(const Expanded: TAes256ExpandedKey;
  const Input: TAesBlock; var Output: TAesBlock);
procedure Aes256DecryptBlock(const Expanded: TAes256ExpandedKey;
  const Input: TAesBlock; var Output: TAesBlock);
function Aes256CbcEncryptPKCS7(const Key: TAes256Key; const IV: TAesBlock;
  const Plain: TBytes): TBytes;
function Aes256CbcEncryptNoPad(const Key: TAes256Key; const IV: TAesBlock;
  const Plain: TBytes): TBytes;
function Aes256CbcDecryptPKCS7(const Key: TAes256Key; const IV: TAesBlock;
  const Cipher: TBytes): TBytes;
function Aes256CbcDecryptNoPad(const Key: TAes256Key; const IV: TAesBlock;
  const Cipher: TBytes): TBytes;
function Aes256KeyWrap(const KeyEncryptionKey: TAes256Key;
  const PlainKeyData: TBytes): TBytes;
function Aes256KeyUnwrap(const KeyEncryptionKey: TAes256Key; const WrappedKeyData: TBytes;
  out PlainKeyData: TBytes): Boolean;
procedure AesGenerateRandomBytes(Buffer: Pointer; Count: Integer);

Description

FPdfAes is a dependency-free AES implementation provided so PDFium Delphi Component can encrypt and decrypt PDF streams on Delphi, C++Builder, and Free Pascal without relying on an external crypto library. It is consumed by the PDF AES-256 security handler in FPdfEncrypt and by the GCM mode in FPdfAesGcm

The block primitives operate on fixed 16-byte blocks via expanded round keys. The CBC wrappers handle PKCS#7 padding or no padding over variable-length byte arrays, matching the two stream-encryption modes the PDF specification requires

AesGenerateRandomBytes fills a buffer with cryptographically random bytes used for IVs, salts, and keys

Functions

FunctionDescription
Aes128KeyExpansionExpands a 128-bit key into the 176-byte round-key schedule
Aes128EncryptBlockEncrypts one 16-byte block with an expanded AES-128 key
Aes256KeyExpansionExpands a 256-bit key into the 240-byte round-key schedule
Aes256EncryptBlockEncrypts one 16-byte block with an expanded AES-256 key
Aes256DecryptBlockDecrypts one 16-byte block with an expanded AES-256 key
Aes256CbcEncryptPKCS7Encrypts a byte array in CBC mode with PKCS#7 padding
Aes256CbcEncryptNoPadEncrypts a byte array in CBC mode without padding (input length must be a block multiple)
Aes256CbcDecryptPKCS7Decrypts a CBC ciphertext and strips PKCS#7 padding
Aes256CbcDecryptNoPadDecrypts a CBC ciphertext without removing padding
Aes256KeyWrapWraps key data with AES-256 using RFC 3394 integrity protection
Aes256KeyUnwrapAuthenticates and unwraps RFC 3394 key data, returning no plaintext on integrity failure
AesGenerateRandomBytesFills a buffer with cryptographically random bytes

Types

TypeDescription
TAesBlockOne 16-byte AES data block, the unit of the single-block primitives and the CBC chaining step, and also the IV type of the CBC wrappers
TAes128Key16-byte AES-128 key accepted by Aes128KeyExpansion
TAes128ExpandedKey176-byte AES-128 round-key schedule produced by Aes128KeyExpansion and consumed by Aes128EncryptBlock
TAes256Key32-byte AES-256 key accepted by Aes256KeyExpansion, the CBC wrappers, and the RFC 3394 key-wrap functions
TAes256ExpandedKey240-byte AES-256 round-key schedule covering all 14 rounds, produced by Aes256KeyExpansion and consumed by the block encrypt/decrypt and CBC routines

Constants

ConstantValueDescription
AES_BLOCK_SIZE16AES block size in bytes
AES128_KEY_SIZE16AES-128 key size in bytes
AES128_ROUND_KEY_SIZE176Expanded AES-128 round-key size in bytes
AES256_KEY_SIZE32AES-256 key size in bytes
AES256_ROUND_KEY_SIZE240Expanded AES-256 round-key size in bytes

Remarks

See Also

FPdfAesGcm, FPdfEncrypt