Comprehensive Notes on Cryptographic Hash Functions, HMAC, SHA-512, and Digital Signatures

Fundamentals of Cryptographic Hash Functions

  • Definition: A cryptographic hash function is a mathematical algorithm that transforms input data into a fixed-length sequence of characters, known as a hash value or message digest.

  • The primary formula for the operation is represented as:   Hash=H(message)\text{Hash} = H(\text{message})

  • Core Operational Characteristics:

    • Input: Can be data of any size, including text, files, or passwords.

    • Output: Always a fixed size (for example, 256 bits or 512 bits).

    • Speed: The function is intended to be fast to compute.

    • Deterministic: The hash function consistently computes the same hash for the exact same input. Any alteration to the input leads to a totally unique hash.

    • One-Way Computation (Irreversibility): The algorithm is designed to be irreversible, meaning it is computationally impossible to recover the original input data from its hash value.

    • Avalanche Effect: A slight variation in the input yields a complete and significantly different hash value.

    • Collision Resistance: Functions are designed to minimize the probability of distinct inputs generating the same hash value. It is very unlikely for two different inputs to produce the same hash.

  • Working Modules:

    • Input Processing: The function can process data of any length.

    • Fixed Output Size Generation: The function generates a fixed output size, providing equality regardless of the input size.

Comparison of Integrity and Authentication: MDC and MAC

  • Modification Detection Code (MDC):

    • Concept: Based on message integrity to determine whether the message has been modified or not.

    • Also known as Message Hash Function or Message Digest.

    • Workflow: Message A is sent to B after performing the hash. The MDC is shared secretly, either by encrypting it using Symmetric or Public Key Cryptography.

  • Message Authentication Code (MAC):

    • Concept: Used to verify that a message was actually sent by the claimed sender (A).

    • Functionality: MAC performs both the modification detection (integrity) and provide authentication.

    • Workflow: Sender and Receiver share a secret key, similar to Symmetric Cryptography.

    • The formula involved includes the Key (KK) and the Message (MM):     MAC=H(K+M)MAC = H(K + M)

    • Security Risk: If the key size is too small, the system is subjected to brute-force attacks.

Hash-Based Message Authentication Code (HMAC)

  • Definition: HMAC is a specific technique for message authentication that ensures both data integrity and authentication using a hash function and a secret key.

  • HMAC Algorithm Components:

    • Key: Left-padded with zeros to match the block size.

    • ipad (Inner Pad): Defined as the bit pattern 001101100011\,0110 (Hexadecimal: 3636).

    • opad (Outer Pad): Defined as the bit pattern 010111000101\,1100 (Hexadecimal: 5C5C).

    • MM: The message, often divided into blocks (M1,M2,,MmM_1, M_2, \dots, M_m).

  • Numerical Example of HMAC:

    • Definitions: H(a)=a(mod10)H(a) = a \pmod{10} , k=5k = 5, m=7m = 7, ipad=2ipad = 2, opad=3opad = 3.

    • Step 1: Compute kipad=52=7k \oplus ipad = 5 \oplus 2 = 7.

    • Step 2: Compute inner hash: H(7+7)=H(14)=4H(7 + 7) = H(14) = 4 .

    • Step 3: Compute kopad=53=6k \oplus opad = 5 \oplus 3 = 6.

    • Step 4: Final HMAC: H(6+4)=H(10)=0H(6 + 4) = H(10) = 0.

Secure Hash Algorithm-512 (SHA-512) Architecture

  • Overview: SHA-512 is a hacking technique that converts text of arbitrary length into a fixed-size string of 512 bits (64 bytes).

  • Basic Properties:

    • Deterministic.

    • Irreversible.

    • Collision resistant.

    • Avalanche effect.

  • Message Processing and Iteration:

    • The variable-length message is divided into fixed block sizes of 1024 bits.

    • This is an iterative process where each 1024-bit block (Block 1, Block 2, $\dots$, Block N) goes through a compression function (C.F.C.F.).

    • The initial value is 512 bits.

    • The final output is the 512-bit Message Digest.

  • Padding and Length Field:

    • The last block may not contain exactly 1024 bits, necessitating padding.

    • Padding Rule: The original message needs to be padded if necessary, and finally, the length of the original message is attached.

    • The length field is assigned a size of 128 bits. The original length of the message (MM) should be less than 21282^{128} bits.

    • Padding bits (PP) calculation example:

    • Let M=1100M = 1100 bits.

    • M+length=1100+128=1228M + \text{length} = 1100 + 128 = 1228.

    • P=(M+L)(mod1024)=1228(mod1024)=820P = -(M + L) \pmod{1024} = -1228 \pmod{1024} = 820 padding bits.

    • Padding structure: A single "1" followed by 819 zeros (100001000\dots0).

