<?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[Software in the Grid: Software Foundations]]></title><description><![CDATA[Rebuilding the foundations beneath modern software systems: networking, protocols, operating systems, security, and the machinery that makes distributed software work.]]></description><link>https://www.softwareinthegrid.com/s/software-foundations</link><image><url>https://substackcdn.com/image/fetch/$s_!M0GQ!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F3976cb25-61bb-4530-b5dc-5535b4b51445_1254x1254.png</url><title>Software in the Grid: Software Foundations</title><link>https://www.softwareinthegrid.com/s/software-foundations</link></image><generator>Substack</generator><lastBuildDate>Sun, 27 Sep 2026 05:03:46 GMT</lastBuildDate><atom:link href="https://www.softwareinthegrid.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[Networking Machinery for Software Engineers Part 2: Extending The Empire and Rebuilding a Global Network]]></title><description><![CDATA[We pushed our kingdom beyond the horizon. To keep it connected, we had to extend our Signal Tower system with Layer 3: IP addresses, subnets, gateways, routing tables, and DHCP.]]></description><link>https://www.softwareinthegrid.com/p/networking-machinery-for-software-f6b</link><guid isPermaLink="false">https://www.softwareinthegrid.com/p/networking-machinery-for-software-f6b</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Wed, 02 Sep 2026 16:02:36 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!JAas!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d96a595-7108-43f7-92b7-4c57c97c5f2a_1672x941.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_!JAas!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d96a595-7108-43f7-92b7-4c57c97c5f2a_1672x941.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!JAas!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d96a595-7108-43f7-92b7-4c57c97c5f2a_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!JAas!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d96a595-7108-43f7-92b7-4c57c97c5f2a_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!JAas!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d96a595-7108-43f7-92b7-4c57c97c5f2a_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!JAas!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d96a595-7108-43f7-92b7-4c57c97c5f2a_1672x941.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!JAas!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d96a595-7108-43f7-92b7-4c57c97c5f2a_1672x941.png" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/0d96a595-7108-43f7-92b7-4c57c97c5f2a_1672x941.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;:3508483,&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/213862386?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d96a595-7108-43f7-92b7-4c57c97c5f2a_1672x941.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_!JAas!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d96a595-7108-43f7-92b7-4c57c97c5f2a_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!JAas!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d96a595-7108-43f7-92b7-4c57c97c5f2a_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!JAas!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d96a595-7108-43f7-92b7-4c57c97c5f2a_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!JAas!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F0d96a595-7108-43f7-92b7-4c57c97c5f2a_1672x941.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Previous Article:</h2><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;7a220686-cfcb-48e3-b64a-8cfb6a19a369&quot;,&quot;caption&quot;:&quot;Recently I started working on a networking series for software developers who enter the electrical-grid world and suddenly discover that the grid speaks in packets, interfaces, switches, routes, protocols and a suspicious number of acronyms.&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Networking Machinery for Software Engineers\nPart 1: Rebuilding a Local Network with Medieval Signal Towers&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Software engineer exploring how complex systems are built. I take them apart, rebuild them from first principles, and trace the layers connecting software, networks, infrastructure, devices, and the physical world.&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-08-28T09:48:20.438Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!Xg26!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81428888-7e1b-4174-8ad1-092e46214718_1672x941.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/networking-machinery-for-software&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:213120887,&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;Rebuilt&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!JM5w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f89608f-4575-4fe4-9df4-7d6a25e088b2_1254x1254.png&quot;,&quot;belowTheFold&quot;:false,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p></p><p><a href="https://www.dmytrohuz.com/p/networking-machinery-for-software">We have just built our small kingdom</a>. One valley, a few villages, a few signal towers, and a local network that works surprisingly well for something invented by people who still believe that medicine is mostly a matter of balancing four bodily fluids.</p><p>Unfortunately, sooner or later power becomes the main goal of the most ambitious people in the kingdom, and sooner or later one kingdom becomes too small for them. Scouts discover settlements behind the mountains, armies march along roads that were previously used for sheep, and within a few years our neat little kingdom becomes an empire with several valleys somewhere beyond the horizon.</p><p>The moral dilemma is outside the scope of this article, and we, as network engineers in medieval Europe, are definitely not in a position to judge the military policy of our lords. What we do need to judge is whether the communication system from <a href="https://www.dmytrohuz.com/p/networking-machinery-for-software">Part 1</a> can survive this expansion.</p><p>It cannot.</p><p>Our old system knows how to move a frame inside one valley, but it has no idea what a valley is, where its border lies, or what to do when a destination is hidden behind three mountain passes and somebody else&#8217;s tax system.</p><p>So we will build the larger system in the same way we built the local one. We will remain with our towers until messages can cross the whole empire, and only after the machinery works will we return to a real computer and the Internet. This order matters because IP addresses, gateways and routing tables are much easier to remember when they arrive as answers to problems we have already felt.</p><p>In this post we will discuss how IP addresses, network prefixes, gateways, routing tables and ARP turn local networks into a network of networks. We will then see how the same model appears on Linux, how DHCP gives a host the information it needs, and what changes while one packet crosses several routers.</p><p>The real-world part focuses on IPv4 carried over Ethernet. Other link technologies can carry IP without Ethernet, and IPv6 uses Neighbor Discovery instead of ARP, but one carefully bounded world is enough for now. Our empire is already difficult to govern without opening several additional continents.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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.softwareinthegrid.com/subscribe?"><span>Subscribe now</span></a></p><h2>Two orders of names</h2><p>In our existing system every tower already has a local name such as <code>0A</code>, <code>0B</code> or <code>0C</code>, and that worked perfectly while the entire known world fitted inside one valley.</p><p>Then we acquired another valley.</p><p>It has its own towers, which may be called <code>1A</code>, <code>1B</code> and <code>1C</code>. They may also be called <code>0A</code>, <code>0B</code> and <code>0C</code>, because coordination between medieval kingdoms is apparently not much better than coordination between modern IT departments.</p><p>Now an order addressed to <code>0A</code> is ambiguous. There is an <code>0A</code> in Valley 1 and another <code>0A</code> in Valley 2, and the old name tells us who should receive a message only after we already know which local network we are talking about.</p><p>This is not a flaw in the old system. A local name was invented for local delivery, and inside one valley it still does that job perfectly. The empire has simply created a second question that the local name was never meant to answer:</p><blockquote><p>In which valley does the destination live?</p></blockquote><p>So we introduce another order of names. Tower <code>0A</code> keeps its local address and independently receives the wider address <code>V1.7</code>. A tower in Valley 2 may reuse the same local address <code>0A</code> while independently receiving <code>V2.12</code>. The local and wider addresses are not constructed from one another. They are separate identifiers assigned for different scopes.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!LCQo!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83cce10d-858c-4273-98ec-989757ccfc1d_1672x941.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!LCQo!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83cce10d-858c-4273-98ec-989757ccfc1d_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!LCQo!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83cce10d-858c-4273-98ec-989757ccfc1d_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!LCQo!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83cce10d-858c-4273-98ec-989757ccfc1d_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!LCQo!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83cce10d-858c-4273-98ec-989757ccfc1d_1672x941.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!LCQo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83cce10d-858c-4273-98ec-989757ccfc1d_1672x941.png" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/83cce10d-858c-4273-98ec-989757ccfc1d_1672x941.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;:2249090,&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/213862386?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83cce10d-858c-4273-98ec-989757ccfc1d_1672x941.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_!LCQo!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83cce10d-858c-4273-98ec-989757ccfc1d_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!LCQo!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83cce10d-858c-4273-98ec-989757ccfc1d_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!LCQo!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83cce10d-858c-4273-98ec-989757ccfc1d_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!LCQo!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F83cce10d-858c-4273-98ec-989757ccfc1d_1672x941.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Figure 1. The local address </em><code>0A</code><em> can be reused in both valleys. The independent wider addresses </em><code>V1.7</code><em> and </em><code>V2.12</code><em> identify the towers across the empire, and neither is derived from </em><code>0A</code><em>.</em></p><p>We have just reconstructed the reason a machine can need both a MAC address and an IP address.</p><p>The MAC address is the local name. It identifies the immediate sender or receiver on one Ethernet network. The IP address is the wider name. It identifies the source and destination across many connected networks.</p><p>The two addresses do not compete, and one is not a more modern replacement for the other. They describe the same machine at different scopes, rather like a person&#8217;s room number and full postal address. A room number can be perfectly useful inside one building even though a letter crossing the country needs more information.</p><h3>Is the destination still inside our valley?</h3><p>Imagine that tower <code>0A</code> in Valley 1 receives an order:</p><blockquote><p>Deliver this message to <code>V1.12</code>.</p></blockquote><p>The destination begins with <code>V1</code>, and our tower also belongs to <code>V1</code>, so the destination is local. It can use exactly the machinery we built in Part 1. It discovers which local tower owns <code>V1.12</code>, learns that tower&#8217;s local address if necessary, and sends a frame through the valley.</p><p>Then another order arrives:</p><blockquote><p>Deliver this message to <code>V2.12</code>.</p></blockquote><p>This time the wider address tells us something important before a single signal is sent. The destination belongs to Valley 2, while our tower belongs to Valley 1. None of the local towers can deliver a frame directly through the mountain.</p><p>Our medieval rule can still be very simple:</p><pre><code><code>V1.*  local
everything else  beyond the valley
</code></code></pre><p>Modern networking calls the precise version of this boundary a network prefix, but we do not need the binary notation yet. For the moment, the important discovery is that an address must contain enough structure to tell us not only who the destination is, but also whether it belongs to our local world.</p><p>Once <code>0A</code> knows that <code>V2.12</code> is remote, it needs somewhere local to send the message.</p><h2>A gateway at the edge of the valley</h2><p>We build a special tower at the border of Valley 1. It can receive local frames from our valley, but it also has a road leading into another network. Let us call it <code>G1</code>.</p><p>Tower <code>0A</code> now learns one additional rule:</p><blockquote><p>If the final destination is outside Valley 1, give the packet to <code>G1</code>.</p></blockquote><p>This creates a situation that looks strange only until the two scopes become visible. Suppose <code>0A</code> wants to reach <code>V2.12</code>. The final destination is still <code>V2.12</code>, but the next tower that can physically receive a local frame is <code>G1</code>.</p><p>The message therefore carries two destinations:</p><pre><code><code>Final IP destination: V2.12
Local MAC destination: G1
</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_!S9s_!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc704fc62-2439-4d16-b27f-78f0054e6665_1672x941.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!S9s_!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc704fc62-2439-4d16-b27f-78f0054e6665_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!S9s_!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc704fc62-2439-4d16-b27f-78f0054e6665_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!S9s_!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc704fc62-2439-4d16-b27f-78f0054e6665_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!S9s_!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc704fc62-2439-4d16-b27f-78f0054e6665_1672x941.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!S9s_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc704fc62-2439-4d16-b27f-78f0054e6665_1672x941.png" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/c704fc62-2439-4d16-b27f-78f0054e6665_1672x941.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;:2934167,&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/213862386?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc704fc62-2439-4d16-b27f-78f0054e6665_1672x941.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_!S9s_!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc704fc62-2439-4d16-b27f-78f0054e6665_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!S9s_!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc704fc62-2439-4d16-b27f-78f0054e6665_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!S9s_!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc704fc62-2439-4d16-b27f-78f0054e6665_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!S9s_!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fc704fc62-2439-4d16-b27f-78f0054e6665_1672x941.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Figure 2. The local frame is an outer delivery envelope addressed to the next hop, </em><code>G1</code><em>. The packet inside keeps the final destination, </em><code>V2.12</code><em>.</em></p><p>The outer address gets the message across the current local network. The inner address survives beyond that network and tells every gateway where the journey should eventually end.</p><p>Gateway <code>G1</code> receives the frame because its own local address is written on the outside. It removes that local envelope and reads the wider address inside:</p><pre><code><code>Destination IP: V2.12
</code></code></pre><p>Now <code>G1</code> faces the same question that tower <code>0A</code> faced, only on a larger map. Where should this packet go next?</p><h2>A small map inside every gateway</h2><p>We hang a parchment inside <code>G1</code>:</p><pre><code><code>Valley 1          directly connected
Valley 2          eastern relay R1
Valley 3          northern relay R2
Everything else  royal highway G0
</code></code></pre><p>This parchment is our routing table.</p><p>It does not describe the complete road from Valley 1 to every tower in the empire. <code>G1</code> does not need to know every bridge, every mountain pass or every official who will lose the paperwork. It only needs to know the next useful step.</p><p>For <code>V2.12</code>, the table selects relay <code>R1</code>. If the link between <code>G1</code> and <code>R1</code> is another local Ethernet-like network, the wider address of the next hop is still not enough to send a frame. <code>G1</code> needs the local address of <code>R1</code> on that link.</p><p>This brings us back to the machinery from Part 1. If the mapping is not already known, <code>G1</code> asks the local network which MAC address belongs to the next-hop IP address. In IPv4 over Ethernet, this is ARP.</p><p>The forwarding process is therefore not one mysterious act called routing. It is a sequence of smaller questions:</p><pre><code><code>Where is the packet ultimately going?
        &#8595;
Which route best matches that destination?
        &#8595;
What is the next hop on that route?
        &#8595;
Which local address belongs to that next hop?
        &#8595;
Send one local frame
</code></code></pre><p>Routing chooses the direction. ARP, when the current link is IPv4 over Ethernet, supplies the local delivery address needed to take the next step.</p><h3>The same packet, a new local frame</h3><p>The route across our empire might look like this:</p><pre><code><code>Tower 0A in Valley 1
        &#8595;
Gateway G1
        &#8595;
Eastern relay R1
        &#8595;
Gateway G2
        &#8595;
