<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dmytro Huz</title>
    <description>The latest articles on DEV Community by Dmytro Huz (@dmytro_huz).</description>
    <link>https://dev.to/dmytro_huz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1824917%2F1549c3d8-4f61-495f-b96f-6ddd331a3c9f.PNG</url>
      <title>DEV Community: Dmytro Huz</title>
      <link>https://dev.to/dmytro_huz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dmytro_huz"/>
    <language>en</language>
    <item>
      <title>Rebuilding TLS, Part 3 — Building Our First Handshake</title>
      <dc:creator>Dmytro Huz</dc:creator>
      <pubDate>Sun, 19 Apr 2026 17:09:17 +0000</pubDate>
      <link>https://dev.to/aws-builders/rebuilding-tls-part-3-building-our-first-handshake-4a2j</link>
      <guid>https://dev.to/aws-builders/rebuilding-tls-part-3-building-our-first-handshake-4a2j</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Overview: Where we are and What Is Still Missing&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In the previous part of this series, we made our fake secure channel much less fake.&lt;/p&gt;

&lt;p&gt;We started with the broken encrypted transport from &lt;a href="https://www.dmytrohuz.com/p/rebuilding-tls-part-1-why-encryption" rel="noopener noreferrer"&gt;Part 1&lt;/a&gt;, added integrity with HMAC, added sequence numbers to make the record layer less naive, &lt;a href="https://www.dmytrohuz.com/p/rebuilding-tls-part-2-adding-integrity" rel="noopener noreferrer"&gt;and then moved to AEAD&lt;/a&gt; — the approach modern systems usually use to protect records.&lt;/p&gt;

&lt;p&gt;At that point, our protocol could already do something meaningful:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;encrypt application data&lt;/li&gt;
&lt;li&gt;detect tampering&lt;/li&gt;
&lt;li&gt;reject modified records&lt;/li&gt;
&lt;li&gt;keep some minimal record-layer state&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That was a real step forward.&lt;/p&gt;

&lt;p&gt;But it still relied on one very unrealistic assumption:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;both sides already shared the secret keys&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And that is exactly what we need to remove now.&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;if client and server do not already share a secret, how can they create one over an insecure network in the first place?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That is the goal of this part.&lt;/p&gt;

&lt;p&gt;We are going to build the next missing layer of the protocol: the handshake.&lt;/p&gt;

&lt;p&gt;The architecture of this step is simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client                           Server
------                           ------
Handshake messages  &amp;lt;---------&amp;gt;  Handshake messages
       |                               |
       v                               v
  shared secret                  shared secret
       |                               |
       +---------&amp;gt; HKDF &amp;lt;--------------+
                    |
                    v
              session keys
                    |
                    v
         protected application data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The idea is to let the connection create fresh key material dynamically instead of starting with a hardcoded application key.&lt;/p&gt;

&lt;p&gt;We will implement that in three steps.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;That will take us one big step closer to the shape of real TLS.&lt;/p&gt;

&lt;p&gt;But still not all the way.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;And that is where this part is heading.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;A Very Short Note on Public Key Exchange&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The basic idea of public key exchange is simple.&lt;/p&gt;

&lt;p&gt;Two sides communicate over an insecure network. They exchange some public information. And from that exchange, both sides derive the same shared secret — without ever sending that secret directly over the wire.&lt;/p&gt;

&lt;p&gt;That is the key point.&lt;/p&gt;

&lt;p&gt;The network can be fully visible.&lt;/p&gt;

&lt;p&gt;An observer can see all handshake messages.&lt;/p&gt;

&lt;p&gt;But the observer still should not be able to derive the same secret.&lt;/p&gt;

&lt;p&gt;That is exactly the kind of mechanism we need now.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;If you want the deeper intuition behind why this works, I already wrote about it here:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The aha moment of public key encryption&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.dmytrohuz.com/p/the-aha-moment-of-public-key-encryption" rel="noopener noreferrer"&gt;https://www.dmytrohuz.com/p/the-aha-moment-of-public-key-encryption&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For now, the main idea we need is this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;each side contributes its own private value&lt;/li&gt;
&lt;li&gt;both sides exchange some public values&lt;/li&gt;
&lt;li&gt;both sides derive the same shared secret&lt;/li&gt;
&lt;li&gt;that secret can then become the basis for session keys&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So let’s build that first in the most explicit way, with classic Diffie-Hellman where the shared public parameters are still visible in the handshake.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Implementation Part 1 — Our First Handshake with Classic Diffie-Hellman&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now let’s build the first real handshake in the series.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;In this version, both sides work with the same public parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a prime p&lt;/li&gt;
&lt;li&gt;a generator g&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The actual secret material comes from somewhere else:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the client chooses a private exponent a&lt;/li&gt;
&lt;li&gt;the server chooses a private exponent b&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From those private values, both sides compute public values:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the client computes A = g^a mod p&lt;/li&gt;
&lt;li&gt;the server computes B = g^b mod p&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then they exchange A and B.&lt;/p&gt;

&lt;p&gt;And this is the key step:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the client computes s = B^a mod p&lt;/li&gt;
&lt;li&gt;the server computes s = A^b mod p&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both sides end up with the same shared secret, without ever sending that secret directly over the network.&lt;/p&gt;

&lt;p&gt;In diagram form, the handshake looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Client                                        Server
------                                        ------
choose private a
compute A = g^a mod p

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

                                              choose private b
                                              compute B = g^b mod p

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

compute s = B^a mod p                           compute s = A^b mod p
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is our first real handshake.&lt;/p&gt;

&lt;p&gt;Until now, the protocol always started with a secret key that already existed.&lt;/p&gt;

&lt;p&gt;Now the connection itself creates the secret.&lt;/p&gt;

&lt;p&gt;That is a major shift.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The raw Diffie-Hellman math&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="c1"&gt;# RFC 3526 Group 14: 2048-bit MODP prime
&lt;/span&gt;&lt;span class="n"&gt;DH_PRIME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;29024E088A67CC74020BBEA63B139B22514A08798E3404DD&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;EF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3D&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;C2007CB8A163BF0598DA48361C55D39A69163FA8FD24CF5F&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;83655D23DCA3AD961C62F356208552BB9ED529077096966D&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;DE2BCBF6955817183995497CEA956AE515D2261898FA0510&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;15728E5A8AACAA68FFFFFFFFFFFFFFFF&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;DH_GENERATOR&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;generate_private_exponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urandom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;big&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;compute_public_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;private&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;private&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;compute_shared_secret&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;peer_public&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;private&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;peer_public&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;private&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the whole core idea in code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;private exponent stays local&lt;/li&gt;
&lt;li&gt;public value goes on the wire&lt;/li&gt;
&lt;li&gt;shared secret is derived independently on both sides&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is the heart of Diffie-Hellman.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Client side&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;client_handshake&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;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.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="c1"&gt;# The client chooses p and g.  These are PUBLIC — not secret.
&lt;/span&gt;    &lt;span class="c1"&gt;# Anyone on the wire can see them, and that is perfectly fine.
&lt;/span&gt;    &lt;span class="c1"&gt;# The security of DH depends on the hardness of the discrete
&lt;/span&gt;    &lt;span class="c1"&gt;# logarithm problem, not on hiding p and g.
&lt;/span&gt;    &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DH_PRIME&lt;/span&gt;
    &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DH_GENERATOR&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  Public parameters (chosen by client, sent to server):&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;    p = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;... (&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bit_length&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; bits)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;    g = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 1: Generate client's private exponent and public value.
&lt;/span&gt;    &lt;span class="c1"&gt;# The private exponent is the ONE thing that stays secret.
&lt;/span&gt;    &lt;span class="n"&gt;client_private&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generate_private_exponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;client_public&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;compute_public_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client_private&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;client_public_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int_to_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client_public&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 2: Send ClientHello with p, g, and our public value.
&lt;/span&gt;    &lt;span class="c1"&gt;# All three are public.  The private exponent is NOT included.
&lt;/span&gt;    &lt;span class="n"&gt;p_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int_to_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;g_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int_to_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;client_hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;encode_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TAG_DH_P&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p_bytes&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TAG_DH_G&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;g_bytes&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TAG_DH_PUBLIC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;client_public_bytes&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# Step 3: send p, g, and the client’s public value inside ClientHello
&lt;/span&gt;    &lt;span class="nf"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;client_hello&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 4: Receive ServerHello with the server's public value.
&lt;/span&gt;    &lt;span class="n"&gt;server_hello_raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recv_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fields&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;decode_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server_hello_raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;server_public_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;TAG_DH_PUBLIC&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;server_public_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;server_public_bytes&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ServerHello missing DH public value&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;server_public&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;bytes_to_int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server_public_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  &amp;lt;- Received ServerHello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  Server public value B:   &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;hex_preview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server_public_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 5: Compute the shared secret.
&lt;/span&gt;    &lt;span class="c1"&gt;# shared = B^a mod p = (g^b)^a mod p = g^(ab) mod p
&lt;/span&gt;    &lt;span class="n"&gt;shared_int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;compute_shared_secret&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server_public&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;client_private&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;shared_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int_to_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shared_int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;shared_bytes&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the client side, the flow is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;choose a private exponent&lt;/li&gt;
&lt;li&gt;compute the public value&lt;/li&gt;
&lt;li&gt;send p, g, and the client’s public value inside ClientHello&lt;/li&gt;
&lt;li&gt;receive the server’s public value&lt;/li&gt;
&lt;li&gt;derive the shared secret&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That is the first point in the series where the client does not begin with the application key. It participates in creating it.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Server side&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;server_handshake&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;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.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="c1"&gt;# Step 1: Receive ClientHello — parse p, g, and client's public value.
&lt;/span&gt;    &lt;span class="c1"&gt;# The server does NOT assume any particular p or g.  It uses whatever
&lt;/span&gt;    &lt;span class="c1"&gt;# the client proposes.  (In a production system, the server would
&lt;/span&gt;    &lt;span class="c1"&gt;# validate that p is a safe prime and g is a proper generator.
&lt;/span&gt;    &lt;span class="c1"&gt;# We skip that here for clarity.)
&lt;/span&gt;    &lt;span class="n"&gt;client_hello_raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recv_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fields&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;decode_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client_hello_raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;p_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
    &lt;span class="n"&gt;g_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
    &lt;span class="n"&gt;client_public_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;TAG_DH_P&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;p_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;TAG_DH_G&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;g_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
        &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;TAG_DH_PUBLIC&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;client_public_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;p_bytes&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ClientHello missing DH prime (p)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;g_bytes&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ClientHello missing DH generator (g)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;client_public_bytes&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ClientHello missing DH public value (A)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Deserialize the parameters from bytes.
&lt;/span&gt;    &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;bytes_to_int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;bytes_to_int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;client_public&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;bytes_to_int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client_public_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 2: Generate server's private exponent and public value
&lt;/span&gt;    &lt;span class="c1"&gt;# using the p and g received from the client.
&lt;/span&gt;    &lt;span class="n"&gt;server_private&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generate_private_exponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 3: Compute server's public value
&lt;/span&gt;    &lt;span class="n"&gt;server_public&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;compute_public_value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server_private&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;server_public_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int_to_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server_public&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 4: Send ServerHello with our public value.
&lt;/span&gt;    &lt;span class="c1"&gt;# Only B is sent — p and g are already known from the ClientHello.
&lt;/span&gt;    &lt;span class="n"&gt;server_hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;encode_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TAG_DH_PUBLIC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;server_public_bytes&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;server_hello&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 5: Compute the shared secret.
&lt;/span&gt;    &lt;span class="c1"&gt;# shared = A^b mod p = (g^a)^b mod p = g^(ab) mod p
&lt;/span&gt;    &lt;span class="n"&gt;shared_int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;compute_shared_secret&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client_public&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;server_private&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;shared_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;int_to_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shared_int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;shared_bytes&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server does the mirror image:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;receive p, g, and the client’s public value&lt;/li&gt;
&lt;li&gt;choose its own private exponent&lt;/li&gt;
&lt;li&gt;compute its own public value&lt;/li&gt;
&lt;li&gt;send that value back in ServerHello&lt;/li&gt;
&lt;li&gt;derive the same shared secret from the client’s public value&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So at the end of the handshake, both sides have the same secret — but that secret was never transmitted directly.&lt;/p&gt;

&lt;p&gt;That is the big win.&lt;/p&gt;

&lt;p&gt;After this step, the connection can create fresh shared key material dynamically.&lt;/p&gt;

&lt;p&gt;That is a much more realistic foundation.&lt;/p&gt;

&lt;p&gt;But it is also still awkward.&lt;/p&gt;

&lt;p&gt;Not conceptually awkward — educationally this version is very useful — 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.&lt;/p&gt;

&lt;p&gt;That is exactly why the next step will replace this version with X25519.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Implementation Part 2 — Simplifying the Handshake with X25519&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The classic Diffie-Hellman version was useful because it made the mechanics of the handshake fully visible.&lt;/p&gt;

&lt;p&gt;But it also makes something else visible:&lt;/p&gt;

&lt;p&gt;it is a bit clunky.&lt;/p&gt;

&lt;p&gt;Not conceptually clunky — educationally it is great — 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.&lt;/p&gt;

&lt;p&gt;So now we keep the same core idea and simplify the workflow.&lt;/p&gt;

&lt;p&gt;That is where &lt;strong&gt;X25519&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;The conceptual goal stays exactly the same:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;both sides generate ephemeral private/public key pairs&lt;/li&gt;
&lt;li&gt;both sides exchange public keys&lt;/li&gt;
&lt;li&gt;both sides derive the same shared secret&lt;/li&gt;
&lt;li&gt;that secret will later become the basis for session keys&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What changes is the &lt;em&gt;shape&lt;/em&gt; of the handshake.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;That is why I wanted this section right after the classic DH version.&lt;/p&gt;

&lt;p&gt;Classic DH makes the mechanism visible.&lt;/p&gt;

&lt;p&gt;X25519 shows what the modern streamlined version looks like.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Client-side handshake structure&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Here is the current client handshake implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;client_handshake&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Perform the client side of the X25519 handshake.

    Returns the 32-byte shared secret.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;[handshake] Client: starting X25519 handshake&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 1: Generate an ephemeral X25519 keypair.
&lt;/span&gt;    &lt;span class="c1"&gt;# "Ephemeral" means we create a fresh keypair for this session only.
&lt;/span&gt;    &lt;span class="c1"&gt;# The private key never leaves this process and is discarded after use.
&lt;/span&gt;    &lt;span class="n"&gt;client_private&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;X25519PrivateKey&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;client_public&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client_private&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;public_key&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;client_public_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client_public&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;public_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Encoding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Raw&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PublicFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 2: Send ClientHello with our public key.
&lt;/span&gt;    &lt;span class="n"&gt;client_hello&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;encode_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TAG_X25519_PUBLIC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;client_public_bytes&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;client_hello&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 3: Receive ServerHello with the server's public key.
&lt;/span&gt;    &lt;span class="n"&gt;server_hello_raw&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recv_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fields&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;decode_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server_hello_raw&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;server_public_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;TAG_X25519_PUBLIC&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;server_public_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;server_public_bytes&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ServerHello missing X25519 public key&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Deserialize the server's public key from raw bytes.
&lt;/span&gt;    &lt;span class="n"&gt;server_public&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;X25519PublicKey&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;from_public_bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server_public_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 4: Compute the shared secret.
&lt;/span&gt;    &lt;span class="c1"&gt;# X25519(client_private, server_public) = X25519(server_private, client_public)
&lt;/span&gt;    &lt;span class="c1"&gt;# This is the elliptic-curve equivalent of g^(ab) mod p from v1.
&lt;/span&gt;    &lt;span class="n"&gt;shared_secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client_private&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;exchange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server_public&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;shared_secret&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I like this version because it makes the transition very clear.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What changed conceptually&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Compared to the classic DH version, the protocol has become simpler in three important ways.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. No explicit shared public parameters in the handshake&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In the previous version, the client sent the prime and generator so the whole structure of classic Diffie-Hellman stayed visible.&lt;/p&gt;

&lt;p&gt;Now that goes away.&lt;/p&gt;

&lt;p&gt;X25519 already gives us a fixed, standard structure for the exchange, so the handshake only needs to carry the public key material.&lt;/p&gt;

&lt;p&gt;That makes the protocol smaller and cleaner.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2. The public values are much more compact&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In the classic DH version, the public values were tied to a large prime-field construction and looked much heavier in the protocol.&lt;/p&gt;

&lt;p&gt;In this version, the public keys are just 32 bytes.&lt;/p&gt;

&lt;p&gt;That is a huge practical simplification.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;3. The code starts to look more like real modern protocol code&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This line from the comments says it well:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;generate(), exchange(), done.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is exactly the feeling this section should create.&lt;/p&gt;

&lt;p&gt;We are still doing public-key exchange.&lt;/p&gt;

&lt;p&gt;We are still deriving a shared secret.&lt;/p&gt;

&lt;p&gt;But the implementation shape is now much closer to what modern systems actually use.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What this version still does not solve&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Even after switching to X25519, this version is still simplified:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;there is still &lt;strong&gt;no authentication&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;the shared secret is &lt;strong&gt;not yet turned into session keys&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;there is still &lt;strong&gt;no record-layer encryption using the new keys&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next step, we will add &lt;strong&gt;HKDF&lt;/strong&gt; and derive proper working session keys from it.&lt;/p&gt;

&lt;p&gt;That is where the handshake starts to connect back to the record protection we built earlier.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Implementation Part 3 — Deriving Session Keys with HKDF&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;At this point, both the classic Diffie-Hellman version and the X25519 version give us the same kind of output:&lt;/p&gt;

&lt;p&gt;a shared secret that both sides can compute independently.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;But there is still one important design question left:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;should we use that raw shared secret directly as the application key?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For a toy demo, we probably could.&lt;/p&gt;

&lt;p&gt;But even here, that would be the wrong direction.&lt;/p&gt;

&lt;p&gt;Because a cleaner protocol separates these two ideas:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the handshake creates a shared secret&lt;/li&gt;
&lt;li&gt;the protocol derives working session keys from that secret&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is exactly where &lt;strong&gt;HKDF&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;So instead of treating the X25519 output as “the AES key,” we will use HKDF to derive proper session keys from it.&lt;/p&gt;

&lt;p&gt;That already makes the protocol feel much closer to real TLS.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What changes conceptually&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The structure now becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;X25519 shared secret
        |
        v
      HKDF
        |
        v
  session key material
        |
        v
 protected application data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an important shift.&lt;/p&gt;

&lt;p&gt;Before this step, the handshake produced something secret and we could have stopped there.&lt;/p&gt;

&lt;p&gt;After this step, the handshake produces an &lt;em&gt;input&lt;/em&gt; to a key schedule.&lt;/p&gt;

&lt;p&gt;That is a much better protocol design.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why this matters&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;There are two main reasons to do this.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1. The raw shared secret is handshake output, not final protocol state&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The shared secret is the result of key exchange. That does not automatically mean it should be used directly as the application-data key.&lt;/p&gt;

&lt;p&gt;Protocols usually want a cleaner boundary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;handshake result first&lt;/li&gt;
&lt;li&gt;working keys second&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2. We can derive keys for different purposes&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Once we introduce a key-derivation step, we are no longer forced into “one secret for everything.”&lt;/p&gt;

&lt;p&gt;Even in this toy protocol, that opens the door to a much more realistic design.&lt;/p&gt;

&lt;p&gt;For example, instead of one single AEAD key, we can derive:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;client → server key&lt;/li&gt;
&lt;li&gt;server → client key&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is already much closer to how real secure protocols think.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Deriving the keys&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In the current implementation, HKDF takes the X25519 shared secret and stretches it into 64 bytes of key material.&lt;/p&gt;

&lt;p&gt;Then that material is split into two 32-byte keys:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;one for traffic from client to server&lt;/li&gt;
&lt;li&gt;one for traffic from server to client&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That gives us directional keys instead of one shared application key for both directions.&lt;/p&gt;

&lt;p&gt;Here is the key schedule:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# key_schedule_x25519.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;cryptography.hazmat.primitives&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;hashes&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;cryptography.hazmat.primitives.kdf.hkdf&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HKDF&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;derive_session_keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shared_secret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;tuple&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
    &lt;span class="n"&gt;key_material&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HKDF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;algorithm&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;hashes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SHA256&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;salt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;toy-tls-part-3-x25519&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;derive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shared_secret&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;client_to_server_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key_material&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;server_to_client_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;key_material&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;client_to_server_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;server_to_client_key&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I like this step a lot because it is small in code, but it changes the protocol mindset in an important way.&lt;/p&gt;

&lt;p&gt;We are no longer thinking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;handshake gives us the key&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We are now thinking:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;handshake gives us secret material, and the protocol derives the keys it actually wants to use&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is a much stronger model.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;A small but important detail&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Notice that the two sides must interpret the derived keys consistently.&lt;/p&gt;

&lt;p&gt;If the client treats the first 32 bytes as the client → server key, then the server must do the same. Otherwise the channel will immediately break.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;That is another reason protocols need structure, not just primitives.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Connecting HKDF back to the record layer&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now we can finally connect this part back to what we built earlier.&lt;/p&gt;

&lt;p&gt;In Part 2, we already built an AEAD-protected record layer. But that record layer still depended on hardcoded keys.&lt;/p&gt;

&lt;p&gt;Now that changes.&lt;/p&gt;

&lt;p&gt;The AEAD layer no longer starts with a static key from configuration.&lt;/p&gt;

&lt;p&gt;It receives fresh traffic keys from the handshake.&lt;/p&gt;

&lt;p&gt;So the protocol shape becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Handshake -&amp;gt; X25519 shared secret -&amp;gt; HKDF -&amp;gt; directional session keys -&amp;gt; AEAD protected records
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is a major milestone in the series.&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;first establish shared key material&lt;/li&gt;
&lt;li&gt;then derive traffic keys&lt;/li&gt;
&lt;li&gt;then use those keys to protect application data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is already much closer to the shape of real TLS.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Using the new session keys&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Once the keys are derived, the record layer can use them directly.&lt;/p&gt;

&lt;p&gt;Conceptually, the flow now looks like this:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Client&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Connected to &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# ==========================================
&lt;/span&gt;    &lt;span class="c1"&gt;# PHASE 1: HANDSHAKE
&lt;/span&gt;    &lt;span class="c1"&gt;# ==========================================
&lt;/span&gt;    &lt;span class="c1"&gt;# New in Part 3: the handshake dynamically establishes session keys.
&lt;/span&gt;    &lt;span class="c1"&gt;# No pre-shared secret needed.
&lt;/span&gt;    &lt;span class="n"&gt;client_write_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;server_write_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;client_handshake&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# ==========================================
&lt;/span&gt;    &lt;span class="c1"&gt;# PHASE 2: APPLICATION DATA
&lt;/span&gt;    &lt;span class="c1"&gt;# ==========================================
&lt;/span&gt;    &lt;span class="c1"&gt;# The record layer now uses HKDF-derived keys instead of hardcoded ones.
&lt;/span&gt;    &lt;span class="c1"&gt;# The record format is the same as Part 2 Stage 3 (AEAD).
&lt;/span&gt;
    &lt;span class="c1"&gt;# --- Send request (encrypted with client_write_key) ---
&lt;/span&gt;    &lt;span class="n"&gt;protected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;protect_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client_write_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;send_seq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;protected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;send_seq&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

    &lt;span class="c1"&gt;# --- Receive response (decrypted with server_write_key) ---
&lt;/span&gt;    &lt;span class="n"&gt;raw_response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recv_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;unprotect_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server_write_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;recv_seq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;raw_response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;recv_seq&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  Decrypted response:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  *** REJECTED: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; ***&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Done.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;use client_write_key to protect outgoing application data&lt;/li&gt;
&lt;li&gt;use server_write_key to unprotect incoming application data&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Server&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setsockopt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOL_SOCKET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SO_REUSEADDR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Listening on &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="c1"&gt;# ==========================================
&lt;/span&gt;        &lt;span class="c1"&gt;# PHASE 1: HANDSHAKE
&lt;/span&gt;        &lt;span class="c1"&gt;# ==========================================
&lt;/span&gt;        &lt;span class="n"&gt;client_write_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;server_write_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;server_handshake&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# ==========================================
&lt;/span&gt;        &lt;span class="c1"&gt;# PHASE 2: APPLICATION DATA
&lt;/span&gt;        &lt;span class="c1"&gt;# ==========================================
&lt;/span&gt;
        &lt;span class="c1"&gt;# --- Receive request (decrypted with client_write_key) ---
&lt;/span&gt;        &lt;span class="n"&gt;raw_request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recv_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;unprotect_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client_write_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;recv_seq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;raw_request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;recv_seq&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  *** REJECTED: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; ***&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  Connection closed — refusing to process invalid data.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

            &lt;span class="c1"&gt;# --- Send response (encrypted with server_write_key) ---
&lt;/span&gt;            &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Content-Type: text/plain&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Content-Length: 13&lt;/span&gt;&lt;span class="se"&gt;\r\n\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello, client&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
            &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="n"&gt;protected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;protect_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;server_write_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;send_seq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;protected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;send_seq&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Done.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;use client_write_key to unprotect incoming client traffic&lt;/li&gt;
&lt;li&gt;use server_write_key to protect outgoing server traffic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That means the two directions are now separated.&lt;/p&gt;

&lt;p&gt;This is cleaner than one symmetric application key shared blindly by both directions, and it makes the protocol feel more deliberate.&lt;/p&gt;

&lt;p&gt;Even in this simplified version, that is a meaningful step.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What this step really gave us&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;By adding HKDF, we improved the protocol in a way that is easy to underestimate.&lt;/p&gt;

&lt;p&gt;We did not just “derive another key.”&lt;/p&gt;

&lt;p&gt;We made the protocol architecture cleaner.&lt;/p&gt;

&lt;p&gt;Now the handshake and the traffic layer are connected in a more principled way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the handshake creates shared secret material&lt;/li&gt;
&lt;li&gt;the key schedule turns that material into working keys&lt;/li&gt;
&lt;li&gt;the record layer consumes those keys&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a much better model than treating the raw X25519 result as the final answer.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;But we are still not secure&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;And now we arrive at the uncomfortable but necessary part.&lt;/p&gt;

