Technical Article

Why Some AES-256 R6 PDFs Reject the Correct Password

When an AES-256 revision 6 PDF rejects a password you know is correct, the usual culprit is the round counter in the R6 hardened hash (ISO 32000-2 Algorithm 2.B). An encryptor that counts rounds from the wrong base produces files that a standard reader refuses roughly 30% of the time, with FPDF_LoadDocument returning FPDF_ERR_PASSWORD for the right password

The nasty part is not the arithmetic. The nasty part is that the slip is invisible to every test that encrypts a file and then checks it with the same code, and that a single successful round trip proves nothing. This post walks through what Algorithm 2.B actually asks for, where a one-round slip hides, why it only fails some of the time, and how to build a regression test that catches a bug like this before a customer does

What does the R6 hash termination rule actually say?

Algorithm 2.B stops only after at least 64 rounds, and then only when the last byte of the most recent AES output E is less than or equal to the round number minus 32, where the round number is the 1-based count of rounds already executed. After the Nth round you compare against N. Not N − 1, not the index of the next round. That one definition is the entire bug surface

The rest of the algorithm is mechanical. Start with K as the SHA-256 of password, salt and (for the owner key) the 48-byte /U value. Each round builds K1 as password ‖ K ‖ UK repeated 64 times, encrypts it with AES-128-CBC without padding using the first 16 bytes of K as key and the next 16 as IV, takes the first 16 bytes of E as a big-endian 128-bit integer mod 3, and picks SHA-256, SHA-384 or SHA-512 accordingly to produce the next K. The final hash is the first 32 bytes of K. ISO 32000-2 §7.6.4.3 defines it; the data-dependent loop length is deliberate, because it makes the work per password guess unpredictable for a GPU cracker

Here is the correct shape of the loop, written as an illustration rather than a drop-in implementation. BuildK1, AesCbcNoPad128, FirstBlockMod3 and the hash helpers stand in for whatever primitives your codebase already has

// Illustration: Algorithm 2.B loop skeleton (ISO 32000-2, revision 6)
K := Sha256(Password + Salt + UK);
RoundNumber := 0;                 // rounds executed so far
repeat
  Inc(RoundNumber);               // this is round 1, 2, 3 ...
  K1 := BuildK1(Password, K, UK); // (Password || K || UK) repeated 64 times
  E  := AesCbcNoPad128(Copy(K, 0, 16), Copy(K, 16, 16), K1);
  case FirstBlockMod3(E) of       // first 16 bytes of E, big-endian, mod 3
    0: K := Sha256(E);
    1: K := Sha384(E);
    2: K := Sha512(E);
  end;
until (RoundNumber >= 64) and (E[High(E)] <= RoundNumber - 32);
Result := Copy(K, 0, 32);

Read the termination line with the counter in mind. The increment sits at the top of the body, so by the time the until test runs, RoundNumber holds exactly the number of completed rounds, which is what the standard means by the round number

PDFium Component Algorithm 2.B round counter for AES-256 R6 in Delphi: the standard tests RoundNumber >= 64 and E <= RoundNumber - 32, while an off-by-one counter reads executed rounds minus one, making the 64-round minimum 65 and the byte threshold one notch tighter
Algorithm 2.B counts rounds one-based, and an off-by-one counter reads executed minus one, shifting both the 64-round minimum and the byte threshold by exactly one round

Where the off-by-one hides in a Delphi port

The classic form is a counter that starts at −1 with the increment at the top of the loop, so the value tested in the until clause is always the executed round count minus one. Two things go wrong at once: the 64-round minimum becomes 65, and the byte threshold is one notch tighter than the standard says. Both shift the exit point by exactly one round in the cases where it matters

The same slip has several equally plausible disguises. Start at 0 and increment at the bottom of the body, after the test has already been written against the pre-increment value. Use a for loop index that is 0-based and reuse it in the comparison. Translate a C reference whose loop variable is checked before ++. Each of these reads naturally in code review, and each produces a hash that looks perfectly random, because it is: it is just a different, equally valid-looking SHA output that no other reader computes. CalculateR6Hash in FPdfEncrypt.pas carries a comment that pins the 1-based semantics, precisely because the wrong version does not look wrong

Why does an AES-256 R6 PDF fail only some of the time?

The failure is probabilistic because the extra round only changes the result when the terminating byte lands in the one-round window between the correct threshold and the shifted one. In every other case both versions exit on the same round and produce the same K, so the file opens fine. Across encryptions with fresh salts, about 30% of an off-by-one implementation's outputs carry /U, /O, /UE and /OE values that disagree with a standard implementation; the other 70% are byte-for-byte correct

Fresh random salts are drawn for every save, so the same document and the same password succeed on one run and fail on the next. From a support desk that looks like flaky storage, a corrupt upload, or a user mistyping a password. Nobody thinks to blame a loop counter when seven out of ten files open. If your product sees intermittent "wrong password" reports on PDFs your own code encrypted, and only on revision 6, this is the first place to look. Files an off-by-one encryptor has already written still open in that same code, so the repair is to decrypt them there, or go back to the plaintext source, and re-encrypt with a corrected implementation

