A hash table finds a key by turning it into a slot number and reading that slot. It fits a collection that will be asked many times, when the only question is whether the key is present. It does not fit a single query, a request for the next…
Category: Algorithms
How X25519 multiplies a point on Curve25519
Short answer: X25519 is Diffie-Hellman on Curve25519. The scalar is clamped before the ladder runs: the low three bits of the first byte are cleared, the high bit of the last byte is cleared, and bit 254 is set. The Montgomery ladder multiplies the u-coordinate by that scalar. The…
How Triple DES encrypts with three DES operations
Short answer: Triple DES applies DES three times in encrypt-decrypt-encrypt order. The key is three 8-byte DES keys. Decryption is decrypt with K3, encrypt with K2, and decrypt with K1. When the three keys are equal, the middle decryption cancels one encryption and the map is single DES. What…
How Shamir sharing reconstructs a secret by interpolation
Short answer: Shamir secret sharing splits a secret into points on a polynomial over a prime field. A polynomial of degree 2 is fixed by any 3 points. The fragment uses the field modulo 101 and the polynomial 42 + 7x + 3x^2. The shares at x = 1,…
How SHA-3 absorbs a message into a Keccak sponge
Short answer: SHA3-256 is a Keccak sponge, not a Merkle-Damgard hash. The state is twenty-five 64-bit lanes. SHA3-256 absorbs 136 bytes at a time and pads with the domain byte 0x06, zeros, and a final byte whose high bit is set. Each absorbed block is xored into the state,…
How SHA-256 compresses a padded message
Short answer: SHA-256 maps a message to a 32-byte digest. It pads with a 1 bit, zeros, and the 64-bit length, then compresses each 64-byte block into eight 32-bit words. The initial words are the first 32 bits of the fractional square roots of the first eight primes. The…
How SHA-1 compresses a padded message
Short answer: SHA-1 maps a message to a 20-byte digest. It pads like SHA-256, with a 1 bit, zeros, and the length, then runs eighty rounds on five 32-bit words. The digest of abc is a9993e364706816aba3e25717850c26c9cd0d89d. Distinct messages can share a SHA-1 digest. A collision is practical, so the…
How scrypt stretches a password with a large table
Short answer: scrypt stretches a password by filling a table and walking it at random. PBKDF2-HMAC-SHA-256 expands the password and salt into p blocks. Each block is mixed by ROMix. ROMix stores N successive BlockMix outputs, then walks N more steps, using the last 64 bytes as an index…
How RC4 generates a keystream from a permutation
Short answer: RC4 builds a 256-byte permutation from the key, then walks that permutation to emit a keystream byte. The key schedule adds the next key byte into an index and swaps. The generator increments i, adds S[i] into j, swaps, and emits S[(S[i] + S[j]) mod 256]. The…
How Poly1305 authenticates a message under a one-time key
Short answer: Poly1305 computes a 16-byte tag under a one-time 32-byte key. The first 16 bytes, with selected bits cleared, are the evaluation point r. The last 16 bytes are the pad s. The message is read in 16-byte chunks. Each chunk gains a trailing 0x01 byte and is…
How PBKDF2 stretches a password by repeating HMAC
Short answer: PBKDF2 stretches a password by repeating HMAC. For one block, the first value is HMAC of the password and the salt followed by a block counter. Each later value is HMAC of the password and the previous value. The block is the xor of those values. The…
How OAEP encodes a short message before the RSA exponent
Short answer: OAEP encodes a short message before the RSA public exponent. With SHA-256, a seed of 32 bytes is masked with MGF1, and a data block is masked with MGF1 of the masked seed. The data block is the hash of the empty label, a run of zero…
How MD5 compresses a padded message
Short answer: MD5 maps a message to a 16-byte digest. The words are little-endian. Sixty-four rounds use four Boolean functions, a left rotation, and a constant. The constants are the integer parts of 2^32 times the absolute value of the sine of 1 through 64. The digest of abc…
How HMAC-SHA-256 mixes a key into a hash
Short answer: HMAC-SHA-256 turns a key and a message into a 32-byte tag. A key longer than 64 bytes is hashed first. The key is padded with zeros to 64 bytes. The inner hash is SHA-256 of the key xored with 0x36, followed by the message. The outer hash…
How HKDF extracts and expands a shared secret
Short answer: HKDF-SHA-256 turns a shared secret into key bytes of a chosen length. Extract sets the pseudorandom key to HMAC of the salt and the input keying material. Expand appends HMAC of that key, the previous block, the info string, and a counter, starting from an empty previous…
How GCM authenticates ciphertext with GHASH
Short answer: GCM encrypts with counter mode and authenticates with GHASH. H is the AES encryption of the zero block. For a 12-byte nonce, J0 is the nonce followed by 00000001. Encryption starts at the next counter. GHASH multiplies each ciphertext block by H in GF(2^128). The last field…
How ElGamal encrypts a message as a pair of powers
Short answer: ElGamal encrypts an integer as a pair. The public key is g^x mod p. A ciphertext is (g^k mod p, m * (g^x)^k mod p). Decryption divides the second element by the first element raised to x, which is multiplication by the modular inverse. The fragment uses…
How Ed25519 signs a message on an Edwards curve
Short answer: Ed25519 signs a message on the twisted Edwards curve related to Curve25519. SHA-512 of the 32-byte secret splits into a clamped scalar and a prefix. The public key is the base point times that scalar. The nonce is SHA-512 of the prefix and the message, reduced modulo…