Tower 0B in Valley 2
</code></code></pre><p>On the first hop, the local frame is addressed to <code>G1</code>. After <code>G1</code> opens that frame and chooses the next route, it creates a new local frame addressed to <code>R1</code>. Relay <code>R1</code> repeats the process and creates another frame addressed to <code>G2</code>. Inside Valley 2, <code>G2</code> finally creates a frame addressed to tower <code>0B</code>.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!qmAC!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25b49c1d-311e-4d69-8e7c-f762a0c33139_1672x941.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!qmAC!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25b49c1d-311e-4d69-8e7c-f762a0c33139_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!qmAC!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25b49c1d-311e-4d69-8e7c-f762a0c33139_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!qmAC!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25b49c1d-311e-4d69-8e7c-f762a0c33139_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!qmAC!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25b49c1d-311e-4d69-8e7c-f762a0c33139_1672x941.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!qmAC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25b49c1d-311e-4d69-8e7c-f762a0c33139_1672x941.png" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/25b49c1d-311e-4d69-8e7c-f762a0c33139_1672x941.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;:2958392,&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/213862386?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25b49c1d-311e-4d69-8e7c-f762a0c33139_1672x941.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_!qmAC!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25b49c1d-311e-4d69-8e7c-f762a0c33139_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!qmAC!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25b49c1d-311e-4d69-8e7c-f762a0c33139_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!qmAC!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25b49c1d-311e-4d69-8e7c-f762a0c33139_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!qmAC!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F25b49c1d-311e-4d69-8e7c-f762a0c33139_1672x941.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Figure 3. The blue end-to-end destination remains </em><code>V2.12</code><em>. Each gold local delivery ends at one next hop, after which a new local frame is created for the following link.</em></p><p>The IP destination does not become <code>G1</code>, then <code>R1</code>, then <code>G2</code>. It remains <code>V2.12</code>, because that is the end of the journey. What changes is the local delivery information wrapped around it.</p><p>This distinction gives us the mental model for the whole article:</p><blockquote><p>The IP packet describes the end-to-end conversation. The local frame describes only the next delivery.</p></blockquote><p>Every gateway repeats the same small procedure. It removes the local frame, reads the destination IP, consults its routing table, chooses a next hop, and creates whatever local frame the next link requires.</p><p>The routes also describe valleys rather than every individual tower. <code>G1</code> does not need separate rules for <code>V2.1</code>, <code>V2.2</code>, <code>V2.3</code> and every other address in Valley 2. One rule for <code>V2.*</code> is enough, because the next useful step is the same for all of them.</p><p>Our empire may eventually grow too large for routes to be written by hand. Routers can then exchange information and learn routes from one another, but that changes how the parchment is produced, not how a packet uses it. Forwarding still means reading the destination, selecting the best route and taking one local step.</p><p>We have now rebuilt the complete mechanism with towers. Local names move frames inside a valley. Wider addresses identify destinations across valleys. A boundary tells a tower whether the destination is local. A gateway accepts everything beyond that boundary. A routing table chooses the next hop, and each local network carries the packet one step farther.</p><p>Our little kingdom is no longer one network.</p><p>It is becoming a network of networks.</p><p>In other words, we have started building Layer 3.</p><p>Only now are we ready to leave the towers.</p><h2>Back to the real world</h2><p>Imagine one Linux machine connected to a home or office network:</p><pre><code><code>IP address:      192.168.1.23/24
Interface:       eth0
Default gateway: 192.168.1.1
MAC address:     3c:52:82:xx:xx:xx
</code></code></pre><p>This is almost exactly what our tower knew. The machine has a local Ethernet identity, a wider IPv4 identity, a description of its local boundary, and the address of a gateway that can accept packets leaving that boundary.</p><p>Suppose an application wants to reach <code>192.168.1.50</code>. Linux has a directly connected route for the local network, so the selected next hop is the destination itself. If the neighbor cache does not already contain the answer, ARP asks:</p><blockquote><p>Who has <code>192.168.1.50</code>?</p></blockquote><p>Once the machine learns a mapping such as <code>192.168.1.50 &#8594; aa:bb:cc:dd:ee:ff</code>, it can place the IP packet inside an Ethernet frame addressed directly to that MAC address.</p><p>Nothing particularly dramatic happened. We stayed inside one valley.</p><h3>Leaving the valley</h3><p>Now the same application wants to reach <code>8.8.8.8</code>.</p><p>Linux exposes the relevant routes with <code>ip route</code>:</p><pre><code><code>default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 scope link
</code></code></pre><p>At first this looks like the usual Linux habit of turning a simple idea into compressed hieroglyphics, but the table contains only two important rules. Destinations in <code>192.168.1.0/24</code> are directly reachable through <code>eth0</code>. Everything for which no more specific route exists goes through <code>192.168.1.1</code>.</p><p>For <code>8.8.8.8</code>, the local route does not match, so the default route selects <code>192.168.1.1</code> as the next hop. The packet remains addressed to <code>8.8.8.8</code>, while the first Ethernet frame is addressed to the MAC address of <code>192.168.1.1</code>.</p><p>If that MAC address is unknown, ARP asks:</p><blockquote><p>Who has <code>192.168.1.1</code>?</p></blockquote><p>There is one technical correction worth making explicit here. It is common to explain the host as if it first performs a separate subnet test and then, only for a remote destination, remembers that routing tables exist. The operating system actually performs a route lookup. Configuring <code>192.168.1.23/24</code> on <code>eth0</code> creates the directly connected route, while configuring the gateway creates the default route. Local and remote delivery are two outcomes of the same lookup.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!dRRQ!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04a65802-2ecb-4e05-8d50-c450eea4361f_1672x941.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!dRRQ!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04a65802-2ecb-4e05-8d50-c450eea4361f_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!dRRQ!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04a65802-2ecb-4e05-8d50-c450eea4361f_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!dRRQ!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04a65802-2ecb-4e05-8d50-c450eea4361f_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!dRRQ!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04a65802-2ecb-4e05-8d50-c450eea4361f_1672x941.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!dRRQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04a65802-2ecb-4e05-8d50-c450eea4361f_1672x941.png" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/04a65802-2ecb-4e05-8d50-c450eea4361f_1672x941.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;:2955647,&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/213862386?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04a65802-2ecb-4e05-8d50-c450eea4361f_1672x941.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_!dRRQ!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04a65802-2ecb-4e05-8d50-c450eea4361f_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!dRRQ!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04a65802-2ecb-4e05-8d50-c450eea4361f_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!dRRQ!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04a65802-2ecb-4e05-8d50-c450eea4361f_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!dRRQ!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04a65802-2ecb-4e05-8d50-c450eea4361f_1672x941.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Figure 4. The route lookup happens first. It selects either the destination itself or the gateway as the next hop, and only then does ARP resolve that selected next hop on Ethernet.</em></p><p>This ordering prevents two common confusions. ARP does not decide whether a destination is local, and it does not choose a route. It answers a local address-resolution question after routing has already chosen the next hop.</p><h2>Where does the valley end?</h2><p>Our tower used a rule such as <code>V1.*</code> to recognize its valley. IPv4 represents the same idea with a network prefix.</p><p>An IPv4 address contains 32 bits, which we usually write as four decimal octets. In <code>192.168.1.23/24</code>, the <code>/24</code> says that the first 24 bits belong to the network portion and the remaining 8 belong to the host portion.</p><p>For this address, the network is <code>192.168.1.0/24</code>. Addresses such as <code>192.168.1.5</code>, <code>192.168.1.50</code> and <code>192.168.1.200</code> belong to that network, while <code>192.168.2.5</code> does not.</p><p>The traditional subnet mask <code>255.255.255.0</code> expresses the same boundary. In binary it contains 24 ones followed by 8 zeroes. The ones mark the network portion, while the zeroes leave room for host addresses.</p><p>The boundary does not have to respect the dots in the decimal notation, because the dots exist for us and the bits exist for the machine. Consider <code>192.168.1.10/26</code>. The first 24 bits and the first 2 bits of the final octet belong to the network, leaving 6 host bits.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Dgl9!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7c80a44-3b68-464e-8f4b-5b8901f96055_1600x900.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Dgl9!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7c80a44-3b68-464e-8f4b-5b8901f96055_1600x900.png 424w, https://substackcdn.com/image/fetch/$s_!Dgl9!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7c80a44-3b68-464e-8f4b-5b8901f96055_1600x900.png 848w, https://substackcdn.com/image/fetch/$s_!Dgl9!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7c80a44-3b68-464e-8f4b-5b8901f96055_1600x900.png 1272w, https://substackcdn.com/image/fetch/$s_!Dgl9!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7c80a44-3b68-464e-8f4b-5b8901f96055_1600x900.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Dgl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7c80a44-3b68-464e-8f4b-5b8901f96055_1600x900.png" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/f7c80a44-3b68-464e-8f4b-5b8901f96055_1600x900.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;:1119051,&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/213862386?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7c80a44-3b68-464e-8f4b-5b8901f96055_1600x900.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_!Dgl9!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7c80a44-3b68-464e-8f4b-5b8901f96055_1600x900.png 424w, https://substackcdn.com/image/fetch/$s_!Dgl9!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7c80a44-3b68-464e-8f4b-5b8901f96055_1600x900.png 848w, https://substackcdn.com/image/fetch/$s_!Dgl9!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7c80a44-3b68-464e-8f4b-5b8901f96055_1600x900.png 1272w, https://substackcdn.com/image/fetch/$s_!Dgl9!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Ff7c80a44-3b68-464e-8f4b-5b8901f96055_1600x900.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Figure 5. Each final octet contains exactly eight bit positions. A </em><code>/24</code><em> leaves positions 1 through 8 for hosts, while a </em><code>/26</code><em> uses positions 1 and 2 for the network and leaves positions 3 through 8, exactly six bits, for hosts.</em></p><p>Those four <code>/26</code> networks begin at:</p><pre><code><code>192.168.1.0/26
192.168.1.64/26
192.168.1.128/26
192.168.1.192/26
</code></code></pre><p>This is why <code>192.168.1.10</code> and <code>192.168.1.50</code> belong to the first <code>/26</code>, while <code>192.168.1.70</code> belongs to the second. The decimal addresses look almost like neighbors, but the prefix has placed a real border between them.</p><p>The prefix is not an intermediate technique unrelated to our story. It is the exact mathematical form of the question our first tower had to answer:</p><blockquote><p>Does the destination belong to my local network, or must the selected route lead through a gateway?</p></blockquote><h2>Who gave the host its map?</h2><p>So far our computer somehow knew its IP address, its prefix, its default gateway and usually a DNS server. On a normal home or office network, nobody sits down every morning and manually configures every laptop, phone, printer and coffee machine that has somehow acquired WiFi.</p><p>Most of the time, DHCP does the paperwork.</p><p>A new host already has its local Ethernet identity, but it does not yet know its IPv4 address, its network boundary or its default gateway. It cannot send a normal IP request to a server whose address it does not know from an address it does not have, so it begins with a local broadcast:</p><blockquote><p>I am here. Can somebody tell me who I am supposed to be?</p></blockquote><p>The basic exchange is usually remembered as DORA: Discover, Offer, Request and Acknowledgement. The server may eventually give the client:</p><pre><code><code>IP address:      192.168.1.23
Prefix:          /24
Default gateway: 192.168.1.1
DNS server:      192.168.1.1
Lease:           24 hours
</code></code></pre><p>DHCP did not route anything. It simply handed the host the small piece of the map it needs before routing can begin.</p><p>The lease means that the address is borrowed rather than carved into stone. Before the lease expires, the client normally tries to renew it, and the server may allow the same address to remain or assign another one.</p><p>The first DHCP discovery is a local broadcast, and routers normally do not forward local broadcasts. When the DHCP server lives in another network, a DHCP relay listens locally and carries the request toward the remote server. This lets one server configure many networks without joining their broadcast domains together.</p><p>If DHCP is absent, nothing fundamental breaks. We can configure the IP address, prefix, gateway and DNS server manually, provided that the values are correct and the chosen address does not conflict with another host.</p><p>Small networks often place DHCP, routing, NAT, firewalling, DNS forwarding and WiFi inside the same plastic box near the wall. Calling that entire creature a router is convenient but slightly misleading. Router is one role performed by the device, just as a castle can be a home, a fortress, a court, a prison and a very expensive heating problem at the same time.</p><h2>Following one real packet</h2><p>Host A lives at <code>192.168.1.23/24</code>, and somewhere beyond several networks Host Z lives at <code>10.20.0.42/16</code>. Host A&#8217;s route lookup selects <code>192.168.1.1</code> as the gateway, and ARP gives it the gateway interface&#8217;s MAC address.</p><p>The word <em>interface</em> matters here. A router connects at least two networks, so it does not have one universal IP address or one universal MAC address. Each Ethernet interface belongs to a particular local network and has its own MAC address, while it will usually have an IP address in that network as well.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Jmd8!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdafd6728-b9ce-45d8-ad98-507cb7da588d_1672x941.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Jmd8!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdafd6728-b9ce-45d8-ad98-507cb7da588d_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!Jmd8!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdafd6728-b9ce-45d8-ad98-507cb7da588d_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!Jmd8!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdafd6728-b9ce-45d8-ad98-507cb7da588d_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!Jmd8!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdafd6728-b9ce-45d8-ad98-507cb7da588d_1672x941.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Jmd8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdafd6728-b9ce-45d8-ad98-507cb7da588d_1672x941.png" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/dafd6728-b9ce-45d8-ad98-507cb7da588d_1672x941.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;:2852013,&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/213862386?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdafd6728-b9ce-45d8-ad98-507cb7da588d_1672x941.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_!Jmd8!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdafd6728-b9ce-45d8-ad98-507cb7da588d_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!Jmd8!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdafd6728-b9ce-45d8-ad98-507cb7da588d_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!Jmd8!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdafd6728-b9ce-45d8-ad98-507cb7da588d_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!Jmd8!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdafd6728-b9ce-45d8-ad98-507cb7da588d_1672x941.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Figure 6. A router&#8217;s interfaces belong to different local networks. The incoming frame ends at Interface A, while a new local frame leaves through Interface B with that interface&#8217;s identity.</em></p><p>Hosts on <code>192.168.1.0/24</code> use <code>192.168.1.1</code> as their gateway because that address belongs to the router interface standing inside their own network. The interface on the other side belongs to another network and has different local neighbors.</p><p>Host A creates the first frame:</p><pre><code><code>Ethernet destination: MAC of 192.168.1.1
IPv4 destination:     10.20.0.42
</code></code></pre><p>The router removes the Ethernet frame, reads the IPv4 destination, performs its own route lookup and chooses the next hop. If the outgoing link is also Ethernet, the router resolves that next hop&#8217;s MAC address and creates a completely new frame. The next router repeats the same process.</p><p>Eventually a router finds that <code>10.20.0.0/16</code> is directly connected. On the final Ethernet network, it uses ARP to learn Host Z&#8217;s MAC address and creates the last local frame.</p><p>The IPv4 source and destination normally remain the same in this simple routed topology. NAT can deliberately rewrite addresses, but NAT is another official with another stamp and is not required to explain routing.</p><p>The packet is not completely untouched, however. Every router decreases the IPv4 Time to Live, usually called TTL, by at least one. Because TTL is part of the IPv4 header, the router also updates the IPv4 header checksum.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!L_ZY!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd2733a9-260c-4b6b-9048-d7473451cb12_1672x941.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!L_ZY!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd2733a9-260c-4b6b-9048-d7473451cb12_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!L_ZY!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd2733a9-260c-4b6b-9048-d7473451cb12_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!L_ZY!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd2733a9-260c-4b6b-9048-d7473451cb12_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!L_ZY!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd2733a9-260c-4b6b-9048-d7473451cb12_1672x941.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!L_ZY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd2733a9-260c-4b6b-9048-d7473451cb12_1672x941.png" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/bd2733a9-260c-4b6b-9048-d7473451cb12_1672x941.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;:2211619,&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/213862386?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd2733a9-260c-4b6b-9048-d7473451cb12_1672x941.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_!L_ZY!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd2733a9-260c-4b6b-9048-d7473451cb12_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!L_ZY!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd2733a9-260c-4b6b-9048-d7473451cb12_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!L_ZY!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd2733a9-260c-4b6b-9048-d7473451cb12_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!L_ZY!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fbd2733a9-260c-4b6b-9048-d7473451cb12_1672x941.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Figure 7. The IPv4 source and destination stay end to end, each local frame is rebuilt for one link, and TTL decreases as routers forward the packet.</em></p><p>TTL exists because routing information can be wrong. If two routers keep sending a packet back and forth, the packet must not wander between them until the end of history. When TTL reaches zero, the router discards it and normally sends an ICMP Time Exceeded message back toward the source.</p><p>Ethernet is not mandatory on every link. Another link technology may use different framing and may have no MAC addresses or ARP at all. What survives is the larger mechanism: the router removes the old link-layer envelope, keeps the IP destination, chooses the next route and creates whatever local delivery the outgoing link requires.</p><p>Layer 3 does not bypass Layer 2. It uses one local link, then another one, then another one. A global network is made from local networks that repeatedly agree to pass the packet on.</p><h2>Routers do not know the whole journey</h2><p>A router does not normally calculate the complete road from Host A to Host Z. It needs to answer only one question:</p><blockquote><p>Where should I send this packet next?</p></blockquote><p>This is why routing tables usually describe networks rather than every individual host. One rule can say that the whole <code>10.20.0.0/16</code> network is reachable through Router B.</p><p>Sometimes several routes match the same destination:</p><pre><code><code>10.0.0.0/8       via Router A
10.20.0.0/16     via Router B
10.20.5.0/24     via Router C
default          via Router D
</code></code></pre><p>The address <code>10.20.5.17</code> matches every line, including the default, but the <code>/24</code> route wins because it has the longest matching prefix. A longer prefix describes a smaller and more specific group of addresses. The default route has a prefix length of zero, so it is the vaguest possible instruction: if nobody knows better, send the packet this way.</p><p>For a small network, somebody can configure these routes by hand. In a much larger system, routers can exchange routes through protocols such as OSPF, IS-IS and BGP. Those protocols deserve their own story, but forwarding remains the same after the table has been built: read the destination IP, choose the most specific matching route, select the next hop, and perform one local delivery.</p><h2>What actually happens when we type <code>ping</code></h2><p>When we run:</p><pre><code><code>ping 8.8.8.8
</code></code></pre><p>the operating system performs the route lookup, selects the next hop, resolves the necessary local address if the outgoing link requires it, and sends an IPv4 packet containing an ICMP Echo Request.</p><p>If the remote host chooses to answer, it sends an ICMP Echo Reply back toward us. A successful reply means that the forward route worked, the return route worked, the necessary local deliveries succeeded, and the policies along the way allowed ICMP.</p><p>A failed <code>ping</code> therefore does not prove that the destination is dead. It proves only that the complete conversation did not return, which is a much less satisfying answer and a much more honest one.</p><h2>The mental model to keep</h2><p>We began with a local network that knew only local tower names. The second valley made those names ambiguous, so we added a wider address that named both the valley and the tower. Once a destination could be remote, we needed a boundary that distinguished our local world from everything beyond it. A remote packet needed a gateway, and the gateway needed a routing table that selected the next useful step.</p><p>Every new idea appeared because the growing system made the previous model insufficient.</p><p>The result can be carried in one sentence:</p><blockquote><p>Routing keeps an end-to-end IP destination inside the packet while rebuilding the local delivery around it for every link.</p></blockquote><p>A MAC address answers who should receive this frame here. An IP address answers where the packet is ultimately going. A prefix describes which addresses belong to a network. A route chooses the next hop, and ARP maps that next-hop IP address to a local Ethernet address when the current link needs one.</p><p>None of this replaced the network from Part 1. We built above it and then reused it at every hop.</p><p>Put those answers together and our valleys become a network of networks, which is rather inconveniently the same direction in which our rulers were taking their empire anyway.</p><h2>One machine is still a black box</h2><p>There is one character in this story that we have treated with suspicious casualness: the computer itself.</p><p>Throughout both parts I have written that the computer checks a route, stores a neighbor, owns a MAC address and sends a frame, but what inside the machine actually does those things? Where does a network interface begin and the operating system end? What changes when one computer has several interfaces, and what happens when some of those interfaces are not physical at all?</p><p>Linux can create virtual interfaces, connect them with virtual Ethernet pairs, place them in bridges, and split the network into namespaces that behave like separate hosts. One physical machine can contain several isolated valleys and the roads between them.</p><p>Which means that, instead of invading another kingdom for the next experiment, we can build almost the entire empire inside one Linux machine.</p><p>That is where we go next.</p><p>Routing explains how a packet finds its next hop. To explore how applications protect data carried over that network, continue with <a href="https://www.dmytrohuz.com/p/rebuilding-tls-from-scratch-my-complete">Rebuilding TLS From Scratch</a>. That completed series builds an authenticated, encrypted channel above TCP, one missing property at a time.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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 Rebuilt for the next step: rebuilding these networks inside Linux with interfaces, bridges, virtual links, and namespaces.</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><h2><strong>Series Hub:</strong></h2><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;cf3dcbc9-766c-4093-b324-5597c004acb2&quot;,&quot;caption&quot;:&quot;I have worked with networked software for years, but for most of that time the network was something I used rather than something I really understood. I could open a socket, call an API, connect to an IP address, and continue with the part of the system I was responsible for.&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;lg&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Networking in the Power Grid for Software Developers&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Software engineer exploring how complex systems are built. I take them apart, rebuild them from first principles, and trace the layers connecting software, networks, infrastructure, devices, and the physical world.&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-08-27T21:18:02.367Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!sXgD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fea2f83a8-82b3-42d5-817b-5266707341bb_1672x941.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/networking-in-the-power-grid-for&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:213058272,&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;Rebuilt&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!JM5w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f89608f-4575-4fe4-9df4-7d6a25e088b2_1254x1254.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div>]]></content:encoded></item><item><title><![CDATA[Networking Machinery for Software Engineers
Part 1: Rebuilding a Local Network with Medieval Signal Towers]]></title><description><![CDATA[How a ridiculous thought experiment leads to Ethernet frames, MAC addresses, switches, broadcast, and ARP.]]></description><link>https://www.softwareinthegrid.com/p/networking-machinery-for-software</link><guid isPermaLink="false">https://www.softwareinthegrid.com/p/networking-machinery-for-software</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Fri, 28 Aug 2026 09:48:20 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!Xg26!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81428888-7e1b-4174-8ad1-092e46214718_1672x941.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_!Xg26!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81428888-7e1b-4174-8ad1-092e46214718_1672x941.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Xg26!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81428888-7e1b-4174-8ad1-092e46214718_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!Xg26!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81428888-7e1b-4174-8ad1-092e46214718_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!Xg26!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81428888-7e1b-4174-8ad1-092e46214718_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!Xg26!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81428888-7e1b-4174-8ad1-092e46214718_1672x941.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Xg26!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81428888-7e1b-4174-8ad1-092e46214718_1672x941.png" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/81428888-7e1b-4174-8ad1-092e46214718_1672x941.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;:1901889,&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/213120887?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81428888-7e1b-4174-8ad1-092e46214718_1672x941.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_!Xg26!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81428888-7e1b-4174-8ad1-092e46214718_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!Xg26!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81428888-7e1b-4174-8ad1-092e46214718_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!Xg26!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81428888-7e1b-4174-8ad1-092e46214718_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!Xg26!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F81428888-7e1b-4174-8ad1-092e46214718_1672x941.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Recently I started working on a networking series for software developers who enter the electrical-grid world and suddenly discover that the grid speaks in packets, interfaces, switches, routes, protocols and a suspicious number of acronyms.</p><p>Before going anywhere near IEC 61850, utility WANs or substation networks, I wanted to understand the ordinary networking machinery itself. Not only what a switch or ARP does, but why these things had to appear in the first place.</p><p>I could start with the OSI model. I have seen that diagram enough times in my life. The problem is that a finished diagram is very good at telling us where something belongs and surprisingly bad at showing why it exists.</p><p>So I had a more ridiculous idea.</p><p>Imagine that I am somewhere in medieval Europe. No copper cable, no fiber, no radio and definitely no Ethernet. I have stone towers on hills, and the only useful property I can rely on is visibility. If one tower can see another, I can make light visible or hide it.</p><p>Could I start from that and slowly rebuild a network?</p><p>This is not a historical reconstruction. I am not claiming that medieval Europe was one good standards committee away from IEEE 802.3. The towers are only a constraint. I want to begin with the smallest possible communication system, push it until it becomes uncomfortable, fix the new problem, and then compare the thing we invented with the machinery inside a real network.</p><p>That approach is much closer to how I like to understand systems. The finished technology often hides the reasons for its own shape. Broken and incomplete versions expose them.</p><p>In this post we will discuss the local part of the network. We will begin with physical signalling between two towers, then add more participants and see why frames, MAC addresses, switching, flooding, broadcast and ARP appear. Near the end we will come back to Linux and look at the same machinery on a real machine.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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.softwareinthegrid.com/subscribe?"><span>Subscribe now</span></a></p><h2>Two towers and one bit</h2><p>I want to begin before packets, addresses and protocols exist at all.</p><p>Two towers stand on neighboring hills. Tower A can see Tower B directly.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!9xb0!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04b2efcb-70e6-4c61-a47b-863cdd855705_1280x720.svg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!9xb0!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04b2efcb-70e6-4c61-a47b-863cdd855705_1280x720.svg 424w, https://substackcdn.com/image/fetch/$s_!9xb0!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04b2efcb-70e6-4c61-a47b-863cdd855705_1280x720.svg 848w, https://substackcdn.com/image/fetch/$s_!9xb0!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04b2efcb-70e6-4c61-a47b-863cdd855705_1280x720.svg 1272w, https://substackcdn.com/image/fetch/$s_!9xb0!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04b2efcb-70e6-4c61-a47b-863cdd855705_1280x720.svg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!9xb0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04b2efcb-70e6-4c61-a47b-863cdd855705_1280x720.svg" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/04b2efcb-70e6-4c61-a47b-863cdd855705_1280x720.svg&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;:87732,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/svg+xml&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/213120887?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04b2efcb-70e6-4c61-a47b-863cdd855705_1280x720.svg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!9xb0!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04b2efcb-70e6-4c61-a47b-863cdd855705_1280x720.svg 424w, https://substackcdn.com/image/fetch/$s_!9xb0!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04b2efcb-70e6-4c61-a47b-863cdd855705_1280x720.svg 848w, https://substackcdn.com/image/fetch/$s_!9xb0!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04b2efcb-70e6-4c61-a47b-863cdd855705_1280x720.svg 1272w, https://substackcdn.com/image/fetch/$s_!9xb0!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F04b2efcb-70e6-4c61-a47b-863cdd855705_1280x720.svg 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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Diagram 1. Our entire network consists of two towers with direct visibility.</em></p><p>Tower A wants to send one piece of information to Tower B. The bridge is closed, for example.</p><p>But even that sentence is already too advanced for our network. We do not yet have a way to move a single bit between the towers.</p><p>The simplest thing I can invent is a lantern with a shutter. During a fixed interval the light is either visible or hidden.</p><pre><code><code>light visible = 1
light hidden  = 0
</code></code></pre><p>So a guard can produce something like:</p><pre><code><code>1 0 1 1 0 0 1 0
</code></code></pre><p>And the other tower can observe it.</p><p>This looks almost stupidly primitive, but it already contains the first real networking problem. An abstract <code>1</code> inside one system has to become something physical, travel through a medium, and be recovered by the other system.</p><p>In our case the medium is light through air. In Ethernet it may be an electrical or optical signal. Wireless uses radio. Real physical layers do not literally push abstract bits through a cable one after another. They deal with symbols, encoding, timing, synchronization, noise, attenuation, clock recovery and many other details that become a rabbit hole very quickly.</p><p>Even the lantern version immediately asks awkward questions. How long does one interval last? How does Tower B know where the sequence begins? What happens in fog? If the light stays visible for three intervals, how does the receiver know that it saw <code>111</code> rather than one long <code>1</code>?</p><p>I am going to stop before our medieval guards have to invent signal processing.</p><p>The useful part for this article is that we have reconstructed the job of Layer 1: make information physical enough that it can cross one link and be recovered at the other side.</p><p>That works beautifully while our entire civilization consists of two towers.</p><p>Unfortunately, I want a network.</p><h2>Adding the rest of the valley</h2><p>So I add more towers.</p><p>Some can see each other directly. Some need a relay point between them. For the moment, imagine one local communication region where a relay tower has direct light paths toward several participants.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!QjZS!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febc4c9fa-29c7-4359-a71d-1b16f5ec5ab8_1280x720.svg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!QjZS!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febc4c9fa-29c7-4359-a71d-1b16f5ec5ab8_1280x720.svg 424w, https://substackcdn.com/image/fetch/$s_!QjZS!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febc4c9fa-29c7-4359-a71d-1b16f5ec5ab8_1280x720.svg 848w, https://substackcdn.com/image/fetch/$s_!QjZS!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febc4c9fa-29c7-4359-a71d-1b16f5ec5ab8_1280x720.svg 1272w, https://substackcdn.com/image/fetch/$s_!QjZS!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febc4c9fa-29c7-4359-a71d-1b16f5ec5ab8_1280x720.svg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!QjZS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febc4c9fa-29c7-4359-a71d-1b16f5ec5ab8_1280x720.svg" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/ebc4c9fa-29c7-4359-a71d-1b16f5ec5ab8_1280x720.svg&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;:133776,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/svg+xml&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/213120887?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febc4c9fa-29c7-4359-a71d-1b16f5ec5ab8_1280x720.svg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!QjZS!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febc4c9fa-29c7-4359-a71d-1b16f5ec5ab8_1280x720.svg 424w, https://substackcdn.com/image/fetch/$s_!QjZS!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febc4c9fa-29c7-4359-a71d-1b16f5ec5ab8_1280x720.svg 848w, https://substackcdn.com/image/fetch/$s_!QjZS!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febc4c9fa-29c7-4359-a71d-1b16f5ec5ab8_1280x720.svg 1272w, https://substackcdn.com/image/fetch/$s_!QjZS!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Febc4c9fa-29c7-4359-a71d-1b16f5ec5ab8_1280x720.svg 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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Diagram 2. Several towers now share one local communication system through a relay point.</em></p><p>The light can move bits across each individual path, but the moment several towers share the system, a stream like this becomes annoyingly ambiguous:</p><pre><code><code>10110010...
</code></code></pre><p>Who is it for? Where does one message end and the next one begin? If the relay receives it, which path should it use? And if I later want to carry different kinds of data, how does the receiver know what is inside?</p><p>This is where the simple Layer 1 picture starts to break. The physical signal still matters, but I need structure above it.</p><h2>Give the towers local addresses</h2><p>I will start with the most obvious missing piece and give every tower a local identifier.</p><pre><code><code>Tower A = 0A
Tower B = 0B
Tower C = 0C
Tower D = 0D
</code></code></pre><p>There is nothing magical about these values. My tiny kingdom does not need a 48-bit address space yet.</p><p>Now instead of sending naked bits, Tower A can send a structure:</p><pre><code><code>TO:   0C
FROM: 0A
TYPE: MESSAGE
DATA: THE BRIDGE IS CLOSED
</code></code></pre><p>The relay no longer has to guess what the bits mean. There is a destination, a source, a description of what the payload contains, and the payload itself.</p><p>That structure is already suspiciously close to an Ethernet frame.</p><p>Our tower frame Ethernet equivalent What it is doing <code>TO</code> Destination MAC address Identifies the local recipient of the frame <code>FROM</code> Source MAC address Identifies the local sender of the frame <code>TYPE</code> EtherType Tells the receiver what kind of payload is carried <code>DATA</code> Payload Carries the higher-level data</p><p><em>Table 1. The fields I needed for the tower network have direct relatives in an Ethernet frame.</em></p><p>A real Ethernet frame has more machinery around this, and the signal on the wire has additional synchronization and encoding details. Our invented text header is obviously not its real wire format. Those details solve different problems, so I will leave them outside this model for now.</p><p>The important thing is that <code>0A</code>, <code>0B</code> and the rest are local link-layer identities. They play the role of MAC addresses. They do not tell us how to reach a tower somewhere on the other side of Europe. They let this local network move a frame toward the right participant.</p><p>This is roughly where Layer 2 enters the picture. We have stopped dealing only with signals and started dealing with frames and local delivery.</p><h2>The relay starts remembering</h2><p>The first version of my relay can be extremely lazy. Every time it receives a transmission, it can repeat the signal toward every other light path. At the frame level, the result is that every frame becomes visible everywhere.</p><p>This is roughly the world of an old Ethernet hub. A real hub operates at Layer 1 and repeats signals rather than parsing Ethernet frames, which is one place where the tower analogy should not be pushed too literally.</p><p>But the relay is sitting in a very useful position. When a frame arrives from the western path and says:</p><pre><code><code>FROM: 0A
</code></code></pre><p>it has learned something for free: whatever <code>0A</code> is, the western path is how I just heard from it.</p><p>So the relay can remember that fact.</p><p>After some traffic its memory may look like this:</p><pre><code><code>0A &#8594; western path
0B &#8594; northern path
0C &#8594; eastern path
0D &#8594; southern path
</code></code></pre><p>Now a frame addressed to <code>0C</code> does not have to be repeated everywhere. The relay can send it only toward the eastern path.</p><p>This is the part where our relay has quietly become an Ethernet switch.</p><p>A switch builds a forwarding table by looking at the source MAC address of frames arriving on its ports. The source address is evidence. If a frame from <code>0C</code> arrived through port 3, then <code>0C</code> is currently reachable through port 3. Real switches also age these entries because networks change, cables move and machines disappear.</p><p>The destination MAC is used for the next decision. If the switch already knows where that destination lives, it forwards the frame through the corresponding port.</p><h3>The destination that has never spoken</h3><p>This was the first place where I caught myself giving the relay knowledge it had never earned.</p><p>Suppose its table contains only:</p><pre><code><code>0A &#8594; western path
0B &#8594; northern path
</code></code></pre><p>Then a frame arrives from the west:</p><pre><code><code>FROM: 0A
TO:   0C
</code></code></pre><p>The relay can refresh <code>0A &#8594; western path</code>, because it has just observed that fact. But the destination field tells it absolutely nothing about where <code>0C</code> is located. <code>TO: 0C</code> only describes what the sender wants.</p><p>So the relay has one honest option: try all the other paths.</p><p>It retransmits the frame everywhere except back through the path it arrived on. Every participant may see it, but only <code>0C</code> should accept a unicast frame addressed to <code>0C</code>.</p><p>Ethernet calls this unknown unicast flooding.</p><p>If Tower C later sends anything:</p><pre><code><code>FROM: 0C
TO:   0A
</code></code></pre><p>then the relay finally gets real evidence. The frame came from the east, so it can learn:</p><pre><code><code>0C &#8594; eastern path
</code></code></pre><p>Future frames for <code>0C</code> can go directly there.</p><p>There is a subtle point here that I want to keep separate from ARP. Unknown unicast flooding means the sender already put a destination MAC address into the Ethernet frame, but the switch does not know which port leads to that MAC. ARP solves a different problem: the sending host may not know the destination MAC address at all.</p><p>In a normal fresh IPv4 conversation, ARP often causes the destination host to reply before the first IP unicast frame is sent. That reply also gives the switch a chance to learn the destination MAC. So you may not actually observe unknown unicast flooding every time you contact a new local IP address.</p><p>The behavior still matters. Imagine that Tower A already knows <code>0C</code>, perhaps from an existing neighbour-cache entry, while the switch&#8217;s own forwarding entry for <code>0C</code> has expired. Then A can send a perfectly valid unicast frame to <code>0C</code>, and the switch may still have to flood it.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EWRA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ce0b6e-5f30-4336-a153-4105ec54446d_1280x720.svg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EWRA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ce0b6e-5f30-4336-a153-4105ec54446d_1280x720.svg 424w, https://substackcdn.com/image/fetch/$s_!EWRA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ce0b6e-5f30-4336-a153-4105ec54446d_1280x720.svg 848w, https://substackcdn.com/image/fetch/$s_!EWRA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ce0b6e-5f30-4336-a153-4105ec54446d_1280x720.svg 1272w, https://substackcdn.com/image/fetch/$s_!EWRA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ce0b6e-5f30-4336-a153-4105ec54446d_1280x720.svg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!EWRA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ce0b6e-5f30-4336-a153-4105ec54446d_1280x720.svg" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/e3ce0b6e-5f30-4336-a153-4105ec54446d_1280x720.svg&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;:97248,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/svg+xml&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/213120887?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ce0b6e-5f30-4336-a153-4105ec54446d_1280x720.svg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EWRA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ce0b6e-5f30-4336-a153-4105ec54446d_1280x720.svg 424w, https://substackcdn.com/image/fetch/$s_!EWRA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ce0b6e-5f30-4336-a153-4105ec54446d_1280x720.svg 848w, https://substackcdn.com/image/fetch/$s_!EWRA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ce0b6e-5f30-4336-a153-4105ec54446d_1280x720.svg 1272w, https://substackcdn.com/image/fetch/$s_!EWRA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fe3ce0b6e-5f30-4336-a153-4105ec54446d_1280x720.svg 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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Diagram 3. The relay tower has become a Layer 2 switch by learning where local addresses are reachable.</em></p><p>The switch now has a useful memory, but Tower A can still be stuck for a completely different reason.</p><h2>I know the IP address. I still cannot send the frame</h2><p>Suppose an application on Tower A wants to send data to:</p><pre><code><code>192.168.1.20
</code></code></pre><p>Let us say that this address belongs to Tower C.</p><p>For the moment I will also grant Tower A one piece of knowledge: it knows that <code>192.168.1.20</code> is on the same local network. How the host makes that decision belongs to IP addressing, subnet masks and routing tables, which is exactly the problem I want to pick up in the next part.</p><p>Tower A can create an IP packet for <code>192.168.1.20</code>, but our local medium still does not forward an ordinary Ethernet frame by looking at that IP address. The Ethernet frame needs a destination MAC address.</p><p>And Tower A does not know it.</p><p>This is different from the problem we just had inside the switch.</p><p>Missing knowledge Who is missing it? Mechanism <code>MAC &#8594; switch port</code> The switch Source-MAC learning, with unknown unicast flooding when the destination is unknown <code>IPv4 address &#8594; MAC</code> The sending host ARP</p><p>I like this distinction because both failures can sound like &#8220;I do not know where C is,&#8221; while they live in different machines and are solved by different mechanisms.</p><p>ARP, the Address Resolution Protocol, exists to solve the second one for IPv4.</p><h2>A local shout</h2><p>There is a bootstrapping problem, though. If Tower A does not know Tower C&#8217;s MAC address, it cannot send Tower C a normal unicast Ethernet frame asking for it.</p><p>So I need one more local address in the tower world:</p><pre><code><code>EVERYONE
</code></code></pre><p>A frame sent to <code>EVERYONE</code> is copied toward every participant in this local communication region.</p><p>Ethernet already has exactly such a destination:</p><pre><code><code>ff:ff:ff:ff:ff:ff
</code></code></pre><p>That is the Ethernet broadcast MAC address.</p><p>Tower A can now send something equivalent to:</p><pre><code><code>Who has 192.168.1.20?
Tell 192.168.1.10.
</code></code></pre><p>The Ethernet destination is the broadcast address, so the switch floods the frame through the local broadcast domain. The switch does not need to understand the IP address in the question. In the ordinary forwarding model it is still doing a Layer 2 job: it sees a broadcast destination MAC and forwards accordingly.</p><p>Inside that Ethernet frame is an ARP message. ARP is not an IP packet, and the switch does not use ARP to decide where ordinary unicast Ethernet frames should go. ARP is a separate protocol carried directly inside Ethernet, identified by EtherType <code>0x0806</code>.</p><p>Every IPv4 host in the local broadcast domain may receive the request. Most of them inspect it, notice that <code>192.168.1.20</code> is not theirs, and move on with their lives.</p><p>Tower C recognizes its own IP address and replies with its MAC address:</p><pre><code><code>192.168.1.20 is at 02:00:00:00:00:0c
</code></code></pre><p>The reply can be sent as a unicast Ethernet frame back to Tower A because the ARP request already contained A&#8217;s addresses.</p><p>That reply does two useful things in two different places. Tower A can store:</p><pre><code><code>192.168.1.20 &#8594; 02:00:00:00:00:0c
</code></code></pre><p>in its neighbour cache, while the switch sees a frame arriving from Tower C and can learn <code>02:00:00:00:00:0c &#8594; eastern port</code> from the source MAC address.</p><p>Nobody had to teach the switch about <code>192.168.1.20</code>. It still does not need that information for normal Layer 2 forwarding.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Vn4O!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383b81cb-d78c-4b76-8529-cbd937041a79_1280x720.svg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Vn4O!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383b81cb-d78c-4b76-8529-cbd937041a79_1280x720.svg 424w, https://substackcdn.com/image/fetch/$s_!Vn4O!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383b81cb-d78c-4b76-8529-cbd937041a79_1280x720.svg 848w, https://substackcdn.com/image/fetch/$s_!Vn4O!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383b81cb-d78c-4b76-8529-cbd937041a79_1280x720.svg 1272w, https://substackcdn.com/image/fetch/$s_!Vn4O!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383b81cb-d78c-4b76-8529-cbd937041a79_1280x720.svg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Vn4O!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383b81cb-d78c-4b76-8529-cbd937041a79_1280x720.svg" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/383b81cb-d78c-4b76-8529-cbd937041a79_1280x720.svg&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;:182380,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/svg+xml&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/213120887?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383b81cb-d78c-4b76-8529-cbd937041a79_1280x720.svg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Vn4O!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383b81cb-d78c-4b76-8529-cbd937041a79_1280x720.svg 424w, https://substackcdn.com/image/fetch/$s_!Vn4O!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383b81cb-d78c-4b76-8529-cbd937041a79_1280x720.svg 848w, https://substackcdn.com/image/fetch/$s_!Vn4O!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383b81cb-d78c-4b76-8529-cbd937041a79_1280x720.svg 1272w, https://substackcdn.com/image/fetch/$s_!Vn4O!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F383b81cb-d78c-4b76-8529-cbd937041a79_1280x720.svg 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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Diagram 4. Broadcast lets ARP discover the local MAC address associated with an IPv4 address.</em></p><p>This is the part of the experiment I find especially satisfying because we now have two small pieces of memory that look similar on paper but belong to completely different parts of the system.</p><h2>Two different maps of the same local network</h2><p>Tower A may remember:</p><pre><code><code>192.168.1.20 &#8594; 02:00:00:00:00:0c
</code></code></pre><p>That is host knowledge. On Linux it lives in the neighbour table. For IPv4, ARP is the mechanism that normally creates and refreshes this mapping.</p><p>The switch may remember:</p><pre><code><code>02:00:00:00:00:0c &#8594; eastern port
</code></code></pre><p>That is switch knowledge. It was learned from the source MAC address of a frame that arrived through the eastern port.</p><p>The two tables answer different questions:</p><p>Machine What it knows Why it needs it Host <code>IPv4 address &#8594; MAC address</code> To build an Ethernet frame for a local IPv4 destination Switch <code>MAC address &#8594; port</code> To decide where to forward that Ethernet frame</p><p>This also makes the MAC and IP distinction less mystical.</p><p>A MAC address is useful for delivery on the current Layer 2 network. An IP address is the logical address of the packet&#8217;s destination in the larger Layer 3 system.</p><p>When A and C are sitting in the same valley, both addresses appear to identify the same machine, so having two of them can feel redundant. The redundancy is mostly an illusion created by the small example. Once the packet has to cross a router, the reason for both becomes much clearer.</p><p>For a local destination, A may build something conceptually like this:</p><pre><code><code>Ethernet:
  dst MAC = 02:00:00:00:00:0c
  src MAC = 02:00:00:00:00:0a
  type    = IPv4