&lt;p&gt;Even with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a real handshake&lt;/li&gt;
&lt;li&gt;X25519&lt;/li&gt;
&lt;li&gt;HKDF&lt;/li&gt;
&lt;li&gt;fresh directional session keys&lt;/li&gt;
&lt;li&gt;AEAD-protected records&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;the protocol still cannot be considered secure enough.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because all of this still says nothing about &lt;strong&gt;who&lt;/strong&gt; is on the other side.&lt;/p&gt;

&lt;p&gt;The handshake can successfully create shared secrets.&lt;/p&gt;

&lt;p&gt;HKDF can successfully derive traffic keys.&lt;/p&gt;

&lt;p&gt;The record layer can successfully protect application data.&lt;/p&gt;

&lt;p&gt;And an attacker can still sit in the middle and run two separate handshakes.&lt;/p&gt;

&lt;p&gt;That is the next lesson.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Still Not Secure — The Man-in-the-Middle Problem&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;At this point, our protocol already looks much more serious than the one we started with.&lt;/p&gt;

&lt;p&gt;We now have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a real handshake&lt;/li&gt;
&lt;li&gt;fresh shared secrets&lt;/li&gt;
&lt;li&gt;X25519 instead of a pre-shared application key&lt;/li&gt;
&lt;li&gt;HKDF-derived session keys&lt;/li&gt;
&lt;li&gt;AEAD-protected application records&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is a long way from the fake secure channel in Part 1.&lt;/p&gt;

&lt;p&gt;But it is still not enough.&lt;/p&gt;

&lt;p&gt;The missing piece is one of the most important ideas in this whole series:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;key exchange is not authentication&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Our handshake proves that both sides can derive the same shared secret.&lt;/p&gt;

&lt;p&gt;What it does &lt;strong&gt;not&lt;/strong&gt; prove is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;who&lt;/strong&gt; is actually on the other side.&lt;/p&gt;

&lt;p&gt;And that difference is the whole problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The attack&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Imagine an active attacker sitting between the client and the server.&lt;/p&gt;

&lt;p&gt;Let’s call her Mallory.&lt;/p&gt;

&lt;p&gt;The client thinks it is talking to the server.&lt;/p&gt;

&lt;p&gt;The server thinks it is talking to the client.&lt;/p&gt;

&lt;p&gt;But Mallory intercepts the handshake and replaces the exchanged public keys with her own.&lt;/p&gt;

&lt;p&gt;In simplified form, the flow looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiq3eu6p0rk0j2mynzq9z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiq3eu6p0rk0j2mynzq9z.png" alt="attack schema"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And now something very important happens.&lt;/p&gt;

&lt;p&gt;The handshake still “works.”&lt;/p&gt;

&lt;p&gt;But it works in the wrong way.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;strong&gt;client&lt;/strong&gt; ends up with a shared secret with &lt;strong&gt;Mallory&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;the &lt;strong&gt;server&lt;/strong&gt; ends up with a different shared secret with &lt;strong&gt;Mallory&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;and &lt;strong&gt;Mallory&lt;/strong&gt; now has one valid secure channel to each side&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;From the point of view of the client and the server, everything looks normal:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;key exchange succeeded&lt;/li&gt;
&lt;li&gt;keys were derived&lt;/li&gt;
&lt;li&gt;encrypted records verify correctly&lt;/li&gt;
&lt;li&gt;AEAD tags are valid&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And yet the protocol has already failed.&lt;/p&gt;

&lt;p&gt;Because Mallory can now:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;decrypt the client’s traffic&lt;/li&gt;
&lt;li&gt;read it or modify it&lt;/li&gt;
&lt;li&gt;re-encrypt it toward the server&lt;/li&gt;
&lt;li&gt;receive the server’s response&lt;/li&gt;
&lt;li&gt;read it or modify it&lt;/li&gt;
&lt;li&gt;re-encrypt it back toward the client&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Neither side can detect this.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;In The Next Part — Building the Certificate Infrastructure&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The handshake only proves one thing:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“I computed a shared secret with whoever sent me this public key.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It does &lt;strong&gt;not&lt;/strong&gt; prove:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“This public key came from the server I actually intended to talk to.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is the missing half.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;That is where the next layer enters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;certificates&lt;/li&gt;
&lt;li&gt;signatures&lt;/li&gt;
&lt;li&gt;trust chains&lt;/li&gt;
&lt;li&gt;certificate authorities&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other words, this is where the protocol must stop proving only that “someone” is there and start proving &lt;strong&gt;who&lt;/strong&gt; that someone is.&lt;/p&gt;

&lt;p&gt;That is exactly what the next article will build.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Our protocol now has secrecy against passive observers.&lt;/p&gt;

&lt;p&gt;It has integrity for protected records.&lt;/p&gt;

&lt;p&gt;It has fresh session keys.&lt;/p&gt;

&lt;p&gt;But it still does not have &lt;strong&gt;identity&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And without identity, a correct shared secret with the wrong party is still a protocol failure.&lt;/p&gt;

&lt;p&gt;That is the deeper lesson of Part 3.&lt;/p&gt;

&lt;p&gt;Part 1 taught us:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;confidentiality is not integrity&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Part 2 taught us:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;protecting records is not the same thing as establishing trust&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And now Part 3 adds the next lesson:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;key exchange is not authentication&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That we will solve in the next article!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Final Code&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The full code for this part is available here:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/DmytroHuzz/rebuilding_tls/tree/main/part_3" rel="noopener noreferrer"&gt;https://github.com/DmytroHuzz/rebuilding_tls/tree/main/part_3&lt;/a&gt;&lt;/p&gt;

</description>
      <category>learning</category>
      <category>development</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Rebuilding TLS, Part 2 — Adding Integrity to the Channel</title>
      <dc:creator>Dmytro Huz</dc:creator>
      <pubDate>Sun, 05 Apr 2026 21:42:10 +0000</pubDate>
      <link>https://dev.to/aws-builders/rebuilding-tls-part-2-adding-integrity-to-the-channel-4k78</link>
      <guid>https://dev.to/aws-builders/rebuilding-tls-part-2-adding-integrity-to-the-channel-4k78</guid>
      <description>&lt;p&gt;In the &lt;a href="https://www.dmytrohuz.com/p/rebuilding-tls-part-1-why-encryption" rel="noopener noreferrer"&gt;first part&lt;/a&gt; of this series, we built our first fake secure channel.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;That was real progress.&lt;/p&gt;

&lt;p&gt;But it still had a fatal flaw.&lt;/p&gt;

&lt;p&gt;The receiver had no way to know whether the encrypted message had been changed on the way.&lt;/p&gt;

&lt;p&gt;Encryption hid the bytes.&lt;/p&gt;

&lt;p&gt;It did not protect their meaning.&lt;/p&gt;

&lt;p&gt;So in this part, we will fix that.&lt;/p&gt;

&lt;p&gt;We will first add a &lt;strong&gt;MAC&lt;/strong&gt; 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 &lt;strong&gt;AEAD&lt;/strong&gt;, because that is how modern secure protocols usually protect records.&lt;/p&gt;

&lt;p&gt;We still will not have real TLS when we are done.&lt;/p&gt;

&lt;p&gt;But we will have a much more serious record layer than the one from Part 1.&lt;/p&gt;




&lt;h2&gt;
  
  
  What we will build in this part
&lt;/h2&gt;

&lt;p&gt;The plan for this article is simple:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;briefly introduce MACs&lt;/li&gt;
&lt;li&gt;add HMAC to our encrypted record format&lt;/li&gt;
&lt;li&gt;make tampering detectable&lt;/li&gt;
&lt;li&gt;add a sequence number to each record&lt;/li&gt;
&lt;li&gt;explain why sequence numbers matter&lt;/li&gt;
&lt;li&gt;then move from our hand-built “encrypt + MAC” construction to AEAD, because that is the approach real systems usually use&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just like in Part 1, I want to keep the pattern simple:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;explain the idea&lt;/li&gt;
&lt;li&gt;show the code&lt;/li&gt;
&lt;li&gt;explain what changed&lt;/li&gt;
&lt;li&gt;explain what is still broken&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why encryption still was not enough
&lt;/h2&gt;

&lt;p&gt;At the end of Part 1, our protocol already had one real property:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;confidentiality against passive observers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That mattered.&lt;/p&gt;

&lt;p&gt;But it still failed against active attackers.&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;confidentiality is not integrity&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So the next missing property is obvious.&lt;/p&gt;

&lt;p&gt;The receiver needs a way to verify that the message arrived unchanged.&lt;/p&gt;

&lt;p&gt;That is what a MAC gives us.&lt;/p&gt;




&lt;h2&gt;
  
  
  A very short note on MACs
&lt;/h2&gt;

&lt;p&gt;MAC stands for &lt;strong&gt;Message Authentication Code&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Very roughly, it is a cryptographic tag computed over a message using a secret key.&lt;/p&gt;

&lt;p&gt;The sender computes the tag and sends it together with the message.&lt;/p&gt;

&lt;p&gt;The receiver recomputes the tag and compares it with the one that was received.&lt;/p&gt;

&lt;p&gt;If the tags match, the receiver can trust that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the message was not modified&lt;/li&gt;
&lt;li&gt;and it was created by someone who knows the MAC key&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the tags do not match, the message must be rejected.&lt;/p&gt;

&lt;p&gt;In this article, we will use &lt;strong&gt;HMAC-SHA256&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;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’ll link that here: &lt;a href="https://www.dmytrohuz.com/p/building-own-mac-part-3-reinventing" rel="noopener noreferrer"&gt;https://www.dmytrohuz.com/p/building-own-mac-part-3-reinventing&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So for our purposes, the important idea is simple:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;encryption hides the message&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MAC protects the message from silent modification&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That is the missing half we need.&lt;/p&gt;




&lt;h2&gt;
  
  
  Adding HMAC to the channel
&lt;/h2&gt;

&lt;p&gt;Let’s start by upgrading the record format from Part 1.&lt;/p&gt;

&lt;p&gt;In Part 1, our protected payload was basically:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nonce || ciphertext
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we will add a MAC tag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nonce || ciphertext || tag
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the sender will compute the HMAC over:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nonce || ciphertext
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the full logic becomes:&lt;/p&gt;

&lt;h3&gt;
  
  
  Sender
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;encrypt plaintext with AES-CTR&lt;/li&gt;
&lt;li&gt;compute HMAC over &lt;code&gt;nonce || ciphertext&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;send &lt;code&gt;nonce || ciphertext || tag&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Receiver
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;read &lt;code&gt;nonce || ciphertext || tag&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;recompute HMAC over &lt;code&gt;nonce || ciphertext&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;compare tags&lt;/li&gt;
&lt;li&gt;only if they match, decrypt the ciphertext&lt;/li&gt;
&lt;li&gt;otherwise reject the message&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There is one more small improvement I want to make here.&lt;/p&gt;

&lt;p&gt;Instead of using one key for everything, we will already separate them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;one key for encryption&lt;/li&gt;
&lt;li&gt;one key for HMAC&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is still a toy setup, but it is better design than reusing the same bytes for every cryptographic job.&lt;/p&gt;

&lt;h3&gt;
  
  
  HMAC helpers
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;cryptography.hazmat.primitives.ciphers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;algorithms&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;modes&lt;/span&gt;

&lt;span class="c1"&gt;# ---------------------------------------------------------------------------
# Keys — 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.
# ---------------------------------------------------------------------------
&lt;/span&gt;
&lt;span class="c1"&gt;# 32-byte (256-bit) key for AES-256-CTR encryption.
&lt;/span&gt;&lt;span class="n"&gt;ENC_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0123456789ABCDEF0123456789ABCDEF&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;# 32-byte key for HMAC-SHA256.  Separate from the encryption key.
&lt;/span&gt;&lt;span class="n"&gt;MAC_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HMAC_KEY_FOR_PART2_DEMO_1234567&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="c1"&gt;# HMAC-SHA256 produces a 32-byte (256-bit) tag.
&lt;/span&gt;&lt;span class="n"&gt;TAG_LEN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;

&lt;span class="c1"&gt;# AES-CTR nonce is 16 bytes (128 bits).
&lt;/span&gt;&lt;span class="n"&gt;NONCE_LEN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;encrypt_then_mac&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Encrypt a plaintext and append an HMAC tag.

    Returns: nonce (16 B) || ciphertext (N B) || tag (32 B)
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 1: Generate a fresh random nonce for AES-CTR.
&lt;/span&gt;    &lt;span class="c1"&gt;# A new nonce MUST be used for every record — reusing a nonce with
&lt;/span&gt;    &lt;span class="c1"&gt;# the same key completely breaks CTR-mode security.
&lt;/span&gt;    &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urandom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NONCE_LEN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 2: Encrypt the plaintext with AES-256-CTR.
&lt;/span&gt;    &lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;algorithms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AES&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ENC_KEY&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;modes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CTR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;encryptor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encryptor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;encryptor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;encryptor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;finalize&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 3: Compute HMAC-SHA256 over (nonce || ciphertext).
&lt;/span&gt;    &lt;span class="c1"&gt;# New in Part 2: we authenticate the encrypted record before sending it.
&lt;/span&gt;    &lt;span class="c1"&gt;# The HMAC input includes the nonce so an attacker cannot swap nonces
&lt;/span&gt;    &lt;span class="c1"&gt;# between records without detection.
&lt;/span&gt;    &lt;span class="n"&gt;mac_input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;ciphertext&lt;/span&gt;
    &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MAC_KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mac_input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  [crypto_hmac] encrypt_then_mac:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;    nonce    = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hex&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;    ct_len   = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; bytes&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;    tag      = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;tag&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hex&lt;/span&gt;&lt;span class="p"&gt;()[&lt;/span&gt;&lt;span class="si"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;...&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 4: Assemble the wire format.
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;verify_then_decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;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.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 1: Parse the record into its components.
&lt;/span&gt;    &lt;span class="c1"&gt;# The tag is always the last 32 bytes.  The nonce is the first 16.
&lt;/span&gt;    &lt;span class="c1"&gt;# Everything in between is ciphertext.
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;NONCE_LEN&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;TAG_LEN&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Record too short to contain nonce + tag&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="n"&gt;NONCE_LEN&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;NONCE_LEN&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;TAG_LEN&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;received_tag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;TAG_LEN&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 2: Recompute the HMAC over (nonce || ciphertext).
&lt;/span&gt;    &lt;span class="n"&gt;mac_input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;ciphertext&lt;/span&gt;
    &lt;span class="n"&gt;expected_tag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MAC_KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mac_input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

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

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  [crypto_hmac] MAC verification: OK&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 4: Decrypt only after verification succeeds.
&lt;/span&gt;    &lt;span class="c1"&gt;# This is the key benefit of encrypt-then-MAC: we never process
&lt;/span&gt;    &lt;span class="c1"&gt;# unauthenticated ciphertext.
&lt;/span&gt;    &lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;algorithms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AES&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ENC_KEY&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;modes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CTR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;decryptor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decryptor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;plaintext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;decryptor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;decryptor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;finalize&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;plaintext&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the first big improvement over Part 1.&lt;/p&gt;

&lt;p&gt;The important change is not just that we added a tag.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;That is a very different protocol posture.&lt;/p&gt;




&lt;h2&gt;
  
  
  Updating the client and server
&lt;/h2&gt;

&lt;p&gt;Now let’s plug this into the channel.&lt;/p&gt;

&lt;h3&gt;
  
  
  HMAC-based client
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;framing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;recv_record&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;crypto_hmac&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;encrypt_then_mac&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;verify_then_decrypt&lt;/span&gt;

&lt;span class="n"&gt;HOST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9001&lt;/span&gt;

&lt;span class="c1"&gt;# A toy HTTP-like request — same spirit as Part 1.
&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET /transfer?to=bob&amp;amp;amount=100 HTTP/1.1&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="s"&gt;Host: localhost&lt;/span&gt;&lt;span class="se"&gt;\r\n\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Part 2 — HMAC Client (encrypt-then-MAC)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Connected to &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# ----- SEND REQUEST -----
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;--- Sending request ---&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;protected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;encrypt_then_mac&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;protected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  Record sent (&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;protected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; bytes on wire)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# ----- RECEIVE RESPONSE -----
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;--- Receiving response ---&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;raw_response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recv_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;verify_then_decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw_response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  Decrypted response:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Done.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  HMAC-based server
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;framing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;recv_record&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;crypto_hmac&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;encrypt_then_mac&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;verify_then_decrypt&lt;/span&gt;

&lt;span class="n"&gt;HOST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9001&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Part 2 — HMAC Server (encrypt-then-MAC)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# SO_REUSEADDR lets us restart the server immediately without waiting
&lt;/span&gt;    &lt;span class="c1"&gt;# for the OS to release the port from TIME_WAIT state.
&lt;/span&gt;    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setsockopt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOL_SOCKET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SO_REUSEADDR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Listening on &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Connected by &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# ----- RECEIVE REQUEST -----
&lt;/span&gt;        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;--- Receiving request ---&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;raw_request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recv_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;verify_then_decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;raw_request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# New in Part 2: if the MAC fails, we reject the record loudly.
&lt;/span&gt;            &lt;span class="c1"&gt;# In Part 1 we had no way to detect tampering at all.
&lt;/span&gt;            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  *** REJECTED: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; ***&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  Connection closed — refusing to process tampered data.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  Decrypted request:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="c1"&gt;# ----- SEND RESPONSE -----
&lt;/span&gt;            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;--- Sending response ---&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Content-Type: text/plain&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Content-Length: 13&lt;/span&gt;&lt;span class="se"&gt;\r\n\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello, client&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
            &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="n"&gt;protected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;encrypt_then_mac&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;protected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  Record sent (&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;protected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; bytes on wire)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Done.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The shape of the channel is still familiar.&lt;/p&gt;

&lt;p&gt;That matters.&lt;/p&gt;

&lt;p&gt;We did not replace the whole design.&lt;/p&gt;

&lt;p&gt;We strengthened one missing property.&lt;/p&gt;

&lt;p&gt;That is how protocol evolution should feel.&lt;/p&gt;




&lt;h4&gt;
  
  
  Let’s check it on the wire.
&lt;/h4&gt;

&lt;p&gt;We try to start the server and client, which we just created.&lt;/p&gt;

&lt;p&gt;Here is our client request’s data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="n"&gt;Sending&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;---&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;crypto_hmac&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;encrypt_then_mac&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;387&lt;/span&gt;&lt;span class="n"&gt;f0065f8915133473597d8cef15f34&lt;/span&gt;&lt;span class="bp"&gt;...&lt;/span&gt;
&lt;span class="n"&gt;ct_len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;61&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;
&lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b7f0db369e5d2a221a18f2d167b5b3a8&lt;/span&gt;&lt;span class="bp"&gt;...&lt;/span&gt;
&lt;span class="n"&gt;Record&lt;/span&gt; &lt;span class="nf"&gt;sent &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;109&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;wire&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsbymray8ejip4sgnfvha.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsbymray8ejip4sgnfvha.png" alt="wireshark"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Detecting tampering
&lt;/h2&gt;

&lt;p&gt;Now let’s revisit the failure from Part 1.&lt;/p&gt;

&lt;p&gt;Previously, if someone modified the ciphertext, the receiver would still decrypt it and accept modified plaintext.&lt;/p&gt;

&lt;p&gt;Now that should no longer work.&lt;/p&gt;

&lt;p&gt;Here is a tiny tampering demo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# tampering_demo_hmac.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;crypto_hmac&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;encrypt_then_mac&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;verify_then_decrypt&lt;/span&gt;

&lt;span class="n"&gt;original&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;amount=100&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;protected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;encrypt_then_mac&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;tampered&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;bytearray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;protected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;tampered&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;^=&lt;/span&gt; &lt;span class="mh"&gt;0x08&lt;/span&gt;  &lt;span class="c1"&gt;# flip one bit somewhere in the encrypted body
&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;verify_then_decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tampered&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Unexpected success:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Tampering detected:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the result should be rejection, not silent acceptance.&lt;/p&gt;

&lt;p&gt;That is exactly what we wanted.&lt;/p&gt;

&lt;p&gt;This is the moment where our channel stops being merely “encrypted” and starts being “protected.”&lt;/p&gt;

&lt;p&gt;Because now the receiver does not just recover bytes. It verifies them first.&lt;/p&gt;

&lt;p&gt;That is a serious step.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why we also need a sequence number
&lt;/h2&gt;

&lt;p&gt;At this point, we fixed the big flaw from Part 1: silent tampering.&lt;/p&gt;

&lt;p&gt;But the record layer is still naive.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because even with a valid HMAC, the receiver still has no sense of record position or freshness.&lt;/p&gt;

&lt;p&gt;Imagine an attacker records one valid protected message and sends it again later.&lt;/p&gt;

&lt;p&gt;The HMAC is still valid.&lt;/p&gt;

&lt;p&gt;The ciphertext is still valid.&lt;/p&gt;

&lt;p&gt;And unless the receiver keeps some state, it may accept the same record again.&lt;/p&gt;

&lt;p&gt;That means integrity alone is not the whole story.&lt;/p&gt;

&lt;p&gt;We also need some sense of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;order&lt;/li&gt;
&lt;li&gt;position&lt;/li&gt;
&lt;li&gt;repetition&lt;/li&gt;
&lt;li&gt;replay&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is where sequence numbers come in.&lt;/p&gt;

&lt;p&gt;A sequence number is just a counter that increases with every record:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;first record = 0&lt;/li&gt;
&lt;li&gt;next = 1&lt;/li&gt;
&lt;li&gt;next = 2&lt;/li&gt;
&lt;li&gt;and so on&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We then include that sequence number in the authenticated data, so the receiver does not just verify “these bytes were protected,” but also “these bytes belong in this position in the stream.”&lt;/p&gt;

&lt;p&gt;That makes the record layer much less naive.&lt;/p&gt;

&lt;p&gt;It still does not solve every replay problem in every possible system. But for our toy protocol, it is a very good next step.&lt;/p&gt;




&lt;h2&gt;
  
  
  Updating the record format
&lt;/h2&gt;

&lt;p&gt;Now our record becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;seq || nonce || ciphertext || tag
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And our HMAC input becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;seq || nonce || ciphertext
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the sender and receiver now both need a little bit of state:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the sender tracks the next sequence number to send&lt;/li&gt;
&lt;li&gt;the receiver tracks the next sequence number it expects&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is one of those moments where secure transport starts looking more like a real protocol and less like “some crypto around a socket.”&lt;/p&gt;

&lt;h3&gt;
  
  
  Sequence-aware HMAC helper
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# crypto_hmac_seq.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;cryptography.hazmat.primitives.ciphers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;algorithms&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;modes&lt;/span&gt;

&lt;span class="c1"&gt;# ---------------------------------------------------------------------------
# Keys — same as crypto_hmac.py, hardcoded for education.
# ---------------------------------------------------------------------------
&lt;/span&gt;&lt;span class="n"&gt;ENC_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0123456789ABCDEF0123456789ABCDEF&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;MAC_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HMAC_KEY_FOR_PART2_DEMO_1234567&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="n"&gt;TAG_LEN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;  &lt;span class="c1"&gt;# HMAC-SHA256 output: 32 bytes (256 bits)
&lt;/span&gt;&lt;span class="n"&gt;NONCE_LEN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;  &lt;span class="c1"&gt;# AES-CTR nonce: 16 bytes (128 bits)
&lt;/span&gt;&lt;span class="n"&gt;SEQ_LEN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;  &lt;span class="c1"&gt;# Sequence number: 8 bytes (64-bit unsigned integer)
&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;protect_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seq&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Encrypt a plaintext record and attach a sequence-aware HMAC tag.

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

    Returns:
        seq (8 B) || nonce (16 B) || ciphertext (N B) || tag (32 B)
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

    &lt;span class="c1"&gt;# Pack the sequence number as an 8-byte big-endian unsigned integer.
&lt;/span&gt;    &lt;span class="c1"&gt;# "!Q" = network byte order, unsigned 64-bit.
&lt;/span&gt;    &lt;span class="n"&gt;seq_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;!Q&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seq&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Generate a fresh AES-CTR nonce.
&lt;/span&gt;    &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urandom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NONCE_LEN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Encrypt the plaintext.
&lt;/span&gt;    &lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;algorithms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AES&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ENC_KEY&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;modes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CTR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;encryptor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encryptor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;encryptor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;encryptor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;finalize&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;# Compute HMAC over (seq || nonce || ciphertext).
&lt;/span&gt;    &lt;span class="c1"&gt;# The sequence number is included in the MAC input so the integrity
&lt;/span&gt;    &lt;span class="c1"&gt;# check also covers record order/position in the stream.
&lt;/span&gt;    &lt;span class="n"&gt;mac_input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;seq_bytes&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;ciphertext&lt;/span&gt;
    &lt;span class="n"&gt;tag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MAC_KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mac_input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;seq_bytes&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;tag&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;verify_and_unprotect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expected_seq&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;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.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

    &lt;span class="n"&gt;min_len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SEQ_LEN&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;NONCE_LEN&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;TAG_LEN&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;min_len&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Record too short&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 1: Parse the record.
&lt;/span&gt;    &lt;span class="n"&gt;seq_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="n"&gt;SEQ_LEN&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SEQ_LEN&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;SEQ_LEN&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;NONCE_LEN&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SEQ_LEN&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;NONCE_LEN&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;TAG_LEN&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;received_tag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;TAG_LEN&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 2: Recompute HMAC over (seq || nonce || ciphertext).
&lt;/span&gt;    &lt;span class="n"&gt;mac_input&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;seq_bytes&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;ciphertext&lt;/span&gt;
    &lt;span class="n"&gt;expected_tag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;MAC_KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mac_input&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hashlib&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sha256&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 3: Constant-time tag comparison.
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;hmac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;compare_digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;received_tag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expected_tag&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  [crypto_hmac_seq] *** MAC VERIFICATION FAILED ***&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HMAC verification failed — record tampered or replayed&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  [crypto_hmac_seq] MAC verification: OK&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  [crypto_hmac_seq] Sequence number: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;received_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; (expected &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;expected_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;) — OK&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 5: Decrypt.
&lt;/span&gt;    &lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;algorithms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AES&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ENC_KEY&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;modes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CTR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;decryptor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decryptor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;plaintext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;decryptor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;decryptor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;finalize&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;plaintext&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now the channel has a bit more memory.&lt;/p&gt;

&lt;p&gt;Not just “is this record authentic?”&lt;/p&gt;

&lt;p&gt;But also “is this the record I expected next?”&lt;/p&gt;

&lt;p&gt;That is a real protocol improvement.&lt;/p&gt;




&lt;h2&gt;
  
  
  Updating the client and server
&lt;/h2&gt;

&lt;p&gt;Now let’s plug this into the channel.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sequence-aware HMAC-based client
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# client_v2_hmac_seq.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;framing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;recv_record&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;crypto_hmac_seq&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;protect_record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;verify_and_unprotect&lt;/span&gt;

&lt;span class="n"&gt;HOST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9003&lt;/span&gt;

&lt;span class="c1"&gt;# Sequence counters — sender and receiver each maintain their own.
# The sender increments after each record sent.
# The receiver expects consecutive values starting from 0.
&lt;/span&gt;&lt;span class="n"&gt;send_seq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="n"&gt;recv_seq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="c1"&gt;# A toy HTTP-like request — same spirit as Part 1.
&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET /transfer?to=bob&amp;amp;amount=100 HTTP/1.1&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="s"&gt;Host: localhost&lt;/span&gt;&lt;span class="se"&gt;\r\n\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Part 2 — HMAC + Sequence Numbers Client (Stage 2)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Connected to &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# ----- SEND REQUEST -----
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;--- Sending request (send_seq=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;send_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;) ---&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;protected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;protect_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;send_seq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;protected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;send_seq&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  Record sent (&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;protected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; bytes on wire)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# ----- RECEIVE RESPONSE -----
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;--- Receiving response (expecting recv_seq=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;recv_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;) ---&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;raw_response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recv_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;verify_and_unprotect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;recv_seq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;raw_response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;recv_seq&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  Decrypted response:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  *** REJECTED: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; ***&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Done.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Sequence-aware HMAC-based server
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# server_v2_hmac_seq.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;framing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;recv_record&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;crypto_hmac_seq&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;protect_record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;verify_and_unprotect&lt;/span&gt;

&lt;span class="n"&gt;HOST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9003&lt;/span&gt;

&lt;span class="c1"&gt;# Sequence counters.
# The server's recv_seq tracks the client's send_seq, and vice versa.
&lt;/span&gt;&lt;span class="n"&gt;send_seq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="n"&gt;recv_seq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Part 2 — HMAC + Sequence Numbers Server (Stage 2)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setsockopt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOL_SOCKET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SO_REUSEADDR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Listening on &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Connected by &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# ----- RECEIVE REQUEST -----
&lt;/span&gt;        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;--- Receiving request (expecting recv_seq=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;recv_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;) ---&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;raw_request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recv_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;verify_and_unprotect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;recv_seq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;raw_request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;recv_seq&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;ValueError&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# Rejection: either the MAC is invalid, the sequence number
&lt;/span&gt;            &lt;span class="c1"&gt;# is wrong, or the data was tampered with / replayed.
&lt;/span&gt;            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  *** REJECTED: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; ***&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  Connection closed — refusing to process invalid data.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  Decrypted request:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="c1"&gt;# ----- SEND RESPONSE -----
&lt;/span&gt;            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;--- Sending response (send_seq=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;send_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;) ---&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Content-Type: text/plain&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Content-Length: 13&lt;/span&gt;&lt;span class="se"&gt;\r\n\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello, client&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
            &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="n"&gt;protected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;protect_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;send_seq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;protected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;send_seq&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  Record sent (&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;protected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; bytes on wire)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Done.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Why real-world systems usually do not stop here
&lt;/h2&gt;

&lt;p&gt;At this point, we have something much stronger than Part 1.&lt;/p&gt;

&lt;p&gt;We have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;encryption&lt;/li&gt;
&lt;li&gt;integrity protection&lt;/li&gt;
&lt;li&gt;message authentication&lt;/li&gt;
&lt;li&gt;sequence-aware records&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is already a meaningful protocol.&lt;/p&gt;

&lt;p&gt;But if you look at how real systems are usually built, they do not normally stop at manually composing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AES-CTR&lt;/li&gt;
&lt;li&gt;HMAC-SHA256&lt;/li&gt;
&lt;li&gt;explicit sequence-aware record protection&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because modern systems usually prefer a single primitive that gives confidentiality and integrity together.&lt;/p&gt;

&lt;p&gt;That is where &lt;strong&gt;AEAD&lt;/strong&gt; comes in.&lt;/p&gt;

&lt;p&gt;We separated these properties on purpose because it makes the protocol easier to understand.&lt;/p&gt;

&lt;p&gt;But the real world usually packages them together.&lt;/p&gt;




&lt;h2&gt;
  
  
  A very short note on AEAD
&lt;/h2&gt;

&lt;p&gt;AEAD stands for &lt;strong&gt;Authenticated Encryption with Associated Data&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That sounds heavier than it really is.&lt;/p&gt;

&lt;p&gt;The practical idea is simple:&lt;/p&gt;

&lt;p&gt;An AEAD construction gives us:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;encryption&lt;/li&gt;
&lt;li&gt;integrity/authentication of the encrypted message&lt;/li&gt;
&lt;li&gt;and the ability to authenticate extra metadata that should not be encrypted&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Common examples are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AES-GCM&lt;/li&gt;
&lt;li&gt;ChaCha20-Poly1305&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is much closer to how modern secure protocols protect records.&lt;/p&gt;

&lt;p&gt;It is also why I wanted to include AEAD in this part. If we stopped only at “encrypt + HMAC,” we would understand the missing property better, but we would still be one step away from how modern systems actually package it.&lt;/p&gt;

&lt;p&gt;So now we take that final step.&lt;/p&gt;




&lt;h2&gt;
  
  
  Moving our channel to AEAD
&lt;/h2&gt;

&lt;p&gt;For the AEAD version, I will use &lt;strong&gt;AES-GCM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The high-level idea is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the plaintext gets encrypted&lt;/li&gt;
&lt;li&gt;integrity/authentication is built in&lt;/li&gt;
&lt;li&gt;and we can include extra metadata as associated data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In our case, the sequence number is a good example of associated data.&lt;/p&gt;

&lt;p&gt;That means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it does not need to be encrypted&lt;/li&gt;
&lt;li&gt;but it should still be authenticated&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  AEAD-based helper
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# crypto_aead.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;cryptography.hazmat.primitives.ciphers.aead&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AESGCM&lt;/span&gt;

&lt;span class="c1"&gt;# ---------------------------------------------------------------------------
# Key — a single 256-bit key for AES-GCM.
# With AEAD, we do NOT need separate encryption and MAC keys — the
# algorithm handles both internally.
# ---------------------------------------------------------------------------
&lt;/span&gt;&lt;span class="n"&gt;AEAD_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;AEAD_KEY_PART2_DEMO_FOR_AES_GCM!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;  &lt;span class="c1"&gt;# 32 bytes → AES-256-GCM
&lt;/span&gt;
&lt;span class="c1"&gt;# AES-GCM nonce length: 12 bytes is the recommended (and most efficient) size.
&lt;/span&gt;&lt;span class="n"&gt;NONCE_LEN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;

&lt;span class="c1"&gt;# Sequence number: 8 bytes (64-bit unsigned integer), same as Stage 2.
&lt;/span&gt;&lt;span class="n"&gt;SEQ_LEN&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;protect_record_aead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seq&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;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)
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

    &lt;span class="c1"&gt;# Pack the sequence number as associated data.