AES-256 R6 intermittent password failure in Delphi: with fresh random salts on every save, about 30 percent of off-by-one encryptions produce /U, /O, /UE and /OE values that FPDF_LoadDocument rejects, because only a terminating byte landing exactly on the one-round threshold window makes the off-by-one loop exit one round later and compute a different hash
Fresh salts move the terminating byte on every save, so about 30 percent of off-by-one runs land on the one-round threshold and reject the correct password

Revision 5 is immune, since it uses a single SHA-256 pass with no iteration. The ISO/TS 32003 AESV4 path is not: TPdfEncryptionRevision value erR7 reuses Algorithm 2.B for its password hashes, so the same counter rule applies to the GCM files described in AES-256-GCM encryption and PDF MAC integrity with ISO/TS 32003

The self-consistency trap: why round-trip tests pass

A round trip through your own encrypt and your own verify always succeeds, because both sides run the same wrong loop and agree on the same wrong hash. The defect only exists relative to somebody else's implementation, so the judge must be an independent one: the PDFium engine in pdfium.dll, Acrobat, qpdf, or any reader you did not write

This is the uncomfortable general lesson for anyone writing crypto-adjacent document code. Unit tests that compute a hash and compare it to the output of the function under test prove determinism, not correctness. Known-answer vectors from the standard or a reference tool are better, and a cross-implementation round trip is the closest thing to ground truth you get. The PDFium Component is in a lucky position here: encryption is done in Pascal by EncryptDocumentBytes, while decryption on load goes through PDFium's own C++ security handler, so reopening the saved file with TPdf is a genuine independent check rather than a mirror

uses
  SysUtils, PDFium, FPdfEncrypt;

procedure SaveR6(const Source, Target: string);
var
  Pdf: TPdf;
  Opts: TPdfEncryptOptions;
begin
  Pdf := TPdf.Create(nil);
  try
    Pdf.FileName := Source;
    Pdf.Active := True;
    Opts := TPdfEncryptOptions.Default;
    Opts.UserPassword := 'userpw';
    Opts.OwnerPassword := 'ownerpw';
    Opts.Revision := erR6;          // AESV3, Algorithm 2.B password hash
    if not Pdf.SaveAsEncrypted(Target, Opts) then
      raise Exception.Create('SaveAsEncrypted failed');
  finally
    Pdf.Free;
  end;
end;

TPdfEncryptOptions.Default starts at revision 5 with all permissions allowed and metadata encrypted, so set Revision explicitly when you want R6. TPdf.SaveAsEncryptedToStream takes the same options record when the output goes to a TStream instead of a file. Note that the passwords are AnsiString in the options record, which matters if you are passing non-ASCII credentials; normalize and encode them before they reach the record

How do you regression-test a probabilistic encryption bug?

Repeat the cross-implementation round trip enough times that a 30% failure rate cannot slip through. A single run misses an off-by-one counter about 70% of the time. Twenty runs, each with fresh salts, drive the miss rate to about 0.08% (0.7 to the 20th power), which is why the test below loops 20 times instead of once

procedure TEncryptTests.R6_EncryptedDocumentReopensWithUserPassword;
var
  Pdf: TPdf;
  I: Integer;
  Target: string;
begin
  Target := TempOutputPath('encrypt_r6_roundtrip.pdf');
  for I := 1 to 20 do
  begin
    SaveR6(SamplePdf, Target);       // fresh salts on every save
    Pdf := TPdf.Create(nil);
    try
      Pdf.Password := 'userpw';
      Pdf.FileName := Target;
      Pdf.Active := True;            // PDFium verifies /U independently
      AssertTrue('Run ' + IntToStr(I) + ' must reopen', Pdf.Active);
    finally
      Pdf.Free;
    end;
  end;
end;

Pair it with the negative case, a wrong password that must fail, so that a broken loader cannot make the positive test pass vacuously. And if you build with Lazarus, rebuild the library package before running the test project after touching Lib\*.pas; the test build links the precompiled .ppu units of the package, and running against stale object code gives you a reassuring green on a bug you have not actually fixed. Seeding is a related trap worth reviewing in the same pass: FPC Random producing constant encryption keys is another case where the output looks random and is quietly wrong

PDFium Component R6 regression strategy in Delphi: a self-consistent round trip through the same Algorithm 2.B loop always passes, so the regression test saves with TPdf.SaveAsEncrypted at revision erR6 twenty times with fresh salts and reopens each file through pdfium.dll FPDF_LoadDocument, letting PDFium's independent C++ security handler catch a wrong CalculateR6Hash
Twenty cross-implementation round trips against PDFium's own security handler drive the miss rate to about 0.08 percent

Encryption, PDF MAC validation and the rest of the save pipeline ship in the PDFium Component for Delphi and C++Builder, with source and regression tests for both Delphi and Lazarus