IPv4 packet inside:
  dst IP  = 192.168.1.20
  src IP  = 192.168.1.10
</code></code></pre><p>The switch cares about the Ethernet destination MAC when forwarding the frame. Tower C eventually receives the frame, sees that the payload is IPv4, and passes the enclosed IP packet upward.</p><p>The complete first exchange is therefore not one magic lookup. It is several small mechanisms cooperating:</p><pre><code><code>Application wants to send to 192.168.1.20
            &#8595;
Host decides 192.168.1.20 is local
            &#8595;
Neighbour cache has no MAC for 192.168.1.20
            &#8595;
Host sends an ARP request in an Ethernet broadcast frame
            &#8595;
Switch learns A's source MAC and floods the broadcast
            &#8595;
C recognizes its IP address and sends an ARP reply
            &#8595;
Switch learns C's source MAC from the reply
            &#8595;
A stores 192.168.1.20 &#8594; C's MAC in its neighbour cache
            &#8595;
A puts the IP packet into a unicast Ethernet frame for C's MAC
            &#8595;
Switch uses C's MAC to select the correct port
            &#8595;
C receives the frame and extracts the IP packet
</code></code></pre><p>This is a simplified path, but it is finally a simplification I can reason with. Each table has an owner. Each address has a purpose. The switch never needs to resolve <code>192.168.1.20</code>, and ARP never tells the switch which destination port to use.</p><h2>Back on a real Linux machine</h2><p>After spending enough time with imaginary towers, I wanted to see whether the same structure is visible without the analogy.</p><p>Linux exposes the host-side neighbour table with:</p><pre><code><code>ip neigh
</code></code></pre><p>For an IPv4 neighbour, an entry can look like this:</p><pre><code><code>192.168.1.20 dev eth0 lladdr 02:00:00:00:00:0c REACHABLE
</code></code></pre><p>There it is: the same <code>IPv4 address &#8594; MAC address</code> mapping that Tower A had to learn. The Linux neighbour table is more general than ARP and also contains IPv6 neighbour information, but for IPv4 on Ethernet, ARP is the mechanism we care about here.</p><p>The exchange itself is even nicer because we can watch it happen:</p><pre><code><code>sudo tcpdump -i eth0 -n -e arp
</code></code></pre><p>If <code>eth0</code> is not your actual interface name, which is very likely on a modern Linux machine because apparently <code>eth0</code> was too easy, replace it with the real interface.</p><p>With no cached neighbour entry, contacting <code>192.168.1.20</code> should first produce an ARP request sent to the Ethernet broadcast address. Then the owner of that IP replies with its MAC address. After the mapping exists, the host can send normal unicast Ethernet frames.</p><p>If the neighbour entry already exists, the absence of ARP traffic is not evidence that the mechanism disappeared. Linux is simply using what it already knows.</p><blockquote><p>&#128270; <strong>Try it yourself:</strong> run <code>ip neigh</code>, start <code>sudo tcpdump -i &lt;interface&gt; -n -e arp</code>, and then ping another machine on the same local network. If a cached entry prevents the ARP exchange from appearing, remove only that entry with <code>sudo ip neigh del &lt;IP&gt; dev &lt;interface&gt;</code> and repeat the experiment. Use a machine and network you control.</p></blockquote><p></p><p>I will stop the Linux part here. Later in Act I I want to open one host properly and look at interfaces, bridges, network namespaces, virtual Ethernet devices and the rest of the machinery Linux piles underneath an innocent socket. Here I only wanted to verify that our medieval invention leaves fingerprints on a real machine.</p><h2>Then I add another valley</h2><p>So far everybody belongs to one local communication region. Broadcast can reach the whole group, and one Layer 2 system is enough.</p><p>Then I put another valley behind the mountains.</p><p>It has its own towers, its own local relay, its own broadcasts and its own local MAC addresses. Tower A cannot simply shout an ARP request across the mountains and expect all of Europe to participate.</p><p>Still, Tower A needs to send a message there.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!MGcI!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb9c3341-23c6-4b73-8265-d1666a070d38_1280x720.svg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!MGcI!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb9c3341-23c6-4b73-8265-d1666a070d38_1280x720.svg 424w, https://substackcdn.com/image/fetch/$s_!MGcI!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb9c3341-23c6-4b73-8265-d1666a070d38_1280x720.svg 848w, https://substackcdn.com/image/fetch/$s_!MGcI!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb9c3341-23c6-4b73-8265-d1666a070d38_1280x720.svg 1272w, https://substackcdn.com/image/fetch/$s_!MGcI!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb9c3341-23c6-4b73-8265-d1666a070d38_1280x720.svg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!MGcI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb9c3341-23c6-4b73-8265-d1666a070d38_1280x720.svg" width="1456" height="819" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/eb9c3341-23c6-4b73-8265-d1666a070d38_1280x720.svg&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;:115640,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;image/svg+xml&quot;,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:&quot;https://www.dmytrohuz.com/i/213120887?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb9c3341-23c6-4b73-8265-d1666a070d38_1280x720.svg&quot;,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!MGcI!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb9c3341-23c6-4b73-8265-d1666a070d38_1280x720.svg 424w, https://substackcdn.com/image/fetch/$s_!MGcI!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb9c3341-23c6-4b73-8265-d1666a070d38_1280x720.svg 848w, https://substackcdn.com/image/fetch/$s_!MGcI!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb9c3341-23c6-4b73-8265-d1666a070d38_1280x720.svg 1272w, https://substackcdn.com/image/fetch/$s_!MGcI!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Feb9c3341-23c6-4b73-8265-d1666a070d38_1280x720.svg 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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Diagram 5. The local-network model stops being sufficient when the destination belongs to another network.</em></p><p>This is where my local-network model finally runs out of road.</p><p>I could try to connect both valleys into one gigantic Layer 2 domain and let broadcasts spread farther and farther. Then I could keep adding valleys until one ARP request lights half the continent. Apart from being a rather beautiful medieval spectacle, it would also turn every local broadcast into everybody else&#8217;s problem.</p><p>The cleaner solution is to let each valley remain a local network and introduce a machine that knows how to move packets between networks.</p><p>That machine is a router.</p><p>And here the two-address idea starts paying rent.</p><p>If Tower A wants to send an IP packet to a host in the remote valley, the packet can keep the remote host as its IP destination. But the Ethernet frame used in A&#8217;s local valley is not addressed to that distant machine. It is addressed to the local router, the next machine that can move the packet closer to where it belongs.</p><p>Conceptually:</p><pre><code><code>IP destination:       remote host in another valley
Ethernet destination: local router
</code></code></pre><p>The router receives the local frame, removes that Layer 2 envelope, examines the IP packet, chooses the next route, and places the packet into a new Layer 2 frame appropriate for the next link.</p><p>This is the point where the medieval analogy also needs a new piece. A relay inside one valley was enough while everybody shared one local addressing and broadcast system. Communication between valleys requires a gateway that understands something larger than local tower addresses.</p><p>That larger system is what I want to rebuild in Part 2: IP addresses, subnet masks, the decision that a destination is local or remote, default gateways, routing tables and routers.</p><p>The towers got us surprisingly far, but the mountains finally forced us into Layer 3.</p><p>Continue with <a href="https://www.dmytrohuz.com/p/networking-machinery-for-software-f6b">Part 2: Extending the Empire and Rebuilding a Global Network</a>, where a second valley forces us to introduce IP addresses, prefixes, gateways, and routing.</p><p>Local delivery is one part of the story. If your next question is how applications protect the data they send, <a href="https://www.dmytrohuz.com/p/rebuilding-tls-from-scratch-my-complete">Rebuilding TLS From Scratch</a> follows a TCP connection from encryption alone to integrity, key exchange, and authentication.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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 Rebuilt to follow this network from local Ethernet delivery through IP routing and into Linux, then onward to the power grid.</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>Series Hub</strong></h2><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;645e6aea-757c-46bb-b4f6-4c9de1db16c1&quot;,&quot;caption&quot;:&quot;I have worked with networked software for years, but for most of that time the network was something I used rather than something I really understood. I could open a socket, call an API, connect to an IP address, and continue with the part of the system I was responsible for.&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;md&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Networking in the Power Grid for Software Developers&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Software engineer exploring how complex systems are built. I take them apart, rebuild them from first principles, and trace the layers connecting software, networks, infrastructure, devices, and the physical world.&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-08-27T21:18:02.367Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!sXgD!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fea2f83a8-82b3-42d5-817b-5266707341bb_1672x941.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/networking-in-the-power-grid-for&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:213058272,&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;Rebuilt&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!JM5w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f89608f-4575-4fe4-9df4-7d6a25e088b2_1254x1254.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><h2>References and further reading</h2><ul><li><p><a href="https://datatracker.ietf.org/doc/html/rfc826">RFC 826: An Ethernet Address Resolution Protocol</a></p></li><li><p><a href="https://www.ieee802.org/3/">IEEE 802.3 Ethernet Working Group</a></p></li><li><p><a href="https://man7.org/linux/man-pages/man8/ip-neighbour.8.html">Linux ip-neighbour manual</a></p></li><li><p><a href="https://docs.kernel.org/networking/tuntap.html">Linux kernel TUN/TAP documentation</a>, useful later when we rebuild networking machinery inside one Linux host</p></li><li><p><a href="https://www.dmytrohuz.com/p/a-developers-map-of-the-european">A Developer&#8217;s Map of the European Power Grid</a>, the wider series context for software developers entering the energy domain</p></li></ul>]]></content:encoded></item><item><title><![CDATA[Adding Homemade TLS to a Homemade Web Server]]></title><description><![CDATA[I combined my self-written web server and my self-written TLS project. The surprising part was how little the HTTP layer needed to know.]]></description><link>https://www.softwareinthegrid.com/p/adding-homemade-tls-to-a-homemade</link><guid isPermaLink="false">https://www.softwareinthegrid.com/p/adding-homemade-tls-to-a-homemade</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Sat, 04 Jul 2026 20:32:45 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!s2Hi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F272dd5c3-9c01-49fc-a311-175eec483b14_1672x941.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div data-attrs="{&quot;url&quot;:&quot;&quot;}" data-component-name="AssetErrorToDOM"><picture><img src="/img/missing-image.png" height="455" width="728"></picture></div><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_!s2Hi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F272dd5c3-9c01-49fc-a311-175eec483b14_1672x941.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!s2Hi!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F272dd5c3-9c01-49fc-a311-175eec483b14_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!s2Hi!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F272dd5c3-9c01-49fc-a311-175eec483b14_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!s2Hi!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F272dd5c3-9c01-49fc-a311-175eec483b14_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!s2Hi!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F272dd5c3-9c01-49fc-a311-175eec483b14_1672x941.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!s2Hi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F272dd5c3-9c01-49fc-a311-175eec483b14_1672x941.png" width="1672" height="941" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/272dd5c3-9c01-49fc-a311-175eec483b14_1672x941.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:&quot;normal&quot;,&quot;height&quot;:941,&quot;width&quot;:1672,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:0,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:&quot;&quot;,&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_!s2Hi!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F272dd5c3-9c01-49fc-a311-175eec483b14_1672x941.png 424w, https://substackcdn.com/image/fetch/$s_!s2Hi!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F272dd5c3-9c01-49fc-a311-175eec483b14_1672x941.png 848w, https://substackcdn.com/image/fetch/$s_!s2Hi!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F272dd5c3-9c01-49fc-a311-175eec483b14_1672x941.png 1272w, https://substackcdn.com/image/fetch/$s_!s2Hi!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F272dd5c3-9c01-49fc-a311-175eec483b14_1672x941.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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><strong>Original series:</strong> <a href="https://dev.to/dmytro_huz/building-your-own-web-server-part-1-theory-and-foundations-3kgo">Building Your Own Web Server</a> + <a href="https://www.dmytrohuz.com/p/rebuilding-tls-from-scratch-my-complete">Rebuilding TLS from Scratch</a></p><p>This article connects two earlier series. In the web-server series, I rebuilt the HTTP side from raw sockets up to routing and file serving. In the TLS series, I rebuilt the secure-channel side: key exchange, certificates, key derivation, and encrypted records.</p><p>This piece is where the two meet: what has to change when a web server stops receiving plaintext HTTP bytes and starts receiving encrypted TCP bytes?</p><p></p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://dmytrohuz.substack.com/subscribe?utm_source=email&amp;r=&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;action&quot;:null,&quot;class&quot;:null}" data-component-name="ButtonCreateButton"><a class="button primary" href="https://dmytrohuz.substack.com/subscribe?utm_source=email&amp;r="><span>Subscribe</span></a></p><div><hr></div><p>I expected adding TLS to my web server to change almost everything.</p><p>It did not.</p><p>The HTTP parser stayed boring. The router stayed boring. The file-serving code stayed boring.</p><p>The real change happened one layer below HTTP.</p><p>I had two separate learning projects before this:</p><ul><li><p>a small web server built from scratch: sockets, HTTP parsing, <code>location</code> matching, file serving, and eventually a single-threaded event-loop server;</p></li><li><p>a small TLS-like secure channel built from scratch: ephemeral key exchange, certificate chains, server authentication, HKDF, AES-GCM records, and the difference between identity keys and session keys.</p></li></ul><p>Putting them together led to one question:</p><blockquote><p>What changes in the web server when the bytes coming from TCP are encrypted?</p></blockquote><p>The useful answer is smaller than I expected:</p><pre><code>TCP socket
   &#8595;