&lt;/span&gt;    &lt;span class="c1"&gt;# The sequence number is authenticated but sent in the clear — the
&lt;/span&gt;    &lt;span class="c1"&gt;# receiver needs it to know which counter value to expect.
&lt;/span&gt;    &lt;span class="n"&gt;seq_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;!Q&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seq&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Generate a random 12-byte nonce for AES-GCM.
&lt;/span&gt;    &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urandom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;NONCE_LEN&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Create an AESGCM instance with our key.
&lt;/span&gt;    &lt;span class="n"&gt;aesgcm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AESGCM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AEAD_KEY&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Encrypt and authenticate in one call.
&lt;/span&gt;    &lt;span class="c1"&gt;# AESGCM.encrypt(nonce, data, associated_data) returns
&lt;/span&gt;    &lt;span class="c1"&gt;# ciphertext || 16-byte authentication tag as a single bytes object.
&lt;/span&gt;    &lt;span class="c1"&gt;# The associated_data (seq_bytes) is authenticated but NOT encrypted.
&lt;/span&gt;    &lt;span class="n"&gt;ciphertext_and_tag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;aesgcm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seq_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  [crypto_aead] protect_record_aead:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;    seq      = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;    nonce    = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hex&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;    sealed   = &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ciphertext_and_tag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; bytes &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;(plaintext &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; + tag 16)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;seq_bytes&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;ciphertext_and_tag&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;unprotect_record_aead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expected_seq&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;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.
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;

    &lt;span class="n"&gt;min_len&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SEQ_LEN&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;NONCE_LEN&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;  &lt;span class="c1"&gt;# at least seq + nonce + tag
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;min_len&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Record too short&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 1: Parse the record.
&lt;/span&gt;    &lt;span class="n"&gt;seq_bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="n"&gt;SEQ_LEN&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SEQ_LEN&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;SEQ_LEN&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;NONCE_LEN&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;ciphertext_and_tag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SEQ_LEN&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;NONCE_LEN&lt;/span&gt; &lt;span class="p"&gt;:]&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 2: Check the sequence number.
&lt;/span&gt;    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;received_seq&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unpack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;!Q&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seq_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;received_seq&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;expected_seq&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  [crypto_aead] *** SEQUENCE MISMATCH: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
            &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;got &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;received_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, expected &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;expected_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; ***&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Sequence number mismatch: got &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;received_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;, expected &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;expected_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  [crypto_aead] Sequence number: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;received_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;(expected &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;expected_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;) — OK&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# Step 3: Decrypt and verify in one call.
&lt;/span&gt;    &lt;span class="c1"&gt;# AESGCM.decrypt(nonce, data, associated_data) verifies the auth tag
&lt;/span&gt;    &lt;span class="c1"&gt;# and decrypts.  If anything was tampered with — the ciphertext, the
&lt;/span&gt;    &lt;span class="c1"&gt;# tag, or the associated data — it raises InvalidTag.
&lt;/span&gt;    &lt;span class="n"&gt;aesgcm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AESGCM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AEAD_KEY&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;plaintext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;aesgcm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ciphertext_and_tag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seq_bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  [crypto_aead] AEAD decryption: OK (&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; bytes)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;plaintext&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is noticeably simpler.&lt;/p&gt;

&lt;p&gt;That is one of the big practical advantages of AEAD.&lt;/p&gt;

&lt;p&gt;Instead of manually:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;encrypting&lt;/li&gt;
&lt;li&gt;computing HMAC&lt;/li&gt;
&lt;li&gt;verifying HMAC&lt;/li&gt;
&lt;li&gt;then decrypting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;we use one primitive that already combines confidentiality and integrity.&lt;/p&gt;

&lt;p&gt;And the sequence number fits naturally as associated data.&lt;/p&gt;

&lt;h3&gt;
  
  
  AEAD-based client
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# client_v2_aead.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;framing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;recv_record&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;crypto_aead&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;protect_record_aead&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unprotect_record_aead&lt;/span&gt;

&lt;span class="n"&gt;HOST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9002&lt;/span&gt;

&lt;span class="c1"&gt;# Sequence counters — sender and receiver each maintain their own.
# The sender increments after each record sent.
# The receiver expects consecutive values starting from 0.
&lt;/span&gt;&lt;span class="n"&gt;send_seq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="n"&gt;recv_seq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="c1"&gt;# A toy HTTP-like request.
&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET /transfer?to=bob&amp;amp;amount=100 HTTP/1.1&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="s"&gt;Host: localhost&lt;/span&gt;&lt;span class="se"&gt;\r\n\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Part 2 — AEAD Client (AES-GCM)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Connected to &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# ----- SEND REQUEST -----
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;--- Sending request (send_seq=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;send_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;) ---&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;protected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;protect_record_aead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;send_seq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;protected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;send_seq&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  Record sent (&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;protected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; bytes on wire)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# ----- RECEIVE RESPONSE -----
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;--- Receiving response (expecting recv_seq=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;recv_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;) ---&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;raw_response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recv_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;unprotect_record_aead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;recv_seq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;raw_response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;recv_seq&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  Decrypted response:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  *** REJECTED: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; ***&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Done.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  AEAD-based server
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# server_v2_aead.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;framing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;recv_record&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;crypto_aead&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;protect_record_aead&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unprotect_record_aead&lt;/span&gt;

&lt;span class="n"&gt;HOST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;9002&lt;/span&gt;

&lt;span class="c1"&gt;# Sequence counters.
# The server's recv_seq tracks the client's send_seq, and vice versa.
&lt;/span&gt;&lt;span class="n"&gt;send_seq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="n"&gt;recv_seq&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Part 2 — AEAD Server (AES-GCM)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setsockopt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOL_SOCKET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SO_REUSEADDR&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Listening on &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Connected by &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="c1"&gt;# ----- RECEIVE REQUEST -----
&lt;/span&gt;        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;--- Receiving request (expecting recv_seq=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;recv_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;) ---&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;raw_request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recv_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;unprotect_record_aead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;recv_seq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;raw_request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;recv_seq&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# AEAD rejection: either the auth tag is invalid, the sequence
&lt;/span&gt;            &lt;span class="c1"&gt;# number is wrong, or the data was tampered with.
&lt;/span&gt;            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  *** REJECTED: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; ***&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  Connection closed — refusing to process invalid data.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  Decrypted request:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;  &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="c1"&gt;# ----- SEND RESPONSE -----
&lt;/span&gt;            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;--- Sending response (send_seq=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;send_seq&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;) ---&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Content-Type: text/plain&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Content-Length: 13&lt;/span&gt;&lt;span class="se"&gt;\r\n\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
                &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello, client&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
            &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="n"&gt;protected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;protect_record_aead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;send_seq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nf"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;protected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;send_seq&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;  Record sent (&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;protected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; bytes on wire)&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;Done.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This version is already much closer to how modern secure transport actually protects records.&lt;/p&gt;

&lt;p&gt;Not identical to TLS, of course. But structurally much closer.&lt;/p&gt;




&lt;h2&gt;
  
  
  What we gained
&lt;/h2&gt;

&lt;p&gt;At this point, our channel is much stronger than the one from Part 1.&lt;/p&gt;

&lt;p&gt;We now have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;confidentiality&lt;/li&gt;
&lt;li&gt;integrity protection&lt;/li&gt;
&lt;li&gt;authenticated records&lt;/li&gt;
&lt;li&gt;sequence-aware message handling&lt;/li&gt;
&lt;li&gt;a much more realistic record protection design through AEAD&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is a big improvement.&lt;/p&gt;

&lt;p&gt;The receiver is no longer just decrypting whatever arrives and trusting the result. Now the receiver can reject modified or structurally unexpected records.&lt;/p&gt;

&lt;p&gt;That is a real protocol boundary.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is still broken
&lt;/h2&gt;

&lt;p&gt;And yet, even now, we are still very far from real TLS.&lt;/p&gt;

&lt;p&gt;Because the biggest assumption in our design is still untouched:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;both sides already share the necessary secret keys&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That means we still do not know how to solve the next real problem:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;how do two strangers establish fresh secrets?&lt;/li&gt;
&lt;li&gt;how does the client know it is talking to the right server?&lt;/li&gt;
&lt;li&gt;how do we scale beyond hardcoded shared secrets?&lt;/li&gt;
&lt;li&gt;how do we build trust instead of assuming it?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We improved record protection a lot.&lt;/p&gt;

&lt;p&gt;But we still do not have a real way to establish trust.&lt;/p&gt;

&lt;p&gt;That is the next wall.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this part, we took the encrypted but still incomplete channel from Part 1 and made it much more serious.&lt;/p&gt;

&lt;p&gt;First, we added &lt;strong&gt;HMAC&lt;/strong&gt;, which gave the receiver a way to detect tampering.&lt;/p&gt;

&lt;p&gt;Then, we added a &lt;strong&gt;sequence number&lt;/strong&gt;, which made the record layer less naive and bound records to their place in the stream.&lt;/p&gt;

&lt;p&gt;Finally, we moved to &lt;strong&gt;AEAD&lt;/strong&gt;, because in real-world systems confidentiality and integrity are usually protected together, not assembled manually from separate pieces.&lt;/p&gt;

&lt;p&gt;So this article had two goals:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;understand the missing property explicitly&lt;/li&gt;
&lt;li&gt;then move toward the real-world shape of the solution&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is why we did not stop at HMAC.&lt;/p&gt;

&lt;p&gt;But even after all of this, we still depend on one assumption that makes the whole thing unrealistic:&lt;/p&gt;

&lt;p&gt;we are still starting with pre-shared secret keys.&lt;/p&gt;

&lt;p&gt;And that is exactly what the next part will attack.&lt;/p&gt;




&lt;h2&gt;
  
  
  Next part — getting rid of the pre-shared key
&lt;/h2&gt;

&lt;p&gt;So we stop here.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;In the next part, we will stop assuming both sides already share a secret.&lt;/p&gt;

&lt;p&gt;We will start building a real handshake and establish fresh session keys instead.&lt;/p&gt;

&lt;p&gt;That still will not give us full TLS.&lt;/p&gt;

&lt;p&gt;But it will take us much closer to the real shape of the protocol.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final code
&lt;/h2&gt;

&lt;p&gt;I’ll put the full code for this part on GitHub here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/DmytroHuzz/rebuilding_tls/tree/main/part_2" rel="noopener noreferrer"&gt;&lt;strong&gt;[GitHub link to final code]&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>networking</category>
      <category>programming</category>
      <category>security</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Rebuilding TLS, Part 1 — Why Encryption Alone Is Not Enough</title>
      <dc:creator>Dmytro Huz</dc:creator>
      <pubDate>Sun, 29 Mar 2026 19:08:43 +0000</pubDate>
      <link>https://dev.to/aws-builders/rebuilding-tls-part-1-a-fake-secure-channel-p0n</link>
      <guid>https://dev.to/aws-builders/rebuilding-tls-part-1-a-fake-secure-channel-p0n</guid>
      <description>&lt;p&gt;A year ago I wrote a series about how a web server works.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Wrap it in TLS. Make the communication secure.&lt;/p&gt;

&lt;p&gt;It did not stay small for long.&lt;/p&gt;

&lt;p&gt;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: &lt;strong&gt;secure connection&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;In this first part, we will start with the most naive version of the problem.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;That will give us our first fake secure channel.&lt;/p&gt;

&lt;p&gt;And that is exactly where we should start.&lt;/p&gt;




&lt;h2&gt;
  
  
  What we will build in this part
&lt;/h2&gt;

&lt;p&gt;The plan for this article is simple:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;build a tiny socket-based client and server&lt;/li&gt;
&lt;li&gt;send plain text between them&lt;/li&gt;
&lt;li&gt;look at the traffic and see that everything is visible&lt;/li&gt;
&lt;li&gt;add shared-key encryption with AES-CTR&lt;/li&gt;
&lt;li&gt;make the traffic unreadable&lt;/li&gt;
&lt;li&gt;then show why encryption alone still does not give us a trustworthy secure protocol&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We are not trying to build real TLS yet.&lt;/p&gt;

&lt;p&gt;We are trying to make the first mistake on purpose.&lt;/p&gt;

&lt;p&gt;Because once that mistake becomes visible, the next piece of the protocol stops looking optional.&lt;/p&gt;




&lt;h2&gt;
  
  
  TLS is not SSL
&lt;/h2&gt;

&lt;p&gt;Before we start, one small clarification.&lt;/p&gt;

&lt;p&gt;People still often say “SSL” when they talk about secure communication on the web. But SSL is the older family of protocols. TLS is its successor.&lt;/p&gt;

&lt;p&gt;So when people say things like “SSL certificate” or “SSL connection,” in practice they usually mean TLS.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;And instead of starting from the finished protocol, we will begin one layer lower — with plain socket communication.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1 — A plain socket-based communication channel
&lt;/h2&gt;

&lt;p&gt;Let’s start with the smallest possible thing: a tiny TCP server and a tiny TCP client.&lt;/p&gt;

&lt;p&gt;The client will send an HTTP-like request.&lt;/p&gt;

&lt;p&gt;The server will read it and return an HTTP-like response.&lt;/p&gt;

&lt;p&gt;Nothing secure yet. Just raw bytes moving over a socket.&lt;/p&gt;

&lt;h3&gt;
  
  
  Plain server
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# server_plain.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;

&lt;span class="n"&gt;HOST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8081&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Listening on &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Connected by &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;recv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Received request:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Content-Type: text/plain&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Content-Length: 13&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello, client&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Plain client
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# client_plain.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;

&lt;span class="n"&gt;HOST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8081&lt;/span&gt;

&lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET /transfer?to=bob&amp;amp;amount=100 HTTP/1.1&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Host: localhost&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;recv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4096&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Received response:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is intentionally tiny.&lt;/p&gt;

&lt;p&gt;The client sends a request like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="nf"&gt;GET&lt;/span&gt; &lt;span class="nn"&gt;/transfer?to=bob&amp;amp;amount=100&lt;/span&gt; &lt;span class="k"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt;
&lt;span class="na"&gt;Host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;localhost&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server reads it and sends a response back.&lt;/p&gt;

&lt;p&gt;That is all.&lt;/p&gt;

&lt;p&gt;And because it is all plain TCP, anyone who can observe the traffic can read it directly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Looking at the traffic
&lt;/h3&gt;

&lt;p&gt;If you capture this communication in Wireshark, the request and response are fully visible in clear text.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff525x9z5e4p3qalg8mj4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff525x9z5e4p3qalg8mj4.png" alt="plain"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That is our baseline.&lt;/p&gt;

&lt;p&gt;The client can read it.&lt;/p&gt;

&lt;p&gt;The server can read it.&lt;/p&gt;

&lt;p&gt;And anyone on the wire can read it too.&lt;/p&gt;

&lt;p&gt;So the first obvious idea is also the first naive one:&lt;/p&gt;

&lt;p&gt;If the problem is that everyone can read the bytes, let’s encrypt the bytes.&lt;/p&gt;

&lt;p&gt;That sounds reasonable.&lt;/p&gt;

&lt;p&gt;And it is still not enough.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 2 — Turning bytes into records
&lt;/h2&gt;

&lt;p&gt;Before we add encryption, we need one small but important thing: structure.&lt;/p&gt;

&lt;p&gt;TCP gives us a byte stream.&lt;/p&gt;

&lt;p&gt;It does not give us message boundaries.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;That means even before security, we need a little bit of protocol design.&lt;/p&gt;

&lt;p&gt;Let’s define the smallest possible record format:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;4 bytes: payload length&lt;/li&gt;
&lt;li&gt;N bytes: payload
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;
+----------+-----------+
|  length  |  payload  |
| (4 bytes)|  (varies) |
+----------+-----------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is enough for our first version.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# framing.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;header&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;!I&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendall&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;header&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;recv_exact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;chunks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;chunk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;recv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;remaining&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ConnectionError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Connection closed while reading data&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;""&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;recv_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;header&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recv_exact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unpack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;!I&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;header&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;recv_exact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sock&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is not a crypto step.&lt;/p&gt;

&lt;p&gt;It is a protocol step.&lt;/p&gt;

&lt;p&gt;And that distinction matters more than it first appears. A secure channel is not just “call encrypt on a string.” It is a protocol with structure, state, and rules.&lt;/p&gt;

&lt;p&gt;Now that we have a way to send and receive well-defined records, we can finally wrap them in encryption.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 3 — Wrapping the channel in shared-key encryption
&lt;/h2&gt;

&lt;p&gt;The most obvious first attempt at secure communication is usually this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;both sides already know the same secret key&lt;/li&gt;
&lt;li&gt;the sender encrypts the message before sending&lt;/li&gt;
&lt;li&gt;the receiver decrypts it after receiving&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is exactly what we will do.&lt;/p&gt;

&lt;p&gt;No handshake yet.&lt;/p&gt;

&lt;p&gt;No certificates yet.&lt;/p&gt;

&lt;p&gt;No integrity yet.&lt;/p&gt;

&lt;p&gt;No authentication yet.&lt;/p&gt;

&lt;p&gt;This version is intentionally naive.&lt;/p&gt;

&lt;p&gt;For encryption, I will use AES in CTR mode.&lt;/p&gt;

&lt;p&gt;Very briefly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;AES is a symmetric block cipher&lt;/li&gt;
&lt;li&gt;CTR mode makes it convenient for encrypting a stream of bytes&lt;/li&gt;
&lt;li&gt;it gives us confidentiality&lt;/li&gt;
&lt;li&gt;but it does &lt;strong&gt;not&lt;/strong&gt; give us integrity&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That last point is the important one for this article.&lt;/p&gt;

&lt;p&gt;If you want a deeper explanation of AES itself, I already wrote about it in my cryptography series: &lt;a href="https://www.dmytrohuz.com/p/building-own-block-cipher-part-3" rel="noopener noreferrer"&gt;https://www.dmytrohuz.com/p/building-own-block-cipher-part-3&lt;/a&gt;, so I will not go into the internals here.&lt;/p&gt;

&lt;h3&gt;
  
  
  The nonce
&lt;/h3&gt;

&lt;p&gt;CTR mode also needs a nonce.&lt;/p&gt;

&lt;p&gt;For now, think of it as a fresh per-message value that must be different for each encryption under the same key.&lt;/p&gt;

&lt;p&gt;It is not secret.&lt;/p&gt;

&lt;p&gt;It just must not be reused.&lt;/p&gt;

&lt;p&gt;So our encrypted payload will look like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;nonce&lt;/li&gt;
&lt;li&gt;ciphertext&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Crypto helper
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# crypto.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;cryptography.hazmat.primitives.ciphers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;algorithms&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;modes&lt;/span&gt;

