Skip to content

Latest commit

 

History

History
42 lines (37 loc) · 3.04 KB

coding-rules.md

File metadata and controls

42 lines (37 loc) · 3.04 KB

Coding rules

The code follows a set of house rules for Delphi. The cryptographic code departs from a few of them on purpose; this file lists those exceptions and the rule each one departs from.

Exceptions

  • var parameters for state that changes in place. The inner loops of AES (Crypto4D.Aes), GHASH (Crypto4D.Ghash), Argon2, BLAKE2b and bcrypt pass their state, a block or a pair of words as a var parameter and change it where it is. Returning a copy of up to 1 KiB per call costs too much in these loops. Crypto4D.Pbkdf2 passes a THashSHA2 record as var to overwrite it with zeros. The Store routines of TByteOrder and TArgon2.PutLengthPrefixed write into the byte array they are given, so that a buffer that holds a secret is filled in place and not copied. Departs from: never use var parameters.
  • Names from the specification. Code that follows a specification or a reference implementation keeps its names, so that it can be compared with the source line by line: the sixteen words A up to P and Mix(A, B, C, D) of Argon2 and BLAKE2b, the schedule index S of BLAKE2b, the names of BearSSL in the AES translation (Q and R in MixColumns), and X0, Y14, T27 and so on in the S-box circuit of Boyar and Peralta. Departs from: descriptive role names.
  • Global external declarations. BCryptGenRandom on Windows and getrandom on Linux are declared as global functions in the implementation section of Crypto4D.Random. An external routine cannot be a class method. Departs from: no global functions.
  • Signatures set by the RTL. The memory manager hooks of the test helper TFreedMemoryScanner take value parameters, because TMemoryManagerEx prescribes the signature. Departs from: const for all parameters.
  • Documentation comments. Public types and routines have /// XML documentation, which the IDE shows as help, and src\Crypto4D.Aes.pas starts with the copyright and permission notice of BearSSL. There are no other comments. Departs from: no comments.
  • No Spring4D. The library depends on the RTL only. Collections are TBytes and TArray<T>, and their length is the count. Departs from: use Spring4D collections.
  • An options record with public fields. TPasswordHashLimits is a record whose fields a caller changes after starting from ForStoredHashes or ForMessages; it has no rules of its own to protect. A record that does have rules, such as TArgon2Parameters or TRsaPublicKey, keeps its fields private, checks them in Create and offers them as read-only properties. Departs from: private fields with properties.

Arithmetic that wraps

The ARX functions of Argon2, BLAKE2b and bcrypt and the masks of TUnpaddedBase64 add and subtract modulo 2^32 or 2^64. These routines sit between {$I Crypto4D.OverflowOff.inc} and {$I Crypto4D.OverflowRestore.inc}, which switch overflow checking off and back on only when it was on. So that an inline routine is also compiled without the check where it is expanded, its callers sit between the same two lines.