TLS handshake / record layer
   &#8595; decrypted plaintext bytes
HTTP parser / router / file serving
   &#8595; plaintext HTTP response
TLS record protection
   &#8595; encrypted bytes
TCP socket
</code></pre><p>That is the whole architecture of the companion project.</p><p>This is not production HTTPS. It does not implement the real TLS 1.3 wire format, and browsers will not connect to it with <code>https://localhost:8443</code>.</p><p>It is a learning project. The goal is to make the integration boundary visible.</p><p>Once that boundary becomes clear, HTTPS feels much less magical.</p><div><hr></div><h2>The plain web server had one assumption</h2><p>Before TLS, the web server had a simple mental model.</p><p>A client connects over TCP. The server reads bytes from the socket. Those bytes are HTTP bytes. The server appends them to a per-connection buffer, tries to parse an HTTP request, matches the path, reads a file, and writes an HTTP response.</p><p>The flow looked like this:</p><pre><code>accept TCP connection
   &#8595;
read bytes from socket
   &#8595;
append bytes to HTTP input buffer
   &#8595;
parse HTTP request
   &#8595;
match URL against location config
   &#8595;
read file from root
   &#8595;
write HTTP response
</code></pre><p>The buffer is important because TCP is a stream.</p><p>One <code>recv()</code> call can give you half a request. Or exactly one request. Or multiple requests joined together. The parser cannot assume that one socket read equals one HTTP request.</p><p>In my server, the parser consumes complete HTTP messages from a buffer. If the request is incomplete, the server waits for more bytes. If there are extra bytes after a complete request, they stay in the buffer for the next parse.</p><p>For plain HTTP, socket bytes and HTTP bytes are the same thing:</p><pre><code>context.http_input.append(data)
self._handle_http_messages(context)
</code></pre><p>That line works only because the bytes from the socket are already plaintext HTTP.</p><p>TLS breaks that assumption.</p><div><hr></div><h2>After TLS, socket bytes are no longer HTTP bytes</h2><p>Once TLS is added, the TCP socket no longer carries a readable HTTP request.</p><p>The client still wants to send this:</p><pre><code>GET / HTTP/1.1
Host: localhost
Connection: close
</code></pre><p>But that request is not placed directly on the wire.</p><p>It is wrapped inside TLS records. After the handshake, those records are encrypted and authenticated. The bytes arriving at the server socket are now protocol bytes for the TLS layer, not text for the HTTP parser.</p><p>That is the shift:</p><pre><code>plain listener:
TCP bytes are HTTP bytes