&lt;span class="c1"&gt;# 32-byte shared key for AES-256
&lt;/span&gt;&lt;span class="n"&gt;SHARED_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;0123456789ABCDEF0123456789ABCDEF&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;encrypt_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urandom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;algorithms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AES&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SHARED_KEY&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;modes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CTR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;encryptor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encryptor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;encryptor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;encryptor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;finalize&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="c1"&gt;# Encrypted payload format:
&lt;/span&gt;    &lt;span class="c1"&gt;# nonce || ciphertext
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;ciphertext&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;decrypt_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;

    &lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;algorithms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AES&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SHARED_KEY&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;modes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CTR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;decryptor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decryptor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;plaintext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;decryptor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;decryptor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;finalize&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;plaintext&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, our wire format becomes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;4-byte length&lt;/li&gt;
&lt;li&gt;16-byte nonce&lt;/li&gt;
&lt;li&gt;ciphertext
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+----------------+----------------+---------------------+
| length (4 B)   | nonce (16 B)   | ciphertext (N bytes)|
+----------------+----------------+---------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That already looks much more like a protocol.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 4 — Encrypt the request and response
&lt;/h2&gt;

&lt;p&gt;Now let’s integrate this into the client and server.&lt;/p&gt;

&lt;h3&gt;
  
  
  Encrypted client
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# client_v1.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;framing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;recv_record&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;crypto&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;encrypt_message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decrypt_message&lt;/span&gt;

&lt;span class="n"&gt;HOST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8081&lt;/span&gt;

&lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;GET /transfer?to=bob&amp;amp;amount=100 HTTP/1.1&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Host: localhost&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="n"&gt;encrypted_request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;encrypt_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encrypted_request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;encrypted_response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recv_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;decrypt_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encrypted_response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Received decrypted response:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Encrypted server
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# server_v1.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;framing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;recv_record&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;crypto&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;encrypt_message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decrypt_message&lt;/span&gt;

&lt;span class="n"&gt;HOST&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;127.0.0.1&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;PORT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8081&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AF_INET&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;socket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SOCK_STREAM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Listening on &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;HOST&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;:&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;PORT&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;addr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Connected by &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;encrypted_request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;recv_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;decrypt_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encrypted_request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Received decrypted request:&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

        &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;HTTP/1.1 200 OK&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="s"&gt;Content-Type: text/plain&lt;/span&gt;&lt;span class="se"&gt;\r\n&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
            &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Content-Length: 13&lt;/span&gt;&lt;span class="se"&gt;\r\n\r\n&lt;/span&gt;&lt;span class="s"&gt;hello, client&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
        &lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;encrypted_response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;encrypt_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;send_record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;conn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encrypted_response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the communication flow changes in an important way.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  What changes on the wire
&lt;/h3&gt;

&lt;p&gt;If you capture this version in Wireshark, the traffic is no longer readable.&lt;/p&gt;

&lt;p&gt;Instead of clear request and response text, you now see opaque binary data.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk0kiccihimtlkuu1sdh2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk0kiccihimtlkuu1sdh2.png" alt="encr"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So yes, we gained something real.&lt;/p&gt;

&lt;p&gt;Let’s stop and say exactly what that is.&lt;/p&gt;




&lt;h2&gt;
  
  
  What encryption actually gave us
&lt;/h2&gt;

&lt;p&gt;This first version gives us one meaningful property:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;confidentiality against passive observers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If someone can only observe the traffic, but cannot modify it, they no longer get the plaintext request and response for free.&lt;/p&gt;

&lt;p&gt;That is already better than raw TCP.&lt;/p&gt;

&lt;p&gt;And this is why “just add encryption” feels so convincing. It visibly solves a real problem.&lt;/p&gt;

&lt;p&gt;But that visible success can hide another, more dangerous failure.&lt;/p&gt;

&lt;p&gt;Because a secure channel needs more than secrecy.&lt;/p&gt;

&lt;p&gt;It also needs protection against tampering.&lt;/p&gt;

&lt;p&gt;And we still do not have that.&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 5 — Why encryption alone is not enough
&lt;/h2&gt;

&lt;p&gt;This is the real point of Part 1.&lt;/p&gt;

&lt;p&gt;We encrypted the messages.&lt;/p&gt;

&lt;p&gt;We did &lt;strong&gt;not&lt;/strong&gt; make them trustworthy.&lt;/p&gt;

&lt;p&gt;AES-CTR protects confidentiality, but it does not protect integrity.&lt;/p&gt;

&lt;p&gt;That means an active attacker may be able to modify ciphertext, and those modifications will flow through into the decrypted plaintext.&lt;/p&gt;

&lt;p&gt;Very roughly, CTR mode behaves like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ciphertext = plaintext XOR keystream
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So if an attacker changes bits in the ciphertext, the corresponding bits change in the plaintext after decryption.&lt;/p&gt;

&lt;p&gt;That property is called &lt;strong&gt;malleability&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And protocol messages are usually predictable enough that this becomes useful to an attacker.&lt;/p&gt;

&lt;p&gt;Our example request already has a very predictable structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="nf"&gt;GET&lt;/span&gt; &lt;span class="nn"&gt;/transfer?to=bob&amp;amp;amount=100&lt;/span&gt; &lt;span class="k"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="m"&gt;1.1&lt;/span&gt;
&lt;span class="na"&gt;Host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;localhost&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact bytes of &lt;code&gt;amount=100&lt;/code&gt; are not random.&lt;/p&gt;

&lt;p&gt;That predictability is enough to hurt us.&lt;/p&gt;

&lt;h3&gt;
  
  
  A tiny isolated demo
&lt;/h3&gt;

&lt;p&gt;We do not need a full man-in-the-middle proxy to show the problem. A small isolated example is enough.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# ctr_malleability_demo.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;crypto&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;encrypt_message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decrypt_message&lt;/span&gt;

&lt;span class="n"&gt;original&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;amount=100&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;encrypted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;encrypt_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;encrypted&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;bytearray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encrypted&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;:])&lt;/span&gt;

&lt;span class="c1"&gt;# Change '1' -&amp;gt; '9'
# ASCII '1' = 0x31
# ASCII '9' = 0x39
# Difference = 0x08
&lt;/span&gt;
&lt;span class="n"&gt;index_of_digit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;amount=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;index_of_digit&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;^=&lt;/span&gt; &lt;span class="mh"&gt;0x08&lt;/span&gt;

&lt;span class="n"&gt;modified&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;decrypted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;decrypt_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;modified&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Original :&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;original&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Modified :&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decrypted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Original : b'amount=100'
Modified : b'amount=900'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that is the failure.&lt;/p&gt;

&lt;p&gt;The attacker did not need the key.&lt;/p&gt;

&lt;p&gt;They did not need to fully decrypt the message first.&lt;/p&gt;

&lt;p&gt;They only needed the ability to modify the encrypted bytes in transit.&lt;/p&gt;

&lt;p&gt;The receiver then decrypts the modified ciphertext and gets modified plaintext — without any built-in indication that anything went wrong.&lt;/p&gt;

&lt;p&gt;So even though the message is hidden from passive observers, it is still vulnerable to active tampering.&lt;/p&gt;

&lt;p&gt;That is not a secure protocol.&lt;/p&gt;

&lt;p&gt;That is only encrypted transport.&lt;/p&gt;




&lt;h2&gt;
  
  
  What is still broken
&lt;/h2&gt;

&lt;p&gt;At this point, our fake secure channel still has many serious holes.&lt;/p&gt;

&lt;h3&gt;
  
  
  No integrity protection
&lt;/h3&gt;

&lt;p&gt;The receiver cannot detect that the ciphertext was modified.&lt;/p&gt;

&lt;h3&gt;
  
  
  No message authentication
&lt;/h3&gt;

&lt;p&gt;The receiver has no cryptographic proof that the message came from the expected sender and arrived unchanged.&lt;/p&gt;

&lt;h3&gt;
  
  
  No replay protection
&lt;/h3&gt;

&lt;p&gt;An attacker can capture an encrypted message and replay it later.&lt;/p&gt;

&lt;h3&gt;
  
  
  Static shared key
&lt;/h3&gt;

&lt;p&gt;Both sides use one long-term shared key for everything.&lt;/p&gt;

&lt;p&gt;That does not scale, and if it leaks, everything built on top of it collapses.&lt;/p&gt;

&lt;h3&gt;
  
  
  No handshake
&lt;/h3&gt;

&lt;p&gt;There is no fresh session establishment. The peers do not negotiate anything. They just start encrypting.&lt;/p&gt;

&lt;h3&gt;
  
  
  No peer identity
&lt;/h3&gt;

&lt;p&gt;The client does not really know who it is talking to beyond “someone who can decrypt with this key.”&lt;/p&gt;

&lt;p&gt;So yes, we improved something.&lt;/p&gt;

&lt;p&gt;But we are still very far from TLS.&lt;/p&gt;

&lt;p&gt;Good.&lt;/p&gt;

&lt;p&gt;That is exactly what Part 1 should make visible.&lt;/p&gt;




&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this first part, we built a fake secure channel.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;That was real progress.&lt;/p&gt;

&lt;p&gt;But it was not enough.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;So the first lesson of this series is simple:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;confidentiality is not integrity&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;And if we want something that starts to deserve the name secure protocol, we need both.&lt;/p&gt;




&lt;h2&gt;
  
  
  Next part — adding integrity with a MAC
&lt;/h2&gt;

&lt;p&gt;So we stop here.&lt;/p&gt;

&lt;p&gt;We now have a channel that can hide bytes from passive observers, but cannot reliably detect tampering.&lt;/p&gt;

&lt;p&gt;In the next part, we will keep the same basic setup and fix the biggest hole we exposed here: we will add a &lt;strong&gt;MAC — a Message Authentication Code&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That will take us from:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“you probably can’t read this”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;to:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“you also can’t silently change this”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That still will not be real TLS.&lt;/p&gt;

&lt;p&gt;But it will make our fake secure channel one step less fake.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final code
&lt;/h2&gt;

&lt;p&gt;I’ll put the full code for this part on GitHub here:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/DmytroHuzz/rebuilding_tls/tree/main/part_1" rel="noopener noreferrer"&gt;https://github.com/DmytroHuzz/rebuilding_tls/tree/main/part_1&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>A Practical Guide to Time for Developers: Part 4 -The Linux Time Sync Cheat Sheet</title>
      <dc:creator>Dmytro Huz</dc:creator>
      <pubDate>Thu, 19 Mar 2026 16:42:58 +0000</pubDate>
      <link>https://dev.to/dmytro_huz/a-practical-guide-to-time-for-developers-part-4-the-linux-time-sync-cheat-sheet-5np</link>
      <guid>https://dev.to/dmytro_huz/a-practical-guide-to-time-for-developers-part-4-the-linux-time-sync-cheat-sheet-5np</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;You know what time means in computing, how a machine keeps it, why clocks drift, how synchronization works, and why timestamp location matters. That is already more than most people ever learn about this topic.&lt;/p&gt;

&lt;p&gt;Now let’s compress all of that into the Linux view of the world.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Think of this as the practical cheat sheet — 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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;This is probably the part you will want to bookmark.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The three main clock entities in Linux&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When people say “Linux time,” they often mean one thing. In reality, Linux commonly deals with at least three different clock entities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;system time&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;RTC&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PHC&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They serve different purposes.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;1. System time&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This is the normal wall-clock time used by most applications.&lt;/p&gt;

&lt;p&gt;It is what you usually see when you run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;date&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;timedatectl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conceptually, this is the kernel’s main wall clock, commonly associated with CLOCK_REALTIME.&lt;/p&gt;

&lt;p&gt;This is the clock used by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;most user-space applications&lt;/li&gt;
&lt;li&gt;logs&lt;/li&gt;
&lt;li&gt;system services&lt;/li&gt;
&lt;li&gt;everyday time queries&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Quick check&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;date
&lt;/span&gt;timedatectl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Mental model&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;System clock = the main OS wall clock
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;2. RTC (Real-Time Clock)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The RTC is the battery-backed hardware clock on the motherboard.&lt;/p&gt;

&lt;p&gt;Its main job is simple: keep time while the machine is powered off.&lt;/p&gt;

&lt;p&gt;Linux often uses it during boot to initialize system time, and may update it again later from system time. But the RTC is usually &lt;strong&gt;not&lt;/strong&gt; the main precision synchronization clock during normal operation.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Quick check&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;hwclock &lt;span class="nt"&gt;--show&lt;/span&gt;
timedatectl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Common operations&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Copy system time to RTC:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;hwclock &lt;span class="nt"&gt;--systohc&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy RTC to system time:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;hwclock &lt;span class="nt"&gt;--hctosys&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Mental model&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RTC = persistent clock for boot/shutdown
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;3. PHC (PTP Hardware Clock)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A PHC is a hardware clock exposed by a PTP-capable network interface.&lt;/p&gt;

&lt;p&gt;This is where Linux gets especially interesting.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;PHCs usually appear as device files like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;/dev/ptp0
/dev/ptp1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Quick check&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;List PHC devices:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt; /dev/ptp&lt;span class="k"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check which PHC belongs to a NIC and whether hardware timestamping is supported:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ethtool &lt;span class="nt"&gt;-T&lt;/span&gt; eth0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Mental model&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PHC = hardware clock on the NIC, used for precise packet timing
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;One picture: how they relate&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A rough summary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RTC&lt;/strong&gt; keeps time while power is off&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;system clock&lt;/strong&gt; is what most software reads&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PHC&lt;/strong&gt; is the precision clock on the NIC&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;How Linux syncs time with NTP&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In the NTP world, Linux usually synchronizes the &lt;strong&gt;system clock&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Common tools:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;chronyd&lt;/li&gt;
&lt;li&gt;systemd-timesyncd&lt;/li&gt;
&lt;li&gt;older setups may use ntpd&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Check whether NTP is active&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;timedatectl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;If you use chrony&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Show synchronization state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;chronyc tracking
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Show time sources:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;chronyc sources &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Mental model&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NTP servers
   |
   v
chronyd / timesyncd / ntpd
   |
   v
System clock
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In other words, NTP usually targets the &lt;strong&gt;system clock&lt;/strong&gt;, not the PHC.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;How Linux syncs time with PTP&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In the PTP world, Linux often follows a two-step path:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;synchronize the &lt;strong&gt;PHC&lt;/strong&gt; to the PTP network&lt;/li&gt;
&lt;li&gt;synchronize the &lt;strong&gt;system clock&lt;/strong&gt; to that PHC&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The two main tools are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ptp4l&lt;/li&gt;
&lt;li&gt;phc2sys&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;ptp4l: syncing the NIC-side clock&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;ptp4l speaks PTP on the network and usually synchronizes the PHC associated with the interface.&lt;/p&gt;

&lt;p&gt;Typical example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;ptp4l &lt;span class="nt"&gt;-i&lt;/span&gt; eth0 &lt;span class="nt"&gt;-m&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Meaning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;i eth0 → use interface eth0&lt;/li&gt;
&lt;li&gt;m → print log messages to stdout&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Mental model&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PTP Grandmaster / network
        |
        v
      ptp4l
        |
        v
PHC on eth0 (/dev/ptpX)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is usually where the NIC-side precision timing gets aligned to the network timing domain.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;phc2sys: syncing one local clock to another&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Once the PHC is synchronized, the rest of the machine may still be reading the system clock.&lt;/p&gt;

&lt;p&gt;That is why phc2sys exists.&lt;/p&gt;

&lt;p&gt;Its job is to synchronize one local clock to another.&lt;/p&gt;

&lt;p&gt;The most common use is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PHC → system clock&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;phc2sys &lt;span class="nt"&gt;-s&lt;/span&gt; eth0 &lt;span class="nt"&gt;-c&lt;/span&gt; CLOCK_REALTIME &lt;span class="nt"&gt;-m&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Meaning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;s eth0 → source is the PHC associated with eth0&lt;/li&gt;
&lt;li&gt;c CLOCK_REALTIME → target is the system clock&lt;/li&gt;
&lt;li&gt;m → print status&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Mental model&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PHC on NIC
   |
   v
phc2sys
   |
   v
System clock
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the classic Linux PTP flow.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The classic Linux PTP pipeline&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one of the most useful diagrams to keep in your head.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;PHC to system clock, or system clock to PHC?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In precision setups, the common direction is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PHC → system clock
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;because the PHC is closer to the wire and usually the better timing source in a PTP environment.&lt;/p&gt;

&lt;p&gt;That is why this is a common command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;phc2sys &lt;span class="nt"&gt;-s&lt;/span&gt; eth0 &lt;span class="nt"&gt;-c&lt;/span&gt; CLOCK_REALTIME &lt;span class="nt"&gt;-m&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But phc2sys is more general than that. It can synchronize clocks in other directions too.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Syncing one PHC to another&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Linux can also synchronize different hardware clocks to each other.&lt;/p&gt;

&lt;p&gt;Conceptually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;PHC A → PHC B
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;phc2sys &lt;span class="nt"&gt;-s&lt;/span&gt; /dev/ptp0 &lt;span class="nt"&gt;-c&lt;/span&gt; /dev/ptp1 &lt;span class="nt"&gt;-m&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is less common than PHC-to-system-clock sync, but it is useful when one interface or subsystem should follow another local hardware clock.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Where software timestamping fits&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Not every NIC has hardware timestamping. Not every system needs that level of precision.&lt;/p&gt;

&lt;p&gt;Linux can still synchronize clocks using software timestamps, and for many use cases that is completely fine.&lt;/p&gt;

&lt;p&gt;But the practical tradeoff remains the same:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;hardware timestamping&lt;/strong&gt; gives measurements closer to the real wire event&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;software timestamping&lt;/strong&gt; includes more delay and variation from the OS path&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is why software timestamping usually belongs to a looser precision budget, while hardware timestamping is what unlocks much tighter synchronization.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The most useful commands at a glance&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Check system time&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;date
&lt;/span&gt;timedatectl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Check RTC&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;hwclock &lt;span class="nt"&gt;--show&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;List PHC devices&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt; /dev/ptp&lt;span class="k"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Check NIC timestamping support&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ethtool &lt;span class="nt"&gt;-T&lt;/span&gt; eth0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Run PTP on an interface&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;ptp4l &lt;span class="nt"&gt;-i&lt;/span&gt; eth0 &lt;span class="nt"&gt;-m&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Sync system clock from PHC&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;phc2sys &lt;span class="nt"&gt;-s&lt;/span&gt; eth0 &lt;span class="nt"&gt;-c&lt;/span&gt; CLOCK_REALTIME &lt;span class="nt"&gt;-m&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Check chrony state&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;chronyc tracking
chronyc sources &lt;span class="nt"&gt;-v&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;The one table worth remembering&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RTC ------------------&amp;gt; system time at boot / shutdown
NTP daemon -----------&amp;gt; system clock
ptp4l ----------------&amp;gt; PHC
phc2sys --------------&amp;gt; PHC &amp;lt;-&amp;gt; system clock
hwclock --systohc ----&amp;gt; system clock -&amp;gt; RTC
hwclock --hctosys ----&amp;gt; RTC -&amp;gt; system clock
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;The biggest practical lesson&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When somebody says:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;“The machine is synchronized.”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That is usually too vague.&lt;/p&gt;

&lt;p&gt;The useful follow-up question is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Which clock is synchronized?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the RTC?&lt;/li&gt;
&lt;li&gt;the system clock?&lt;/li&gt;
&lt;li&gt;the PHC?&lt;/li&gt;
&lt;li&gt;and which one is the application actually using?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That one question prevents a lot of confusion.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;One-screen summary&lt;/strong&gt;
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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 -&amp;gt; ptp4l -&amp;gt; PHC -&amp;gt; phc2sys -&amp;gt; system clock
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;Final note&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you made it all the way here, you did not just read a few articles about clocks.&lt;/p&gt;

&lt;p&gt;You built a real mental model of time in computing — 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.&lt;/p&gt;

&lt;p&gt;That already puts you far ahead of most engineers who touch these systems.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;And it was the final piece: a practical cheat sheet you can actually use. Bookmark it. Return to it. Break things with it. Fix things with it.&lt;/p&gt;




</description>
      <category>cli</category>
      <category>devops</category>
      <category>linux</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>A Practical Guide to Time for Developers: Part 3 — How Computers Share Time</title>
      <dc:creator>Dmytro Huz</dc:creator>
      <pubDate>Mon, 16 Mar 2026 16:00:00 +0000</pubDate>
      <link>https://dev.to/dmytro_huz/a-practical-guide-to-time-for-developers-part-3-how-computers-share-time-16d4</link>
      <guid>https://dev.to/dmytro_huz/a-practical-guide-to-time-for-developers-part-3-how-computers-share-time-16d4</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;Every action film has that scene just before the military operation begins.&lt;/p&gt;

&lt;p&gt;“Let’s sync our watches,” the captain says.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 (&lt;a href="https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers" rel="noopener noreferrer"&gt;https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers&lt;/a&gt;), we explored the foundations of time itself. In the second (&lt;a href="https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-2ec" rel="noopener noreferrer"&gt;https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers-2ec&lt;/a&gt;), 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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Let’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.&lt;/p&gt;

&lt;p&gt;How can we make that happen?&lt;/p&gt;

&lt;p&gt;The obvious answer is: the turbines need synchronized clocks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fang0fw1quxulsmcj6t1h.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fang0fw1quxulsmcj6t1h.jpg" alt="Isync" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But how we can keep them in sync for hundreds and thousands devices with amazing accuracy?&lt;br&gt;
Let’s see! &lt;/p&gt;

&lt;h2&gt;
  
  
  Just sync the clocks once?
&lt;/h2&gt;

&lt;p&gt;Let’s start with the most obvious idea: set the same time on all clocks once, and the problem is solved.&lt;/p&gt;

&lt;p&gt;Unfortunately, it does not work that way.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;That turns synchronization from a one-time setup task into a continuous process.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;You can see how quickly clocks with different rates and wander fall out of sync, even when they start at exactly the same time with the interactive simulation I created for this exact scenario: &lt;a href="https://dmytrohuzz.github.io/interactive_demo/clock_sync/index.html" rel="noopener noreferrer"&gt;https://dmytrohuzz.github.io/interactive_demo/clock_sync/index.html&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  From setting time to synchronization
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The difficult part is that machines cannot read each other’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.&lt;/p&gt;

&lt;p&gt;At the center of that estimate are two questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;how far apart are the clocks?&lt;/li&gt;
&lt;li&gt;how much of the observed difference comes from network delay rather than clock error?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This sounds simple in theory, but the key idea only becomes clear once we walk through it step by step.&lt;/p&gt;

&lt;p&gt;A basic synchronization exchange gives us four timestamps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;t1 — the client sends a request&lt;/li&gt;
&lt;li&gt;t2 — the server receives that request&lt;/li&gt;
&lt;li&gt;t3 — the server sends a response&lt;/li&gt;
&lt;li&gt;t4 — the client receives the response&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F536uky2zlk4mfmsl7oq2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F536uky2zlk4mfmsl7oq2.png" alt="Isync" width="544" height="851"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Now imagine the exchange from the client’s point of view.&lt;/p&gt;

&lt;p&gt;The client sends a request at local time t1 = 00:00.&lt;/p&gt;

&lt;p&gt;Later, it receives the response at local time t4 = 00:04.&lt;/p&gt;

&lt;p&gt;Inside that response, the server includes its own timestamps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it received the request at t2 = 00:06&lt;/li&gt;
&lt;li&gt;it sent the response at t3 = 00:06&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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?&lt;/p&gt;

&lt;p&gt;The answer is simple: t1 and t2 do not belong to the same timeline.&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;p&gt;If the client sees one moment as 00:00, what does the server call that same moment?&lt;/p&gt;

&lt;p&gt;That relationship is what we call offset.&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;packet travel time&lt;/li&gt;
&lt;li&gt;clock offset between client and server&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A useful way to think about it is with time zones.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Firkm47q5zur79sdjhqpz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Firkm47q5zur79sdjhqpz.png" alt="cars" width="800" height="533"&gt;&lt;/a&gt;&lt;br&gt;
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’s clock shows 20:00.&lt;/p&gt;

&lt;p&gt;Now suppose the travel time is the same in both directions.&lt;/p&gt;

&lt;p&gt;The first leg, from your city to the other one, includes:&lt;/p&gt;

&lt;p&gt;travel time + time-zone difference&lt;/p&gt;

&lt;p&gt;The return leg includes:&lt;/p&gt;

&lt;p&gt;travel time - time-zone difference&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;This is exactly what synchronization protocols exploit.&lt;/p&gt;

&lt;p&gt;Under the usual symmetric-delay assumption, the offset can be estimated as:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;offset = ((t2 - t1) + (t3 - t4)) /2&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
and the round-trip delay as:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;delay = (t4 - t1) - (t3 - t2)&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
The first formula separates clock offset from the two directions of travel. The second removes the server’s processing time and leaves only the network round-trip time.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;That is the core idea behind the whole topic.&lt;/p&gt;

&lt;p&gt;Feel free to play with the simulation here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://dmytrohuzz.github.io/interactive_demo/clock_sync/clock_sync_explained" rel="noopener noreferrer"&gt;https://dmytrohuzz.github.io/interactive_demo/clock_sync/clock_sync_explained&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  NTP and PTP: two ways to synchronize clocks
&lt;/h2&gt;

&lt;p&gt;Over time, two major protocol families became the standard answers to the synchronization problem: NTP and PTP.&lt;/p&gt;

&lt;p&gt;Both solve the same core problem: a machine cannot read another machine’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.&lt;/p&gt;

&lt;p&gt;The difference is not the basic idea, but the precision target and the environment they are designed for.&lt;/p&gt;

&lt;h3&gt;
  
  
  NTP: practical synchronization for general systems
&lt;/h3&gt;

&lt;p&gt;NTP — the Network Time Protocol — is the general-purpose approach. It is designed to keep clocks reasonably aligned across ordinary systems and ordinary networks.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;That makes NTP a good fit for:&lt;br&gt;
    • logs and observability&lt;br&gt;
    • authentication and certificate validation&lt;br&gt;
    • scheduled jobs&lt;br&gt;
    • general wall-clock correctness across servers and infrastructure&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
[&lt;a href="https://dmytrohuzz.github.io/interactive_demo/clock_sync/ntp_visualized.html" rel="noopener noreferrer"&gt;Interactive demo&lt;/a&gt;]&lt;/p&gt;

&lt;h3&gt;
  
  
  PTP: tighter synchronization for controlled environments
&lt;/h3&gt;

