<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Dmytro’s Substack]]></title><description><![CDATA[Deconstructing core technologies to first principles - and rebuilding them from scratch]]></description><link>https://www.dmytrohuz.com</link><image><url>https://substackcdn.com/image/fetch/$s_!t_-c!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png</url><title>Dmytro’s Substack</title><link>https://www.dmytrohuz.com</link></image><generator>Substack</generator><lastBuildDate>Mon, 20 Apr 2026 08:49:12 GMT</lastBuildDate><atom:link href="https://www.dmytrohuz.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Dmytro Huz]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[dmytrohuz@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[dmytrohuz@substack.com]]></itunes:email><itunes:name><![CDATA[Dmytro Huz]]></itunes:name></itunes:owner><itunes:author><![CDATA[Dmytro Huz]]></itunes:author><googleplay:owner><![CDATA[dmytrohuz@substack.com]]></googleplay:owner><googleplay:email><![CDATA[dmytrohuz@substack.com]]></googleplay:email><googleplay:author><![CDATA[Dmytro Huz]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Rebuilding TLS, Part 3 — Building Our First Handshake]]></title><description><![CDATA[We get rid of the pre-shared key assumption, build a simple key exchange handshake, and discover why key agreement alone still does not give us real TLS.]]></description><link>https://www.dmytrohuz.com/p/rebuilding-tls-part-3-building-our</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/rebuilding-tls-part-3-building-our</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Sun, 19 Apr 2026 16:38:45 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!Gn-u!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fb29e52-36ee-433b-a83f-355dc7736265_1536x600.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Gn-u!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fb29e52-36ee-433b-a83f-355dc7736265_1536x600.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Gn-u!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fb29e52-36ee-433b-a83f-355dc7736265_1536x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Gn-u!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fb29e52-36ee-433b-a83f-355dc7736265_1536x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Gn-u!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fb29e52-36ee-433b-a83f-355dc7736265_1536x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Gn-u!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fb29e52-36ee-433b-a83f-355dc7736265_1536x600.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Gn-u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fb29e52-36ee-433b-a83f-355dc7736265_1536x600.jpeg" width="1456" height="569" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4fb29e52-36ee-433b-a83f-355dc7736265_1536x600.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:569,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:250820,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/194707545?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fb29e52-36ee-433b-a83f-355dc7736265_1536x600.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Gn-u!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fb29e52-36ee-433b-a83f-355dc7736265_1536x600.jpeg 424w, https://substackcdn.com/image/fetch/$s_!Gn-u!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fb29e52-36ee-433b-a83f-355dc7736265_1536x600.jpeg 848w, https://substackcdn.com/image/fetch/$s_!Gn-u!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fb29e52-36ee-433b-a83f-355dc7736265_1536x600.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!Gn-u!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fb29e52-36ee-433b-a83f-355dc7736265_1536x600.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2><strong>Overview: Where we are and What Is Still Missing</strong></h2><p>In the previous part of this series, we made our fake secure channel much less fake.</p><p>We started with the broken encrypted transport from <a href="https://www.dmytrohuz.com/p/rebuilding-tls-part-1-why-encryption">Part 1</a>, added integrity with HMAC, <a href="https://www.dmytrohuz.com/p/rebuilding-tls-part-2-adding-integrity">added sequence numbers to make the record layer less naive, and then moved to AEAD</a> &#8212; the approach modern systems usually use to protect records.</p><p>At that point, our protocol could already do something meaningful:</p><ul><li><p>encrypt application data</p></li><li><p>detect tampering</p></li><li><p>reject modified records</p></li><li><p>keep some minimal record-layer state</p></li></ul><p>That was a real step forward.</p><p>But it still relied on one very unrealistic assumption:</p><p><strong>both sides already shared the secret keys</strong></p><p>And that is exactly what we need to remove now.</p><p>Because a real secure protocol cannot stop at protecting data after the keys already exist. It also has to answer one of the harder questions first:</p><p><strong>if client and server do not already share a secret, how can they create one over an insecure network in the first place?</strong></p><p>That is the goal of this part.</p><p>We are going to build the next missing layer of the protocol: the handshake.</p><p>The architecture of this step is simple:</p><pre><code><code>Client                           Server
------                           ------
Handshake messages  &lt;---------&gt;  Handshake messages
       |                               |
       v                               v
  shared secret                  shared secret
       |                               |
       +---------&gt; HKDF &lt;--------------+
                    |
                    v
              session keys
                    |
                    v
         protected application data
</code></code></pre><p>The idea is to let the connection create fresh key material dynamically instead of starting with a hardcoded application key.</p><p>We will implement that in three steps.</p><p>First, we will build a handshake with classic Diffie-Hellman, where the shared prime and base are still explicit and visible in the protocol. Then we will replace that version with X25519 to show how modern protocols simplify the same idea. After that, we will use HKDF to derive proper session keys from the raw shared secret.</p><p>That will take us one big step closer to the shape of real TLS.</p><p>But still not all the way.</p><p>Because even if both sides manage to derive the same fresh session keys, one critical problem will remain: they still do not know who is on the other side.</p><p>And that is where this part is heading.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p><h2><strong>A Very Short Note on Public Key Exchange</strong></h2><p>The basic idea of public key exchange is simple.</p><p>Two sides communicate over an insecure network. They exchange some public information. And from that exchange, both sides derive the same shared secret &#8212; without ever sending that secret directly over the wire.</p><p>That is the key point.</p><p>The network can be fully visible.</p><p>An observer can see all handshake messages.</p><p>But the observer still should not be able to derive the same secret.</p><p>That is exactly the kind of mechanism we need now.</p><p>Until this point in the series, our protocol always started with a secret that already existed. Public key exchange changes that. It gives the connection a way to create fresh shared key material dynamically.</p><p>In this article, I do not want to go deep into the mathematics behind it. I only want to use the core idea as the next building block of the protocol.</p><p>If you want the deeper intuition behind why this works, I already wrote about it here:</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;dea52c91-5c61-4a96-88ff-8813525b2584&quot;,&quot;caption&quot;:&quot;I started my deep dive into cryptography six months ago. I wanted to deconstruct its internals into basic building blocks and then build them back up again. One simple idea kept pulling me forward&#8212;fascinating me and motivating me to go deeper: how can a crowd of absolute strangers&#8212;over the internet, an inherently insecure medium&#8212;exchange information sec&#8230;&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;The Aha-Moment of Public-Key Encryption&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder - I deconstruct complex systems to first principles and rebuild them into clear engineering mental models, diagrams, and practical tools.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a4a14e6b-5f68-4257-9ee7-e33b8864d56a_1024x1024.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2026-02-13T12:29:49.434Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!uy8G!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa57299a6-5758-4c3a-bcd3-443638e6a53c_1536x672.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/the-aha-moment-of-public-key-encryption&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:187849238,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:0,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!t_-c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p>For now, the main idea we need is this:</p><ul><li><p>each side contributes its own private value</p></li><li><p>both sides exchange some public values</p></li><li><p>both sides derive the same shared secret</p></li><li><p>that secret can then become the basis for session keys</p></li></ul><p>So let&#8217;s build that first in the most explicit way, with classic Diffie-Hellman where the shared public parameters are still visible in the handshake.</p><h2><strong>Implementation Part 1 &#8212; Our First Handshake with Classic Diffie-Hellman</strong></h2><p>(The whole code can be find here: <a href="https://github.com/DmytroHuzz/rebuilding_tls/tree/main/part_3/v1_classic_dh_handshake">https://github.com/DmytroHuzz/rebuilding_tls/tree/main/part_3/v1_classic_dh_handshake</a> )</p><p>Now let&#8217;s build the first real handshake in the series.</p><p>I want to start with classic Diffie-Hellman, not because this is the final form we want to keep, but because it makes the mechanics of key exchange much more visible.</p><p>In this version, both sides work with the same public parameters:</p><ul><li><p>a prime p</p></li><li><p>a generator g</p></li></ul><p>These values are not secret. In our implementation, the client sends them in the handshake, which makes the whole mechanism more explicit on the wire. That is exactly what I want at this stage. Before we hide the details behind a cleaner modern primitive, I want to make the structure fully visible.</p><p>The actual secret material comes from somewhere else:</p><ul><li><p>the client chooses a private exponent a</p></li><li><p>the server chooses a private exponent b</p></li></ul><p>From those private values, both sides compute public values:</p><ul><li><p>the client computes A = g^a mod p</p></li><li><p>the server computes B = g^b mod p</p></li></ul><p>Then they exchange A and B.</p><p>And this is the key step:</p><ul><li><p>the client computes s = B^a mod p</p></li><li><p>the server computes s = A^b mod p</p></li></ul><p>Both sides end up with the same shared secret, without ever sending that secret directly over the network.</p><p>In diagram form, the handshake looks like this:</p><pre><code><code>Client                                        Server
------                                        ------
choose private a
compute A = g^a mod p

ClientHello(p, g, A)      ---------&gt;

                                              choose private b
                                              compute B = g^b mod p

                          &lt;---------          ServerHello(B)

compute s = B^a mod p                           compute s = A^b mod p
</code></code></pre><p>That is our first real handshake.</p><p>Until now, the protocol always started with a secret key that already existed.</p><p>Now the connection itself creates the secret.</p><p>That is a major shift.</p><h3><strong>The raw Diffie-Hellman math</strong></h3><p>At the lowest level, the core operations are very small. That is one of the nice things about starting with classic Diffie-Hellman: the whole idea is still visible in a few functions.</p><pre><code><code>
# RFC 3526 Group 14: 2048-bit MODP prime
DH_PRIME = int(
    "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1"
    "29024E088A67CC74020BBEA63B139B22514A08798E3404DD"
    "EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245"
    "E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED"
    "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D"
    "C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F"
    "83655D23DCA3AD961C62F356208552BB9ED529077096966D"
    "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B"
    "E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9"
    "DE2BCBF6955817183995497CEA956AE515D2261898FA0510"
    "15728E5A8AACAA68FFFFFFFFFFFFFFFF",
    16,
)

DH_GENERATOR = 2

def generate_private_exponent() -&gt; int:
    return int.from_bytes(os.urandom(32), "big")

def compute_public_value(private: int, g: int, p: int) -&gt; int:
    return pow(g, private, p)

def compute_shared_secret(peer_public: int, private: int, p: int) -&gt; int:
    return pow(peer_public, private, p)
</code></code></pre><p>This is the whole core idea in code:</p><ul><li><p>private exponent stays local</p></li><li><p>public value goes on the wire</p></li><li><p>shared secret is derived independently on both sides</p></li></ul><p>That is the heart of Diffie-Hellman.</p><h3><strong>Client side</strong></h3><pre><code><code>def client_handshake(sock) -&gt; bytes:
    """Perform the client side of the classic DH handshake.

    The client picks the public parameters (p, g) and sends them to the
    server along with its own public DH value.  The server uses those
    parameters to compute its own public value and sends it back.

    Returns the shared secret as bytes.
    """
    # The client chooses p and g.  These are PUBLIC &#8212; not secret.
    # Anyone on the wire can see them, and that is perfectly fine.
    # The security of DH depends on the hardness of the discrete
    # logarithm problem, not on hiding p and g.
    p = DH_PRIME
    g = DH_GENERATOR

    print(f"  Public parameters (chosen by client, sent to server):")
    print(f"    p = {str(p)[:40]}... ({p.bit_length()} bits)")
    print(f"    g = {g}")

    # Step 1: Generate client's private exponent and public value.
    # The private exponent is the ONE thing that stays secret.
    client_private = generate_private_exponent()
    client_public = compute_public_value(client_private, g, p)
    client_public_bytes = int_to_bytes(client_public)

    # Step 2: Send ClientHello with p, g, and our public value.
    # All three are public.  The private exponent is NOT included.
    p_bytes = int_to_bytes(p)
    g_bytes = int_to_bytes(g)

    client_hello = encode_message(
        [
            (TAG_DH_P, p_bytes),
            (TAG_DH_G, g_bytes),
            (TAG_DH_PUBLIC, client_public_bytes),
        ]
    )
    # Step 3: send p, g, and the client&#8217;s public value inside ClientHello
    send_record(sock, client_hello)

    # Step 4: Receive ServerHello with the server's public value.
    server_hello_raw = recv_record(sock)
    fields = decode_message(server_hello_raw)
    server_public_bytes = None
    for tag, value in fields:
        if tag == TAG_DH_PUBLIC:
            server_public_bytes = value
    if server_public_bytes is None:
        raise ValueError("ServerHello missing DH public value")

    server_public = bytes_to_int(server_public_bytes)
    print(f"  &lt;- Received ServerHello")
    print(f"  Server public value B:   {hex_preview(server_public_bytes)}")

    # Step 5: Compute the shared secret.
    # shared = B^a mod p = (g^b)^a mod p = g^(ab) mod p
    shared_int = compute_shared_secret(server_public, client_private, p)
    shared_bytes = int_to_bytes(shared_int)

    return shared_bytes
</code></code></pre><p>On the client side, the flow is:</p><ol><li><p>choose a private exponent</p></li><li><p>compute the public value</p></li><li><p>send p, g, and the client&#8217;s public value inside ClientHello</p></li><li><p>receive the server&#8217;s public value</p></li><li><p>derive the shared secret</p></li></ol><p>That is the first point in the series where the client does not begin with the application key. It participates in creating it.</p><h3><strong>Server side</strong></h3><pre><code><code>def server_handshake(sock) -&gt; bytes:
    """Perform the server side of the classic DH handshake.

    The server receives p, g, and client_public from the ClientHello,
    uses those parameters to generate its own keypair, and sends its
    public value back.

    Returns the shared secret as bytes.
    """
    # Step 1: Receive ClientHello &#8212; parse p, g, and client's public value.
    # The server does NOT assume any particular p or g.  It uses whatever
    # the client proposes.  (In a production system, the server would
    # validate that p is a safe prime and g is a proper generator.
    # We skip that here for clarity.)
    client_hello_raw = recv_record(sock)
    fields = decode_message(client_hello_raw)

    p_bytes = None
    g_bytes = None
    client_public_bytes = None
    for tag, value in fields:
        if tag == TAG_DH_P:
            p_bytes = value
        elif tag == TAG_DH_G:
            g_bytes = value
        elif tag == TAG_DH_PUBLIC:
            client_public_bytes = value

    if p_bytes is None:
        raise ValueError("ClientHello missing DH prime (p)")
    if g_bytes is None:
        raise ValueError("ClientHello missing DH generator (g)")
    if client_public_bytes is None:
        raise ValueError("ClientHello missing DH public value (A)")

    # Deserialize the parameters from bytes.
    p = bytes_to_int(p_bytes)
    g = bytes_to_int(g_bytes)
    client_public = bytes_to_int(client_public_bytes)

    # Step 2: Generate server's private exponent and public value
    # using the p and g received from the client.
    server_private = generate_private_exponent()
    
    # Step 3: Compute server's public value
    server_public = compute_public_value(server_private, g, p)
    server_public_bytes = int_to_bytes(server_public)

    # Step 4: Send ServerHello with our public value.
    # Only B is sent &#8212; p and g are already known from the ClientHello.
    server_hello = encode_message(
        [
            (TAG_DH_PUBLIC, server_public_bytes),
        ]
    )
    send_record(sock, server_hello)

    # Step 5: Compute the shared secret.
    # shared = A^b mod p = (g^a)^b mod p = g^(ab) mod p
    shared_int = compute_shared_secret(client_public, server_private, p)
    shared_bytes = int_to_bytes(shared_int)

    return shared_bytes

</code></code></pre><p>The server does the mirror image:</p><ol><li><p>receive p, g, and the client&#8217;s public value</p></li><li><p>choose its own private exponent</p></li><li><p>compute its own public value</p></li><li><p>send that value back in ServerHello</p></li><li><p>derive the same shared secret from the client&#8217;s public value</p></li></ol><p>So at the end of the handshake, both sides have the same secret &#8212; but that secret was never transmitted directly.</p><p>That is the big win.</p><p>After this step, the connection can create fresh shared key material dynamically.</p><p>That is a much more realistic foundation.</p><p>But it is also still awkward.</p><p>Not conceptually awkward &#8212; educationally this version is very useful &#8212; but operationally awkward. We now have explicit p and g in the handshake, which is nice for understanding the mechanism, but clunky for a modern protocol design.</p><p>That is exactly why the next step will replace this version with X25519.</p><h2><strong>Implementation Part 2 &#8212; Simplifying the Handshake with X25519</strong></h2><p>(The whole code can be find here: https://github.com/DmytroHuzz/rebuilding_tls/tree/main/part_3/v2_x25519_handshake )</p><p>The classic Diffie-Hellman version was useful because it made the mechanics of the handshake fully visible.</p><p>But it also makes something else visible:</p><p>it is a bit clunky.</p><p>Not conceptually clunky &#8212; educationally it is great &#8212; but operationally clunky. There are more moving parts in the handshake, more explicit protocol fields, and more visible math than modern protocols usually want to expose directly.</p><p>So now we keep the same core idea and simplify the workflow.</p><p>That is where <strong>X25519</strong> comes in.</p><p>The conceptual goal stays exactly the same:</p><ul><li><p>both sides generate ephemeral private/public key pairs</p></li><li><p>both sides exchange public keys</p></li><li><p>both sides derive the same shared secret</p></li><li><p>that secret will later become the basis for session keys</p></li></ul><p>What changes is the <em>shape</em> of the handshake.</p><p>We no longer need to carry an explicit prime and generator through the protocol. We no longer manually perform modular exponentiation with visible p and g. X25519 gives us the same public-key exchange idea in a much cleaner modern form.</p><p>That is why I wanted this section right after the classic DH version.</p><p>Classic DH makes the mechanism visible.</p><p>X25519 shows what the modern streamlined version looks like.</p><h3><strong>Client-side handshake structure</strong></h3><p>Here is the current client handshake implementation:</p><pre><code><code>def client_handshake(sock) -&gt; bytes:
    """Perform the client side of the X25519 handshake.

    Returns the 32-byte shared secret.
    """
    print("\\n[handshake] Client: starting X25519 handshake")

    # Step 1: Generate an ephemeral X25519 keypair.
    # "Ephemeral" means we create a fresh keypair for this session only.
    # The private key never leaves this process and is discarded after use.
    client_private = X25519PrivateKey.generate()
    client_public = client_private.public_key()
    client_public_bytes = client_public.public_bytes(Encoding.Raw, PublicFormat.Raw)

    # Step 2: Send ClientHello with our public key.
    client_hello = encode_message(
        [
            (TAG_X25519_PUBLIC, client_public_bytes),
        ]
    )
    send_record(sock, client_hello)

    # Step 3: Receive ServerHello with the server's public key.
    server_hello_raw = recv_record(sock)
    fields = decode_message(server_hello_raw)
    server_public_bytes = None
    for tag, value in fields:
        if tag == TAG_X25519_PUBLIC:
            server_public_bytes = value
    if server_public_bytes is None:
        raise ValueError("ServerHello missing X25519 public key")

    # Deserialize the server's public key from raw bytes.
    server_public = X25519PublicKey.from_public_bytes(server_public_bytes)

    # Step 4: Compute the shared secret.
    # X25519(client_private, server_public) = X25519(server_private, client_public)
    # This is the elliptic-curve equivalent of g^(ab) mod p from v1.
    shared_secret = client_private.exchange(server_public)

    return shared_secret
</code></code></pre><p>I like this version because it makes the transition very clear.</p><p>The client code no longer has to think about p and g at all. It just performs the handshake, gets the shared secret, and prints it. That is exactly the point of this stage in the series: the workflow becomes smaller, but the underlying purpose stays the same.</p><h3><strong>What changed conceptually</strong></h3><p>Compared to the classic DH version, the protocol has become simpler in three important ways.</p><h3><strong>1. No explicit shared public parameters in the handshake</strong></h3><p>In the previous version, the client sent the prime and generator so the whole structure of classic Diffie-Hellman stayed visible.</p><p>Now that goes away.</p><p>X25519 already gives us a fixed, standard structure for the exchange, so the handshake only needs to carry the public key material.</p><p>That makes the protocol smaller and cleaner.</p><h3><strong>2. The public values are much more compact</strong></h3><p>In the classic DH version, the public values were tied to a large prime-field construction and looked much heavier in the protocol.</p><p>In this version, the public keys are just 32 bytes.</p><p>That is a huge practical simplification.</p><h3><strong>3. The code starts to look more like real modern protocol code</strong></h3><p>This line from the comments says it well:</p><blockquote><p>generate(), exchange(), done.</p></blockquote><p>That is exactly the feeling this section should create.</p><p>We are still doing public-key exchange.</p><p>We are still deriving a shared secret.</p><p>But the implementation shape is now much closer to what modern systems actually use.</p><h3><strong>What this version still does not solve</strong></h3><p>Even after switching to X25519, this version is still simplified:</p><ul><li><p>there is still <strong>no authentication</strong></p></li><li><p>the shared secret is <strong>not yet turned into session keys</strong></p></li><li><p>there is still <strong>no record-layer encryption using the new keys</strong></p></li></ul><p>In the next step, we will add <strong>HKDF</strong> and derive proper working session keys from it.</p><p>That is where the handshake starts to connect back to the record protection we built earlier.</p><h2><strong>Implementation Part 3 &#8212; Deriving Session Keys with HKDF</strong></h2><p>(The whole code can be find here: https://github.com/DmytroHuzz/rebuilding_tls/tree/main/part_3/v3_hkdf_session_keys)</p><p>At this point, both the classic Diffie-Hellman version and the X25519 version give us the same kind of output:</p><p>a shared secret that both sides can compute independently.</p><p>That is already a big step forward compared to the pre-shared-key model from the previous parts. The connection can now create fresh key material dynamically instead of starting with one hardcoded application key.</p><p>But there is still one important design question left:</p><p><strong>should we use that raw shared secret directly as the application key?</strong></p><p>For a toy demo, we probably could.</p><p>But even here, that would be the wrong direction.</p><p>Because a cleaner protocol separates these two ideas:</p><ul><li><p>the handshake creates a shared secret</p></li><li><p>the protocol derives working session keys from that secret</p></li></ul><p>That is exactly where <strong>HKDF</strong> comes in.</p><p>HKDF is a key-derivation function. Its job is not to invent secrecy out of nowhere, but to take existing secret material and turn it into keys that are better structured and easier to use safely inside the protocol.</p><p>So instead of treating the X25519 output as &#8220;the AES key,&#8221; we will use HKDF to derive proper session keys from it.</p><p>That already makes the protocol feel much closer to real TLS.</p><h3><strong>What changes conceptually</strong></h3><p>The structure now becomes:</p><pre><code><code>X25519 shared secret
        |
        v
      HKDF
        |
        v
  session key material
        |
        v
 protected application data
</code></code></pre><p>This is an important shift.</p><p>Before this step, the handshake produced something secret and we could have stopped there.</p><p>After this step, the handshake produces an <em>input</em> to a key schedule.</p><p>That is a much better protocol design.</p><h3><strong>Why this matters</strong></h3><p>There are two main reasons to do this.</p><h3><strong>1. The raw shared secret is handshake output, not final protocol state</strong></h3><p>The shared secret is the result of key exchange. That does not automatically mean it should be used directly as the application-data key.</p><p>Protocols usually want a cleaner boundary:</p><ul><li><p>handshake result first</p></li><li><p>working keys second</p></li></ul><h3><strong>2. We can derive keys for different purposes</strong></h3><p>Once we introduce a key-derivation step, we are no longer forced into &#8220;one secret for everything.&#8221;</p><p>Even in this toy protocol, that opens the door to a much more realistic design.</p><p>For example, instead of one single AEAD key, we can derive:</p><ul><li><p>client &#8594; server key</p></li><li><p>server &#8594; client key</p></li></ul><p>That is already much closer to how real secure protocols think.</p><div><hr></div><h3><strong>Deriving the keys</strong></h3><p>In the current implementation, HKDF takes the X25519 shared secret and stretches it into 64 bytes of key material.</p><p>Then that material is split into two 32-byte keys:</p><ul><li><p>one for traffic from client to server</p></li><li><p>one for traffic from server to client</p></li></ul><p>That gives us directional keys instead of one shared application key for both directions.</p><p>Here is the key schedule:</p><pre><code><code># key_schedule_x25519.py
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.hkdf import HKDF

def derive_session_keys(shared_secret: bytes) -&gt; tuple[bytes, bytes]:
    key_material = HKDF(
        algorithm=hashes.SHA256(),
        length=64,
        salt=None,
        info=b"toy-tls-part-3-x25519",
    ).derive(shared_secret)

    client_to_server_key = key_material[:32]
    server_to_client_key = key_material[32:]

    return client_to_server_key, server_to_client_key
</code></code></pre><p>I like this step a lot because it is small in code, but it changes the protocol mindset in an important way.</p><p>We are no longer thinking:</p><blockquote><p>handshake gives us the key</p></blockquote><p>We are now thinking:</p><blockquote><p>handshake gives us secret material, and the protocol derives the keys it actually wants to use</p></blockquote><p>That is a much stronger model.</p><h3><strong>A small but important detail</strong></h3><p>Notice that the two sides must interpret the derived keys consistently.</p><p>If the client treats the first 32 bytes as the client &#8594; server key, then the server must do the same. Otherwise the channel will immediately break.</p><p>So now the handshake is not only producing shared secret material. It is also establishing a shared rule for how that material becomes working traffic keys.</p><p>That is another reason protocols need structure, not just primitives.</p><div><hr></div><h2><strong>Connecting HKDF back to the record layer</strong></h2><p>Now we can finally connect this part back to what we built earlier.</p><p>In Part 2, we already built an AEAD-protected record layer. But that record layer still depended on hardcoded keys.</p><p>Now that changes.</p><p>The AEAD layer no longer starts with a static key from configuration.</p><p>It receives fresh traffic keys from the handshake.</p><p>So the protocol shape becomes:</p><pre><code><code>Handshake -&gt; X25519 shared secret -&gt; HKDF -&gt; directional session keys -&gt; AEAD protected records
</code></code></pre><p>That is a major milestone in the series.</p><p>At this point, the protocol no longer just looks secure because we wrapped some bytes in encryption. It now has a real high-level structure:</p><ul><li><p>first establish shared key material</p></li><li><p>then derive traffic keys</p></li><li><p>then use those keys to protect application data</p></li></ul><p>That is already much closer to the shape of real TLS.</p><div><hr></div><h2><strong>Using the new session keys</strong></h2><p>Once the keys are derived, the record layer can use them directly.</p><p>Conceptually, the flow now looks like this:</p><h3><strong>Client</strong></h3><pre><code><code>with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
    client.connect((HOST, PORT))
    print(f"Connected to {HOST}:{PORT}")

    # ==========================================
    # PHASE 1: HANDSHAKE
    # ==========================================
    # New in Part 3: the handshake dynamically establishes session keys.
    # No pre-shared secret needed.
    client_write_key, server_write_key = client_handshake(client)

    # ==========================================
    # PHASE 2: APPLICATION DATA
    # ==========================================
    # The record layer now uses HKDF-derived keys instead of hardcoded ones.
    # The record format is the same as Part 2 Stage 3 (AEAD).

    # --- Send request (encrypted with client_write_key) ---
    protected = protect_record(client_write_key, send_seq, request)
    send_record(client, protected)
    send_seq += 1

    # --- Receive response (decrypted with server_write_key) ---
    raw_response = recv_record(client)

    try:
        response = unprotect_record(server_write_key, recv_seq, raw_response)
        recv_seq += 1
        print(f"\\n  Decrypted response:\\n  {response.decode('utf-8')}")
    except Exception as e:
        print(f"\\n  *** REJECTED: {e} ***")

print("\\nDone.")
</code></code></pre><ul><li><p>use client_write_key to protect outgoing application data</p></li><li><p>use server_write_key to unprotect incoming application data</p></li></ul><h3><strong>Server</strong></h3><pre><code><code>with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server.bind((HOST, PORT))
    server.listen(1)
    print(f"Listening on {HOST}:{PORT}")

    conn, addr = server.accept()
    with conn:
        # ==========================================
        # PHASE 1: HANDSHAKE
        # ==========================================
        client_write_key, server_write_key = server_handshake(conn)

        # ==========================================
        # PHASE 2: APPLICATION DATA
        # ==========================================

        # --- Receive request (decrypted with client_write_key) ---
        raw_request = recv_record(conn)

        try:
            request = unprotect_record(client_write_key, recv_seq, raw_request)
            recv_seq += 1
        except Exception as e:
            print(f"\\n  *** REJECTED: {e} ***")
            print("  Connection closed &#8212; refusing to process invalid data.")
        else:

            # --- Send response (encrypted with server_write_key) ---
            response = (
                "HTTP/1.1 200 OK\\r\\n"
                "Content-Type: text/plain\\r\\n"
                "Content-Length: 13\\r\\n\\r\\n"
                "hello, client"
            ).encode("utf-8")

            protected = protect_record(server_write_key, send_seq, response)
            send_record(conn, protected)
            send_seq += 1

print("\\nDone.")

</code></code></pre><ul><li><p>use client_write_key to unprotect incoming client traffic</p></li><li><p>use server_write_key to protect outgoing server traffic</p></li></ul><p>That means the two directions are now separated.</p><p>This is cleaner than one symmetric application key shared blindly by both directions, and it makes the protocol feel more deliberate.</p><p>Even in this simplified version, that is a meaningful step.</p><div><hr></div><h2><strong>What this step really gave us</strong></h2><p>By adding HKDF, we improved the protocol in a way that is easy to underestimate.</p><p>We did not just &#8220;derive another key.&#8221;</p><p>We made the protocol architecture cleaner.</p><p>Now the handshake and the traffic layer are connected in a more principled way:</p><ul><li><p>the handshake creates shared secret material</p></li><li><p>the key schedule turns that material into working keys</p></li><li><p>the record layer consumes those keys</p></li></ul><p>This is a much better model than treating the raw X25519 result as the final answer.</p><p>And it brings us one step closer to real TLS, where key derivation is not an optional detail, but one of the central pieces of the protocol design.</p><div><hr></div><h2><strong>But we are still not secure</strong></h2><p>And now we arrive at the uncomfortable but necessary part.</p><p>Even with:</p><ul><li><p>a real handshake</p></li><li><p>X25519</p></li><li><p>HKDF</p></li><li><p>fresh directional session keys</p></li><li><p>AEAD-protected records</p></li></ul><p>the protocol still cannot be considered secure enough.</p><p>Why?</p><p>Because all of this still says nothing about <strong>who</strong> is on the other side.</p><p>The handshake can successfully create shared secrets.</p><p>HKDF can successfully derive traffic keys.</p><p>The record layer can successfully protect application data.</p><p>And an attacker can still sit in the middle and run two separate handshakes.</p><p>That is the next lesson.</p><div><hr></div><h2><strong>Still Not Secure &#8212; The Man-in-the-Middle Problem</strong></h2><p>At this point, our protocol already looks much more serious than the one we started with.</p><p>We now have:</p><ul><li><p>a real handshake</p></li><li><p>fresh shared secrets</p></li><li><p>X25519 instead of a pre-shared application key</p></li><li><p>HKDF-derived session keys</p></li><li><p>AEAD-protected application records</p></li></ul><p>That is a long way from the fake secure channel in Part 1.</p><p>But it is still not enough.</p><p>The missing piece is one of the most important ideas in this whole series:</p><p><strong>key exchange is not authentication</strong></p><p>That sentence is easy to read quickly and move on from. But it is worth stopping here, because this is exactly where many protocols fail.</p><p>Our handshake proves that both sides can derive the same shared secret.</p><p>What it does <strong>not</strong> prove is:</p><p><strong>who</strong> is actually on the other side.</p><p>And that difference is the whole problem.</p><h3><strong>The attack</strong></h3><p>Imagine an active attacker sitting between the client and the server.</p><p>Let&#8217;s call her Mallory.</p><p>The client thinks it is talking to the server.</p><p>The server thinks it is talking to the client.</p><p>But Mallory intercepts the handshake and replaces the exchanged public keys with her own.</p><p>In simplified form, the flow looks like this:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uLbh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed534692-c29b-4fb3-8154-428e9f5cf001_2525x2776.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uLbh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed534692-c29b-4fb3-8154-428e9f5cf001_2525x2776.png 424w, https://substackcdn.com/image/fetch/$s_!uLbh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed534692-c29b-4fb3-8154-428e9f5cf001_2525x2776.png 848w, https://substackcdn.com/image/fetch/$s_!uLbh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed534692-c29b-4fb3-8154-428e9f5cf001_2525x2776.png 1272w, https://substackcdn.com/image/fetch/$s_!uLbh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed534692-c29b-4fb3-8154-428e9f5cf001_2525x2776.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uLbh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed534692-c29b-4fb3-8154-428e9f5cf001_2525x2776.png" width="1456" height="1601" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ed534692-c29b-4fb3-8154-428e9f5cf001_2525x2776.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1601,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:556263,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/194707545?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed534692-c29b-4fb3-8154-428e9f5cf001_2525x2776.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uLbh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed534692-c29b-4fb3-8154-428e9f5cf001_2525x2776.png 424w, https://substackcdn.com/image/fetch/$s_!uLbh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed534692-c29b-4fb3-8154-428e9f5cf001_2525x2776.png 848w, https://substackcdn.com/image/fetch/$s_!uLbh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed534692-c29b-4fb3-8154-428e9f5cf001_2525x2776.png 1272w, https://substackcdn.com/image/fetch/$s_!uLbh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fed534692-c29b-4fb3-8154-428e9f5cf001_2525x2776.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>And now something very important happens.</p><p>The handshake still &#8220;works.&#8221;</p><p>But it works in the wrong way.</p><ul><li><p>the <strong>client</strong> ends up with a shared secret with <strong>Mallory</strong></p></li><li><p>the <strong>server</strong> ends up with a different shared secret with <strong>Mallory</strong></p></li><li><p>and <strong>Mallory</strong> now has one valid secure channel to each side</p></li></ul><p>From the point of view of the client and the server, everything looks normal:</p><ul><li><p>key exchange succeeded</p></li><li><p>keys were derived</p></li><li><p>encrypted records verify correctly</p></li><li><p>AEAD tags are valid</p></li></ul><p>And yet the protocol has already failed.</p><p>Because Mallory can now:</p><ol><li><p>decrypt the client&#8217;s traffic</p></li><li><p>read it or modify it</p></li><li><p>re-encrypt it toward the server</p></li><li><p>receive the server&#8217;s response</p></li><li><p>read it or modify it</p></li><li><p>re-encrypt it back toward the client</p></li></ol><p>Neither side can detect this.</p><h3><strong>In The Next Article &#8212; Building the Certificate Infrastructure</strong></h3><p>The handshake only proves one thing:</p><blockquote><p>&#8220;I computed a shared secret with whoever sent me this public key.&#8221;</p></blockquote><p>It does <strong>not</strong> prove:</p><blockquote><p>&#8220;This public key came from the server I actually intended to talk to.&#8221;</p></blockquote><p>That is the missing half.</p><p>To fix this, the client needs a way to verify that the public key it receives during the handshake actually belongs to the server it wanted to talk to.</p><p>That is where the next layer enters:</p><ul><li><p>certificates</p></li><li><p>signatures</p></li><li><p>trust chains</p></li><li><p>certificate authorities</p></li></ul><p>In other words, this is where the protocol must stop proving only that &#8220;someone&#8221; is there and start proving <strong>who</strong> that someone is.</p><p>That is exactly what the next article will build.</p><h3>Summary</h3><p>Our protocol now has secrecy against passive observers.</p><p>It has integrity for protected records.</p><p>It has fresh session keys.</p><p>But it still does not have <strong>identity</strong>.</p><p>And without identity, a correct shared secret with the wrong party is still a protocol failure.</p><p>That is the deeper lesson of Part 3.</p><p>Part 1 taught us:</p><p><strong>confidentiality is not integrity</strong></p><p>Part 2 taught us:</p><p><strong>protecting records is not the same thing as establishing trust</strong></p><p>And now Part 3 adds the next lesson:</p><p><strong>key exchange is not authentication</strong></p><p>That we will solve in the next article!</p><h2><strong>Final Code</strong></h2><p>The full code for this part is available here:</p><p><strong>GitHub:</strong> <a href="https://github.com/DmytroHuzz/rebuilding_tls/tree/main/part_3">https://github.com/DmytroHuzz/rebuilding_tls/tree/main/part_3</a></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[Rebuilding TLS, Part 2 — Adding Integrity to the Channel]]></title><description><![CDATA[We teach our protocol to detect tampering, make records less naive with sequence numbers, and then switch to the AEAD style used in real systems.]]></description><link>https://www.dmytrohuz.com/p/rebuilding-tls-part-2-adding-integrity</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/rebuilding-tls-part-2-adding-integrity</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Sun, 05 Apr 2026 21:40:43 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!78kv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F680242ba-f7c2-4916-990b-5c813987d655_1536x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!78kv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F680242ba-f7c2-4916-990b-5c813987d655_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!78kv!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F680242ba-f7c2-4916-990b-5c813987d655_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!78kv!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F680242ba-f7c2-4916-990b-5c813987d655_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!78kv!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F680242ba-f7c2-4916-990b-5c813987d655_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!78kv!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F680242ba-f7c2-4916-990b-5c813987d655_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!78kv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F680242ba-f7c2-4916-990b-5c813987d655_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/680242ba-f7c2-4916-990b-5c813987d655_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3423615,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/193281237?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F680242ba-f7c2-4916-990b-5c813987d655_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!78kv!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F680242ba-f7c2-4916-990b-5c813987d655_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!78kv!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F680242ba-f7c2-4916-990b-5c813987d655_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!78kv!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F680242ba-f7c2-4916-990b-5c813987d655_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!78kv!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F680242ba-f7c2-4916-990b-5c813987d655_1536x1024.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In the first part of this series, we built our first fake secure channel:</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;0e21ae64-25bc-4008-8f34-84996112a315&quot;,&quot;caption&quot;:&quot;A year ago I wrote a series about how a web server works.&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Rebuilding TLS, Part 1 &#8212; Why Encryption Alone Is Not Enough&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder - I deconstruct complex systems to first principles and rebuild them into clear engineering mental models, diagrams, and practical tools.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a4a14e6b-5f68-4257-9ee7-e33b8864d56a_1024x1024.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2026-03-29T18:57:36.417Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!5phz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f085090-d20e-4850-844a-c79a878be8e6_1536x1024.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/rebuilding-tls-part-1-why-encryption&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:192533658,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:3,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!t_-c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png&quot;,&quot;belowTheFold&quot;:false,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p>We took a simple socket-based client and server, wrapped their communication in AES-CTR with a shared secret key, and got something that already looked much more serious than plain TCP. The traffic stopped being transparent. A passive observer could no longer read the request and response directly.</p><p>That was real progress.</p><p>But it still had a fatal flaw.</p><p>The receiver had no way to know whether the encrypted message had been changed on the way.</p><p>Encryption hid the bytes.</p><p>It did not protect their meaning.</p><p>So in this part, we will fix that.</p><p>We will first add a <strong>MAC</strong> so the receiver can detect tampering. Then we will make the record layer a little less naive by adding a sequence number. And after that, we will take one more step toward the real world and move to <strong>AEAD</strong>, because that is how modern secure protocols usually protect records.</p><p>We still will not have real TLS when we are done.</p><p>But we will have a much more serious record layer than the one from Part 1.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2>What we will build in this part</h2><p>The plan for this article is simple:</p><ul><li><p>briefly introduce MACs</p></li><li><p>add HMAC to our encrypted record format</p></li><li><p>make tampering detectable</p></li><li><p>add a sequence number to each record</p></li><li><p>explain why sequence numbers matter</p></li><li><p>then move from our hand-built &#8220;encrypt + MAC&#8221; construction to AEAD, because that is the approach real systems usually use</p></li></ul><p>Just like in Part 1, I want to keep the pattern simple:</p><ul><li><p>explain the idea</p></li><li><p>show the code</p></li><li><p>explain what changed</p></li><li><p>explain what is still broken</p></li></ul><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.dmytrohuz.com/subscribe?"><span>Subscribe now</span></a></p><div><hr></div><h2>Why encryption still was not enough</h2><p>At the end of Part 1, our protocol already had one real property:</p><ul><li><p>confidentiality against passive observers</p></li></ul><p>That mattered.</p><p>But it still failed against active attackers.</p><p>Because AES-CTR by itself does not provide integrity, an attacker could modify ciphertext and the receiver would still decrypt it and trust the result. That was the main lesson of the first article:</p><p><strong>confidentiality is not integrity</strong></p><p>So the next missing property is obvious.</p><p>The receiver needs a way to verify that the message arrived unchanged.</p><p>That is what a MAC gives us.</p><div><hr></div><h2>A very short note on MACs</h2><p>MAC stands for <strong>Message Authentication Code</strong>.</p><p>Very roughly, it is a cryptographic tag computed over a message using a secret key.</p><p>The sender computes the tag and sends it together with the message.</p><p>The receiver recomputes the tag and compares it with the one that was received.</p><p>If the tags match, the receiver can trust that:</p><ul><li><p>the message was not modified</p></li><li><p>and it was created by someone who knows the MAC key</p></li></ul><p>If the tags do not match, the message must be rejected.</p><p>In this article, we will use <strong>HMAC-SHA256</strong>.</p><blockquote><p>I do not want to go too deep into HMAC itself here, because the goal of this series is to understand TLS as a protocol. But if you want a deeper explanation of MACs and HMAC, I already wrote about them in my cryptography series, and I&#8217;ll link that here.</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;f1db8dab-a8e3-4f94-98bd-6e31e78a717d&quot;,&quot;caption&quot;:&quot;In the previous article, we did something slightly ridiculous.&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Building Own MAC &#8212; Part 3: Reinventing HMAC from SHA-256&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder - I deconstruct complex systems to first principles and rebuild them into clear engineering mental models, diagrams, and practical tools.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a4a14e6b-5f68-4257-9ee7-e33b8864d56a_1024x1024.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2026-01-23T18:58:16.766Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!QAi6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41bb1c89-ee6c-4240-9150-0469a12ab722_1536x672.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/building-own-mac-part-3-reinventing&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:185566303,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:3,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!t_-c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div></blockquote><p>So for our purposes, the important idea is simple:</p><p><strong>encryption hides the message</strong></p><p><strong>MAC protects the message from silent modification</strong></p><p>That is the missing half we need.</p><div><hr></div><h2>Adding HMAC to the channel</h2><p>Let&#8217;s start by upgrading the record format from Part 1.</p><p>In Part 1, our protected payload was basically:</p><pre><code><code>nonce || ciphertext</code></code></pre><p>Now we will add a MAC tag:</p><pre><code><code>nonce || ciphertext || tag</code></code></pre><p>And the sender will compute the HMAC over:</p><pre><code><code>nonce || ciphertext</code></code></pre><p>So the full logic becomes:</p><h3>Sender</h3><ol><li><p>encrypt plaintext with AES-CTR</p></li><li><p>compute HMAC over <code>nonce || ciphertext</code></p></li><li><p>send <code>nonce || ciphertext || tag</code></p></li></ol><h3>Receiver</h3><ol><li><p>read <code>nonce || ciphertext || tag</code></p></li><li><p>recompute HMAC over <code>nonce || ciphertext</code></p></li><li><p>compare tags</p></li><li><p>only if they match, decrypt the ciphertext</p></li><li><p>otherwise reject the message</p></li></ol><p>There is one more small improvement I want to make here.</p><p>Instead of using one key for everything, we will already separate them:</p><ul><li><p>one key for encryption</p></li><li><p>one key for HMAC</p></li></ul><p>This is still a toy setup, but it is better design than reusing the same bytes for every cryptographic job.</p><h3>HMAC helpers</h3><pre><code><code>import os
import hmac
import hashlib

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

# ---------------------------------------------------------------------------
# Keys &#8212; hardcoded for educational purposes.
# In a real protocol, these would be derived from a key exchange (e.g.,
# Diffie-Hellman), not embedded in source code.
# ---------------------------------------------------------------------------

# 32-byte (256-bit) key for AES-256-CTR encryption.
ENC_KEY = b"0123456789ABCDEF0123456789ABCDEF"

# 32-byte key for HMAC-SHA256.  Separate from the encryption key.
MAC_KEY = b"HMAC_KEY_FOR_PART2_DEMO_1234567"

# HMAC-SHA256 produces a 32-byte (256-bit) tag.
TAG_LEN = 32

# AES-CTR nonce is 16 bytes (128 bits).
NONCE_LEN = 16

def encrypt_then_mac(plaintext: bytes) -&gt; bytes:
    """Encrypt a plaintext and append an HMAC tag.

    Returns: nonce (16 B) || ciphertext (N B) || tag (32 B)
    """

    # Step 1: Generate a fresh random nonce for AES-CTR.
    # A new nonce MUST be used for every record &#8212; reusing a nonce with
    # the same key completely breaks CTR-mode security.
    nonce = os.urandom(NONCE_LEN)

    # Step 2: Encrypt the plaintext with AES-256-CTR.
    cipher = Cipher(algorithms.AES(ENC_KEY), modes.CTR(nonce))
    encryptor = cipher.encryptor()
    ciphertext = encryptor.update(plaintext) + encryptor.finalize()

    # Step 3: Compute HMAC-SHA256 over (nonce || ciphertext).
    # New in Part 2: we authenticate the encrypted record before sending it.
    # The HMAC input includes the nonce so an attacker cannot swap nonces
    # between records without detection.
    mac_input = nonce + ciphertext
    tag = hmac.new(MAC_KEY, mac_input, hashlib.sha256).digest()

    print(f"  [crypto_hmac] encrypt_then_mac:")
    print(f"    nonce    = {nonce.hex()[:32]}...")
    print(f"    ct_len   = {len(ciphertext)} bytes")
    print(f"    tag      = {tag.hex()[:32]}...")

    # Step 4: Assemble the wire format.
    return nonce + ciphertext + tag

def verify_then_decrypt(payload: bytes) -&gt; bytes:
    """Verify the HMAC tag, then decrypt if valid.

    Expects: nonce (16 B) || ciphertext (N B) || tag (32 B)
    Raises ValueError if the tag does not match.
    """

    # Step 1: Parse the record into its components.
    # The tag is always the last 32 bytes.  The nonce is the first 16.
    # Everything in between is ciphertext.
    if len(payload) &lt; NONCE_LEN + TAG_LEN:
        raise ValueError("Record too short to contain nonce + tag")

    nonce = payload[:NONCE_LEN]
    ciphertext = payload[NONCE_LEN:-TAG_LEN]
    received_tag = payload[-TAG_LEN:]

    # Step 2: Recompute the HMAC over (nonce || ciphertext).
    mac_input = nonce + ciphertext
    expected_tag = hmac.new(MAC_KEY, mac_input, hashlib.sha256).digest()

    # Step 3: Compare tags using constant-time comparison.
    # hmac.compare_digest() prevents timing side-channel attacks.
    # A naive `==` comparison can leak information about which byte
    # position differs first, allowing an attacker to forge a valid
    # tag byte by byte.
    if not hmac.compare_digest(received_tag, expected_tag):
        print("  [crypto_hmac] *** MAC VERIFICATION FAILED &#8212; record rejected ***")
        raise ValueError("HMAC verification failed &#8212; record has been tampered with")

    print("  [crypto_hmac] MAC verification: OK")

    # Step 4: Decrypt only after verification succeeds.
    # This is the key benefit of encrypt-then-MAC: we never process
    # unauthenticated ciphertext.
    cipher = Cipher(algorithms.AES(ENC_KEY), modes.CTR(nonce))
    decryptor = cipher.decryptor()
    plaintext = decryptor.update(ciphertext) + decryptor.finalize()

    return plaintext</code></code></pre><p>This is the first big improvement over Part 1.</p><p>The important change is not just that we added a tag.</p><p>It is that the receiver no longer blindly trusts ciphertext and only then discovers what it means. Now the receiver first checks whether the record is authentic and unchanged.</p><p>That is a very different protocol posture.</p><div><hr></div><h2>Updating the client and server</h2><p>Now let&#8217;s plug this into the channel.</p><h3>HMAC-based client</h3><pre><code><code>import socket

from framing import send_record, recv_record
from crypto_hmac import encrypt_then_mac, verify_then_decrypt

HOST = "127.0.0.1"
PORT = 9001

# A toy HTTP-like request &#8212; same spirit as Part 1.
request = (
    "GET /transfer?to=bob&amp;amount=100 HTTP/1.1\\r\\nHost: localhost\\r\\n\\r\\n"
).encode("utf-8")

print("=" * 60)
print("Part 2 &#8212; HMAC Client (encrypt-then-MAC)")
print("=" * 60)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
    client.connect((HOST, PORT))
    print(f"Connected to {HOST}:{PORT}")

    # ----- SEND REQUEST -----
    print("\\n--- Sending request ---")
    protected = encrypt_then_mac(request)
    send_record(client, protected)
    print(f"  Record sent ({len(protected)} bytes on wire)")

    # ----- RECEIVE RESPONSE -----
    print("\\n--- Receiving response ---")
    raw_response = recv_record(client)
    response = verify_then_decrypt(raw_response)
    print(f"\\n  Decrypted response:\\n  {response.decode('utf-8')}")

print("\\nDone.")</code></code></pre><h3>HMAC-based server</h3><pre><code><code>import socket

from framing import send_record, recv_record
from crypto_hmac import encrypt_then_mac, verify_then_decrypt

HOST = "127.0.0.1"
PORT = 9001

print("=" * 60)
print("Part 2 &#8212; HMAC Server (encrypt-then-MAC)")
print("=" * 60)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
    # SO_REUSEADDR lets us restart the server immediately without waiting
    # for the OS to release the port from TIME_WAIT state.
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server.bind((HOST, PORT))
    server.listen(1)
    print(f"Listening on {HOST}:{PORT}")

    conn, addr = server.accept()
    with conn:
        print(f"Connected by {addr}")

        # ----- RECEIVE REQUEST -----
        print("\\n--- Receiving request ---")
        raw_request = recv_record(conn)

        try:
            request = verify_then_decrypt(raw_request)
        except ValueError as e:
            # New in Part 2: if the MAC fails, we reject the record loudly.
            # In Part 1 we had no way to detect tampering at all.
            print(f"\\n  *** REJECTED: {e} ***")
            print("  Connection closed &#8212; refusing to process tampered data.")
        else:
            print(f"\\n  Decrypted request:\\n  {request.decode('utf-8')}")

            # ----- SEND RESPONSE -----
            print("--- Sending response ---")
            response = (
                "HTTP/1.1 200 OK\\r\\n"
                "Content-Type: text/plain\\r\\n"
                "Content-Length: 13\\r\\n\\r\\n"
                "hello, client"
            ).encode("utf-8")

            protected = encrypt_then_mac(response)
            send_record(conn, protected)
            print(f"  Record sent ({len(protected)} bytes on wire)")

print("\\nDone.")</code></code></pre><p>The shape of the channel is still familiar.</p><p>That matters.</p><p>We did not replace the whole design.</p><p>We strengthened one missing property.</p><p>That is how protocol evolution should feel.</p><div><hr></div><h4>Let&#8217;s check it on the wire.</h4><p>We try to start the server and client, which we just created.</p><p>Here is our client request&#8217;s data.</p><pre><code><code>-- Sending request ---
[crypto_hmac] encrypt_then_mac:
nonce = 387f0065f8915133473597d8cef15f34...
ct_len = 61 bytes
tag = b7f0db369e5d2a221a18f2d167b5b3a8...
Record sent (109 bytes on wire)
</code></code></pre><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JQ4z!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdddd6f-33a6-499c-b25f-e4d43ecbae2d_2880x1620.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JQ4z!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdddd6f-33a6-499c-b25f-e4d43ecbae2d_2880x1620.png 424w, https://substackcdn.com/image/fetch/$s_!JQ4z!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdddd6f-33a6-499c-b25f-e4d43ecbae2d_2880x1620.png 848w, https://substackcdn.com/image/fetch/$s_!JQ4z!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdddd6f-33a6-499c-b25f-e4d43ecbae2d_2880x1620.png 1272w, https://substackcdn.com/image/fetch/$s_!JQ4z!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdddd6f-33a6-499c-b25f-e4d43ecbae2d_2880x1620.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JQ4z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdddd6f-33a6-499c-b25f-e4d43ecbae2d_2880x1620.png" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1bdddd6f-33a6-499c-b25f-e4d43ecbae2d_2880x1620.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:819,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:651773,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/193281237?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdddd6f-33a6-499c-b25f-e4d43ecbae2d_2880x1620.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JQ4z!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdddd6f-33a6-499c-b25f-e4d43ecbae2d_2880x1620.png 424w, https://substackcdn.com/image/fetch/$s_!JQ4z!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdddd6f-33a6-499c-b25f-e4d43ecbae2d_2880x1620.png 848w, https://substackcdn.com/image/fetch/$s_!JQ4z!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdddd6f-33a6-499c-b25f-e4d43ecbae2d_2880x1620.png 1272w, https://substackcdn.com/image/fetch/$s_!JQ4z!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1bdddd6f-33a6-499c-b25f-e4d43ecbae2d_2880x1620.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><div><hr></div><p></p><h2>Detecting tampering</h2><p>Now let&#8217;s revisit the failure from Part 1.</p><p>Previously, if someone modified the ciphertext, the receiver would still decrypt it and accept modified plaintext.</p><p>Now that should no longer work.</p><p>Here is a tiny tampering demo:</p><pre><code><code># tampering_demo_hmac.py
from crypto_hmac import encrypt_then_mac, verify_then_decrypt

original = b"amount=100"
protected = encrypt_then_mac(original)

tampered = bytearray(protected)
tampered[20] ^= 0x08  # flip one bit somewhere in the encrypted body

try:
    result = verify_then_decrypt(bytes(tampered))
    print("Unexpected success:", result)
except ValueError as e:
    print("Tampering detected:", e)
</code></code></pre><p>Now the result should be rejection, not silent acceptance.</p><p>That is exactly what we wanted.</p><p>This is the moment where our channel stops being merely &#8220;encrypted&#8221; and starts being &#8220;protected.&#8221;</p><p>Because now the receiver does not just recover bytes. It verifies them first.</p><p>That is a serious step.</p><div><hr></div><h2>Why we also need a sequence number</h2><p>At this point, we fixed the big flaw from Part 1: silent tampering.</p><p>But the record layer is still naive.</p><p>Why?</p><p>Because even with a valid HMAC, the receiver still has no sense of record position or freshness.</p><p>Imagine an attacker records one valid protected message and sends it again later.</p><p>The HMAC is still valid.</p><p>The ciphertext is still valid.</p><p>And unless the receiver keeps some state, it may accept the same record again.</p><p>That means integrity alone is not the whole story.</p><p>We also need some sense of:</p><ul><li><p>order</p></li><li><p>position</p></li><li><p>repetition</p></li><li><p>replay</p></li></ul><p>This is where sequence numbers come in.</p><p>A sequence number is just a counter that increases with every record:</p><ul><li><p>first record = 0</p></li><li><p>next = 1</p></li><li><p>next = 2</p></li><li><p>and so on</p></li></ul><p>We then include that sequence number in the authenticated data, so the receiver does not just verify &#8220;these bytes were protected,&#8221; but also &#8220;these bytes belong in this position in the stream.&#8221;</p><p>That makes the record layer much less naive.</p><p>It still does not solve every replay problem in every possible system. But for our toy protocol, it is a very good next step.</p><div><hr></div><h2>Updating the record format</h2><p>Now our record becomes:</p><pre><code><code>seq || nonce || ciphertext || tag
</code></code></pre><p>And our HMAC input becomes:</p><pre><code><code>seq || nonce || ciphertext
</code></code></pre><p>So the sender and receiver now both need a little bit of state:</p><ul><li><p>the sender tracks the next sequence number to send</p></li><li><p>the receiver tracks the next sequence number it expects</p></li></ul><p>This is one of those moments where secure transport starts looking more like a real protocol and less like &#8220;some crypto around a socket.&#8221;</p><h3>Sequence-aware HMAC helper</h3><pre><code><code># crypto_hmac_seq.py
import os
import hmac
import hashlib
import struct

from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

# ---------------------------------------------------------------------------
# Keys &#8212; same as crypto_hmac.py, hardcoded for education.
# ---------------------------------------------------------------------------
ENC_KEY = b"0123456789ABCDEF0123456789ABCDEF"
MAC_KEY = b"HMAC_KEY_FOR_PART2_DEMO_1234567"

TAG_LEN = 32  # HMAC-SHA256 output: 32 bytes (256 bits)
NONCE_LEN = 16  # AES-CTR nonce: 16 bytes (128 bits)
SEQ_LEN = 8  # Sequence number: 8 bytes (64-bit unsigned integer)

def protect_record(seq: int, plaintext: bytes) -&gt; bytes:
    """Encrypt a plaintext record and attach a sequence-aware HMAC tag.

    Args:
        seq:       The current send-side sequence number (0, 1, 2, &#8230;).
        plaintext: The message to protect.

    Returns:
        seq (8 B) || nonce (16 B) || ciphertext (N B) || tag (32 B)
    """

    # Pack the sequence number as an 8-byte big-endian unsigned integer.
    # "!Q" = network byte order, unsigned 64-bit.
    seq_bytes = struct.pack("!Q", seq)

    # Generate a fresh AES-CTR nonce.
    nonce = os.urandom(NONCE_LEN)

    # Encrypt the plaintext.
    cipher = Cipher(algorithms.AES(ENC_KEY), modes.CTR(nonce))
    encryptor = cipher.encryptor()
    ciphertext = encryptor.update(plaintext) + encryptor.finalize()

    # Compute HMAC over (seq || nonce || ciphertext).
    # The sequence number is included in the MAC input so the integrity
    # check also covers record order/position in the stream.
    mac_input = seq_bytes + nonce + ciphertext
    tag = hmac.new(MAC_KEY, mac_input, hashlib.sha256).digest()

    return seq_bytes + nonce + ciphertext + tag

def verify_and_unprotect(expected_seq: int, payload: bytes) -&gt; bytes:
    """Verify the HMAC and sequence number, then decrypt.

    Args:
        expected_seq: The sequence number the receiver expects next.
        payload:      The raw bytes received: seq || nonce || ct || tag.

    Returns:
        The decrypted plaintext.

    Raises:
        ValueError if the MAC is invalid or the sequence number is wrong.
    """

    min_len = SEQ_LEN + NONCE_LEN + TAG_LEN
    if len(payload) &lt; min_len:
        raise ValueError("Record too short")

    # Step 1: Parse the record.
    seq_bytes = payload[:SEQ_LEN]
    nonce = payload[SEQ_LEN : SEQ_LEN + NONCE_LEN]
    ciphertext = payload[SEQ_LEN + NONCE_LEN : -TAG_LEN]
    received_tag = payload[-TAG_LEN:]

    # Step 2: Recompute HMAC over (seq || nonce || ciphertext).
    mac_input = seq_bytes + nonce + ciphertext
    expected_tag = hmac.new(MAC_KEY, mac_input, hashlib.sha256).digest()

    # Step 3: Constant-time tag comparison.
    if not hmac.compare_digest(received_tag, expected_tag):
        print("  [crypto_hmac_seq] *** MAC VERIFICATION FAILED ***")
        raise ValueError("HMAC verification failed &#8212; record tampered or replayed")

    print("  [crypto_hmac_seq] MAC verification: OK")

    # Step 4: Check the sequence number matches what we expect.
    # Even though the MAC already covers the sequence number (so an
    # attacker cannot change it without invalidating the MAC), we still
    # explicitly verify that it matches our counter.  This catches
    # replayed or reordered records that carry a valid MAC but belong
    # to a different position in the stream.
    (received_seq,) = struct.unpack("!Q", seq_bytes)
    if received_seq != expected_seq:
        print(
            f"  [crypto_hmac_seq] *** SEQUENCE MISMATCH: "
            f"got {received_seq}, expected {expected_seq} ***"
        )
        raise ValueError(
            f"Sequence number mismatch: got {received_seq}, expected {expected_seq}"
        )

    print(
        f"  [crypto_hmac_seq] Sequence number: {received_seq} (expected {expected_seq}) &#8212; OK"
    )

    # Step 5: Decrypt.
    cipher = Cipher(algorithms.AES(ENC_KEY), modes.CTR(nonce))
    decryptor = cipher.decryptor()
    plaintext = decryptor.update(ciphertext) + decryptor.finalize()

    return plaintext

</code></code></pre><p>And now the channel has a bit more memory.</p><p>Not just &#8220;is this record authentic?&#8221;</p><p>But also &#8220;is this the record I expected next?&#8221;</p><p>That is a real protocol improvement.</p><div><hr></div><h2>Updating the client and server</h2><p>Now let&#8217;s plug this into the channel.</p><h3>Sequence-aware HMAC-based client</h3><pre><code><code># client_v2_hmac_seq.py
import socket

from framing import send_record, recv_record
from crypto_hmac_seq import protect_record, verify_and_unprotect

HOST = "127.0.0.1"
PORT = 9003

# Sequence counters &#8212; sender and receiver each maintain their own.
# The sender increments after each record sent.
# The receiver expects consecutive values starting from 0.
send_seq = 0
recv_seq = 0

# A toy HTTP-like request &#8212; same spirit as Part 1.
request = (
    "GET /transfer?to=bob&amp;amount=100 HTTP/1.1\\r\\nHost: localhost\\r\\n\\r\\n"
).encode("utf-8")

print("=" * 60)
print("Part 2 &#8212; HMAC + Sequence Numbers Client (Stage 2)")
print("=" * 60)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
    client.connect((HOST, PORT))
    print(f"Connected to {HOST}:{PORT}")

    # ----- SEND REQUEST -----
    print(f"\\n--- Sending request (send_seq={send_seq}) ---")
    protected = protect_record(send_seq, request)
    send_record(client, protected)
    send_seq += 1
    print(f"  Record sent ({len(protected)} bytes on wire)")

    # ----- RECEIVE RESPONSE -----
    print(f"\\n--- Receiving response (expecting recv_seq={recv_seq}) ---")
    raw_response = recv_record(client)

    try:
        response = verify_and_unprotect(recv_seq, raw_response)
        recv_seq += 1
        print(f"\\n  Decrypted response:\\n  {response.decode('utf-8')}")
    except ValueError as e:
        print(f"\\n  *** REJECTED: {e} ***")

print("\\nDone.")

</code></code></pre><h3>Sequence-aware HMAC-based server</h3><pre><code><code># server_v2_hmac_seq.py
import socket

from framing import send_record, recv_record
from crypto_hmac_seq import protect_record, verify_and_unprotect

HOST = "127.0.0.1"
PORT = 9003

# Sequence counters.
# The server's recv_seq tracks the client's send_seq, and vice versa.
send_seq = 0
recv_seq = 0

print("=" * 60)
print("Part 2 &#8212; HMAC + Sequence Numbers Server (Stage 2)")
print("=" * 60)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server.bind((HOST, PORT))
    server.listen(1)
    print(f"Listening on {HOST}:{PORT}")

    conn, addr = server.accept()
    with conn:
        print(f"Connected by {addr}")

        # ----- RECEIVE REQUEST -----
        print(f"\\n--- Receiving request (expecting recv_seq={recv_seq}) ---")
        raw_request = recv_record(conn)

        try:
            request = verify_and_unprotect(recv_seq, raw_request)
            recv_seq += 1
        except ValueError as e:
            # Rejection: either the MAC is invalid, the sequence number
            # is wrong, or the data was tampered with / replayed.
            print(f"\\n  *** REJECTED: {e} ***")
            print("  Connection closed &#8212; refusing to process invalid data.")
        else:
            print(f"\\n  Decrypted request:\\n  {request.decode('utf-8')}")

            # ----- SEND RESPONSE -----
            print(f"--- Sending response (send_seq={send_seq}) ---")
            response = (
                "HTTP/1.1 200 OK\\r\\n"
                "Content-Type: text/plain\\r\\n"
                "Content-Length: 13\\r\\n\\r\\n"
                "hello, client"
            ).encode("utf-8")

            protected = protect_record(send_seq, response)
            send_record(conn, protected)
            send_seq += 1
            print(f"  Record sent ({len(protected)} bytes on wire)")

print("\\nDone.")

</code></code></pre><div><hr></div><h2>Why real-world systems usually do not stop here</h2><p>At this point, we have something much stronger than Part 1.</p><p>We have:</p><ul><li><p>encryption</p></li><li><p>integrity protection</p></li><li><p>message authentication</p></li><li><p>sequence-aware records</p></li></ul><p>That is already a meaningful protocol.</p><p>But if you look at how real systems are usually built, they do not normally stop at manually composing:</p><ul><li><p>AES-CTR</p></li><li><p>HMAC-SHA256</p></li><li><p>explicit sequence-aware record protection</p></li></ul><p>Why?</p><p>Because modern systems usually prefer a single primitive that gives confidentiality and integrity together.</p><p>That is where <strong>AEAD</strong> comes in.</p><p>We separated these properties on purpose because it makes the protocol easier to understand.</p><p>But the real world usually packages them together.</p><div><hr></div><h2>A very short note on AEAD</h2><p>AEAD stands for <strong>Authenticated Encryption with Associated Data</strong>.</p><p>That sounds heavier than it really is.</p><p>The practical idea is simple:</p><p>An AEAD construction gives us:</p><ul><li><p>encryption</p></li><li><p>integrity/authentication of the encrypted message</p></li><li><p>and the ability to authenticate extra metadata that should not be encrypted</p></li></ul><p>Common examples are:</p><ul><li><p>AES-GCM</p></li><li><p>ChaCha20-Poly1305</p></li></ul><p>This is much closer to how modern secure protocols protect records.</p><p>It is also why I wanted to include AEAD in this part. If we stopped only at &#8220;encrypt + HMAC,&#8221; we would understand the missing property better, but we would still be one step away from how modern systems actually package it.</p><p>So now we take that final step.</p><div><hr></div><h2>Moving our channel to AEAD</h2><p>For the AEAD version, I will use <strong>AES-GCM</strong>.</p><p>The high-level idea is:</p><ul><li><p>the plaintext gets encrypted</p></li><li><p>integrity/authentication is built in</p></li><li><p>and we can include extra metadata as associated data</p></li></ul><p>In our case, the sequence number is a good example of associated data.</p><p>That means:</p><ul><li><p>it does not need to be encrypted</p></li><li><p>but it should still be authenticated</p></li></ul><h3>AEAD-based helper</h3><pre><code><code># crypto_aead.py
import os
import struct

from cryptography.hazmat.primitives.ciphers.aead import AESGCM

# ---------------------------------------------------------------------------
# Key &#8212; a single 256-bit key for AES-GCM.
# With AEAD, we do NOT need separate encryption and MAC keys &#8212; the
# algorithm handles both internally.
# ---------------------------------------------------------------------------
AEAD_KEY = b"AEAD_KEY_PART2_DEMO_FOR_AES_GCM!"  # 32 bytes &#8594; AES-256-GCM

# AES-GCM nonce length: 12 bytes is the recommended (and most efficient) size.
NONCE_LEN = 12

# Sequence number: 8 bytes (64-bit unsigned integer), same as Stage 2.
SEQ_LEN = 8

def protect_record_aead(seq: int, plaintext: bytes) -&gt; bytes:
    """Seal a plaintext record with AES-GCM.

    Args:
        seq:       The current send-side sequence number.
        plaintext: The message to protect.

    Returns:
        seq (8 B) || nonce (12 B) || ciphertext_and_tag (N+16 B)
    """

    # Pack the sequence number as associated data.
    # The sequence number is authenticated but sent in the clear &#8212; the
    # receiver needs it to know which counter value to expect.
    seq_bytes = struct.pack("!Q", seq)

    # Generate a random 12-byte nonce for AES-GCM.
    nonce = os.urandom(NONCE_LEN)

    # Create an AESGCM instance with our key.
    aesgcm = AESGCM(AEAD_KEY)

    # Encrypt and authenticate in one call.
    # AESGCM.encrypt(nonce, data, associated_data) returns
    # ciphertext || 16-byte authentication tag as a single bytes object.
    # The associated_data (seq_bytes) is authenticated but NOT encrypted.
    ciphertext_and_tag = aesgcm.encrypt(nonce, plaintext, seq_bytes)

    print(f"  [crypto_aead] protect_record_aead:")
    print(f"    seq      = {seq}")
    print(f"    nonce    = {nonce.hex()}")
    print(
        f"    sealed   = {len(ciphertext_and_tag)} bytes "
        f"(plaintext {len(plaintext)} + tag 16)"
    )

    return seq_bytes + nonce + ciphertext_and_tag

def unprotect_record_aead(expected_seq: int, payload: bytes) -&gt; bytes:
    """Verify and decrypt an AES-GCM sealed record.

    Args:
        expected_seq: The sequence number the receiver expects next.
        payload:      seq (8 B) || nonce (12 B) || ciphertext_and_tag.

    Returns:
        The decrypted plaintext.

    Raises:
        ValueError if the sequence number is wrong.
        cryptography.exceptions.InvalidTag if decryption/auth fails.
    """

    min_len = SEQ_LEN + NONCE_LEN + 16  # at least seq + nonce + tag
    if len(payload) &lt; min_len:
        raise ValueError("Record too short")

    # Step 1: Parse the record.
    seq_bytes = payload[:SEQ_LEN]
    nonce = payload[SEQ_LEN : SEQ_LEN + NONCE_LEN]
    ciphertext_and_tag = payload[SEQ_LEN + NONCE_LEN :]

    # Step 2: Check the sequence number.
    (received_seq,) = struct.unpack("!Q", seq_bytes)
    if received_seq != expected_seq:
        print(
            f"  [crypto_aead] *** SEQUENCE MISMATCH: "
            f"got {received_seq}, expected {expected_seq} ***"
        )
        raise ValueError(
            f"Sequence number mismatch: got {received_seq}, expected {expected_seq}"
        )

    print(
        f"  [crypto_aead] Sequence number: {received_seq} "
        f"(expected {expected_seq}) &#8212; OK"
    )

    # Step 3: Decrypt and verify in one call.
    # AESGCM.decrypt(nonce, data, associated_data) verifies the auth tag
    # and decrypts.  If anything was tampered with &#8212; the ciphertext, the
    # tag, or the associated data &#8212; it raises InvalidTag.
    aesgcm = AESGCM(AEAD_KEY)
    plaintext = aesgcm.decrypt(nonce, ciphertext_and_tag, seq_bytes)

    print(f"  [crypto_aead] AEAD decryption: OK ({len(plaintext)} bytes)")

    return plaintext

</code></code></pre><p>This code is noticeably simpler.</p><p>That is one of the big practical advantages of AEAD.</p><p>Instead of manually:</p><ul><li><p>encrypting</p></li><li><p>computing HMAC</p></li><li><p>verifying HMAC</p></li><li><p>then decrypting</p></li></ul><p>we use one primitive that already combines confidentiality and integrity.</p><p>And the sequence number fits naturally as associated data.</p><h3>AEAD-based client</h3><pre><code><code># client_v2_aead.py
import socket

from framing import send_record, recv_record
from crypto_aead import protect_record_aead, unprotect_record_aead

HOST = "127.0.0.1"
PORT = 9002

# Sequence counters &#8212; sender and receiver each maintain their own.
# The sender increments after each record sent.
# The receiver expects consecutive values starting from 0.
send_seq = 0
recv_seq = 0

# A toy HTTP-like request.
request = (
    "GET /transfer?to=bob&amp;amount=100 HTTP/1.1\\r\\nHost: localhost\\r\\n\\r\\n"
).encode("utf-8")

print("=" * 60)
print("Part 2 &#8212; AEAD Client (AES-GCM)")
print("=" * 60)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
    client.connect((HOST, PORT))
    print(f"Connected to {HOST}:{PORT}")

    # ----- SEND REQUEST -----
    print(f"\\n--- Sending request (send_seq={send_seq}) ---")
    protected = protect_record_aead(send_seq, request)
    send_record(client, protected)
    send_seq += 1
    print(f"  Record sent ({len(protected)} bytes on wire)")

    # ----- RECEIVE RESPONSE -----
    print(f"\\n--- Receiving response (expecting recv_seq={recv_seq}) ---")
    raw_response = recv_record(client)

    try:
        response = unprotect_record_aead(recv_seq, raw_response)
        recv_seq += 1
        print(f"\\n  Decrypted response:\\n  {response.decode('utf-8')}")
    except Exception as e:
        print(f"\\n  *** REJECTED: {e} ***")

print("\\nDone.")

</code></code></pre><h3>AEAD-based server</h3><pre><code><code># server_v2_aead.py
import socket

from framing import send_record, recv_record
from crypto_aead import protect_record_aead, unprotect_record_aead

HOST = "127.0.0.1"
PORT = 9002

# Sequence counters.
# The server's recv_seq tracks the client's send_seq, and vice versa.
send_seq = 0
recv_seq = 0

print("=" * 60)
print("Part 2 &#8212; AEAD Server (AES-GCM)")
print("=" * 60)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
    server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server.bind((HOST, PORT))
    server.listen(1)
    print(f"Listening on {HOST}:{PORT}")

    conn, addr = server.accept()
    with conn:
        print(f"Connected by {addr}")

        # ----- RECEIVE REQUEST -----
        print(f"\\n--- Receiving request (expecting recv_seq={recv_seq}) ---")
        raw_request = recv_record(conn)

        try:
            request = unprotect_record_aead(recv_seq, raw_request)
            recv_seq += 1
        except Exception as e:
            # AEAD rejection: either the auth tag is invalid, the sequence
            # number is wrong, or the data was tampered with.
            print(f"\\n  *** REJECTED: {e} ***")
            print("  Connection closed &#8212; refusing to process invalid data.")
        else:
            print(f"\\n  Decrypted request:\\n  {request.decode('utf-8')}")

            # ----- SEND RESPONSE -----
            print(f"--- Sending response (send_seq={send_seq}) ---")
            response = (
                "HTTP/1.1 200 OK\\r\\n"
                "Content-Type: text/plain\\r\\n"
                "Content-Length: 13\\r\\n\\r\\n"
                "hello, client"
            ).encode("utf-8")

            protected = protect_record_aead(send_seq, response)
            send_record(conn, protected)
            send_seq += 1
            print(f"  Record sent ({len(protected)} bytes on wire)")

print("\\nDone.")

</code></code></pre><p>This version is already much closer to how modern secure transport actually protects records.</p><p>Not identical to TLS, of course. But structurally much closer.</p><div><hr></div><h2>What we gained</h2><p>At this point, our channel is much stronger than the one from Part 1.</p><p>We now have:</p><ul><li><p>confidentiality</p></li><li><p>integrity protection</p></li><li><p>authenticated records</p></li><li><p>sequence-aware message handling</p></li><li><p>a much more realistic record protection design through AEAD</p></li></ul><p>That is a big improvement.</p><p>The receiver is no longer just decrypting whatever arrives and trusting the result. Now the receiver can reject modified or structurally unexpected records.</p><p>That is a real protocol boundary.</p><div><hr></div><h2>What is still broken</h2><p>And yet, even now, we are still very far from real TLS.</p><p>Because the biggest assumption in our design is still untouched:</p><p><strong>both sides already share the necessary secret keys</strong></p><p>That means we still do not know how to solve the next real problem:</p><ul><li><p>how do two strangers establish fresh secrets?</p></li><li><p>how does the client know it is talking to the right server?</p></li><li><p>how do we scale beyond hardcoded shared secrets?</p></li><li><p>how do we build trust instead of assuming it?</p></li></ul><p>We improved record protection a lot.</p><p>But we still do not have a real way to establish trust.</p><p>That is the next wall.</p><div><hr></div><h2>Summary</h2><p>In this part, we took the encrypted but still incomplete channel from Part 1 and made it much more serious.</p><p>First, we added <strong>HMAC</strong>, which gave the receiver a way to detect tampering.</p><p>Then, we added a <strong>sequence number</strong>, which made the record layer less naive and bound records to their place in the stream.</p><p>Finally, we moved to <strong>AEAD</strong>, because in real-world systems confidentiality and integrity are usually protected together, not assembled manually from separate pieces.</p><p>So this article had two goals:</p><ul><li><p>understand the missing property explicitly</p></li><li><p>then move toward the real-world shape of the solution</p></li></ul><p>That is why we did not stop at HMAC.</p><p>But even after all of this, we still depend on one assumption that makes the whole thing unrealistic:</p><p>we are still starting with pre-shared secret keys.</p><p>And that is exactly what the next part will attack.</p><div><hr></div><h2>Next part &#8212; getting rid of the pre-shared key</h2><p>So we stop here.</p><p>We now have a much better record layer than in Part 1. But we are still relying on a hardcoded shared secret, and that is not how real secure communication between strangers on the internet works.</p><p>In the next part, we will stop assuming both sides already share a secret.</p><p>We will start building a real handshake and establish fresh session keys instead.</p><p>That still will not give us full TLS.</p><p>But it will take us much closer to the real shape of the protocol.</p><div><hr></div><h2>Final code</h2><p>I&#8217;ll put the full code for this part on GitHub here:</p><p><strong><a href="https://github.com/DmytroHuzz/rebuilding_tls/tree/main/part_2">[GitHub link to final code]</a></strong></p>]]></content:encoded></item><item><title><![CDATA[Rebuilding TLS, Part 1 — Why Encryption Alone Is Not Enough]]></title><description><![CDATA[From transparent TCP traffic to encrypted records &#8212; and the first reason TLS needs more than encryption]]></description><link>https://www.dmytrohuz.com/p/rebuilding-tls-part-1-why-encryption</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/rebuilding-tls-part-1-why-encryption</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Sun, 29 Mar 2026 18:57:36 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!5phz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f085090-d20e-4850-844a-c79a878be8e6_1536x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!5phz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f085090-d20e-4850-844a-c79a878be8e6_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!5phz!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f085090-d20e-4850-844a-c79a878be8e6_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!5phz!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f085090-d20e-4850-844a-c79a878be8e6_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!5phz!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f085090-d20e-4850-844a-c79a878be8e6_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!5phz!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f085090-d20e-4850-844a-c79a878be8e6_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!5phz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f085090-d20e-4850-844a-c79a878be8e6_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4f085090-d20e-4850-844a-c79a878be8e6_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2856965,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/192533658?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f085090-d20e-4850-844a-c79a878be8e6_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!5phz!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f085090-d20e-4850-844a-c79a878be8e6_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!5phz!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f085090-d20e-4850-844a-c79a878be8e6_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!5phz!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f085090-d20e-4850-844a-c79a878be8e6_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!5phz!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f085090-d20e-4850-844a-c79a878be8e6_1536x1024.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>A year ago I wrote a series about how a web server works.</p><p>I started from a very primitive version and step by step moved toward the same core ideas modern production servers rely on. When I finished that series, I thought the next step would be small.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>Wrap it in TLS. Make the communication secure.</p><p>It did not stay small for long.</p><p>What looked like a thin security layer on top of an existing server turned into a much deeper journey into cryptography, authentication, trust, certificates, protocol design, and many details usually hidden behind one familiar phrase: <strong>secure connection</strong>.</p><p>So this series is my attempt to approach TLS the same way I approached the web server: not as a finished black box, but as something we can rebuild from simpler pieces until its shape starts to make sense.</p><p>In this first part, we will start with the most naive version of the problem.</p><p>We will build a very simple socket-based communication channel, see that it is fully transparent, wrap it in encryption with a shared secret key, and then see why that is still not enough.</p><p>That will give us our first fake secure channel.</p><p>And that is exactly where we should start.</p><div><hr></div><h2>What we will build in this part</h2><p>The plan for this article is simple:</p><ul><li><p>build a tiny socket-based client and server</p></li><li><p>send plain text between them</p></li><li><p>look at the traffic and see that everything is visible</p></li><li><p>add shared-key encryption with AES-CTR</p></li><li><p>make the traffic unreadable</p></li><li><p>then show why encryption alone still does not give us a trustworthy secure protocol</p></li></ul><p>We are not trying to build real TLS yet.</p><p>We are trying to make the first mistake on purpose.</p><p>Because once that mistake becomes visible, the next piece of the protocol stops looking optional.</p><div><hr></div><h2>TLS is not SSL</h2><p>Before we start, one small clarification.</p><p>People still often say &#8220;SSL&#8221; when they talk about secure communication on the web. But SSL is the older family of protocols. TLS is its successor.</p><p>So when people say things like &#8220;SSL certificate&#8221; or &#8220;SSL connection,&#8221; in practice they usually mean TLS.</p><p>For modern systems, the relevant protocols are TLS, especially TLS 1.2 and TLS 1.3. This series is about understanding the ideas behind TLS by rebuilding simpler versions of the problems it solves.</p><p>And instead of starting from the finished protocol, we will begin one layer lower &#8212; with plain socket communication.</p><div><hr></div><h2>Step 1 &#8212; A plain socket-based communication channel</h2><p>Let&#8217;s start with the smallest possible thing: a tiny TCP server and a tiny TCP client.</p><p>The client will send an HTTP-like request.</p><p>The server will read it and return an HTTP-like response.</p><p>Nothing secure yet. Just raw bytes moving over a socket.</p><h3>Plain server</h3><pre><code><code># server_plain.py
import socket

HOST = "127.0.0.1"
PORT = 8081

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
    server.bind((HOST, PORT))
    server.listen(1)

    print(f"Listening on {HOST}:{PORT}")
    conn, addr = server.accept()

    with conn:
        print(f"Connected by {addr}")

        data = conn.recv(4096)
        request = data.decode("utf-8")
        print("Received request:")
        print(request)

        response = (
            "HTTP/1.1 200 OK\\r\\n"
            "Content-Type: text/plain\\r\\n"
            "Content-Length: 13\\r\\n"
            "\\r\\n"
            "hello, client"
        )
        conn.sendall(response.encode("utf-8"))
</code></code></pre><h3>Plain client</h3><pre><code><code># client_plain.py
import socket

HOST = "127.0.0.1"
PORT = 8081

request = (
    "GET /transfer?to=bob&amp;amount=100 HTTP/1.1\\r\\n"
    "Host: localhost\\r\\n"
    "\\r\\n"
)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
    client.connect((HOST, PORT))
    client.sendall(request.encode("utf-8"))

    response = client.recv(4096)
    print("Received response:")
    print(response.decode("utf-8"))
</code></code></pre><p>This is intentionally tiny.</p><p>The client sends a request like this:</p><pre><code><code>GET /transfer?to=bob&amp;amount=100 HTTP/1.1
Host: localhost
</code></code></pre><p>The server reads it and sends a response back.</p><p>That is all.</p><p>And because it is all plain TCP, anyone who can observe the traffic can read it directly.</p><h3>Looking at the traffic</h3><p>If you capture this communication in Wireshark, the request and response are fully visible in clear text.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!aF_t!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5181b7e6-c7b5-4740-9a2a-6d19ac38e57f_1677x810.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!aF_t!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5181b7e6-c7b5-4740-9a2a-6d19ac38e57f_1677x810.png 424w, https://substackcdn.com/image/fetch/$s_!aF_t!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5181b7e6-c7b5-4740-9a2a-6d19ac38e57f_1677x810.png 848w, https://substackcdn.com/image/fetch/$s_!aF_t!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5181b7e6-c7b5-4740-9a2a-6d19ac38e57f_1677x810.png 1272w, https://substackcdn.com/image/fetch/$s_!aF_t!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5181b7e6-c7b5-4740-9a2a-6d19ac38e57f_1677x810.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!aF_t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5181b7e6-c7b5-4740-9a2a-6d19ac38e57f_1677x810.png" width="1456" height="703" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/5181b7e6-c7b5-4740-9a2a-6d19ac38e57f_1677x810.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:703,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:135818,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/192533658?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5181b7e6-c7b5-4740-9a2a-6d19ac38e57f_1677x810.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!aF_t!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5181b7e6-c7b5-4740-9a2a-6d19ac38e57f_1677x810.png 424w, https://substackcdn.com/image/fetch/$s_!aF_t!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5181b7e6-c7b5-4740-9a2a-6d19ac38e57f_1677x810.png 848w, https://substackcdn.com/image/fetch/$s_!aF_t!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5181b7e6-c7b5-4740-9a2a-6d19ac38e57f_1677x810.png 1272w, https://substackcdn.com/image/fetch/$s_!aF_t!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F5181b7e6-c7b5-4740-9a2a-6d19ac38e57f_1677x810.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>That is our baseline.</p><p>The client can read it.</p><p>The server can read it.</p><p>And anyone on the wire can read it too.</p><p>So the first obvious idea is also the first naive one:</p><p>If the problem is that everyone can read the bytes, let&#8217;s encrypt the bytes.</p><p>That sounds reasonable.</p><p>And it is still not enough.</p><div><hr></div><h2>Step 2 &#8212; Turning bytes into records</h2><p>Before we add encryption, we need one small but important thing: structure.</p><p>TCP gives us a byte stream.</p><p>It does not give us message boundaries.</p><p>So once we stop sending plain text directly and start sending encrypted blobs, we need a way to tell the receiver how many bytes belong to one logical message.</p><p>That means even before security, we need a little bit of protocol design.</p><p>Let&#8217;s define the smallest possible record format:</p><ul><li><p>4 bytes: payload length</p></li><li><p>N bytes: payload</p></li></ul><pre><code><code>
+----------+-----------+
|  length  |  payload  |
| (4 bytes)|  (varies) |
+----------+-----------+
</code></code></pre><p>That is enough for our first version.</p><pre><code><code># framing.py
import struct

def send_record(sock, payload: bytes) -&gt; None:
    header = struct.pack("!I", len(payload))
    sock.sendall(header + payload)

def recv_exact(sock, n: int) -&gt; bytes:
    chunks = []
    remaining = n

    while remaining &gt; 0:
        chunk = sock.recv(remaining)
        if not chunk:
            raise ConnectionError("Connection closed while reading data")
        chunks.append(chunk)
        remaining -= len(chunk)

    return b"".join(chunks)

def recv_record(sock) -&gt; bytes:
    header = recv_exact(sock, 4)
    (length,) = struct.unpack("!I", header)
    return recv_exact(sock, length)
</code></code></pre><p>This is not a crypto step.</p><p>It is a protocol step.</p><p>And that distinction matters more than it first appears. A secure channel is not just &#8220;call encrypt on a string.&#8221; It is a protocol with structure, state, and rules.</p><p>Now that we have a way to send and receive well-defined records, we can finally wrap them in encryption.</p><div><hr></div><h2>Step 3 &#8212; Wrapping the channel in shared-key encryption</h2><p>The most obvious first attempt at secure communication is usually this:</p><ul><li><p>both sides already know the same secret key</p></li><li><p>the sender encrypts the message before sending</p></li><li><p>the receiver decrypts it after receiving</p></li></ul><p>That is exactly what we will do.</p><p>No handshake yet.</p><p>No certificates yet.</p><p>No integrity yet.</p><p>No authentication yet.</p><p>This version is intentionally naive.</p><p>For encryption, I will use AES in CTR mode.</p><p>Very briefly:</p><ul><li><p>AES is a symmetric block cipher</p></li><li><p>CTR mode makes it convenient for encrypting a stream of bytes</p></li><li><p>it gives us confidentiality</p></li><li><p>but it does <strong>not</strong> give us integrity</p></li></ul><p>That last point is the important one for this article.</p><p>If you want a deeper explanation of AES itself, I already wrote about it in my cryptography series: <a href="https://www.dmytrohuz.com/p/building-own-block-cipher-part-3">https://www.dmytrohuz.com/p/building-own-block-cipher-part-3</a>, so I will not go into the internals here.</p><h3>The nonce</h3><p>CTR mode also needs a nonce.</p><p>For now, think of it as a fresh per-message value that must be different for each encryption under the same key.</p><p>It is not secret.</p><p>It just must not be reused.</p><p>So our encrypted payload will look like this:</p><ul><li><p>nonce</p></li><li><p>ciphertext</p></li></ul><h3>Crypto helper</h3><pre><code><code># crypto.py
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

# 32-byte shared key for AES-256
SHARED_KEY = b"0123456789ABCDEF0123456789ABCDEF"

def encrypt_message(plaintext: bytes) -&gt; bytes:
    nonce = os.urandom(16)

    cipher = Cipher(algorithms.AES(SHARED_KEY), modes.CTR(nonce))
    encryptor = cipher.encryptor()
    ciphertext = encryptor.update(plaintext) + encryptor.finalize()

    # Encrypted payload format:
    # nonce || ciphertext
    return nonce + ciphertext

def decrypt_message(payload: bytes) -&gt; bytes:
    nonce = payload[:16]
    ciphertext = payload[16:]

    cipher = Cipher(algorithms.AES(SHARED_KEY), modes.CTR(nonce))
    decryptor = cipher.decryptor()
    plaintext = decryptor.update(ciphertext) + decryptor.finalize()

    return plaintext
</code></code></pre><p>At this point, our wire format becomes:</p><ul><li><p>4-byte length</p></li><li><p>16-byte nonce</p></li><li><p>ciphertext</p></li></ul><pre><code><code>+----------------+----------------+---------------------+
| length (4 B)   | nonce (16 B)   | ciphertext (N bytes)|
+----------------+----------------+---------------------+</code></code></pre><p>That already looks much more like a protocol.</p><div><hr></div><h2>Step 4 &#8212; Encrypt the request and response</h2><p>Now let&#8217;s integrate this into the client and server.</p><h3>Encrypted client</h3><pre><code><code># client_v1.py
import socket

from framing import send_record, recv_record
from crypto import encrypt_message, decrypt_message

HOST = "127.0.0.1"
PORT = 8081

request = (
    "GET /transfer?to=bob&amp;amount=100 HTTP/1.1\\r\\n"
    "Host: localhost\\r\\n"
    "\\r\\n"
).encode("utf-8")

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
    client.connect((HOST, PORT))

    encrypted_request = encrypt_message(request)
    send_record(client, encrypted_request)

    encrypted_response = recv_record(client)
    response = decrypt_message(encrypted_response)

    print("Received decrypted response:")
    print(response.decode("utf-8"))
</code></code></pre><h3>Encrypted server</h3><pre><code><code># server_v1.py
import socket

from framing import send_record, recv_record
from crypto import encrypt_message, decrypt_message

HOST = "127.0.0.1"
PORT = 8081

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server:
    server.bind((HOST, PORT))
    server.listen(1)

    print(f"Listening on {HOST}:{PORT}")
    conn, addr = server.accept()

    with conn:
        print(f"Connected by {addr}")

        encrypted_request = recv_record(conn)
        request = decrypt_message(encrypted_request)

        print("Received decrypted request:")
        print(request.decode("utf-8"))

        response = (
            "HTTP/1.1 200 OK\\r\\nContent-Type: text/plain\\r\\n"
            "Content-Length: 13\\r\\n\\r\\nhello, client"
        ).encode("utf-8")

        encrypted_response = encrypt_message(response)
        send_record(conn, encrypted_response)
</code></code></pre><p>Now the communication flow changes in an important way.</p><p>Instead of sending readable HTTP-like text directly, the client sends an encrypted record. The server reads the record, decrypts it, and sees the original request.</p><h3>What changes on the wire</h3><p>If you capture this version in Wireshark, the traffic is no longer readable.</p><p>Instead of clear request and response text, you now see opaque binary data.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!1qMA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65774ee7-4444-4135-93a9-ccc576fae9ec_1677x810.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!1qMA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65774ee7-4444-4135-93a9-ccc576fae9ec_1677x810.png 424w, https://substackcdn.com/image/fetch/$s_!1qMA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65774ee7-4444-4135-93a9-ccc576fae9ec_1677x810.png 848w, https://substackcdn.com/image/fetch/$s_!1qMA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65774ee7-4444-4135-93a9-ccc576fae9ec_1677x810.png 1272w, https://substackcdn.com/image/fetch/$s_!1qMA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65774ee7-4444-4135-93a9-ccc576fae9ec_1677x810.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!1qMA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65774ee7-4444-4135-93a9-ccc576fae9ec_1677x810.png" width="1456" height="703" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/65774ee7-4444-4135-93a9-ccc576fae9ec_1677x810.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:703,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:220787,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/192533658?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65774ee7-4444-4135-93a9-ccc576fae9ec_1677x810.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!1qMA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65774ee7-4444-4135-93a9-ccc576fae9ec_1677x810.png 424w, https://substackcdn.com/image/fetch/$s_!1qMA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65774ee7-4444-4135-93a9-ccc576fae9ec_1677x810.png 848w, https://substackcdn.com/image/fetch/$s_!1qMA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65774ee7-4444-4135-93a9-ccc576fae9ec_1677x810.png 1272w, https://substackcdn.com/image/fetch/$s_!1qMA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F65774ee7-4444-4135-93a9-ccc576fae9ec_1677x810.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>So yes, we gained something real.</p><p>Let&#8217;s stop and say exactly what that is.</p><div><hr></div><h2>What encryption actually gave us</h2><p>This first version gives us one meaningful property:</p><p><strong>confidentiality against passive observers</strong></p><p>If someone can only observe the traffic, but cannot modify it, they no longer get the plaintext request and response for free.</p><p>That is already better than raw TCP.</p><p>And this is why &#8220;just add encryption&#8221; feels so convincing. It visibly solves a real problem.</p><p>But that visible success can hide another, more dangerous failure.</p><p>Because a secure channel needs more than secrecy.</p><p>It also needs protection against tampering.</p><p>And we still do not have that.</p><div><hr></div><h2>Step 5 &#8212; Why encryption alone is not enough</h2><p>This is the real point of Part 1.</p><p>We encrypted the messages.</p><p>We did <strong>not</strong> make them trustworthy.</p><p>AES-CTR protects confidentiality, but it does not protect integrity.</p><p>That means an active attacker may be able to modify ciphertext, and those modifications will flow through into the decrypted plaintext.</p><p>Very roughly, CTR mode behaves like this:</p><pre><code><code>ciphertext = plaintext XOR keystream
</code></code></pre><p>So if an attacker changes bits in the ciphertext, the corresponding bits change in the plaintext after decryption.</p><p>That property is called <strong>malleability</strong>.</p><p>And protocol messages are usually predictable enough that this becomes useful to an attacker.</p><p>Our example request already has a very predictable structure:</p><pre><code><code>GET /transfer?to=bob&amp;amount=100 HTTP/1.1
Host: localhost
</code></code></pre><p>The exact bytes of <code>amount=100</code> are not random.</p><p>That predictability is enough to hurt us.</p><h3>A tiny isolated demo</h3><p>We do not need a full man-in-the-middle proxy to show the problem. A small isolated example is enough.</p><pre><code><code># ctr_malleability_demo.py
from crypto import encrypt_message, decrypt_message

original = b"amount=100"
encrypted = encrypt_message(original)

nonce = encrypted[:16]
ciphertext = bytearray(encrypted[16:])

# Change '1' -&gt; '9'
# ASCII '1' = 0x31
# ASCII '9' = 0x39
# Difference = 0x08

index_of_digit = len("amount=")
ciphertext[index_of_digit] ^= 0x08

modified = nonce + bytes(ciphertext)
decrypted = decrypt_message(modified)

print("Original :", original)
print("Modified :", decrypted)
</code></code></pre><p>Output:</p><pre><code><code>Original : b'amount=100'
Modified : b'amount=900'
</code></code></pre><p>And that is the failure.</p><p>The attacker did not need the key.</p><p>They did not need to fully decrypt the message first.</p><p>They only needed the ability to modify the encrypted bytes in transit.</p><p>The receiver then decrypts the modified ciphertext and gets modified plaintext &#8212; without any built-in indication that anything went wrong.</p><p>So even though the message is hidden from passive observers, it is still vulnerable to active tampering.</p><p>That is not a secure protocol.</p><p>That is only encrypted transport.</p><div><hr></div><h2>What is still broken</h2><p>At this point, our fake secure channel still has many serious holes.</p><h3>No integrity protection</h3><p>The receiver cannot detect that the ciphertext was modified.</p><h3>No message authentication</h3><p>The receiver has no cryptographic proof that the message came from the expected sender and arrived unchanged.</p><h3>No replay protection</h3><p>An attacker can capture an encrypted message and replay it later.</p><h3>Static shared key</h3><p>Both sides use one long-term shared key for everything.</p><p>That does not scale, and if it leaks, everything built on top of it collapses.</p><h3>No handshake</h3><p>There is no fresh session establishment. The peers do not negotiate anything. They just start encrypting.</p><h3>No peer identity</h3><p>The client does not really know who it is talking to beyond &#8220;someone who can decrypt with this key.&#8221;</p><p>So yes, we improved something.</p><p>But we are still very far from TLS.</p><p>Good.</p><p>That is exactly what Part 1 should make visible.</p><div><hr></div><h2>Summary</h2><p>In this first part, we built a fake secure channel.</p><p>We started with plain socket communication and saw that everything was fully transparent. Then we wrapped the communication in shared-key encryption with AES-CTR, which gave us confidentiality against passive observers.</p><p>That was real progress.</p><p>But it was not enough.</p><p>Because encrypting a message is not the same thing as protecting the message from being changed. Our channel still accepts modified ciphertext, decrypts it, and trusts the result.</p><p>So the first lesson of this series is simple:</p><p><strong>confidentiality is not integrity</strong></p><p>And if we want something that starts to deserve the name secure protocol, we need both.</p><div><hr></div><h2>Next part &#8212; adding integrity with a MAC</h2><p>So we stop here.</p><p>We now have a channel that can hide bytes from passive observers, but cannot reliably detect tampering.</p><p>In the next part, we will keep the same basic setup and fix the biggest hole we exposed here: we will add a <strong>MAC &#8212; a Message Authentication Code</strong>.</p><p>That will take us from:</p><blockquote><p>&#8220;you probably can&#8217;t read this&#8221;</p></blockquote><p>to:</p><blockquote><p>&#8220;you also can&#8217;t silently change this&#8221;</p></blockquote><p>That still will not be real TLS.</p><p>But it will make our fake secure channel one step less fake.</p><div><hr></div><h2>Final code</h2><p>I&#8217;ll put the full code for this part on GitHub here: <a href="https://github.com/DmytroHuzz/rebuilding_tls/tree/main/part_1">rebuilding_tls</a></p><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Rebuilding TLS From Scratch — My Complete Learning Journey]]></title><description><![CDATA[This collection brings together my attempt to rebuild TLS from first principles]]></description><link>https://www.dmytrohuz.com/p/rebuilding-tls-from-scratch-my-complete</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/rebuilding-tls-from-scratch-my-complete</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Fri, 27 Mar 2026 21:39:41 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!59qZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2268c1-b8d1-42db-9320-6a750a764263_1536x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!59qZ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2268c1-b8d1-42db-9320-6a750a764263_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!59qZ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2268c1-b8d1-42db-9320-6a750a764263_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!59qZ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2268c1-b8d1-42db-9320-6a750a764263_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!59qZ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2268c1-b8d1-42db-9320-6a750a764263_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!59qZ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2268c1-b8d1-42db-9320-6a750a764263_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!59qZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2268c1-b8d1-42db-9320-6a750a764263_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/2d2268c1-b8d1-42db-9320-6a750a764263_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:2967175,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/192355388?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2268c1-b8d1-42db-9320-6a750a764263_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!59qZ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2268c1-b8d1-42db-9320-6a750a764263_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!59qZ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2268c1-b8d1-42db-9320-6a750a764263_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!59qZ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2268c1-b8d1-42db-9320-6a750a764263_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!59qZ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2d2268c1-b8d1-42db-9320-6a750a764263_1536x1024.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>A year ago I wrote a series of articles about how a <a href="https://dev.to/dmytro_huz/building-your-own-web-server-part-1-theory-and-foundations-3kgo">web server works</a>.</p><p>I started from a very primitive version and step by step moved toward the same core ideas modern production servers rely on. That journey was already deep enough on its own. But when I finished it, I had one more thing in mind.</p><p>A small improvement.</p><p>Wrap the server in TLS. Make the communication secure.</p><p>At least, that was how it looked from the outside.</p><p>In reality, that &#8220;small improvement&#8221; turned into a much longer journey than I expected. What looked like one tiny feature hidden behind the familiar lock icon in the browser opened the door into a huge world of cryptography, key exchange, authentication, certificates, trust, protocol design, and many layers of details that usually stay invisible when everything just works.</p><p>And that is exactly why I decided to make this series.</p><p>I did not want to approach TLS as a finished black box. I did not want to start from the RFC and just repeat the names of the protocol messages until they sounded familiar. I wanted to understand what TLS is, why it exists in the form it exists, and why it needs so many moving parts.</p><p>So this series is my attempt to rebuild it.</p><p>Not the full production-ready TLS implementation, of course. But a step-by-step reconstruction of the logic behind it. We will start from something intentionally naive, something that only looks secure, and in each article we will add one missing piece, one new idea, one more reason why real TLS had to become what it is.</p><p>By the end, I want us to arrive at a simplified TLS-like protocol that is close enough to the real thing to make the real thing much easier to understand.</p><p>Because that is still the real goal.</p><p>I do not want to stay forever in toy examples, and I do not think you want that either. In the final part of the series, I want to take everything we built ourselves and map it to the real TLS protocol: what is different, what extra problems the real one solves, why it is structured the way it is, and maybe also how some popular libraries implement it in practice.</p><p>But I really believe that jumping directly into the finished TLS protocol is the wrong way to learn it.</p><p>TLS is one of those technologies where the final design hides the reasons. If you look at the real protocol too early, you see a forest of details: handshakes, certificates, transcript hashes, traffic secrets, record protection, extensions, verification steps. All of them are there for a reason. But those reasons are much easier to understand when you first build the broken versions that fail without them.</p><p>That is the main idea of this series:</p><p><strong>we will learn TLS by first building the wrong thing, and then fixing it step by step.</strong></p><p>So if your goal is to understand the real TLS better, I would strongly recommend following the whole journey and not jumping directly to the last part.</p><p>As I mentioned, TLS is built on many cryptographic ideas. I will explain the important ones when we need them, but I also already wrote a separate series where I rebuild the main cryptographic foundations from scratch:</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;d1c91fa2-1989-4ec7-9c1a-cf6ec139d6b2&quot;,&quot;caption&quot;:&quot;Why this project exists - and why it might matter to you&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Rebuilding Cryptography From Scratch - My Complete Learning Journey (All Parts Inside)&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder - I deconstruct complex systems to first principles and rebuild them into clear engineering mental models, diagrams, and practical tools.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a4a14e6b-5f68-4257-9ee7-e33b8864d56a_1024x1024.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2025-12-01T19:09:39.536Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!SRa_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8a9697d-5837-4c57-947c-4cf941c3bc3d_1024x608.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/rebuilding-cryptography-from-scratch&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:180433391,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:3,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!t_-c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p>So throughout this TLS series, I will link back to those parts whenever we meet a concept that deserves a deeper look.</p><p>This page will be the central hub for the whole project. I will keep it updated as new parts are published, so all articles stay connected in one place.</p><p>I really hope you enjoy this journey as much as I do.</p><div><hr></div><h1><strong>Full Summary of the TLS Series</strong></h1><p><em>This section will be updated as new parts are released.</em></p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;f6554474-5a3b-4fc1-84c6-686cd8bcec9d&quot;,&quot;caption&quot;:&quot;From transparent TCP traffic to encrypted records &#8212; and the first reason TLS needs more than encryption&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;md&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Rebuilding TLS, Part 1 &#8212; Why Encryption Alone Is Not Enough&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder - I deconstruct complex systems to first principles and rebuild them into clear engineering mental models, diagrams, and practical tools.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a4a14e6b-5f68-4257-9ee7-e33b8864d56a_1024x1024.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2026-03-29T18:57:36.417Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!5phz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4f085090-d20e-4850-844a-c79a878be8e6_1536x1024.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/rebuilding-tls-part-1-why-encryption&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:192533658,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:0,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!t_-c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;1ba5526e-56cd-46ad-be1a-ecf7a0624726&quot;,&quot;caption&quot;:&quot;We teach our protocol to detect tampering, make records less naive with sequence numbers, and then switch to the AEAD style used in real systems.&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;md&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Rebuilding TLS, Part 2 &#8212; Adding Integrity to the Channel&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder - I deconstruct complex systems to first principles and rebuild them into clear engineering mental models, diagrams, and practical tools.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a4a14e6b-5f68-4257-9ee7-e33b8864d56a_1024x1024.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2026-04-05T21:40:43.808Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!78kv!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F680242ba-f7c2-4916-990b-5c813987d655_1536x1024.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/rebuilding-tls-part-2-adding-integrity&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:193281237,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:0,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!t_-c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;75e5e3a4-f06f-48f5-947b-d6e777d285e0&quot;,&quot;caption&quot;:&quot;We get rid of the pre-shared key assumption, build a simple key exchange handshake, and discover why key agreement alone still does not give us real TLS.&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;md&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Rebuilding TLS, Part 3 &#8212; Building Our First Handshake&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder - I deconstruct complex systems to first principles and rebuild them into clear engineering mental models, diagrams, and practical tools.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a4a14e6b-5f68-4257-9ee7-e33b8864d56a_1024x1024.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2026-04-19T16:38:45.365Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!Gn-u!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4fb29e52-36ee-433b-a83f-355dc7736265_1536x600.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/rebuilding-tls-part-3-building-our&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:194707545,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:0,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!t_-c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.dmytrohuz.com/subscribe?"><span>Subscribe now</span></a></p><div><hr></div><h1><strong>What this series is really about</strong></h1><p>This is not just a series about TLS.</p><p>It is also another exercise in the same thing I keep coming back to again and again: taking foundational technology that usually appears to us as a finished black box, opening it up, and rebuilding it from simpler parts until it stops feeling magical.</p><p>That was the idea behind the web server series.</p><p>That was the idea behind the cryptography series.</p><p>And now this is the same idea applied to TLS.</p><div><hr></div><h1><strong>Follow the journey</strong></h1><p>This page will stay the central entry point for the whole series.</p><p>If this kind of deep, step-by-step reconstruction of systems is interesting to you, you can subscribe so you do not miss the next parts.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.dmytrohuz.com/subscribe?"><span>Subscribe now</span></a></p><p></p>]]></content:encoded></item><item><title><![CDATA[A Practical Guide to Time for Developers — The Complete Series]]></title><description><![CDATA[Time looks simple until you have to trust it.]]></description><link>https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-746</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-746</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Thu, 19 Mar 2026 21:05:34 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!mg5A!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbef6652f-dc32-4078-ac16-7d4ac73c0392_1024x608.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!mg5A!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbef6652f-dc32-4078-ac16-7d4ac73c0392_1024x608.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!mg5A!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbef6652f-dc32-4078-ac16-7d4ac73c0392_1024x608.png 424w, https://substackcdn.com/image/fetch/$s_!mg5A!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbef6652f-dc32-4078-ac16-7d4ac73c0392_1024x608.png 848w, https://substackcdn.com/image/fetch/$s_!mg5A!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbef6652f-dc32-4078-ac16-7d4ac73c0392_1024x608.png 1272w, https://substackcdn.com/image/fetch/$s_!mg5A!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbef6652f-dc32-4078-ac16-7d4ac73c0392_1024x608.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!mg5A!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbef6652f-dc32-4078-ac16-7d4ac73c0392_1024x608.png" width="1024" height="608" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bef6652f-dc32-4078-ac16-7d4ac73c0392_1024x608.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:608,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!mg5A!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbef6652f-dc32-4078-ac16-7d4ac73c0392_1024x608.png 424w, https://substackcdn.com/image/fetch/$s_!mg5A!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbef6652f-dc32-4078-ac16-7d4ac73c0392_1024x608.png 848w, https://substackcdn.com/image/fetch/$s_!mg5A!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbef6652f-dc32-4078-ac16-7d4ac73c0392_1024x608.png 1272w, https://substackcdn.com/image/fetch/$s_!mg5A!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbef6652f-dc32-4078-ac16-7d4ac73c0392_1024x608.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Time looks simple until you have to trust it.</p><p>We use timestamps everywhere: logs, APIs, databases, schedulers, certificates, metrics, traces, distributed systems, industrial systems. But the moment you start asking basic questions &#8212; <em>what exactly is this timestamp measuring? which clock produced it? how does one machine keep time? how do many machines agree on it?</em> &#8212; the topic becomes much deeper than it first appears.</p><p>This series was my attempt to build a practical mental model of time for developers from first principles all the way down to Linux clocks, NTP, PTP, PHCs, and synchronization tools.</p><p>If you want one entry point into the whole topic, this is it.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-746?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-746?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><h2><strong>What this series covers</strong></h2><p>This series is built as a path:</p><ul><li><p>first, what time actually means in software</p></li><li><p>then, how one computer keeps time</p></li><li><p>then, how many computers synchronize time</p></li><li><p>and finally, how Linux represents all of this in practice</p></li></ul><p>The goal was never to produce a dry reference manual.</p><p>The goal was to make time feel understandable.</p><p>Not just &#8220;I know NTP exists,&#8221; but a real working model of:</p><ul><li><p>what time is</p></li><li><p>how clocks drift</p></li><li><p>why synchronization is hard</p></li><li><p>why timestamp location matters</p></li><li><p>and how Linux exposes all of this in real systems</p></li></ul><h2><strong>The full reading path</strong></h2><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.dmytrohuz.com/subscribe?"><span>Subscribe now</span></a></p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;c40c49bc-90c6-4eac-b83e-aee3b47e2760&quot;,&quot;caption&quot;:&quot;&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;lg&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;A Practical Guide to Time for Developers: Part 1 &#8212; What time is in software (physics + agreements)&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder - I deconstruct complex systems to first principles and rebuild them into clear engineering mental models, diagrams, and practical tools.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a4a14e6b-5f68-4257-9ee7-e33b8864d56a_1024x1024.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2026-03-01T06:30:46.601Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!8hv5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7fbc17c1-cd36-4488-8a63-51f076a67229_1536x672.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:189526403,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:1,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!t_-c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p>This is the foundation.</p><p>If you ever used timestamps without being fully sure what they really mean, start here.</p><p>This part covers the basic language of the topic: what time means in software, why timestamps are not &#8220;time itself,&#8221; what role standards and agreements play, and why the whole subject is more subtle than it first appears.</p><p>This is the part that gives you the mental vocabulary for everything that comes later.</p><div><hr></div><h3></h3><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;785061f6-fbd7-4cdf-b896-43b0116e287c&quot;,&quot;caption&quot;:&quot;&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;lg&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;A Practical Guide to Time for Developers: Part 2 &#8212; How one computer keeps time (Linux)&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder - I deconstruct complex systems to first principles and rebuild them into clear engineering mental models, diagrams, and practical tools.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a4a14e6b-5f68-4257-9ee7-e33b8864d56a_1024x1024.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2026-03-05T21:16:33.261Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!JuSw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0671b0e-b346-4268-aa3f-03463bde5436_1536x672.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-2ec&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:190040669,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:1,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!t_-c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p>Once the theory is clear, the next question is obvious:</p><p>How does one computer actually keep time?</p><p>This part moves inside the machine. It covers the clocks and mechanisms Linux uses to track time, including the RTC, system time, counters, and the distinction between different clock sources and time domains.</p><p>If Part 1 explains what time means, Part 2 explains how a single system turns that idea into something measurable and usable.</p><div><hr></div><h3></h3><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;90da410a-7e34-4143-aee5-98eb6d0d5712&quot;,&quot;caption&quot;:&quot;&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;lg&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;A Practical Guide to Time for Developers: Part 3 &#8212; How Computers Share Time&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder - I deconstruct complex systems to first principles and rebuild them into clear engineering mental models, diagrams, and practical tools.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a4a14e6b-5f68-4257-9ee7-e33b8864d56a_1024x1024.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2026-03-16T15:42:35.677Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!0JCc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ff7fad6-3567-42cf-a8e4-f948866f3fd5_1000x420.webp&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-ec8&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:191125080,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:0,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!t_-c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p>A single computer can keep time locally.</p><p>A distributed system has a harder problem: many machines must keep time together.</p><p>This part is about synchronization. Why simply setting clocks once does not work. Why drift makes synchronization a continuous process. How NTP and PTP approach the problem. And why the quality of synchronization depends not only on the protocol, but also on where timestamps are taken.</p><p>This is where time stops being a local machine detail and becomes a systems problem.</p><div><hr></div><h3></h3><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;35cb7f76-68b8-40d1-9ed4-b6242e3185b8&quot;,&quot;caption&quot;:&quot;&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;lg&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;A Practical Guide to Time for Developers: Part 4 -The Linux Time Sync Cheat Sheet&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder - I deconstruct complex systems to first principles and rebuild them into clear engineering mental models, diagrams, and practical tools.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a4a14e6b-5f68-4257-9ee7-e33b8864d56a_1024x1024.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2026-03-19T16:36:51.707Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!MEgQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6acd666-fe66-4163-a98f-62e564fa6c7e_1536x672.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-314&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:191493632,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:0,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!t_-c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p>This is the practical payoff.</p><p>It turns the concepts from the whole series into the Linux view of the world:</p><ul><li><p>RTC</p></li><li><p>system clock</p></li><li><p>PHC</p></li><li><p>NTP</p></li><li><p>PTP</p></li><li><p>ptp4l</p></li><li><p>phc2sys</p></li><li><p>and the usual synchronization paths between them</p></li></ul><p>This is the compact field guide version &#8212; the part you can actually bookmark and return to when you need to inspect, operate, or debug time synchronization on Linux.</p><div><hr></div><h2><strong>Interactive visuals and supporting materials</strong></h2><p>Along the way, I also started building visual and interactive explanations for some of the harder ideas, such as:</p><ul><li><p>clock drift</p></li><li><p>synchronization by timestamp exchange</p></li><li><p>NTP principles</p></li><li><p>PTP principles</p></li><li><p>software vs hardware timestamping</p></li></ul><p>I want this series to be more than just text. Time is easier to understand when you can see it move.</p><h2><strong>Who this series is for</strong></h2><p>This series should be useful if you work with:</p><ul><li><p>backend or distributed systems</p></li><li><p>Linux and infrastructure</p></li><li><p>observability and logs</p></li><li><p>event ordering</p></li><li><p>industrial or measurement systems</p></li><li><p>networking</p></li><li><p>NTP/PTP</p></li><li><p>or just any system where timestamps need to be trusted</p></li></ul><p>In other words: if time can break your system, this topic is worth understanding properly.</p><h2><strong>Why I wrote this</strong></h2><p>Time is one of the most used and least understood parts of software.</p><p>Most of us interact with it every day, but only occasionally stop to ask what is actually happening underneath. I wanted to fix that for myself first &#8212; and then turn that learning process into something practical and usable for other engineers.</p><p>This series is the result.</p><h2><strong>Final note</strong></h2><p>If you made it through the whole series, you did not just read a few posts about clocks.</p><p>You built a real mental model of time in computing &#8212; from first principles, to clocks inside a machine, to synchronization across networks, to the actual Linux entities and tools that make it work in practice.</p><p>That already puts you ahead of most engineers who touch these systems.</p><p>You now know enough to stop treating time as a mysterious background feature and start seeing it for what it really is:</p><p><strong>infrastructure, measurement, coordination, and engineering.</strong></p><p>Bookmark this page. I&#8217;ll keep it as the main entry point for the whole series.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[A Practical Guide to Time for Developers: Part 4 -The Linux Time Sync Cheat Sheet]]></title><description><![CDATA[RTC, system clock, PHC, NTP, PTP, ptp4l, and phc2sys &#8212; the practical map of how time works in Linux]]></description><link>https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-314</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-314</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Thu, 19 Mar 2026 16:36:51 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!MEgQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6acd666-fe66-4163-a98f-62e564fa6c7e_1536x672.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MEgQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6acd666-fe66-4163-a98f-62e564fa6c7e_1536x672.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MEgQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6acd666-fe66-4163-a98f-62e564fa6c7e_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!MEgQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6acd666-fe66-4163-a98f-62e564fa6c7e_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!MEgQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6acd666-fe66-4163-a98f-62e564fa6c7e_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!MEgQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6acd666-fe66-4163-a98f-62e564fa6c7e_1536x672.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MEgQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6acd666-fe66-4163-a98f-62e564fa6c7e_1536x672.png" width="1456" height="637" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b6acd666-fe66-4163-a98f-62e564fa6c7e_1536x672.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:637,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1371588,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/191493632?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6acd666-fe66-4163-a98f-62e564fa6c7e_1536x672.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!MEgQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6acd666-fe66-4163-a98f-62e564fa6c7e_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!MEgQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6acd666-fe66-4163-a98f-62e564fa6c7e_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!MEgQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6acd666-fe66-4163-a98f-62e564fa6c7e_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!MEgQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb6acd666-fe66-4163-a98f-62e564fa6c7e_1536x672.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>If you got here and made it through the previous articles, you have already done the hard part. You are basically a time guru now.</p><p>You know <a href="https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers">what time means in computing</a>, <a href="https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-2ec">how a machine keeps it</a>, why clocks drift, <a href="https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-ec8">how synchronization works, and why timestamp location matters.</a> That is already more than most people ever learn about this topic.</p><p>Now let&#8217;s compress all of that into the Linux view of the world.</p><p>This is the top of the iceberg: a small set of clocks, commands, and tools that represent most of the concepts we have been building up across the series.</p><p>Think of this as the practical cheat sheet &#8212; the 90% version. The one you can use to inspect clocks, understand what is synchronized to what, and handle most everyday Linux time-sync tasks without drowning in documentation.</p><p><em><strong>This is probably the part you will want to bookmark.</strong></em></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-314?utm_source=substack&utm_medium=email&utm_content=share&action=share&quot;,&quot;text&quot;:&quot;Share&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-314?utm_source=substack&utm_medium=email&utm_content=share&action=share"><span>Share</span></a></p><div><hr></div><h2><strong>The three main clock entities in Linux</strong></h2><p>When people say &#8220;Linux time,&#8221; they often mean one thing. In reality, Linux commonly deals with at least three different clock entities:</p><ul><li><p><strong>system time</strong></p></li><li><p><strong>RTC</strong></p></li><li><p><strong>PHC</strong></p></li></ul><p>They serve different purposes.</p><div><hr></div><h2><strong>1. System time</strong></h2><p>This is the normal wall-clock time used by most applications.</p><p>It is what you usually see when you run:</p><pre><code><code>date
</code></code></pre><p>or:</p><pre><code><code>timedatectl
</code></code></pre><p>Conceptually, this is the kernel&#8217;s main wall clock, commonly associated with CLOCK_REALTIME.</p><p>This is the clock used by:</p><ul><li><p>most user-space applications</p></li><li><p>logs</p></li><li><p>system services</p></li><li><p>everyday time queries</p></li></ul><h3><strong>Quick check</strong></h3><pre><code><code>date
timedatectl
</code></code></pre><h3><strong>Mental model</strong></h3><pre><code><code>System clock = the main OS wall clock
</code></code></pre><div><hr></div><h2><strong>2. RTC (Real-Time Clock)</strong></h2><p>The RTC is the battery-backed hardware clock on the motherboard.</p><p>Its main job is simple: keep time while the machine is powered off.</p><p>Linux often uses it during boot to initialize system time, and may update it again later from system time. But the RTC is usually <strong>not</strong> the main precision synchronization clock during normal operation.</p><h3><strong>Quick check</strong></h3><pre><code><code>sudo hwclock --show
timedatectl
</code></code></pre><h3><strong>Common operations</strong></h3><p>Copy system time to RTC:</p><pre><code><code>sudo hwclock --systohc
</code></code></pre><p>Copy RTC to system time:</p><pre><code><code>sudo hwclock --hctosys
</code></code></pre><h3><strong>Mental model</strong></h3><pre><code><code>RTC = persistent clock for boot/shutdown
</code></code></pre><div><hr></div><h2><strong>3. PHC (PTP Hardware Clock)</strong></h2><p>A PHC is a hardware clock exposed by a PTP-capable network interface.</p><p>This is where Linux gets especially interesting.</p><p>A PHC lives on the NIC, much closer to the real transmit/receive event than the normal system clock. That is why it matters for precise synchronization.</p><p>PHCs usually appear as device files like:</p><pre><code><code>/dev/ptp0
/dev/ptp1
</code></code></pre><h3><strong>Quick check</strong></h3><p>List PHC devices:</p><pre><code><code>ls -l /dev/ptp*
</code></code></pre><p>Check which PHC belongs to a NIC and whether hardware timestamping is supported:</p><pre><code><code>ethtool -T eth0
</code></code></pre><h3><strong>Mental model</strong></h3><pre><code><code>PHC = hardware clock on the NIC, used for precise packet timing
</code></code></pre><div><hr></div><h2><strong>One picture: how they relate</strong></h2><pre><code><code>RTC
  |
  | used mainly at boot / shutdown
  v
System clock (CLOCK_REALTIME)
  ^
  |
  | phc2sys can sync between them
  |
PHC (/dev/ptpX on NIC)
  ^
  |
  | ptp4l syncs PHC to PTP network
  |
PTP network / Grandmaster
</code></code></pre><p>A rough summary:</p><ul><li><p><strong>RTC</strong> keeps time while power is off</p></li><li><p><strong>system clock</strong> is what most software reads</p></li><li><p><strong>PHC</strong> is the precision clock on the NIC</p></li></ul><div><hr></div><h2><strong>How Linux syncs time with NTP</strong></h2><p>In the NTP world, Linux usually synchronizes the <strong>system clock</strong>.</p><p>Common tools:</p><ul><li><p>chronyd</p></li><li><p>systemd-timesyncd</p></li><li><p>older setups may use ntpd</p></li></ul><h3><strong>Check whether NTP is active</strong></h3><pre><code><code>timedatectl
</code></code></pre><h3><strong>If you use chrony</strong></h3><p>Show synchronization state:</p><pre><code><code>chronyc tracking
</code></code></pre><p>Show time sources:</p><pre><code><code>chronyc sources -v
</code></code></pre><h3><strong>Mental model</strong></h3><pre><code><code>NTP servers
   |
   v
chronyd / timesyncd / ntpd
   |
   v
System clock
</code></code></pre><p>In other words, NTP usually targets the <strong>system clock</strong>, not the PHC.</p><div><hr></div><h2><strong>How Linux syncs time with PTP</strong></h2><p>In the PTP world, Linux often follows a two-step path:</p><ol><li><p>synchronize the <strong>PHC</strong> to the PTP network</p></li><li><p>synchronize the <strong>system clock</strong> to that PHC</p></li></ol><p>The two main tools are:</p><ul><li><p>ptp4l</p></li><li><p>phc2sys</p></li></ul><div><hr></div><h2><strong>ptp4l: syncing the NIC-side clock</strong></h2><p>ptp4l speaks PTP on the network and usually synchronizes the PHC associated with the interface.</p><p>Typical example:</p><pre><code><code>sudo ptp4l -i eth0 -m
</code></code></pre><p>Meaning:</p><ul><li><p>i eth0 &#8594; use interface eth0</p></li><li><p>m &#8594; print log messages to stdout</p></li></ul><h3><strong>Mental model</strong></h3><pre><code><code>PTP Grandmaster / network
        |
        v
      ptp4l
        |
        v
PHC on eth0 (/dev/ptpX)
</code></code></pre><p>This is usually where the NIC-side precision timing gets aligned to the network timing domain.</p><div><hr></div><h2><strong>phc2sys: syncing one local clock to another</strong></h2><p>Once the PHC is synchronized, the rest of the machine may still be reading the system clock.</p><p>That is why phc2sys exists.</p><p>Its job is to synchronize one local clock to another.</p><p>The most common use is:</p><p><strong>PHC &#8594; system clock</strong></p><p>Example:</p><pre><code><code>sudo phc2sys -s eth0 -c CLOCK_REALTIME -m
</code></code></pre><p>Meaning:</p><ul><li><p>s eth0 &#8594; source is the PHC associated with eth0</p></li><li><p>c CLOCK_REALTIME &#8594; target is the system clock</p></li><li><p>m &#8594; print status</p></li></ul><h3><strong>Mental model</strong></h3><pre><code><code>PHC on NIC
   |
   v
phc2sys
   |
   v
System clock
</code></code></pre><p>This is the classic Linux PTP flow.</p><div><hr></div><h2><strong>The classic Linux PTP pipeline</strong></h2><pre><code><code>PTP Grandmaster
      |
      v
  [ network ]
      |
      v
NIC hardware timestamping
      |
      v
    ptp4l
      |
      v
PHC (/dev/ptp0) synchronized to PTP domain
      |
    phc2sys
      |
      v
System clock (CLOCK_REALTIME)
      |
      v
Applications / logs / services
</code></code></pre><p>This is one of the most useful diagrams to keep in your head.</p><div><hr></div><h2><strong>PHC to system clock, or system clock to PHC?</strong></h2><p>In precision setups, the common direction is:</p><pre><code><code>PHC &#8594; system clock
</code></code></pre><p>because the PHC is closer to the wire and usually the better timing source in a PTP environment.</p><p>That is why this is a common command:</p><pre><code><code>sudo phc2sys -s eth0 -c CLOCK_REALTIME -m
</code></code></pre><p>But phc2sys is more general than that. It can synchronize clocks in other directions too.</p><div><hr></div><h2><code>ts2phc:</code>Syncing PHCs</h2><p>Linux can synchronize hardware clocks too, but the right tool depends on the synchronization path.</p><p>If the goal is to make the <strong>system clock</strong> follow a PHC, the usual tool is <code>phc2sys</code>. If the goal is to synchronize <strong>one or more PHCs from an external timestamp source</strong> such as PPS or GNSS, the usual tool is <code>ts2phc</code>. <code>ts2phc</code> is specifically designed to synchronize PHCs to external timestamp signals, and it can distribute one source to multiple PHCs.</p><p>Conceptually:</p><pre><code>External PPS / GNSS / timestamp source &#8594; ts2phc &#8594; one or more PHCs
PHC &#8594; phc2sys &#8594; system clock</code></pre><p>A typical <code>ts2phc</code> command looks like this:</p><pre><code>sudo ts2phc -s eth0 -c eth1 -m</code></pre><p>In this form:</p><ul><li><p><code>-s eth0</code> selects the source clock or source interface,<br></p></li><li><p><code>-c eth1</code> selects a PHC to synchronize,<br></p></li><li><p><code>-m</code> prints log messages to stdout. The tool also allows multiple <code>-c</code> options if you want to synchronize more than one PHC from the same source. <br></p></li></ul><p>This is less common than PHC-to-system-clock synchronization, but it matters in systems where multiple hardware clocks need to follow the same precise external reference. </p><div><hr></div><h2><strong>Where software timestamping fits</strong></h2><p>Not every NIC has hardware timestamping. Not every system needs that level of precision.</p><p>Linux can still synchronize clocks using software timestamps, and for many use cases that is completely fine.</p><p>But the practical tradeoff remains the same:</p><ul><li><p><strong>hardware timestamping</strong> gives measurements closer to the real wire event</p></li><li><p><strong>software timestamping</strong> includes more delay and variation from the OS path</p></li></ul><p>That is why software timestamping usually belongs to a looser precision budget, while hardware timestamping is what unlocks much tighter synchronization.</p><div><hr></div><h2><strong>The most useful commands at a glance</strong></h2><h3><strong>Check system time</strong></h3><pre><code><code>date
timedatectl
</code></code></pre><h3><strong>Check RTC</strong></h3><pre><code><code>sudo hwclock --show
</code></code></pre><h3><strong>List PHC devices</strong></h3><pre><code><code>ls -l /dev/ptp*
</code></code></pre><h3><strong>Check NIC timestamping support</strong></h3><pre><code><code>ethtool -T eth0
</code></code></pre><h3><strong>Run PTP on an interface</strong></h3><pre><code><code>sudo ptp4l -i eth0 -m
</code></code></pre><h3><strong>Sync system clock from PHC</strong></h3><pre><code><code>sudo phc2sys -s eth0 -c CLOCK_REALTIME -m
</code></code></pre><h3><strong>Sync PHCs</strong></h3><pre><code><code>sudo ts2phc -s eth0 -c eth1 -m</code></code></pre><h3><strong>Check chrony state</strong></h3><pre><code><code>chronyc tracking
chronyc sources -v
</code></code></pre><div><hr></div><h2><strong>The one table worth remembering</strong></h2><pre><code><code>RTC ------------------&gt; system time at boot / shutdown
NTP daemon -----------&gt; system clock
ptp4l ----------------&gt; PHC
phc2sys --------------&gt; PHC &lt;-&gt; system clock
External PPS / GNSS / timestamp source &#8594; ts2phc &#8594; one or more PHCs
hwclock --systohc ----&gt; system clock -&gt; RTC
hwclock --hctosys ----&gt; RTC -&gt; system clock
</code></code></pre><div><hr></div><h2><strong>The biggest practical lesson</strong></h2><p>When somebody says:</p><p><strong>&#8220;The machine is synchronized.&#8221;</strong></p><p>That is usually too vague.</p><p>The useful follow-up question is:</p><p><strong>Which clock is synchronized?</strong></p><ul><li><p>the RTC?</p></li><li><p>the system clock?</p></li><li><p>the PHC?</p></li><li><p>and which one is the application actually using?</p></li></ul><p>That one question prevents a lot of confusion.</p><div><hr></div><h2><strong>One-screen summary</strong></h2><pre><code><code>RTC
- battery-backed motherboard clock
- keeps time while machine is off
- checked with: hwclock --show

System clock
- main OS wall clock
- used by most applications
- checked with: date, timedatectl
- synchronized by: NTP or by phc2sys from PHC

PHC
- hardware clock on a PTP-capable NIC
- represented as: /dev/ptpX
- checked with: ls /dev/ptp*, ethtool -T eth0
- synchronized by: ptp4l

Typical Linux PTP flow
PTP network -&gt; ptp4l -&gt; PHC -&gt; phc2sys -&gt; system clock
</code></code></pre><div><hr></div><h2><strong>Final note</strong></h2><p>If you made it all the way here, you did not just read a few articles about clocks.</p><p>You built a real mental model of time in computing &#8212; from first principles, to clocks inside a machine, to synchronization across networks, to the actual Linux entities and tools that make it work in practice.</p><p>That already puts you far ahead of most engineers who touch these systems.</p><p>You now know enough to stop treating time as a mysterious background feature and start seeing it for what it really is: infrastructure, measurement, coordination, and engineering.</p><p>And it was the final piece: a practical cheat sheet you can actually use. Bookmark it. Return to it. Break things with it. Fix things with it.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[A Practical Guide to Time for Developers: Part 3 — How Computers Share Time]]></title><description><![CDATA[How computers synchronize time with NTP, PTP, and timestamping in Linux]]></description><link>https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-ec8</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-ec8</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Mon, 16 Mar 2026 15:42:35 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!0JCc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ff7fad6-3567-42cf-a8e4-f948866f3fd5_1000x420.webp" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!0JCc!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ff7fad6-3567-42cf-a8e4-f948866f3fd5_1000x420.webp" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!0JCc!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ff7fad6-3567-42cf-a8e4-f948866f3fd5_1000x420.webp 424w, https://substackcdn.com/image/fetch/$s_!0JCc!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ff7fad6-3567-42cf-a8e4-f948866f3fd5_1000x420.webp 848w, https://substackcdn.com/image/fetch/$s_!0JCc!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ff7fad6-3567-42cf-a8e4-f948866f3fd5_1000x420.webp 1272w, https://substackcdn.com/image/fetch/$s_!0JCc!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ff7fad6-3567-42cf-a8e4-f948866f3fd5_1000x420.webp 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!0JCc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ff7fad6-3567-42cf-a8e4-f948866f3fd5_1000x420.webp" width="1000" height="420" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/1ff7fad6-3567-42cf-a8e4-f948866f3fd5_1000x420.webp&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:420,&quot;width&quot;:1000,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:139314,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/webp&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/191125080?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ff7fad6-3567-42cf-a8e4-f948866f3fd5_1000x420.webp&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!0JCc!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ff7fad6-3567-42cf-a8e4-f948866f3fd5_1000x420.webp 424w, https://substackcdn.com/image/fetch/$s_!0JCc!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ff7fad6-3567-42cf-a8e4-f948866f3fd5_1000x420.webp 848w, https://substackcdn.com/image/fetch/$s_!0JCc!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ff7fad6-3567-42cf-a8e4-f948866f3fd5_1000x420.webp 1272w, https://substackcdn.com/image/fetch/$s_!0JCc!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F1ff7fad6-3567-42cf-a8e4-f948866f3fd5_1000x420.webp 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2><strong>Intro</strong></h2><p>Every action film has that scene just before the military operation begins.</p><p>&#8220;Let&#8217;s sync our watches,&#8221; the captain says.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>The idea is simple: if every stage of the plan depends on precise coordination, everyone involved has to act in sync and according to the same timeline.</p><p>A while ago, we started our journey with a practical goal: synchronizing the time of many computers. To get there, we first had to understand what time actually is and learn the basic glossary needed to speak the language of this problem and its solutions. In the first part (<a href="https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers">https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers</a>), we explored the foundations of time itself. In the second (<a href="https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-2ec">https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-2ec</a>), we looked at how time is kept and tracked inside a single computer. Now we are finally ready to move to the next step: how many computers share time with each other.</p><p>Keeping precise time across many computers is not unusual or exotic. In fact, the opposite is true. Distributed systems, industrial networks, telecom infrastructure, financial systems, and measurement environments often involve hundreds or thousands of devices that must stay synchronized within a clearly defined precision budget.</p><p>Let&#8217;s imagine a wind farm. Each turbine is around 120 meters tall and has a warning light at the top. To make the turbines visible to planes at night, the lights should blink every second. And to make the whole field clearly visible as one coordinated structure, those lights should blink simultaneously.</p><p>How can we make that happen?</p><p>The obvious answer is: the turbines need synchronized clocks.</p><p>But how we can keep them in sync for hundreds and thousands devices with amazing accuracy?</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!huJQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd454b3f2-43d7-45e4-8300-54fdef86c487_1536x1024.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!huJQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd454b3f2-43d7-45e4-8300-54fdef86c487_1536x1024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!huJQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd454b3f2-43d7-45e4-8300-54fdef86c487_1536x1024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!huJQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd454b3f2-43d7-45e4-8300-54fdef86c487_1536x1024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!huJQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd454b3f2-43d7-45e4-8300-54fdef86c487_1536x1024.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!huJQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd454b3f2-43d7-45e4-8300-54fdef86c487_1536x1024.jpeg" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/d454b3f2-43d7-45e4-8300-54fdef86c487_1536x1024.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:336712,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/191125080?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd454b3f2-43d7-45e4-8300-54fdef86c487_1536x1024.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!huJQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd454b3f2-43d7-45e4-8300-54fdef86c487_1536x1024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!huJQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd454b3f2-43d7-45e4-8300-54fdef86c487_1536x1024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!huJQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd454b3f2-43d7-45e4-8300-54fdef86c487_1536x1024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!huJQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fd454b3f2-43d7-45e4-8300-54fdef86c487_1536x1024.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Let&#8217;s see! </p><h2><strong>Just sync the clocks once?</strong></h2><p>Let&#8217;s start with the most obvious idea: set the same time on all clocks once, and the problem is solved.</p><p>Unfortunately, it does not work that way.</p><p>Every clock has physical behavior behind it. Its frequency is affected by things like oscillator quality, temperature, aging, and other environmental factors. As a result, every clock drifts in its own way. Some also exhibit short-term fluctuations, often described as wander. These effects cannot be fully eliminated, and in practice they mean that two clocks will slowly diverge even if they start perfectly aligned.</p><p>That turns synchronization from a one-time setup task into a continuous process.</p><p>Clocks do not just need to be set. They need to be kept aligned over time. In practice, that means measuring the difference between clocks again and again, then adjusting their time and, more importantly, their rate so that they do not immediately drift apart again.</p><p>You can see how quickly clocks with different rates and wander fall out of sync, even when they start at exactly the same time:</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!zdtw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b507daa-5124-42c2-93c7-fb7792096dc3_644x512.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zdtw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b507daa-5124-42c2-93c7-fb7792096dc3_644x512.gif 424w, https://substackcdn.com/image/fetch/$s_!zdtw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b507daa-5124-42c2-93c7-fb7792096dc3_644x512.gif 848w, https://substackcdn.com/image/fetch/$s_!zdtw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b507daa-5124-42c2-93c7-fb7792096dc3_644x512.gif 1272w, https://substackcdn.com/image/fetch/$s_!zdtw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b507daa-5124-42c2-93c7-fb7792096dc3_644x512.gif 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!zdtw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b507daa-5124-42c2-93c7-fb7792096dc3_644x512.gif" width="644" height="512" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9b507daa-5124-42c2-93c7-fb7792096dc3_644x512.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:512,&quot;width&quot;:644,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:510655,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/191125080?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b507daa-5124-42c2-93c7-fb7792096dc3_644x512.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zdtw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b507daa-5124-42c2-93c7-fb7792096dc3_644x512.gif 424w, https://substackcdn.com/image/fetch/$s_!zdtw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b507daa-5124-42c2-93c7-fb7792096dc3_644x512.gif 848w, https://substackcdn.com/image/fetch/$s_!zdtw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b507daa-5124-42c2-93c7-fb7792096dc3_644x512.gif 1272w, https://substackcdn.com/image/fetch/$s_!zdtw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9b507daa-5124-42c2-93c7-fb7792096dc3_644x512.gif 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Feel free to explore the interactive simulation I created for this exact scenario and see how quickly clocks drift out of sync: https://dmytrohuzz.github.io/interactive_demo/clock_sync/index.html</p><h2><strong>From setting time to synchronization</strong></h2><p>Once we accept that clocks drift, a one-time setup stops looking like a real solution. Time is not something you assign once. It is something you keep aligned.</p><p>In practice, synchronization is a feedback loop. A machine compares its local clock to some reference, estimates the difference, adjusts its own clock, and repeats the process again and again.</p><p>The difficult part is that machines cannot read each other&#8217;s clocks directly. They can only communicate over a network, and the network adds delay and uncertainty. So synchronization protocols work indirectly: they exchange messages with timestamps and use those timestamps to estimate the relationship between clocks.</p><p>At the center of that estimate are two questions:</p><ul><li><p>how far apart are the clocks?</p></li><li><p>how much of the observed difference comes from network delay rather than clock error?</p></li></ul><p>This sounds simple in theory, but the key idea only becomes clear once we walk through it step by step.</p><p>A basic synchronization exchange gives us four timestamps:</p><ul><li><p><strong>t1</strong> &#8212; the client sends a request</p></li><li><p><strong>t2</strong> &#8212; the server receives that request</p></li><li><p><strong>t3</strong> &#8212; the server sends a response</p></li><li><p><strong>t4</strong> &#8212; the client receives the response</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Dzv1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc664f7c-78b9-461f-a226-36da449bde39_644x838.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Dzv1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc664f7c-78b9-461f-a226-36da449bde39_644x838.gif 424w, https://substackcdn.com/image/fetch/$s_!Dzv1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc664f7c-78b9-461f-a226-36da449bde39_644x838.gif 848w, https://substackcdn.com/image/fetch/$s_!Dzv1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc664f7c-78b9-461f-a226-36da449bde39_644x838.gif 1272w, https://substackcdn.com/image/fetch/$s_!Dzv1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc664f7c-78b9-461f-a226-36da449bde39_644x838.gif 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Dzv1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc664f7c-78b9-461f-a226-36da449bde39_644x838.gif" width="644" height="838" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/fc664f7c-78b9-461f-a226-36da449bde39_644x838.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:838,&quot;width&quot;:644,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:493203,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/191125080?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc664f7c-78b9-461f-a226-36da449bde39_644x838.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Dzv1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc664f7c-78b9-461f-a226-36da449bde39_644x838.gif 424w, https://substackcdn.com/image/fetch/$s_!Dzv1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc664f7c-78b9-461f-a226-36da449bde39_644x838.gif 848w, https://substackcdn.com/image/fetch/$s_!Dzv1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc664f7c-78b9-461f-a226-36da449bde39_644x838.gif 1272w, https://substackcdn.com/image/fetch/$s_!Dzv1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ffc664f7c-78b9-461f-a226-36da449bde39_644x838.gif 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p></p><p>These four timestamps are the heart of the whole mechanism. Once this pattern becomes intuitive, the rest of the synchronization topic becomes much easier to follow.</p><p>Now imagine the exchange from the client&#8217;s point of view.</p><p>The client sends a request at local time <strong>t1 = 00:00</strong>.</p><p>Later, it receives the response at local time <strong>t4 = 00:04</strong>.</p><p>Inside that response, the server includes its own timestamps:</p><ul><li><p>it received the request at <strong>t2 = 00:06</strong></p></li><li><p>it sent the response at <strong>t3 = 00:06</strong></p></li></ul><p>At first glance, this looks strange. How can the server receive the request at 00:06 if the client sent it at 00:00, and the whole round trip took only four seconds on the client side?</p><p>The answer is simple: <strong>t1 and t2 do not belong to the same timeline</strong>.</p><p>The client clock and the server clock are different local views of time. What synchronization tries to estimate is the relation between those two timelines. In other words, it tries to answer this question:</p><p><strong>If the client sees one moment as 00:00, what does the server call that same moment?</strong></p><p>That relationship is what we call <strong>offset</strong>.</p><p>This is the most important insight in the whole topic: the difference t2 - t1 does not represent only network delay. It contains two things mixed together:</p><ul><li><p>packet travel time</p></li><li><p>clock offset between client and server</p></li></ul><p>A useful way to think about it is with time zones.</p><p></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EBqI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca685d13-2c0c-45ca-85cf-472d423dab3a_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EBqI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca685d13-2c0c-45ca-85cf-472d423dab3a_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!EBqI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca685d13-2c0c-45ca-85cf-472d423dab3a_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!EBqI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca685d13-2c0c-45ca-85cf-472d423dab3a_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!EBqI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca685d13-2c0c-45ca-85cf-472d423dab3a_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!EBqI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca685d13-2c0c-45ca-85cf-472d423dab3a_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ca685d13-2c0c-45ca-85cf-472d423dab3a_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1751180,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/191125080?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca685d13-2c0c-45ca-85cf-472d423dab3a_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EBqI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca685d13-2c0c-45ca-85cf-472d423dab3a_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!EBqI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca685d13-2c0c-45ca-85cf-472d423dab3a_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!EBqI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca685d13-2c0c-45ca-85cf-472d423dab3a_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!EBqI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fca685d13-2c0c-45ca-85cf-472d423dab3a_1536x1024.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>Imagine you leave one city at local time 00:00, travel to another city, and arrive when the local clock there shows 14:00. Then you immediately turn around and come back, arriving home when your original city&#8217;s clock shows 20:00.</p><p>Now suppose the travel time is the same in both directions.</p><p>The first leg, from your city to the other one, includes:</p><p><strong>travel time + time-zone difference</strong></p><p>The return leg includes:</p><p><strong>travel time - time-zone difference</strong></p><p>So if the outward journey appears shorter or longer than the return journey, that difference tells you something about the offset between the two local clocks.</p><p>This is exactly what synchronization protocols exploit.</p><p>Under the usual symmetric-delay assumption, the offset can be estimated as:</p><p><code>offset = ((t2 - t1) + (t3 - t4)) /2</code></p><p>and the round-trip delay as:</p><p><code>delay = (t4 - t1) - (t3 - t2)</code></p><p>The first formula separates clock offset from the two directions of travel. The second removes the server&#8217;s processing time and leaves only the network round-trip time.</p><p>So synchronization is not about directly copying time from one machine to another. It is about observing message exchanges, separating delay from clock difference, and then correcting the local clock based on that estimate.</p><p>That is the core idea behind the whole topic.</p><p>Feel free to play with the simulation here:</p><p><a href="https://dmytrohuzz.github.io/interactive_demo/clock_sync/clock_sync_explained">https://dmytrohuzz.github.io/interactive_demo/clock_sync/clock_sync_explained</a></p><div><hr></div><h2><strong>NTP and PTP: two ways to synchronize clocks</strong></h2><p>Over time, two major protocol families became the standard answers to the synchronization problem: <strong>NTP</strong> and <strong>PTP</strong>.</p><p>Both solve the same core problem: a machine cannot read another machine&#8217;s clock directly, so it has to infer the difference by exchanging timestamped messages over a network. From those timestamps, it estimates clock offset and network delay, then adjusts the local clock toward a reference.</p><p>The difference is not the basic idea, but the precision target and the environment they are designed for.</p><h3><strong>NTP: practical synchronization for general systems</strong></h3><p><strong>NTP</strong> &#8212; the Network Time Protocol &#8212; is the general-purpose approach. It is designed to keep clocks reasonably aligned across ordinary systems and ordinary networks.</p><p>Its main principle is simple: a client exchanges request and response messages with a time server, records timestamps on both sides, estimates round-trip delay and clock offset, and then gradually disciplines its own clock. It repeats this process continuously, using multiple measurements to smooth out noise and avoid reacting too aggressively to one bad sample.</p><p>That makes NTP a good fit for:</p><ul><li><p>logs and observability</p></li><li><p>authentication and certificate validation</p></li><li><p>scheduled jobs</p></li><li><p>general wall-clock correctness across servers and infrastructure</p></li></ul><p>NTP does not assume a perfect network. It is built for real environments, where delays vary, paths are not perfectly symmetric, and hosts are under changing load. Its strength is robustness, not extreme precision.</p><p>[<a href="https://dmytrohuzz.github.io/interactive_demo/clock_sync/ntp_visualized.html">Interactive Demo</a>]</p><h3><strong>PTP: tighter synchronization for controlled environments</strong></h3><p><strong>PTP</strong> &#8212; the Precision Time Protocol &#8212; targets systems where much tighter agreement between clocks is required.</p><p>Its principle is similar to NTP: devices exchange timing messages, estimate offset and delay, and adjust local clocks. But PTP is designed for local precision networks, where the entire timing path is treated more carefully. In practice, this often means hardware timestamping, PTP-aware switches, and a dedicated timing hierarchy built around a grandmaster clock distributing time to other devices.</p><p>PTP is commonly used in:</p><ul><li><p>industrial and automation systems</p></li><li><p>telecom networks</p></li><li><p>audio and video systems</p></li><li><p>measurement systems</p></li><li><p>finance</p></li><li><p>power and substation environments</p></li></ul><p>PTP is not just &#8220;a more accurate NTP.&#8221; It usually operates in a different class of environment, with tighter timing requirements and more deliberate infrastructure support.</p><p>[<a href="https://dmytrohuzz.github.io/interactive_demo/clock_sync/ptp_visualized.html">Interactive Demo</a>]</p><h3><strong>Different tools for different timing budgets</strong></h3><p>So NTP and PTP are not really rivals. They are different engineering choices.</p><p>If the goal is to keep ordinary systems aligned to real time well enough for general infrastructure behavior, NTP is usually the right tool.</p><p>If the goal is to keep clocks tightly aligned in a local timing domain where timing quality directly affects correctness, event ordering, or measurement precision, PTP is often the better fit.</p><p>The key point is this: both protocols depend on timestamp exchange, but the quality of synchronization depends heavily on how those timestamps are produced.</p><p>And that leads to the next question: <strong>where exactly was the timestamp taken?</strong></p><p>This is where timestamping location &#8212; in software or in hardware &#8212; starts to matter.</p><h2><strong>Why timestamp location changes everything</strong></h2><p>At this point, NTP and PTP may still look like protocol problems: exchange messages, estimate offset, correct the clock.</p><p>But in practice, a large part of synchronization quality depends on something more physical:</p><p><strong>where exactly is the timestamp taken?</strong></p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9Nt1!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9057a893-8302-4c5c-bf0a-4997fbb9a8fd_1536x1024.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9Nt1!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9057a893-8302-4c5c-bf0a-4997fbb9a8fd_1536x1024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!9Nt1!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9057a893-8302-4c5c-bf0a-4997fbb9a8fd_1536x1024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!9Nt1!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9057a893-8302-4c5c-bf0a-4997fbb9a8fd_1536x1024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!9Nt1!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9057a893-8302-4c5c-bf0a-4997fbb9a8fd_1536x1024.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9Nt1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9057a893-8302-4c5c-bf0a-4997fbb9a8fd_1536x1024.jpeg" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9057a893-8302-4c5c-bf0a-4997fbb9a8fd_1536x1024.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:223519,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/191125080?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9057a893-8302-4c5c-bf0a-4997fbb9a8fd_1536x1024.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9Nt1!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9057a893-8302-4c5c-bf0a-4997fbb9a8fd_1536x1024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!9Nt1!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9057a893-8302-4c5c-bf0a-4997fbb9a8fd_1536x1024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!9Nt1!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9057a893-8302-4c5c-bf0a-4997fbb9a8fd_1536x1024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!9Nt1!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9057a893-8302-4c5c-bf0a-4997fbb9a8fd_1536x1024.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>That matters because a packet does not appear in software at the exact moment it hits the wire. Between the real network event and the moment the operating system records a timestamp, the packet may pass through the NIC, driver, kernel, interrupt handling, scheduling, and software processing. Every one of those layers can add delay and variation.</p><p>So two timestamps may look equally precise as numbers while representing very different physical moments.</p><h3><strong>Software timestamping</strong></h3><p>With software timestamping, the timestamp is recorded somewhere in the software stack after the packet has already passed through part of the system.</p><p>That makes software timestamping widely available and easy to use, but it also means the measurement includes more uncertainty:</p><ul><li><p>interrupt latency</p></li><li><p>kernel and driver delay</p></li><li><p>scheduling effects</p></li><li><p>queueing and system load</p></li></ul><p>As a result, a software timestamp often reflects when the system handled the packet, not the exact moment the packet crossed the network interface.</p><h3><strong>Hardware timestamping</strong></h3><p>With hardware timestamping, the timestamp is recorded much closer to the real transmit or receive event, typically inside the NIC itself.</p><p>This removes a large part of the software-induced uncertainty and makes the measurement more stable and repeatable. The closer the timestamp is to the actual wire event, the more useful it becomes for precise synchronization.</p><p>That is one of the main reasons PTP can achieve much better accuracy in the right environment: not only because of the protocol itself, but because it is often paired with hardware timestamping and a more carefully controlled timing path.</p><p>So the practical precision limit is not defined by the protocol name alone. It depends on the full measurement path.</p><p>A good rule of thumb is simple:</p><p><strong>the closer the timestamp is to the wire, the better the synchronization can be.</strong></p><div><hr></div><h2><strong>Summary</strong></h2><p>A single computer can keep time locally. A distributed system has a harder task: many machines must keep time together.</p><p>That is why simply setting clocks once is not enough. Real clocks drift, so synchronization has to be continuous. Protocols such as <strong>NTP</strong> and <strong>PTP</strong> address this by exchanging timestamped messages, estimating clock offset and network delay, and repeatedly steering local clocks toward a reference.</p><p>But protocol choice is only part of the story. In practice, synchronization quality also depends heavily on where timestamps are taken. A timestamp captured deep in software carries more uncertainty than one captured close to the physical network event.</p><p>So if this part was about the general idea of shared time &#8212; why it matters, why it is difficult, and how systems approach it &#8212; the next part will move from principle to implementation.</p><p>We will look at how Linux actually does this in practice: NICs, software and hardware timestamping, PHCs, and the tools that connect them into a real synchronization stack.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Cloud Sells Geographical Abstraction. Critical Systems Buy Geographical Proximity.]]></title><description><![CDATA[What recent disruptions around AWS infrastructure in the Gulf reveal about latency, sovereignty, and the hidden geography of modern cloud systems]]></description><link>https://www.dmytrohuz.com/p/cloud-sells-geographical-abstraction</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/cloud-sells-geographical-abstraction</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Sun, 15 Mar 2026 20:33:15 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!ovmG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5688691-d876-494b-a962-f12f790f6098_1536x672.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ovmG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5688691-d876-494b-a962-f12f790f6098_1536x672.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ovmG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5688691-d876-494b-a962-f12f790f6098_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!ovmG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5688691-d876-494b-a962-f12f790f6098_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!ovmG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5688691-d876-494b-a962-f12f790f6098_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!ovmG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5688691-d876-494b-a962-f12f790f6098_1536x672.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ovmG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5688691-d876-494b-a962-f12f790f6098_1536x672.png" width="1456" height="637" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/b5688691-d876-494b-a962-f12f790f6098_1536x672.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:637,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1702675,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/191062210?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5688691-d876-494b-a962-f12f790f6098_1536x672.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ovmG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5688691-d876-494b-a962-f12f790f6098_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!ovmG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5688691-d876-494b-a962-f12f790f6098_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!ovmG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5688691-d876-494b-a962-f12f790f6098_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!ovmG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fb5688691-d876-494b-a962-f12f790f6098_1536x672.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>We like to talk about &#8220;the cloud&#8221; as if software has escaped geography.</p><p>The interface encourages that illusion. You choose a region, deploy a service, replicate some data, add a failover plan, and the system starts to feel abstract. Compute is elastic. Storage is managed. Infrastructure appears to have become location-independent.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>But critical systems do not really stop living somewhere.</p><p>They still sit on top of power, cooling, fiber, telecom topology, legal jurisdiction, operational teams, and the physics of latency. And the more a system becomes real &#8212; real users, real regulation, real response-time constraints, real business consequence &#8212; the more geography tends to re-enter the design.</p><p>That is the part cloud culture hides well: cloud abstracts geography at the interface level, but many important systems reintroduce geography at the architecture level.</p><p>Recent disruptions around AWS infrastructure in the Gulf make that harder to ignore. Reuters reported issues affecting AWS data centers in the UAE and Bahrain amid Iranian strikes, including problems tied to power and connectivity. Separate Reuters reporting also described incidents in the UAE involving drone interceptions and falling debris in Fujairah. Whether one looks at these as isolated disruptions or as signals of a broader shift, the underlying point is the same: data centers are no longer just &#8220;IT facilities.&#8221; They increasingly sit inside the same map of strategic exposure as energy, ports, and communications infrastructure.</p><p>That does not just make cloud infrastructure more important. It makes geography more important.</p><h2><strong>The abstraction is useful. The abstraction is also misleading.</strong></h2><p>This is not an anti-cloud argument.</p><p>Cloud abstractions are powerful because they compress operational complexity. They let teams build and scale systems without owning every layer directly. They make certain kinds of resilience easier to implement. They lower coordination cost. They make global software feel tractable.</p><p>But useful abstractions have a habit of concealing the substrate.</p><p>In cloud, the concealed substrate is not just hardware. It is geography.</p><p>A lot of modern software is designed as if &#8220;where it runs&#8221; is a secondary implementation detail. For some workloads, that is mostly true. For critical ones, it often is not.</p><p>Latency-sensitive systems want to be physically closer to where requests originate. Regulated systems want data to remain in specific jurisdictions. Operationally important systems want predictable local integration with identity, networking, observability, and response teams. Once those requirements become strong enough, the architecture starts to pull back toward place.</p><p>So while the cloud sells a kind of placelessness, critical systems often buy proximity instead.</p><h2><strong>Latency quietly defeats the fantasy of placeless compute</strong></h2><p>The first force that brings geography back is latency.</p><p>Latency is often described as just a technical metric. In practice, it is a design constraint that pushes infrastructure back into physical space. If response time matters, distance matters. If user experience matters, path length matters. If control loops matter, locality matters.</p><p>This is not theoretical. AWS explicitly recommends choosing regions close to users for latency reasons, and sells Local Zones and Wavelength for workloads that need to run nearer to end users and telecom networks. Microsoft says similar things about Azure Extended Zones for low-latency and data-residency-sensitive workloads.</p><p>At the interface level, cloud encourages us to think in regions, services, and APIs. At the architecture level, latency-sensitive systems often say something much simpler:</p><p><strong>put the system near the place where the consequences happen.</strong></p><p>That is already a partial collapse of abstraction.</p><h2><strong>Sovereignty brings geography back a second time</strong></h2><p>The second force is law.</p><p>Even if a workload could technically run anywhere, that does not mean it is allowed to. Data residency, sector-specific regulation, national security requirements, and jurisdictional constraints all push architecture back toward geography. That is why sovereign cloud offerings now exist at all. AWS has launched a European Sovereign Cloud specifically for stricter residency and operational autonomy requirements, while Google and Microsoft document controls for customers that cannot treat geography as interchangeable.</p><p>This is a useful correction to one of cloud&#8217;s most seductive promises.</p><p>People say cloud gives you geographic flexibility. Sometimes it does. But in important systems, law can be just as constraining as physics. The workload may appear abstract, but its permitted runtime geography can still be narrow.</p><p>Once again, cloud did not remove geography. It pushed geography behind a cleaner control plane.</p><h2><strong>The result is hidden concentration</strong></h2><p>This is where the systems point matters.</p><p>Cloud encourages a mental model of distribution. Critical systems often rebuild concentration underneath that model.</p><p>Not recklessly. Often for completely rational reasons:</p><ul><li><p>lower latency</p></li><li><p>data residency</p></li><li><p>operational simplicity</p></li><li><p>cost structure</p></li><li><p>regional business requirements</p></li><li><p>local ecosystem dependence</p></li></ul><p>Each decision can make sense in isolation. But in aggregate, a system may look globally abstract while the important runtime, data, and dependency paths remain tied to a much narrower geography.</p><p>That is where hidden fragility comes from.</p><p>The problem is not that cloud is fake. The problem is that cloud can make concentration feel more diversified than it really is.</p><p>A clean interface can hide a concentrated substrate.</p><p>And once that substrate becomes strategically important, the illusion gets expensive.</p><p>That is why the AWS example in the Gulf matters beyond the specific incident. It is not only a story about one provider or one region. It is a visible reminder that cloud infrastructure now sits close enough to the center of commerce, communications, and increasingly AI-related compute demand that it begins to resemble critical infrastructure in the older sense of the term. And critical infrastructure is always geographic.</p><h2><strong>Good architecture can counter this &#8212; but not by accident</strong></h2><p>Cloud does not automatically imply weak geographic resilience. In fact, major providers explicitly support multi-region architectures, active-active designs, and cross-region failover. Google recommends multi-region deployments to improve latency and availability, and AWS Well-Architected explicitly warns against consolidating all workload resources into one geographic location. AWS also documents active-active multi-region patterns for low-latency and high-availability systems.</p><p><strong>Cloud does not diversify geography by default just because it is cloud.</strong></p><p>That has to be designed.</p><p>And the design often runs against real pressures:</p><ul><li><p>performance wants locality</p></li><li><p>regulation wants jurisdictional specificity</p></li><li><p>operations want manageable complexity</p></li><li><p>economics want concentration where scale is cheapest</p></li></ul><p>Resilience is not the natural resting state. It is a deliberate counterweight to optimization pressure.</p><p>That is the systems lesson.</p><h2><strong>Why this matters more now</strong></h2><p>This matters more than it used to because modern systems are becoming more dependent on concentrated compute.</p><p>As more of business, communications, platforms, and AI workloads sit on top of large cloud regions and data center clusters, the abstraction becomes more consequential &#8212; and more dangerous to misunderstand. The cloud did not make geography disappear. It made geography easier to ignore until latency, law, outage, or conflict forces it back into view.</p><p>At that point, the important question is no longer whether cloud is &#8220;really distributed.&#8221;</p><p>The better question is:</p><p><strong>How much geographic reality has your architecture quietly reintroduced underneath the abstraction &#8212; and have you designed resilience there on purpose, or just assumed the word cloud already did that for you?</strong></p><p>Because that assumption is where a lot of hidden concentration begins.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[I built ac-trace to question the trust we place in passing tests]]></title><description><![CDATA[ac-trace is an early, narrow experiment in checking whether tested systems are actually defended]]></description><link>https://www.dmytrohuz.com/p/i-built-ac-trace-to-question-the</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/i-built-ac-trace-to-question-the</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Mon, 09 Mar 2026 16:01:57 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!N-RY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F369aff60-0ae8-46fb-8e16-dc86e3bedde7_1536x672.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!N-RY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F369aff60-0ae8-46fb-8e16-dc86e3bedde7_1536x672.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!N-RY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F369aff60-0ae8-46fb-8e16-dc86e3bedde7_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!N-RY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F369aff60-0ae8-46fb-8e16-dc86e3bedde7_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!N-RY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F369aff60-0ae8-46fb-8e16-dc86e3bedde7_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!N-RY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F369aff60-0ae8-46fb-8e16-dc86e3bedde7_1536x672.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!N-RY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F369aff60-0ae8-46fb-8e16-dc86e3bedde7_1536x672.png" width="1456" height="637" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/369aff60-0ae8-46fb-8e16-dc86e3bedde7_1536x672.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:637,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1155279,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/190389898?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F369aff60-0ae8-46fb-8e16-dc86e3bedde7_1536x672.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!N-RY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F369aff60-0ae8-46fb-8e16-dc86e3bedde7_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!N-RY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F369aff60-0ae8-46fb-8e16-dc86e3bedde7_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!N-RY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F369aff60-0ae8-46fb-8e16-dc86e3bedde7_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!N-RY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F369aff60-0ae8-46fb-8e16-dc86e3bedde7_1536x672.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>AI-assisted coding is making one part of software development much faster than another: it is becoming easier to generate code and tests, but not easier to know what is actually protected.</p><p>That gap worries me. A green test suite can look convincing. Coverage can look convincing too. But neither one proves that the acceptance criteria &#8212; the behaviors the system is supposed to guarantee &#8212; are truly defended. And when AI helps produce both the implementation and the tests at speed, it becomes much easier to mistake test activity for real confidence.</p><p>That is why I built <strong><a href="https://github.com/DmytroHuzz/ac-trace">ac-trace</a></strong>, a new open-source tool.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.dmytrohuz.com/subscribe?"><span>Subscribe now</span></a></p><p>The problem is simple. Teams often treat these as roughly the same thing: tests are passing, code is covered, therefore the requirement is safe. But those are different signals. Code can be exercised without the important behavior being strongly checked. Tests can pass while the actual acceptance criterion is still weakly defended.</p><p>A realistic example: imagine a billing service with a rule that premium users must never be charged above their monthly cap.</p><p>You may have tests for invoice creation. You may have tests that run the premium billing path. You may even hit the exact function where the cap logic lives, so coverage looks good. But if someone removes the cap check or flips the comparison and the tests still pass, then the requirement was never really protected. The code ran. The suite stayed green. The acceptance criterion still had a confidence gap.</p><p>This becomes more dangerous with AI-assisted coding.</p><p>AI is very good at producing plausible code and plausible tests. That is useful. But it also lowers the cost of producing a large amount of evidence that looks reassuring. More tests, more mocks, more fixtures, more green pipelines &#8212; without a proportional increase in justified confidence. The faster teams generate software artifacts, the easier it becomes to confuse speed and volume with protection.</p><p>So I wanted a tool that asks a more specific question: not just whether tests exist, and not just whether code is covered, but whether the tests actually defend the acceptance criteria they are supposed to protect.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!ZWTg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94ba3b4e-398d-46c5-9d06-07f1bc282510_1536x1024.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!ZWTg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94ba3b4e-398d-46c5-9d06-07f1bc282510_1536x1024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!ZWTg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94ba3b4e-398d-46c5-9d06-07f1bc282510_1536x1024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!ZWTg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94ba3b4e-398d-46c5-9d06-07f1bc282510_1536x1024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!ZWTg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94ba3b4e-398d-46c5-9d06-07f1bc282510_1536x1024.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!ZWTg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94ba3b4e-398d-46c5-9d06-07f1bc282510_1536x1024.jpeg" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/94ba3b4e-398d-46c5-9d06-07f1bc282510_1536x1024.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:130367,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/190389898?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94ba3b4e-398d-46c5-9d06-07f1bc282510_1536x1024.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!ZWTg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94ba3b4e-398d-46c5-9d06-07f1bc282510_1536x1024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!ZWTg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94ba3b4e-398d-46c5-9d06-07f1bc282510_1536x1024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!ZWTg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94ba3b4e-398d-46c5-9d06-07f1bc282510_1536x1024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!ZWTg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F94ba3b4e-398d-46c5-9d06-07f1bc282510_1536x1024.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><strong><a href="https://github.com/DmytroHuzz/ac-trace">ac-trace</a></strong> maps acceptance criteria to code and tests, then mutates the mapped code to check whether the linked tests actually catch the breakage. In simple terms: if a requirement is really protected, then deliberately breaking the relevant implementation should cause the relevant tests to fail.</p><p>The current scope is intentionally narrow. Right now, <a href="https://github.com/DmytroHuzz/ac-trace">ac-trace</a> focuses on Python + pytest. It uses a YAML manifest, can infer links from annotated tests, and generates reports showing what was mapped and what happened when the mapped code was mutated.</p><p>This is an early experiment, not a grand claim. I am not trying to &#8220;solve testing.&#8221; I just want to make one important gap more visible: passing tests are often a weaker signal than teams think, and AI-assisted coding increases the risk of over-trusting them.</p><p>So this is the launch: <strong><a href="https://github.com/DmytroHuzz/ac-trace">ac-trace</a> is now open source</strong>.</p><p>If this problem sounds familiar, check out the repo. Try it on a small project. Tell me where it is useful, where it is naive, and where it should go next.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://github.com/DmytroHuzz/ac-trace&quot;,&quot;text&quot;:&quot;ac-trace REPO&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://github.com/DmytroHuzz/ac-trace"><span>ac-trace REPO</span></a></p>]]></content:encoded></item><item><title><![CDATA[A Practical Guide to Time for Developers: Part 2 — How one computer keeps time (Linux)]]></title><description><![CDATA[explained by one diagram]]></description><link>https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-2ec</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-2ec</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Thu, 05 Mar 2026 21:16:33 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!JuSw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0671b0e-b346-4268-aa3f-03463bde5436_1536x672.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!JuSw!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0671b0e-b346-4268-aa3f-03463bde5436_1536x672.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JuSw!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0671b0e-b346-4268-aa3f-03463bde5436_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!JuSw!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0671b0e-b346-4268-aa3f-03463bde5436_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!JuSw!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0671b0e-b346-4268-aa3f-03463bde5436_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!JuSw!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0671b0e-b346-4268-aa3f-03463bde5436_1536x672.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JuSw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0671b0e-b346-4268-aa3f-03463bde5436_1536x672.png" width="1456" height="637" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a0671b0e-b346-4268-aa3f-03463bde5436_1536x672.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:637,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1389057,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/190040669?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0671b0e-b346-4268-aa3f-03463bde5436_1536x672.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!JuSw!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0671b0e-b346-4268-aa3f-03463bde5436_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!JuSw!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0671b0e-b346-4268-aa3f-03463bde5436_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!JuSw!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0671b0e-b346-4268-aa3f-03463bde5436_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!JuSw!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0671b0e-b346-4268-aa3f-03463bde5436_1536x672.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Time on a computer <em>looks</em> simple: call now(), get a timestamp, move on.</p><p>In the <a href="https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers">previous article</a>, we discussed the idea that time is just a single point on a timeline. The crucial part is defining <strong>which</strong> timeline that point belongs to.</p><p>For computers, this matters a lot. A system effectively works with two timelines: <strong>real time</strong> and <strong>boot time</strong>. You can convert between them, but they are different measuring systems and shouldn&#8217;t be used interchangeably&#8212;because each timeline serves a different purpose.</p><ul><li><p><strong>Real time</strong> answers: &#8220;What time is it in the real world right now?&#8221;</p></li><li><p><strong>Boot time</strong> answers: &#8220;How much time has passed since X (boot)?&#8221;</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!fcPM!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ed778-4a90-4e54-8bc1-812cb7fead06_514x338.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!fcPM!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ed778-4a90-4e54-8bc1-812cb7fead06_514x338.png 424w, https://substackcdn.com/image/fetch/$s_!fcPM!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ed778-4a90-4e54-8bc1-812cb7fead06_514x338.png 848w, https://substackcdn.com/image/fetch/$s_!fcPM!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ed778-4a90-4e54-8bc1-812cb7fead06_514x338.png 1272w, https://substackcdn.com/image/fetch/$s_!fcPM!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ed778-4a90-4e54-8bc1-812cb7fead06_514x338.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!fcPM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ed778-4a90-4e54-8bc1-812cb7fead06_514x338.png" width="514" height="338" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a96ed778-4a90-4e54-8bc1-812cb7fead06_514x338.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:338,&quot;width&quot;:514,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:20298,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/190040669?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ed778-4a90-4e54-8bc1-812cb7fead06_514x338.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!fcPM!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ed778-4a90-4e54-8bc1-812cb7fead06_514x338.png 424w, https://substackcdn.com/image/fetch/$s_!fcPM!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ed778-4a90-4e54-8bc1-812cb7fead06_514x338.png 848w, https://substackcdn.com/image/fetch/$s_!fcPM!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ed778-4a90-4e54-8bc1-812cb7fead06_514x338.png 1272w, https://substackcdn.com/image/fetch/$s_!fcPM!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa96ed778-4a90-4e54-8bc1-812cb7fead06_514x338.png 1456w" sizes="100vw"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>The computer has has an ecosystem - a few components: hardware and software to track and calculate different times in both timelines.</p><p>This part explains that ecosystem using one diagram, top to bottom. The diagram is the spine; everything else is commentary that makes it click: where ticks come from, what the hardware pieces do, why there are multiple &#8220;system times&#8221;, what suspend breaks, where interrupts fit, and how epoch nanoseconds become &#8220;Tuesday 14:03 in Vienna&#8221;.</p><p></p><p>Figure 1 &#8212; The whole pipeline</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!awcK!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3d486e6-36f6-4d8c-ada9-5492fa001a70_3211x3692.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!awcK!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3d486e6-36f6-4d8c-ada9-5492fa001a70_3211x3692.png 424w, https://substackcdn.com/image/fetch/$s_!awcK!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3d486e6-36f6-4d8c-ada9-5492fa001a70_3211x3692.png 848w, https://substackcdn.com/image/fetch/$s_!awcK!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3d486e6-36f6-4d8c-ada9-5492fa001a70_3211x3692.png 1272w, https://substackcdn.com/image/fetch/$s_!awcK!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3d486e6-36f6-4d8c-ada9-5492fa001a70_3211x3692.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!awcK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3d486e6-36f6-4d8c-ada9-5492fa001a70_3211x3692.png" width="1456" height="1674" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a3d486e6-36f6-4d8c-ada9-5492fa001a70_3211x3692.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1674,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:972626,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/190040669?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3d486e6-36f6-4d8c-ada9-5492fa001a70_3211x3692.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!awcK!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3d486e6-36f6-4d8c-ada9-5492fa001a70_3211x3692.png 424w, https://substackcdn.com/image/fetch/$s_!awcK!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3d486e6-36f6-4d8c-ada9-5492fa001a70_3211x3692.png 848w, https://substackcdn.com/image/fetch/$s_!awcK!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3d486e6-36f6-4d8c-ada9-5492fa001a70_3211x3692.png 1272w, https://substackcdn.com/image/fetch/$s_!awcK!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa3d486e6-36f6-4d8c-ada9-5492fa001a70_3211x3692.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Four lanes:</p><ul><li><p><strong>RTC</strong>: survives power-off, keeps &#8220;wall time&#8221;</p></li><li><p><strong>CPU counter</strong> (often TSC): ticks while the CPU runs</p></li><li><p><strong>Kernel timekeeper</strong>: turns ticks into several clocks</p></li><li><p><strong>Userspace</strong>: calls clock_gettime() and formats time for humans</p></li></ul><p>Now: top &#8594; bottom.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><h1><strong>1) Boot &amp; Initialization Phase</strong></h1><h3><strong>1.1 RTC: the &#8220;battery clock&#8221;</strong></h3><p>At the top-left sits the RTC. Think of it as the tiny clock that keeps time while the computer is asleep or powered off. It usually stores calendar-ish values (year/month/day/hour/min/sec). It&#8217;s not &#8220;nanoseconds since 1970&#8221; by nature &#8212; that&#8217;s something software creates later.</p><p>RTC exists so the system doesn&#8217;t boot into the void. Without it, everything starts at &#8220;some default&#8221; until NTP/PTP (or a human) sets the time.</p><h3><strong>1.2 Turning RTC into &#8220;Unix time&#8221;</strong></h3><p>Next step: the kernel reads RTC and converts it into Unix epoch time (seconds + nanoseconds since 1970-01-01T00:00:00Z). That gives a sensible starting point for time-of-day.</p><p>This is still a <em>bootstrap</em> value. RTC isn&#8217;t a precision clock. It&#8217;s a &#8220;good enough to start&#8221; clock.</p><h3><strong>1.3 The CPU counter: ticks while running</strong></h3><p>Now the machine is awake, so Linux wants something faster and more stable than &#8220;ask the RTC all the time.&#8221; Enter the CPU/platform counter &#8212; the <strong>clocksource</strong>. On modern x86, that&#8217;s often the <strong>TSC</strong>.</p><p>Important mindset shift:</p><blockquote><p>TSC is not &#8220;the time.&#8221;</p><p>TSC is &#8220;how many ticks happened since some arbitrary start.&#8221;</p></blockquote><p>It&#8217;s a counter. It&#8217;s only meaningful after conversion.</p><h3><strong>1.4 Calibration: making ticks speak nanoseconds</strong></h3><p>Ticks are just ticks until the kernel knows the counter&#8217;s rate. That&#8217;s why the diagram shows calibration: &#8220;cycles per second&#8221;.</p><p>Linux maintains conversion parameters so it can cheaply do:</p><ul><li><p>delta ticks &#8594; delta nanoseconds</p></li></ul><p>That&#8217;s the key: Linux mostly cares about <strong>deltas</strong>.</p><h3><strong>1.5 Boot finishes by aligning the timelines</strong></h3><p>At the end of boot, two things are true:</p><ul><li><p>there&#8217;s an &#8220;elapsed since boot&#8221; timeline (monotonic) starting at 0</p></li><li><p>there&#8217;s a &#8220;wall clock&#8221; timeline (realtime) aligned to the RTC-derived epoch time</p></li></ul><p>The clean relationship is:</p><blockquote><p><strong>CLOCK_REALTIME = CLOCK_MONOTONIC + wall_clock_offset</strong></p></blockquote><p>At boot, the kernel chooses the offset so realtime matches RTC.</p><p>This one relationship explains half of the weirdness people hit later.</p><div><hr></div><h1><strong>2) Runtime Phase (Continuous Tracking)</strong></h1><h3><strong>2.1 Where ticks come from (without going into physics)</strong></h3><p>Under the hood, some oscillator ticks, hardware counts those ticks, and Linux reads the count. That&#8217;s it at the conceptual level.</p><p>The only thing worth memorizing here:</p><blockquote><p>The hardware gives ticks. The OS gives meaning.</p></blockquote><h3><strong>2.2 The kernel&#8217;s &#8220;working memory&#8221;</strong></h3><p>In the middle of the diagram there&#8217;s a box of variables. That box is basically the kernel&#8217;s timekeeping brain:</p><ul><li><p>tsc_now, tsc_last &#8212; current and previous counter snapshot</p></li><li><p>mult, shift &#8212; ticks&#8594;ns conversion</p></li><li><p>mono &#8212; accumulated elapsed time since boot</p></li><li><p>suspend_ns &#8212; time spent asleep (so BOOTTIME can include it)</p></li><li><p>wall_off &#8212; the offset that turns monotonic into realtime</p></li></ul><p>With those, Linux can build the clock APIs that user space expects.</p><div><hr></div><h1><strong>3) Time requests: clock_gettime() makes everything happen</strong></h1><p>This part of the diagram is the &#8220;action scene&#8221;:</p><ul><li><p>userspace calls clock_gettime(CLOCK_...)</p></li><li><p>kernel reads the counter (RDTSC in the TSC world)</p></li><li><p>kernel updates its state (delta ticks &#8594; delta ns &#8594; accumulate)</p></li><li><p>kernel returns the requested clock</p></li></ul><p>A useful mental shortcut:</p><blockquote><p>The kernel doesn&#8217;t need a metronome to keep time moving.</p><p>It can compute &#8220;now&#8221; on demand by reading a running counter.</p></blockquote><p>(Internally there are periodic activities too, but this is the clean model.)</p><h3><strong>The tiny update loop</strong></h3><p>The heartbeat is:</p><ul><li><p>delta_ticks = tsc_now - tsc_last</p></li><li><p>delta_ns = ticks_to_ns(delta_ticks)</p></li><li><p>mono += delta_ns</p></li></ul><p>Everything else is derived from mono.</p><div><hr></div><h1><strong>4) Kernel clocks: three timelines, three different promises</strong></h1><p>Now the diagram derives the clocks.</p><h3><strong>4.1 CLOCK_MONOTONIC &#8212; for durations</strong></h3><p>The diagram says:</p><blockquote><p>CLOCK_MONOTONIC = mono</p></blockquote><p>That&#8217;s the &#8220;elapsed time&#8221; clock.</p><p>It&#8217;s the clock to use for anything that needs to be sane even if wall time changes:</p><ul><li><p>timeouts</p></li><li><p>retries</p></li><li><p>rate limiting</p></li><li><p>latency measurements</p></li><li><p>&#8220;sleep for X&#8221;</p></li></ul><p>It doesn&#8217;t go backwards, and it doesn&#8217;t jump when someone sets the wall clock.</p><h3><strong>4.2 CLOCK_REALTIME &#8212; for human time</strong></h3><p>The diagram says:</p><blockquote><p>CLOCK_REALTIME = mono + wall_off</p><p><em>(setting time changes wall_off, not mono)</em></p></blockquote><p>This is <em>the</em> sentence.</p><p>Realtime is epoch-based wall time. It&#8217;s the one that becomes &#8220;2026-03-05 13:00:00&#8221;.</p><p>Because it must match the outside world, it&#8217;s adjustable (NTP/PTP/manual set), and that means it can jump. It&#8217;s great for timestamps, terrible for measuring elapsed time.</p><h3><strong>4.3 CLOCK_BOOTTIME &#8212; monotonic that includes sleep</strong></h3><p>The diagram says:</p><blockquote><p>CLOCK_BOOTTIME = mono + suspend_ns</p></blockquote><p>Suspend is where people get surprised: monotonic often pauses while the system sleeps. BOOTTIME exists for the &#8220;time since boot including sleep&#8221; definition.</p><div><hr></div><h1><strong>5) Power management: suspend/resume is where the split matters</strong></h1><p>During suspend:</p><ul><li><p>CPU isn&#8217;t running</p></li><li><p>counters may stop or aren&#8217;t sampled</p></li><li><p>mono doesn&#8217;t move (in the simple model)</p></li><li><p>real world keeps moving</p></li></ul><p>So the diagram does a clever but simple thing:</p><ul><li><p>store persistent time at suspend (often RTC)</p></li><li><p>store persistent time at resume</p></li><li><p>difference = sleep delta</p></li><li><p>add it to suspend_ns so BOOTTIME advances across sleep</p></li><li><p>keep wall clock aligned after resume (effectively by updating the offset)</p></li></ul><p>That&#8217;s why BOOTTIME exists and why wall time remains useful after sleep.</p><div><hr></div><h2><strong>6) From epoch nanoseconds to &#8220;Tuesday 14:03 in Vienna&#8221;</strong></h2><p>Kernel time is a number. Humans want a calendar.</p><p>On Linux, CLOCK_REALTIME is typically represented as <strong>nanoseconds since the Unix epoch</strong> (1970-01-01T00:00:00Z). It&#8217;s just an integer coordinate on a timeline. Converting it into &#8220;Tuesday 14:03 in Vienna&#8221; is a user-space job, and it happens in a few very specific steps.</p><h3><strong>6.1 Step 1 &#8212; split nanoseconds into seconds + remainder</strong></h3><p>Most time libraries work in &#8220;seconds since epoch&#8221; plus a fractional part:</p><ul><li><p>sec = epoch_ns / 1_000_000_000</p></li><li><p>nsec = epoch_ns % 1_000_000_000</p></li></ul><p>That split is practical: seconds are large-scale time, nanoseconds are the sub-second detail.</p><h3><strong>6.2 Step 2 &#8212; interpret seconds as UTC and create a UTC timestamp</strong></h3><p>At this point the number becomes &#8220;a moment&#8221; in UTC:</p><ul><li><p>utc_instant = epoch_seconds_to_utc(sec, nsec)</p></li></ul><h3><strong>6.3 Step 3 &#8212; convert UTC to a named time zone using tzdata rules</strong></h3><p>Now comes the real-world complexity.</p><p>A numeric offset like +01:00 is not a time zone. It&#8217;s just &#8220;the offset right now.&#8221; Real zones (like Europe/Vienna) are a <strong>ruleset</strong>: they include historical changes and DST transitions.</p><p>So the conversion is:</p><ul><li><p>local_instant = convert_utc_to_zone(utc_instant, &#8220;Europe/Vienna&#8221;, tzdata)</p></li></ul><p>That conversion does three things:</p><ol><li><p>finds the correct offset for that instant (+01:00 or +02:00, depending on DST and history)</p></li><li><p>applies that offset</p></li><li><p>produces local calendar fields (year/month/day/hour/min/sec) <em>plus</em> the offset</p></li></ol><p>This is why &#8220;time zones are formatting&#8221; is wrong: it&#8217;s not string styling, it&#8217;s rule evaluation.</p><h3><strong>6.4 Ambiguous and missing local times (DST pain in one minute)</strong></h3><p>DST creates two special situations that break naive systems:</p><p><strong>Ambiguous local time (fall back)</strong></p><p>The clock repeats an hour. The same local time occurs twice.</p><ul><li><p>Example: 2026-10-25 02:30 in many European zones can mean two different instants.</p></li></ul><p><strong>Missing local time (spring forward)</strong></p><p>The clock jumps forward. Some local times never occur.</p><ul><li><p>Example: 02:30 on the spring-forward day might not exist at all.</p></li></ul><p>Notice what happens here: converting <em>from UTC &#8594; local</em> is always unambiguous (UTC instants are unique). The pain happens when converting <em>from local &#8594; UTC</em> without enough context.</p><p>That&#8217;s why systems that store &#8220;local wall time&#8221; without a zone ID eventually end up in a fight with reality.</p><h3><strong>6.5 Step 4 &#8212; format for display or transport (ISO 8601 / RFC 3339)</strong></h3><p>After conversion, formatting is easy:</p><ul><li><p>UTC canonical log style: 2026-03-05T13:03:12.123456789Z</p></li><li><p>Local display style (with offset): 2026-03-05T14:03:12.123456789+01:00</p></li></ul><p>The important thing is that formatted output should preserve:</p><ul><li><p>the offset (or Z)</p></li><li><p>and ideally the zone context when it matters</p></li></ul><h3><strong>6.6 What should be stored vs what should be displayed</strong></h3><p>This is where many systems accidentally create &#8220;time debt.&#8221;</p><p><strong>Store (internally / in DB / across services):</strong></p><ul><li><p>an unambiguous instant:</p><ul><li><p>epoch timestamp (integer + unit), or</p></li><li><p>UTC/RFC3339 timestamp with Z</p></li></ul></li></ul><p><strong>Display (UI / reports):</strong></p><ul><li><p>convert to the user&#8217;s zone at the edge using tzdata.</p></li></ul><p><strong>If civil meaning matters (schedules, payroll, appointments):</strong></p><ul><li><p>store the <em>rule</em>, not just the instant:</p><ul><li><p>&#8220;every day at 09:00 Europe/Vienna&#8221;</p></li><li><p>plus the zone ID</p><p>Because recurring human schedules live in civil time and DST rules matter.</p></li></ul></li></ul><h3><strong>6.7 Tiny practical checklist (saves a lot of bugs)</strong></h3><ul><li><p>Use a <strong>zone ID</strong> (Europe/Vienna), not a fixed offset, for civil-time logic.</p></li><li><p>Keep timestamps in <strong>UTC-like canonical form</strong> internally.</p></li><li><p>Convert to local time only at the edges.</p></li><li><p>Treat &#8220;local naive timestamps&#8221; as incomplete data unless paired with a zone/ruleset.</p></li><li><p>When parsing timestamps, require either:</p><ul><li><p>Z, or</p></li><li><p>an explicit offset, or</p></li><li><p>a zone ID (for civil-time workflows).</p></li></ul></li></ul><div><hr></div><h1><strong>7) Compact model (matches the diagram)</strong></h1><p><strong>Variables</strong></p><ul><li><p>tsc_last, mult, shift</p></li><li><p>mono (ns since boot)</p></li><li><p>suspend_ns (ns spent suspended)</p></li><li><p>wall_off (epoch ns &#8722; mono)</p></li></ul><p><strong>Update step (on each read)</strong></p><ul><li><p>compute delta ticks from the clocksource</p></li><li><p>convert delta ticks &#8594; delta ns</p></li><li><p>accumulate monotonic time: mono += delta_ns</p></li></ul><p><strong>Clock readouts</strong></p><ul><li><p>CLOCK_MONOTONIC = mono</p></li><li><p>CLOCK_BOOTTIME = mono + suspend_ns</p></li><li><p>CLOCK_REALTIME = mono + wall_off <em>(setting time changes wall_off, not mono)</em></p></li></ul><div><hr></div><h2><strong>8) Code: a small Linux-style emulator</strong></h2><p>I tried to create an clear and easy to understand code that would nicely show how everything works together. </p><p>This code mirrors the diagram and uses Linux-like APIs (clock_gettime(CLOCK_...), clock_settime(CLOCK_REALTIME, ...)) to show the interactions between RTC, TSC, and the kernel clocks.</p><p>You can find the result of my experiment here:</p><p><a href="https://github.com/DmytroHuzz/linux_clock_emulator/blob/main/linux_clock.py">https://github.com/DmytroHuzz/linux_clock_emulator/blob/main/linux_clock.py</a></p><h1><strong>9) Developer cheat sheet</strong></h1><ul><li><p>Use <strong>CLOCK_MONOTONIC</strong> for: timeouts, retries, intervals, measuring latency, scheduling &#8220;sleep X&#8221;.</p></li><li><p>Use <strong>CLOCK_BOOTTIME</strong> for elapsed time that should include suspend.</p></li><li><p>Use <strong>CLOCK_REALTIME</strong> for logs, audits, UI timestamps, business meaning.</p></li><li><p>Never compute durations as realtime_end - realtime_start.</p></li><li><p>Time zone conversion is userspace logic (tzdata). Store UTC-like timestamps internally.</p></li></ul><h2><strong>Next: Part 3</strong></h2><p>Part 3 leaves the single machine and goes to the network and we will see how to sync many machines.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Subscribe to do not miss the next part: </p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p></p>]]></content:encoded></item><item><title><![CDATA[A Practical Guide to Time for Developers: Part 1 — What time is in software (physics + agreements)]]></title><description><![CDATA[The foundations: what you&#8217;re really tracking when you store a timestamp]]></description><link>https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Sun, 01 Mar 2026 06:30:46 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!8hv5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7fbc17c1-cd36-4488-8a63-51f076a67229_1536x672.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!8hv5!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7fbc17c1-cd36-4488-8a63-51f076a67229_1536x672.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!8hv5!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7fbc17c1-cd36-4488-8a63-51f076a67229_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!8hv5!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7fbc17c1-cd36-4488-8a63-51f076a67229_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!8hv5!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7fbc17c1-cd36-4488-8a63-51f076a67229_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!8hv5!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7fbc17c1-cd36-4488-8a63-51f076a67229_1536x672.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!8hv5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7fbc17c1-cd36-4488-8a63-51f076a67229_1536x672.png" width="1456" height="637" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/7fbc17c1-cd36-4488-8a63-51f076a67229_1536x672.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:637,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1233540,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/189526403?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7fbc17c1-cd36-4488-8a63-51f076a67229_1536x672.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!8hv5!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7fbc17c1-cd36-4488-8a63-51f076a67229_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!8hv5!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7fbc17c1-cd36-4488-8a63-51f076a67229_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!8hv5!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7fbc17c1-cd36-4488-8a63-51f076a67229_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!8hv5!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F7fbc17c1-cd36-4488-8a63-51f076a67229_1536x672.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2><strong>Preface to the series</strong></h2><p>I was tasked with synchronizing time across <strong>N computers</strong> with <strong>~1 nanosecond accuracy</strong>. Not &#8220;a laptop over Wi-Fi&#8221; &#8212; a controlled wired setup where hardware timestamping and disciplined clocks make that goal at least a meaningful engineering target.</p><p>At first it sounded trivial. We learn clocks, dates, and time zones as kids. How hard can it be?</p><p>The industry already has a standard solution: <strong>Precision Time Protocol (PTP)</strong>.</p><p>But I wanted to look inside the protocol and understand what it actually does. I expected it to be the easiest part of the whole story. Instead I ran straight into a wall of concepts: <strong>TAI vs UTC, epochs, leap seconds, RTC vs system clock, wall clock vs monotonic time, time zones, na&#239;ve timestamps</strong>. It turns out &#8220;time&#8221; is not a single thing &#8212; it&#8217;s physics, standards, and human conventions layered on top of each other.</p><p>I searched for a single article that explains the whole chain &#8212; something like &#8220;Time for software developers: zero to hero&#8221; or &#8220;From RTC to PTP&#8221; &#8212; and couldn&#8217;t find it. So I decided to write the guide I wished existed: a practical manual for developers that covers the essential concepts, the typical failure modes, and the protocols and algorithms we use to keep and distribute time.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>This series has four parts:</p><ol><li><p><strong>What time is (in software)</strong> &#8212; what exactly we&#8217;re tracking, and what &#8220;correct&#8221; even means.</p></li><li><p><strong>How a computer keeps time</strong> &#8212; where ticks come from, how clocks drift, and why operating systems maintain multiple clocks.</p></li><li><p><strong>How systems share time</strong> &#8212; NTP vs PTP, timestamping, asymmetry, and what really limits accuracy.</p></li><li><p><strong>What can go wrong (and how you detect it)</strong> &#8212; validation, monitoring, failure modes, and security/trust of time sources.</p></li></ol><p>Let&#8217;s start with the foundation: <strong>what is time &#8212; physics or agreements?</strong></p><h2>Intro</h2><p>You already know what time <em>feels</em> like. That&#8217;s the trap.</p><p>In software, &#8220;time&#8221; is not one thing. It&#8217;s a mix of <strong>physical reality</strong> (oscillators drift, signals take time to travel), <strong>standards</strong> (UTC, leap seconds), and <strong>human conventions</strong> (time zones, calendars). If you don&#8217;t separate these layers, you end up building systems that look correct in tests and then collapse in production&#8212;usually around midnight, DST, or a &#8220;rare&#8221; edge case.</p><p>This part builds a simple foundation: <strong>what exactly is the thing we&#8217;re tracking when we say &#8220;time&#8221;?</strong></p><div><hr></div><h2><strong>Four different problems people call &#8220;time&#8221;</strong></h2><p>Most confusion comes from mixing these up. People say &#8220;time,&#8221; but they might mean <strong>four totally different things</strong>, and each one requires a different kind of clock, API, and mental model.</p><h3><strong>1) Time-of-day (civil time)</strong></h3><p><strong>Question:</strong> <em>&#8220;What date/time is it right now?&#8221;</em></p><p>This is the time humans care about: calendars, weekdays, business hours, &#8220;yesterday,&#8221; tax reports, contracts.</p><p><strong>Used for:</strong> logs, UI, audit trails, business processes, legal records.</p><p><strong>Typical failure modes:</strong></p><ul><li><p>DST: the same local time can happen twice, or not happen at all.</p></li><li><p>Time zones: &#8220;10:00&#8221; without a zone is not a timestamp, it&#8217;s a vague sentence.</p></li><li><p>Clock corrections: timestamps can jump forward/backward when the system is adjusted.</p></li></ul><p><strong>Rule of thumb:</strong> civil time is great for <em>human meaning</em>, not for measuring anything.</p><h3><strong>2) Duration / intervals</strong></h3><p><strong>Question:</strong> <em>&#8220;How long did it take?&#8221; / &#8220;Wait 500 ms.&#8221;</em></p><p>This is not &#8220;date/time.&#8221; This is <strong>elapsed time</strong>. You don&#8217;t want it to jump. You want it to be steady and monotonic.</p><p><strong>Used for:</strong> timeouts, retries, benchmarks, scheduling, rate limiting.</p><p><strong>Typical failure modes:</strong></p><ul><li><p>Using wall clock for timeouts &#8594; timeout triggers instantly or never triggers after a time correction.</p></li><li><p>Negative durations (&#8220;operation took -3 ms&#8221;) because the clock moved backwards.</p></li><li><p>Inconsistent metrics when different machines have different offsets.</p></li></ul><p><strong>Rule of thumb:</strong> durations must come from a clock that only moves forward.</p><h3><strong>3) Ordering / causality</strong></h3><p><strong>Question:</strong> <em>&#8220;Which event happened first?&#8221;</em> (especially across threads/processes/machines)</p><p>This is the one that causes the most hidden damage. Humans intuitively think timestamps imply ordering. In distributed systems, that&#8217;s often false.</p><p><strong>Used for:</strong> distributed tracing, message processing, state machines, replication, conflict resolution.</p><p><strong>Typical failure modes:</strong></p><ul><li><p>Two machines disagree about &#8220;now&#8221; &#8594; you see &#8220;future&#8221; events in logs.</p></li><li><p>Network delay/scheduling jitter reorder events even if clocks are &#8220;pretty good.&#8221;</p></li><li><p>You use timestamps to order messages and occasionally violate invariants (&#8220;this update happened before its cause&#8221;).</p></li></ul><p><strong>Rule of thumb:</strong> if correctness depends on ordering, don&#8217;t quietly rely on wall-clock time alone. Use explicit ordering mechanisms (sequence numbers, causality-aware designs, etc.) and treat timestamps as <em>metadata</em>.</p><h3><strong>4) Frequency / rate</strong></h3><p><strong>Question:</strong> <em>&#8220;Are two clocks running at the same speed?&#8221;</em></p><p>This isn&#8217;t &#8220;what time is it,&#8221; it&#8217;s <strong>how fast time passes</strong>. For high-precision work (PTP, measurement, telecom), this matters as much as absolute offset.</p><p><strong>Used for:</strong> high-precision sync, control loops, telecom, measurement systems, sampling, sensor fusion.</p><p><strong>Typical failure modes:</strong></p><ul><li><p>You correct offset but ignore drift &#8594; you constantly &#8220;chase&#8221; the reference.</p></li><li><p>Short-term jitter ruins measurements even if average offset looks good.</p></li><li><p>You assume nanosecond <em>resolution</em> implies nanosecond <em>accuracy</em>.</p></li></ul><p><strong>Rule of thumb:</strong> precision time is always a control problem: you manage both offset (sync) and rate (syntonization).</p><div><hr></div><h3><strong>The category mistake that creates most time bugs</strong></h3><p>A lot of bugs are simply <strong>using a tool from one category to solve another</strong>.</p><p>Classic example: using civil time to measure durations:</p><ul><li><p>You record start = wall_clock_now()</p></li><li><p>You record end = wall_clock_now()</p></li><li><p>You compute end - start</p></li></ul><p>It works&#8230; until the system clock is adjusted (NTP/PTP correction, manual change, VM migration, DST misconfig). Then the wall clock can jump backwards, and your &#8220;duration&#8221; becomes negative, your retry logic breaks, or your timeout never fires.</p><p>That&#8217;s not a rare corner case. It&#8217;s an inevitable result of mixing categories.</p><p>If you remember one thing from this section, remember this:</p><blockquote><p><strong>Time-of-day is for meaning. Duration is for measurement. Ordering is for correctness. Frequency is for precision.</strong></p></blockquote><div><hr></div><h2><strong>Basic vocabulary that prevents endless confusion</strong></h2><p>When someone says &#8220;we need <strong>1 ns accuracy</strong>,&#8221; the only correct first reaction is: <em>accuracy of what, relative to what, over what time window, and how will we measure it?</em></p><p>If you don&#8217;t pin down the vocabulary, teams end up arguing for weeks while everyone is technically correct in their own private definition.</p><p>Below are the terms you must keep separate.</p><div><hr></div><h3><strong>Resolution</strong></h3><p><strong>What it is:</strong> the smallest step your clock can <em>represent</em> or <em>report</em>.</p><p>Example: a timestamp API that returns nanoseconds has <strong>1 ns resolution</strong>.</p><p><strong>What it is not:</strong> a guarantee that the clock is correct to 1 ns.</p><p>A clock can happily produce nanosecond-looking numbers while being microseconds (or milliseconds) away from the truth. This is why &#8220;we have nanosecond timestamps&#8221; is almost meaningless by itself.</p><div><hr></div><h3><strong>Precision</strong></h3><p><strong>What it is:</strong> how fine your measurement or reporting is &#8212; how many digits you output and how repeatable your measurement process is.</p><p>Precision often gets confused with resolution. A useful way to think about it:</p><ul><li><p><strong>Resolution</strong> is &#8220;how small a step the counter can show.&#8221;</p></li><li><p><strong>Precision</strong> is &#8220;how finely we can <em>measure</em> and how consistent our measurement results are.&#8221;</p></li></ul><p>You can have high precision measurements of a clock that is not accurate. You can also have a very accurate system that still reports in coarse units.</p><div><hr></div><h3><strong>Accuracy</strong></h3><p><strong>What it is:</strong> how close your clock is to a reference (a trusted source, or &#8220;true time&#8221; in some defined sense).</p><p>Accuracy always depends on:</p><ul><li><p><strong>the reference</strong> (UTC? TAI? GPS? a grandmaster clock?),</p></li><li><p><strong>the path</strong> (network delays),</p></li><li><p><strong>the method</strong> (hardware timestamping vs software),</p></li><li><p><strong>the measurement point</strong> (where you observe time).</p></li></ul><p>So &#8220;1 ns accuracy&#8221; without specifying the reference and measurement method is not a requirement &#8212; it&#8217;s a slogan.</p><div><hr></div><h3><strong>Stability</strong></h3><p><strong>What it is:</strong> how consistently the clock runs over time. In other words: how noisy it is and how much its rate changes.</p><p>Two systems can have the same accuracy at a single moment and wildly different stability:</p><ul><li><p>One stays close for hours.</p></li><li><p>The other drifts immediately and needs constant correction.</p></li></ul><p>In practice, stability is what determines how hard your synchronization algorithm has to work.</p><div><hr></div><h3><strong>Offset</strong></h3><p><strong>What it is:</strong> the difference between your clock and the reference <em>right now</em>.</p><p>If the reference says 12:00:00.000000000 and you say 12:00:00.000000500, your offset is <strong>+500 ns</strong>.</p><p>Offset is the number people usually mean when they casually say &#8220;we&#8217;re synced to X.&#8221;</p><p>But offset alone is not the full story, because it doesn&#8217;t tell you how noisy that offset is or how it behaves over time.</p><div><hr></div><h3><strong>Jitter</strong></h3><p><strong>What it is:</strong> short-term variation &#8212; the &#8220;shake&#8221; around the average.</p><p>If you measure offset once per second and the values bounce around like:</p><p>+20 ns, -15 ns, +35 ns, -10 ns...</p><p>that bounce is jitter.</p><p>Jitter matters because many systems care about instantaneous behavior, not just long-term average. A &#8220;perfect&#8221; average offset is useless if the clock is too noisy for your application.</p><div><hr></div><h3><strong>Wander</strong></h3><p><strong>What it is:</strong> slow changes over longer timescales &#8212; the &#8220;drift of the drift.&#8221;</p><p>Where jitter is rapid noise, wander is a slow trend: temperature changes, oscillator aging, environmental effects, network path changes that persist.</p><p>Wander is what makes a system look great in a short demo and gradually fall apart over hours or days if the control loop can&#8217;t track it.</p><div><hr></div><h3><strong>Synchronization vs syntonization (phase vs rate)</strong></h3><p>This is one of the most important distinctions in precision time, and it&#8217;s usually not named explicitly &#8212; which is why people get confused.</p><ul><li><p><strong>Synchronize</strong> = align <strong>phase</strong> &#8594; reduce <strong>offset</strong></p><p>&#8220;Make our timestamps match right now.&#8221;</p></li><li><p><strong>Syntonize</strong> = align <strong>rate</strong> &#8594; reduce <strong>drift</strong></p><p>&#8220;Make our clocks run at the same speed.&#8221;</p></li></ul><p>If you only synchronize (phase) but don&#8217;t syntonize (rate), you get a system that constantly drifts away and needs repeated &#8220;kicks&#8221; back into place. If you syntonize well, the system stays close with small, smooth corrections.</p><p>That&#8217;s why protocols like <strong>PTP</strong> are not &#8220;set the time once and forget it.&#8221; They run a continuous control loop: measure offset, estimate delay, correct phase and rate, and fight noise (jitter) and slow effects (wander).</p><div><hr></div><p>If you want a single mental model: <strong>precision time is control theory applied to clocks</strong>. The numbers (offset/jitter/wander) are the feedback signals; synchronization and syntonization are the control objectives.</p><div><hr></div><h2><strong>Timescales: what your timestamps are actually referencing</strong></h2><p>A timestamp looks like a number. That&#8217;s why developers treat it like a number.</p><p>But a timestamp is not &#8220;time.&#8221; It&#8217;s a <strong>coordinate</strong> in some system &#8212; and the most important part of that coordinate system is the <strong>timescale</strong>: <em>what kind of &#8220;time&#8221; this number is measuring.</em></p><p>If two systems use different timescales, their timestamps can be perfectly well-formed and still be fundamentally incomparable.</p><div><hr></div><h3><strong>What is a timescale?</strong></h3><p>A <strong>timescale</strong> is a definition of how seconds are counted and how that count is anchored to reality.</p><p>A useful way to think about it:</p><ul><li><p>It defines what &#8220;one second&#8221; means (atomic seconds vs adjusted seconds).</p></li><li><p>It defines whether the count is <strong>continuous</strong> or can <strong>jump</strong>.</p></li><li><p>It defines how it relates to civil time (what humans call &#8220;UTC time&#8221;).</p></li></ul><p>So when someone says &#8220;store timestamps in UTC,&#8221; they&#8217;re implicitly making a choice about a timescale &#8212; and about how the system behaves during edge cases.</p><div><hr></div><h3><strong>TAI (International Atomic Time)</strong></h3><p>TAI is the cleanest mental model for engineers:</p><ul><li><p>it is a <strong>continuous</strong> count of atomic seconds</p></li><li><p>it <strong>does not have leap seconds</strong></p></li><li><p>it does not care about Earth&#8217;s rotation</p></li></ul><p>TAI is what you&#8217;d want if your only goal was: <em>a global, steady clock that never inserts weird discontinuities.</em></p><p>The downside is social, not technical: people don&#8217;t live in TAI. Civil time is defined using UTC.</p><div><hr></div><h3><strong>UTC (Coordinated Universal Time)</strong></h3><p>UTC is the time humans and laws use. It is designed to stay close to Earth rotation, which is irregular. To keep UTC aligned with that, the standard allows <strong>leap seconds</strong>.</p><p>That single detail has a huge consequence:</p><blockquote><p>UTC is not guaranteed to be perfectly continuous.</p></blockquote><p>Most of the time, UTC behaves like a normal continuous timescale. But around leap seconds, systems can:</p><ul><li><p>repeat a second,</p></li><li><p>represent 23:59:60,</p></li><li><p>step,</p></li><li><p>or smear.</p></li></ul><p>So &#8220;UTC&#8221; is a civil agreement that often behaves like a smooth clock &#8212; until it doesn&#8217;t.</p><p>This is why developers eventually run into bugs that sound impossible:</p><ul><li><p>&#8220;Why did the same timestamp appear twice?&#8221;</p></li><li><p>&#8220;Why did time go backwards for a second?&#8221;</p></li><li><p>&#8220;Why do two machines disagree about UTC during the same minute?&#8221;</p></li></ul><div><hr></div><h3><strong>GPS time (and other system times)</strong></h3><p>GPS time is a common example of a system timescale:</p><ul><li><p>it is <strong>continuous</strong> (no leap seconds)</p></li><li><p>it is used internally by a technical system because continuity is convenient</p></li><li><p>it has a <strong>known offset</strong> relative to other scales (like UTC/TAI), but that offset is not &#8220;magically applied&#8221; everywhere the same way</p></li></ul><p>And GPS is not alone. Many systems use their own &#8220;continuous time&#8221; internally because it simplifies math and avoids leap-second edge cases.</p><p>The important point isn&#8217;t the details of GPS time. It&#8217;s the category:</p><blockquote><p>Many technical systems use a continuous timescale internally and only convert to UTC for humans.</p></blockquote><div><hr></div><h3><strong>You don&#8217;t need to memorize offsets &#8212; you need the</strong></h3><h3><strong>contract</strong></h3><p>At this stage, memorizing &#8220;how many seconds UTC differs from TAI&#8221; is not the goal. You can look up numbers.</p><p>The goal is to internalize this:</p><blockquote><p>In software, &#8220;time&#8221; is usually</p><p><strong>a number plus a contract</strong></p></blockquote><ul><li><p><strong>What is it anchored to?</strong> (UTC? TAI? a grandmaster? device-local monotonic time?)</p></li><li><p><strong>Is it continuous, or can it jump?</strong></p></li><li><p><strong>What happens during leap seconds?</strong> (step? smear? ignore? represent 23:59:60?)</p></li><li><p><strong>How do we convert it for humans?</strong></p></li></ul><p>Once you treat timestamps as &#8220;numbers with contracts,&#8221; a lot of time-related confusion disappears &#8212; and the rest becomes an engineering problem you can actually reason about.</p><div><hr></div><h2><strong>Leap seconds: the edge case that isn&#8217;t optional</strong></h2><p>Leap seconds exist for a simple reason: Earth is not a perfect clock.</p><p>Its rotation speed changes slightly due to geophysics, tides, atmosphere, even large-scale events. But civil time is supposed to stay roughly aligned with the Sun (&#8220;noon should be around when the Sun is highest&#8221;). So UTC is designed to track Earth rotation closely enough &#8212; and when the gap grows too large, UTC is adjusted by inserting (and in theory removing) a second.</p><p>That&#8217;s the astronomy story. Here&#8217;s the software story:</p><blockquote><p><strong>The real problem is not that leap seconds exist.</strong></p><p><strong>The real problem is that systems don&#8217;t agree on how to implement them.</strong></p></blockquote><div><hr></div><h3><strong>The trap: &#8220;UTC&#8221; is not one behavior</strong></h3><p>If your fleet contains different operating systems, different kernels, different NTP/PTP stacks, different cloud providers, or even different configuration defaults, you will encounter multiple &#8220;UTC behaviors&#8221; in the wild.</p><p>That means two machines can both claim &#8220;UTC&#8221; and still produce timestamps that aren&#8217;t directly comparable during a leap-second event.</p><div><hr></div><h3><strong>Common behaviors you will encounter</strong></h3><p><strong>1) Explicit leap second (23:59:60)</strong></p><p>Some systems model the extra second as a real extra label in the clock representation.</p><p>This is conceptually honest: there really is an inserted second.</p><p>But it breaks assumptions everywhere:</p><ul><li><p>parsers that reject :60</p></li><li><p>sorting logic that doesn&#8217;t expect it</p></li><li><p>&#8220;every minute has exactly 60 seconds&#8221; code</p></li></ul><p><strong>2) Step / repeat / jump</strong></p><p>Other systems handle the event by effectively repeating a second or stepping the time.</p><p>From a developer perspective this looks like:</p><ul><li><p>timestamps that stop moving forward for a moment</p></li><li><p>a repeated time value</p></li><li><p>or &#8220;time went backwards&#8221; depending on representation</p></li></ul><p>This is poison for anything that assumes monotonic behavior from civil time, especially ordering and durations.</p><p><strong>3) Smear (stretching time over a window)</strong></p><p>Instead of inserting a visible extra second, some environments &#8220;smear&#8221; it: they slightly slow down (or speed up) the clock over a window so that the leap second is absorbed smoothly.</p><p>This avoids a hard discontinuity, which is great for many systems.</p><p>But it introduces a different kind of inconsistency:</p><ul><li><p>during the smear window, your &#8220;UTC&#8221; is <strong>not exactly UTC</strong></p></li><li><p>two systems can disagree because one smears and the other doesn&#8217;t</p></li><li><p>comparisons across vendors become tricky (&#8220;why are we off by hundreds of ms even though we&#8217;re both &#8216;UTC&#8217;?&#8221;)</p></li></ul><div><hr></div><h3><strong>Why this matters even if leap seconds are rare</strong></h3><p>You might think: &#8220;Leap seconds almost never happen. Who cares?&#8221;</p><p>Two reasons you should care anyway:</p><ol><li><p><strong>The edge case exists at the standards level</strong>, so it shows up in libraries, operating systems, and infrastructure &#8212; whether you like it or not. You inherit it.</p></li><li><p><strong>Distributed systems amplify rare events</strong>.</p><p>One leap second can create:</p></li></ol><ul><li><p>broken ordering across machines</p></li><li><p>weird negative durations in logs/metrics pipelines</p></li><li><p>parsing failures in analytics</p></li><li><p>incident timelines that don&#8217;t line up when you need them most</p></li></ul><div><hr></div><h3><strong>What you must decide early (policy, not implementation)</strong></h3><p>If your system needs strict timestamp comparisons, you need an explicit policy. Not a vibe. A policy.</p><p>Key questions:</p><ul><li><p><strong>Do you store time-of-day as UTC timestamps, or as a continuous internal timescale?</strong></p><p>(Many serious systems store continuous internal time and convert for display.)</p></li><li><p><strong>Do you require &#8220;true UTC,&#8221; or are you okay with smeared UTC?</strong></p><p>(If you compare across cloud providers, &#8220;okay with smear&#8221; might be forced on you.)</p></li><li><p><strong>Where do you do conversions?</strong></p><p>Store canonical time internally; convert at the edges (UI, reporting), or the other way around?</p></li></ul><p>You don&#8217;t have to solve leap-second handling in Part 1. That comes later. But you must do the one thing that prevents surprise:</p><blockquote><p><strong>Acknowledge that &#8220;UTC&#8221; is not a single universal runtime behavior.</strong></p></blockquote><div><hr></div><h3><strong>Epochs and units: a timestamp is a coordinate system</strong></h3><p>Almost all practical timestamps in software are just a coordinate:</p><blockquote><p><strong>&#8220;X units since some chosen origin.&#8221;</strong></p></blockquote><p>That origin is an <strong>epoch</strong>. The unit is seconds / milliseconds / nanoseconds (or sometimes &#8220;ticks&#8221;). Together they define the coordinate system your entire platform will live in.</p><p>Most of the time we treat epoch choice as a boring implementation detail. And it <em>is</em> mostly engineering convenience &#8212; until you need long-term compatibility, cross-system integration, or debugging. Then epoch and units suddenly become the difference between &#8220;obvious&#8221; and &#8220;impossible.&#8221;</p><div><hr></div><h3><strong>Epoch: what is your &#8220;zero&#8221;?</strong></h3><p>An <strong>epoch</strong> is simply the timestamp you call &#8220;0.&#8221;</p><p>Common epochs you&#8217;ll encounter:</p><ul><li><p><strong>Unix epoch</strong>: 1970-01-01 (the usual &#8220;POSIX time&#8221; family)</p></li><li><p><strong>System uptime / boot time</strong>: epoch = when the OS booted</p></li><li><p><strong>Process start</strong>: epoch = when a process started</p></li><li><p><strong>Custom epochs</strong>: sometimes chosen for storage size, legacy reasons, or protocol specs</p></li></ul><p>None of these is inherently &#8220;better.&#8221; They serve different purposes.</p><p>The important thing is: once you choose an epoch for storage or APIs, you&#8217;ve created a contract. Changing it later is like changing the unit of distance in the middle of a highway.</p><div><hr></div><h3><strong>Units: the silent multiplier that breaks everything</strong></h3><p>A timestamp number is meaningless unless you know its unit.</p><p>Typical units:</p><ul><li><p>seconds (s)</p></li><li><p>milliseconds (ms)</p></li><li><p>microseconds (&#181;s)</p></li><li><p>nanoseconds (ns)</p></li></ul><p>The most common production bug here is not exotic. It&#8217;s this:</p><ul><li><p>someone sends milliseconds,</p></li><li><p>someone reads seconds,</p></li><li><p>everything looks &#8220;roughly right&#8221; in small tests,</p></li><li><p>and then you ship timestamps that are off by <strong>1000&#215;</strong>.</p></li></ul><p>If your codebase uses multiple units, enforce one of these rules:</p><ul><li><p>include unit in the name (timestamp_ms, timeout_ns)</p></li><li><p>or use a strong type / duration type system</p></li><li><p>or centralize conversions in one place</p></li></ul><p>Don&#8217;t rely on comments and good intentions.</p><div><hr></div><h3><strong>Integers vs floats: why &#8220;it fits in a double&#8221; is not a plan</strong></h3><p>Floats look convenient because you can write 1700000000.123456789.</p><p>The problem: floating point has limited precision, and the bigger the number gets, the fewer distinct fractional steps you can represent. So you end up silently losing sub-millisecond precision (or worse) depending on magnitude.</p><p>Practical rule:</p><ul><li><p><strong>store and transport timestamps as integers</strong></p></li><li><p>attach the unit explicitly</p></li><li><p>if you need fractions for display, convert at the edges</p></li></ul><p>This is especially important once you claim nanoseconds. If you represent &#8220;nanoseconds since 1970&#8221; as a float, you&#8217;re basically begging for precision loss.</p><div><hr></div><h3><strong>You must label the kind of timestamp, not just the number</strong></h3><p>Even if you know the epoch and unit, you still need to know what kind of &#8220;time&#8221; it is.</p><p>Be explicit about whether a timestamp is:</p><ul><li><p><strong>Time-of-day (civil / wall-clock)</strong></p><p>Anchored to a UTC-like timescale. Comparable across machines <em>if</em> they share the same time source and leap-second policy.</p></li><li><p><strong>Monotonic-ish (elapsed time)</strong></p><p>Anchored to boot or process start. Great for measuring durations and scheduling. Usually meaningless to compare across machines, and often not meaningful after reboot.</p></li><li><p><strong>Logical (ordering)</strong></p><p>Anchored to an ordering scheme (sequence numbers, causality). Comparable for ordering, not for &#8220;real-world time.&#8221;</p></li></ul><p>This is the difference between a useful timestamp and a number that accidentally sorts most of the time.</p><div><hr></div><h3><strong>The classic &#8220;1970&#8221; bug and what it really means</strong></h3><p>When someone says: &#8220;Why is this event from 1970?&#8221; it usually means one of these happened:</p><ul><li><p>you interpreted <strong>milliseconds</strong> as <strong>seconds</strong> (or vice versa)</p></li><li><p>you used the wrong epoch (boot-time treated as Unix time)</p></li><li><p>you parsed a timestamp as local civil time when it was UTC (or vice versa)</p></li><li><p>you truncated or overflowed (32-bit seconds, wrong cast)</p></li><li><p>you mixed timescales (rare, but catastrophic when it happens)</p></li></ul><p>The &#8220;1970&#8221; symptom is your system screaming: <em>your coordinate system is inconsistent.</em></p><div><hr></div><h3><strong>Practical rules (expanded)</strong></h3><ul><li><p>Always know your <strong>epoch</strong> and your <strong>unit</strong>. If you can&#8217;t answer both instantly, you don&#8217;t have a timestamp &#8212; you have a random number.</p></li><li><p>Prefer <strong>integer</strong> storage/transport.</p></li><li><p>Encode the unit in the API/type/name.</p></li><li><p>Treat timestamp types as separate domains:</p><ul><li><p>wall-clock for human meaning</p></li><li><p>monotonic for durations</p></li><li><p>logical for ordering</p></li></ul></li><li><p>Never mix different timestamp kinds without explicit conversion and a clear reason.</p></li></ul><div><hr></div><h3><strong>Why this matters for the rest of the series</strong></h3><p>Protocols and OS clocks become much easier to understand once you separate:</p><ul><li><p><strong>what &#8220;time&#8221; means</strong> (timescale and behavior)</p></li><li><p>from <strong>how it&#8217;s encoded</strong> (epoch + unit + representation)</p></li></ul><p>In Part 2 we&#8217;ll look at where these numbers come from inside one machine, and why &#8220;the system time&#8221; is actually several different clocks with different guarantees.</p><div><hr></div><h3><strong>Civil time: time zones, DST, and calendars are not &#8220;formatting&#8221;</strong></h3><p>A lot of engineers treat time zones as UI: &#8220;we&#8217;ll store UTC and just format it for the user.&#8221; That instinct is half right.</p><p>The other half is where projects die: <strong>civil time is not a formatting layer</strong>. It&#8217;s a set of rules that changes across geography <em>and across history</em>. If your system interacts with humans, payroll, contracts, schedules, billing cycles, or &#8220;days,&#8221; you are doing civil-time logic whether you admit it or not.</p><div><hr></div><h3><strong>Civil time is a rules database, not a law of physics</strong></h3><p>Time zones are not just &#8220;UTC+2.&#8221; They are effectively:</p><ul><li><p>a region identifier (e.g., &#8220;Europe/Vienna&#8221;, not &#8220;+01:00&#8221;)</p></li><li><p>plus a historical database of changes:</p><ul><li><p>offsets change over the decades</p></li><li><p>DST rules change (sometimes with very short notice)</p></li><li><p>sometimes entire countries switch policy</p></li></ul></li></ul><p>So &#8220;local time&#8221; is not stable unless you store the <em>zone identifier</em> and consult a time zone database for the correct rule at that date.</p><p>If you store only &#8220;UTC offset at the moment,&#8221; you lose the ability to reproduce civil time correctly later.</p><div><hr></div><h3><strong>DST creates two kinds of broken times: ambiguous and missing</strong></h3><p>Daylight saving time is where naive systems reveal themselves.</p><p><strong>Ambiguous local time (fall back)</strong></p><p>The clock is set back. A local time interval happens twice.</p><p>So a local timestamp like:</p><ul><li><p>2026-10-25 02:30</p></li></ul><p>is ambiguous in many European zones: it could refer to the &#8220;first 02:30&#8221; or the &#8220;second 02:30.&#8221; Without extra context (offset or zone rule), that timestamp is not uniquely defined.</p><p><strong>Missing local time (spring forward)</strong></p><p>The clock jumps forward. Some local times never happen.</p><p>So a local timestamp like &#8220;02:30&#8221; on the DST jump day might literally be invalid: it never occurred.</p><p>This is why &#8220;local time as a primary storage format&#8221; is a trap. Your database will happily store impossible moments.</p><div><hr></div><h3><strong>Calendars are hostile: &#8220;day&#8221; and &#8220;month&#8221; are not durations</strong></h3><p>Civil time mixes clocks with calendars, and calendars don&#8217;t behave like physics.</p><p><strong>&#8220;Add 24 hours&#8221; &#8800; &#8220;add 1 day&#8221;</strong></p><ul><li><p>Adding 24 hours means &#8220;exactly 86,400 seconds later.&#8221;</p></li><li><p>Adding 1 day often means &#8220;same local clock time on the next calendar day.&#8221;</p></li></ul><p>During DST transitions, those diverge. Some days are 23 hours, some are 25. So the &#8220;next day at 09:00&#8221; is not always &#8220;+24h&#8221;.</p><p><strong>&#8220;Add 1 month&#8221; is not a duration at all</strong></p><p>A month is not a fixed number of seconds. It&#8217;s a calendar concept with edge cases:</p><ul><li><p>What is &#8220;one month after January 31&#8221;?</p></li><li><p>Is it February 28/29? March 3? &#8220;clamp to end of month&#8221;? error?</p></li></ul><p>There isn&#8217;t one universally correct answer. There are policies &#8212; and you must choose one explicitly.</p><div><hr></div><h3><strong>The hidden production bugs this causes</strong></h3><p>Civil-time mistakes usually appear as:</p><ul><li><p>duplicated timestamps in logs (same local time twice)</p></li><li><p>scheduling drift (&#8220;meeting moved by one hour&#8221;)</p></li><li><p>billing/payroll disagreements (&#8220;which day counts?&#8221;)</p></li><li><p>impossible events (&#8220;this happened at a time that never existed&#8221;)</p></li><li><p>long-term reproducibility issues (&#8220;it used to show 10:00, now it shows 11:00 for old data&#8221;)</p></li></ul><p>The worst part: these bugs often don&#8217;t show up in unit tests because tests don&#8217;t run across DST boundaries or historical rule changes.</p><div><hr></div><h3><strong>A policy that prevents endless pain (and where it breaks)</strong></h3><p>A sane default for most systems:</p><ul><li><p><strong>Store and exchange</strong> timestamps in a single global standard (typically UTC-like).</p></li><li><p><strong>Convert</strong> to local time zones only at the edges (UI, reports).</p></li><li><p><strong>Keep calendar arithmetic explicit and isolated</strong> (and test DST boundaries).</p></li></ul><p>Two important additions to make this actually work in real systems:</p><ul><li><p>When civil meaning matters (appointments, payroll, &#8220;local midnight&#8221;), store the <strong>time zone ID</strong> (e.g., &#8220;Europe/Vienna&#8221;), not just an offset.</p></li><li><p>If you store recurring schedules (&#8220;every day at 09:00 local time&#8221;), store them as <strong>civil-time rules</strong>, not as precomputed UTC instants.</p></li></ul><p>Because recurring human schedules are defined in civil time &#8212; and civil time changes.</p><div><hr></div><p>If you internalize one idea from this section, make it this:</p><blockquote><p>Local time is not a timestamp.</p><p>It becomes a timestamp only when you attach a time zone rule set &#8212; and accept the edge cases.</p></blockquote><div><hr></div><h3><strong>Physical time vs logical time (distributed systems reality)</strong></h3><p>Even if you had &#8220;perfect&#8221; clock synchronization, distributed systems still wouldn&#8217;t behave like a single machine. Reality gets in the way:</p><ul><li><p><strong>network delay</strong> (packets take time to arrive),</p></li><li><p><strong>asymmetry</strong> (A&#8594;B delay is not necessarily equal to B&#8594;A),</p></li><li><p><strong>scheduling delays</strong> (your process didn&#8217;t run when you think it did),</p></li><li><p><strong>partial failures</strong> (timeouts, retries, partitions),</p></li><li><p>and simply <strong>different perspectives of &#8220;now.&#8221;</strong></p></li></ul><p>This matters because developers use time for two very different purposes:</p><ol><li><p><em>to attach human meaning</em> (&#8220;when did it happen?&#8221;)</p></li><li><p><em>to decide correctness</em> (&#8220;which happened first?&#8221;)</p></li></ol><p>Those are not the same problem.</p><div><hr></div><h3><strong>Two broad approaches to ordering events</strong></h3><h3><strong>Physical timestamps (&#8220;wall clock time&#8221;)</strong></h3><p>This is the familiar one: attach a wall-clock timestamp to events.</p><p><strong>Why it&#8217;s useful:</strong></p><ul><li><p>humans can read it</p></li><li><p>audit trails and legal records need it</p></li><li><p>it&#8217;s great for observability (&#8220;show me what happened around 12:03&#8221;)</p></li><li><p>it helps correlate events across services <em>when sync is good enough</em></p></li></ul><p><strong>Why it&#8217;s risky for correctness:</strong></p><p>Physical time is an approximation. Even with good sync, you can still get:</p><ul><li><p><strong>mis-ordering</strong>: event B appears &#8220;earlier&#8221; than its cause A because A&#8217;s message was delayed or A&#8217;s clock is slightly behind</p></li><li><p><strong>future events</strong>: logs show something that &#8220;happened in the future&#8221; relative to another machine</p></li><li><p><strong>time going backwards</strong> locally when clocks are stepped</p></li></ul><p>So: wall-clock timestamps are excellent metadata. They are a weak foundation for correctness.</p><h3><strong>Logical time (causality-aware ordering)</strong></h3><p>Logical time exists because &#8220;timestamp ordering&#8221; is not the same as &#8220;happened-before ordering.&#8221;</p><p>The core idea is simple:</p><ul><li><p><strong>A happened before B</strong> if A could have influenced B.</p><p>Not because A&#8217;s timestamp is smaller.</p></li></ul><p>Logical clocks (conceptually: Lamport timestamps and vector clocks) encode causality:</p><ul><li><p>If B observed A (directly or indirectly), B must be ordered after A.</p></li><li><p>If two events are independent, they may be concurrent, and ordering them is a policy decision, not a fact revealed by a clock.</p></li></ul><p><strong>Why it&#8217;s useful:</strong></p><ul><li><p>correctness in replication, conflict resolution, messaging systems</p></li><li><p>reasoning about distributed workflows and state machines</p></li><li><p>avoiding &#8220;timestamp lies&#8221; when clocks disagree</p></li></ul><p><strong>Why it&#8217;s not a replacement for wall time:</strong></p><p>Logical time doesn&#8217;t tell you &#8220;it&#8217;s 12:03.&#8221; It tells you &#8220;this depends on that.&#8221;</p><div><hr></div><h3><strong>Mature systems use both (and are explicit about it)</strong></h3><p>Most serious systems end up with two parallel layers:</p><ul><li><p><strong>Physical time</strong> for observability, audit, user-facing meaning, &#8220;what happened when&#8221;</p></li><li><p><strong>Logical ordering / protocol guarantees</strong> where correctness depends on ordering</p></li></ul><p>This is also how you keep sane during incidents:</p><ul><li><p>physical timestamps help humans reconstruct timelines</p></li><li><p>ordering guarantees help the system stay correct even when time is messy</p></li></ul><div><hr></div><h3><strong>The takeaway</strong></h3><p>If you need <strong>correct ordering</strong>, don&#8217;t silently assume wall-clock time gives it.</p><p>Use wall-clock timestamps for meaning and correlation &#8212; but when correctness depends on &#8220;what happened first,&#8221; you need explicit ordering mechanisms (protocol guarantees, sequence numbers, causality-aware clocks, or designs that don&#8217;t depend on global time).</p><p>Because in distributed systems, &#8220;now&#8221; is not a global fact. It&#8217;s a local opinion.</p><div><hr></div><h3><strong>Summary: what &#8220;time&#8221; is, in one sentence</strong></h3><p>In software, time is <strong>a continuously maintained estimate</strong> of some reference, expressed in a chosen coordinate system (timescale + epoch + units), and only then mapped into human conventions like calendars and time zones.</p><p>If that sounds heavier than &#8220;a number that increases,&#8221; good &#8212; because treating time as &#8220;just a number&#8221; is exactly how you end up with negative durations, duplicated local timestamps, and distributed logs that can&#8217;t be reconciled.</p><p>In the next part we&#8217;ll go one layer deeper and get practical: how a single computer actually keeps time &#8212; where ticks come from, what the OS does with them, why there are multiple clocks, and why &#8220;correcting the clock&#8221; can make time jump (and break anything that assumed it couldn&#8217;t).</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.dmytrohuz.com/subscribe?"><span>Subscribe now</span></a></p><div><hr></div><h2><strong>Developer rules (keep this as your reference card)</strong></h2><p>If you remember nothing else from this part, remember these:</p><ol><li><p>Decide what problem you&#8217;re solving: <strong>time-of-day vs duration vs ordering vs frequency</strong>.</p></li><li><p>Store/transport canonical time in one global form (typically UTC-like).</p></li><li><p>Measure durations with a <strong>monotonic</strong> clock, not wall time.</p></li><li><p>Treat time zones/DST/calendar arithmetic as <strong>logic</strong>, not formatting.</p></li><li><p>Be explicit about <strong>timescale, epoch, and units</strong>; prefer integer timestamps.</p></li><li><p>Assume leap seconds exist &#8212; and that &#8220;UTC&#8221; may be implemented differently (including smearing).</p></li><li><p>Don&#8217;t assume timestamps provide correct distributed ordering.</p></li><li><p>For serious systems, treat time like a dependency: define trust, monitor offset/jitter, plan failure behavior.</p></li></ol><p>These rules are defaults for most systems. High-precision setups add stricter constraints &#8212; we&#8217;ll get there when we talk about distributing time across machines.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[The Aha-Moment of Public-Key Encryption]]></title><description><![CDATA[A small idea behind the huge topic]]></description><link>https://www.dmytrohuz.com/p/the-aha-moment-of-public-key-encryption</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/the-aha-moment-of-public-key-encryption</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Fri, 13 Feb 2026 12:29:49 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!uy8G!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa57299a6-5758-4c3a-bcd3-443638e6a53c_1536x672.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!uy8G!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa57299a6-5758-4c3a-bcd3-443638e6a53c_1536x672.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!uy8G!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa57299a6-5758-4c3a-bcd3-443638e6a53c_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!uy8G!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa57299a6-5758-4c3a-bcd3-443638e6a53c_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!uy8G!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa57299a6-5758-4c3a-bcd3-443638e6a53c_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!uy8G!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa57299a6-5758-4c3a-bcd3-443638e6a53c_1536x672.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!uy8G!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa57299a6-5758-4c3a-bcd3-443638e6a53c_1536x672.png" width="1456" height="637" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a57299a6-5758-4c3a-bcd3-443638e6a53c_1536x672.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:637,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1851982,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/187849238?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa57299a6-5758-4c3a-bcd3-443638e6a53c_1536x672.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!uy8G!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa57299a6-5758-4c3a-bcd3-443638e6a53c_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!uy8G!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa57299a6-5758-4c3a-bcd3-443638e6a53c_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!uy8G!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa57299a6-5758-4c3a-bcd3-443638e6a53c_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!uy8G!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa57299a6-5758-4c3a-bcd3-443638e6a53c_1536x672.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>I started my deep <a href="https://www.dmytrohuz.com/p/rebuilding-cryptography-from-scratch">dive into cryptography</a> six months ago. I wanted to deconstruct its internals into basic building blocks and then build them back up again. One simple idea kept pulling me forward&#8212;fascinating me and motivating me to go deeper: how can a crowd of absolute strangers&#8212;over the internet, an inherently insecure medium&#8212;exchange information securely?</p><p>I won&#8217;t lie: I honestly expected to find one simple answer that would &#8220;click&#8221; and give me an Aha moment. Instead, I fell into a rabbit hole. One idea led to another; one logical structure interacted with&#8212;and depended on&#8212;another. Math, history, logic, statistics. Topics and disciplines intertwined into a complicated, sophisticated ornament. No final answers&#8212;only more questions, theories, experiments, and try-and-fail stories. Stories of absolute trust and absolute failure, of elegant ideas that didn&#8217;t work, of intuition that misled, and of randomness that won. Brilliant people built brilliant solutions&#8212;some failed, then came new solutions, and new solutions again, until something finally held. The long, long, long road to security&#8230; Yes, it was a fascinating journey. And in the end, I finally understood how the philosopher&#8217;s stone of public-key encryption works.</p><p>As we saw in the previous articles, the first challenge was to build a secure and reliable way for two peers&#8212;who know each other and share a secret key&#8212;to communicate. It doesn&#8217;t sound difficult, yet it took more than ten articles to show how to do it properly (and how many ways it can go wrong).</p><p>The next challenge is to achieve the same goal for&#8230; strangers&#8230; who share no key at all.</p><p>I want to keep it short this time. The internet is full of articles that implement protocols and asymmetric ciphers. Here I just want to show the core idea as simply as possible. I want to give you the Aha moment I was searching for&#8212;and then point you to deeper references if you&#8217;re interested in real-world usage and implementation. Or we can go further: tell me what topic you want next, and I&#8217;ll happily write about it.</p><p>So, let the show begin&#8212;and please follow my hands very carefully.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.dmytrohuz.com/subscribe?"><span>Subscribe now</span></a></p><div><hr></div><h2><strong>The &#8220;Aha-math&#8221; behind public-key encryption</strong></h2><p>Let&#8217;s take two prime numbers (numbers divisible only by 1 and themselves):</p><pre><code><code>p = 3
q = 11</code></code></pre><p>These will be the foundation of everything that follows.</p><p>Now let&#8217;s multiply them:</p><pre><code><code>n = p * q = 3 * 11 = 33</code></code></pre><p>This number, n, will be our modulus.</p><p>Modulo arithmetic simply means that numbers &#8220;wrap around&#8221; after reaching a certain value. If the result of an operation (addition, multiplication, etc.) is larger than the modulus, we divide by the modulus and take the remainder.</p><p>For example:</p><pre><code><code>result = 23 + 11 = 34
result = 34 % 33 = 1</code></code></pre><p>Because 34 divided by 33 leaves a remainder of 1.</p><p>You can imagine modulo arithmetic as an infinite array that keeps repeating the same sequence of numbers:</p><pre><code><code>modulo_array_33 = [0,1,2,3,...,31,32,0,1,2,3,...,31,32,0,...]
</code></code></pre><p>So when we compute:</p><pre><code><code>print(modulo_array_33[34])
# 1</code></code></pre><p>We &#8220;wrap around&#8221; and land back at 1.</p><div><hr></div><p>Next, let&#8217;s consider all numbers from 1 to 33:</p><pre><code><code>S = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32}</code></code></pre><p>Now we ask: how many of these numbers do <strong>not</strong> share a common divisor with 33?</p><p>In other words, how many numbers are <em>relatively prime</em> to 33?</p><p>There is a beautiful formula for that:</p><pre><code><code>f = (p-1)*(q-1) = (3-1)*(11-1) = 2*10 = 20</code></code></pre><p>So there are <strong>20 numbers</strong> that are relatively prime to 33.</p><p>This number f is known as Euler&#8217;s totient of n. It plays a central role in RSA.</p><div><hr></div><p>Now the most mystical part begins.</p><p>We need to choose a number e that is relatively prime to 20.</p><pre><code><code>e = 3</code></code></pre><p>3 and 20 share no common factors &#8212; so this works.</p><p>Now comes the heart of the magic.</p><p>We need to find a number d such that:</p><pre><code><code>e * d &#8801; 1 (mod f)
3 * d &#8801; 1 (mod 20)</code></code></pre><p>We are looking for a number d that, when multiplied by 3, leaves remainder 1 after division by 20.</p><p>It turns out:</p><pre><code><code>d = 7</code></code></pre><p>Because:</p><pre><code><code>3 * 7 = 21
21 % 20 = 1</code></code></pre><p><strong>You&#8217;ll be surprised &#8212; at least, I was.</strong> At this point we&#8217;ve already built everything we need for public-key encryption. Now let me show you.</p><div><hr></div><p>Suppose we want to encrypt the number:</p><pre><code><code>m = 4</code></code></pre><p>Our public key is two numbers that we previously created:</p><pre><code><code>public_key = (n, e) = (33, 3)</code></code></pre><p>To encrypt:</p><pre><code><code>ciphertext = m**e mod(n) = 4**3 % 33 = 64 % 33 = 31</code></code></pre><p>So the ciphertext is:</p><pre><code><code>31</code></code></pre><p>Now we decrypt using the private key d = 7:</p><pre><code><code>message = ciphertext**d mod(n) = 31**7 % 33 = 27512614111 % 33 = 4 # 0_o</code></code></pre><pre><code><code>4</code></code></pre><p>Exactly the original message.</p><div><hr></div><p>And this &#8212; in its purest, smallest, most transparent form &#8212; is how RSA works.</p><h3><strong>Why does it work?</strong></h3><p>In the small examples, it almost feels like a trick: we raise a number to one power, then to another, and somehow everything &#8220;cancels out&#8221; and the original message comes back. But what&#8217;s really happening is simpler&#8212;and honestly, more beautiful. When you work modulo a number, powers don&#8217;t grow forever; they eventually start repeating. You&#8217;re operating inside a finite world, so exponentiation can&#8217;t keep producing &#8220;new&#8221; values indefinitely&#8212;at some point it must loop. In our tiny example the loop is short, so you can literally watch it happen by hand. RSA chooses the public exponent and the private exponent so that, together, they make you go &#8220;one full loop plus one extra step.&#8221; Encryption moves you forward along that loop, and decryption moves you forward again in a way that lands you exactly back at the starting point. With small numbers the cycle is visible; with real RSA it&#8217;s the same mechanism, just scaled to cycles that are unimaginably large. The math isn&#8217;t magic&#8212;it&#8217;s controlled movement inside a huge circular system, with the steps chosen so that going forward twice brings you back home.</p><p>It took me a while to understand the math behind this. If you want the deeper, more formal explanation, I highly recommend Appendix A of <strong>A Graduate Course in Applied Cryptography</strong>: <a href="https://toc.cryptobook.us/book.pdf">https://toc.cryptobook.us/book.pdf</a></p><h3><strong>Why is it secure?</strong></h3><p>In the examples above I deliberately used tiny numbers (like 3, 11, and 33) because that&#8217;s the only way to see the whole mechanism with your own eyes and not drown in computation. But with numbers that small, RSA is basically a toy: anyone can factor n in seconds, reconstruct the hidden structure, and &#8220;unlock&#8221; everything. In real life it&#8217;s the same exact workflow&#8212;just scaled up brutally. p and q are enormous primes (hundreds of digits), so n becomes massive. It&#8217;s still easy to multiply two huge primes and publish n, but it becomes practically impossible to run that process backwards and recover the original primes from n. And that&#8217;s the whole point: if you can&#8217;t factor n, you can&#8217;t rebuild the private key. Small numbers help you understand the idea; huge numbers are what make the idea survive contact with the real world.</p><p>If you want a more implementation-oriented walkthrough of RSA, here&#8217;s a practical reference: <a href="https://www.geeksforgeeks.org/computer-networks/rsa-algorithm-cryptography/">https://www.geeksforgeeks.org/computer-networks/rsa-algorithm-cryptography/</a></p><h2><strong>Final word</strong></h2><p>I deliberately kept this article as small and abstract as possible&#8212;not to avoid details, but to show the core idea behind the whole concept. There are many related topics that go deeper and wider: real-world protocols, padding schemes, key formats, attacks, implementation traps, and all the practical engineering that turns &#8220;nice math&#8221; into something you can safely deploy. It&#8217;s an endless rabbit hole, and it makes no sense to cram all of it into one post.</p><p>What I wanted here was the quintessence: one core idea. The mechanism. The &#8220;Aha.&#8221; And I hope it landed.</p><p>Thank you for staying with me for so long. With this, I&#8217;m closing my cryptography series and moving on to the next technologies.</p><p>As always, I&#8217;m open to suggestions and requests&#8212;feel free to drop me a note ;)</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Building Own MAC — Part 3: Reinventing HMAC from SHA-256]]></title><description><![CDATA[In the previous article, we did something slightly ridiculous.]]></description><link>https://www.dmytrohuz.com/p/building-own-mac-part-3-reinventing</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/building-own-mac-part-3-reinventing</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Fri, 23 Jan 2026 18:58:16 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!QAi6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41bb1c89-ee6c-4240-9150-0469a12ab722_1536x672.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!QAi6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41bb1c89-ee6c-4240-9150-0469a12ab722_1536x672.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!QAi6!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41bb1c89-ee6c-4240-9150-0469a12ab722_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!QAi6!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41bb1c89-ee6c-4240-9150-0469a12ab722_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!QAi6!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41bb1c89-ee6c-4240-9150-0469a12ab722_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!QAi6!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41bb1c89-ee6c-4240-9150-0469a12ab722_1536x672.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!QAi6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41bb1c89-ee6c-4240-9150-0469a12ab722_1536x672.png" width="1456" height="637" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/41bb1c89-ee6c-4240-9150-0469a12ab722_1536x672.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:637,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1814272,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/185566303?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41bb1c89-ee6c-4240-9150-0469a12ab722_1536x672.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!QAi6!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41bb1c89-ee6c-4240-9150-0469a12ab722_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!QAi6!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41bb1c89-ee6c-4240-9150-0469a12ab722_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!QAi6!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41bb1c89-ee6c-4240-9150-0469a12ab722_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!QAi6!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41bb1c89-ee6c-4240-9150-0469a12ab722_1536x672.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><a href="https://www.dmytrohuz.com/p/building-own-mac-part-2-fixing-aes">In the previous article</a>, we did something slightly ridiculous.</p><p>We took a block cipher &#8212; a tool designed to transform <strong>one block into one block</strong> &#8212; and forced it to behave like something else.</p><p>We wanted authentication.</p><p>We needed a fixed-size tag.</p><p>We had arbitrary-length messages.</p><p>So we built a &#8220;message &#8594; block&#8221; machine out of a &#8220;block &#8594; block&#8221; primitive.</p><p>It worked.</p><p>We reinvented <strong>CMAC</strong>.</p><p>And then an uncomfortable thought appears:</p><p>Why did we do all of that?</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div><h2><strong>A strange d&#233;j&#224; vu</strong></h2><p>Look again at the final construction from Part 2.</p><p>It has this shape:</p><ul><li><p>arbitrary-length input</p></li><li><p>processed block by block</p></li><li><p>a small internal state</p></li><li><p>a fixed-size output</p></li><li><p>no way to reverse it</p></li><li><p>sensitive to every bit of input</p></li></ul><p>That shape should feel very familiar.</p><p>Because that is <strong>exactly</strong> the shape of a hash function.</p><p>So the obvious question is:</p><blockquote><p>If hash functions already compress messages into fixed-size values,</p><p>why didn&#8217;t we start there?</p></blockquote><div><hr></div><h2><strong>Fix attempt #1 &#8212; &#8220;Just hash with a secret&#8221;</strong></h2><p>Let&#8217;s do the thing every brain does first.</p><p>We want a tag.</p><p>We have a hash function.</p><p>We also have a secret key.</p><p>So we try:</p><pre><code><code>tag = SHA256(K || M)</code></code></pre><p>Or maybe:</p><pre><code><code>tag = SHA256(M || K)</code></code></pre><p>It feels clean.</p><p>It feels simple.</p><p>It feels <em>much</em> simpler than CMAC.</p><p>No AES.</p><p>No modes.</p><p>No subkeys.</p><p>No final-block gymnastics.</p><p>Before we trust it, we do what this series is about.</p><p>We break it.</p><div><hr></div><h2><strong>A reminder: how SHA-256 actually works</strong></h2><p>SHA-256 is not a black box.</p><p>Internally, it is an <strong>iterative compression machine</strong>.</p><p>Here, <em>compression</em> does <strong>not</strong> mean &#8220;making data smaller&#8221; in the everyday sense.</p><p>It means something more precise:</p><blockquote><p>a function that takes</p><p>a <strong>fixed-size internal state</strong></p><p>and a <strong>fixed-size input block</strong>,</p><p>and produces a <strong>new fixed-size state</strong>.</p></blockquote><p>Nothing is expanded.</p><p>Nothing is reversible.</p><p>Information is <em>folded</em> into state.</p><p>Conceptually, it looks like this:</p><pre><code><code>H0 = IV
H1 = compress(H0, block1)
H2 = compress(H1, block2)
...
output = Hn</code></code></pre><p>One detail matters more than everything else:</p><blockquote><p>The output hash <strong>is the final internal state</strong>.</p></blockquote><p>There is no extra sealing step at the end.</p><p>Which means:</p><p>if you know Hash(M), you know the state <em>after</em> processing M.</p><p>And that detail matters far more than intuition suggests.</p><div><hr></div><h2><strong>Break &#8212; length extension</strong></h2><p>Assume the system uses:</p><pre><code><code>tag = SHA256(K || M)</code></code></pre><p>The attacker sees:</p><ul><li><p>the message M</p></li><li><p>the tag SHA256(K || M)</p></li></ul><p>They do <strong>not</strong> know K.</p><p>But they do know:</p><ul><li><p>the hash algorithm</p></li><li><p>the block size</p></li><li><p>the padding rules</p></li></ul><p>And that is enough.</p><p>Because they can do this:</p><pre><code><code>tag' = SHA256_continue(
          state = tag,
          data  = padding(K || M) || extra
       )</code></code></pre><p>Result:</p><pre><code><code>tag' = SHA256(K || M || padding || extra)</code></code></pre><blockquote><p>The attacker reused the final internal hash state and simply continued the hash computation, producing a valid tag for a longer message without knowing the key.</p></blockquote><p>No key.</p><p>No guessing.</p><p>No cryptanalysis.</p><p>The attacker just extended an authenticated message.</p><p>This is a <strong>length extension attack</strong>.</p><p>And it completely breaks this construction.</p><div><hr></div><h2><strong>Important lesson #1</strong></h2><p>This is not a weakness of SHA-256.</p><p>SHA-256 did exactly what it was designed to do.</p><p>The failure is conceptual:</p><blockquote><p>You treated a structured machine as if it were a black box.</p></blockquote><p>Hash functions expose their internal chaining state by design.</p><p>If your MAC construction allows an attacker to reuse that state, it is broken.</p><div><hr></div><h2><strong>Fix attempt #2 &#8212; &#8220;Fine. Let&#8217;s hash twice.&#8221;</strong></h2><p>Okay.</p><p>If the structure leaks, let&#8217;s hide it.</p><p>What about this?</p><pre><code><code>tag = SHA256(K || SHA256(K || M))</code></code></pre><p>Now the internal state of the first hash is buried inside another hash.</p><p>This feels safer.</p><p>But pause for a moment and look at what we&#8217;re doing.</p><p>We are:</p><ul><li><p>stacking primitives blindly</p></li><li><p>hoping structure disappears</p></li><li><p>having no clear argument <em>why</em> this fixes the problem</p></li></ul><p>This is exactly the pattern we saw in Part 2.</p><p>We&#8217;re patching again.</p><p>And we already know where patching leads.</p><p>So let&#8217;s stop and reset.</p><div><hr></div><h2><strong>Define the problem properly (again)</strong></h2><p>From everything we learned so far, a real hash-based MAC must guarantee:</p><ol><li><p>Only someone with the key can compute a valid tag</p></li><li><p>Only someone with the key can verify a valid tag</p></li><li><p>The message cannot be extended or truncated</p></li><li><p>The internal hash state cannot be reused</p></li><li><p>Variable-length messages must be safe by design</p></li></ol><p>So the real question is not:</p><blockquote><p>&#8220;How do we mix a key into a hash?&#8221;</p></blockquote><p>The real question is:</p><blockquote><p><strong>How do we prevent the attacker from continuing the hash computation?</strong></p></blockquote><div><hr></div><h2><strong>The key insight &#8212; control the boundaries</strong></h2><p>The mistake so far was mixing everything into one stream:</p><ul><li><p>key</p></li><li><p>message</p></li><li><p>finalization</p></li></ul><p>That gave the attacker something extendable.</p><p>So what if we don&#8217;t do that?</p><p>What if:</p><ul><li><p>the key is mixed <strong>before</strong> the message</p></li><li><p>the message is fully compressed</p></li><li><p>the key is mixed <strong>again</strong> after</p></li></ul><p>So the attacker never sees a reusable internal state.</p><p>The shape becomes:</p><pre><code><code>inner = SHA256( (K &#8853; ipad) || M )
tag   = SHA256( (K &#8853; opad) || inner )</code></code></pre><p>Don&#8217;t focus on the constants yet.</p><p>Focus on the structure:</p><ul><li><p>the message is fully absorbed before finalization</p></li><li><p>the attacker never gets a state they can continue</p></li><li><p>the key controls both boundaries</p></li><li><p>length extension becomes impossible</p></li></ul><p>This feels different.</p><p>Because this time, we&#8217;re not guessing.</p><p>We&#8217;re designing.</p><div><hr></div><h2><strong>What are ipad and opad?</strong></h2><p>At first glance, ipad and opad look like magic constants.</p><p>They are not.</p><p>They serve <strong>one very specific purpose</strong>:</p><p><strong>domain separation</strong>.</p><ul><li><p>ipad (inner padding) = byte 0x36 repeated to block size</p></li><li><p>opad (outer padding) = byte 0x5c repeated to block size</p></li></ul><p>They ensure that:</p><ul><li><p>the inner hash and outer hash live in <strong>different domains</strong></p></li><li><p>no internal state can be reused across phases</p></li><li><p>Hash(K &#8853; ipad || M) can never collide structurally with Hash(K &#8853; opad || something_else)</p></li></ul><p>In other words:</p><blockquote><p>ipad and opad prevent the inner hash from being mistaken for the outer hash.</p></blockquote><p>They are not there for randomness.</p><p>They are there to make <em>structure explicit</em> and unforgeable.</p><div><hr></div><h2><strong>Why this construction survives</strong></h2><p>Let&#8217;s stress it the same way we stressed everything else.</p><ul><li><p>Can the attacker extend the message?</p><p>No &#8212; the inner hash is finalized before the outer hash begins.</p></li><li><p>Can they reuse an internal state?</p><p>No &#8212; the state is never exposed in a usable form.</p></li><li><p>Can they fake a tag without the key?</p><p>No &#8212; both passes depend on secret key material.</p></li><li><p>Does variable message length matter?</p><p>No &#8212; the hash function already handles it safely.</p></li></ul><p>This construction doesn&#8217;t feel clever.</p><p>It feels <strong>inevitable</strong>.</p><p>Exactly like CMAC did once all constraints were visible.</p><div><hr></div><h2><strong>Name reveal: HMAC</strong></h2><p>At this point, we can finally say the name.</p><p>The construction we just derived is called:</p><p><strong>HMAC &#8212; Hash-based Message Authentication Code</strong></p><p>And just like with CMAC, the name is the least interesting part.</p><p>The important part is that:</p><ul><li><p>it exists because constraints exist</p></li><li><p>it looks complex because the problem is subtle</p></li><li><p>it survived decades of cryptanalysis because it was designed, not guessed</p></li></ul><div><hr></div><h2><strong>Python implementation (SHA-256 + HMAC)</strong></h2><p>As before, we&#8217;ll use a library for the primitive and write the logic ourselves.</p><p>We are not trying to reimplement SHA-256 bit by bit.</p><p>We are showing the structure clearly.</p><pre><code><code>import hashlib

def sha256(data: bytes) -&gt; bytes:
    return hashlib.sha256(data).digest()

def hmac_sha256(key: bytes, message: bytes) -&gt; bytes:
    block_size = 64  # SHA-256 block size

    if len(key) &gt; block_size:
        key = sha256(key)
    if len(key) &lt; block_size:
        key = key + b"\x00" * (block_size - len(key))

    ipad = bytes([0x36] * block_size)
    opad = bytes([0x5c] * block_size)

    inner = sha256(bytes(k ^ i for k, i in zip(key, ipad)) + message)
    tag   = sha256(bytes(k ^ o for k, o in zip(key, opad)) + inner)

    return tag</code></code></pre><p>There is no magic here.</p><p>Every line corresponds to a design decision we just derived.</p><p>And if you compare the output with Python&#8217;s built-in hmac module, it will match.</p><div><hr></div><h2><strong>Testing the implementation</strong></h2><p>A MAC is useless if you can&#8217;t trust it.</p><p>So we verify our implementation against Python&#8217;s standard library:</p><pre><code>import hmac

def test_hmac():
    key = b"super-secret-key"
    message = b"hello world"

    my_tag = hmac_sha256(key, message)
    std_tag = hmac.new(key, message, hashlib.sha256).digest()

    assert my_tag == std_tag
    print("HMAC implementation verified.")

test_hmac()</code></pre><p>If this assertion passes, your implementation is correct.</p><p>No hand-waving.</p><p>No &#8220;it seems to work&#8221;.</p><p>Just a hard <strong>yes</strong> or <strong>no</strong>.</p><h2><strong>Final symmetry</strong></h2><p>Let&#8217;s zoom out one last time.</p><p>In this series, we built two MACs from scratch:</p><ul><li><p><strong>CMAC</strong> &#8212; built from a block cipher</p></li><li><p><strong>HMAC</strong> &#8212; built from a hash function</p></li></ul><p>Different primitives.</p><p>Same constraints.</p><p>And in both cases, the path was identical:</p><ul><li><p>intuition failed</p></li><li><p>naive fixes broke</p></li><li><p>constraints emerged</p></li><li><p>structure followed</p></li><li><p>names came last</p></li></ul><p>Once you see the constraints, the designs stop looking arbitrary.</p><p>They look inevitable.</p><p>And that was the real goal of this series.</p><p>Not to teach you how to <em>use</em> MACs.</p><p>But to teach you how to <strong>recognize when a construction makes sense</strong> &#8212;</p><p>and when it&#8217;s just intuition lying to you again.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.dmytrohuz.com/subscribe?"><span>Subscribe now</span></a></p>]]></content:encoded></item><item><title><![CDATA[Building Own MAC — Part 2: Fixing AES (and accidentally reinventing CMAC)]]></title><description><![CDATA[Why intuition fails in cryptography]]></description><link>https://www.dmytrohuz.com/p/building-own-mac-part-2-fixing-aes</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/building-own-mac-part-2-fixing-aes</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Mon, 19 Jan 2026 20:32:47 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!2YJ-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf5f63b1-3d56-4578-acde-87945b4cd3f5_1536x672.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!2YJ-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf5f63b1-3d56-4578-acde-87945b4cd3f5_1536x672.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!2YJ-!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf5f63b1-3d56-4578-acde-87945b4cd3f5_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!2YJ-!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf5f63b1-3d56-4578-acde-87945b4cd3f5_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!2YJ-!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf5f63b1-3d56-4578-acde-87945b4cd3f5_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!2YJ-!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf5f63b1-3d56-4578-acde-87945b4cd3f5_1536x672.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!2YJ-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf5f63b1-3d56-4578-acde-87945b4cd3f5_1536x672.png" width="1456" height="637" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/df5f63b1-3d56-4578-acde-87945b4cd3f5_1536x672.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:637,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:1502606,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/185104992?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf5f63b1-3d56-4578-acde-87945b4cd3f5_1536x672.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!2YJ-!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf5f63b1-3d56-4578-acde-87945b4cd3f5_1536x672.png 424w, https://substackcdn.com/image/fetch/$s_!2YJ-!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf5f63b1-3d56-4578-acde-87945b4cd3f5_1536x672.png 848w, https://substackcdn.com/image/fetch/$s_!2YJ-!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf5f63b1-3d56-4578-acde-87945b4cd3f5_1536x672.png 1272w, https://substackcdn.com/image/fetch/$s_!2YJ-!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf5f63b1-3d56-4578-acde-87945b4cd3f5_1536x672.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In the previous series we finished <a href="https://www.dmytrohuz.com/p/building-own-block-cipher-part-3">AES and its modes</a>. And in the previous article revealed why <a href="https://www.dmytrohuz.com/p/building-own-mac-part-1-encrypted">it is still not secure</a>.</p><p>We can encrypt messages.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>We can decrypt messages.</p><p>We can ship.</p><p>And then reality does what reality always does:</p><p>It slaps you.</p><p>Because encryption solves <strong>one</strong> problem:</p><blockquote><p>&#8220;If you don&#8217;t know the key, you can&#8217;t read the message.&#8221;</p></blockquote><p>It does <strong>not</strong> solve this problem:</p><blockquote><p>&#8220;If you don&#8217;t know the key, you can&#8217;t change the message.&#8221;</p></blockquote><p>So we face the classic situation:</p><p>&#9989; we have secrecy</p><p>&#10060; we still don&#8217;t have trust</p><p>And now we do what every engineer does when something breaks:</p><p><strong>we try to patch it fast.</strong></p><p>Let&#8217;s go through the exact fixes your brain naturally generates at 2am.</p><p>Most of them fail.</p><p>Some fail <em>spectacularly</em>.</p><p>And each failure forces one new design constraint&#8230; until we reinvent a real solution.</p><div><hr></div><h1><strong>Goal (simple and brutal)</strong></h1><p>We want the receiver to be able to say:</p><ul><li><p>&#9989; accept</p></li><li><p>&#10060; reject</p></li></ul><p>based on <strong>one small extra value</strong>.</p><p>No philosophy. No &#8220;maybe&#8221;.</p><p>Just: <em>does this message deserve to exist?</em></p><div><hr></div><h1>Fix attempt #1 &#8212; &#8220;Just hash the plaintext&#8221;</h1><p>Classic.</p><pre><code><code>C   = Enc(M)
tag = Hash(M)
send (C, tag)</code></code></pre><p>Receiver decrypts <code>C</code>, recomputes <code>Hash(M)</code>, compares.</p><h3>Break (instant)</h3><p>Hashes have no secrets.</p><p>Attacker changes message &#8594; attacker recomputes hash &#8594; sends new pair.</p><p>&#9989; receiver accepts</p><p>&#9989; attacker wins</p><p>&#9989; we learn nothing except pain</p><h3>Takeaway</h3><p><strong>If the attacker can compute your tag, it&#8217;s not authentication.</strong></p><p>It&#8217;s a checksum.</p><p>So the tag must involve a secret.</p><div><hr></div><h1>Fix attempt #1.5 &#8212; &#8220;Put the hash inside the encrypted message&#8221;</h1><p>Okay, fine.</p><p>Let&#8217;s hide the hash.</p><pre><code><code>payload = M || Hash(M)
C = Enc(payload)
send C</code></code></pre><p>Receiver decrypts, extracts <code>M</code> and the embedded hash, recomputes, compares.</p><p>This feels smart.</p><p>It&#8217;s not.</p><h3>Break (modes bite you)</h3><p>In malleable modes (CTR / OFB / CFB), the attacker can flip bits in ciphertext to flip predictable bits in plaintext.</p><p>So they flip bits in the message part&#8230;</p><p>and flip corresponding bits in the embedded hash part.</p><p>They don&#8217;t need to know the key.</p><p>They don&#8217;t need to know the hash function.</p><p>They don&#8217;t need to understand anything.</p><p>They just move bits.</p><p>Receiver decrypts:</p><pre><code><code>M' || Hash(M')</code></code></pre><p>Hash matches. Receiver accepts.</p><p>0_o</p><h3>Takeaway</h3><p><strong>Hiding a checker does not make it a check.</strong></p><p>If your &#8220;verification data&#8221; lives inside the thing being protected, it can be modified together with it.</p><p>We need the tag to be <em>outside</em> the encrypted payload and unforgeable.</p><div><hr></div><h1>Fix attempt #2 &#8212; &#8220;Encrypt the hash separately&#8221;</h1><p>Next idea:</p><pre><code><code>C   = Enc(M)
tag = Enc(Hash(M))
send (C, tag)</code></code></pre><p>Now the attacker can&#8217;t see the hash, can&#8217;t recompute it.</p><p>So&#8230; done?</p><p>Not even close.</p><h3>Break (you verified&#8230; what exactly?)</h3><p>Now you have two encrypted blobs.</p><p>And the receiver has to answer a painful question:</p><blockquote><p>What exactly are we proving by comparing these?</p></blockquote><ul><li><p>Do we verify the plaintext?</p></li><li><p>The ciphertext?</p></li><li><p>The padding?</p></li><li><p>The length?</p></li><li><p>The context (endpoint / protocol version / message type)?</p></li></ul><p>Nothing is bound. Everything is implied.</p><p>And implied security is not security.</p><p>Also: even if you try to &#8220;compare decrypted hash to recomputed hash&#8221;, you&#8217;re back to the earlier issue &#8212; encryption modes are malleable and protocol context is not bound.</p><h3>Takeaway</h3><p><strong>Encrypting a checker is not the same as verifying a message.</strong></p><p>We need a <em>single verification value</em>, not two blobs that &#8220;seem related&#8221;.</p><div><hr></div><h1>Fix attempt #2.5 &#8212; &#8220;Encrypt the ciphertext and compare&#8221;</h1><p>Okay. Let&#8217;s remove ambiguity.</p><pre><code><code>C   = Enc(M)
tag = Enc(C)
send (C, tag)</code></code></pre><p>Receiver decrypts tag &#8594; gets <code>C'</code> &#8594; compares <code>C' == C</code>.</p><p>Now we have:</p><ul><li><p>a secret</p></li><li><p>a deterministic check</p></li><li><p>a clean yes/no</p></li></ul><p>This looks decent.</p><p>And it still fails.</p><h3>Break (you authenticated bytes, not meaning)</h3><p>This check proves exactly one thing:</p><blockquote><p>&#8220;The ciphertext blob wasn&#8217;t modified.&#8221;</p></blockquote><p>It does <strong>not</strong> prove:</p><ul><li><p>that this ciphertext belongs to <em>this</em> endpoint</p></li><li><p>that it&#8217;s intended for <em>this</em> message type</p></li><li><p>that it&#8217;s valid <em>in this context</em></p></li><li><p>that it&#8217;s fresh</p></li><li><p>that it&#8217;s not a replay</p></li></ul><p>So the attacker doesn&#8217;t need to forge anything.</p><p>They just <strong>replay</strong> a valid <code>(C, tag)</code> where it causes damage.</p><p>&#8220;Transfer $10&#8221; becomes &#8220;Transfer $10 again.&#8221;</p><p>Or becomes &#8220;Approve something you approved yesterday.&#8221;</p><p>You built a very strong proof of internal consistency&#8230; and zero proof of intent.</p><h3>Takeaway</h3><p><strong>Consistency is not authenticity.</strong></p><p>Authentication must bind <em>meaning and context</em>, not just bytes.</p><div><hr></div><h1>Fix attempt #3 &#8212; &#8220;Use a ciphertext artifact as the tag&#8221;</h1><p>Another idea people try:</p><blockquote><p>&#8220;Maybe the last ciphertext block depends on everything. Let&#8217;s use it.&#8221;</p></blockquote><pre><code><code>C   = Enc(M)
tag = last_block(C)</code></code></pre><h3>Break (structural)</h3><p>This &#8220;tag&#8221; depends on:</p><ul><li><p>mode internals</p></li><li><p>padding</p></li><li><p>message length</p></li><li><p>where the last block boundary falls</p></li></ul><p>Truncation, extension, prefixes&#8230; the whole thing becomes ambiguous.</p><h3>Takeaway</h3><p><strong>Artifacts are not tags.</strong></p><p>We need a tag function that we control, end-to-end.</p><div><hr></div><h1>Fix attempt #4 &#8212; &#8220;Fine. Let&#8217;s build a tag ourselves.&#8221;</h1><p>At this point it&#8217;s pretty clear that all &#8220;attach something and encrypt it&#8221; hacks are cursed.</p><p>So let&#8217;s stop patching symptoms and define what we actually need.</p><p>We need a function that takes:</p><ul><li><p>a secret key <code>K</code></p></li><li><p>a message <code>M</code> of <strong>any length</strong></p></li></ul><p>and produces:</p><ul><li><p>a <strong>fixed-size tag</strong> (something like 16 bytes)</p></li></ul><p>So not:</p><ul><li><p>Block &#8594; Block (that&#8217;s what AES gives us)</p></li><li><p>Message &#8594; Message (that&#8217;s what modes give us)</p></li></ul><p>but:</p><blockquote><p>Message &#8594; Block</p></blockquote><p>And yes, AES still sounds useful, because it&#8217;s keyed and strong.</p><p>The only problem is&#8230; AES doesn&#8217;t speak &#8220;message&#8221;. It speaks &#8220;one block&#8221;.</p><p>So we have to build a &#8220;message-to-block machine&#8221; out of &#8220;block-to-block&#8221;.</p><p>Which means: <strong>state</strong>.</p><p>We invent a small internal value <code>X</code> (one AES block), and we feed the message block-by-block:</p><ul><li><p>start with some known initial state <code>X0</code></p></li><li><p>combine the current block with the state</p></li><li><p>run AES</p></li><li><p>repeat</p></li></ul><p>The most natural combine operation is XOR (it keeps the size).</p><p>So the first honest design becomes:</p><pre><code><code>X0 = 0
for each block Bi in message:
    Xi = AES(K, Xi-1 XOR Bi)
tag = Xn</code></code></pre><p>This is the first time we&#8217;re no longer &#8220;encrypting a message&#8221;.</p><p>We&#8217;re doing something else:</p><ul><li><p>the output is fixed-size no matter how long the message is</p></li><li><p>you can&#8217;t decrypt the tag back into the message</p></li><li><p>we are folding the message into a state</p></li></ul><p>That&#8217;s not encryption. That&#8217;s <strong>compression</strong> (in the cryptographic sense).</p><p>Now: does it work?</p><p>It works <em>almost</em> perfectly&#8230; until you remember messages are not fixed-length.</p><p>And the moment message length varies, this construction starts bleeding</p><p>Now: break it.</p><h3>The break: variable-length messages</h3><p>This construction is essentially &#8220;CBC-MAC&#8221;.</p><p>It works for fixed-length messages.</p><p>It breaks for variable length.</p><p>Reason: the output tag is a valid internal state, so extension/splicing tricks become possible unless you bind the message length / finalization rules.</p><p>(And yes, this is a known and very real class of failures: <em>CBC-MAC on variable-length messages is insecure</em>.)</p><h3>Takeaway</h3><p><strong>The end of the message must be cryptographically bound.</strong></p><p>We must make &#8220;this is the final block&#8221; unforgeable.</p><div><hr></div><h1>Fix attempt #5 &#8212; &#8220;Fix the ending (this is where real engineering starts)&#8221;</h1><p>So what exactly breaks in Fix #4?</p><p>Not the chaining itself.</p><p>The break is <strong>the ending</strong>:</p><ul><li><p>messages can end on a block boundary or not</p></li><li><p>messages can have different lengths</p></li><li><p>the final state of one message must not become a reusable internal state for another</p></li></ul><p>So we add finalization rules that make &#8220;this is the end&#8221; cryptographically real.</p><p>Here is the mental model (extended pseudocode):</p><h3>Step A &#8212; Generate two subkeys (K1, K2)</h3><p>We derive subkeys from AES itself:</p><pre><code><code>L  = AES(K,0^128)
K1 = dbl(L)
K2 = dbl(K1)</code></code></pre><p>Where <code>dbl()</code> is &#8220;shift-left-by-1-bit, and if a carry falls off, XOR a constant (Rb) into the last byte&#8221;.</p><p>This is not random ceremony &#8212; it gives us two distinct &#8220;domains&#8221; for the last block.</p><h3>Step B &#8212; Split message into blocks</h3><pre><code><code>B1..B(n-1),last = split_into_16B_blocks(M)</code></code></pre><p>Now two cases:</p><h3>Case 1 &#8212; last block is FULL (exactly 16 bytes)</h3><pre><code><code>M_last = last XOR K1</code></code></pre><h3>Case 2 &#8212; last block is PARTIAL (0..15 bytes)</h3><p>Pad it first (append 0x80 then zeros), then:</p><pre><code><code>last_padded = pad_7816_4(last)
M_last      = last_padded XOR K2</code></code></pre><h3>Step C &#8212; Run the chaining as before, but finalize with M_last</h3><pre><code><code>X = 0^128
for i in 1..(n-1):
    X = AES(K, X XOR Bi)

tag = AES(K, X XOR M_last)</code></code></pre><p>That&#8217;s the &#8220;fixed ending&#8221; logic in full.</p><h3>A very important clarification: there is nothing to decrypt here</h3><p>At this point, it&#8217;s worth stopping for a second and being explicit.</p><p>The output of Fix attempt #5 &#8212; the <strong>tag</strong> &#8212; is <strong>not encrypted data</strong>.</p><p>It is:</p><ul><li><p>not reversible</p></li><li><p>not meant to be decrypted</p></li><li><p>not carrying the message inside it</p></li></ul><p>It is the <strong>final internal state</strong> of a compression process.</p><p>Verification works like this:</p><ol><li><p>The receiver already has the message <code>M</code></p></li><li><p>The receiver recomputes the tag <strong>from scratch</strong> using the same algorithm and the same key</p></li><li><p>The receiver compares the two tags</p></li></ol><p>If they match &#8594; accept</p><p>If they don&#8217;t &#8594; reject</p><p>There is no &#8220;decryption step&#8221; for the tag, because <strong>authentication is not a transformation &#8212; it&#8217;s a decision</strong>.</p><p>This is a crucial mental shift:</p><blockquote><p>Encryption answers: &#8220;What was the message?&#8221;</p><p>MAC answers: <em>&#8220;Can I trust this message?&#8221;</em></p></blockquote><p>Trying to &#8220;decrypt a MAC&#8221; is like trying to &#8220;decrypt a checksum&#8221;.</p><p>There is nothing there to recover.</p><blockquote><p>If you feel uncomfortable that the tag can&#8217;t be decrypted &#8212; good. That discomfort means you&#8217;ve stopped thinking about authentication as encryption.</p></blockquote><div><hr></div><h1>Name reveal: <strong>CMAC</strong> (and what we accidentally reinvented)</h1><p>So what did we just build?</p><ul><li><p>We built a <strong>compression function</strong>: message &#8594; fixed-size tag</p><p>(not zip compression &#8212; cryptographic compression: folding data into state)</p></li><li><p>We built <strong>chaining</strong>: a state that evolves block-by-block.</p></li><li><p>We built a <strong>CBC-style MAC core</strong>: <code>X = AES(K, X XOR Bi)</code>.</p></li><li><p>We discovered the &#8220;variable length&#8221; landmine, and fixed it with:</p><ul><li><p><strong>finalization</strong></p></li><li><p><strong>domain separation</strong> for the last block (full vs partial)</p></li><li><p><strong>subkeys</strong> <code>K1</code>, <code>K2</code></p></li><li><p><strong>padding</strong> for the partial last block</p></li></ul></li></ul><p>And once you assemble those pieces, the whole thing has a name:</p><blockquote><p>CMAC &#8212; Cipher-based Message Authentication Code.</p></blockquote><p>CMAC is what remains after you remove all the broken variants.</p><div><hr></div><h1>Python implementation (AES-CMAC)</h1><p>We&#8217;ll use an existing crypto library (because we are not trying to spend 3 weeks reimplementing AES here).</p><p>Install:</p><pre><code><code>pip install pycryptodome</code></code></pre><p>And here is a minimal CMAC implementation (plus a self-test with NIST vectors):</p><pre><code><code>"""
AES-CMAC (CMAC) &#8212; final implementation.

- Uses PyCryptodome for AES-ECB (the block primitive).
- Implements CMAC per NIST SP 800-38B:
    * Subkeys K1/K2 derived from L = AES_K(0^128)
    * CBC-style chaining with IV=0
    * Special last-block handling:
        - full last block  -&gt; XOR K1
        - partial last block -&gt; pad (7816-4) then XOR K2

Install:
    pip install pycryptodome
"""

from __future__ import annotations
from Crypto.Cipher import AES
from Crypto.Hash import CMAC as CMAC_LIB

BLOCK_SIZE = 16
RB = 0x87  # Rb for 128-bit CMAC


def _xor(a: bytes, b: bytes) -&gt; bytes:
    if len(a) != len(b):
        raise ValueError("XOR requires equal-length inputs.")
    return bytes(x ^ y for x, y in zip(a, b))


def _left_shift_1(block: bytes) -&gt; bytes:
    """Shift a 128-bit block left by 1 bit."""
    if len(block) != BLOCK_SIZE:
        raise ValueError("Expected 16-byte block.")
    out = bytearray(BLOCK_SIZE)
    carry = 0
    for i in range(BLOCK_SIZE - 1, -1, -1):
        out[i] = ((block[i] &lt;&lt; 1) &amp; 0xFF) | carry
        carry = (block[i] &gt;&gt; 7) &amp; 1
    return bytes(out)


def _pad_7816_4(partial: bytes) -&gt; bytes:
    """
    ISO/IEC 7816-4 padding: append 0x80 then zeros to reach 16 bytes.
    Used only when the last block is partial (len &lt; 16).
    """
    if len(partial) &gt;= BLOCK_SIZE:
        raise ValueError("pad_7816_4 expects len(partial) &lt; 16.")
    return partial + b"\x80" + b"\x00" * (BLOCK_SIZE - len(partial) - 1)


def _dbl(block: bytes) -&gt; bytes:
    """
    GF(2^128) doubling used for CMAC subkeys.

    dbl(x) = (x&lt;&lt;1)           if MSB(x) = 0
             (x&lt;&lt;1) XOR Rb    if MSB(x) = 1
    """
    if len(block) != BLOCK_SIZE:
        raise ValueError("Expected 16-byte block.")
    shifted = _left_shift_1(block)
    if block[0] &amp; 0x80:  # MSB(x) = 1
        shifted = shifted[:-1] + bytes([shifted[-1] ^ RB])
    return shifted


def _generate_subkeys(aes_ecb_encrypt) -&gt; tuple[bytes, bytes]:
    """
    Subkeys:
      L  = AES_K(0^128)
      K1 = dbl(L)
      K2 = dbl(K1)
    """
    L = aes_ecb_encrypt(bytes(BLOCK_SIZE))

    K1 = _dbl(L)
    K2 = _dbl(K1)

    return K1, K2


def aes_cmac(key: bytes, msg: bytes) -&gt; bytes:
    """
    Compute AES-CMAC tag (16 bytes).

    key: 16/24/32 bytes (AES-128/192/256)
    msg: arbitrary bytes
    """
    if len(key) not in (16, 24, 32):
        raise ValueError("AES key must be 16, 24, or 32 bytes.")

    aes = AES.new(key, AES.MODE_ECB)

    def aes_ecb_encrypt(block: bytes) -&gt; bytes:
        if len(block) != BLOCK_SIZE:
            raise ValueError("AES-ECB expects 16-byte blocks.")
        return aes.encrypt(block)

    K1, K2 = _generate_subkeys(aes_ecb_encrypt)

    # Number of blocks (CMAC treats empty message as one partial block)
    n = (len(msg) + BLOCK_SIZE - 1) // BLOCK_SIZE
    if n == 0:
        n = 1

    # Split: first n-1 full blocks, and a last block (0..16 bytes)
    blocks = [msg[i * BLOCK_SIZE:(i + 1) * BLOCK_SIZE] for i in range(n - 1)]
    last = msg[(n - 1) * BLOCK_SIZE:]  # may be empty, partial, or full

    # Prepare final block with domain separation
    if len(last) == BLOCK_SIZE:
        m_last = _xor(last, K1)
    else:
        m_last = _xor(_pad_7816_4(last), K2)

    # CBC-like chaining with IV=0 on blocks[0..n-2]
    X = bytes(BLOCK_SIZE)
    for b in blocks:
        if len(b) != BLOCK_SIZE:
            raise ValueError("Internal error: non-full intermediate block.")
        X = aes_ecb_encrypt(_xor(X, b))

    # Final tag
    T = aes_ecb_encrypt(_xor(X, m_last))
    return T


# ---------------------------
# Verification helpers/tests
# ---------------------------

def _hx(s: str) -&gt; bytes:
    return bytes.fromhex(s.replace(" ", "").replace("\n", ""))


def verify_against_library(key: bytes, msg: bytes) -&gt; None:
    """Cross-check our CMAC vs PyCryptodome's CMAC."""
    mine = aes_cmac(key, msg)
    lib = CMAC_LIB.new(key, ciphermod=AES)
    lib.update(msg)
    theirs = lib.digest()
    assert mine == theirs, (
        "CMAC mismatch!\n"
        f"mine   = {mine.hex()}\n"
        f"theirs = {theirs.hex()}"
    )


def self_test() -&gt; None:
    """
    Known CMAC values for the famous NIST key + messages from SP 800-38B context.
    We *also* verify with PyCryptodome CMAC to avoid any &#8220;vector confusion&#8221;.
    """
    key = _hx("2b7e151628aed2a6abf7158809cf4f3c")

    msg1 = _hx("6bc1bee22e409f96e93d7e117393172a")  # 16 bytes
    msg2 = _hx("ae2d8a571e03ac9c9eb76fac45af8e51")  # 16 bytes
    msg3 = _hx("30c81c46a35ce411e5fbc1191a0a52ef")  # 16 bytes
    msg4 = _hx("f69f2445df4f9b17ad2b417be66c3710")  # 16 bytes

    tests = [
        (b"", "bb1d6929e95937287fa37d129b756746"),                       # 0
        (msg1, "070a16b46b4d4144f79bdd9dd04a287c"),                     # 16
        (msg1 + msg2, "ce0cbf1738f4df6428b1d93bf12081c9"),              # 32
        (msg1 + msg2 + msg3[:8], "dfa66747de9ae63030ca32611497c827"),   # 40
        (msg1 + msg2 + msg3 + msg4, "51f0bebf7e3b9d92fc49741779363cfe") # 64
    ]

    for m, expected_hex in tests:
        got = aes_cmac(key, m).hex()
        assert got == expected_hex, f"Vector mismatch: got {got}, expected {expected_hex}"
        verify_against_library(key, m)


if __name__ == "__main__":
    self_test()
    print("AES-CMAC OK (vectors + library cross-check passed)")
</code></code></pre><p>The final version is on github: https://github.com/DmytroHuzz/build_own_mac</p><h1>One last observation (and the bridge to Part 3)</h1><p>Look at what we built.</p><p>This tag function:</p><ul><li><p>takes arbitrary-length input</p></li><li><p>updates a small internal state block by block</p></li><li><p>produces a fixed-size output</p></li></ul><p>That is&#8230; suspiciously familiar.</p><p>It looks like the shape of a hash function.</p><p>So in the next article we&#8217;ll do the same trick again:</p><blockquote><p>Instead of forcing AES to behave like a compressor, let&#8217;s start from a primitive that is <em>already</em> a compressor.</p></blockquote><p>And we&#8217;ll see if we can reinvent a hash-based MAC in the same &#8220;no names until the end&#8221; style.</p><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Building Own MAC (Message Authentication Code): Part 1 - Encrypted, but Not Trusted]]></title><description><![CDATA[Why encryption alone is not enough]]></description><link>https://www.dmytrohuz.com/p/building-own-mac-part-1-encrypted</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/building-own-mac-part-1-encrypted</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Sat, 10 Jan 2026 21:59:26 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!bwTN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02b23a1a-ae7b-4e25-ada3-9b75c1634b23_900x672.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!bwTN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02b23a1a-ae7b-4e25-ada3-9b75c1634b23_900x672.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!bwTN!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02b23a1a-ae7b-4e25-ada3-9b75c1634b23_900x672.jpeg 424w, https://substackcdn.com/image/fetch/$s_!bwTN!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02b23a1a-ae7b-4e25-ada3-9b75c1634b23_900x672.jpeg 848w, https://substackcdn.com/image/fetch/$s_!bwTN!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02b23a1a-ae7b-4e25-ada3-9b75c1634b23_900x672.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!bwTN!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02b23a1a-ae7b-4e25-ada3-9b75c1634b23_900x672.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!bwTN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02b23a1a-ae7b-4e25-ada3-9b75c1634b23_900x672.jpeg" width="900" height="672" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/02b23a1a-ae7b-4e25-ada3-9b75c1634b23_900x672.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:672,&quot;width&quot;:900,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:143355,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/jpeg&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/184157394?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02b23a1a-ae7b-4e25-ada3-9b75c1634b23_900x672.jpeg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!bwTN!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02b23a1a-ae7b-4e25-ada3-9b75c1634b23_900x672.jpeg 424w, https://substackcdn.com/image/fetch/$s_!bwTN!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02b23a1a-ae7b-4e25-ada3-9b75c1634b23_900x672.jpeg 848w, https://substackcdn.com/image/fetch/$s_!bwTN!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02b23a1a-ae7b-4e25-ada3-9b75c1634b23_900x672.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!bwTN!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02b23a1a-ae7b-4e25-ada3-9b75c1634b23_900x672.jpeg 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><h2>Huston, we have a problem</h2><p>All right, now we have a super-duper cipher &#8212; AES. (You don&#8217;t? 0_o That means you missed the previous series of articles where we, with paper, glue, and a bit of magic, built our own AES cipher from scratch. Stop reading. Go grab it: <a href="https://www.dmytrohuz.com/p/building-own-block-cipher-part-3">Building Own Block Cipher: Part 3 - AES</a>)</p><p>We can encrypt any message, and only the person who knows the secret key can decrypt it. The rest of the world would need a billion years to break it.</p><p>Does this mean we&#8217;re fine now? Did we finish cryptography? Is there nothing left to worry about?</p><p><strong>Let&#8217;s make a small experiment.</strong></p><p>We created an API for the bank. And one simplified endpoint looks like this:</p><pre><code><code>POST: /api/transfer

BODY
user=$NAME
transaction=$AMOUNT
</code></code></pre><p>Assume the bank can decrypt the request. Ignore key management for now &#8212; this story is not about that.</p><p>Allice wants to send Bob 10$.</p><p>She prepares the message:</p><pre><code><code>user=Bob; transaction=10
</code></code></pre><p>She encrypts the message with AES and sent to the bank.</p><pre><code><code>POST: /api/transfer
BODY:
"a94f4aabdf..."
</code></code></pre><p>In a few minutes she received a notification from the bank:</p><blockquote><p>Your transaction of <strong>10 000$</strong> to the Bob is successful.</p></blockquote><p>0_o&#8230;</p><p>That was&#8230;unexpected.</p><h3><strong>What just happened?</strong></h3><p>A hacker has been quietly listening to Alice&#8217;s network traffic for weeks.</p><p>He cannot decrypt anything. AES is doing its job.</p><p>But he <em>can</em> see many encrypted transfers going back and forth.</p><p>Over time, he notices something interesting:</p><p>small, predictable changes in the encrypted data cause predictable changes <strong>after decryption</strong>.</p><p>So he modifies the encrypted message &#8212; just a little.</p><p>The bank decrypts the message successfully.</p><p>And that is the problem.</p><p>The message was decrypted&#8230;successfully &#129318;&#127996;&#8205;&#9794;&#65039;</p><p>But it was <strong>not authentic!!!</strong></p><p>Yes, this example is simplified. Real attacks look different.</p><p>But the idea is very real and very dangerous.</p><p>We can no longer blindly trust encrypted data.</p><p>Houston, we have a problem.</p><h2>It&#8217;s a Cipher&#8230; It&#8217;s a Hash&#8230; It&#8217;s SuperMAC</h2><p>So.</p><p>We encrypted the message.</p><p>AES did its job.</p><p>The attacker still won.</p><p>That should feel wrong.</p><p>Let&#8217;s rewind a bit.</p><div><hr></div><h3>What encryption actually promised us</h3><p>Encryption is very honest.</p><p>It promises exactly one thing:</p><blockquote><p>&#8220;If you don&#8217;t know the key, you can&#8217;t read this message.&#8221;</p></blockquote><p>That&#8217;s it.</p><p>No hidden features. No bonus guarantees.</p><p>And to be fair &#8212; it kept that promise perfectly.</p><p>The hacker never learned:</p><ul><li><p>who Alice paid</p></li><li><p>how much she paid</p></li><li><p>what the message even says</p></li></ul><p>AES was innocent.</p><div><hr></div><h3>Then why did everything break?</h3><p>Because we quietly assumed something else.</p><p>We assumed that:</p><blockquote><p>&#8220;If the message decrypts &#8212; it must be OK.&#8221;</p></blockquote><p>And that assumption is false.</p><p>Encryption does <strong>not</strong> promise that:</p><ul><li><p>the message wasn&#8217;t modified</p></li><li><p>the message was constructed intentionally</p></li><li><p>the message makes sense</p></li><li><p>the message is safe to execute</p></li></ul><p>It only promises secrecy.</p><p>And yes &#8212; secrecy <strong>is still necessary</strong>.</p><p>We absolutely want the attacker to stay blind.</p><p>But secrecy alone is not enough.</p><div><hr></div><h3>What the bank actually needed</h3><p>The bank needed one more answer.</p><p>Not a complicated one.</p><p>Just this:</p><blockquote><p>&#8220;Was this message created by someone who knows the secret &#8212; and was it changed on the way?&#8221;</p></blockquote><p>Encryption cannot answer that question.</p><p>So we don&#8217;t throw encryption away.</p><p>We <strong>add something next to it</strong>.</p><p>Not to hide the message.</p><p>But to protect it.</p><div><hr></div><h3>&#8220;Can&#8217;t we just hash it?&#8221;</h3><p>At this point, many people say:</p><blockquote><p>&#8220;Okay, fine. Let&#8217;s just hash the message.&#8221;</p></blockquote><p>Hashes are nice.</p><ul><li><p>change one bit &#8594; completely different output</p></li><li><p>fast</p></li><li><p>simple</p></li></ul><p>But there&#8217;s a problem.</p><p>Hashes have no secrets.</p><p>Anyone can compute them.</p><p>Which means anyone can fake them.</p><p>So hashes alone don&#8217;t help.</p><div><hr></div><h3>So&#8230; SuperMAC?</h3><p>Let&#8217;s make a small trick.</p><p>Alice is still sending a message to the bank.</p><p>She already knows how to encrypt it.</p><p>We don&#8217;t change that part.</p><p>Now we add one more step.</p><p>Before sending the message, Alice takes:</p><ul><li><p>the message itself</p></li><li><p>a secret key (shared with the bank)</p></li></ul><p>And she computes a small extra value.</p><p>Call it a <strong>tag</strong>.</p><p>This tag is not encrypted data.</p><p>It does not hide anything.</p><p>It&#8217;s just a short fingerprint that depends on:</p><ul><li><p>the message</p></li><li><p>and the secret key</p></li></ul><p>Now Alice sends <strong>two things</strong> to the bank:</p><ul><li><p>the encrypted message</p></li><li><p>the tag</p></li></ul><p>That&#8217;s it.</p><div><hr></div><h3>What does the bank do?</h3><p>The bank receives:</p><ul><li><p>the encrypted message</p></li><li><p>the tag</p></li></ul><p>It decrypts the message.</p><p>Then it does the same computation itself:</p><ul><li><p>same message</p></li><li><p>same secret key</p></li></ul><p>If the newly computed tag matches the received one &#8212; good.</p><p>The message was not modified.</p><p>If it doesn&#8217;t &#8212; something is wrong.</p><p>The message is rejected.</p><p>No guessing.</p><p>No &#8220;maybe it&#8217;s fine&#8221;.</p><p>Just a hard <strong>yes</strong> or <strong>no</strong>.</p><h3>So what is a MAC, finally?</h3><p>This is it.</p><p>That <strong>tag</strong> we just computed</p><p><em>is</em> the <strong>Message Authentication Code</strong>.</p><p>Nothing more.</p><p>Nothing less.</p><p>A MAC is:</p><ul><li><p>a small piece of data</p></li><li><p>computed from the message</p></li><li><p>using a secret key</p></li><li><p>and verified on the other side</p></li></ul><p>If the tag matches &#8212; the message is authentic.</p><p>If it doesn&#8217;t &#8212; the message is rejected.</p><p>That&#8217;s the whole mechanism.</p><div><hr></div><h3>Important clarification</h3><p>A MAC does <strong>not</strong> replace encryption.</p><p>We still need encryption.</p><p>We still want secrecy.</p><p>The MAC adds something else.</p><p>It adds:</p><ul><li><p><strong>integrity</strong> &#8212; the message was not modified</p></li><li><p><strong>authenticity</strong> &#8212; the message was created by someone who knows the secret</p></li></ul><p>So the picture now looks like this:</p><ul><li><p><strong>Encryption</strong> hides the message</p></li><li><p><strong>MAC</strong> protects the message</p></li></ul><p>Two different tools.</p><p>Two different guarantees.</p><p>Used together.</p><div><hr></div><h3>Why this extra layer matters</h3><p>Without a MAC:</p><ul><li><p>modified messages can slip through</p></li><li><p>decryption can succeed on garbage</p></li><li><p>the system has no way to say &#8220;stop&#8221;</p></li></ul><p>With a MAC:</p><ul><li><p>every modification is detected</p></li><li><p>every forged message is rejected</p></li><li><p>the system can finally trust what it decrypts</p></li></ul><p>This is why real systems don&#8217;t use encryption alone.</p><p>They use <strong>encryption + authentication</strong>.</p><h2>Final thoughts &#8212; and what comes next</h2><p>Over my career, I&#8217;ve learned one important thing.</p><p>If you can <strong>detect</strong> the problem and then <strong>define</strong> it correctly &#8212; you already solved about <strong>60%</strong> of it.</p><p>Another <strong>38%</strong> is designing the right architecture.</p><p>And the remaining <strong>2%</strong> is implementation.</p><p>In this article, we focused on the biggest and most underestimated part of the problem:</p><p><strong>trusting encrypted data</strong>.</p><p>We saw that encryption alone is not enough.</p><p>We saw why &#8220;it decrypts successfully&#8221; is not a security guarantee.</p><p>And we saw what kind of extra property we are missing.</p><p>Deliberately, we stopped there.</p><div><hr></div><h3>Why we stop here</h3><p>Because a MAC is not a single algorithm.</p><p>It is not a cipher.</p><p>It is not a trick.</p><p>It is not one formula.</p><p>A MAC is a <strong>family of designs</strong>.</p><p>And before touching any code, it&#8217;s much more important to understand:</p><ul><li><p><em>how</em> MACs are built</p></li><li><p><em>which architectures exist</em></p></li><li><p>and <em>which building blocks they rely on</em></p></li></ul><p>That&#8217;s what the next article is about.</p><div><hr></div><h3>What&#8217;s next</h3><p>In the next article, we will:</p><ul><li><p>take a close look at the <strong>two main architectures</strong> used to build MACs</p></li><li><p>zoom in on the <strong>primitives</strong> used inside those architectures</p></li><li><p>understand why these designs work &#8212; and why others don&#8217;t</p></li></ul><p>Only after that, in the final part, we&#8217;ll move to implementation.</p><p>We&#8217;ll:</p><ul><li><p>implement the primitives (starting with SHA-256)</p></li><li><p>and then build a MAC on top of them</p></li><li><p>completely from scratch</p></li></ul><p>No black boxes.</p><p>No &#8220;just trust the library&#8221;.</p><p>No magic.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.dmytrohuz.com/subscribe?"><span>Subscribe now</span></a></p>]]></content:encoded></item><item><title><![CDATA[Progress, Abstraction, Diversity, and Why Advanced Systems Break]]></title><description><![CDATA[How abstraction, diversity, and entropy shape the rise and failure of complex systems]]></description><link>https://www.dmytrohuz.com/p/progress-abstraction-diversity-and</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/progress-abstraction-diversity-and</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Mon, 05 Jan 2026 18:58:14 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!MKLg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82b96498-1493-4ffb-8e57-9be90ec96b3e_1024x608.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MKLg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82b96498-1493-4ffb-8e57-9be90ec96b3e_1024x608.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MKLg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82b96498-1493-4ffb-8e57-9be90ec96b3e_1024x608.png 424w, https://substackcdn.com/image/fetch/$s_!MKLg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82b96498-1493-4ffb-8e57-9be90ec96b3e_1024x608.png 848w, https://substackcdn.com/image/fetch/$s_!MKLg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82b96498-1493-4ffb-8e57-9be90ec96b3e_1024x608.png 1272w, https://substackcdn.com/image/fetch/$s_!MKLg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82b96498-1493-4ffb-8e57-9be90ec96b3e_1024x608.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MKLg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82b96498-1493-4ffb-8e57-9be90ec96b3e_1024x608.png" width="1024" height="608" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/82b96498-1493-4ffb-8e57-9be90ec96b3e_1024x608.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:608,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!MKLg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82b96498-1493-4ffb-8e57-9be90ec96b3e_1024x608.png 424w, https://substackcdn.com/image/fetch/$s_!MKLg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82b96498-1493-4ffb-8e57-9be90ec96b3e_1024x608.png 848w, https://substackcdn.com/image/fetch/$s_!MKLg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82b96498-1493-4ffb-8e57-9be90ec96b3e_1024x608.png 1272w, https://substackcdn.com/image/fetch/$s_!MKLg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F82b96498-1493-4ffb-8e57-9be90ec96b3e_1024x608.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>Progress is usually described as accumulation: more knowledge, more wealth, more technology.</p><p>That story is incomplete.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>A more accurate description is this: <strong>progress is an increase in abstraction density</strong>.</p><p>Early societies solve problems directly in the physical world.</p><p>Advanced societies solve them by building systems that <em>represent</em> reality &#8212; laws, markets, institutions, software, models. These abstractions compress complexity and allow coordination at scale.</p><p>This works. Until it doesn&#8217;t.</p><div><hr></div><h3>Abstraction as stored order</h3><p>Abstractions are not decorations on top of reality. They are <strong>frozen decisions</strong>.</p><p>A law is a stored resolution to a class of conflicts.</p><p>Money is stored trust.</p><p>Code is stored behavior.</p><p>Institutions are stored roles and expectations.</p><p>Each abstraction reduces entropy by preventing endless renegotiation. Instead of resolving chaos every time, the system reuses structure.</p><p>This is why abstraction scales.</p><p>And this is why advanced societies depend on it.</p><div><hr></div><h3>The hidden cost of abstraction</h3><p>Every abstraction is a <strong>compression</strong>. And every compression loses information.</p><p>As abstractions grow more complex:</p><ul><li><p>fewer people understand them</p></li><li><p>fewer people can modify them</p></li><li><p>fewer alternatives are explored</p></li></ul><p>This introduces fragility.</p><p>When abstractions drift away from reality, they don&#8217;t fail gracefully. They fail suddenly &#8212; because the system has forgotten how to operate without them.</p><p>At that point, entropy returns in a predictable form:</p><ul><li><p>force instead of legitimacy</p></li><li><p>micromanagement instead of systems</p></li><li><p>physical control instead of abstract coordination</p></li></ul><p>This is not regression by choice.</p><p>It is <strong>abstraction failure</strong>.</p><div><hr></div><h3>Why diversity matters (and why it&#8217;s not optional)</h3><p>Biological systems survive uncertainty through variation.</p><p>Apple trees reject their own pollen to avoid genetic monoculture.</p><p>Immune systems generate millions of useless antibodies to find a few that work.</p><p>Evolution wastes endlessly to avoid fragility.</p><p>Human societies face the same problem &#8212; but at a cognitive level.</p><p>No one knows:</p><ul><li><p>which ideology will fit the next technological shift</p></li><li><p>which institutional design will survive the next shock</p></li><li><p>which abstraction will remain aligned with reality</p></li></ul><p>So societies evolve a different strategy: <strong>parallel abstractions</strong>.</p><p>Religions, political systems, economic models, cultural norms &#8212; these are not noise. They are <strong>ideological diversity</strong>, and they serve the same function as genetic diversity: exploration of the solution space.</p><p>Most ideas are wrong.</p><p>That is not a bug. That is the point.</p><div><hr></div><h3>The monoculture trap</h3><p>Every advanced system is tempted by monoculture.</p><p>A single ideology.</p><p>A single &#8220;correct&#8221; model.</p><p>A single optimal abstraction.</p><p>Monocultures feel efficient. They eliminate friction. They simplify coordination.</p><p>They also collapse catastrophically.</p><p>Why?</p><p>Because when reality changes &#8212; and it always does &#8212; there are no alternatives left to adapt. Suppressed ideas don&#8217;t disappear; they decay underground, untested, until the dominant abstraction fails and the system has nothing ready to replace it.</p><p>At that point, systems don&#8217;t innovate. They panic.</p><div><hr></div><h3>Stress increases variance (this explains polarization)</h3><p>Under stress, biological systems increase mutation rates.</p><p>Social systems do the same.</p><p>Polarization, ideological fragmentation, and radical disagreement are often framed as dysfunction. Another interpretation is more precise: <strong>the system is exploring</strong>.</p><p>When existing abstractions stop predicting reality, societies generate stronger, more divergent alternatives. This is noisy, dangerous, and emotionally unpleasant &#8212; but it is also adaptive.</p><p>Total consensus under stress is not stability.</p><p>It is blindness.</p><div><hr></div><h3>Where abstraction and diversity intersect</h3><p>Here is the key synthesis:</p><ul><li><p><strong>Abstractions fight entropy</strong></p></li><li><p><strong>Diverse abstractions prevent catastrophic failure</strong></p></li></ul><p>Progress increases abstraction depth.</p><p>Abstraction depth increases leverage.</p><p>It also increases fragility.</p><p>The only known hedge against that fragility is <strong>plurality of abstractions</strong> &#8212; competing models of reality that prevent total collapse when one drifts too far from the world it represents.</p><p>This is not moral pluralism.</p><p>It is structural necessity.</p><div><hr></div><h3>Failure modes (predictive power)</h3><p>This lens predicts several recurring patterns:</p><ul><li><p>When abstraction maintenance exceeds abstraction renewal &#8594; stagnation</p></li><li><p>When ideological diversity collapses &#8594; fragility spikes</p></li><li><p>When understanding concentrates in too few minds &#8594; control replaces legitimacy</p></li><li><p>When models are trusted more than feedback &#8594; sudden failure</p></li></ul><p>These patterns appear in empires, corporations, institutions, and now in AI-heavy systems.</p><p>Different domain. Same mechanism.</p><div><hr></div><h3>The uncomfortable conclusion</h3><p>Individuals crave certainty.</p><p>Systems require disagreement.</p><p>Evolution resolves this conflict by making individuals commit strongly to single abstractions &#8212; while allowing populations to fragment into many.</p><p>No one intends to preserve ideological diversity.</p><p>It emerges anyway, because systems that don&#8217;t do this die.</p><p>Uniformity feels safe.</p><p>It just doesn&#8217;t survive reality.</p><div><hr></div><h3>Final claim</h3><p>Abstraction is how advanced systems hold entropy at bay.</p><p>Diversity of abstraction is how they survive when those abstractions fail.</p><p>Any civilization that optimizes one without the other is not progressing.</p><p>It is loading failure into the future.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Building Own Block Cipher: Part 3 - AES]]></title><description><![CDATA[Building AES the IKEA Way: Follow the Manual or It Falls Apart]]></description><link>https://www.dmytrohuz.com/p/building-own-block-cipher-part-3</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/building-own-block-cipher-part-3</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Thu, 25 Dec 2025 10:49:50 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!yxEG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ab6e26-dbe9-473d-b4d5-4c38838a8c48_1024x608.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!yxEG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ab6e26-dbe9-473d-b4d5-4c38838a8c48_1024x608.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!yxEG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ab6e26-dbe9-473d-b4d5-4c38838a8c48_1024x608.png 424w, https://substackcdn.com/image/fetch/$s_!yxEG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ab6e26-dbe9-473d-b4d5-4c38838a8c48_1024x608.png 848w, https://substackcdn.com/image/fetch/$s_!yxEG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ab6e26-dbe9-473d-b4d5-4c38838a8c48_1024x608.png 1272w, https://substackcdn.com/image/fetch/$s_!yxEG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ab6e26-dbe9-473d-b4d5-4c38838a8c48_1024x608.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!yxEG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ab6e26-dbe9-473d-b4d5-4c38838a8c48_1024x608.png" width="1024" height="608" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e5ab6e26-dbe9-473d-b4d5-4c38838a8c48_1024x608.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:608,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!yxEG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ab6e26-dbe9-473d-b4d5-4c38838a8c48_1024x608.png 424w, https://substackcdn.com/image/fetch/$s_!yxEG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ab6e26-dbe9-473d-b4d5-4c38838a8c48_1024x608.png 848w, https://substackcdn.com/image/fetch/$s_!yxEG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ab6e26-dbe9-473d-b4d5-4c38838a8c48_1024x608.png 1272w, https://substackcdn.com/image/fetch/$s_!yxEG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ab6e26-dbe9-473d-b4d5-4c38838a8c48_1024x608.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p></p><p>In the previous article</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div class="embedded-post-wrap" data-attrs="{&quot;id&quot;:180272519,&quot;url&quot;:&quot;https://dmytrohuz.substack.com/p/building-your-own-block-cipher-part&quot;,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!x7qR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe14b37a2-0818-4b6d-9a18-e3f9717989d6_144x144.png&quot;,&quot;title&quot;:&quot;Building Own Block Cipher: Part 2 &#8212; Block Cipher Theory &amp; Rebuilding DES &quot;,&quot;truncated_body_text&quot;:&quot;&#128312; 1. Introduction&quot;,&quot;date&quot;:&quot;2025-11-29T20:39:54.507Z&quot;,&quot;like_count&quot;:2,&quot;comment_count&quot;:0,&quot;bylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;handle&quot;:&quot;dmytrohuz&quot;,&quot;previous_name&quot;:null,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e660032f-1e0c-4a8d-ad53-66adfa358f18_320x320.png&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder &#8212; Learning, building, and documenting the systems that connect fundamentals with frontiers.&quot;,&quot;profile_set_up_at&quot;:&quot;2025-09-13T20:56:59.242Z&quot;,&quot;reader_installed_at&quot;:&quot;2025-09-18T13:55:12.703Z&quot;,&quot;publicationUsers&quot;:[{&quot;id&quot;:6399670,&quot;user_id&quot;:392416265,&quot;publication_id&quot;:6272314,&quot;role&quot;:&quot;admin&quot;,&quot;public&quot;:true,&quot;is_primary&quot;:false,&quot;publication&quot;:{&quot;id&quot;:6272314,&quot;name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;subdomain&quot;:&quot;dmytrohuz&quot;,&quot;custom_domain&quot;:&quot;www.dmytrohuz.com&quot;,&quot;custom_domain_optional&quot;:true,&quot;hero_text&quot;:&quot;My personal Substack&quot;,&quot;logo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e14b37a2-0818-4b6d-9a18-e3f9717989d6_144x144.png&quot;,&quot;author_id&quot;:392416265,&quot;primary_user_id&quot;:392416265,&quot;theme_var_background_pop&quot;:&quot;#FF6719&quot;,&quot;created_at&quot;:&quot;2025-09-13T20:57:02.215Z&quot;,&quot;email_from_name&quot;:null,&quot;copyright&quot;:&quot;Dmytro Huz&quot;,&quot;founding_plan_name&quot;:&quot;Founding Member&quot;,&quot;community_enabled&quot;:true,&quot;invite_only&quot;:false,&quot;payments_state&quot;:&quot;enabled&quot;,&quot;language&quot;:null,&quot;explicit&quot;:false,&quot;homepage_type&quot;:&quot;newspaper&quot;,&quot;is_personal_mode&quot;:false}}],&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null,&quot;status&quot;:{&quot;bestsellerTier&quot;:null,&quot;subscriberTier&quot;:null,&quot;leaderboard&quot;:null,&quot;vip&quot;:false,&quot;badge&quot;:null,&quot;paidPublicationIds&quot;:[],&quot;subscriber&quot;:null}}],&quot;utm_campaign&quot;:null,&quot;belowTheFold&quot;:false,&quot;type&quot;:&quot;newsletter&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="EmbeddedPostToDOM"><a class="embedded-post" native="true" href="https://dmytrohuz.substack.com/p/building-your-own-block-cipher-part?utm_source=substack&amp;utm_campaign=post_embed&amp;utm_medium=web"><div class="embedded-post-header"><img class="embedded-post-publication-logo" src="https://substackcdn.com/image/fetch/$s_!x7qR!,w_56,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe14b37a2-0818-4b6d-9a18-e3f9717989d6_144x144.png"><span class="embedded-post-publication-name">Dmytro&#8217;s Substack</span></div><div class="embedded-post-title-wrapper"><div class="embedded-post-title">Building Own Block Cipher: Part 2 &#8212; Block Cipher Theory &amp; Rebuilding DES </div></div><div class="embedded-post-body">&#128312; 1. Introduction&#8230;</div><div class="embedded-post-cta-wrapper"><span class="embedded-post-cta">Read more</span></div><div class="embedded-post-meta">5 months ago &#183; 2 likes &#183; Dmytro Huz</div></a></div><p>we built <strong>DES from scratch</strong> &#8212; not because it&#8217;s still relevant, but because it forces you to understand how block ciphers are assembled from specifications.</p><p>DES is awkward, bit-heavy, and full of historical baggage.</p><p>That&#8217;s exactly why it&#8217;s a good learning tool.</p><p>AES continues that journey &#8212; but with very different design goals and very different failure modes.</p><p>Where DES teaches <em>structure</em>,</p><p>AES teaches <em>discipline</em>.</p><div><hr></div><p>Before touching code, let&#8217;s be honest about one thing:</p><p><strong>AES is not interesting because it&#8217;s new.</strong></p><p>It&#8217;s interesting because it&#8217;s <em>everywhere</em>.</p><p>Right now, AES is used in:</p><ul><li><p>HTTPS (TLS)</p></li><li><p>Disk encryption (BitLocker, FileVault, LUKS)</p></li><li><p>Password managers</p></li><li><p>Cloud storage</p></li><li><p>Mobile devices</p></li><li><p>VPNs</p></li><li><p>Hardware security modules</p></li></ul><p>If data is encrypted in 2025, there&#8217;s a very high chance <strong>AES is involved</strong>.</p><p>And yet, despite being studied, standardized, and deployed for more than 20 years, AES is still <strong>frequently implemented incorrectly</strong>.</p><p>Not because AES is broken.</p><p>But because people misunderstand <em>what AES actually is</em>.</p><div><hr></div><h2><strong>AES Is Not Broken &#8212; Implementations Are</strong></h2><p>There are no practical cryptanalytic breaks of AES.</p><p>What <em>does</em> exist is a long history of failures caused by:</p><ul><li><p>using <strong>AES-ECB</strong> (still happening)</p></li><li><p>reusing nonces in CTR or GCM</p></li><li><p>broken key expansion</p></li><li><p>incorrect byte/row/column layout</p></li><li><p>treating AES as &#8220;encryption&#8221; instead of a <strong>primitive</strong></p></li></ul><p>So the goal of this article is not to &#8220;learn AES&#8221;.</p><p>It&#8217;s to remove the illusion that AES is magic.</p><div><hr></div><h2><strong>Scope of This Article (Read This First)</strong></h2><p>This article is an <strong>overview of how AES works and how it is implemented</strong>, based on a real, working implementation.</p><p>&#128073; <strong>The full implementation with detailed comments is here:</strong></p><p><a href="https://github.com/DmytroHuzz/build_own_block_cipher/blob/main/aes/aes.py">https://github.com/DmytroHuzz/build_own_block_cipher/blob/main/aes/aes.py</a></p><p>What you&#8217;ll find on GitHub:</p><ul><li><p>full AES-128 implementation</p></li><li><p>key expansion</p></li><li><p>block encryption</p></li><li><p><strong>CTR mode</strong></p></li><li><p>tests against known vectors</p></li></ul><p>What you&#8217;ll find here:</p><ul><li><p>the mental model</p></li><li><p>the algorithmic structure</p></li><li><p>why the code is written the way it is</p></li></ul><p>Think of this article as the <strong>assembly instructions</strong></p><p>and the GitHub file as the <strong>assembled furniture</strong>.</p><div><hr></div><h2><strong>Reading the AES Specification Like IKEA Instructions</strong></h2><p>AES is defined in <strong>FIPS-197:</strong> <a href="https://nvlpubs.nist.gov/nistpubs/fips/nist.fips.197.pdf">https://nvlpubs.nist.gov/nistpubs/fips/nist.fips.197.pdf</a>.</p><p>The document is:</p><ul><li><p>precise</p></li><li><p>clean</p></li><li><p>unforgiving</p></li></ul><p>It is also extremely easy to <em>think</em> you understand while silently misreading it.</p><p>So I approached the spec the same way I approach IKEA manuals:</p><ul><li><p>don&#8217;t assume understanding</p></li><li><p>follow the order literally</p></li><li><p>assemble exactly what is written</p></li><li><p>only then ask <em>why it works</em></p></li></ul><p>Before touching individual transformations, we need to understand the <strong>whole machine</strong>.</p><div><hr></div><h2><strong>Step 0: The Big Picture &#8212; What AES Actually Does</strong></h2><p>Before key expansion, S-boxes, or finite fields, answer this:</p><p><strong>What does AES look like as a complete algorithm?</strong></p><h3><strong>AES in One Sentence</strong></h3><blockquote><p>AES repeatedly transforms a 4&#215;4 byte matrix (<em>the state</em>) using round-specific keys, until the original plaintext is no longer recognizable.</p></blockquote><p>That&#8217;s it.</p><p>Everything else is detail.</p><div><hr></div><h2><strong>The Three Moving Parts of AES</strong></h2><p>AES consists of exactly <strong>three conceptual components</strong>:</p><h3><strong>1. The State</strong></h3><ul><li><p>16 bytes (128 bits)</p></li><li><p>arranged as a 4&#215;4 matrix</p></li><li><p>transformed <em>in place</em></p></li></ul><h3><strong>2. The Round Keys</strong></h3><ul><li><p>derived from the original key</p></li><li><p>one per round</p></li><li><p>injected using XOR</p></li></ul><h3><strong>3. The Round Function</strong></h3><ul><li><p>a fixed sequence of transformations</p></li><li><p>applied repeatedly</p></li></ul><p>No branching.</p><p>No randomness.</p><p>No conditions.</p><p>AES is completely deterministic.</p><div><hr></div><h2><strong>AES as a Loop (Not a Black Box)</strong></h2><p>For AES-128, the algorithm looks like this:</p><pre><code><code>state = plaintext_block_as_state
round_keys = expand_key(key)

state ^= round_keys[0]

for round = 1..9:
    SubBytes(state)
    ShiftRows(state)
    MixColumns(state)
    state ^= round_keys[round]

SubBytes(state)
ShiftRows(state)
state ^= round_keys[10]

ciphertext = state_as_bytes
</code></code></pre><p>That&#8217;s the entire cipher.</p><p>If you understand this loop, <strong>every AES implementation becomes readable</strong>.</p><div><hr></div><h2><strong>Why AES Starts with AddRoundKey</strong></h2><p>AES begins with <strong>AddRoundKey</strong>, before any substitutions or mixing.</p><p>This is deliberate.</p><p>It ensures:</p><ul><li><p>the key affects the cipher immediately</p></li><li><p>no &#8220;raw plaintext&#8221; enters a round</p></li><li><p>every transformation is key-dependent</p></li></ul><p>This is one of those design choices that looks boring &#8212; and turns out to be essential.</p><div><hr></div><h2><strong>Why the Final Round Is Different</strong></h2><p>The final round <strong>does not include MixColumns</strong>.</p><p>Why?</p><p>Because MixColumns exists to:</p><ul><li><p>spread changes across columns</p></li><li><p>increase diffusion <em>between rounds</em></p></li></ul><p>After the final round, there is no next round.</p><p>Removing MixColumns:</p><ul><li><p>simplifies decryption</p></li><li><p>keeps symmetry clean</p></li><li><p>avoids unnecessary diffusion</p></li></ul><p>AES isn&#8217;t symmetric by accident.</p><p>It&#8217;s symmetric because it was engineered to be implemented correctly.</p><div><hr></div><h2><strong>AES Is Not &#8220;Encryption&#8221; Yet</strong></h2><p>At this point we have:</p><ul><li><p>a block algorithm</p></li><li><p>deterministic output</p></li><li><p>no randomness</p></li></ul><p>That means:</p><blockquote><p>AES alone does not encrypt messages.</p></blockquote><p>It encrypts <strong>one 16-byte block</strong>.</p><p>This is why modes of operation exist &#8212; something we&#8217;ll come back to later, especially since the implementation includes <strong>CTR mode</strong>.</p><div><hr></div><h2><strong>Step 1: Key Expansion (Before Anything Else)</strong></h2><p>Before AES can encrypt <em>anything</em>, it expands the key.</p><p>This is not optional.</p><p>This is not a helper.</p><p>This is <strong>half the cipher</strong>.</p><h3><strong>Why Key Expansion Matters</strong></h3><p>AES does <strong>not</strong> reuse the same key every round.</p><p>Instead:</p><ul><li><p>the original key is expanded into <strong>round keys</strong></p></li><li><p>each round uses a different key</p></li><li><p>each round key depends on all previous ones</p></li></ul><p>This prevents:</p><ul><li><p>simple patterns</p></li><li><p>slide attacks</p></li><li><p>related-key attacks</p></li></ul><h3><strong>What Key Expansion Actually Does</strong></h3><p>High-level process:</p><ol><li><p>Split the key into 4-byte words</p></li><li><p>For every new word:</p><ul><li><p>rotate</p></li><li><p>apply SubBytes</p></li><li><p>XOR with round constant (Rcon)</p></li><li><p>XOR with the word 4 positions back</p></li></ul></li></ol><p>The implementation mirrors the spec line-by-line &#8212; and that&#8217;s exactly what you want here.</p><p>This is not a place to be clever.</p><p>This is a place to be correct.</p><div><hr></div><h2><strong>AddRoundKey: The Most Honest Operation in AES</strong></h2><p>Now that we have round keys, AES begins with <strong>AddRoundKey</strong>.</p><p>It&#8217;s simply:</p><blockquote><p>XOR the state with the round key</p></blockquote><p>Why XOR?</p><ul><li><p>reversible</p></li><li><p>fast</p></li><li><p>symmetric</p></li><li><p>no information loss</p></li></ul><p>AddRoundKey is how key material enters the cipher.</p><p>Everything else rearranges or mixes data &#8212;</p><p>this is where the key actually <em>does something</em>.</p><div><hr></div><h2><strong>The AES State (Where Most Bugs Are Born)</strong></h2><p>AES operates on <strong>16 bytes</strong>, arranged into a <strong>4&#215;4 matrix</strong>.</p><p>And this is where implementations silently fail.</p><blockquote><p>The state is <strong>column-major</strong>, not row-major.</p></blockquote><pre><code><code>[ b0   b4   b8   b12 ]
[ b1   b5   b9   b13 ]
[ b2   b6   b10  b14 ]
[ b3   b7   b11  b15 ]
</code></code></pre><p>Your block_to_state and state_to_bytes functions make this explicit.</p><p>If this mapping is wrong:</p><ul><li><p>AES still runs</p></li><li><p>output still looks random</p></li><li><p>tests almost pass</p></li></ul><p>This is why AES bugs are dangerous.</p><div><hr></div><h2><strong>SubBytes: The Only Non-Linear Step</strong></h2><p>SubBytes replaces each byte using a fixed lookup table (S-box).</p><p>Important fact:</p><blockquote><p>This is the only non-linear operation in AES.</p></blockquote><p>Everything else is linear algebra and XOR.</p><p>You <em>can</em> derive the S-box mathematically.</p><p>You don&#8217;t need to to implement AES.</p><p>Using the fixed table is correct and intentional.</p><div><hr></div><h2><strong>ShiftRows: Small Function, Big Consequences</strong></h2><p>Each row is rotated left:</p><ul><li><p>row 0 &#8594; 0</p></li><li><p>row 1 &#8594; 1</p></li><li><p>row 2 &#8594; 2</p></li><li><p>row 3 &#8594; 3</p></li></ul><p>This step:</p><ul><li><p>breaks column alignment</p></li><li><p>ensures diffusion across rounds</p></li></ul><p>If your state layout is wrong, this is where everything collapses.</p><div><hr></div><h2><strong>MixColumns: Algebra Without Fear</strong></h2><p>MixColumns treats <strong>each column independently</strong>.</p><p>It mixes bytes using fixed coefficients in GF(2&#8312;).</p><p>In practice:</p><ul><li><p>multiplication by 2 &#8594; xtime</p></li><li><p>multiplication by 3 &#8594; xtime(x) XOR x</p></li><li><p>addition &#8594; XOR</p></li><li><p>reduction &#8594; fixed polynomial</p></li></ul><p>The implementation strips this down to what actually matters.</p><p>No matrices.</p><p>No abstractions.</p><p>Just mechanics.</p><div><hr></div><h2><strong>AES Rounds (Putting It Together)</strong></h2><p>AES-128:</p><ul><li><p>initial AddRoundKey</p></li><li><p>9 full rounds:</p><ul><li><p>SubBytes</p></li><li><p>ShiftRows</p></li><li><p>MixColumns</p></li><li><p>AddRoundKey</p></li></ul></li><li><p>final round (no MixColumns)</p></li></ul><p>At this point:</p><ul><li><p>nothing is hidden</p></li><li><p>nothing is magical</p></li><li><p>everything is mechanical</p></li></ul><p>That&#8217;s exactly what you want in cryptography.</p><div><hr></div><h2><strong>CTR Mode: Turning AES into Real Encryption</strong></h2><p>Important clarification:</p><blockquote><p>AES itself is not encryption.</p><p>It&#8217;s a block cipher.</p></blockquote><p>To encrypt real data, we need a <strong>mode of operation</strong>.</p><p>Your implementation includes <strong>CTR (Counter) mode</strong> &#8212; deliberately.</p><h3><strong>How CTR Mode Works</strong></h3><p>CTR turns AES into a <strong>stream cipher</strong>:</p><ol><li><p>Combine nonce + counter</p></li><li><p>Encrypt with AES</p></li><li><p>XOR with plaintext</p></li><li><p>Increment counter</p></li><li><p>Repeat</p></li></ol><p>Key properties:</p><ul><li><p>no padding</p></li><li><p>encryption == decryption</p></li><li><p>parallelizable</p></li><li><p>fast</p></li></ul><p>Critical rule:</p><blockquote><p>Never reuse the same key + nonce pair.</p></blockquote><p>CTR is secure only if nonces are unique.</p><div><hr></div><h2><strong>What AES Actually Teaches</strong></h2><p>DES taught structure.</p><p>AES teaches discipline.</p><p>Most AES failures are not cryptographic.</p><p>They are:</p><ul><li><p>layout mistakes</p></li><li><p>key handling errors</p></li><li><p>mode misuse</p></li><li><p>overconfidence</p></li></ul><p>Implementing AES once forces you to:</p><ul><li><p>respect specifications</p></li><li><p>respect data layout</p></li><li><p>respect boring correctness</p></li></ul><h2>Summary</h2><p>If you&#8217;ve made it this far &#8212;</p><p>you&#8217;re no longer &#8220;using AES&#8221;.</p><p>You&#8217;re <strong>implementing it</strong>.</p><p>And that changes how you read every crypto API forever.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Rebuilding Cryptography From Scratch - My Complete Learning Journey (All Parts Inside)]]></title><description><![CDATA[This collection brings together all my deep-dive explorations into cryptography]]></description><link>https://www.dmytrohuz.com/p/rebuilding-cryptography-from-scratch</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/rebuilding-cryptography-from-scratch</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Mon, 01 Dec 2025 19:09:39 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!SRa_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8a9697d-5837-4c57-947c-4cf941c3bc3d_1024x608.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!SRa_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8a9697d-5837-4c57-947c-4cf941c3bc3d_1024x608.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!SRa_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8a9697d-5837-4c57-947c-4cf941c3bc3d_1024x608.png 424w, https://substackcdn.com/image/fetch/$s_!SRa_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8a9697d-5837-4c57-947c-4cf941c3bc3d_1024x608.png 848w, https://substackcdn.com/image/fetch/$s_!SRa_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8a9697d-5837-4c57-947c-4cf941c3bc3d_1024x608.png 1272w, https://substackcdn.com/image/fetch/$s_!SRa_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8a9697d-5837-4c57-947c-4cf941c3bc3d_1024x608.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!SRa_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8a9697d-5837-4c57-947c-4cf941c3bc3d_1024x608.png" width="1024" height="608" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a8a9697d-5837-4c57-947c-4cf941c3bc3d_1024x608.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:608,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!SRa_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8a9697d-5837-4c57-947c-4cf941c3bc3d_1024x608.png 424w, https://substackcdn.com/image/fetch/$s_!SRa_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8a9697d-5837-4c57-947c-4cf941c3bc3d_1024x608.png 848w, https://substackcdn.com/image/fetch/$s_!SRa_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8a9697d-5837-4c57-947c-4cf941c3bc3d_1024x608.png 1272w, https://substackcdn.com/image/fetch/$s_!SRa_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa8a9697d-5837-4c57-947c-4cf941c3bc3d_1024x608.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2><strong>Why this project exists - and why it might matter to you</strong></h2><p>I picked up a guitar at fifteen because I wanted to play Metallica and Lamb of God. I hunted down tabs, copied riffs, and tried to brute-force my way into sounding like my heroes. It didn&#8217;t work. Months passed, and I still couldn&#8217;t play anything properly.</p><p>A friend told me to stop chasing songs and start practicing scales and exercises. I hated that idea. &#8220;I want to play real music, not this boring stuff.&#8221; But eventually, frustrated and humbled, I gave in. It felt like those Kung Fu movies where the student does strange, meaningless chores&#8212;until suddenly he realises he&#8217;s been training all along.</p><p>That was me. Those &#8220;useless&#8221; exercises were the foundations. Scales weren&#8217;t separate from music; they <em>were</em> the music. They were hidden inside every riff, every chord, every moment that sounded good. Once I accepted that, everything clicked. I gained the skill, the understanding, and finally the ability not just to imitate songs&#8212;but to create my own.</p><p>Around the same age, I fell in love with programming. For years I jumped between whatever was new: frameworks, clouds, AI, agents&#8212;always chasing the next shiny thing. Only recently did I realise something important: everything modern sits on top of a small set of timeless principles. The hype comes and goes. The fundamentals stay.</p><p>And again, just like the guitar, I recognised the pattern. The real power isn&#8217;t in the newest trend&#8212;it&#8217;s in the old &#8220;boring&#8221; building blocks that shape everything else. Master them deeply, and the whole matrix becomes understandable and controllable.</p><p>That&#8217;s why I started rebuilding my foundations from scratch. Not just reading concepts, but internalising them&#8212;running them through my mind and hands until they became part of me. The results are dramatic: my understanding of modern systems grew sharper, deeper, more connected.</p><p>Cryptography is one of those foundations. It looks distant and academic, yet every part of our digital world depends on it. Security is more important than ever, yet our actual <em>understanding</em> of it slips away behind layers of abstractions and frameworks.</p><p>So I&#8217;m going back to the source&#8212;relearning and rebuilding cryptography from the ground up.</p><p>And I invite you to join me on this journey into the fundamentals that truly matter.</p><div><hr></div><h1><strong>&#128216; Full Summary of the Cryptography Series</strong></h1><p>Below is the complete, growing list of all parts in this project.</p><p>This article will stay updated as new chapters are released.</p><div><hr></div><h2><strong>&#128313; 1. Theoretical Foundations of Cryptography</strong></h2><p><strong><a href="https://dev.to/dmytro_huz/introduction-to-cryptography-basic-blocks-3an7">Part 1 &#8212; Basic Building Blocks</a></strong></p><p><em>What cryptography is actually made of &#8212; entropy, keys, messages, adversaries.</em></p><p><strong><a href="https://dev.to/dmytro_huz/introduction-to-cryptography-perfect-secrecy-37aj">Part 2 &#8212; Perfect Secrecy</a></strong></p><p><em>The ideal world: when encryption is mathematically unbreakable.</em></p><p><strong><a href="https://dev.to/dmytro_huz/introduction-to-cryptography-a-beginners-guide-to-computational-security-2n91">Part 3 &#8212; Computational Security</a></strong></p><p><em>The real world: security when perfect secrecy is impossible.</em></p><div><hr></div><h2><strong>&#128313; 2. Rebuilding a Stream Cipher</strong></h2><p><strong><a href="https://dev.to/dmytro_huz/introduction-to-cryptography-the-essential-guide-to-stream-ciphers-for-beginners-36l2">Part 1 &#8212; How Stream Ciphers Work</a></strong></p><p><em>Intuition, structure, and why they&#8217;re fast.</em></p><p><strong><a href="https://dev.to/dmytro_huz/rc4-from-ubiquity-to-collapse-and-what-it-taught-us-about-trust-45ld">Part 2 &#8212; RC4: From Ubiquity to Collapse</a></strong></p><p><em>The rise and fall of one of the most widely used stream ciphers ever.</em></p><div><hr></div><h2><strong>&#128313; 3. Rebuilding a Block Cipher</strong></h2><p><strong>Part 1 - Lego Bricks of Encryption</strong></p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;a508effe-e574-403a-bd7d-d12144af4074&quot;,&quot;caption&quot;:&quot;Introduction&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Building Own Block Cipher: Part 1- Lego Bricks of Modern Security&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder &#8212; Learning, building, and documenting the systems that connect fundamentals with frontiers.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e660032f-1e0c-4a8d-ad53-66adfa358f18_320x320.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2025-09-30T13:01:53.680Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!z_sf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dfe2571-9ada-4164-be4c-01aa106510b8_1536x1024.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://dmytrohuz.substack.com/p/building-cryptography-lego-bricks&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:174698235,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:0,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!x7qR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe14b37a2-0818-4b6d-9a18-e3f9717989d6_144x144.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p><em>The fundamental components from which modern block ciphers are constructed.</em></p><p><strong>Part 2 - Building Your Own Block Cipher</strong></p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;52c8eb61-fb85-454c-a618-52afd95785b0&quot;,&quot;caption&quot;:&quot;&#128312; 1. Introduction&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Building Own Block Cipher: Part 2 &#8212; Block Cipher Theory &amp; Rebuilding DES &quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder &#8212; Learning, building, and documenting the systems that connect fundamentals with frontiers.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e660032f-1e0c-4a8d-ad53-66adfa358f18_320x320.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2025-11-29T20:39:54.507Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!MJ5F!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb973ab9-aa92-4826-8f3e-b3a4c0796cbc_1024x608.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://dmytrohuz.substack.com/p/building-your-own-block-cipher-part&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:180272519,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:1,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!x7qR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe14b37a2-0818-4b6d-9a18-e3f9717989d6_144x144.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p><em>A hands-on journey into assembling a functional toy-cipher from first principles.</em></p><p><strong>Part 3 - Building Own Block Cipher: AES</strong></p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;b0e38bee-19c9-42f8-b7c3-47ea2b12647a&quot;,&quot;caption&quot;:&quot;In the previous articleDmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Building Own Block Cipher: Part 3 - AES&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder &#8212; Learning, building, and documenting the systems that connect fundamentals with frontiers.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e660032f-1e0c-4a8d-ad53-66adfa358f18_320x320.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2025-12-25T10:49:50.073Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!yxEG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe5ab6e26-dbe9-473d-b4d5-4c38838a8c48_1024x608.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://dmytrohuz.substack.com/p/building-own-block-cipher-part-3&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:182561163,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:0,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!x7qR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe14b37a2-0818-4b6d-9a18-e3f9717989d6_144x144.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p>Creating an AES encryption algorithm from scratch based on the technical specification.</p><div><hr></div><h2><strong>&#128313; 4. Rebuilding a MAC (Message Authentication Code)</strong></h2><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;dbc40ac1-f866-4a6d-a081-b60fc83fd6d3&quot;,&quot;caption&quot;:&quot;Huston, we have a problem&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Building Own MAC: Part 1 - Encrypted, but Not Trusted&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder &#8212; Learning, building, and documenting the systems that connect fundamentals with frontiers.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e660032f-1e0c-4a8d-ad53-66adfa358f18_320x320.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2026-01-10T21:59:26.824Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!bwTN!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F02b23a1a-ae7b-4e25-ada3-9b75c1634b23_900x672.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/building-own-mac-part-1-encrypted&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:184157394,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:0,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!t_-c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;caa7e7a9-4fe6-4990-a062-c3fdfd1f1470&quot;,&quot;caption&quot;:&quot;In the previous series we finished AES and its modes. And in the previous article revealed why it is still not secure.&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Building Own MAC &#8212; Part 2: Fixing AES (and accidentally reinventing CMAC)&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder &#8212; Learning, building, and documenting the systems that connect fundamentals with frontiers.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e660032f-1e0c-4a8d-ad53-66adfa358f18_320x320.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2026-01-19T20:32:47.057Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!2YJ-!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdf5f63b1-3d56-4578-acde-87945b4cd3f5_1536x672.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/building-own-mac-part-2-fixing-aes&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:185104992,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:0,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!t_-c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;da595292-3dbb-40e5-ae03-6c29ee26ec3f&quot;,&quot;caption&quot;:&quot;In the previous article, we did something slightly ridiculous.&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Building Own MAC &#8212; Part 3: Reinventing HMAC from SHA-256&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder &#8212; Learning, building, and documenting the systems that connect fundamentals with frontiers.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e660032f-1e0c-4a8d-ad53-66adfa358f18_320x320.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2026-01-23T18:58:16.766Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!QAi6!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F41bb1c89-ee6c-4240-9150-0469a12ab722_1536x672.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/building-own-mac-part-3-reinventing&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:185566303,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:0,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!t_-c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div><hr></div><h2><strong>&#128313; 5.Public-Key Encryption</strong></h2><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;4d11e193-3f60-4df3-976a-ada056b1455b&quot;,&quot;caption&quot;:&quot;I started my deep dive into cryptography six months ago. I wanted to deconstruct its internals into basic building blocks and then build them back up again. One simple idea kept pulling me forward&#8212;fascinating me and motivating me to go deeper: how can a crowd of absolute strangers&#8212;over the internet, an inherently insecure medium&#8212;exchange information sec&#8230;&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;The Aha-Moment of Public-Key Encryption&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder - I deconstruct complex systems to first principles and rebuild them into clear engineering mental models, diagrams, and practical tools.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a4a14e6b-5f68-4257-9ee7-e33b8864d56a_1024x1024.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2026-02-13T12:29:49.434Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!uy8G!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa57299a6-5758-4c3a-bcd3-443638e6a53c_1536x672.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/the-aha-moment-of-public-key-encryption&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:187849238,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:0,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!t_-c!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F046f0d8c-fecd-41e6-a43f-4718cf07a50f_608x608.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p>If you want to follow the journey, this page will always be the central hub.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><div><hr></div>]]></content:encoded></item><item><title><![CDATA[Building Own Block Cipher: Part 2 — Block Cipher Theory & Rebuilding DES ]]></title><description><![CDATA[How real encryption engines work &#8212; and how you can build one from scratch.]]></description><link>https://www.dmytrohuz.com/p/building-your-own-block-cipher-part</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/building-your-own-block-cipher-part</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Sat, 29 Nov 2025 20:39:54 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!MJ5F!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb973ab9-aa92-4826-8f3e-b3a4c0796cbc_1024x608.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MJ5F!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb973ab9-aa92-4826-8f3e-b3a4c0796cbc_1024x608.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MJ5F!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb973ab9-aa92-4826-8f3e-b3a4c0796cbc_1024x608.png 424w, https://substackcdn.com/image/fetch/$s_!MJ5F!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb973ab9-aa92-4826-8f3e-b3a4c0796cbc_1024x608.png 848w, https://substackcdn.com/image/fetch/$s_!MJ5F!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb973ab9-aa92-4826-8f3e-b3a4c0796cbc_1024x608.png 1272w, https://substackcdn.com/image/fetch/$s_!MJ5F!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb973ab9-aa92-4826-8f3e-b3a4c0796cbc_1024x608.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MJ5F!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb973ab9-aa92-4826-8f3e-b3a4c0796cbc_1024x608.png" width="1024" height="608" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bb973ab9-aa92-4826-8f3e-b3a4c0796cbc_1024x608.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:608,&quot;width&quot;:1024,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!MJ5F!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb973ab9-aa92-4826-8f3e-b3a4c0796cbc_1024x608.png 424w, https://substackcdn.com/image/fetch/$s_!MJ5F!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb973ab9-aa92-4826-8f3e-b3a4c0796cbc_1024x608.png 848w, https://substackcdn.com/image/fetch/$s_!MJ5F!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb973ab9-aa92-4826-8f3e-b3a4c0796cbc_1024x608.png 1272w, https://substackcdn.com/image/fetch/$s_!MJ5F!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbb973ab9-aa92-4826-8f3e-b3a4c0796cbc_1024x608.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption"></figcaption></figure></div><h2><strong>&#128312; 1. Introduction</strong></h2><p>In the previous article, we built the <a href="https://dmytrohuz.substack.com/p/building-cryptography-lego-bricks">&#8220;Lego bricks&#8221; of cryptography </a>&#8212; pseudo-random generators, one-way functions, and permutations. Those components are not just academic toys; they form the internal mechanics of real encryption systems.</p><p>Now it&#8217;s time to assemble those bricks into something that actually encrypts data.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><p>Welcome to <strong>block ciphers</strong>.</p><p>A block cipher is a deterministic algorithm that:</p><ul><li><p>takes a <strong>fixed-size block</strong> of input (e.g. 64 or 128 bits),</p></li><li><p>mixes it with a <strong>secret key</strong>,</p></li><li><p>and outputs another block of the same size that looks completely random.</p></li></ul><p>With the same key, the process is perfectly reversible.</p><p>Without the key, the transformation should be indistinguishable from pure randomness.</p><p>Block ciphers are used everywhere:</p><p>HTTPS, VPNs, banking systems, disk encryption, password managers, secure messaging&#8230;</p><p>They are the <em>engines</em> behind modern symmetric cryptography.</p><p>But why do we need block ciphers in the first place?</p><p>Let&#8217;s quickly build the foundation.</p><div><hr></div><h2><strong>&#128312; 2. What Are Block Ciphers, and Why Do We Need Them?</strong></h2><p>We encrypt data for two reasons:</p><ol><li><p><strong>Confidentiality</strong> &#8212; making information unreadable to anyone without the key</p></li><li><p><strong>Integrity &amp; authenticity</strong> &#8212; ensuring the data was not changed or forged</p></li></ol><p>To do this efficiently, encryption algorithms operate on <strong>fixed-size blocks</strong>.</p><p>Why blocks?</p><ul><li><p>They let us design compact and analysable algorithms</p></li><li><p>They allow predictable performance on hardware and software</p></li><li><p>They reduce the complexity of invertible transformations</p></li><li><p>They provide consistency across inputs and outputs</p></li></ul><p>But real data is <em>not</em> always aligned to 64 or 128 bits.</p><p>Emails, files, passwords, videos &#8212; all come in arbitrary lengths.</p><p>You might think: <em>why not just use a pure stream cipher and skip blocks entirely?</em></p><p>Stream ciphers (like Salsa20 or ChaCha20) are excellent &#8212; fast and lightweight &#8212; but block ciphers give us two critical things that streams alone cannot guarantee:</p><h3><strong>&#10004; Structure for authentication modes</strong></h3><p>Block ciphers enable modern authenticated encryption schemes (AES-GCM, AES-CCM, OCB).</p><p>These protect confidentiality <em>and</em> detect tampering.</p><h3><strong>&#10004; Predictable, algebraically analysable transformations</strong></h3><p>Block ciphers provide controlled diffusion, confusion, and round structure &#8212;</p><p>making them suitable for:</p><ul><li><p>hardware acceleration</p></li><li><p>cryptanalysis and security proofs</p></li><li><p>secure key wrapping</p></li><li><p>deterministic encryption building blocks</p></li></ul><p>Block ciphers are the <strong>workhorses</strong>.</p><p>Stream ciphers are the <strong>special tools</strong>.</p><p>We need both.</p><div><hr></div><h2><strong>&#128312; 3. The First Big Cipher: DES</strong></h2><p><strong>Data Encryption Standard (DES)</strong> was introduced in the 1970s as the first widely deployed modern block cipher.</p><p>It works like this:</p><ul><li><p><strong>Block size:</strong> 64 bits</p></li><li><p><strong>Key:</strong> 64 bits (but only 56 effective &#8212; 8 parity bits)</p></li><li><p><strong>Structure:</strong> 16-round Feistel network</p></li><li><p><strong>Core components:</strong> permutations, S-boxes, expansion, key schedule</p></li><li><p><strong>Output:</strong> 64-bit ciphertext block</p></li></ul><p>For almost 20 years, DES secured ATMs, bank traffic, government systems, corporate networks &#8212; basically everything.</p><p>But DES has two major problems today:</p><h3><strong>&#10060; 1. The key is too short</strong></h3><p>A 56-bit key can be brute-forced by modern hardware.</p><h3><strong>&#10060; 2. The block size is too small</strong></h3><p>64-bit blocks make certain high-volume protocols vulnerable to collisions and pattern leaks.</p><p>Because of this, DES is no longer considered secure.</p><p>However, DES has one <em>amazing</em> property:</p><blockquote><p>It is the clearest modern example of how a block cipher is constructed from basic components.</p></blockquote><p>That&#8217;s exactly why I rebuilt DES from scratch using only the original technical specification.</p><p>If you want to see that process &#8212; bit by bit &#8212; here are the resources:</p><p>&#128279; <strong>Jupyter Notebook (step-by-step reconstruction):</strong></p><p><a href="https://colab.research.google.com/github/DmytroHuzz/build_own_block_cipher/blob/main/des/des_story.ipynb">https://colab.research.google.com/github/DmytroHuzz/build_own_block_cipher/blob/main/des/des_story.ipynb</a></p><p>&#128279; <strong>GitHub repository (code + tests + CI/CD):</strong></p><p><a href="https://github.com/DmytroHuzz/build_own_block_cipher/tree/main/des">https://github.com/DmytroHuzz/build_own_block_cipher/tree/main/des</a></p><p>This notebook is essentially the &#8220;story mode&#8221; of DES:</p><p>you will see how every permutation, S-box, and round key is derived.</p><div><hr></div><h2><strong>&#128312; 4. The Successor: AES &#8212; Modern, Fast, and Secure</strong></h2><p>After DES was broken by brute force, the cryptographic community spent years designing a successor.</p><p>The result was the <strong>Advanced Encryption Standard (AES)</strong>.</p><p>AES has:</p><ul><li><p><strong>128-bit block size</strong></p></li><li><p><strong>Key sizes:</strong> 128, 192, or 256 bits</p></li><li><p><strong>Structure:</strong> substitution&#8211;permutation network (not Feistel)</p></li><li><p><strong>Rounds:</strong> 10 / 12 / 14 depending on key size</p></li><li><p><strong>Massive software and hardware optimisation</strong></p></li></ul><p>Why is AES used everywhere?</p><h4><strong>&#10004; Large block size</strong></h4><p>Prevents block collisions in realistic workloads.</p><h4><strong>&#10004; Strong security margin</strong></h4><p>No practical attacks exist; the design has held up under 20+ years of analysis.</p><h4><strong>&#10004; Hardware acceleration</strong></h4><p>AES instructions are available on nearly all CPUs (Intel, AMD, ARM).</p><h4><strong>&#10004; Authenticated modes</strong></h4><p>AES-GCM and AES-CCM power TLS, SSH, VPNs, and modern APIs.</p><p>If you are using HTTPS today, you are almost certainly using AES-GCM.</p><p>AES is the industry standard for symmetric encryption &#8212; strong, fast, secure.</p><div><hr></div><h3><strong>&#128221; Want to see AES built from scratch too?</strong></h3><p>If you&#8217;d like a deep, step-by-step AES reconstruction (similar to the DES notebook),</p><p>let me know in the comments or send me an email.</p><p>If enough people are interested, I will rebuild it ;)</p><div><hr></div><h2><strong>&#128312; 5. Modes of Operation &#8212; Making Block Ciphers Handle Real Data</strong></h2><p>There&#8217;s one important limitation of block ciphers:</p><blockquote><p>They only encrypt <strong>exactly one block</strong></p></blockquote><p>But real data is messy:</p><ul><li><p>files are large</p></li><li><p>messages are unpredictable in length</p></li><li><p>patterns may repeat</p></li><li><p>attackers may modify or reorder blocks</p></li></ul><p>If we simply encrypt each 64- or 128-bit block independently, we leak structural information.</p><p>(This mistake has a name: <strong>ECB mode</strong>, the &#8220;don&#8217;t do this&#8221; mode.)</p><p>So cryptographers designed <strong>modes of operation</strong> &#8212; strategies to apply a block cipher safely to multi-block data.</p><p>A mode defines how each block is processed and how blocks relate to each other.</p><p>Here are the most common ones:</p><p>ECB  &#8594;  simple but insecure</p><p>       encrypt blocks independently</p><p>       &#10004; only safe on truly random data</p><p>       &#10060; leaks structure</p><p>CBC  &#8594;  chain blocks via XOR</p><p>       &#10004; great for large files</p><p>       &#10060; needs padding + sequential</p><p>CTR  &#8594;  encrypt counter &#8594; XOR</p><p>       &#10004; streaming, parallel, fast</p><p>       &#10060; careful nonce handling required</p><p>GCM  &#8594;  CTR + integrity tag</p><p>       &#10004; modern web &amp; APIs (TLS)</p><p>       &#10060; more complex to implement safely<br></p><h3><strong>Why modes matter</strong></h3><p>Modes give us:</p><ul><li><p><strong>Confidentiality</strong> &#8212; data looks random</p></li><li><p><strong>Randomness</strong> &#8212; even repeated plaintext &#8594; different ciphertext</p></li><li><p><strong>Flexibility</strong> &#8212; any-length data, streaming, random access</p></li><li><p><strong>Modern guarantees</strong> &#8212; integrity and authenticity with AEAD modes like GCM</p></li></ul><p>Without modes, even the strongest block cipher fails to secure real-world messages.</p><div><hr></div><h3><strong>&#129514; DES With CTR Mode &#8212; Rebuilt From Scratch</strong></h3><p>In my implementation, I rebuilt DES step-by-step <strong>and</strong> wrapped it in <strong>CTR mode</strong>:</p><ul><li><p>DES produces a pseudorandom block</p></li><li><p>Counter increments per block</p></li><li><p>XOR with plaintext gives ciphertext</p></li><li><p>Same process decrypts (symmetry of stream XOR)</p></li></ul><p>CTR turns any block cipher into a <strong>stream cipher</strong> with nice properties:</p><ul><li><p><strong>No padding needed</strong></p></li><li><p><strong>Parallelizable</strong></p></li><li><p><strong>Random-access decryption</strong> (decrypt block N without decrypting all before it)</p></li></ul><p>You can try running DES-CTR live:</p><p>&#128279; <strong>Colab Notebook</strong> &#8212; editable, runnable: <a href="https://colab.research.google.com/github/DmytroHuzz/build_own_block_cipher/blob/main/des/des_story.ipynb">https://colab.research.google.com/github/DmytroHuzz/build_own_block_cipher/blob/main/des/des_story.ipynb</a></p><p>&#128279; <strong>GitHub Repo</strong> &#8212; code + CI/CD + tests</p><p><a href="https://colab.research.google.com/github/DmytroHuzz/build_own_block_cipher/blob/main/des/des_story.ipynb">https://colab.research.google.com/github/DmytroHuzz/build_own_block_cipher/blob/main/des/des_story.ipynb</a></p><p>CTR isn&#8217;t the only mode worth exploring &#8212; it&#8217;s just the cleanest and easiest to start with.</p><div><hr></div><h3><strong>&#128221; Want other modes reconstructed too?</strong></h3><p>If you&#8217;d like me to:</p><ul><li><p>implement <strong>CBC</strong>, <strong>CFB</strong>, <strong>OFB</strong>, <strong>GCM</strong></p></li><li><p>explain how they prevent pattern leaks and tampering</p></li><li><p>demonstrate attacks when modes are misused</p></li></ul><p>Please let me know in the comments, or email me directly.</p><p>I&#8217;m happy to continue the series in the direction that helps you most.</p><div><hr></div><h2><strong>&#128312; 6. Summary &#8212; Where We Go From Here</strong></h2><p>Today we connected the ideas from the previous article (&#8220;the cryptography Lego bricks&#8221;) to <strong>real-world encryption</strong>:</p><p>&#10004; What block ciphers are, and why we need them</p><p>&#10004; Why stream ciphers alone are not enough</p><p>&#10004; How DES introduced the first practical, structured encryption engine</p><p>&#10004; Why DES is insecure today &#8212; but still worth rebuilding to understand the foundations</p><p>&#10004; Why AES became the modern global standard</p><p>&#10004; How <strong>modes of operation</strong> (especially CTR, CBC, GCM) make block ciphers safe for real data</p><p>&#10004; How <strong>I rebuilt DES from the original spec</strong> &#8212; including a working CTR mode implementation</p><p>All the resources are here:</p><p>&#128279; <strong>GitHub repository (code + tests + CI/CD):</strong></p><p><a href="https://github.com/DmytroHuzz/build_own_block_cipher/tree/main">https://github.com/DmytroHuzz/build_own_block_cipher/tree/main</a></p><p>&#128279; <strong>Step-by-step DES reconstruction notebook (Colab):</strong></p><p><a href="https://colab.research.google.com/github/DmytroHuzz/build_own_block_cipher/blob/main/des/des_story.ipynb">https://colab.research.google.com/github/DmytroHuzz/build_own_block_cipher/blob/main/des/des_story.ipynb</a></p><p>&#128279; Previous Article</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;f16ff62c-1eaf-4899-8577-ebee485ddcc9&quot;,&quot;caption&quot;:&quot;Introduction&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Building Own Block Cipher: Part 0 Lego Bricks of Modern Security&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Engineer | Writer | Builder &#8212; Learning, building, and documenting the systems that connect fundamentals with frontiers.&quot;,&quot;photo_url&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e660032f-1e0c-4a8d-ad53-66adfa358f18_320x320.png&quot;,&quot;is_guest&quot;:false,&quot;bestseller_tier&quot;:null}],&quot;post_date&quot;:&quot;2025-09-30T13:01:53.680Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!z_sf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dfe2571-9ada-4164-be4c-01aa106510b8_1536x1024.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://dmytrohuz.substack.com/p/building-cryptography-lego-bricks&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:174698235,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:0,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Dmytro&#8217;s Substack&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!x7qR!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe14b37a2-0818-4b6d-9a18-e3f9717989d6_144x144.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div><hr></div><h3><strong>What&#8217;s next?</strong></h3><p>There are two natural directions from here &#8212; and <strong>you</strong> get to decide where we go:</p><p>&#127344;&#65039; <strong>Continue the Block Cipher Journey</strong></p><ul><li><p>More modes (CBC / CFB / OFB / GCM)</p></li><li><p>Example attacks when modes are misused</p></li><li><p>AES reconstruction &#8220;from the standard&#8221;</p></li><li><p>Performance, key schedules, design philosophy</p></li></ul><p>&#127345;&#65039; <strong>Move to Next Cryptography Foundations</strong></p><ul><li><p>Message integrity</p></li><li><p>MACs &amp; authenticated encryption</p></li><li><p>Why confidentiality <em>without</em> integrity can be dangerous</p></li><li><p>How encryption interacts with signatures and hashing</p></li></ul><p>If you&#8217;re interested in <strong>AES step-by-step</strong> like we did with DES &#8212; or want me to dive deeper into modes &#8212; just leave a comment below or email me directly.</p><p>Your preference will shape the next episode in this series.</p><div><hr></div><p>Block ciphers are the engines of secure communication.</p><p>Now you know how they are designed, how they evolved, and how we actually use them today.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item><item><title><![CDATA[Building Own Block Cipher: Part 1- Lego Bricks of Modern Security]]></title><description><![CDATA[A hands-on journey into pseudo-random generators, functions, and permutations: the Lego bricks that make block ciphers and secure protocols possible.]]></description><link>https://www.dmytrohuz.com/p/building-cryptography-lego-bricks</link><guid isPermaLink="false">https://www.dmytrohuz.com/p/building-cryptography-lego-bricks</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Tue, 30 Sep 2025 13:01:53 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!z_sf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dfe2571-9ada-4164-be4c-01aa106510b8_1536x1024.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!z_sf!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dfe2571-9ada-4164-be4c-01aa106510b8_1536x1024.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!z_sf!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dfe2571-9ada-4164-be4c-01aa106510b8_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!z_sf!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dfe2571-9ada-4164-be4c-01aa106510b8_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!z_sf!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dfe2571-9ada-4164-be4c-01aa106510b8_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!z_sf!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dfe2571-9ada-4164-be4c-01aa106510b8_1536x1024.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!z_sf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dfe2571-9ada-4164-be4c-01aa106510b8_1536x1024.png" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/4dfe2571-9ada-4164-be4c-01aa106510b8_1536x1024.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:971,&quot;width&quot;:1456,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:3192384,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/png&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:&quot;https://dmytrohuz.substack.com/i/174698235?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dfe2571-9ada-4164-be4c-01aa106510b8_1536x1024.png&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!z_sf!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dfe2571-9ada-4164-be4c-01aa106510b8_1536x1024.png 424w, https://substackcdn.com/image/fetch/$s_!z_sf!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dfe2571-9ada-4164-be4c-01aa106510b8_1536x1024.png 848w, https://substackcdn.com/image/fetch/$s_!z_sf!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dfe2571-9ada-4164-be4c-01aa106510b8_1536x1024.png 1272w, https://substackcdn.com/image/fetch/$s_!z_sf!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F4dfe2571-9ada-4164-be4c-01aa106510b8_1536x1024.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><h2><strong>Introduction</strong></h2><p>In the previous section, we looked at <strong><a href="https://dev.to/dmytro_huz/introduction-to-cryptography-the-essential-guide-to-stream-ciphers-for-beginners-36l2">stream ciphers</a></strong>: fast, infinite, and unpredictable. But are they enough to secure everything in our digital world? Not really. Stream ciphers are powerful, but they are not a silver bullet. They fail in several important scenarios:</p><ol><li><p><strong>Non-invertible encryption</strong> &#8211; Sometimes we need encryption that cannot be reversed (e.g., storing passwords). Stream ciphers always decrypt what they encrypt.</p></li><li><p><strong>Integrity and authentication</strong> &#8211; Stream ciphers hide content but cannot prevent tampering or impersonation. We need tools to check whether a message was changed and to confirm its sender.</p></li><li><p><strong>Reuse of keys across multiple messages</strong> &#8211; With stream ciphers, the output depends only on the secret key. Using the same key for multiple messages risks reusing the keystream. We need constructions where input and key both matter.</p></li></ol><p>Clearly, building a custom cipher for every special case would be impossible: each would need years of testing, breaking, and patching. Instead, cryptographers chose a different path:</p><p>&#128073; <strong>Build a small set of secure primitives</strong> &#8212; each with well-defined properties and proofs of security &#8212; and then combine them as Lego blocks to solve different real-world problems.</p><p>In this article we&#8217;ll explore three fundamental primitives:</p><ul><li><p><strong>PRG (Pseudo-Random Generator)</strong> &#8211; stretches a short random seed into a long, random-looking sequence.</p></li><li><p><strong>PRF (Pseudo-Random Function)</strong> &#8211; produces a fixed-size random-looking output from a key and an input.</p></li><li><p><strong>PRP (Pseudo-Random Permutation)</strong> &#8211; a PRF that is invertible: outputs can be reversed if you know the key.</p></li></ul><p>We&#8217;ll also see how each can be built from the previous one. That&#8217;s the elegant chain:</p><blockquote><p>If PRG is possible and secure &#8594; PRF is possible and secure &#8594; PRP is possible and secure.</p></blockquote><p>By the end, you&#8217;ll have an intuitive map of these primitives, working code samples, and animations that bring the theory to life.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.dmytrohuz.com/subscribe?"><span>Subscribe now</span></a></p><h2><strong>PRG: Pseudo-Random Generator</strong></h2><p>We&#8217;ve already met <a href="https://dev.to/dmytro_huz/introduction-to-cryptography-the-essential-guide-to-stream-ciphers-for-beginners-36l2">PRGs before</a>. They are the <strong>foundation</strong> of modern cryptography:</p><ul><li><p>Input: a short truly random seed.</p></li><li><p>Output: a much longer string that looks random.</p></li></ul><p>For this article, we&#8217;ll just recap by showing one <a href="https://github.com/DmytroHuzz/building_block_ciphers/blob/main/prg.py">practical construction</a>: a PRG that doubles the seed length using HMAC.</p><pre><code><code>import hmac
import hashlib

def prg_double(seed):
    &#8220;&#8221;&#8220;
    HMAC-based Pseudo-Random Generator commonly used in modern ciphers
    
    Args:
        seed (bytes): The seed value
        
    Returns:
        bytes: Random bytes twice the length of the seed
    &#8220;&#8221;&#8220;
    length = len(seed) * 2
    
    output = b&#8217;&#8216;
    counter = 0
    
    # Generate output until we have enough bytes
    while len(output) &lt; length:
        counter_bytes = counter.to_bytes(4, byteorder=&#8217;big&#8217;)
        h = hmac.new(seed, counter_bytes, hashlib.sha256)
        output += h.digest()
        counter += 1    
    return output[:length]

# Example usage
if __name__ == &#8220;__main__&#8221;:
    seed = &#8220;hello world&#8221;
    random_str = prg_double(seed.encode(&#8217;utf-8&#8217;))
    print(f&#8221;Seed: {seed}&#8221;)
    print(f&#8221;Seed length (bytes) (length {len(seed.encode(&#8217;utf-8&#8217;))}): {seed.encode(&#8217;utf-8&#8217;)}&#8221;)
    print(f&#8221;Random string (bytes) (length {len(random_str)}): {random_str}&#8221;)</code></code></pre><p>This is our basic building block &#8212; and the seed for everything else.</p><h2><strong>Pseudo-Random Function (PRF)</strong></h2><p>A <strong>PRF</strong> is the natural next step after a PRG. Instead of just stretching randomness, we want a function that behaves like this:</p><ul><li><p>It takes a <strong>secret key</strong> and an <strong>input</strong>.</p></li><li><p>With the same key and input, it always returns the same output.</p></li><li><p>To anyone without the key, the outputs look completely random.</p></li></ul><p>Formally: for a key k and input x, the function <em>F&#8342;(x)</em> is deterministic but indistinguishable from a truly random function.</p><div><hr></div><h3><strong>Why do we need PRFs?</strong></h3><p>PRFs are everywhere in modern cryptography:</p><ul><li><p><strong>Message authentication codes (MACs):</strong> compute a tag t = <em>F&#8342;(m)</em> so any tampering can be detected.</p></li><li><p><strong>Key derivation:</strong> derive fresh, independent keys k&#8321;, k&#8322; &#8230; from one master key.</p></li><li><p><strong>Counters into keystreams:</strong> avoid keystream reuse by turning counters or nonces into blocks <em>F&#8342;(ctr)</em>.</p></li><li><p><strong>Bigger constructions:</strong> PRPs (via Feistel), KDFs, and many secure protocols use PRFs as their inner engines.</p></li></ul><p>Without PRFs, we couldn&#8217;t authenticate messages, manage keys securely, or even build block ciphers.</p><div><hr></div><h3><strong>From PRG to PRF: the GGM construction</strong></h3><p>The beautiful part of theory is that we don&#8217;t need to assume PRFs &#8220;out of nowhere.&#8221;</p><p>The Goldreich&#8211;Goldwasser&#8211;Micali (GGM) construction shows:</p><p>&#128073; <strong>If secure PRGs exist, then secure PRFs exist.</strong></p><p>The idea:</p><ol><li><p>Start with a seed (the secret key).</p></li><li><p>Use the PRG to expand it into two new seeds.</p></li><li><p>Input bits decide which path you follow: 0 = left child, 1 = right child.</p></li><li><p>After consuming all input bits, the seed you reach becomes the PRF output.</p></li></ol><p>Think of it as growing a binary tree of seeds, and your input walks a path through the tree.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!iKDG!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff044a2f8-842e-443e-acac-c8e4eee61b70_1000x625.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!iKDG!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff044a2f8-842e-443e-acac-c8e4eee61b70_1000x625.gif 424w, https://substackcdn.com/image/fetch/$s_!iKDG!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff044a2f8-842e-443e-acac-c8e4eee61b70_1000x625.gif 848w, https://substackcdn.com/image/fetch/$s_!iKDG!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff044a2f8-842e-443e-acac-c8e4eee61b70_1000x625.gif 1272w, https://substackcdn.com/image/fetch/$s_!iKDG!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff044a2f8-842e-443e-acac-c8e4eee61b70_1000x625.gif 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!iKDG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff044a2f8-842e-443e-acac-c8e4eee61b70_1000x625.gif" width="1000" height="625" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f044a2f8-842e-443e-acac-c8e4eee61b70_1000x625.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:625,&quot;width&quot;:1000,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:694990,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://dmytrohuz.substack.com/i/174698235?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff044a2f8-842e-443e-acac-c8e4eee61b70_1000x625.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!iKDG!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff044a2f8-842e-443e-acac-c8e4eee61b70_1000x625.gif 424w, https://substackcdn.com/image/fetch/$s_!iKDG!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff044a2f8-842e-443e-acac-c8e4eee61b70_1000x625.gif 848w, https://substackcdn.com/image/fetch/$s_!iKDG!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff044a2f8-842e-443e-acac-c8e4eee61b70_1000x625.gif 1272w, https://substackcdn.com/image/fetch/$s_!iKDG!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff044a2f8-842e-443e-acac-c8e4eee61b70_1000x625.gif 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em><a href="https://github.com/DmytroHuzz/building_block_ciphers/blob/main/ggm_animation.html">Feel free to download an animation and play with it on your own.</a></em></p><p>This gives us a PRF whose security reduces directly to the PRG&#8217;s security. That&#8217;s a profound result: PRFs are guaranteed if PRGs exist.</p><h3><strong>Implementation demo</strong></h3><p>Here&#8217;s a fixed-parameter <a href="https://github.com/DmytroHuzz/building_block_ciphers/blob/main/prf.py">Python implementation</a> of the GGM PRF:</p><pre><code><code>#!/usr/bin/env python3
&#8220;&#8221;&#8220;
GGM PRF (fixed-parameter version)
---------------------------------
- Key size:    128 bits (16 bytes) exactly
- Output size: 128 bits (16 bytes) exactly
- Domain:      bitstrings (you can pass str of &#8216;0&#8217;/&#8217;1&#8217;)
&#8220;&#8221;&#8220;

from prg import prg_double

KEY_LEN = 16  # 128-bit key and output
OUT_LEN = 16

def ensure_bits(x) -&gt; str:
    if isinstance(x, str):
        if not set(x) &lt;= {&#8221;0&#8221;,&#8221;1&#8221;}:
            raise ValueError(&#8221;bit string must contain only &#8216;0&#8217;/&#8217;1&#8217;&#8221;)
        return x
    raise TypeError(&#8221;x must be str of bits, bytes, or int&#8221;)

# ---- Fixed-parameter GGM PRF: F_k(x) -&gt; 16 bytes ----
def ggm_prf_128(key: bytes, x) -&gt; bytes:
    if not isinstance(key, (bytes, bytearray)) or len(key) != KEY_LEN:
        raise ValueError(f&#8221;key must be exactly {KEY_LEN} bytes (128 bits)&#8221;)
    seed = bytes(key)
    path = ensure_bits(x)

    for i, bit in enumerate(path):
        lr = prg_double(seed)           # 32 bytes: left||right
        seed = lr[:OUT_LEN] if bit == &#8220;0&#8221; else lr[OUT_LEN:]
        print(f&#8221;  Level {i}: bit={bit}  seed={seed.hex()}&#8221;)

    # Fixed output length: 16 bytes
    return seed  # 128-bit PRF output

def _demo(bits: str) -&gt; None:
    key = bytes.fromhex(&#8221;00112233445566778899aabbccddeeff&#8221;)

    print(f&#8221;key    = {key.hex()}&#8221;)
    print(f&#8221;x      = {bits}  (len={len(bits)} bits)&#8221;)

    y = ggm_prf_128(key, bits)

    print(f&#8221;F_k(x) = {y.hex()}  ({len(y)} bytes)&#8221;)

if __name__ == &#8220;__main__&#8221;:
    print(&#8221;Demo 1:&#8221;)
    _demo(&#8221;0101&#8221;)
    </code></code></pre><h3><strong>GGM is not the only way</strong></h3><p>GGM is powerful as a <strong>proof of existence</strong> and a teaching tool, but it&#8217;s not what systems use in practice. Real-world PRFs are built differently:</p><ul><li><p><strong>HMAC (with SHA-2/3):</strong> deployed everywhere and modeled as a PRF.</p></li><li><p><strong>HKDF:</strong> key derivation built on HMAC.</p></li><li><p><strong>AES as a PRF:</strong> <em>F&#8342;(x)</em>  = AES<em>&#8342;(x)</em> , with variants like CMAC or PMAC for authentication.</p></li></ul><p>These are faster, widely standardized, and hardened by decades of cryptanalysis.</p><div><hr></div><h3><strong>Reflection</strong></h3><p>We can treat GGM as the <strong>conceptual golden standard</strong> of PRFs, much like the one-time pad is for stream ciphers. It isn&#8217;t deployed, but it proves that PRFs are possible and gives us a clean reference point. Practical PRFs like HMAC or AES can then be understood as <em>flesh built on this soul</em>.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe now&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://www.dmytrohuz.com/subscribe?"><span>Subscribe now</span></a></p><h2><strong>Pseudo-Random Permutation (PRP)</strong></h2><p>A <strong>PRP</strong> is like a PRF with one extra property: <strong>invertibility</strong>.</p><ul><li><p>A PRF maps inputs to outputs but doesn&#8217;t guarantee you can reverse the process.</p></li><li><p>A PRP is a PRF that&#8217;s also a <strong>bijection</strong>: every input block maps to a unique output block, and every output maps back to its input.</p></li></ul><p>Formally: for a key k, a PRP P&#8342; is a permutation over fixed-length blocks (e.g. 128 bits). Both P&#8342;(x) and its inverse P&#8342;&#8315;&#185;(y) are efficiently computable if you know the key.</p><div><hr></div><h3><strong>Why do we need PRPs?</strong></h3><p>Invertibility is what makes <strong>encryption and decryption possible</strong>. With a PRF you can authenticate or derive, but you can&#8217;t take ciphertext and recover plaintext. PRPs give us:</p><ul><li><p><strong>Block ciphers</strong> like AES &#8212; encryption that can always be undone with the same key.</p></li><li><p><strong>Secure shuffling</strong> of fixed-size blocks.</p></li><li><p><strong>Building blocks for modes of operation</strong> (CBC, CTR, GCM), which rely on encryption/decryption symmetry.</p></li></ul><p>Without PRPs, we couldn&#8217;t have reversible encryption schemes.</p><div><hr></div><h3><strong>From PRF to PRP: the Feistel network</strong></h3><p>So how do we take a PRF (one-way) and make it invertible? The answer is the <strong>Feistel construction</strong>.</p><ol><li><p>Split the input block into two halves: L&#8320;, R&#8320;.</p></li><li><p>Compute:</p><p>L&#7522;&#8330;&#8321; = R&#7522;</p><p>R&#7522;&#8330;&#8321; = L&#7522; &#8853; F&#8342;(R&#7522;)</p></li><li><p>Repeat for several rounds.</p></li></ol><p>The magic:</p><ul><li><p>Even though the round function F (a PRF) is not invertible by itself, the whole Feistel structure is.</p></li><li><p>You can always recover L&#7522;, R&#7522; from L&#7522;&#8330;&#8321;, R&#7522;&#8330;&#8321; .</p></li><li><p>With enough rounds, the permutation looks indistinguishable from random.</p></li></ul><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!VvXg!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09323ba-3f8a-44ea-8e10-ee3761d05af3_1000x625.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!VvXg!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09323ba-3f8a-44ea-8e10-ee3761d05af3_1000x625.gif 424w, https://substackcdn.com/image/fetch/$s_!VvXg!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09323ba-3f8a-44ea-8e10-ee3761d05af3_1000x625.gif 848w, https://substackcdn.com/image/fetch/$s_!VvXg!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09323ba-3f8a-44ea-8e10-ee3761d05af3_1000x625.gif 1272w, https://substackcdn.com/image/fetch/$s_!VvXg!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09323ba-3f8a-44ea-8e10-ee3761d05af3_1000x625.gif 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!VvXg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09323ba-3f8a-44ea-8e10-ee3761d05af3_1000x625.gif" width="1000" height="625" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c09323ba-3f8a-44ea-8e10-ee3761d05af3_1000x625.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:625,&quot;width&quot;:1000,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:594645,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/gif&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://dmytrohuz.substack.com/i/174698235?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09323ba-3f8a-44ea-8e10-ee3761d05af3_1000x625.gif&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!VvXg!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09323ba-3f8a-44ea-8e10-ee3761d05af3_1000x625.gif 424w, https://substackcdn.com/image/fetch/$s_!VvXg!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09323ba-3f8a-44ea-8e10-ee3761d05af3_1000x625.gif 848w, https://substackcdn.com/image/fetch/$s_!VvXg!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09323ba-3f8a-44ea-8e10-ee3761d05af3_1000x625.gif 1272w, https://substackcdn.com/image/fetch/$s_!VvXg!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc09323ba-3f8a-44ea-8e10-ee3761d05af3_1000x625.gif 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg role="img" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><title></title><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p><em><a href="https://github.com/DmytroHuzz/building_block_ciphers/blob/main/fiestel_animation.html">Feel free to download an animation and play with it on your own.</a></em></p><div><hr></div><h3><strong>Example intuition (no numbers)</strong></h3><p>Think of it as a <strong>dance between halves</strong>:</p><ul><li><p>One half moves forward unchanged.</p></li><li><p>The other half is &#8220;mixed&#8221; with a pseudo-random function.</p></li><li><p>Then they swap roles.</p><p>Round after round, both halves keep carrying and mixing information, until the whole block is scrambled but reversible.</p></li></ul><div><hr></div><h3><strong>Implementation demo</strong></h3><p>Here is the full <a href="https://github.com/DmytroHuzz/building_block_ciphers/blob/main/prp.py">implementation</a>:</p><pre><code><code>#!/usr/bin/env python3
from prf import prf

class PRP:
    def __init__(self, key, rounds=4):
        &#8220;&#8221;&#8220;Initialize PRP with a key and number of rounds.
        
        Args:
            key (bytes): The secret key (must be exactly 16 bytes/128 bits for prf)
            rounds (int): Number of Feistel rounds
        &#8220;&#8221;&#8220;
        # Ensure key is exactly 16 bytes as required by prf.py
        if len(key) != 16:
            raise ValueError(&#8221;Key must be exactly 16 bytes (128 bits)&#8221;)
        self.key = key
        self.rounds = rounds
        
    def _round_function(self, right_half):
        &#8220;&#8221;&#8220;Round function based on PRF from prf.py.
        
        Args:
            right_half (bytes): Right half of the current state            
        Returns:
            bytes: Output of the round function
        &#8220;&#8221;&#8220;
        # Use the entire right_half as input to the PRF
        # Convert each byte to its 8-bit binary representation
        if right_half:
            # Convert all bytes to bits
            bits = &#8216;&#8217;.join(format(byte, &#8216;08b&#8217;) for byte in right_half)
        else:
            # Default if input is empty
            bits = &#8220;0000&#8221;
            
        # Apply the PRF from prf.py using all bits from right_half
        return prf(self.key, bits)
        
    def encrypt(self, plaintext):
        &#8220;&#8221;&#8220;Encrypt a block using the Feistel network.
        
        Args:
            plaintext (bytes): Input block to encrypt
            
        Returns:
            bytes: Encrypted block with padding byte if needed
        &#8220;&#8221;&#8220;
        # Ensure even length by padding if necessary
        if len(plaintext) % 2 != 0:
            # Add padding indicator as the last byte
            plaintext = plaintext + b&#8217;\\x01&#8217;
        else:
            # Even length, add padding block to indicate no padding needed
            plaintext = plaintext + b&#8217;\\x00&#8217;
            
        # Split the plaintext into two equal halves
        half_size = len(plaintext) // 2
        left = plaintext[:half_size]
        right = plaintext[half_size:]
        
        # Feistel rounds
        for _ in range(self.rounds):
            # Apply round function to right half
            f_output = self._round_function(right)
            # XOR the output with left half
            new_right = bytes(a ^ b for a, b in zip(left, f_output[:half_size]))
            # Swap halves for next round
            left = right
            right = new_right
            
        # For an even number of rounds, we need to swap back for decryption to work properly
        if self.rounds % 2 == 0:
            return right + left
        else:
            return left + right
            
    def decrypt(self, ciphertext):
        &#8220;&#8221;&#8220;Decrypt a block using the Feistel network.
        
        Args:
            ciphertext (bytes): Input block to decrypt
            
        Returns:
            bytes: Decrypted block with padding removed
        &#8220;&#8221;&#8220;
        # Split the ciphertext into two equal halves
        half_size = len(ciphertext) // 2
        left = ciphertext[:half_size]
        right = ciphertext[half_size:]
        
        # Feistel rounds in reverse
        for i in range(self.rounds-1, -1, -1):
            # Apply round function to right half
            f_output = self._round_function(right)
            # XOR the output with left half
            new_right = bytes(a ^ b for a, b in zip(left, f_output[:half_size]))
            # Swap halves for next round
            left = right
            right = new_right
            
        # For an even number of rounds, we need to swap back
        if self.rounds % 2 == 0:
            decrypted = right + left
        else:
            decrypted = left + right
            
        # Remove padding and return
        return decrypted[:-1]
            
            
# Example usage
if __name__ == &#8220;__main__&#8221;:
    # Create a 16-byte key (128 bits)
    key = bytes.fromhex(&#8221;00112233445566778899aabbccddeeff&#8221;)
    
    # Create a PRP instance with 4 rounds
    prp = PRP(key, rounds=4)
    
    # Create a plaintext (must be even length for splitting)
    plaintext = b&#8221;This is a test!&#8221;  # 16 bytes
    
    print(f&#8221;Original: {plaintext}&#8221;)
    print(f&#8221;Length: {len(plaintext)} bytes&#8221;)
    
    # Encrypt
    ciphertext = prp.encrypt(plaintext)
    print(f&#8221;Encrypted (hex): {ciphertext.hex()}&#8221;)
    print(f&#8221;Encrypted length: {len(ciphertext)} bytes&#8221;)
    
    # Decrypt
    decrypted = prp.decrypt(ciphertext)
    print(f&#8221;Decrypted: {decrypted}&#8221;)
    print(f&#8221;Decrypted length: {len(decrypted)} bytes&#8221;)
    
    # Compare byte by byte
    if len(plaintext) == len(decrypted):
        for i in range(len(plaintext)):
            if plaintext[i] != decrypted[i]:
                print(f&#8221;Mismatch at position {i}: {plaintext[i]} vs {decrypted[i]}&#8221;)
    else:
        print(f&#8221;Length mismatch: {len(plaintext)} vs {len(decrypted)}&#8221;)
    
    # Verify
    if plaintext == decrypted:
        print(&#8221;Success: Encryption/decryption verified!&#8221;)
    else:
        print(&#8221;Failed: Decryption didn&#8217;t match original!&#8221;)

</code></code></pre><p><em>(Note: this is just a toy; real ciphers use many rounds, carefully designed PRFs, and strong diffusion.)</em></p><div><hr></div><h3><strong>Reflection</strong></h3><p>PRPs are the natural endpoint of our chain:</p><ul><li><p><strong>PRG &#8594; PRF &#8594; PRP.</strong></p></li><li><p>From stretching randomness, to keyed functions, to invertible permutations.</p></li><li><p>PRPs give us encryption that can be undone &#8212; the core of block ciphers.</p></li></ul><p>Just like GGM proves PRFs are possible, the Feistel construction proves that <strong>PRPs are possible from PRFs</strong>. Real-world block ciphers (DES, AES) go beyond this basic template, but the Feistel idea is the conceptual backbone of why reversible, secure encryption exists.</p><div><hr></div><h2>What is next?</h2><p>We&#8217;ve unlocked the core techniques of our cryptographic &#8220;combat training.&#8221; Next up: the real combos &#8212; the block ciphers and modes that defenders actually use in the field. In the next article we&#8217;ll study the most important block ciphers (AES, DES/3DES history), explain modes like CTR, CBC, and GCM, and build working implementations from scratch. Bring curiosity &#8212; and your code editor.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.dmytrohuz.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Dmytro&#8217;s Substack is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item></channel></rss>