TLS listener:
TCP bytes are TLS records
TLS records decrypt into HTTP bytes
</code></pre><p>If the HTTP parser starts caring about certificates, keys, record sequence numbers, or AES-GCM tags, the abstraction has leaked.</p><p>The HTTP parser should parse HTTP.</p><p>The TLS layer should turn unsafe network bytes into authenticated plaintext bytes.</p><div><hr></div><h2>The integration point belongs below HTTP</h2><p>Production servers use the same high-level boundary.</p><p>NGINX does not ask the HTTP parser to understand encrypted TLS records. A TLS implementation such as OpenSSL handles the connection security first. After that, the HTTP processing layer receives plaintext HTTP bytes.</p><p>My project copies that architecture in a smaller educational form:</p><pre><code>Production-ish mental model:
TCP &#8594; OpenSSL/TLS state &#8594; plaintext HTTP &#8594; NGINX HTTP processing

This project:
TCP &#8594; ToyTLSServerConnection &#8594; plaintext HTTP &#8594; HTTPParser / route matcher
</code></pre><p>The analogy is architectural, not protocol-compatible.</p><p>The project uses a small TLS-like protocol so the moving pieces are possible to read in one sitting. The handshake messages are simplified. The record framing is simplified. Many real TLS 1.3 details are missing on purpose.</p><p>But the boundary is the point:</p><pre><code>socket bytes in
   &#8595;
connection layer decides whether they are plain HTTP or TLS records
   &#8595;