&lt;p&gt;PTP — the Precision Time Protocol — targets systems where much tighter agreement between clocks is required.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;PTP is commonly used in:&lt;br&gt;
    • industrial and automation systems&lt;br&gt;
    • telecom networks&lt;br&gt;
    • audio and video systems&lt;br&gt;
    • measurement systems&lt;br&gt;
    • finance&lt;br&gt;
    • power and substation environments&lt;/p&gt;

&lt;p&gt;PTP is not just “a more accurate NTP.” It usually operates in a different class of environment, with tighter timing requirements and more deliberate infrastructure support.&lt;br&gt;
[&lt;a href="https://dmytrohuzz.github.io/interactive_demo/clock_sync/ptp_visualized.html" rel="noopener noreferrer"&gt;Interactive Demo&lt;/a&gt;]&lt;/p&gt;

&lt;h3&gt;
  
  
  Different tools for different timing budgets
&lt;/h3&gt;

&lt;p&gt;So NTP and PTP are not really rivals. They are different engineering choices.&lt;/p&gt;

&lt;p&gt;If the goal is to keep ordinary systems aligned to real time well enough for general infrastructure behavior, NTP is usually the right tool.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The key point is this: both protocols depend on timestamp exchange, but the quality of synchronization depends heavily on how those timestamps are produced.&lt;/p&gt;

&lt;p&gt;And that leads to the next question: where exactly was the timestamp taken?&lt;/p&gt;

&lt;p&gt;This is where timestamping location — in software or in hardware — starts to matter.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why timestamp location changes everything
&lt;/h2&gt;

&lt;p&gt;At this point, NTP and PTP may still look like protocol problems: exchange messages, estimate offset, correct the clock.&lt;/p&gt;

&lt;p&gt;But in practice, a large part of synchronization quality depends on something more physical:&lt;/p&gt;

&lt;p&gt;where exactly is the timestamp taken?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5zcekuqv8cljfmie0lx0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5zcekuqv8cljfmie0lx0.jpg" alt="timestamp" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;So two timestamps may look equally precise as numbers while representing very different physical moments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Software timestamping
&lt;/h3&gt;

&lt;p&gt;With software timestamping, the timestamp is recorded somewhere in the software stack after the packet has already passed through part of the system.&lt;/p&gt;

&lt;p&gt;That makes software timestamping widely available and easy to use, but it also means the measurement includes more uncertainty:&lt;br&gt;
    • interrupt latency&lt;br&gt;
    • kernel and driver delay&lt;br&gt;
    • scheduling effects&lt;br&gt;
    • queueing and system load&lt;/p&gt;

&lt;p&gt;As a result, a software timestamp often reflects when the system handled the packet, not the exact moment the packet crossed the network interface.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hardware timestamping
&lt;/h3&gt;

&lt;p&gt;With hardware timestamping, the timestamp is recorded much closer to the real transmit or receive event, typically inside the NIC itself.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;So the practical precision limit is not defined by the protocol name alone. It depends on the full measurement path.&lt;/p&gt;

&lt;p&gt;A good rule of thumb is simple:&lt;/p&gt;

&lt;p&gt;the closer the timestamp is to the wire, the better the synchronization can be.&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;A single computer can keep time locally. A distributed system has a harder task: many machines must keep time together.&lt;/p&gt;

&lt;p&gt;That is why simply setting clocks once is not enough. Real clocks drift, so synchronization has to be continuous. Protocols such as NTP and PTP address this by exchanging timestamped messages, estimating clock offset and network delay, and repeatedly steering local clocks toward a reference.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;So if this part was about the general idea of shared time — why it matters, why it is difficult, and how systems approach it — the next part will move from principle to implementation.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>learning</category>
      <category>devops</category>
    </item>
    <item>
      <title>When you delegate code writing to AI, you can still lean on tests.
But when you delegate the tests to AI as well, what exactly are you relying on?
The code looks fine. The tests are green.
Does that mean the behavior you expect is truly protect?</title>
      <dc:creator>Dmytro Huz</dc:creator>
      <pubDate>Wed, 11 Mar 2026 09:22:32 +0000</pubDate>
      <link>https://dev.to/dmytro_huz/when-you-delegate-code-writing-to-ai-you-can-still-lean-on-tests-but-when-you-delegate-the-tests-30h6</link>
      <guid>https://dev.to/dmytro_huz/when-you-delegate-code-writing-to-ai-you-can-still-lean-on-tests-but-when-you-delegate-the-tests-30h6</guid>
      <description>&lt;p&gt;

&lt;/p&gt;
&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/dmytro_huz/i-built-ac-trace-to-check-what-tests-actually-protect-2pnc" class="crayons-story__hidden-navigation-link"&gt;I Built ac-trace to Check What Tests Actually Protect&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/dmytro_huz" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1824917%2F1549c3d8-4f61-495f-b96f-6ddd331a3c9f.PNG" alt="dmytro_huz profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/dmytro_huz" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Dmytro Huz
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Dmytro Huz
                
              
              &lt;div id="story-author-preview-content-3331161" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/dmytro_huz" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1824917%2F1549c3d8-4f61-495f-b96f-6ddd331a3c9f.PNG" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Dmytro Huz&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/dmytro_huz/i-built-ac-trace-to-check-what-tests-actually-protect-2pnc" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Mar 9&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/dmytro_huz/i-built-ac-trace-to-check-what-tests-actually-protect-2pnc" id="article-link-3331161"&gt;
          I Built ac-trace to Check What Tests Actually Protect
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/testing"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;testing&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/ai"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;ai&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/development"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;development&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/opensource"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;opensource&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/dmytro_huz/i-built-ac-trace-to-check-what-tests-actually-protect-2pnc" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;1&lt;span class="hidden s:inline"&gt; reaction&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/dmytro_huz/i-built-ac-trace-to-check-what-tests-actually-protect-2pnc#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              Comments


              &lt;span class="hidden s:inline"&gt;Add Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            4 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;




</description>
      <category>testing</category>
      <category>ai</category>
      <category>development</category>
      <category>opensource</category>
    </item>
    <item>
      <title>I Built ac-trace to Check What Tests Actually Protect</title>
      <dc:creator>Dmytro Huz</dc:creator>
      <pubDate>Mon, 09 Mar 2026 14:45:13 +0000</pubDate>
      <link>https://dev.to/dmytro_huz/i-built-ac-trace-to-check-what-tests-actually-protect-2pnc</link>
      <guid>https://dev.to/dmytro_huz/i-built-ac-trace-to-check-what-tests-actually-protect-2pnc</guid>
      <description>&lt;p&gt;AI-assisted coding is making one part of software development much faster than another.&lt;/p&gt;

&lt;p&gt;It is now easier than ever to generate implementation code, unit tests, fixtures, mocks, and even test structure. But while output is getting faster, confidence is not automatically getting deeper. In fact, the opposite can happen: the more quickly code and tests are produced, the easier it becomes to confuse visible testing activity with real protection.&lt;/p&gt;

&lt;p&gt;That gap is exactly why I built &lt;strong&gt;&lt;a href="https://github.com/DmytroHuzz/ac-trace" rel="noopener noreferrer"&gt;ac-trace&lt;/a&gt;&lt;/strong&gt;, a new open-source tool.&lt;/p&gt;

&lt;p&gt;The core problem is simple: passing tests are often a weaker signal than teams think. Coverage is not enough either. A test suite can be green, a code path can be exercised, and the intended behavior can still be only weakly defended.&lt;/p&gt;

&lt;p&gt;What I care about is not just whether code ran, or whether assertions passed. The harder question is this:&lt;/p&gt;

&lt;p&gt;Are the acceptance criteria actually protected?&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem: green tests do not prove much by themselves
&lt;/h2&gt;

&lt;p&gt;In many teams, these ideas get blended together:&lt;br&gt;
    • tests are passing&lt;br&gt;
    • code is covered&lt;br&gt;
    • therefore the requirement is safe&lt;/p&gt;

&lt;p&gt;But those are different signals.&lt;/p&gt;

&lt;p&gt;A passing test tells you that some expectation held in one scenario. Coverage tells you that code executed. Neither one, by itself, proves that the important business behavior is strongly defended against breakage.&lt;/p&gt;

&lt;p&gt;This becomes more important with AI-assisted coding.&lt;/p&gt;

&lt;p&gt;AI is good at producing plausible implementations and plausible tests very quickly. That is useful. But it also lowers the cost of producing code that looks well tested. You get more test files, more green checks, more visible structure — and sometimes only shallow confidence underneath.&lt;/p&gt;

&lt;h2&gt;
  
  
  A concrete example
&lt;/h2&gt;

&lt;p&gt;Imagine a billing service with this acceptance criterion:&lt;/p&gt;

&lt;p&gt;Premium users must never be charged above their contractual monthly cap.&lt;/p&gt;

&lt;p&gt;Now imagine the code has tests for invoice creation. It has tests for premium-user billing flow. It has good coverage around the billing function. The relevant lines all execute. The pipeline is green.&lt;/p&gt;

&lt;p&gt;Looks fine.&lt;/p&gt;

&lt;p&gt;But now remove the cap check. Or flip the comparison. Or mutate the mapped billing logic in a way that breaks the intended behavior.&lt;/p&gt;

&lt;p&gt;Do the tests fail?&lt;/p&gt;

&lt;p&gt;If they do not, then the acceptance criterion was never really protected. The system had tests. The code was covered. But the thing that mattered was still weakly defended.&lt;/p&gt;

&lt;p&gt;That is the gap I wanted to make more visible.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3mrdyj9j1754j74laft5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3mrdyj9j1754j74laft5.jpg" alt="schema" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  That is why I built ac-trace
&lt;/h2&gt;

&lt;p&gt;ac-trace (Repo: &lt;a href="https://github.com/DmytroHuzz/ac-trace" rel="noopener noreferrer"&gt;https://github.com/DmytroHuzz/ac-trace&lt;/a&gt;) is an open-source tool that maps acceptance criteria to code and tests, then mutates the mapped code to verify whether the tests actually catch the breakage.&lt;/p&gt;

&lt;p&gt;In plain terms:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;it tries to answer whether the tests defend the behavior they are supposed to defend.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is not just traceability for documentation. The point is not only to show links between requirements, code, and tests. The point is to test whether those links have teeth.&lt;/p&gt;

&lt;h2&gt;
  
  
  How it works
&lt;/h2&gt;

&lt;p&gt;The current workflow is intentionally simple:&lt;br&gt;
    1.  Define acceptance criteria&lt;br&gt;
    2.  Map them to relevant source code and tests&lt;br&gt;
    3.  Infer some links from annotated tests&lt;br&gt;
    4.  Mutate the mapped implementation&lt;br&gt;
    5.  Run the relevant tests&lt;br&gt;
    6.  Generate a report showing what failed and what survived&lt;/p&gt;

&lt;p&gt;So the flow is roughly:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;acceptance criteria → code → tests → mutation → report&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If the mapped code is changed and the linked tests &lt;strong&gt;&lt;em&gt;fail&lt;/em&gt;&lt;/strong&gt;, that is a useful sign.&lt;/p&gt;

&lt;p&gt;If the mapped code is changed and the linked tests still pass, that is also useful — because it shows a confidence gap that might otherwise stay hidden behind a green suite.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why this matters more now
&lt;/h2&gt;

&lt;p&gt;I do not think AI-assisted coding is the problem by itself.&lt;/p&gt;

&lt;p&gt;The problem is that AI increases output faster than it increases justified confidence.&lt;/p&gt;

&lt;p&gt;When implementation and tests both become cheap to generate, teams need better ways to distinguish between:&lt;br&gt;
    • code that looks tested&lt;br&gt;
    • code that is covered&lt;br&gt;
    • code whose important behavior is actually defended&lt;/p&gt;

&lt;p&gt;Without that distinction, it becomes very easy to over-trust green pipelines.&lt;/p&gt;

&lt;p&gt;That is the broader reason for ac-trace. I wanted something practical that pushes on this exact point.&lt;/p&gt;

&lt;h2&gt;
  
  
  Current scope
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/DmytroHuzz/ac-trace" rel="noopener noreferrer"&gt;ac-trace&lt;/a&gt; is still early and intentionally narrow.&lt;/p&gt;

&lt;p&gt;Right now it focuses on:&lt;br&gt;
    • Python&lt;br&gt;
    • pytest&lt;br&gt;
    • YAML manifests&lt;br&gt;
    • inferred links from annotated tests&lt;br&gt;
    • generated reports&lt;/p&gt;

&lt;p&gt;I kept the scope small on purpose. I would rather build a narrow tool around one precise question than make broad claims too early.&lt;/p&gt;

&lt;p&gt;This is an experiment in making one software-quality problem more concrete.&lt;/p&gt;

&lt;h2&gt;
  
  
  Launch note
&lt;/h2&gt;

&lt;p&gt;So this post is also the announcement: ac-trace is now open source: &lt;a href="https://github.com/DmytroHuzz/ac-trace" rel="noopener noreferrer"&gt;https://github.com/DmytroHuzz/ac-trace&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you work on backend systems, care about software quality, or are thinking seriously about how AI changes testing and confidence, I think this problem is worth exploring.&lt;/p&gt;

&lt;p&gt;I built ac-trace because I kept coming back to the same thought:&lt;/p&gt;

&lt;p&gt;Passing tests are useful, but they do not necessarily mean the &lt;strong&gt;&lt;em&gt;acceptance criteria are protected&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;I want a more direct way to inspect that gap.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/DmytroHuzz/ac-trace" rel="noopener noreferrer"&gt;ac-trace&lt;/a&gt; is my open-source attempt to make the gap between green tests and justified confidence more visible.&lt;/p&gt;

&lt;h2&gt;
  
  
  CTA
&lt;/h2&gt;

&lt;p&gt;Check out the &lt;a href="https://github.com/DmytroHuzz/ac-trace" rel="noopener noreferrer"&gt;repo&lt;/a&gt;, try it on a small Python project, and tell me where the idea is useful, naive, or worth pushing further.&lt;/p&gt;

</description>
      <category>testing</category>
      <category>ai</category>
      <category>development</category>
      <category>opensource</category>
    </item>
    <item>
      <title>A Practical Guide to Time for Developers: Part 2 — How one computer keeps time (Linux)</title>
      <dc:creator>Dmytro Huz</dc:creator>
      <pubDate>Thu, 05 Mar 2026 21:28:19 +0000</pubDate>
      <link>https://dev.to/dmytro_huz/a-practical-guide-to-time-for-developers-part-2-how-one-computer-keeps-time-linux-4f1e</link>
      <guid>https://dev.to/dmytro_huz/a-practical-guide-to-time-for-developers-part-2-how-one-computer-keeps-time-linux-4f1e</guid>
      <description>&lt;p&gt;Time on a computer &lt;em&gt;looks&lt;/em&gt; simple: call now(), get a timestamp, move on.&lt;/p&gt;

&lt;p&gt;In the &lt;a href="https://www.dmytrohuz.com/p/a-practical-guide-to-time-for-developers" rel="noopener noreferrer"&gt;previous article&lt;/a&gt;, we discussed the idea that time is just a single point on a timeline. The crucial part is defining &lt;strong&gt;which&lt;/strong&gt; timeline that point belongs to.&lt;/p&gt;

&lt;p&gt;For computers, this matters a lot. A system effectively works with two timelines: &lt;strong&gt;real time&lt;/strong&gt; and &lt;strong&gt;boot time&lt;/strong&gt;. You can convert between them, but they are different measuring systems and shouldn’t be used interchangeably—because each timeline serves a different purpose.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Real time&lt;/strong&gt; answers: “What time is it in the real world right now?”&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Boot time&lt;/strong&gt; answers: “How much time has passed since X (boot)?”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbs7ld4isas2yihapw9o8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbs7ld4isas2yihapw9o8.png" alt="timeline" width="514" height="338"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The computer has has an ecosystem - a few components: hardware and software to track and calculate different times in both timelines. &lt;/p&gt;

&lt;p&gt;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 “system times”, what suspend breaks, where interrupts fit, and how epoch nanoseconds become “Tuesday 14:03 in Vienna”.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Figure 1 — The whole pipeline&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlmk629rsoscvaf559l0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlmk629rsoscvaf559l0.png" alt="Pipeline" width="800" height="919"&gt;&lt;/a&gt;&lt;br&gt;
Four lanes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;RTC&lt;/strong&gt;: survives power-off, keeps “wall time”&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CPU counter&lt;/strong&gt; (often TSC): ticks while the CPU runs&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Kernel timekeeper&lt;/strong&gt;: turns ticks into several clocks&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Userspace&lt;/strong&gt;: calls clock_gettime() and formats time for humans&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now: top → bottom.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;1) Boot &amp;amp; Initialization Phase&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1.1 RTC: the “battery clock”&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;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’s not “nanoseconds since 1970” by nature — that’s something software creates later.&lt;/p&gt;

&lt;p&gt;RTC exists so the system doesn’t boot into the void. Without it, everything starts at “some default” until NTP/PTP (or a human) sets the time.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1.2 Turning RTC into “Unix time”&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;This is still a &lt;em&gt;bootstrap&lt;/em&gt; value. RTC isn’t a precision clock. It’s a “good enough to start” clock.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1.3 The CPU counter: ticks while running&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now the machine is awake, so Linux wants something faster and more stable than “ask the RTC all the time.” Enter the CPU/platform counter — the &lt;strong&gt;clocksource&lt;/strong&gt;. On modern x86, that’s often the &lt;strong&gt;TSC&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Important mindset shift:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;TSC is not “the time.”&lt;/p&gt;

&lt;p&gt;TSC is “how many ticks happened since some arbitrary start.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It’s a counter. It’s only meaningful after conversion.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1.4 Calibration: making ticks speak nanoseconds&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Ticks are just ticks until the kernel knows the counter’s rate. That’s why the diagram shows calibration: “cycles per second”.&lt;/p&gt;

&lt;p&gt;Linux maintains conversion parameters so it can cheaply do:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;delta ticks → delta nanoseconds&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s the key: Linux mostly cares about &lt;strong&gt;deltas&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1.5 Boot finishes by aligning the timelines&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;At the end of boot, two things are true:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;there’s an “elapsed since boot” timeline (monotonic) starting at 0&lt;/li&gt;
&lt;li&gt;there’s a “wall clock” timeline (realtime) aligned to the RTC-derived epoch time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The clean relationship is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;CLOCK_REALTIME = CLOCK_MONOTONIC + wall_clock_offset&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At boot, the kernel chooses the offset so realtime matches RTC.&lt;/p&gt;

&lt;p&gt;This one relationship explains half of the weirdness people hit later.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;2) Runtime Phase (Continuous Tracking)&lt;/strong&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2.1 Where ticks come from (without going into physics)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Under the hood, some oscillator ticks, hardware counts those ticks, and Linux reads the count. That’s it at the conceptual level.&lt;/p&gt;

&lt;p&gt;The only thing worth memorizing here:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The hardware gives ticks. The OS gives meaning.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2.2 The kernel’s “working memory”&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In the middle of the diagram there’s a box of variables. That box is basically the kernel’s timekeeping brain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;tsc_now, tsc_last — current and previous counter snapshot&lt;/li&gt;
&lt;li&gt;mult, shift — ticks→ns conversion&lt;/li&gt;
&lt;li&gt;mono — accumulated elapsed time since boot&lt;/li&gt;
&lt;li&gt;suspend_ns — time spent asleep (so BOOTTIME can include it)&lt;/li&gt;
&lt;li&gt;wall_off — the offset that turns monotonic into realtime&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With those, Linux can build the clock APIs that user space expects.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;3) Time requests: clock_gettime() makes everything happen&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This part of the diagram is the “action scene”:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;userspace calls clock_gettime(CLOCK_...)&lt;/li&gt;
&lt;li&gt;kernel reads the counter (RDTSC in the TSC world)&lt;/li&gt;
&lt;li&gt;kernel updates its state (delta ticks → delta ns → accumulate)&lt;/li&gt;
&lt;li&gt;kernel returns the requested clock&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A useful mental shortcut:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The kernel doesn’t need a metronome to keep time moving.&lt;/p&gt;

&lt;p&gt;It can compute “now” on demand by reading a running counter.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;(Internally there are periodic activities too, but this is the clean model.)&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;The tiny update loop&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The heartbeat is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;delta_ticks = tsc_now - tsc_last&lt;/li&gt;
&lt;li&gt;delta_ns = ticks_to_ns(delta_ticks)&lt;/li&gt;
&lt;li&gt;mono += delta_ns&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything else is derived from mono.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;4) Kernel clocks: three timelines, three different promises&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Now the diagram derives the clocks.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;4.1 CLOCK_MONOTONIC — for durations&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The diagram says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;CLOCK_MONOTONIC = mono&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That’s the “elapsed time” clock.&lt;/p&gt;

&lt;p&gt;It’s the clock to use for anything that needs to be sane even if wall time changes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;timeouts&lt;/li&gt;
&lt;li&gt;retries&lt;/li&gt;
&lt;li&gt;rate limiting&lt;/li&gt;
&lt;li&gt;latency measurements&lt;/li&gt;
&lt;li&gt;“sleep for X”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It doesn’t go backwards, and it doesn’t jump when someone sets the wall clock.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;4.2 CLOCK_REALTIME — for human time&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The diagram says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;CLOCK_REALTIME = mono + wall_off&lt;/p&gt;

&lt;p&gt;&lt;em&gt;(setting time changes wall_off, not mono)&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is &lt;em&gt;the&lt;/em&gt; sentence.&lt;/p&gt;

&lt;p&gt;Realtime is epoch-based wall time. It’s the one that becomes “2026-03-05 13:00:00”.&lt;/p&gt;

&lt;p&gt;Because it must match the outside world, it’s adjustable (NTP/PTP/manual set), and that means it can jump. It’s great for timestamps, terrible for measuring elapsed time.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;4.3 CLOCK_BOOTTIME — monotonic that includes sleep&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The diagram says:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;CLOCK_BOOTTIME = mono + suspend_ns&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Suspend is where people get surprised: monotonic often pauses while the system sleeps. BOOTTIME exists for the “time since boot including sleep” definition.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;5) Power management: suspend/resume is where the split matters&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;During suspend:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CPU isn’t running&lt;/li&gt;
&lt;li&gt;counters may stop or aren’t sampled&lt;/li&gt;
&lt;li&gt;mono doesn’t move (in the simple model)&lt;/li&gt;
&lt;li&gt;real world keeps moving&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the diagram does a clever but simple thing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;store persistent time at suspend (often RTC)&lt;/li&gt;
&lt;li&gt;store persistent time at resume&lt;/li&gt;
&lt;li&gt;difference = sleep delta&lt;/li&gt;
&lt;li&gt;add it to suspend_ns so BOOTTIME advances across sleep&lt;/li&gt;
&lt;li&gt;keep wall clock aligned after resume (effectively by updating the offset)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s why BOOTTIME exists and why wall time remains useful after sleep.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;6) From epoch nanoseconds to “Tuesday 14:03 in Vienna”&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Kernel time is a number. Humans want a calendar.&lt;/p&gt;

&lt;p&gt;On Linux, CLOCK_REALTIME is typically represented as &lt;strong&gt;nanoseconds since the Unix epoch&lt;/strong&gt; (1970-01-01T00:00:00Z). It’s just an integer coordinate on a timeline. Converting it into “Tuesday 14:03 in Vienna” is a user-space job, and it happens in a few very specific steps.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;6.1 Step 1 — split nanoseconds into seconds + remainder&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Most time libraries work in “seconds since epoch” plus a fractional part:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;sec = epoch_ns / 1_000_000_000&lt;/li&gt;
&lt;li&gt;nsec = epoch_ns % 1_000_000_000&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That split is practical: seconds are large-scale time, nanoseconds are the sub-second detail.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;6.2 Step 2 — interpret seconds as UTC and create a UTC timestamp&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;At this point the number becomes “a moment” in UTC:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;utc_instant = epoch_seconds_to_utc(sec, nsec)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;6.3 Step 3 — convert UTC to a named time zone using tzdata rules&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now comes the real-world complexity.&lt;/p&gt;

&lt;p&gt;A numeric offset like +01:00 is not a time zone. It’s just “the offset right now.” Real zones (like Europe/Vienna) are a &lt;strong&gt;ruleset&lt;/strong&gt;: they include historical changes and DST transitions.&lt;/p&gt;

&lt;p&gt;So the conversion is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;local_instant = convert_utc_to_zone(utc_instant, "Europe/Vienna", tzdata)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That conversion does three things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;finds the correct offset for that instant (+01:00 or +02:00, depending on DST and history)&lt;/li&gt;
&lt;li&gt;applies that offset&lt;/li&gt;
&lt;li&gt;produces local calendar fields (year/month/day/hour/min/sec) &lt;em&gt;plus&lt;/em&gt; the offset&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is why “time zones are formatting” is wrong: it’s not string styling, it’s rule evaluation.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;6.4 Ambiguous and missing local times (DST pain in one minute)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;DST creates two special situations that break naive systems:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ambiguous local time (fall back)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The clock repeats an hour. The same local time occurs twice.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example: 2026-10-25 02:30 in many European zones can mean two different instants.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Missing local time (spring forward)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The clock jumps forward. Some local times never occur.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Example: 02:30 on the spring-forward day might not exist at all.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice what happens here: converting &lt;em&gt;from UTC → local&lt;/em&gt; is always unambiguous (UTC instants are unique). The pain happens when converting &lt;em&gt;from local → UTC&lt;/em&gt; without enough context.&lt;/p&gt;

&lt;p&gt;That’s why systems that store “local wall time” without a zone ID eventually end up in a fight with reality.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;6.5 Step 4 — format for display or transport (ISO 8601 / RFC 3339)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;After conversion, formatting is easy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UTC canonical log style: 2026-03-05T13:03:12.123456789Z&lt;/li&gt;
&lt;li&gt;Local display style (with offset): 2026-03-05T14:03:12.123456789+01:00&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The important thing is that formatted output should preserve:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the offset (or Z)&lt;/li&gt;
&lt;li&gt;and ideally the zone context when it matters&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;6.6 What should be stored vs what should be displayed&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is where many systems accidentally create “time debt.”&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Store (internally / in DB / across services):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an unambiguous instant:

&lt;ul&gt;
&lt;li&gt;epoch timestamp (integer + unit), or&lt;/li&gt;
&lt;li&gt;UTC/RFC3339 timestamp with Z&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Display (UI / reports):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;convert to the user’s zone at the edge using tzdata.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;If civil meaning matters (schedules, payroll, appointments):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;store the &lt;em&gt;rule&lt;/em&gt;, not just the instant:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“every day at 09:00 Europe/Vienna”&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;plus the zone ID&lt;/p&gt;

&lt;p&gt;Because recurring human schedules live in civil time and DST rules matter.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;6.7 Tiny practical checklist (saves a lot of bugs)&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use a &lt;strong&gt;zone ID&lt;/strong&gt; (Europe/Vienna), not a fixed offset, for civil-time logic.&lt;/li&gt;
&lt;li&gt;Keep timestamps in &lt;strong&gt;UTC-like canonical form&lt;/strong&gt; internally.&lt;/li&gt;
&lt;li&gt;Convert to local time only at the edges.&lt;/li&gt;
&lt;li&gt;Treat “local naive timestamps” as incomplete data unless paired with a zone/ruleset.&lt;/li&gt;
&lt;li&gt;When parsing timestamps, require either:

&lt;ul&gt;
&lt;li&gt;Z, or&lt;/li&gt;
&lt;li&gt;an explicit offset, or&lt;/li&gt;
&lt;li&gt;a zone ID (for civil-time workflows).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;7) Compact model (matches the diagram)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Variables&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;tsc_last, mult, shift&lt;/li&gt;
&lt;li&gt;mono (ns since boot)&lt;/li&gt;
&lt;li&gt;suspend_ns (ns spent suspended)&lt;/li&gt;
&lt;li&gt;wall_off (epoch ns − mono)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Update step (on each read)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;compute delta ticks from the clocksource&lt;/li&gt;
&lt;li&gt;convert delta ticks → delta ns&lt;/li&gt;
&lt;li&gt;accumulate monotonic time: mono += delta_ns&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Clock readouts&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CLOCK_MONOTONIC = mono&lt;/li&gt;
&lt;li&gt;CLOCK_BOOTTIME = mono + suspend_ns&lt;/li&gt;
&lt;li&gt;CLOCK_REALTIME = mono + wall_off &lt;em&gt;(setting time changes wall_off, not mono)&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;8) Code: a small Linux-style emulator&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I tried to create an clear and easy to understand code that would nicely show how everything works together.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;You can find the result of my experiment here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/DmytroHuzz/linux_clock_emulator/blob/main/linux_clock.py" rel="noopener noreferrer"&gt;https://github.com/DmytroHuzz/linux_clock_emulator/blob/main/linux_clock.py&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;9) Developer cheat sheet&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;CLOCK_MONOTONIC&lt;/strong&gt; for: timeouts, retries, intervals, measuring latency, scheduling “sleep X”.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;CLOCK_BOOTTIME&lt;/strong&gt; for elapsed time that should include suspend.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;CLOCK_REALTIME&lt;/strong&gt; for logs, audits, UI timestamps, business meaning.&lt;/li&gt;
&lt;li&gt;Never compute durations as realtime_end - realtime_start.&lt;/li&gt;
&lt;li&gt;Time zone conversion is userspace logic (tzdata). Store UTC-like timestamps internally.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Next: Part 3&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Part 3 leaves the single machine and goes to the network and we will see how to sync many machines.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>A Practical Guide to Time for Developers: Part 1 — What time is in software (physics + agreements)</title>
      <dc:creator>Dmytro Huz</dc:creator>
      <pubDate>Sun, 01 Mar 2026 06:36:23 +0000</pubDate>
      <link>https://dev.to/dmytro_huz/a-practical-guide-to-time-for-developers-part-1-what-time-is-in-software-physics-agreements-5f96</link>
      <guid>https://dev.to/dmytro_huz/a-practical-guide-to-time-for-developers-part-1-what-time-is-in-software-physics-agreements-5f96</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Preface to the series&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I was tasked with synchronizing time across &lt;strong&gt;N computers&lt;/strong&gt; with &lt;strong&gt;~1 nanosecond accuracy&lt;/strong&gt;. Not “a laptop over Wi-Fi” — a controlled wired setup where hardware timestamping and disciplined clocks make that goal at least a meaningful engineering target.&lt;/p&gt;

&lt;p&gt;At first it sounded trivial. We learn clocks, dates, and time zones as kids. How hard can it be?&lt;/p&gt;

&lt;p&gt;The industry already has a standard solution: &lt;strong&gt;Precision Time Protocol (PTP)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;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: &lt;strong&gt;TAI vs UTC, epochs, leap seconds, RTC vs system clock, wall clock vs monotonic time, time zones, naïve timestamps&lt;/strong&gt;. It turns out “time” is not a single thing — it’s physics, standards, and human conventions layered on top of each other.&lt;/p&gt;

&lt;p&gt;I searched for a single article that explains the whole chain — something like “Time for software developers: zero to hero” or “From RTC to PTP” — and couldn’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.&lt;/p&gt;

&lt;p&gt;This series has four parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;What time is (in software)&lt;/strong&gt; — what exactly we’re tracking, and what “correct” even means.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How a computer keeps time&lt;/strong&gt; — where ticks come from, how clocks drift, and why operating systems maintain multiple clocks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How systems share time&lt;/strong&gt; — NTP vs PTP, timestamping, asymmetry, and what really limits accuracy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What can go wrong (and how you detect it)&lt;/strong&gt; — validation, monitoring, failure modes, and security/trust of time sources.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let’s start with the foundation: &lt;strong&gt;what is time — physics or agreements?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;You already know what time &lt;em&gt;feels&lt;/em&gt; like. That’s the trap.&lt;/p&gt;

&lt;p&gt;In software, “time” is not one thing. It’s a mix of &lt;strong&gt;physical reality&lt;/strong&gt; (oscillators drift, signals take time to travel), &lt;strong&gt;standards&lt;/strong&gt; (UTC, leap seconds), and &lt;strong&gt;human conventions&lt;/strong&gt; (time zones, calendars). If you don’t separate these layers, you end up building systems that look correct in tests and then collapse in production—usually around midnight, DST, or a “rare” edge case.&lt;/p&gt;

&lt;p&gt;This part builds a simple foundation: &lt;strong&gt;what exactly is the thing we’re tracking when we say “time”?&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Four different problems people call “time”&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Most confusion comes from mixing these up. People say “time,” but they might mean &lt;strong&gt;four totally different things&lt;/strong&gt;, and each one requires a different kind of clock, API, and mental model.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;1) Time-of-day (civil time)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; &lt;em&gt;“What date/time is it right now?”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is the time humans care about: calendars, weekdays, business hours, “yesterday,” tax reports, contracts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Used for:&lt;/strong&gt; logs, UI, audit trails, business processes, legal records.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Typical failure modes:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DST: the same local time can happen twice, or not happen at all.&lt;/li&gt;
&lt;li&gt;Time zones: “10:00” without a zone is not a timestamp, it’s a vague sentence.&lt;/li&gt;
&lt;li&gt;Clock corrections: timestamps can jump forward/backward when the system is adjusted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Rule of thumb:&lt;/strong&gt; civil time is great for &lt;em&gt;human meaning&lt;/em&gt;, not for measuring anything.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;2) Duration / intervals&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; &lt;em&gt;“How long did it take?” / “Wait 500 ms.”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is not “date/time.” This is &lt;strong&gt;elapsed time&lt;/strong&gt;. You don’t want it to jump. You want it to be steady and monotonic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Used for:&lt;/strong&gt; timeouts, retries, benchmarks, scheduling, rate limiting.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Typical failure modes:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Using wall clock for timeouts → timeout triggers instantly or never triggers after a time correction.&lt;/li&gt;
&lt;li&gt;Negative durations (“operation took -3 ms”) because the clock moved backwards.&lt;/li&gt;
&lt;li&gt;Inconsistent metrics when different machines have different offsets.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Rule of thumb:&lt;/strong&gt; durations must come from a clock that only moves forward.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;3) Ordering / causality&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; &lt;em&gt;“Which event happened first?”&lt;/em&gt; (especially across threads/processes/machines)&lt;/p&gt;

&lt;p&gt;This is the one that causes the most hidden damage. Humans intuitively think timestamps imply ordering. In distributed systems, that’s often false.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Used for:&lt;/strong&gt; distributed tracing, message processing, state machines, replication, conflict resolution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Typical failure modes:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Two machines disagree about “now” → you see “future” events in logs.&lt;/li&gt;
&lt;li&gt;Network delay/scheduling jitter reorder events even if clocks are “pretty good.”&lt;/li&gt;
&lt;li&gt;You use timestamps to order messages and occasionally violate invariants (“this update happened before its cause”).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Rule of thumb:&lt;/strong&gt; if correctness depends on ordering, don’t quietly rely on wall-clock time alone. Use explicit ordering mechanisms (sequence numbers, causality-aware designs, etc.) and treat timestamps as &lt;em&gt;metadata&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;4) Frequency / rate&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; &lt;em&gt;“Are two clocks running at the same speed?”&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This isn’t “what time is it,” it’s &lt;strong&gt;how fast time passes&lt;/strong&gt;. For high-precision work (PTP, measurement, telecom), this matters as much as absolute offset.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Used for:&lt;/strong&gt; high-precision sync, control loops, telecom, measurement systems, sampling, sensor fusion.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Typical failure modes:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You correct offset but ignore drift → you constantly “chase” the reference.&lt;/li&gt;
&lt;li&gt;Short-term jitter ruins measurements even if average offset looks good.&lt;/li&gt;
&lt;li&gt;You assume nanosecond &lt;em&gt;resolution&lt;/em&gt; implies nanosecond &lt;em&gt;accuracy&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Rule of thumb:&lt;/strong&gt; precision time is always a control problem: you manage both offset (sync) and rate (syntonization).&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;The category mistake that creates most time bugs&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A lot of bugs are simply &lt;strong&gt;using a tool from one category to solve another&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Classic example: using civil time to measure durations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You record start = wall_clock_now()&lt;/li&gt;
&lt;li&gt;You record end = wall_clock_now()&lt;/li&gt;
&lt;li&gt;You compute end - start&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It works… until the system clock is adjusted (NTP/PTP correction, manual change, VM migration, DST misconfig). Then the wall clock can jump backwards, and your “duration” becomes negative, your retry logic breaks, or your timeout never fires.&lt;/p&gt;

&lt;p&gt;That’s not a rare corner case. It’s an inevitable result of mixing categories.&lt;/p&gt;

&lt;p&gt;If you remember one thing from this section, remember this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Time-of-day is for meaning. Duration is for measurement. Ordering is for correctness. Frequency is for precision.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Basic vocabulary that prevents endless confusion&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When someone says “we need &lt;strong&gt;1 ns accuracy&lt;/strong&gt;,” the only correct first reaction is: &lt;em&gt;accuracy of what, relative to what, over what time window, and how will we measure it?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you don’t pin down the vocabulary, teams end up arguing for weeks while everyone is technically correct in their own private definition.&lt;/p&gt;

&lt;p&gt;Below are the terms you must keep separate.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Resolution&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What it is:&lt;/strong&gt; the smallest step your clock can &lt;em&gt;represent&lt;/em&gt; or &lt;em&gt;report&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Example: a timestamp API that returns nanoseconds has &lt;strong&gt;1 ns resolution&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What it is not:&lt;/strong&gt; a guarantee that the clock is correct to 1 ns.&lt;/p&gt;

&lt;p&gt;A clock can happily produce nanosecond-looking numbers while being microseconds (or milliseconds) away from the truth. This is why “we have nanosecond timestamps” is almost meaningless by itself.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Precision&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What it is:&lt;/strong&gt; how fine your measurement or reporting is — how many digits you output and how repeatable your measurement process is.&lt;/p&gt;

&lt;p&gt;Precision often gets confused with resolution. A useful way to think about it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Resolution&lt;/strong&gt; is “how small a step the counter can show.”&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Precision&lt;/strong&gt; is “how finely we can &lt;em&gt;measure&lt;/em&gt; and how consistent our measurement results are.”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Accuracy&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What it is:&lt;/strong&gt; how close your clock is to a reference (a trusted source, or “true time” in some defined sense).&lt;/p&gt;

&lt;p&gt;Accuracy always depends on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;the reference&lt;/strong&gt; (UTC? TAI? GPS? a grandmaster clock?),&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;the path&lt;/strong&gt; (network delays),&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;the method&lt;/strong&gt; (hardware timestamping vs software),&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;the measurement point&lt;/strong&gt; (where you observe time).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So “1 ns accuracy” without specifying the reference and measurement method is not a requirement — it’s a slogan.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Stability&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What it is:&lt;/strong&gt; how consistently the clock runs over time. In other words: how noisy it is and how much its rate changes.&lt;/p&gt;

&lt;p&gt;Two systems can have the same accuracy at a single moment and wildly different stability:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One stays close for hours.&lt;/li&gt;
&lt;li&gt;The other drifts immediately and needs constant correction.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In practice, stability is what determines how hard your synchronization algorithm has to work.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Offset&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What it is:&lt;/strong&gt; the difference between your clock and the reference &lt;em&gt;right now&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;If the reference says 12:00:00.000000000 and you say 12:00:00.000000500, your offset is &lt;strong&gt;+500 ns&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Offset is the number people usually mean when they casually say “we’re synced to X.”&lt;/p&gt;

&lt;p&gt;But offset alone is not the full story, because it doesn’t tell you how noisy that offset is or how it behaves over time.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Jitter&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What it is:&lt;/strong&gt; short-term variation — the “shake” around the average.&lt;/p&gt;

&lt;p&gt;If you measure offset once per second and the values bounce around like:&lt;/p&gt;

&lt;p&gt;+20 ns, -15 ns, +35 ns, -10 ns...&lt;/p&gt;

&lt;p&gt;that bounce is jitter.&lt;/p&gt;

&lt;p&gt;Jitter matters because many systems care about instantaneous behavior, not just long-term average. A “perfect” average offset is useless if the clock is too noisy for your application.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Wander&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;What it is:&lt;/strong&gt; slow changes over longer timescales — the “drift of the drift.”&lt;/p&gt;

&lt;p&gt;Where jitter is rapid noise, wander is a slow trend: temperature changes, oscillator aging, environmental effects, network path changes that persist.&lt;/p&gt;

&lt;p&gt;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’t track it.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Synchronization vs syntonization (phase vs rate)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is one of the most important distinctions in precision time, and it’s usually not named explicitly — which is why people get confused.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Synchronize&lt;/strong&gt; = align &lt;strong&gt;phase&lt;/strong&gt; → reduce &lt;strong&gt;offset&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;“Make our timestamps match right now.”&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Syntonize&lt;/strong&gt; = align &lt;strong&gt;rate&lt;/strong&gt; → reduce &lt;strong&gt;drift&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;“Make our clocks run at the same speed.”&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you only synchronize (phase) but don’t syntonize (rate), you get a system that constantly drifts away and needs repeated “kicks” back into place. If you syntonize well, the system stays close with small, smooth corrections.&lt;/p&gt;

&lt;p&gt;That’s why protocols like &lt;strong&gt;PTP&lt;/strong&gt; are not “set the time once and forget it.” They run a continuous control loop: measure offset, estimate delay, correct phase and rate, and fight noise (jitter) and slow effects (wander).&lt;/p&gt;




&lt;p&gt;If you want a single mental model: &lt;strong&gt;precision time is control theory applied to clocks&lt;/strong&gt;. The numbers (offset/jitter/wander) are the feedback signals; synchronization and syntonization are the control objectives.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Timescales: what your timestamps are actually referencing&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A timestamp looks like a number. That’s why developers treat it like a number.&lt;/p&gt;

&lt;p&gt;But a timestamp is not “time.” It’s a &lt;strong&gt;coordinate&lt;/strong&gt; in some system — and the most important part of that coordinate system is the &lt;strong&gt;timescale&lt;/strong&gt;: &lt;em&gt;what kind of “time” this number is measuring.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If two systems use different timescales, their timestamps can be perfectly well-formed and still be fundamentally incomparable.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;What is a timescale?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;timescale&lt;/strong&gt; is a definition of how seconds are counted and how that count is anchored to reality.&lt;/p&gt;

&lt;p&gt;A useful way to think about it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It defines what “one second” means (atomic seconds vs adjusted seconds).&lt;/li&gt;
&lt;li&gt;It defines whether the count is &lt;strong&gt;continuous&lt;/strong&gt; or can &lt;strong&gt;jump&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;It defines how it relates to civil time (what humans call “UTC time”).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So when someone says “store timestamps in UTC,” they’re implicitly making a choice about a timescale — and about how the system behaves during edge cases.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;TAI (International Atomic Time)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;TAI is the cleanest mental model for engineers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it is a &lt;strong&gt;continuous&lt;/strong&gt; count of atomic seconds&lt;/li&gt;
&lt;li&gt;it &lt;strong&gt;does not have leap seconds&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;it does not care about Earth’s rotation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;TAI is what you’d want if your only goal was: &lt;em&gt;a global, steady clock that never inserts weird discontinuities.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The downside is social, not technical: people don’t live in TAI. Civil time is defined using UTC.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;UTC (Coordinated Universal Time)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;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 &lt;strong&gt;leap seconds&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That single detail has a huge consequence:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;UTC is not guaranteed to be perfectly continuous.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Most of the time, UTC behaves like a normal continuous timescale. But around leap seconds, systems can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;repeat a second,&lt;/li&gt;
&lt;li&gt;represent 23:59:60,&lt;/li&gt;
&lt;li&gt;step,&lt;/li&gt;
&lt;li&gt;or smear.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So “UTC” is a civil agreement that often behaves like a smooth clock — until it doesn’t.&lt;/p&gt;

&lt;p&gt;This is why developers eventually run into bugs that sound impossible:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“Why did the same timestamp appear twice?”&lt;/li&gt;
&lt;li&gt;“Why did time go backwards for a second?”&lt;/li&gt;
&lt;li&gt;“Why do two machines disagree about UTC during the same minute?”&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;GPS time (and other system times)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;GPS time is a common example of a system timescale:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it is &lt;strong&gt;continuous&lt;/strong&gt; (no leap seconds)&lt;/li&gt;
&lt;li&gt;it is used internally by a technical system because continuity is convenient&lt;/li&gt;
&lt;li&gt;it has a &lt;strong&gt;known offset&lt;/strong&gt; relative to other scales (like UTC/TAI), but that offset is not “magically applied” everywhere the same way&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And GPS is not alone. Many systems use their own “continuous time” internally because it simplifies math and avoids leap-second edge cases.&lt;/p&gt;

&lt;p&gt;The important point isn’t the details of GPS time. It’s the category:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Many technical systems use a continuous timescale internally and only convert to UTC for humans.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;You don’t need to memorize offsets — you need the&lt;/strong&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;contract&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;At this stage, memorizing “how many seconds UTC differs from TAI” is not the goal. You can look up numbers.&lt;/p&gt;

&lt;p&gt;The goal is to internalize this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In software, “time” is usually&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;a number plus a contract&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What is it anchored to?&lt;/strong&gt; (UTC? TAI? a grandmaster? device-local monotonic time?)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Is it continuous, or can it jump?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What happens during leap seconds?&lt;/strong&gt; (step? smear? ignore? represent 23:59:60?)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;How do we convert it for humans?&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once you treat timestamps as “numbers with contracts,” a lot of time-related confusion disappears — and the rest becomes an engineering problem you can actually reason about.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Leap seconds: the edge case that isn’t optional&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Leap seconds exist for a simple reason: Earth is not a perfect clock.&lt;/p&gt;

&lt;p&gt;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 (“noon should be around when the Sun is highest”). So UTC is designed to track Earth rotation closely enough — and when the gap grows too large, UTC is adjusted by inserting (and in theory removing) a second.&lt;/p&gt;

&lt;p&gt;That’s the astronomy story. Here’s the software story:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The real problem is not that leap seconds exist.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The real problem is that systems don’t agree on how to implement them.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;The trap: “UTC” is not one behavior&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;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 “UTC behaviors” in the wild.&lt;/p&gt;

&lt;p&gt;That means two machines can both claim “UTC” and still produce timestamps that aren’t directly comparable during a leap-second event.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Common behaviors you will encounter&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1) Explicit leap second (23:59:60)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some systems model the extra second as a real extra label in the clock representation.&lt;/p&gt;

&lt;p&gt;This is conceptually honest: there really is an inserted second.&lt;/p&gt;

&lt;p&gt;But it breaks assumptions everywhere:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;parsers that reject :60&lt;/li&gt;
&lt;li&gt;sorting logic that doesn’t expect it&lt;/li&gt;
&lt;li&gt;“every minute has exactly 60 seconds” code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2) Step / repeat / jump&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Other systems handle the event by effectively repeating a second or stepping the time.&lt;/p&gt;

&lt;p&gt;From a developer perspective this looks like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;timestamps that stop moving forward for a moment&lt;/li&gt;
&lt;li&gt;a repeated time value&lt;/li&gt;
&lt;li&gt;or “time went backwards” depending on representation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is poison for anything that assumes monotonic behavior from civil time, especially ordering and durations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3) Smear (stretching time over a window)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of inserting a visible extra second, some environments “smear” it: they slightly slow down (or speed up) the clock over a window so that the leap second is absorbed smoothly.&lt;/p&gt;

&lt;p&gt;This avoids a hard discontinuity, which is great for many systems.&lt;/p&gt;

&lt;p&gt;But it introduces a different kind of inconsistency:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;during the smear window, your “UTC” is &lt;strong&gt;not exactly UTC&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;two systems can disagree because one smears and the other doesn’t&lt;/li&gt;
&lt;li&gt;comparisons across vendors become tricky (“why are we off by hundreds of ms even though we’re both ‘UTC’?”)&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Why this matters even if leap seconds are rare&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You might think: “Leap seconds almost never happen. Who cares?”&lt;/p&gt;

&lt;p&gt;Two reasons you should care anyway:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;The edge case exists at the standards level&lt;/strong&gt;, so it shows up in libraries, operating systems, and infrastructure — whether you like it or not. You inherit it.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Distributed systems amplify rare events&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;One leap second can create:&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;broken ordering across machines&lt;/li&gt;
&lt;li&gt;weird negative durations in logs/metrics pipelines&lt;/li&gt;
&lt;li&gt;parsing failures in analytics&lt;/li&gt;
&lt;li&gt;incident timelines that don’t line up when you need them most&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;What you must decide early (policy, not implementation)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If your system needs strict timestamp comparisons, you need an explicit policy. Not a vibe. A policy.&lt;/p&gt;

&lt;p&gt;Key questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Do you store time-of-day as UTC timestamps, or as a continuous internal timescale?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;(Many serious systems store continuous internal time and convert for display.)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Do you require “true UTC,” or are you okay with smeared UTC?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;(If you compare across cloud providers, “okay with smear” might be forced on you.)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Where do you do conversions?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Store canonical time internally; convert at the edges (UI, reporting), or the other way around?&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You don’t have to solve leap-second handling in Part 1. That comes later. But you must do the one thing that prevents surprise:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Acknowledge that “UTC” is not a single universal runtime behavior.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Epochs and units: a timestamp is a coordinate system&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Almost all practical timestamps in software are just a coordinate:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;“X units since some chosen origin.”&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That origin is an &lt;strong&gt;epoch&lt;/strong&gt;. The unit is seconds / milliseconds / nanoseconds (or sometimes “ticks”). Together they define the coordinate system your entire platform will live in.&lt;/p&gt;

&lt;p&gt;Most of the time we treat epoch choice as a boring implementation detail. And it &lt;em&gt;is&lt;/em&gt; mostly engineering convenience — until you need long-term compatibility, cross-system integration, or debugging. Then epoch and units suddenly become the difference between “obvious” and “impossible.”&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Epoch: what is your “zero”?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;An &lt;strong&gt;epoch&lt;/strong&gt; is simply the timestamp you call “0.”&lt;/p&gt;

&lt;p&gt;Common epochs you’ll encounter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Unix epoch&lt;/strong&gt;: 1970-01-01 (the usual “POSIX time” family)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;System uptime / boot time&lt;/strong&gt;: epoch = when the OS booted&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Process start&lt;/strong&gt;: epoch = when a process started&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Custom epochs&lt;/strong&gt;: sometimes chosen for storage size, legacy reasons, or protocol specs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of these is inherently “better.” They serve different purposes.&lt;/p&gt;

&lt;p&gt;The important thing is: once you choose an epoch for storage or APIs, you’ve created a contract. Changing it later is like changing the unit of distance in the middle of a highway.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Units: the silent multiplier that breaks everything&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A timestamp number is meaningless unless you know its unit.&lt;/p&gt;

&lt;p&gt;Typical units:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;seconds (s)&lt;/li&gt;
&lt;li&gt;milliseconds (ms)&lt;/li&gt;
&lt;li&gt;microseconds (µs)&lt;/li&gt;
&lt;li&gt;nanoseconds (ns)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most common production bug here is not exotic. It’s this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;someone sends milliseconds,&lt;/li&gt;
&lt;li&gt;someone reads seconds,&lt;/li&gt;
&lt;li&gt;everything looks “roughly right” in small tests,&lt;/li&gt;
&lt;li&gt;and then you ship timestamps that are off by &lt;strong&gt;1000×&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your codebase uses multiple units, enforce one of these rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;include unit in the name (timestamp_ms, timeout_ns)&lt;/li&gt;
&lt;li&gt;or use a strong type / duration type system&lt;/li&gt;
&lt;li&gt;or centralize conversions in one place&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don’t rely on comments and good intentions.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Integers vs floats: why “it fits in a double” is not a plan&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Floats look convenient because you can write 1700000000.123456789.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Practical rule:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;store and transport timestamps as integers&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;attach the unit explicitly&lt;/li&gt;
&lt;li&gt;if you need fractions for display, convert at the edges&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is especially important once you claim nanoseconds. If you represent “nanoseconds since 1970” as a float, you’re basically begging for precision loss.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;You must label the kind of timestamp, not just the number&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Even if you know the epoch and unit, you still need to know what kind of “time” it is.&lt;/p&gt;