SHA-512 Compression Function and Word Expansion

  • Word Conversion:

    • Each 1024-bit block is converted into 16 words, where each word is 64 bits (1024/16=641024 / 16 = 64).

    • The 512-bit initial values/intermediate states are converted into 8 words labelled A,B,C,D,E,F,G,HA, B, C, D, E, F, G, H (each 64 bits).

  • Word Expansion Logic:

    • 1024 bits are expanded into 80 words (W0W_0 to W79W_{79}).

    • W0W_0 through W15W_{15} are taken directly from the 1024-bit message block.

    • For i=16i = 16 to 7979, words are generated using the formula:     Wi=Wi16+RotShift(Wi15)+Wi7+RotShift(Wi2)W_i = W_{i-16} + RotShift(W_{i-15}) + W_{i-7} + RotShift(W_{i-2})

    • The addition is performed (mod264)\pmod{2^{64}} to ensure the output stays within 64 bits.

  • Rotation and Shift Operations:

    • RotRn(W)RotR_n(W): Perform Right Circular Shift nn times.

    • ShiftLeftn(W)ShiftLeft_n(W): Shift left nn positions (fill with zeros; not circular).

    • Example of RotR1(10111)RotR_1(10111): Becomes 1101111011.

  • Initial Constants (A0,B0,,H0A_0, B_0, \dots, H_0):

    • Generated by taking the square root of the first 8 prime numbers (2,3,5,7,11,13,17,192, 3, 5, 7, 11, 13, 17, 19) and expanding the fractional part to 64 bits.

    • Example values:

    • A0=6A09E667F3BCC90816A_0 = 6A09E667F3BCC908_{16}

    • H0=5BE0CD19137E217916H_0 = 5BE0CD19137E2179_{16}

  • Iterative Rounds:

    • There are 80 rounds (00 to 7979).

    • Each round uses one word WtW_t and one constant KtK_t.

    • KtK_t values are derived from the cube roots of the first 80 prime numbers.

SHA-512 Logic Functions

  • Operations performed in each round involve bitwise logic:

    • Majority(x,y,z)=(x AND y)(x AND z)(y AND z)Majority(x, y, z) = (x \text{ AND } y) \oplus (x \text{ AND } z) \oplus (y \text{ AND } z)

    • Conditional(x,y,z)=(x AND y)((NOT x) AND z)Conditional(x, y, z) = (x \text{ AND } y) \oplus ((\text{NOT } x) \text{ AND } z)

    • Rotate(x)=RotR28(x)RotR34(x)RotR39(x)Rotate(x) = RotR_{28}(x) \oplus RotR_{34}(x) \oplus RotR_{39}(x)

Digital Signature Principles

  • Technology: Based on Asymmetric Key Cryptography.

  • Process:

    • Signing: The sender uses their Private Key to sign the message/digest.

    • Verification: The receiver uses the sender's Public Key to decrypt and verify the signature.

  • Purpose:

    • Authentication: Proof of identity.

    • Non-repudiation: The sender cannot deny signing the message.

  • Implementation:

    • Digital Signatures are typically one-to-one (one signature per file).

    • Signatures are normally applied to the Message Digest (MDMD) rather than the original message (MM). This is because the MDMD is unique and has a fixed length.

    • Non-repudiation can be proven via a Trusted Third Party.

RSA Digital Signature Scheme Example

  • Scenario: Sender signs a message using their private key; receiver verifies with the sender's public key.

  • Example Setup:

    • Choose primes: P=3,q=11P = 3, q = 11.

    • Compute n=P×q=33n = P \times q = 33.

    • Compute Euler's totient: Φ(n)=(P1)(q1)=(2)(10)=20\Phi(n) = (P-1)(q-1) = (2)(10) = 20.

    • Choose public exponent ee such that gcd(e,20)=1gcd(e, 20) = 1. Let e=3e = 3.

    • Find private exponent dd such that d×e1(mod20)d \times e \equiv 1 \pmod{20}.

    • 3d1(mod20)3d \equiv 1 \pmod{20}; since 3×7=211(mod20)3 \times 7 = 21 \equiv 1 \pmod{20}, then d=7d = 7.

    • Keys:

    • Public Key: (e=3,n=33)(e=3, n=33)

    • Private Key: (d=7,n=33)(d=7, n=33)

  • Signing Process:

    • Message M=4M = 4.

    • Assume hash H(M)=4H(M) = 4.

    • Signature S=Md(modn)=47(mod33)S = M^d \pmod{n} = 4^7 \pmod{33}.

    • Calculation: 47=163844^7 = 16384. 16384/33=49616384 / 33 = 496 with remainder 1616. So S=16S = 16.

  • Verification Process:

    • Receiver receives M=4M = 4 and S=16S = 16.

    • Receiver calculates expected hash: H(M)=4H(M) = 4.

    • Receiver verifies signature: Se(modn)=163(mod33)S^e \pmod{n} = 16^3 \pmod{33}.

    • Calculation: 163=409616^3 = 4096. 4096/33=1244096 / 33 = 124 with remainder 44.

    • Comparison: Since 4=44 = 4, the signature is valid.