HTTP layer receives plaintext HTTP bytes either way
</code></pre><p>That boundary lets the same HTTP code serve both ports.</p><div><hr></div><h2>The config makes the boundary visible</h2><p>The server uses an NGINX-style config because that was part of the original web-server project.</p><p>A single server block can listen on a plain port and on a TLS port:</p><pre><code>http {
    server {
        listen 8080;
        listen 8443 ssl;
        server_name localhost;

        ssl_certificate certs/server_cert.pem;
        ssl_certificate_key certs/server_key.pem;
        ssl_certificate_chain certs/intermediate_cert.pem;

        location / {
            root html;
        }
    }
}
</code></pre><p><code>listen 8080;</code> creates a normal HTTP listener.</p><p><code>listen 8443 ssl;</code> creates a listener that attaches TLS state to every accepted connection.</p><p>Both listeners still share the same routing and file-serving code. The difference is what happens between <code>recv()</code> and <code>HTTPParser.parse_message(...)</code>.</p><p>That is the part I wanted the project to expose.</p><div><hr></div><h2>The blocking TLS demo had to become a state machine</h2><p>The original TLS demo was linear and blocking.</p><p>That is perfect for teaching the handshake:</p><pre><code>receive ClientHello
send ServerHello
send ServerAuth
receive encrypted request
send encrypted response
</code></pre><p>But the web server I wanted to integrate with was not a one-client-at-a-time demo. It was a selectors-based server.</p><p>One event loop handles listening sockets and client sockets. A slow client should not freeze the whole process. That means the TLS implementation cannot sit inside a function waiting for the next record to arrive.</p><p>So the TLS code had to become buffer-oriented.</p><p>Instead of blocking on the socket, the TLS connection exposes three operations:</p><pre><code>tls.feed_wire_data(socket_bytes)
pending = tls.pop_pending_wire_data()
plaintext = tls.read_plaintext()
</code></pre><p><code>feed_wire_data(...)</code> accepts whatever bytes TCP happened to deliver.</p><p><code>pop_pending_wire_data()</code> returns handshake bytes or encrypted application data that need to be sent back to the client.</p><p><code>read_plaintext()</code> returns decrypted HTTP bytes after the TLS layer has enough complete records to process.</p><p>The web server does not need to know whether TCP split a TLS record into three reads or merged several records together. The TLS record buffer owns that problem.</p><p>That small interface is the bridge between the two projects.</p><div><hr></div><h2>Plain and TLS reads now differ by one layer</h2><p>Here is the core read path in the integrated server.</p><p>For plain HTTP, the socket bytes go directly into the HTTP input buffer:</p><pre><code># Plain path: socket bytes are already HTTP bytes.
context.http_input.append(data)
self._handle_http_messages(context)
</code></pre><p>For the TLS listener, the same socket bytes first pass through the TLS connection state:</p><pre><code># TLS path: socket bytes are handshake/application records, not HTTP.
context.tls.feed_wire_data(data)
self._queue_output(context, context.tls.pop_pending_wire_data())

plaintext = context.tls.read_plaintext()
if plaintext:
    context.http_input.append(plaintext)
    self._handle_http_messages(context)
</code></pre><p>The HTTP handler never receives encrypted bytes. It receives plaintext after the TLS layer has done its job.</p><p>After that, request handling is the same:</p><pre><code>request, consumed = HTTPParser.parse_message(context.http_input.data)
context.http_input.reduce_data(consumed)
response, should_close = build_http_response(request, context.server_block, self.base_dir)
self._send_application_bytes(context, response)
</code></pre><p>The response path mirrors the read path.</p><p>The HTTP layer builds a normal plaintext HTTP response. If the connection is plain HTTP, those bytes are written directly to the socket. If the connection is TLS, the server wraps the response in an encrypted application record first.</p><pre><code>if context.tls is not None:
    wire = context.tls.protect_application_data(plaintext_response)
else:
    wire = plaintext_response
</code></pre><p>This is the part I like most about the final design.</p><p>The web server still feels like a web server.</p><p>TLS becomes a connection concern, not an HTTP concern.</p><div><hr></div><h2>What the toy TLS layer does</h2><p>The TLS-like layer in this project is intentionally small, but it keeps the important shape of the original TLS series.</p><p>The handshake starts with ephemeral X25519 keys.</p><p>The client sends a <code>ClientHello</code> containing its ephemeral public key. The server generates its own ephemeral key for this connection and sends back a <code>ServerHello</code>.</p><p>Then the server sends authentication material:</p><ul><li><p>the server certificate,</p></li><li><p>intermediate certificates,</p></li><li><p>a signature over the two ephemeral public keys.</p></li></ul><p>That signature is the simplified version of <code>CertificateVerify</code>. It proves that the peer presenting the certificate also controls the matching private key for this specific key exchange.</p><p>After that, both sides compute the same X25519 shared secret and derive directional AES-GCM keys through HKDF.</p><p>Directional keys matter. The client-to-server key protects client records. The server-to-client key protects server records.</p><pre><code>client_write_key: records sent by the client
server_write_key: records sent by the server
</code></pre><p>Application data records also carry sequence numbers into the authenticated encryption. That lets the receiver detect tampering and sequence mismatches.</p><p>Again, this is not real TLS 1.3. The real protocol has much more structure: transcript binding, alerts, Finished messages, SNI, ALPN, session resumption, real record headers, and many other details.</p><p>For learning the web-server boundary, the smaller version is enough:</p><pre><code>handshake creates keys
records protect HTTP bytes
HTTP parser sees only decrypted bytes
</code></pre><div><hr></div><h2>The useful lesson was not only cryptography</h2><p>I started the project thinking the interesting part would be TLS.</p><p>The cryptographic pieces do matter. It is useful to understand why ephemeral keys exist, why certificates are about identity rather than encryption, and why authenticated encryption needs sequence numbers.</p><p>But the integration lesson was more general.</p><p>When you add a lower-level protocol to an existing system, the hard part is often choosing the seam.</p><p>If the seam is wrong, everything above it starts learning details it should not know.</p><p>If the seam is right, most of the application code stays boring.</p><p>In this case, the seam is between TCP and HTTP.</p><p>The HTTP parser should not know about X25519. The route matcher should not know about certificates. The file-serving code should not care whether the client used the plain port or the TLS port.</p><p>Those parts operate on HTTP.</p><p>The connection layer is responsible for turning the outside world into HTTP bytes.</p><p>That pattern shows up in a lot of infrastructure code:</p><ul><li><p>network bytes become parsed messages,</p></li><li><p>encrypted records become plaintext streams,</p></li><li><p>unreliable external systems become retried operations,</p></li><li><p>hardware signals become typed events,</p></li><li><p>logs become structured facts.</p></li></ul><p>A design question I keep coming back to is:</p><blockquote><p>What should the next layer be allowed to assume?</p></blockquote><p>For this project, the HTTP layer is allowed to assume one thing:</p><pre><code>I receive plaintext HTTP bytes.
</code></pre><p>Everything below that belongs to the connection layer.</p><div><hr></div><h2>Why build this if it is not real HTTPS?</h2><p>A fair objection is: if the project is not browser-compatible, why build it?</p><p>For me, browser-compatible TLS is too large if the goal is to understand the integration boundary.</p><p>If I used OpenSSL directly, the project would be more practical, but the interesting internals would disappear behind a library call.</p><p>If I tried to implement full TLS 1.3, the project would become mostly about protocol correctness. That is valuable, but it would bury the web-server lesson under too much detail.</p><p>The educational middle ground is a TLS-like secure channel with the same high-level shape:</p><pre><code>handshake
certificate verification
key derivation
encrypted records
application data
</code></pre><p>Then I can integrate that channel into the web server as if it were a real TLS stack.</p><p>The result is small enough to read, but realistic enough to expose the architecture.</p><p>That is the kind of learning project I like: simplified around one specific question.</p><p>Here the question is:</p><blockquote><p>What does a web server need from TLS?</p></blockquote><p>My answer after building it:</p><blockquote><p>A secure byte stream that produces plaintext HTTP for the existing HTTP layer.</p></blockquote><div><hr></div><h2>How to run the companion project</h2><p>The repository is here:</p><p><a href="https://github.com/DmytroHuzz/tls_web_server">https://github.com/DmytroHuzz/tls_web_server</a></p><p>The visual walkthrough is here:</p><p><a href="https://dmytrohuzz.github.io/tls_web_server/">https://dmytrohuzz.github.io/tls_web_server/</a></p><p>From a fresh clone:</p><pre><code>python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install --upgrade pip
python3 -m pip install -e ".[dev]"
</code></pre><p>Then run the one-command demo:</p><pre><code>python3 scripts/demo.py
</code></pre><p>It starts the server in a background thread, sends one plain HTTP request, then sends one HTTP request through the self-written TLS-like layer.</p><p>Both paths should return:</p><pre><code>HTTP/1.1 200 OK
</code></pre><p>There are also separate <code>server_side.py</code> and <code>client_side.py</code> files if you want to see the workflow from each side, closer to the style of the original projects.</p><p>The tests cover the integration points that are easy to get wrong:</p><ul><li><p>fragmented TLS handshake records,</p></li><li><p>fragmented encrypted application records,</p></li><li><p>wrong DNS name certificate rejection,</p></li><li><p>tampered encrypted record rejection,</p></li><li><p>sequence-number mismatch rejection,</p></li><li><p>HTTP requests split across multiple encrypted TLS records,</p></li><li><p>the same web server serving plain HTTP and HTTP-over-self-written-TLS.</p></li></ul><div><hr></div><h2>The mental model I keep now</h2><p>Before building this, I would have described HTTPS as &#8220;HTTP with encryption.&#8221;</p><p>That is fine as a user-level description, but it is not precise enough when writing the server.</p><p>The server-side model that helped me is this:</p><pre><code>TCP gives bytes.
TLS turns encrypted bytes into authenticated plaintext bytes.
HTTP parses the plaintext bytes.
</code></pre><p>That sounds simple, but it changes where the code belongs.</p><p>You do not sprinkle TLS checks through the HTTP parser.</p><p>You do not make the route matcher aware of certificates.</p><p>You do not duplicate the file server for secure and insecure connections.</p><p>You put a protocol layer below HTTP and keep the rest of the server honest.</p><p>That is what I wanted from this project.</p><p>Not a production HTTPS server. Not a replacement for OpenSSL.</p><p>Just a clearer picture of where HTTPS belongs.</p><p>If you want to follow the full path from encrypted TCP bytes to a parsed HTTP request and back again, I put the code and visual walkthrough here:</p><ul><li><p>Companion repo: <a href="https://github.com/DmytroHuzz/tls_web_server">https://github.com/DmytroHuzz/tls_web_server</a></p></li><li><p>Visual walkthrough: <a href="https://dmytrohuzz.github.io/tls_web_server/">https://dmytrohuzz.github.io/tls_web_server</a></p></li></ul><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:null,&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">Thanks for reading! Subscribe for free to receive new posts and support my work.</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 4 - Certificates and Trust]]></title><description><![CDATA[How certificates close the man-in-the-middle gap]]></description><link>https://www.softwareinthegrid.com/p/rebuilding-tls-part-4-certificates</link><guid isPermaLink="false">https://www.softwareinthegrid.com/p/rebuilding-tls-part-4-certificates</guid><dc:creator><![CDATA[Dmytro Huz]]></dc:creator><pubDate>Sun, 26 Apr 2026 20:24:36 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!HYYb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1a0a1c1-8a6f-4b73-8101-881f128943d8_1536x1024.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_!HYYb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1a0a1c1-8a6f-4b73-8101-881f128943d8_1536x1024.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!HYYb!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1a0a1c1-8a6f-4b73-8101-881f128943d8_1536x1024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!HYYb!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1a0a1c1-8a6f-4b73-8101-881f128943d8_1536x1024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!HYYb!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1a0a1c1-8a6f-4b73-8101-881f128943d8_1536x1024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!HYYb!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1a0a1c1-8a6f-4b73-8101-881f128943d8_1536x1024.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!HYYb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1a0a1c1-8a6f-4b73-8101-881f128943d8_1536x1024.jpeg" width="1456" height="971" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a1a0a1c1-8a6f-4b73-8101-881f128943d8_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;:325803,&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/195558698?img=https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1a0a1c1-8a6f-4b73-8101-881f128943d8_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_!HYYb!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1a0a1c1-8a6f-4b73-8101-881f128943d8_1536x1024.jpeg 424w, https://substackcdn.com/image/fetch/$s_!HYYb!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1a0a1c1-8a6f-4b73-8101-881f128943d8_1536x1024.jpeg 848w, https://substackcdn.com/image/fetch/$s_!HYYb!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1a0a1c1-8a6f-4b73-8101-881f128943d8_1536x1024.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!HYYb!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1a0a1c1-8a6f-4b73-8101-881f128943d8_1536x1024.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Previous Article:</em></p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;59aa73da-7378-48a3-ae45-d9dc744b972d&quot;,&quot;caption&quot;:&quot;Overview: Where we are and What Is Still Missing&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;sm&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;Software Engineer in the Energy Sector. AWS Community Builder (Dev Tools). (Re)building core tech in public &#8212; so it stop feeling like magic. Writing at Rebuilt.&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;:1,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Rebuilt&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!JM5w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f89608f-4575-4fe4-9df4-7d6a25e088b2_1254x1254.png&quot;,&quot;belowTheFold&quot;:false,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><p>A secure connection to the wrong server is still a broken connection.</p><p>That sentence looks strange at first. If traffic is encrypted, if nobody can read it, if nobody can modify it, then what is still missing?</p><p>The missing piece is identity.</p><p>In the previous parts of this series, we slowly moved from a plain TCP connection to something that started to look like a real secure channel. First, we added encryption. Then we added integrity. Then we stopped using fixed shared keys and introduced a handshake with X25519 and HKDF, so the client and server could derive fresh session keys for every connection.</p><p>That was a big step forward, but it was still not enough.</p><p>The client could now create a strong encrypted channel, but it still had no reliable way to know <strong>who</strong> it had created that channel with. It could be the real server. It could also be an attacker sitting in the middle, performing a separate key exchange with the client and another one with the real server.</p><p>In this post we will discuss why key exchange is not the same as authentication, why certificates exist, how certificate chains work, and how I added a simplified certificate-based authentication layer to my small TLS-like protocol.</p><p>By the end, the protocol will finally move from:</p><blockquote><p>&#8220;I have an encrypted channel with whoever answered.&#8221;</p></blockquote><p>to:</p><blockquote><p>&#8220;I have an encrypted channel with the server that proved its identity.&#8221;</p></blockquote><p>That is the moment where this toy protocol starts to feel much closer to real TLS.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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.softwareinthegrid.com/subscribe?"><span>Subscribe now</span></a></p><h2><strong>The Problem We Still Had After Key Exchange</strong></h2><p>At the end of Part 3, the protocol already had a real handshake.</p><p>The client and server exchanged ephemeral X25519 public keys. They used the resulting shared secret as input to HKDF. From that, they derived separate keys for client-to-server and server-to-client traffic. Then they used those keys to protect application data with AES-GCM.</p><p>That is already much better than hardcoding a shared key into both programs.</p><p>A hardcoded key has many problems. If someone gets it once, every connection using that key is compromised. If you want to rotate it, you need to update both sides. If two clients use the same key, one compromised client can affect other connections. It is simple for a demo, but it is not a good model for a real secure protocol.</p><p>X25519 and HKDF solved a different problem. They allowed the client and server to create fresh session keys for every connection without sending the actual secret over the network.</p><p>But there was still a hole.</p><p>The client received a public key from &#8220;the server,&#8221; but it had no way to know whether that public key really belonged to the server. The protocol could protect traffic after the handshake, but the handshake itself was not authenticated.</p><p>That means a man-in-the-middle could sit between the client and server and do this:</p><pre><code><code>Client  &lt;---- key exchange ----&gt;  Attacker  &lt;---- key exchange ----&gt;  Server</code></code></pre><p>From the client&#8217;s point of view, everything looks fine. It completed a key exchange and derived keys.</p><p>From the server&#8217;s point of view, everything also looks fine. It completed a key exchange and derived keys.</p><p>But the attacker is in the middle of both secure channels.</p><p>This is the uncomfortable lesson:</p><p>A secure key exchange with an unauthenticated peer can still give you a secure channel to the attacker.</p><p>That is why Part 4 exists.</p><div><hr></div><h2><strong>Encryption Is Not the Same as Trust</strong></h2><p>It is easy to mix these ideas together because HTTPS makes them feel like one thing.</p><p>When we open a website over HTTPS, we usually think:</p><p>The connection is encrypted, so it is secure.</p><p>But real TLS gives us several properties at the same time. Encryption is only one of them.</p><p>A useful way to separate the ideas is this:</p><pre><code><code>Encryption answers:
Can outsiders read the data?