&lt;p&gt;Be explicit about whether a timestamp is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Time-of-day (civil / wall-clock)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Anchored to a UTC-like timescale. Comparable across machines &lt;em&gt;if&lt;/em&gt; they share the same time source and leap-second policy.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Monotonic-ish (elapsed time)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Anchored to boot or process start. Great for measuring durations and scheduling. Usually meaningless to compare across machines, and often not meaningful after reboot.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Logical (ordering)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Anchored to an ordering scheme (sequence numbers, causality). Comparable for ordering, not for “real-world time.”&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is the difference between a useful timestamp and a number that accidentally sorts most of the time.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;The classic “1970” bug and what it really means&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When someone says: “Why is this event from 1970?” it usually means one of these happened:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you interpreted &lt;strong&gt;milliseconds&lt;/strong&gt; as &lt;strong&gt;seconds&lt;/strong&gt; (or vice versa)&lt;/li&gt;
&lt;li&gt;you used the wrong epoch (boot-time treated as Unix time)&lt;/li&gt;
&lt;li&gt;you parsed a timestamp as local civil time when it was UTC (or vice versa)&lt;/li&gt;
&lt;li&gt;you truncated or overflowed (32-bit seconds, wrong cast)&lt;/li&gt;
&lt;li&gt;you mixed timescales (rare, but catastrophic when it happens)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The “1970” symptom is your system screaming: &lt;em&gt;your coordinate system is inconsistent.&lt;/em&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Practical rules (expanded)&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Always know your &lt;strong&gt;epoch&lt;/strong&gt; and your &lt;strong&gt;unit&lt;/strong&gt;. If you can’t answer both instantly, you don’t have a timestamp — you have a random number.&lt;/li&gt;
&lt;li&gt;Prefer &lt;strong&gt;integer&lt;/strong&gt; storage/transport.&lt;/li&gt;
&lt;li&gt;Encode the unit in the API/type/name.&lt;/li&gt;
&lt;li&gt;Treat timestamp types as separate domains:

&lt;ul&gt;
&lt;li&gt;wall-clock for human meaning&lt;/li&gt;
&lt;li&gt;monotonic for durations&lt;/li&gt;
&lt;li&gt;logical for ordering&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Never mix different timestamp kinds without explicit conversion and a clear reason.&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Why this matters for the rest of the series&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Protocols and OS clocks become much easier to understand once you separate:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;what “time” means&lt;/strong&gt; (timescale and behavior)&lt;/li&gt;
&lt;li&gt;from &lt;strong&gt;how it’s encoded&lt;/strong&gt; (epoch + unit + representation)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Part 2 we’ll look at where these numbers come from inside one machine, and why “the system time” is actually several different clocks with different guarantees.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Civil time: time zones, DST, and calendars are not “formatting”&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A lot of engineers treat time zones as UI: “we’ll store UTC and just format it for the user.” That instinct is half right.&lt;/p&gt;

&lt;p&gt;The other half is where projects die: &lt;strong&gt;civil time is not a formatting layer&lt;/strong&gt;. It’s a set of rules that changes across geography &lt;em&gt;and across history&lt;/em&gt;. If your system interacts with humans, payroll, contracts, schedules, billing cycles, or “days,” you are doing civil-time logic whether you admit it or not.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Civil time is a rules database, not a law of physics&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Time zones are not just “UTC+2.” They are effectively:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a region identifier (e.g., “Europe/Vienna”, not “+01:00”)&lt;/li&gt;
&lt;li&gt;plus a historical database of changes:

&lt;ul&gt;
&lt;li&gt;offsets change over the decades&lt;/li&gt;
&lt;li&gt;DST rules change (sometimes with very short notice)&lt;/li&gt;
&lt;li&gt;sometimes entire countries switch policy&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;So “local time” is not stable unless you store the &lt;em&gt;zone identifier&lt;/em&gt; and consult a time zone database for the correct rule at that date.&lt;/p&gt;

&lt;p&gt;If you store only “UTC offset at the moment,” you lose the ability to reproduce civil time correctly later.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;DST creates two kinds of broken times: ambiguous and missing&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Daylight saving time is where naive systems reveal themselves.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ambiguous local time (fall back)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The clock is set back. A local time interval happens twice.&lt;/p&gt;

&lt;p&gt;So a local timestamp like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;2026-10-25 02:30&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;is ambiguous in many European zones: it could refer to the “first 02:30” or the “second 02:30.” Without extra context (offset or zone rule), that timestamp is not uniquely defined.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Missing local time (spring forward)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The clock jumps forward. Some local times never happen.&lt;/p&gt;

&lt;p&gt;So a local timestamp like “02:30” on the DST jump day might literally be invalid: it never occurred.&lt;/p&gt;

&lt;p&gt;This is why “local time as a primary storage format” is a trap. Your database will happily store impossible moments.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Calendars are hostile: “day” and “month” are not durations&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Civil time mixes clocks with calendars, and calendars don’t behave like physics.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;“Add 24 hours” ≠ “add 1 day”&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adding 24 hours means “exactly 86,400 seconds later.”&lt;/li&gt;
&lt;li&gt;Adding 1 day often means “same local clock time on the next calendar day.”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;During DST transitions, those diverge. Some days are 23 hours, some are 25. So the “next day at 09:00” is not always “+24h”.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;“Add 1 month” is not a duration at all&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A month is not a fixed number of seconds. It’s a calendar concept with edge cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is “one month after January 31”?&lt;/li&gt;
&lt;li&gt;Is it February 28/29? March 3? “clamp to end of month”? error?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There isn’t one universally correct answer. There are policies — and you must choose one explicitly.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;The hidden production bugs this causes&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Civil-time mistakes usually appear as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;duplicated timestamps in logs (same local time twice)&lt;/li&gt;
&lt;li&gt;scheduling drift (“meeting moved by one hour”)&lt;/li&gt;
&lt;li&gt;billing/payroll disagreements (“which day counts?”)&lt;/li&gt;
&lt;li&gt;impossible events (“this happened at a time that never existed”)&lt;/li&gt;
&lt;li&gt;long-term reproducibility issues (“it used to show 10:00, now it shows 11:00 for old data”)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The worst part: these bugs often don’t show up in unit tests because tests don’t run across DST boundaries or historical rule changes.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;A policy that prevents endless pain (and where it breaks)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A sane default for most systems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Store and exchange&lt;/strong&gt; timestamps in a single global standard (typically UTC-like).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Convert&lt;/strong&gt; to local time zones only at the edges (UI, reports).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep calendar arithmetic explicit and isolated&lt;/strong&gt; (and test DST boundaries).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Two important additions to make this actually work in real systems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When civil meaning matters (appointments, payroll, “local midnight”), store the &lt;strong&gt;time zone ID&lt;/strong&gt; (e.g., “Europe/Vienna”), not just an offset.&lt;/li&gt;
&lt;li&gt;If you store recurring schedules (“every day at 09:00 local time”), store them as &lt;strong&gt;civil-time rules&lt;/strong&gt;, not as precomputed UTC instants.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because recurring human schedules are defined in civil time — and civil time changes.&lt;/p&gt;




&lt;p&gt;If you internalize one idea from this section, make it this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Local time is not a timestamp.&lt;/p&gt;

&lt;p&gt;It becomes a timestamp only when you attach a time zone rule set — and accept the edge cases.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Physical time vs logical time (distributed systems reality)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Even if you had “perfect” clock synchronization, distributed systems still wouldn’t behave like a single machine. Reality gets in the way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;network delay&lt;/strong&gt; (packets take time to arrive),&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;asymmetry&lt;/strong&gt; (A→B delay is not necessarily equal to B→A),&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;scheduling delays&lt;/strong&gt; (your process didn’t run when you think it did),&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;partial failures&lt;/strong&gt; (timeouts, retries, partitions),&lt;/li&gt;
&lt;li&gt;and simply &lt;strong&gt;different perspectives of “now.”&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This matters because developers use time for two very different purposes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;em&gt;to attach human meaning&lt;/em&gt; (“when did it happen?”)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;to decide correctness&lt;/em&gt; (“which happened first?”)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Those are not the same problem.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Two broad approaches to ordering events&lt;/strong&gt;
&lt;/h3&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Physical timestamps (“wall clock time”)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is the familiar one: attach a wall-clock timestamp to events.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it’s useful:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;humans can read it&lt;/li&gt;
&lt;li&gt;audit trails and legal records need it&lt;/li&gt;
&lt;li&gt;it’s great for observability (“show me what happened around 12:03”)&lt;/li&gt;
&lt;li&gt;it helps correlate events across services &lt;em&gt;when sync is good enough&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why it’s risky for correctness:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Physical time is an approximation. Even with good sync, you can still get:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;mis-ordering&lt;/strong&gt;: event B appears “earlier” than its cause A because A’s message was delayed or A’s clock is slightly behind&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;future events&lt;/strong&gt;: logs show something that “happened in the future” relative to another machine&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;time going backwards&lt;/strong&gt; locally when clocks are stepped&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So: wall-clock timestamps are excellent metadata. They are a weak foundation for correctness.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Logical time (causality-aware ordering)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Logical time exists because “timestamp ordering” is not the same as “happened-before ordering.”&lt;/p&gt;

&lt;p&gt;The core idea is simple:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;A happened before B&lt;/strong&gt; if A could have influenced B.&lt;/p&gt;

&lt;p&gt;Not because A’s timestamp is smaller.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Logical clocks (conceptually: Lamport timestamps and vector clocks) encode causality:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If B observed A (directly or indirectly), B must be ordered after A.&lt;/li&gt;
&lt;li&gt;If two events are independent, they may be concurrent, and ordering them is a policy decision, not a fact revealed by a clock.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why it’s useful:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;correctness in replication, conflict resolution, messaging systems&lt;/li&gt;
&lt;li&gt;reasoning about distributed workflows and state machines&lt;/li&gt;
&lt;li&gt;avoiding “timestamp lies” when clocks disagree&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why it’s not a replacement for wall time:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Logical time doesn’t tell you “it’s 12:03.” It tells you “this depends on that.”&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Mature systems use both (and are explicit about it)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Most serious systems end up with two parallel layers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Physical time&lt;/strong&gt; for observability, audit, user-facing meaning, “what happened when”&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Logical ordering / protocol guarantees&lt;/strong&gt; where correctness depends on ordering&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is also how you keep sane during incidents:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;physical timestamps help humans reconstruct timelines&lt;/li&gt;
&lt;li&gt;ordering guarantees help the system stay correct even when time is messy&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;The takeaway&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If you need &lt;strong&gt;correct ordering&lt;/strong&gt;, don’t silently assume wall-clock time gives it.&lt;/p&gt;

&lt;p&gt;Use wall-clock timestamps for meaning and correlation — but when correctness depends on “what happened first,” you need explicit ordering mechanisms (protocol guarantees, sequence numbers, causality-aware clocks, or designs that don’t depend on global time).&lt;/p&gt;

&lt;p&gt;Because in distributed systems, “now” is not a global fact. It’s a local opinion.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Summary: what “time” is, in one sentence&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In software, time is &lt;strong&gt;a continuously maintained estimate&lt;/strong&gt; of some reference, expressed in a chosen coordinate system (timescale + epoch + units), and only then mapped into human conventions like calendars and time zones.&lt;/p&gt;

&lt;p&gt;If that sounds heavier than “a number that increases,” good — because treating time as “just a number” is exactly how you end up with negative durations, duplicated local timestamps, and distributed logs that can’t be reconciled.&lt;/p&gt;

&lt;p&gt;In the next part we’ll go one layer deeper and get practical: how a single computer actually keeps time — where ticks come from, what the OS does with them, why there are multiple clocks, and why “correcting the clock” can make time jump (and break anything that assumed it couldn’t).&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Developer rules (keep this as your reference card)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you remember nothing else from this part, remember these:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Decide what problem you’re solving: &lt;strong&gt;time-of-day vs duration vs ordering vs frequency&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Store/transport canonical time in one global form (typically UTC-like).&lt;/li&gt;
&lt;li&gt;Measure durations with a &lt;strong&gt;monotonic&lt;/strong&gt; clock, not wall time.&lt;/li&gt;
&lt;li&gt;Treat time zones/DST/calendar arithmetic as &lt;strong&gt;logic&lt;/strong&gt;, not formatting.&lt;/li&gt;
&lt;li&gt;Be explicit about &lt;strong&gt;timescale, epoch, and units&lt;/strong&gt;; prefer integer timestamps.&lt;/li&gt;
&lt;li&gt;Assume leap seconds exist — and that “UTC” may be implemented differently (including smearing).&lt;/li&gt;
&lt;li&gt;Don’t assume timestamps provide correct distributed ordering.&lt;/li&gt;
&lt;li&gt;For serious systems, treat time like a dependency: define trust, monitor offset/jitter, plan failure behavior.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These rules are defaults for most systems. High-precision setups add stricter constraints — we’ll get there when we talk about distributing time across machines.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>The Aha-Moment of Public-Key Encryption</title>
      <dc:creator>Dmytro Huz</dc:creator>
      <pubDate>Fri, 13 Feb 2026 12:37:30 +0000</pubDate>
      <link>https://dev.to/dmytro_huz/the-aha-moment-of-public-key-encryption-4lnl</link>
      <guid>https://dev.to/dmytro_huz/the-aha-moment-of-public-key-encryption-4lnl</guid>
      <description>&lt;p&gt;I started my deep &lt;a href="https://www.dmytrohuz.com/p/rebuilding-cryptography-from-scratch" rel="noopener noreferrer"&gt;dive into cryptography&lt;/a&gt; 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—fascinating me and motivating me to go deeper: how can a crowd of absolute strangers—over the internet, an inherently insecure medium—exchange information securely?&lt;/p&gt;

&lt;p&gt;I won’t lie: I honestly expected to find one simple answer that would “click” and give me an Aha moment. Instead, I fell into a rabbit hole. One idea led to another; one logical structure interacted with—and depended on—another. Math, history, logic, statistics. Topics and disciplines intertwined into a complicated, sophisticated ornament. No final answers—only more questions, theories, experiments, and try-and-fail stories. Stories of absolute trust and absolute failure, of elegant ideas that didn’t work, of intuition that misled, and of randomness that won. Brilliant people built brilliant solutions—some failed, then came new solutions, and new solutions again, until something finally held. The long, long, long road to security… Yes, it was a fascinating journey. And in the end, I finally understood how the philosopher’s stone of public-key encryption works.&lt;/p&gt;

&lt;p&gt;As we saw in the previous articles, the first challenge was to build a secure and reliable way for two peers—who know each other and share a secret key—to communicate. It doesn’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).&lt;/p&gt;

&lt;p&gt;The next challenge is to achieve the same goal for… strangers… who share no key at all.&lt;/p&gt;

&lt;p&gt;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—and then point you to deeper references if you’re interested in real-world usage and implementation. Or we can go further: tell me what topic you want next, and I’ll happily write about it.&lt;/p&gt;

&lt;p&gt;So, let the show begin—and please follow my hands very carefully.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;The “Aha-math” behind public-key encryption&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let’s take two prime numbers (numbers divisible only by 1 and themselves):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p = 3
q = 11
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These will be the foundation of everything that follows.&lt;/p&gt;

&lt;p&gt;Now let’s multiply them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;n = p * q = 3 * 11 = 33
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This number, n, will be our modulus.&lt;/p&gt;

&lt;p&gt;Modulo arithmetic simply means that numbers “wrap around” 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.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;result = 23 + 11 = 34
result = 34 % 33 = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because 34 divided by 33 leaves a remainder of 1.&lt;/p&gt;

&lt;p&gt;You can imagine modulo arithmetic as an infinite array that keeps repeating the same sequence of numbers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;modulo_array_33 = [0,1,2,3,...,31,32,0,1,2,3,...,31,32,0,...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So when we compute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(modulo_array_33[34])
# 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We “wrap around” and land back at 1.&lt;/p&gt;




&lt;p&gt;Next, let’s consider all numbers from 1 to 33:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we ask: how many of these numbers do &lt;strong&gt;not&lt;/strong&gt; share a common divisor with 33?&lt;/p&gt;

&lt;p&gt;In other words, how many numbers are &lt;em&gt;relatively prime&lt;/em&gt; to 33?&lt;/p&gt;

&lt;p&gt;There is a beautiful formula for that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f = (p-1)*(q-1) = (3-1)*(11-1) = 2*10 = 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So there are &lt;strong&gt;20 numbers&lt;/strong&gt; that are relatively prime to 33.&lt;/p&gt;

&lt;p&gt;This number f is known as Euler’s totient of n. It plays a central role in RSA.&lt;/p&gt;




&lt;p&gt;Now the most mystical part begins.&lt;/p&gt;

&lt;p&gt;We need to choose a number e that is relatively prime to 20.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;e = 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3 and 20 share no common factors — so this works.&lt;/p&gt;

&lt;p&gt;Now comes the heart of the magic.&lt;/p&gt;

&lt;p&gt;We need to find a number d such that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;e * d ≡ 1 (mod f)
3 * d ≡ 1 (mod 20)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are looking for a number d that, when multiplied by 3, leaves remainder 1 after division by 20.&lt;/p&gt;

&lt;p&gt;It turns out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;d = 7
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;3 * 7 = 21
21 % 20 = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;You’ll be surprised — at least, I was.&lt;/strong&gt; At this point we’ve already built everything we need for public-key encryption. Now let me show you.&lt;/p&gt;




&lt;p&gt;Let’s try it.&lt;/p&gt;

&lt;p&gt;Suppose we want to encrypt the number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;m = 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our public key is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public_key = (n, e) = (33, 3)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To encrypt:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ciphertext = m**e mod(n) = 4**3 % 33 = 64 % 33 = 31
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the ciphertext is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;31
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we decrypt using the private key d = 7:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;message = ciphertext**d mod(n) = 31**7 % 33 = 27512614111 % 33 = 4 # 0_o
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Exactly the original message.&lt;/p&gt;




&lt;p&gt;And this — in its purest, smallest, most transparent form — is how RSA works.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why does it work?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In the small examples, it almost feels like a trick: we raise a number to one power, then to another, and somehow everything “cancels out” and the original message comes back. But what’s really happening is simpler—and honestly, more beautiful. When you work modulo a number, powers don’t grow forever; they eventually start repeating. You’re operating inside a finite world, so exponentiation can’t keep producing “new” values indefinitely—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 “one full loop plus one extra step.” 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’s the same mechanism, just scaled to cycles that are unimaginably large. The math isn’t magic—it’s controlled movement inside a huge circular system, with the steps chosen so that going forward twice brings you back home.&lt;/p&gt;

&lt;p&gt;It took me a while to internalize the math behind this. If you want the deeper, more formal explanation, I highly recommend Appendix A of &lt;strong&gt;A Graduate Course in Applied Cryptography&lt;/strong&gt;: &lt;a href="https://toc.cryptobook.us/book.pdf" rel="noopener noreferrer"&gt;https://toc.cryptobook.us/book.pdf&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why is it secure?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In the examples above I deliberately used tiny numbers (like 3, 11, and 33) because that’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 “unlock” everything. In real life it’s the same exact workflow—just scaled up brutally. p and q are enormous primes (hundreds of digits), so n becomes massive. It’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’s the whole point: if you can’t factor n, you can’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.&lt;/p&gt;

&lt;p&gt;If you want a more implementation-oriented walkthrough of RSA, here’s a practical reference: &lt;a href="https://www.geeksforgeeks.org/computer-networks/rsa-algorithm-cryptography/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/computer-networks/rsa-algorithm-cryptography/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Final word&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I deliberately kept this article as small and abstract as possible—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 “nice math” into something you can safely deploy. It’s an endless rabbit hole, and it makes no sense to cram all of it into one post.&lt;/p&gt;

&lt;p&gt;What I wanted here was the quintessence: one core idea. The mechanism. The “Aha.” And I hope it landed.&lt;/p&gt;

&lt;p&gt;Thank you for staying with me for so long. With this, I’m closing my cryptography series and moving on to the next technologies.&lt;/p&gt;

&lt;p&gt;As always, I’m open to suggestions and requests—feel free to drop me a note ;)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>security</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Building Your Own MAC — Part 3: reinventing HMAC from SHA-256</title>
      <dc:creator>Dmytro Huz</dc:creator>
      <pubDate>Fri, 23 Jan 2026 19:11:10 +0000</pubDate>
      <link>https://dev.to/dmytro_huz/building-your-own-mac-part-3-reinventing-hmac-from-sha-256-eoa</link>
      <guid>https://dev.to/dmytro_huz/building-your-own-mac-part-3-reinventing-hmac-from-sha-256-eoa</guid>
      <description>&lt;p&gt;&lt;a href="https://www.dmytrohuz.com/p/building-own-mac-part-3-reinventing" rel="noopener noreferrer"&gt;In the previous article&lt;/a&gt;, we did something slightly ridiculous.&lt;/p&gt;

&lt;p&gt;We took a block cipher — a tool designed to transform one block into one block — and forced it to behave like something else.&lt;/p&gt;

&lt;p&gt;We wanted authentication.&lt;br&gt;
We needed a fixed-size tag.&lt;br&gt;
We had arbitrary-length messages.&lt;/p&gt;

&lt;p&gt;So we built a “message → block” machine out of a “block → block” primitive.&lt;/p&gt;

&lt;p&gt;It worked.&lt;/p&gt;

&lt;p&gt;We reinvented CMAC.&lt;/p&gt;

&lt;p&gt;And then an uncomfortable thought appeared:&lt;/p&gt;

&lt;p&gt;Why did we do all of that?&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;
&lt;h2&gt;
  
  
  A strange déjà vu
&lt;/h2&gt;

&lt;p&gt;Look again at the final construction from Part 2.&lt;/p&gt;

&lt;p&gt;It has this shape:&lt;br&gt;
    • arbitrary-length input&lt;br&gt;
    • processed block by block&lt;br&gt;
    • a small internal state&lt;br&gt;
    • a fixed-size output&lt;br&gt;
    • no way to reverse it&lt;br&gt;
    • sensitive to every bit of input&lt;/p&gt;

&lt;p&gt;That shape should feel familiar.&lt;/p&gt;

&lt;p&gt;Because that is exactly the shape of a hash function.&lt;/p&gt;

&lt;p&gt;So the obvious question is:&lt;/p&gt;

&lt;p&gt;If hash functions already compress messages into fixed-size values,&lt;br&gt;
why didn’t we start there?&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;
&lt;h2&gt;
  
  
  Fix attempt #1 — “Just hash with a secret”
&lt;/h2&gt;

&lt;p&gt;Let’s do what intuition suggests immediately.&lt;/p&gt;

&lt;p&gt;We want a tag.&lt;br&gt;
We have a hash function.&lt;br&gt;
We also have a secret key.&lt;/p&gt;

&lt;p&gt;So we try:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tag = SHA256(K || M)&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Or maybe:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tag = SHA256(M || K)&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
It feels clean.&lt;br&gt;
It feels simple.&lt;br&gt;
It feels much simpler than CMAC.&lt;/p&gt;

&lt;p&gt;No AES.&lt;br&gt;
No modes.&lt;br&gt;
No subkeys.&lt;br&gt;
No final-block gymnastics.&lt;/p&gt;

&lt;p&gt;Before trusting it, we do what this series is about.&lt;/p&gt;

&lt;p&gt;We break it.&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;
&lt;h2&gt;
  
  
  A reminder: how SHA-256 actually works
&lt;/h2&gt;

&lt;p&gt;SHA-256 is not a black box.&lt;/p&gt;

&lt;p&gt;Internally, it is an iterative compression machine.&lt;/p&gt;

&lt;p&gt;Here, compression does not mean “making data smaller” in the everyday sense.&lt;/p&gt;

&lt;p&gt;It means something more precise:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;a function that takes&lt;br&gt;
a fixed-size internal state&lt;br&gt;
and a fixed-size input block,&lt;br&gt;
and produces a new fixed-size state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Nothing is expanded.&lt;br&gt;
Nothing is reversible.&lt;br&gt;
Information is folded into state.&lt;/p&gt;

&lt;p&gt;Conceptually, SHA-256 looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;H0 = IV
H1 = compress(H0, block1)
H2 = compress(H1, block2)
...
output = Hn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each message block updates the state.&lt;br&gt;
The final hash is simply the last state.&lt;/p&gt;

&lt;p&gt;And that detail — that the output is the internal state — is exactly where intuition starts to lie to us.&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;
&lt;h2&gt;
  
  
  Break — length extension
&lt;/h2&gt;

&lt;p&gt;Assume the system uses:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tag = SHA256(K || M)&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
The attacker sees:&lt;br&gt;
    • the message M&lt;br&gt;
    • the tag SHA256(K || M)&lt;/p&gt;

&lt;p&gt;They do not know K.&lt;/p&gt;

&lt;p&gt;But they do know:&lt;br&gt;
    • the hash algorithm&lt;br&gt;
    • the block size&lt;br&gt;
    • the padding rules&lt;/p&gt;

&lt;p&gt;And that is enough.&lt;/p&gt;

&lt;p&gt;They can do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tag' = SHA256_continue(
          state = tag,
          data  = padding(K || M) || extra
       )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tag' = SHA256(K || M || padding || extra)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;This is a length extension attack.&lt;/p&gt;

&lt;p&gt;And it completely breaks this construction.&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;

&lt;h2&gt;
  
  
  Important lesson #1
&lt;/h2&gt;

&lt;p&gt;This is not a weakness of SHA-256.&lt;/p&gt;

&lt;p&gt;SHA-256 did exactly what it was designed to do.&lt;/p&gt;

&lt;p&gt;The failure is conceptual:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You treated a structured machine as if it were a black box.&lt;br&gt;
Hash functions expose their internal chaining state by design.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If your MAC construction allows an attacker to reuse that state, it is broken.&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix attempt #2 — “Fine. Let’s hash twice.”
&lt;/h2&gt;

&lt;p&gt;Okay.&lt;/p&gt;

&lt;p&gt;If structure leaks, let’s hide it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tag = SHA256(K || SHA256(K || M))&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
This feels safer.&lt;/p&gt;

&lt;p&gt;But pause for a moment and look at what we’re doing.&lt;/p&gt;

&lt;p&gt;We are:&lt;br&gt;
    • stacking primitives blindly&lt;br&gt;
    • hoping structure disappears&lt;br&gt;
    • having no clear argument why this works&lt;/p&gt;

&lt;p&gt;This is exactly the pattern we saw in Part 2.&lt;/p&gt;

&lt;p&gt;We are patching again.&lt;/p&gt;