Integrity answers:
Can outsiders modify the data without being detected?

Key exchange answers:
Can both sides create fresh session keys?

Authentication answers:
Do I know who is on the other side?</code></code></pre><p>Before Part 4, our protocol had the first three. It did not have the fourth.</p><p>That distinction matters because an attacker does not always need to break encryption. Sometimes the attacker only needs to become the endpoint that you encrypt to.</p><p>If the client encrypts data to the attacker&#8217;s key, the encryption still works. The math is not broken. AES-GCM still protects the records. HKDF still derives keys. X25519 still creates a shared secret.</p><p>The problem is not the cryptography. The problem is that the client trusted the wrong public key.</p><p>So the next question becomes simple:</p><p>How does the client know that the server&#8217;s handshake key belongs to the real server?</p><p>This is where certificates enter the story.</p><div><hr></div><h2><strong>What Certificates Actually Add</strong></h2><p>A certificate is not magic. It is also not just a random file that makes browsers happy.</p><p>At a practical level, a certificate connects an identity to a public key.</p><p>For example, a server certificate says something like:</p><pre><code><code>This public key belongs to this server identity.</code></code></pre><p>But the client should not just believe that statement because the server said so. Anyone can generate a key pair and create a file that claims to be <code>example.com</code>.</p><p>So certificates are signed.</p><p>That means another key, usually belonging to a Certificate Authority, signs the certificate data. The client can then verify the signature using the Certificate Authority&#8217;s public key.</p><p>In the real Web PKI, this usually forms a chain:</p><pre><code><code>Root CA
  signs
Intermediate CA
  signs
Server Certificate</code></code></pre><p>The root certificate is already trusted by the client&#8217;s system or browser. The server sends its certificate chain during the handshake. The client verifies each signature in the chain until it reaches a trusted root.</p><p>In my simplified implementation for Part 4, I use the same conceptual model:</p><pre><code><code>Root CA
  &#8595;
Intermediate CA
  &#8595;
Server Certificate</code></code></pre><p>The point is not to recreate all of Web PKI. The point is to make the trust chain visible.</p><p>The client does not trust the server certificate because the server sent it. The client trusts it because it can verify that the certificate was signed through a chain that ends at a trusted root.</p><p>Now the client has something it did not have before:</p><p>A public identity key that is connected to the server certificate and can be verified through a certificate chain.</p><p>But there is still one more important step.</p><div><hr></div><h2><strong>Why the Server Must Sign the Handshake</strong></h2><p>A certificate chain proves that a certificate is valid.</p><p>It does not automatically prove that the server currently speaking in this connection owns the private key for that certificate.</p><p>That difference is important.</p><p>If the server only sends a certificate chain, an attacker could potentially copy that public certificate chain and send it to the client. Public certificates are public. They are not secrets.</p><p>So the server must prove ownership of the corresponding private key.</p><p>In real TLS, this is done with a handshake signature. In TLS 1.3, the server signs data derived from the handshake transcript. This binds the server&#8217;s authenticated identity to the exact handshake that is happening now.</p><p>In my simplified Part 4 implementation, I use the same core idea, but in a smaller form.</p><p>The server has two different types of keys:</p><pre><code><code>Long-term identity key
= connected to the server certificate

Ephemeral X25519 key
= used only for this connection&#8217;s key exchange</code></code></pre><p>The server sends its ephemeral X25519 public key, its certificate chain, and a signature over the handshake data.</p><p>The client verifies three things:</p><pre><code><code>1. Is the certificate chain valid?

2. Does the server certificate contain the expected identity key?

3. Did the server sign this handshake using the private key that matches the certificate?</code></code></pre><p>This is the key conceptual step.</p><p>The ephemeral X25519 key gives us fresh session keys. The certificate gives us identity. The signature connects both worlds together.</p><p>Without that signature, the certificate and the key exchange are two separate facts.</p><p>With the signature, the server says:</p><p>I own the private key for this certificate, and I am binding that identity to this ephemeral key exchange.</p><p>That is what stops the man-in-the-middle from silently replacing the handshake key.</p><div><hr></div><h2><strong>The Architecture of Part 4</strong></h2><p>After Part 4, the handshake looks roughly like this:</p><pre><code><code>Client
  |
  |  ClientHello
  |  ephemeral X25519 public key
  |
  v
Server
  |
  |  ServerHello
  |  ephemeral X25519 public key
  |  certificate chain
  |  handshake signature
  |
  v
Client</code></code></pre><p>Then both sides derive the shared secret using X25519:</p><pre><code><code>client private key + server public key
server private key + client public key</code></code></pre><p>Both sides arrive at the same shared secret without sending that secret over the network.</p><p>Then HKDF turns that shared secret into actual session keys.</p><p>Then AES-GCM protects the application records.</p><p>The important change is that the client no longer accepts the server&#8217;s ephemeral public key blindly. It checks whether the authenticated server signed the handshake.</p><p>The complete shape now looks like this:</p><pre><code><code>Certificate chain
  proves server identity

Handshake signature
  binds server identity to the ephemeral key exchange

X25519
  creates a fresh shared secret

HKDF
  derives directional session keys

AES-GCM
  protects application records</code></code></pre><p>This is still a simplified protocol, but the main pieces now line up with the real TLS story much better.</p><div><hr></div><h2><strong>What We Built in Code</strong></h2><p>For this part, I added a small certificate infrastructure to the project.</p><p>The implementation generates a simple hierarchy:</p><pre><code><code>Root CA
Intermediate CA
Server Certificate</code></code></pre><p>The server uses the server certificate and private key as its long-term identity. During the handshake, it still creates a fresh ephemeral X25519 key pair for the current connection.</p><p>That distinction matters.</p><p>The long-term certificate key is not used to encrypt application data. It is used to prove identity.</p><p>The ephemeral X25519 key is not used as identity. It is used to create a fresh shared secret for this connection.</p><p>This separation is one of the most important design ideas in modern TLS. Long-term keys authenticate. Ephemeral keys protect the session.</p><p>The simplified Part 4 flow is:</p><pre><code><code>1. Client creates an ephemeral X25519 key pair.

2. Client sends its public key.

3. Server creates an ephemeral X25519 key pair.

4. Server sends:
   - its ephemeral public key
   - certificate chain
   - signature over handshake data

5. Client verifies the certificate chain.

6. Client verifies the handshake signature.

7. Both sides derive the shared secret with X25519.

8. Both sides derive session keys with HKDF.

9. Application data is protected with AES-GCM.</code></code></pre><p>The full code is in the GitHub repository:</p><p><a href="https://github.com/DmytroHuzz/rebuilding_tls">https://github.com/DmytroHuzz/rebuilding_tls</a></p><p>The full technical walkthrough is here:</p><p><a href="https://dmytrohuzz.github.io/rebuilding_tls/part_4/walkthrough/walkthrough.html">https://dmytrohuzz.github.io/rebuilding_tls/part_4/walkthrough/walkthrough.html</a></p><p>I intentionally keep the article focused on the protocol idea rather than pasting the whole implementation here. Long code blocks inside an article often create an illusion of depth, but they usually make the article harder to read.</p><p>The repository is the right place for the full implementation. The article is the right place for the explanation.</p><div><hr></div><h2><strong>What This Still Is Not</strong></h2><p>This is not real TLS.</p><p>That sentence is important.</p><p>This project is an educational reconstruction of some core TLS ideas. It is not a library. It is not a production protocol. It is not something that should protect real traffic.</p><p>Real TLS has many more details and much stronger guarantees around the handshake. For example, real TLS 1.3 binds the handshake with a transcript hash. It supports negotiation, extensions, certificate validation rules, revocation mechanisms, session resumption, alerts, many edge cases, and years of hardening against attacks that are easy to miss when building a small protocol.</p><p>My version is intentionally smaller.</p><p>It is useful because it makes the main ideas visible:</p><pre><code><code>Why encryption alone is not enough.

Why integrity must be added.

Why fixed keys are weak.

Why key exchange gives fresh session keys.

Why key exchange is still not authentication.

Why certificates exist.

Why the server must sign the handshake.

Why TLS has the shape it has.</code></code></pre><p>That is the real goal of this series.</p><p>Not to replace TLS, but to make TLS less mysterious.</p><h2><strong>Try It Yourself</strong></h2><p>The project is public and runnable.</p><p>The main repository is here:</p><p><a href="https://github.com/DmytroHuzz/rebuilding_tls">https://github.com/DmytroHuzz/rebuilding_tls</a></p><p>The Part 4 walkthrough is here:</p><p><a href="https://dmytrohuzz.github.io/rebuilding_tls/part_4/walkthrough/walkthrough.html">https://dmytrohuzz.github.io/rebuilding_tls/part_4/walkthrough/walkthrough.html</a></p><p>The complete series landing page is here:</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;d90ba029-d3a4-4d48-8461-304373219921&quot;,&quot;caption&quot;:&quot;A year ago I wrote a series of articles about how a web server works.&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Rebuilding TLS From Scratch &#8212; My Complete Learning Journey&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Software Engineer in the Energy Sector. AWS Community Builder (Dev Tools). (Re)building core tech in public &#8212; so it stop feeling like magic. Writing at Rebuilt.&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-27T21:39:41.947Z&quot;,&quot;cover_image&quot;:&quot;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&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/rebuilding-tls-from-scratch-my-complete&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:192355388,&quot;type&quot;:&quot;newsletter&quot;,&quot;reaction_count&quot;:2,&quot;comment_count&quot;:1,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Rebuilt&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!JM5w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f89608f-4575-4fe4-9df4-7d6a25e088b2_1254x1254.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 understand the path from the beginning, I recommend reading the series in order. Each part fixes one problem and reveals the next one.</p><p>Part 1 starts with encryption. Part 2 adds integrity. Part 3 adds key exchange and session keys. Part 4 adds authentication through certificates and a handshake signature.</p><p>That sequence matters because it shows why TLS is not just &#8220;encryption.&#8221; It is a stack of answers to different problems.</p><div><hr></div><h2><strong>Summary</strong></h2><p>Before Part 4, the protocol could create an encrypted channel, but it could not prove who was on the other side.</p><p>That is a serious problem.</p><p>Encryption protects data from being read. Integrity protects data from being modified. Key exchange creates fresh session keys. But authentication tells the client whether it is talking to the real server or to an attacker in the middle.</p><p>In Part 4, I added that missing authentication layer.</p><p>The client now verifies a certificate chain and checks a handshake signature. The server uses a long-term identity key to authenticate itself, while still using an ephemeral X25519 key for the actual key exchange. HKDF derives session keys, and AES-GCM protects application data.</p><p>The result is still not real TLS, but it now has the core shape:</p><pre><code><code>authenticated handshake
fresh session keys
protected records</code></code></pre><p>And that is the point of this whole series.</p><p>TLS is hard to understand when you only look at the final protocol. There are too many details, too many names, too many moving parts.</p><p>But when you rebuild it step by step, each piece starts to make sense.</p><p>You first feel the problem.</p><p>Then you add the missing mechanism.</p><p>Then you see why the real protocol looks the way it does.</p><p>For the building blocks beneath this handshake, return to <a href="https://www.dmytrohuz.com/p/rebuilding-cryptography-from-scratch">Rebuilding Cryptography From Scratch</a>, the completed reading path through ciphers, message authentication, and public-key encryption.</p><p>For the machinery beneath the TCP connection, continue with <a href="https://www.dmytrohuz.com/p/networking-in-the-power-grid-for">the networking series</a>. It starts with local Ethernet delivery, then introduces IP and routing before moving toward Linux and the power grid.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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 Rebuilt to follow the next reconstruction: the networks beneath an application, from Ethernet and IP toward Linux and the power grid.</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, 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.softwareinthegrid.com/p/rebuilding-tls-part-3-building-our</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Previous Article:</em></p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;a3b907b8-ab7a-4b77-a780-e20636f0cc84&quot;,&quot;caption&quot;:&quot;In the first part of this series, we built our first fake secure channel:&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;sm&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;Software Engineer in the Energy Sector. AWS Community Builder (Dev Tools). (Re)building core tech in public &#8212; so it stop feeling like magic. Writing at Rebuilt.&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;Rebuilt&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!JM5w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f89608f-4575-4fe4-9df4-7d6a25e088b2_1254x1254.png&quot;,&quot;belowTheFold&quot;:false,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></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.softwareinthegrid.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 Rebuilt for more systems rebuilt from first principles, from cryptographic protocols to the networks beneath them.</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;showDescription&quot;:true,&quot;showImage&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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>Part 4 builds this missing layer of authentication.</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>Continue with <a href="https://www.dmytrohuz.com/p/rebuilding-tls-part-4-certificates">Part 4: Certificates and Trust</a>, which completes this reconstruction by adding authentication. The <a href="https://www.dmytrohuz.com/p/rebuilding-tls-from-scratch-my-complete">complete TLS series hub</a> collects all four parts.</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><h2>Next article</h2><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;ed1112e7-0049-4193-bb7e-ec60ded31368&quot;,&quot;caption&quot;:&quot;Previous Article:&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;sm&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Rebuilding TLS, Part 4 - Certificates and Trust&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Software Engineer in the Energy Sector. AWS Community Builder (Dev Tools). (Re)building core tech in public &#8212; so it stop feeling like magic. Writing at Rebuilt.&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-26T20:24:36.572Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!HYYb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1a0a1c1-8a6f-4b73-8101-881f128943d8_1536x1024.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/rebuilding-tls-part-4-certificates&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:195558698,&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;Rebuilt&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!JM5w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f89608f-4575-4fe4-9df4-7d6a25e088b2_1254x1254.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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 Rebuilt for more systems rebuilt from first principles, from cryptographic protocols to the networks beneath them.</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.softwareinthegrid.com/p/rebuilding-tls-part-2-adding-integrity</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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;showDescription&quot;:true,&quot;showImage&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.softwareinthegrid.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 Rebuilt for more reconstructions of the mechanisms behind secure communication.</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.softwareinthegrid.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.softwareinthegrid.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;showDescription&quot;:true,&quot;showImage&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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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><h2>Full code</h2><p>The complete code for this part is available on GitHub:</p><p><a href="https://github.com/DmytroHuzz/rebuilding_tls/tree/main/part_2">Rebuilding TLS, Part 2 code</a></p><div><hr></div><h2>Next article:</h2><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;fedd2861-0c5a-4033-9d31-4d253307661e&quot;,&quot;caption&quot;:&quot;Previous Article:&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;sm&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;Software Engineer in the Energy Sector. AWS Community Builder (Dev Tools). (Re)building core tech in public &#8212; so it stop feeling like magic. Writing at Rebuilt.&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;:1,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Rebuilt&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!JM5w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f89608f-4575-4fe4-9df4-7d6a25e088b2_1254x1254.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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 Rebuilt for more reconstructions of the mechanisms behind secure communication.</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 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.softwareinthegrid.com/p/rebuilding-tls-part-1-why-encryption</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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.softwareinthegrid.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 Rebuilt for more reconstructions of the mechanisms behind secure communication.</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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>The complete code for this part is available on GitHub: <a href="https://github.com/DmytroHuzz/rebuilding_tls/tree/main/part_1">Rebuilding TLS, Part 1 code</a>.</p><div><hr></div><h2>Next article:</h2><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;1e716372-eb3d-4b99-ac9c-aac2ac0cf863&quot;,&quot;caption&quot;:&quot;In the first part of this series, we built our first fake secure channel:&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;sm&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;Software Engineer in the Energy Sector. AWS Community Builder (Dev Tools). (Re)building core tech in public &#8212; so it stop feeling like magic. Writing at Rebuilt.&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;Rebuilt&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!JM5w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f89608f-4575-4fe4-9df4-7d6a25e088b2_1254x1254.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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 Rebuilt for more reconstructions of the mechanisms behind secure communication.</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[The complete reading path from encrypted records to integrity, key exchange, certificates, and trust, with code and experiments.]]></description><link>https://www.softwareinthegrid.com/p/rebuilding-tls-from-scratch-my-complete</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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 class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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.softwareinthegrid.com/subscribe?"><span>Subscribe now</span></a></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>The point of the toy protocol is to make real TLS easier to understand. The completed series follows that path through record protection, key exchange, certificates, and trust. The repository and Part 4 walkthrough linked below let you inspect the final implementation and its simplifications.</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;showDescription&quot;:true,&quot;showImage&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 is the central hub for the completed series. It brings the articles, code, and walkthrough together in one reading path.</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>Status: complete. Read Parts 1&#8211;5 in order, from encryption and record integrity to the handshake, certificates, and trust. And adding toy-TLS to the toy-WebServer in the end.</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;showDescription&quot;:true,&quot;showImage&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;showDescription&quot;:true,&quot;showImage&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;showDescription&quot;:true,&quot;showImage&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><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;04357092-974f-4863-98c1-af7c43351329&quot;,&quot;caption&quot;:&quot;We add certificates and a certificate authority to authenticate the server, explain how verification helps prevent man-in-the-middle attacks, and integrate this final piece into our simplified TLS protocol.&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;md&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Rebuilding TLS, Part 4 - Certificates and Trust&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Software Engineer in the Energy Sector. AWS Community Builder (Dev Tools). (Re)building core tech in public &#8212; so it stop feeling like magic. Writing at Rebuilt.&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-26T20:24:36.572Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!HYYb!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa1a0a1c1-8a6f-4b73-8101-881f128943d8_1536x1024.jpeg&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/rebuilding-tls-part-4-certificates&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:195558698,&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;Rebuilt&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!JM5w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f89608f-4575-4fe4-9df4-7d6a25e088b2_1254x1254.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;8a8bc075-aae7-4603-a04a-8801eeb31eaf&quot;,&quot;caption&quot;:&quot;This article connects two series. In the web-server series, I rebuilt the HTTP side from raw sockets up to routing and file serving. In the current TLS series, I rebuilt the secure-channel side: key exchange, certificates, key derivation, and encrypted records.\n\nThis piece is where the two meet: what has to change when a web server stops receiving plaintext HTTP bytes and starts receiving encrypted TCP bytes?&quot;,&quot;cta&quot;:null,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;md&quot;,&quot;isEditorNode&quot;:true,&quot;title&quot;:&quot;Adding Homemade TLS to a Homemade Web Server&quot;,&quot;publishedBylines&quot;:[{&quot;id&quot;:392416265,&quot;name&quot;:&quot;Dmytro Huz&quot;,&quot;bio&quot;:&quot;Software engineer exploring how complex systems are built. I take them apart, rebuild them from first principles, and trace the layers connecting software, networks, infrastructure, devices, and the physical world.&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-07-04T20:32:45.302Z&quot;,&quot;cover_image&quot;:&quot;https://substackcdn.com/image/fetch/$s_!s2Hi!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F272dd5c3-9c01-49fc-a311-175eec483b14_1672x941.png&quot;,&quot;cover_image_alt&quot;:null,&quot;canonical_url&quot;:&quot;https://www.dmytrohuz.com/p/adding-homemade-tls-to-a-homemade&quot;,&quot;section_name&quot;:null,&quot;video_upload_id&quot;:null,&quot;id&quot;:205097817,&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;Rebuilt&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!JM5w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f89608f-4575-4fe4-9df4-7d6a25e088b2_1254x1254.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.softwareinthegrid.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.softwareinthegrid.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 you enjoyed rebuilding TLS, continue with <a href="https://www.dmytrohuz.com/p/networking-in-the-power-grid-for">the networking series</a>, which follows the machinery beneath a socket, from Ethernet and IP toward Linux and the power grid. Subscribe to Rebuilt to follow the next system as I rebuild it.</p><h2><strong>Links</strong></h2><ul><li><p>Part 4 walkthrough (rendered) &#8212; <a href="https://dmytrohuzz.github.io/rebuilding_tls/part_4/walkthrough/walkthrough.html">dmytrohuzz.github.io/rebuilding_tls/.../walkthrough.html</a></p></li><li><p>Repository &#8212; <a href="https://github.com/DmytroHuzz/rebuilding_tls">github.com/DmytroHuzz/rebuilding_tls</a></p></li><li><p>Questions, feedback, found a bug? &#8212; <a href="https://www.linkedin.com/in/dmitriyhuz/">LinkedIn: dmitriyhuz</a></p></li></ul><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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.softwareinthegrid.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[A four-part reading guide to timestamps, Linux clocks, NTP, PTP, and the tools that make time synchronization work.]]></description><link>https://www.softwareinthegrid.com/p/a-practical-guide-to-time-for-developers-746</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>The four-part reading path below moves from the meaning of a timestamp to clocks inside one computer, synchronization between computers, and a practical Linux cheat sheet.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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.softwareinthegrid.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.softwareinthegrid.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.softwareinthegrid.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;showDescription&quot;:true,&quot;showImage&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><p></p><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;showDescription&quot;:true,&quot;showImage&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><p></p><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;showDescription&quot;:true,&quot;showImage&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><p></p><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;showDescription&quot;:true,&quot;showImage&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 as the reading guide to the series. For the packet paths that synchronization depends on, continue with <a href="https://www.dmytrohuz.com/p/networking-in-the-power-grid-for">the networking series</a>. For the wider infrastructure around those networks, start with <a href="https://www.dmytrohuz.com/p/a-developers-map-of-the-european">A Developer&#8217;s Map of the European Power Grid</a>.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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 Rebuilt to follow the next system, from Linux and networking to the devices and infrastructure that depend on them.</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.softwareinthegrid.com/p/a-practical-guide-to-time-for-developers-314</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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.softwareinthegrid.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.softwareinthegrid.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>This is the final piece of the four-part guide: a practical cheat sheet to bookmark and return to. The <a href="https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-746">complete time series hub</a> collects the explanations behind these commands.</p><div><hr></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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 Rebuilt for practical explorations of clocks, Linux, networks, and the systems that depend on them.</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.softwareinthegrid.com/p/a-practical-guide-to-time-for-developers-ec8</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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.softwareinthegrid.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 Rebuilt to follow how clocks, networks, and Linux fit together, and where those mechanisms meet real devices and infrastructure.</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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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><p>To understand the network carrying those timing messages, follow <a href="https://www.dmytrohuz.com/p/networking-in-the-power-grid-for">the networking series</a> through Ethernet, IP, and routing. For the wider setting behind the power-grid examples, <a href="https://www.dmytrohuz.com/p/where-software-lives-in-the-power">Where Software Lives in the Power Grid</a> maps the devices, control systems, and applications around that communication.</p><div><hr></div><h2>Next part:</h2><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;42da1442-0c6d-4034-a99c-039a91a7b2de&quot;,&quot;caption&quot;:&quot;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.&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;sm&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;Software Engineer in the Energy Sector. AWS Community Builder (Dev Tools). (Re)building core tech in public &#8212; so it stop feeling like magic. Writing at Rebuilt.&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;Rebuilt&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!JM5w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f89608f-4575-4fe4-9df4-7d6a25e088b2_1254x1254.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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 Rebuilt to follow how clocks, networks, and Linux fit together, and where those mechanisms meet real devices and infrastructure.</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.softwareinthegrid.com/p/cloud-sells-geographical-abstraction</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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.softwareinthegrid.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.softwareinthegrid.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.softwareinthegrid.com/p/i-built-ac-trace-to-question-the</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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.softwareinthegrid.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.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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.softwareinthegrid.com/p/a-practical-guide-to-time-for-developers-2ec</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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.softwareinthegrid.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 Rebuilt for practical explorations of clocks, Linux, networks, and the systems that depend on them.</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>Continue with <a href="https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-ec8">Part 3: How Computers Share Time</a> to move from clocks inside one machine to synchronization across a network. The <a href="https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-746">four-part series hub</a> collects the full reading path.</p><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;1363e9ca-99fc-4937-a697-46057e481de6&quot;,&quot;caption&quot;:&quot;Intro Every action film has that scene just before the military operation begins.&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;sm&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;Software Engineer in the Energy Sector. AWS Community Builder (Dev Tools). (Re)building core tech in public &#8212; so it stop feeling like magic. Writing at Rebuilt.&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;:1,&quot;comment_count&quot;:0,&quot;publication_id&quot;:6272314,&quot;publication_name&quot;:&quot;Rebuilt&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!JM5w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f89608f-4575-4fe4-9df4-7d6a25e088b2_1254x1254.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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 Rebuilt for practical explorations of clocks, Linux, networks, and the systems that depend on them.</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.softwareinthegrid.com/p/a-practical-guide-to-time-for-developers</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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.softwareinthegrid.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 Rebuilt for practical explorations of clocks, Linux, networks, and the systems that depend on them.</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.softwareinthegrid.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.softwareinthegrid.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><hr></div><h2>Next part:</h2><div class="digest-post-embed" data-attrs="{&quot;nodeId&quot;:&quot;2b8e9ee6-db13-40cd-919a-6feae55ede15&quot;,&quot;caption&quot;:&quot;Time on a computer looks simple: call now(), get a timestamp, move on.&quot;,&quot;cta&quot;:&quot;Read full story&quot;,&quot;showBylines&quot;:true,&quot;showDescription&quot;:true,&quot;showImage&quot;:true,&quot;size&quot;:&quot;sm&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;Software Engineer in the Energy Sector. AWS Community Builder (Dev Tools). (Re)building core tech in public &#8212; so it stop feeling like magic. Writing at Rebuilt.&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;Rebuilt&quot;,&quot;publication_logo_url&quot;:&quot;https://substackcdn.com/image/fetch/$s_!JM5w!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F2f89608f-4575-4fe4-9df4-7d6a25e088b2_1254x1254.png&quot;,&quot;belowTheFold&quot;:true,&quot;youtube_url&quot;:null,&quot;show_links&quot;:null,&quot;feed_url&quot;:null}"></div><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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 Rebuilt for practical explorations of clocks, Linux, networks, and the systems that depend on them.</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.softwareinthegrid.com/p/the-aha-moment-of-public-key-encryption</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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.softwareinthegrid.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.softwareinthegrid.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. This concludes the <a href="https://www.dmytrohuz.com/p/rebuilding-cryptography-from-scratch">cryptography series</a>. Continue with <a href="https://www.dmytrohuz.com/p/rebuilding-tls-from-scratch-my-complete">Rebuilding TLS From Scratch</a>, the completed four-part reconstruction that brings encryption, integrity, key exchange, and authentication into one channel.</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.softwareinthegrid.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 Rebuilt to follow how cryptographic building blocks become protocols, networks, and working systems.</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.softwareinthegrid.com/p/building-own-mac-part-3-reinventing</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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.softwareinthegrid.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 Rebuilt for more reconstructions of the mechanisms behind secure communication.</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>To see message authentication become part of a working channel, continue with <a href="https://www.dmytrohuz.com/p/rebuilding-tls-from-scratch-my-complete">Rebuilding TLS From Scratch</a>. For the ciphers and other foundations behind these MACs, return to the <a href="https://www.dmytrohuz.com/p/rebuilding-cryptography-from-scratch">complete cryptography series hub</a>.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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.softwareinthegrid.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.softwareinthegrid.com/p/building-own-mac-part-2-fixing-aes</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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.softwareinthegrid.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 Rebuilt for more reconstructions of the mechanisms behind secure communication.</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>Part 3 repeats the experiment with a different primitive:</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>Continue with <a href="https://www.dmytrohuz.com/p/building-own-mac-part-3-reinventing">Part 3: Reinventing HMAC from SHA-256</a> to rebuild a hash-based MAC in the same &#8220;no names until the end&#8221; style. The <a href="https://www.dmytrohuz.com/p/rebuilding-cryptography-from-scratch">complete cryptography reading path</a> connects both MAC designs to the ciphers beneath them.</p><p></p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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 Rebuilt for more reconstructions of the mechanisms behind secure communication.</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.softwareinthegrid.com/p/building-own-mac-part-1-encrypted</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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>The published follow-ups explore both block-cipher-based and hash-based MAC designs.</p><div><hr></div><h3>What&#8217;s next</h3><p>The remaining articles examine how to:</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>Continue with <a href="https://www.dmytrohuz.com/p/building-own-mac-part-2-fixing-aes">Part 2: Fixing AES and Reinventing CMAC</a> for the block-cipher construction and its implementation.</p><p>Then follow <a href="https://www.dmytrohuz.com/p/building-own-mac-part-3-reinventing">Part 3: Reinventing HMAC from SHA-256</a> to explore the hash-based construction:</p><ul><li><p>start from SHA-256</p></li><li><p>build a MAC on top of it</p></li><li><p>derive the design through failed attempts and fixes</p></li></ul><p>No black boxes.</p><p>No &#8220;just trust the library&#8221;.</p><p>No magic. The <a href="https://www.dmytrohuz.com/p/rebuilding-cryptography-from-scratch">complete cryptography series hub</a> collects the full reading path.</p><p class="button-wrapper" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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.softwareinthegrid.com/subscribe?"><span>Subscribe now</span></a></p>]]></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.softwareinthegrid.com/p/building-own-block-cipher-part-3</link><guid isPermaLink="false">https://www.softwareinthegrid.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 buttonBase-GK1x3M"><svg aria-hidden="true" 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" class="icon-noB79L"><g><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 buttonBase-GK1x3M"><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 icon-noB79L"><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.softwareinthegrid.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 Rebuilt for more reconstructions of the mechanisms behind secure communication.</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;embedding_publication_id&quot;:null,&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;,&quot;source&quot;:null}" 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">10 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><p>Encryption is only part of secure communication. Continue with <a href="https://www.dmytrohuz.com/p/building-own-mac-part-1-encrypted">Building Own MAC, Part 1: Encrypted, but Not Trusted</a> to explore the missing integrity property, or return to the <a href="https://www.dmytrohuz.com/p/rebuilding-cryptography-from-scratch">complete cryptography series hub</a>.</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://www.softwareinthegrid.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 Rebuilt for more reconstructions of the mechanisms behind secure communication.</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>