&lt;p&gt;And patching never survives contact with attackers.&lt;/p&gt;

&lt;p&gt;So let’s reset properly.&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;
&lt;h2&gt;
  
  
  Define the problem correctly (again)
&lt;/h2&gt;

&lt;p&gt;From everything we learned so far, a real hash-based MAC must guarantee:&lt;br&gt;
    1.  Only someone with the key can compute a valid tag&lt;br&gt;
    2.  Only someone with the key can verify a valid tag&lt;br&gt;
    3.  The message cannot be extended or truncated&lt;br&gt;
    4.  The internal hash state cannot be reused&lt;br&gt;
    5.  Variable-length messages must be safe by design&lt;/p&gt;

&lt;p&gt;So the real question is not:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“How do we mix a key into a hash?”&lt;br&gt;
The real question is:&lt;/p&gt;

&lt;p&gt;How do we prevent the attacker from continuing the hash computation?&lt;br&gt;
⸻&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  The key insight — control the boundaries
&lt;/h2&gt;

&lt;p&gt;The mistake so far was mixing everything into one stream:&lt;br&gt;
    • key&lt;br&gt;
    • message&lt;br&gt;
    • finalization&lt;/p&gt;

&lt;p&gt;That gave the attacker something extendable.&lt;/p&gt;

&lt;p&gt;So we separate roles.&lt;/p&gt;

&lt;p&gt;What if:&lt;br&gt;
    • the key is mixed before the message&lt;br&gt;
    • the message is fully compressed&lt;br&gt;
    • the key is mixed again after&lt;/p&gt;

&lt;p&gt;So the attacker never sees a reusable internal state.&lt;/p&gt;

&lt;p&gt;The structure becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;inner = SHA256( (K ⊕ ipad) || M )
tag   = SHA256( (K ⊕ opad) || inner )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⸻&lt;/p&gt;

&lt;h2&gt;
  
  
  What are ipad and opad?
&lt;/h2&gt;

&lt;p&gt;At first glance, ipad and opad look like magic constants.&lt;/p&gt;

&lt;p&gt;They are not.&lt;/p&gt;

&lt;p&gt;They serve one very specific purpose: domain separation.&lt;br&gt;
    • ipad (inner padding) is the byte 0x36 repeated to the hash block size&lt;br&gt;
    • opad (outer padding) is the byte 0x5c repeated to the hash block size&lt;/p&gt;

&lt;p&gt;They ensure that:&lt;br&gt;
    • the inner hash and the outer hash live in different domains&lt;br&gt;
    • no internal hash state can be reused across phases&lt;br&gt;
    • the structure of the computation is explicit and unambiguous&lt;/p&gt;

&lt;p&gt;In simple terms:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;ipad and opad make it cryptographically impossible to confuse&lt;br&gt;
“hashing a message” with “finalizing a tag”.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;They are not there for randomness.&lt;/p&gt;

&lt;p&gt;They are there to make structure unforgeable.&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;
&lt;h2&gt;
  
  
  Why this construction survives
&lt;/h2&gt;

&lt;p&gt;Let’s stress it the same way we stressed everything else.&lt;br&gt;
    • Can the attacker extend the message?&lt;br&gt;
No — the inner hash is finalized before the outer hash begins.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;• Can they reuse an internal state?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;No — the state is never exposed in a usable form.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;• Can they fake a tag without the key?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;No — both passes depend on secret key material.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;• Does variable message length matter?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;No — the hash function already handles it safely.&lt;/p&gt;

&lt;p&gt;This construction doesn’t feel clever.&lt;/p&gt;

&lt;p&gt;It feels inevitable.&lt;/p&gt;

&lt;p&gt;Exactly like CMAC did once all constraints were visible.&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;
&lt;h2&gt;
  
  
  Name reveal: HMAC
&lt;/h2&gt;

&lt;p&gt;At this point, we can finally say the name.&lt;/p&gt;

&lt;p&gt;The construction we just derived is called:&lt;/p&gt;

&lt;p&gt;HMAC — Hash-based Message Authentication Code&lt;/p&gt;

&lt;p&gt;And just like with CMAC, the name is the least interesting part.&lt;/p&gt;

&lt;p&gt;The design came first.&lt;br&gt;
The name came later.&lt;/p&gt;

&lt;p&gt;That’s how real primitives are born.&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;
&lt;h2&gt;
  
  
  Python implementation (SHA-256 + HMAC)
&lt;/h2&gt;

&lt;p&gt;As before, we use a library for the primitive and write the logic ourselves.&lt;/p&gt;

&lt;p&gt;We are not re-implementing SHA-256 bit by bit.&lt;br&gt;
We are making the structure explicit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import hashlib
import hmac

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

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

    # Step 1: normalize the key
    if len(key) &amp;gt; block_size:
        key = sha256(key)
    if len(key) &amp;lt; block_size:
        key = key + b"\x00" * (block_size - len(key))

    # Step 2: define inner and outer pads
    ipad = bytes([0x36] * block_size)
    opad = bytes([0x5c] * block_size)

    # Step 3: inner hash
    inner = sha256(bytes(k ^ i for k, i in zip(key, ipad)) + message)

    # Step 4: outer hash (final tag)
    tag = sha256(bytes(k ^ o for k, o in zip(key, opad)) + inner)

    return tag

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⸻&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing the implementation
&lt;/h2&gt;

&lt;p&gt;A MAC is useless if you can’t verify it.&lt;/p&gt;

&lt;p&gt;So we test our implementation against Python’s standard library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If this assertion passes, the implementation is correct.&lt;/p&gt;

&lt;p&gt;No hand-waving.&lt;br&gt;
No “it seems to work”.&lt;/p&gt;

&lt;p&gt;Just a hard yes or no.&lt;/p&gt;

&lt;p&gt;⸻&lt;/p&gt;

&lt;h2&gt;
  
  
  Final symmetry
&lt;/h2&gt;

&lt;p&gt;Zoom out one last time.&lt;/p&gt;

&lt;p&gt;In this series, we built two MACs from scratch:&lt;br&gt;
    • CMAC — built from a block cipher&lt;br&gt;
    • HMAC — built from a hash function&lt;/p&gt;

&lt;p&gt;Different primitives.&lt;br&gt;
Same constraints.&lt;/p&gt;

&lt;p&gt;And in both cases, the path was identical:&lt;br&gt;
    • intuition failed&lt;br&gt;
    • naive fixes broke&lt;br&gt;
    • constraints emerged&lt;br&gt;
    • structure followed&lt;br&gt;
    • names came last&lt;/p&gt;

&lt;p&gt;Once you see the constraints, the designs stop looking arbitrary.&lt;/p&gt;

&lt;p&gt;They look inevitable.&lt;/p&gt;

&lt;p&gt;And that was the real goal of this series.&lt;/p&gt;

&lt;p&gt;Not to teach you how to use MACs.&lt;/p&gt;

&lt;p&gt;But to teach you how to recognize when a construction makes sense —&lt;br&gt;
and when intuition is lying to you again.&lt;/p&gt;

</description>
      <category>security</category>
      <category>programming</category>
      <category>learning</category>
      <category>development</category>
    </item>
    <item>
      <title>Building Own MAC — Part 2: Fixing AES (and accidentally reinventing CMAC)</title>
      <dc:creator>Dmytro Huz</dc:creator>
      <pubDate>Mon, 19 Jan 2026 20:44:42 +0000</pubDate>
      <link>https://dev.to/dmytro_huz/building-own-mac-part-2-fixing-aes-and-accidentally-reinventing-cmac-p17</link>
      <guid>https://dev.to/dmytro_huz/building-own-mac-part-2-fixing-aes-and-accidentally-reinventing-cmac-p17</guid>
      <description>&lt;p&gt;In the previous series we finished &lt;a href="https://www.dmytrohuz.com/p/building-own-block-cipher-part-3" rel="noopener noreferrer"&gt;AES and its modes&lt;/a&gt;. And in the previous article revealed why it &lt;a href="https://www.dmytrohuz.com/p/building-own-mac-part-1-encrypted" rel="noopener noreferrer"&gt;is still not secure&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We can encrypt messages.&lt;br&gt;
We can decrypt messages.&lt;br&gt;
We can ship.&lt;/p&gt;

&lt;p&gt;And then reality does what reality always does:&lt;/p&gt;

&lt;p&gt;It slaps you.&lt;/p&gt;

&lt;p&gt;Because encryption solves &lt;strong&gt;one&lt;/strong&gt; problem:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“If you don’t know the key, you can’t read the message.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It does &lt;strong&gt;not&lt;/strong&gt; solve this problem:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“If you don’t know the key, you can’t change the message.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So we face the classic situation:&lt;/p&gt;

&lt;p&gt;✅ we have secrecy&lt;/p&gt;

&lt;p&gt;❌ we still don’t have trust&lt;/p&gt;

&lt;p&gt;And now we do what every engineer does when something breaks:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;we try to patch it fast.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s go through the exact fixes your brain naturally generates at 2am.&lt;/p&gt;

&lt;p&gt;Most of them fail.&lt;/p&gt;

&lt;p&gt;Some fail &lt;em&gt;spectacularly&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;And each failure forces one new design constraint… until we reinvent a real solution.&lt;/p&gt;


&lt;h2&gt;
  
  
  &lt;strong&gt;Goal (simple and brutal)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;We want the receiver to be able to say:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ accept&lt;/li&gt;
&lt;li&gt;❌ reject&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;based on &lt;strong&gt;one small extra value&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;No philosophy. No “maybe”.&lt;/p&gt;

&lt;p&gt;Just: &lt;em&gt;does this message deserve to exist?&lt;/em&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  Fix attempt #1 — “Just hash the plaintext”
&lt;/h2&gt;

&lt;p&gt;Classic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C   = Enc(M)
tag = Hash(M)
send (C, tag)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Receiver decrypts &lt;code&gt;C&lt;/code&gt;, recomputes &lt;code&gt;Hash(M)&lt;/code&gt;, compares.&lt;/p&gt;

&lt;h3&gt;
  
  
  Break (instant)
&lt;/h3&gt;

&lt;p&gt;Hashes have no secrets.&lt;/p&gt;

&lt;p&gt;Attacker changes message → attacker recomputes hash → sends new pair.&lt;/p&gt;

&lt;p&gt;✅ receiver accepts&lt;/p&gt;

&lt;p&gt;✅ attacker wins&lt;/p&gt;

&lt;p&gt;✅ we learn nothing except pain&lt;/p&gt;

&lt;h3&gt;
  
  
  Takeaway
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;If the attacker can compute your tag, it’s not authentication.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It’s a checksum.&lt;/p&gt;

&lt;p&gt;So the tag must involve a secret.&lt;/p&gt;




&lt;h2&gt;
  
  
  Fix attempt #1.5 — “Put the hash inside the encrypted message”
&lt;/h2&gt;

&lt;p&gt;Okay, fine.&lt;/p&gt;

&lt;p&gt;Let’s hide the hash.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;payload = M || Hash(M)
C = Enc(payload)
send C

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Receiver decrypts, extracts &lt;code&gt;M&lt;/code&gt; and the embedded hash, recomputes, compares.&lt;/p&gt;

&lt;p&gt;This feels smart.&lt;/p&gt;

&lt;p&gt;It’s not.&lt;/p&gt;

&lt;h3&gt;
  
  
  Break (modes bite you)
&lt;/h3&gt;

&lt;p&gt;In malleable modes (CTR / OFB / CFB), the attacker can flip bits in ciphertext to flip predictable bits in plaintext.&lt;/p&gt;

&lt;p&gt;So they flip bits in the message part…&lt;/p&gt;

&lt;p&gt;and flip corresponding bits in the embedded hash part.&lt;/p&gt;

&lt;p&gt;They don’t need to know the key.&lt;/p&gt;

&lt;p&gt;They don’t need to know the hash function.&lt;/p&gt;

&lt;p&gt;They don’t need to understand anything.&lt;/p&gt;

&lt;p&gt;They just move bits.&lt;/p&gt;

&lt;p&gt;Receiver decrypts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;M' || Hash(M')

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hash matches. Receiver accepts.&lt;/p&gt;

&lt;p&gt;0_o&lt;/p&gt;

&lt;h3&gt;
  
  
  Takeaway
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Hiding a checker does not make it a check.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If your “verification data” lives inside the thing being protected, it can be modified together with it.&lt;/p&gt;

&lt;p&gt;We need the tag to be &lt;em&gt;outside&lt;/em&gt; the encrypted payload and unforgeable.&lt;/p&gt;




&lt;h2&gt;
  
  
  Fix attempt #2 — “Encrypt the hash separately”
&lt;/h2&gt;

&lt;p&gt;Next idea:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C   = Enc(M)
tag = Enc(Hash(M))
send (C, tag)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the attacker can’t see the hash, can’t recompute it.&lt;/p&gt;

&lt;p&gt;So… done?&lt;/p&gt;

&lt;p&gt;Not even close.&lt;/p&gt;

&lt;h3&gt;
  
  
  Break (you verified… what exactly?)
&lt;/h3&gt;

&lt;p&gt;Now you have two encrypted blobs.&lt;/p&gt;

&lt;p&gt;And the receiver has to answer a painful question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;What exactly are we proving by comparing these?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do we verify the plaintext?&lt;/li&gt;
&lt;li&gt;The ciphertext?&lt;/li&gt;
&lt;li&gt;The padding?&lt;/li&gt;
&lt;li&gt;The length?&lt;/li&gt;
&lt;li&gt;The context (endpoint / protocol version / message type)?&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Nothing is bound. Everything is implied.&lt;/p&gt;

&lt;p&gt;And implied security is not security.&lt;/p&gt;

&lt;p&gt;Also: even if you try to “compare decrypted hash to recomputed hash”, you’re back to the earlier issue — encryption modes are malleable and protocol context is not bound.&lt;/p&gt;

&lt;h3&gt;
  
  
  Takeaway
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Encrypting a checker is not the same as verifying a message.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We need a &lt;em&gt;single verification value&lt;/em&gt;, not two blobs that “seem related”.&lt;/p&gt;




&lt;h2&gt;
  
  
  Fix attempt #2.5 — “Encrypt the ciphertext and compare”
&lt;/h2&gt;

&lt;p&gt;Okay. Let’s remove ambiguity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C   = Enc(M)
tag = Enc(C)
send (C, tag)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Receiver decrypts tag → gets &lt;code&gt;C'&lt;/code&gt; → compares &lt;code&gt;C' == C&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now we have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a secret&lt;/li&gt;
&lt;li&gt;a deterministic check&lt;/li&gt;
&lt;li&gt;a clean yes/no&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This looks decent.&lt;/p&gt;

&lt;p&gt;And it still fails.&lt;/p&gt;

&lt;h3&gt;
  
  
  Break (you authenticated bytes, not meaning)
&lt;/h3&gt;

&lt;p&gt;This check proves exactly one thing:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“The ciphertext blob wasn’t modified.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It does &lt;strong&gt;not&lt;/strong&gt; prove:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;that this ciphertext belongs to &lt;em&gt;this&lt;/em&gt; endpoint&lt;/li&gt;
&lt;li&gt;that it’s intended for &lt;em&gt;this&lt;/em&gt; message type&lt;/li&gt;
&lt;li&gt;that it’s valid &lt;em&gt;in this context&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;that it’s fresh&lt;/li&gt;
&lt;li&gt;that it’s not a replay&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the attacker doesn’t need to forge anything.&lt;/p&gt;

&lt;p&gt;They just &lt;strong&gt;replay&lt;/strong&gt; a valid &lt;code&gt;(C, tag)&lt;/code&gt; where it causes damage.&lt;/p&gt;

&lt;p&gt;“Transfer $10” becomes “Transfer $10 again.”&lt;/p&gt;

&lt;p&gt;Or becomes “Approve something you approved yesterday.”&lt;/p&gt;

&lt;p&gt;You built a very strong proof of internal consistency… and zero proof of intent.&lt;/p&gt;

&lt;h3&gt;
  
  
  Takeaway
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Consistency is not authenticity.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Authentication must bind &lt;em&gt;meaning and context&lt;/em&gt;, not just bytes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Fix attempt #3 — “Use a ciphertext artifact as the tag”
&lt;/h2&gt;

&lt;p&gt;Another idea people try:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Maybe the last ciphertext block depends on everything. Let’s use it.”&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;C   = Enc(M)
tag = last_block(C)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Break (structural)
&lt;/h3&gt;

&lt;p&gt;This “tag” depends on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;mode internals&lt;/li&gt;
&lt;li&gt;padding&lt;/li&gt;
&lt;li&gt;message length&lt;/li&gt;
&lt;li&gt;where the last block boundary falls&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Truncation, extension, prefixes… the whole thing becomes ambiguous.&lt;/p&gt;

&lt;h3&gt;
  
  
  Takeaway
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Artifacts are not tags.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We need a tag function that we control, end-to-end.&lt;/p&gt;




&lt;h2&gt;
  
  
  Fix attempt #4 — “Fine. Let’s build a tag ourselves.”
&lt;/h2&gt;

&lt;p&gt;At this point it’s pretty clear that all “attach something and encrypt it” hacks are cursed.&lt;/p&gt;

&lt;p&gt;So let’s stop patching symptoms and define what we actually need.&lt;/p&gt;

&lt;p&gt;We need a function that takes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a secret key &lt;code&gt;K&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;a message &lt;code&gt;M&lt;/code&gt; of &lt;strong&gt;any length&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and produces:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;strong&gt;fixed-size tag&lt;/strong&gt; (something like 16 bytes)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So not:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Block → Block (that’s what AES gives us)&lt;/li&gt;
&lt;li&gt;Message → Message (that’s what modes give us)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;but:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Message → Block&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And yes, AES still sounds useful, because it’s keyed and strong.&lt;/p&gt;

&lt;p&gt;The only problem is… AES doesn’t speak “message”. It speaks “one block”.&lt;/p&gt;

&lt;p&gt;So we have to build a “message-to-block machine” out of “block-to-block”.&lt;/p&gt;

&lt;p&gt;Which means: &lt;strong&gt;state&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We invent a small internal value &lt;code&gt;X&lt;/code&gt; (one AES block), and we feed the message block-by-block:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;start with some known initial state &lt;code&gt;X0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;combine the current block with the state&lt;/li&gt;
&lt;li&gt;run AES&lt;/li&gt;
&lt;li&gt;repeat&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most natural combine operation is XOR (it keeps the size).&lt;/p&gt;

&lt;p&gt;So the first honest design becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;X0 = 0
for each block Bi in message:
    Xi = AES(K, Xi-1 XOR Bi)
tag = Xn

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the first time we’re no longer “encrypting a message”.&lt;/p&gt;

&lt;p&gt;We’re doing something else:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the output is fixed-size no matter how long the message is&lt;/li&gt;
&lt;li&gt;you can’t decrypt the tag back into the message&lt;/li&gt;
&lt;li&gt;we are folding the message into a state&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s not encryption. That’s &lt;strong&gt;compression&lt;/strong&gt; (in the cryptographic sense).&lt;/p&gt;

&lt;p&gt;Now: does it work?&lt;/p&gt;

&lt;p&gt;It works &lt;em&gt;almost&lt;/em&gt; perfectly… until you remember messages are not fixed-length.&lt;/p&gt;

&lt;p&gt;And the moment message length varies, this construction starts bleeding&lt;/p&gt;

&lt;p&gt;Now: break it.&lt;/p&gt;

&lt;h3&gt;
  
  
  The break: variable-length messages
&lt;/h3&gt;

&lt;p&gt;This construction is essentially “CBC-MAC”.&lt;/p&gt;

&lt;p&gt;It works for fixed-length messages.&lt;/p&gt;

&lt;p&gt;It breaks for variable length.&lt;/p&gt;

&lt;p&gt;Reason: the output tag is a valid internal state, so extension/splicing tricks become possible unless you bind the message length / finalization rules.&lt;/p&gt;

&lt;p&gt;(And yes, this is a known and very real class of failures: &lt;em&gt;CBC-MAC on variable-length messages is insecure&lt;/em&gt;.)&lt;/p&gt;

&lt;h3&gt;
  
  
  Takeaway
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;The end of the message must be cryptographically bound.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We must make “this is the final block” unforgeable.&lt;/p&gt;




&lt;h2&gt;
  
  
  Fix attempt #5 — “Fix the ending (this is where real engineering starts)”
&lt;/h2&gt;

&lt;p&gt;So what exactly breaks in Fix #4?&lt;/p&gt;

&lt;p&gt;Not the chaining itself.&lt;/p&gt;

&lt;p&gt;The break is &lt;strong&gt;the ending&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;messages can end on a block boundary or not&lt;/li&gt;
&lt;li&gt;messages can have different lengths&lt;/li&gt;
&lt;li&gt;the final state of one message must not become a reusable internal state for another&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So we add finalization rules that make “this is the end” cryptographically real.&lt;/p&gt;

&lt;p&gt;Here is the mental model (extended pseudocode):&lt;/p&gt;

&lt;h3&gt;
  
  
  Step A — Generate two subkeys (K1, K2)
&lt;/h3&gt;

&lt;p&gt;We derive subkeys from AES itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;L  = AES(K,0^128)
K1 = dbl(L)
K2 = dbl(K1)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Where &lt;code&gt;dbl()&lt;/code&gt; is “shift-left-by-1-bit, and if a carry falls off, XOR a constant (Rb) into the last byte”.&lt;/p&gt;

&lt;p&gt;This is not random ceremony — it gives us two distinct “domains” for the last block.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step B — Split message into blocks
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;B1..B(n-1),last = split_into_16B_blocks(M)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now two cases:&lt;/p&gt;

&lt;h3&gt;
  
  
  Case 1 — last block is FULL (exactly 16 bytes)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;M_last = last XOR K1

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Case 2 — last block is PARTIAL (0..15 bytes)
&lt;/h3&gt;

&lt;p&gt;Pad it first (append 0x80 then zeros), then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;last_padded = pad_7816_4(last)
M_last      = last_padded XOR K2

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step C — Run the chaining as before, but finalize with M_last
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;X = 0^128
for i in 1..(n-1):
    X = AES(K, X XOR Bi)

tag = AES(K, X XOR M_last)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s the “fixed ending” logic in full.&lt;/p&gt;

&lt;h3&gt;
  
  
  A very important clarification: there is nothing to decrypt here
&lt;/h3&gt;

&lt;p&gt;At this point, it’s worth stopping for a second and being explicit.&lt;/p&gt;

&lt;p&gt;The output of Fix attempt #5 — the &lt;strong&gt;tag&lt;/strong&gt; — is &lt;strong&gt;not encrypted data&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It is:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;not reversible&lt;/li&gt;
&lt;li&gt;not meant to be decrypted&lt;/li&gt;
&lt;li&gt;not carrying the message inside it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is the &lt;strong&gt;final internal state&lt;/strong&gt; of a compression process.&lt;/p&gt;

&lt;p&gt;Verification works like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The receiver already has the message &lt;code&gt;M&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The receiver recomputes the tag &lt;strong&gt;from scratch&lt;/strong&gt; using the same algorithm and the same key&lt;/li&gt;
&lt;li&gt;The receiver compares the two tags&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If they match → accept&lt;/p&gt;

&lt;p&gt;If they don’t → reject&lt;/p&gt;

&lt;p&gt;There is no “decryption step” for the tag, because &lt;strong&gt;authentication is not a transformation — it’s a decision&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is a crucial mental shift:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Encryption answers: “What was the message?”&lt;/p&gt;

&lt;p&gt;MAC answers: &lt;em&gt;“Can I trust this message?”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Trying to “decrypt a MAC” is like trying to “decrypt a checksum”.&lt;/p&gt;

&lt;p&gt;There is nothing there to recover.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you feel uncomfortable that the tag can’t be decrypted — good. That discomfort means you’ve stopped thinking about authentication as encryption.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Name reveal: &lt;strong&gt;CMAC&lt;/strong&gt; (and what we accidentally reinvented)
&lt;/h2&gt;

&lt;p&gt;So what did we just build?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;We built a &lt;strong&gt;compression function&lt;/strong&gt;: message → fixed-size tag&lt;/p&gt;

&lt;p&gt;(not zip compression — cryptographic compression: folding data into state)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We built &lt;strong&gt;chaining&lt;/strong&gt;: a state that evolves block-by-block.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We built a &lt;strong&gt;CBC-style MAC core&lt;/strong&gt;: &lt;code&gt;X = AES(K, X XOR Bi)&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We discovered the “variable length” landmine, and fixed it with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;finalization&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;domain separation&lt;/strong&gt; for the last block (full vs partial)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;subkeys&lt;/strong&gt; &lt;code&gt;K1&lt;/code&gt;, &lt;code&gt;K2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;padding&lt;/strong&gt; for the partial last block&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;And once you assemble those pieces, the whole thing has a name:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;CMAC — Cipher-based Message Authentication Code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;CMAC is what remains after you remove all the broken variants.&lt;/p&gt;

&lt;h2&gt;
  
  
  Python implementation (AES-CMAC)
&lt;/h2&gt;

&lt;p&gt;We’ll use an existing crypto library (because we are not trying to spend 3 weeks reimplementing AES here). Here you can find the final code: &lt;a href="https://github.com/DmytroHuzz/build_own_mac/blob/main/cmac.py" rel="noopener noreferrer"&gt;Build Own CMAC&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  One last observation (and the bridge to Part 3)
&lt;/h2&gt;

&lt;p&gt;Look at what we built.&lt;/p&gt;

&lt;p&gt;This tag function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;takes arbitrary-length input&lt;/li&gt;
&lt;li&gt;updates a small internal state block by block&lt;/li&gt;
&lt;li&gt;produces a fixed-size output&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is… suspiciously familiar.&lt;/p&gt;

&lt;p&gt;It looks like the shape of a hash function.&lt;/p&gt;

&lt;p&gt;So in the next article we’ll do the same trick again:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Instead of forcing AES to behave like a compressor,&lt;/p&gt;

&lt;p&gt;let’s start from a primitive that is &lt;em&gt;already&lt;/em&gt; a compressor.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And we’ll see if we can reinvent a hash-based MAC in the same “no names until the end” style.&lt;/p&gt;

</description>
      <category>security</category>
      <category>programming</category>
      <category>learning</category>
      <category>development</category>
    </item>
  </channel>
</rss>
