<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>cryptography on smarx.com</title>
    <link>https://smarx.com/tags/cryptography/</link>
    <description>Recent content on smarx.com</description>
    <language>en-us</language>
    <managingEditor>smarx@smarx.com (Steve Marx)</managingEditor>
    <webMaster>smarx@smarx.com (Steve Marx)</webMaster>
    <lastBuildDate>Fri, 18 Sep 2020 20:44:21 -0400</lastBuildDate>
    
        <atom:link href="https://smarx.com/tags/cryptography/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Convergent Encryption and Why No One Uses It</title>
      <link>https://smarx.com/posts/2020/09/convergent-encryption-and-why-no-one-uses-it/</link>
      <pubDate>Fri, 18 Sep 2020 20:44:21 -0400</pubDate>
      <author>smarx@smarx.com (Steve Marx)</author>
      <guid>https://smarx.com/posts/2020/09/convergent-encryption-and-why-no-one-uses-it/</guid>
      <description>&lt;p&gt;&amp;ldquo;Convergent encryption&amp;rdquo; allows for end-to-end encryption with server-side deduplication across all users&amp;rsquo; data.&lt;/p&gt;
&lt;p&gt;The first time I heard about this, it sounded impossible. End-to-end encryption means I encrypt the file before sending it anywhere. The idea of deduplication is to only store a single copy of each file, but encrypted files from different users should be unique!&lt;/p&gt;
&lt;p&gt;Convergent encryption solves this seemingly impossible problem, but it has some major drawbacks that prevent adoption.&lt;/p&gt;





    




    


&lt;figure class=&#34;flex flex-col items-center&#34;&gt;
    
    &lt;a href=&#34;https://smarx.com/posts/2020/09/convergent-encryption-and-why-no-one-uses-it/convergent-featured-cover.jpg&#34; data-lightbox=&#34;128b38a2f0d2c5634ced14046db1bf67&#34; data-title=&#34;graphic by davidzydd&#34; &gt;
    
    &lt;img class=&#34;mx-auto&#34; src=&#34;https://smarx.com/posts/2020/09/convergent-encryption-and-why-no-one-uses-it/convergent-featured-cover_hu0cb52ef75735ba7d146dba5f4c3f17df_2771813_320x0_resize_q75_lanczos.jpg&#34; width=&#34;320&#34; height=&#34;320&#34;&gt;
    
    &lt;/a&gt;
    
    
    &lt;figcaption&gt;
        &lt;p&gt;graphic by &lt;a href=&#34;https://www.freepik.com/davidzydd&#34;&gt;davidzydd&lt;/a&gt;&lt;/p&gt;
    &lt;/figcaption&gt;
    
&lt;/figure&gt;

&lt;h2 id=&#34;how-to-converge-on-a-single-encrypted-file&#34;&gt;How to &amp;ldquo;converge&amp;rdquo; on a single encrypted file&lt;/h2&gt;
&lt;p&gt;If you already know how convergent encryption works, you can skip this section. But for everyone else, it&amp;rsquo;s fun to reinvent it. Here are the high-level goals:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We want &lt;strong&gt;end-to-end encryption&lt;/strong&gt;, which means each user encrypts their files before uploading them to a shared storage service. Only the user (or users!) who encrypted the file should be able to decrypt it.&lt;/li&gt;
&lt;li&gt;We want &lt;strong&gt;server-side deduplication&lt;/strong&gt;. If two users encrypt the same file, the result of their encryption needs to be byte-for-byte identical. The server can then safely store only one copy of the file.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For multiple users to encrypt a file and arrive at an identical result, they must use the same encryption key. That key, then, has to be derived from something only those users know. So what do users who possess a given file know that other users don&amp;rsquo;t?&lt;/p&gt;
&lt;p&gt;The answer is the content of the file! The trick is to encrypt the file with the hash of its content:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;enc(m) = E(H(m), m)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;E(k, m)&lt;/code&gt; is a symmetric encryption algorithm such as AES that encrypts message &lt;code&gt;m&lt;/code&gt; with key &lt;code&gt;k&lt;/code&gt;, and &lt;code&gt;H&lt;/code&gt; is a cryptographic hash function like SHA-1.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Any user who has the message/file &lt;code&gt;m&lt;/code&gt; will end up with the same encrypted version. Those users will have no trouble decrypting the file later, but no one else will be able to learn the file&amp;rsquo;s content.&lt;/p&gt;
&lt;h2 id=&#34;attacks&#34;&gt;Attacks&lt;/h2&gt;
&lt;p&gt;The security of a system can only be analyzed in the context of well-defined goals. I&amp;rsquo;d say that for end-to-end file encryption, I want to make sure that no one, including the storage service I&amp;rsquo;m using, knows the content of the files I&amp;rsquo;ve stored. I&amp;rsquo;ll accept some metadata leakage. Specifically, it&amp;rsquo;s okay with me if file sizes are leaked.&lt;/p&gt;
&lt;p&gt;With that in mind, there are two attacks on convergent encryption: the &amp;ldquo;confirmation of file&amp;rdquo; attack and the &amp;ldquo;learn the remaining information&amp;rdquo; attack.&lt;/p&gt;
&lt;h3 id=&#34;confirmation-of-file&#34;&gt;Confirmation of file&lt;/h3&gt;
&lt;p&gt;When I upload a file to a storage provider, they know the content of the file I&amp;rsquo;ve uploaded. If it&amp;rsquo;s encrypted, they know the ciphertext, but they shouldn&amp;rsquo;t know the plaintext.&lt;/p&gt;
&lt;p&gt;With convergent encryption, the ciphertext for a given plaintext is always the same. Suppose I&amp;rsquo;ve obtained a bootleg copy of the upcoming film &lt;a href=&#34;https://www.imdb.com/title/tt2850386/&#34;&gt;The Croods: A New Age&lt;/a&gt;. Let&amp;rsquo;s also suppose that the storage service I&amp;rsquo;m using has the same file. (Perhaps the MPAA asked them to be on the lookout for it.)&lt;/p&gt;
&lt;p&gt;If the storage service and I both encrypt that file with convergent encryption, we&amp;rsquo;ll end up with the same result. When I upload my copy of the encrypted file, the storage provider will know that I&amp;rsquo;ve gotten an illegal sneak peek at the most anticipated film of 2020. This is called the &amp;ldquo;confirmation of file&amp;rdquo; attack.&lt;/p&gt;
&lt;h3 id=&#34;learn-the-remaining-information&#34;&gt;Learn the remaining information&lt;/h3&gt;
&lt;p&gt;The &amp;ldquo;confirmation of file&amp;rdquo; attack was well understood in the 1990s when convergent encryption first emerged, but it wasn&amp;rsquo;t until 2008 that the &amp;ldquo;learn the remaining information&amp;rdquo; attack was described.&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Suppose I&amp;rsquo;ve rolled my own password manager, and I store my unencrypted passwords in a local Redis database.&lt;sup id=&#34;fnref:3&#34;&gt;&lt;a href=&#34;#fn:3&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;3&lt;/a&gt;&lt;/sup&gt; I know that a malicious web page could potentially &lt;a href=&#34;https://blog.bugreplay.com/2017/05/for-users-of-redis-running-locally-can-be-dangerous.html&#34;&gt;exfiltrate my local Redis database&lt;/a&gt;, so I&amp;rsquo;ve &lt;a href=&#34;https://redis.io/topics/config&#34;&gt;configured Redis&lt;/a&gt; to require a password. That&amp;rsquo;s the only change I made to the standard Redis configuration.&lt;/p&gt;
&lt;p&gt;If I encrypt that Redis config file with convergent encryption and someone (e.g. my storage provider) obtains a copy of that encrypted file, they can perform an offline attack to determine my password. All they need to do is grab the original Redis config file and keep plugging in different passwords until they get a match.&lt;/p&gt;
&lt;p&gt;This is much like an offline attack on a hashed password but way worse. Passwords are hashed with key derivation functions like scrypt or Argon2, which are intentionally resistant to offline attacks.&lt;sup id=&#34;fnref:4&#34;&gt;&lt;a href=&#34;#fn:4&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;4&lt;/a&gt;&lt;/sup&gt; Passwords are also &lt;em&gt;salted&lt;/em&gt;. A salt is a bit of random information that&amp;rsquo;s added to the password before hashing. If you and I both use the password &amp;ldquo;12345&amp;rdquo;&lt;sup id=&#34;fnref:5&#34;&gt;&lt;a href=&#34;#fn:5&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;5&lt;/a&gt;&lt;/sup&gt;, it will hash to two different values after salting, rendering a lookup table useless.&lt;/p&gt;
&lt;p&gt;Convergent encryption, on the other hand, is typically designed to be fast and efficient, which makes an offline attack feasible. Further, convergent encryption would no longer be convergent if it used a salt, so attackers can use pregenerated lookup tables.&lt;/p&gt;
&lt;h2 id=&#34;how-to-use-convergent-encryption-securely&#34;&gt;How to use convergent encryption securely&lt;/h2&gt;
&lt;p&gt;Sadly, the weaknesses of convergent encryption are baked in. If you want deduplication of encrypted data, you have to live with attackers being able to confirm the existence of a file and to learn a small amount of missing data with offline dictionary and brute force attacks.&lt;/p&gt;
&lt;p&gt;However, if you only care about deduplicating a single user&amp;rsquo;s data, then each user can pick their own unique key. Similarly, if you care about deduplicating across a group of users, like a company, you can pick a group-wide key and do an extra layer of encryption before sending the file to a shared storage provider.&lt;/p&gt;
&lt;h2 id=&#34;further-reading&#34;&gt;Further reading&lt;/h2&gt;
&lt;p&gt;If you&amp;rsquo;re interested in a more thorough definition and analysis of convergent encryption, I recommend &lt;a href=&#34;https://eprint.iacr.org/2012/631.pdf&#34;&gt;Message-Locked Encryption and Secure Deduplication by Bellare et al.&lt;/a&gt;&lt;/p&gt;
&lt;section class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;To get a key of the right size and to avoid pitfalls around key reuse, it&amp;rsquo;s probably a good idea to use a &lt;em&gt;key derivation function&lt;/em&gt; such as HKDF instead of just using the content hash directly. &lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:2&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;&lt;a href=&#34;https://tahoe-lafs.org/hacktahoelafs/drew_perttula.html&#34;&gt;https://tahoe-lafs.org/hacktahoelafs/drew_perttula.html&lt;/a&gt; &lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:3&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;Don&amp;rsquo;t do any of this. &lt;a href=&#34;#fnref:3&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:4&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;This is achieved by using a lot of CPU and RAM and being hard to parallelize. &lt;a href=&#34;#fnref:4&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:5&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;This is the kind of password &lt;a href=&#34;http://www.moviequotedb.com/movies/spaceballs/quote_22060.html&#34;&gt;an idiot would have on his luggage&lt;/a&gt;. &lt;a href=&#34;#fnref:5&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</description>
    </item>
    
    <item>
      <title>Privacy and the Google/Apple Exposure Notification System</title>
      <link>https://smarx.com/posts/2020/09/privacy-google-apple-ens/</link>
      <pubDate>Thu, 03 Sep 2020 22:44:03 -0400</pubDate>
      <author>smarx@smarx.com (Steve Marx)</author>
      <guid>https://smarx.com/posts/2020/09/privacy-google-apple-ens/</guid>
      <description>&lt;p&gt;Earlier this year, Google and Apple teamed up to create the Exposure Notification System (ENS)&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;, a digital contact tracing system for the COVID-19 pandemic. When someone who uses ENS is diagnosed with COVID-19, the system notifies other users who have had recent contact with the sick person.&lt;/p&gt;
&lt;p&gt;Amazingly, this is done while preserving participants&amp;rsquo; privacy.&lt;/p&gt;
&lt;p&gt;In this post, I&amp;rsquo;d like to reinvent the ENS protocol step by step. This is a good way to appreciate the system&amp;rsquo;s design and understand its privacy-preserving properties.&lt;/p&gt;
&lt;h2 id=&#34;tracking-contact-without-location-or-identity&#34;&gt;Tracking contact without location or identity&lt;/h2&gt;
&lt;p&gt;One easy way to find out who had contact with whom is to log everybody&amp;rsquo;s location on a server. If Alice and Bob were both at the same location at the same time, then when Alice is diagnosed with COVID-19, Bob should be notified. This type of system is what a lot of people imagine when they first hear about digital contact tracing.&lt;/p&gt;
&lt;p&gt;Fortunately for our privacy, location and identity are both unnecessary. For Bob to be notified that he was exposed, the system just needs to know that he came in contact &lt;strong&gt;somewhere&lt;/strong&gt; with &lt;strong&gt;someone&lt;/strong&gt; who had the disease.&lt;/p&gt;
&lt;p&gt;The idea behind ENS is to use cellphones as proxies for humans. If two cellphones are close together, that&amp;rsquo;s a good indication that their owners are also close together. The phones themselves can notice their proximity using &lt;a href=&#34;https://en.wikipedia.org/wiki/Bluetooth_Low_Energy&#34;&gt;Bluetooth Low Energy (BLE)&lt;/a&gt;, a wireless technology for local communication. If two phones can communicate via BLE, they&amp;rsquo;re close together. The signal strength can be used to guess how close.&lt;/p&gt;
&lt;p&gt;As long as the phones broadcast a randomly chosen device ID and not the real identity of the user, this also eliminates the need to tie identities to the contact events.&lt;/p&gt;
&lt;p&gt;At this point, I can propose a bad solution for exposure notification. To be clear, this is &lt;strong&gt;not&lt;/strong&gt; how ENS works, but it will serve as a starting point for discussion:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Every phone broadcasts a device ID.&lt;/li&gt;
&lt;li&gt;When a phone observes another phone&amp;rsquo;s broadcast, it sends a timestamped event to the server noting the two device IDs in contact.&lt;/li&gt;
&lt;li&gt;When a user is diagnosed with COVID-19, they send their device ID to the server.&lt;/li&gt;
&lt;li&gt;The server sends exposure notifications to all devices that had contact with the infected user&amp;rsquo;s device ID (during, say, the past 14 days).&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;pseudonymity-isnt-anonymity&#34;&gt;Pseudonymity isn&amp;rsquo;t anonymity&lt;/h2&gt;
&lt;p&gt;The above proposal uses pseudonyms in the form of the device IDs. Pseudonyms are helpful in terms of privacy, but they still have major drawbacks:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Pseudonyms still allow tracking.&lt;/strong&gt; For example, a grocery store might use a BLE beacon to keep track of device movement throughout the store.&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt; An app on your phone might use this data to offer you targeted ads, even if it doesn&amp;rsquo;t know your true identity.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pseudonyms can be stripped away.&lt;/strong&gt; A BLE beacon at the cash register could be used to associate your pseudonymous device ID with the name on your credit card. Now all the collected data for your device ID can be associated with your real identity.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is actually already a problem for cellphones, so they use rotating MAC addresses when they &lt;a href=&#34;https://en.wikipedia.org/wiki/Bluetooth_Low_Energy#Advertising_and_discovery&#34;&gt;broadcast advertising packets&lt;/a&gt;. These addresses are only around for about ten minutes at a time, so they can&amp;rsquo;t be used to correlate a user&amp;rsquo;s activity over time.&lt;sup id=&#34;fnref:3&#34;&gt;&lt;a href=&#34;#fn:3&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;h2 id=&#34;ephemeral-identities&#34;&gt;Ephemeral identities&lt;/h2&gt;
&lt;p&gt;I can improve on my straw man design by adopting the same solution as OS developers already use. Instead of a persistent device ID, I&amp;rsquo;ll use a random ID that changes every ten minutes. This gives users something a lot closer to true anonymity. If a user of such a system walked through a grocery store, they&amp;rsquo;d look to BLE beacons like a completely different person by the time they reached the cash register.&lt;/p&gt;
&lt;p&gt;Unfortunately, this change breaks the earlier design. When someone is diagnosed with COVID-19, the server needs to notify everyone who had contact with that person. But given the new ephemeral identities, there&amp;rsquo;s no way for the server to know who to notify.&lt;/p&gt;
&lt;p&gt;This actually presents an opportunity to do even better in terms of privacy. It&amp;rsquo;s possible to make this all work without telling the server about contact events at all. Here&amp;rsquo;s a new proposal, which is pretty close to how ENS actually works:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Every phone broadcasts an ephemeral ID, which is randomly chosen and changes every 10 minutes. Phones remember their past ephemeral IDs for 14 days.&lt;/li&gt;
&lt;li&gt;When a phone sees another phone&amp;rsquo;s ID, it records that ID locally, also for 14 days.&lt;/li&gt;
&lt;li&gt;When someone is diagnosed with COVID-19, they upload their recent ephemeral IDs to a server, which makes these publicly available.&lt;/li&gt;
&lt;li&gt;Every phone downloads those IDs each day and compares them with the IDs it remembers seeing. If there&amp;rsquo;s a match, the user is notified of potential exposure.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;a-pragmatic-privacy-tradeoff&#34;&gt;A pragmatic privacy tradeoff&lt;/h2&gt;
&lt;p&gt;Until this point, I&amp;rsquo;ve focused exclusively on the system&amp;rsquo;s privacy properties, in an effort to motivate the overall design. But the actual ENS protocol differs slightly from what I&amp;rsquo;ve described, and that difference has a material effect on privacy. To understand this final detail, it&amp;rsquo;s necessary to do some practical analysis.&lt;/p&gt;
&lt;h3 id=&#34;lets-do-some-arithmetic&#34;&gt;Let&amp;rsquo;s do some arithmetic&lt;/h3&gt;
&lt;p&gt;The last step of the current design is that every phone downloads every ephemeral ID for infected people. Ephemeral IDs change every 10 minutes, so there are 6 IDs per hour. Under the assumption that someone gets a diagnosis within 10 days of their earliest infection date, I&amp;rsquo;ll assume each infected user uploads 10 days worth of ephemeral IDs. That&amp;rsquo;s 6 IDs per hour × 24 hours per day × 10 days = 1,440 IDs.&lt;/p&gt;
&lt;p&gt;So how much data does each phone need to download per day? At the time I&amp;rsquo;m writing this, in the US, we&amp;rsquo;ve seen an average of about 40,000 cases per day over the past week.&lt;sup id=&#34;fnref:4&#34;&gt;&lt;a href=&#34;#fn:4&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;4&lt;/a&gt;&lt;/sup&gt; It&amp;rsquo;s easy to break this up by region or state, though, so I&amp;rsquo;ll arbitrarily say that each person needs to care about 1,000 cases per day.&lt;/p&gt;
&lt;p&gt;1,000 cases per day × 1,440 IDs per case = 1.44 million IDs to download each day. ENS uses 16-byte IDs. That size is a compromise between keeping IDs as small as possible while giving a low probability of collisions. 1.44 million IDs × 16 bytes per ID = &lt;strong&gt;23MB of data per day&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id=&#34;the-tradeoff&#34;&gt;The tradeoff&lt;/h3&gt;
&lt;p&gt;That&amp;rsquo;s a lot of data, especially for people without consistent access to WiFi. There&amp;rsquo;s not much I can do about the number of new cases per day or the number of hours in a day&lt;sup id=&#34;fnref:5&#34;&gt;&lt;a href=&#34;#fn:5&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;5&lt;/a&gt;&lt;/sup&gt;, but I &lt;em&gt;can&lt;/em&gt; change the number of ephemeral IDs that have to be uploaded.&lt;/p&gt;
&lt;p&gt;This is where the privacy tradeoff comes into play. What if ephemeral IDs only changed once per day? Then infected individuals would just upload a single 16-byte ID. That would cut the data by a factor of 144, bringing us down to a much more manageable &lt;strong&gt;160KB of data per day&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;10-minute ephemeral IDs are better for privacy, but 1-day ephemeral IDs are good for data transfer. Fortunately, there&amp;rsquo;s a way to get (almost) the best of both worlds.&lt;/p&gt;
&lt;h3 id=&#34;having-our-cake-and-eating-it-too&#34;&gt;Having our cake and eating it too&lt;/h3&gt;
&lt;p&gt;In ENS, each device picks a new random number every day, called a Temporary Exposure Key (TEK). It uses that key to generate 144 Rolling Proximity Identifiers (RPIs) by simply encrypting the timestamp at each 10-minute interval throughout the day.&lt;sup id=&#34;fnref:6&#34;&gt;&lt;a href=&#34;#fn:6&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;6&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;Importantly, if you knew the TEK from a given day, you could recompute all the RPIs. This is how we get to have our cake and eat it too. The RPIs are 10-minute ephemeral keys, but the TEKs are one-per-day. The grocery store trying to track you when you walk around the store sees new IDs every 10 minutes, but if someone gets COVID-19, they only need to upload one key per day.&lt;/p&gt;
&lt;p&gt;So far, it&amp;rsquo;s a win-win, but note that this means the &lt;strong&gt;infected person&lt;/strong&gt; is retroactively giving up some of their privacy. Once an infected user uploads their TEK for a given day, anyone can correlate all of that user&amp;rsquo;s RPIs for that day.&lt;/p&gt;
&lt;p&gt;This is an altruistic action by the infected user, who is giving up some of their privacy in order to help protect others.&lt;/p&gt;
&lt;h2 id=&#34;possible-attacks&#34;&gt;Possible attacks&lt;/h2&gt;
&lt;p&gt;I&amp;rsquo;ll wrap up by describing a couple possible attacks and how they&amp;rsquo;re mitigated (or not) by ENS.&lt;/p&gt;
&lt;h3 id=&#34;spam&#34;&gt;Spam&lt;/h3&gt;
&lt;p&gt;What would happen in the system I&amp;rsquo;ve described if someone just uploaded a bunch of made up TEKs? Everyone would have to download them all, derive RPIs from them, and check whether they&amp;rsquo;d recorded contact with those IDs. This would be a very effective denial of service attack.&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s also possible for someone to run around the city getting close to as many people as possible and then claim that they&amp;rsquo;re sick by uploading their TEK for the day. This would result in a large number of false alarm notifications, causing a bunch of unnecessary quarantining and/or reducing trust in the system.&lt;/p&gt;
&lt;p&gt;ENS deals with this by requiring a trusted entity to authorize each upload. The server rejects uploads that don&amp;rsquo;t have a digital signature from a preregistered public health authority.&lt;/p&gt;
&lt;h3 id=&#34;spoofing&#34;&gt;Spoofing&lt;/h3&gt;
&lt;p&gt;The following attack has no mitigation that I&amp;rsquo;m aware of.&lt;/p&gt;
&lt;p&gt;Any device can spoof any other by just sending out its RPI. This means you can cause contacts to be recorded that never happened.&lt;/p&gt;
&lt;p&gt;For example, you could set up a BLE beacon in two different cities. When one beacon sees an RPI, it transmits it to the other beacon, which then broadcasts the same RPI. This would create something of a wormhole effect: every device that passed one location would also appear to pass through the other location. When someone inevitably got infected, spurious exposure notifications would get generated.&lt;/p&gt;
&lt;p&gt;Setting up more beacons/repeaters would increase the scope of the attack. Fortunately, this requires physical access, so the bar is a bit higher than the (preventable) spam discussed in the previous section.&lt;/p&gt;
&lt;h2 id=&#34;summary&#34;&gt;Summary&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;The Google/Apple Exposure Notification System is designed well with privacy in mind.&lt;/li&gt;
&lt;li&gt;There&amp;rsquo;s a privacy tradeoff to keep the data transfer manageable.&lt;/li&gt;
&lt;li&gt;This tradeoff means that infected users who upload their Temporary Exposure Keys are giving up a small amount of privacy.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;further-reading&#34;&gt;Further reading&lt;/h2&gt;
&lt;p&gt;If you want to learn more about this, including the details I omitted, the best place to go is &lt;a href=&#34;https://www.google.com/covid19/exposurenotifications/&#34;&gt;Google&amp;rsquo;s Exposure Notification website&lt;/a&gt;. In particular, I encourage you to read the &lt;a href=&#34;https://blog.google/documents/69/Exposure_Notification_-_Cryptography_Specification_v1.2.1.pdf&#34;&gt;cryptography specification&lt;/a&gt; and the &lt;a href=&#34;https://google.github.io/exposure-notifications-server/&#34;&gt;reference key server documentation&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;If you want to see some more analysis of the system&amp;rsquo;s privacy implications, I recommend the EFF&amp;rsquo;s blog post &amp;ldquo;&lt;a href=&#34;https://www.eff.org/deeplinks/2020/04/apple-and-googles-covid-19-exposure-notification-api-questions-and-answers&#34;&gt;Apple and Google&amp;rsquo;s COVID-19 Exposure Notification API: Questions and Answers&lt;/a&gt;&amp;rdquo;.&lt;/p&gt;
&lt;section class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;Formerly the &amp;ldquo;Privacy-Preserving Contract Tracing Project&amp;rdquo;. &lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:2&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;This isn&amp;rsquo;t a hypothetical scenario. See the NYT&amp;rsquo;s &amp;ldquo;&lt;a href=&#34;https://www.nytimes.com/interactive/2019/06/14/opinion/bluetooth-wireless-tracking-privacy.html&#34;&gt;In Stores, Secret Surveillance Tracks Your Every Move&lt;/a&gt;&amp;rdquo;. &lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:3&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;With the help of an app on the phone, though, a persistent identity can be reestablished. &lt;a href=&#34;#fnref:3&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:4&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;Source: &lt;a href=&#34;https://www.nytimes.com/interactive/2020/us/coronavirus-us-cases.html,&#34;&gt;https://www.nytimes.com/interactive/2020/us/coronavirus-us-cases.html,&lt;/a&gt; but of course the numbers change quickly. &lt;a href=&#34;#fnref:4&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:5&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;It&amp;rsquo;s not for a lack of trying. &lt;a href=&#34;#fnref:5&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:6&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;This paragraph is a simplification of the actual cryptography involved, but not in a way that matters for understanding its privacy attributes. &lt;a href=&#34;#fnref:6&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</description>
    </item>
    
    <item>
      <title>Cracking BIP39 Seed Phrases</title>
      <link>https://smarx.com/posts/2020/08/cracking-bip39-seed-phrases/</link>
      <pubDate>Fri, 28 Aug 2020 16:32:08 -0400</pubDate>
      <author>smarx@smarx.com (Steve Marx)</author>
      <guid>https://smarx.com/posts/2020/08/cracking-bip39-seed-phrases/</guid>
      <description>&lt;p&gt;In &lt;a href=&#34;https://smarx.com/posts/2020/08/bip39-mnemonics-for-recording-long-keys/&#34;&gt;my last post about BIP39&lt;/a&gt;, I mentioned that the checksum could be used to help in case you lost a word from your seed phrase. In this post, I&amp;rsquo;ll expand on exactly how the checksum helps.&lt;/p&gt;
&lt;h2 id=&#34;algorithm-for-recovering-a-lost-word&#34;&gt;Algorithm for recovering a lost word&lt;/h2&gt;
&lt;p&gt;Because seed phrases use a finite word list, you can simply guess the missing word:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Pick a word from the word list.&lt;/li&gt;
&lt;li&gt;Insert that word where the missing word is in the phrase.&lt;/li&gt;
&lt;li&gt;Use PBKDF2 to derive a seed from the completed phrase.&lt;/li&gt;
&lt;li&gt;If that&amp;rsquo;s not the seed you&amp;rsquo;re looking for, go back to the first step.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;But we can do better! Step 3 is where the checksum can speed things up.&lt;/p&gt;
&lt;h2 id=&#34;using-the-checksum&#34;&gt;Using the checksum&lt;/h2&gt;
&lt;p&gt;The checksum can check the validity of a seed phrase. This functions as a shortcut to skip words that can&amp;rsquo;t work. Here&amp;rsquo;s the modified algorithm:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Pick a word from the word list.&lt;/li&gt;
&lt;li&gt;Insert that word where the missing word is in the phrase.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compute the checksum. If it doesn&amp;rsquo;t match, go back to the first step.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Use PBKDF2 to derive a seed from the completed phrase.&lt;/li&gt;
&lt;li&gt;If that&amp;rsquo;s not the seed you&amp;rsquo;re looking for, go back to the first step.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I&amp;rsquo;ve added a step, but it saves quite a bit of time. This is because PBKDF2 is the most expensive step in the process. The purpose of PBKDF2 with a high difficulty setting&amp;mdash;BIP39 uses 2,048 iterations of HMAC-SHA512&amp;mdash;is that it makes guessing passwords slow.&lt;/p&gt;
&lt;p&gt;For a 12-word seed phrase, the checksum is 4 bits long, so there are 2&lt;sup&gt;4&lt;/sup&gt;==16 possible checksum values. This means that, on average, only 1 out of 16 guesses will result in a matching checksum. For the rest, PBKDF2 can be skipped entirely.&lt;/p&gt;
&lt;h2 id=&#34;what-if-you-dont-know-where-the-missing-word-goes&#34;&gt;What if you don&amp;rsquo;t know where the missing word goes?&lt;/h2&gt;
&lt;p&gt;Say you wrote down your seed phrase, but it&amp;rsquo;s somehow only 11 words long. This is an invalid BIP39 seed phrase because the minimum length is 12 words. Can you recover the missing word if you don&amp;rsquo;t know where it goes?&lt;/p&gt;
&lt;p&gt;The answer is yes, but you&amp;rsquo;ll just have to try each position. There are 12 possible places the missing word could go, so it would take 12 times as long as if you already knew where the word belonged.&lt;/p&gt;
&lt;h2 id=&#34;what-if-the-checksum-itself-is-missing&#34;&gt;What if the checksum itself is missing?&lt;/h2&gt;
&lt;p&gt;You might wonder what happens if the last word of the seed phrase is the one that&amp;rsquo;s gone missing. That&amp;rsquo;s where the checksum is, so how can we use the checksum if we don&amp;rsquo;t know it?&lt;/p&gt;
&lt;p&gt;This case can be safely ignored. The checksum has to &lt;em&gt;match&lt;/em&gt; the rest of the phrase, so the validity check looks like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;checksum(&amp;lt;all but last n bits&amp;gt;) == &amp;lt;last n bits&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This same process works no matter which bits we&amp;rsquo;re guessing.&lt;/p&gt;
&lt;p&gt;However, this case does present an opportunity for optimization. Each word in the mnemonic phrase represents 11 bits of data. If I need to guess the last word in a 12-word phrase, 4 of those bits are actually the checksum. This means that instead of guessing and testing every combination of 11 bits, I can guess just 7 bits and then fill the other 4 in with the checksum. Longer phrases use more checksum bits, so this advantage gets even greater.&lt;/p&gt;
&lt;p&gt;In the code I&amp;rsquo;ll link to at the bottom of this post, I didn&amp;rsquo;t implement this optimization.&lt;/p&gt;
&lt;h2 id=&#34;what-if-you-lost-the-password&#34;&gt;What if you lost the password?&lt;/h2&gt;
&lt;p&gt;As I mentioned in my previous post, the mnemonic phrase can optionally be combined with a password in the key derivation step. If you&amp;rsquo;ve lost that password, you&amp;rsquo;re in a bit of trouble.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://en.wikipedia.org/wiki/PBKDF2&#34;&gt;PBKDF2&lt;/a&gt; is used with a parameterized difficulty to make password guessing expensive. BIP39 specifies 2,048 rounds of HMAC-SHA512. This is too slow to guess a large number of passwords. And, unlike the finite word list for seed phrases, passwords can be absolutely anything.&lt;/p&gt;
&lt;p&gt;If you can narrow your password down to a relatively small number of choices, guessing could be feasible again.&lt;/p&gt;
&lt;h2 id=&#34;implementation-and-results&#34;&gt;Implementation and results&lt;/h2&gt;
&lt;p&gt;I added seed phrase cracking functionality to &lt;a href=&#34;https://github.com/smarx/bip39&#34;&gt;my example code&lt;/a&gt;, and it shows the expected speedup from using checksums. I made no attempt to optimize the implementation itself. Notably, there&amp;rsquo;s no concurrency, so the code can&amp;rsquo;t take advantage of multiple processors.&lt;/p&gt;
&lt;p&gt;On my laptop (Pixelbook Go), guessing one missing word from a 12-word seed phrase without taking advantage of checksums takes about &lt;strong&gt;5.6 seconds&lt;/strong&gt;. When using the checksum, the same task takes only &lt;strong&gt;0.5 seconds&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;With &lt;em&gt;two&lt;/em&gt; missing words, guessing with the help of a checksum takes &lt;strong&gt;13 minutes and 15 seconds&lt;/strong&gt;. I didn&amp;rsquo;t attempt this without the checksum&amp;rsquo;s help, and I didn&amp;rsquo;t attempt to guess more than two missing words. (The algorithm&amp;rsquo;s time complexity grows exponentially with the number of missing words.)&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>BIP39: Mnemonics for Recording Long Keys</title>
      <link>https://smarx.com/posts/2020/08/bip39-mnemonics-for-recording-long-keys/</link>
      <pubDate>Wed, 26 Aug 2020 11:21:51 -0400</pubDate>
      <author>smarx@smarx.com (Steve Marx)</author>
      <guid>https://smarx.com/posts/2020/08/bip39-mnemonics-for-recording-long-keys/</guid>
      <description>&lt;p&gt;If you&amp;rsquo;ve worked with a blockchain like Bitcoin or Ethereum, you&amp;rsquo;ve probably been given a &amp;ldquo;seed phrase&amp;rdquo; or &amp;ldquo;mnemonic&amp;rdquo;, a phrase of a dozen or more words like this:&lt;/p&gt;





    




    


&lt;figure class=&#34;flex flex-col items-center&#34;&gt;
    
    &lt;a href=&#34;https://smarx.com/posts/2020/08/bip39-mnemonics-for-recording-long-keys/seed-phrase-featured-cover.jpg&#34; data-lightbox=&#34;ac890f5957a96ba99fd8048fdb895f61&#34; data-title=&#34;Yes, that&amp;amp;rsquo;s my handwriting. No, that&amp;amp;rsquo;s not my mnemonic phrase.&#34; &gt;
    
    &lt;img class=&#34;mx-auto&#34; src=&#34;https://smarx.com/posts/2020/08/bip39-mnemonics-for-recording-long-keys/seed-phrase-featured-cover_hufc3661fabac2406cb32049add9b9adbc_2637757_728x0_resize_q75_lanczos.jpg&#34; width=&#34;728&#34; height=&#34;546&#34;&gt;
    
    &lt;/a&gt;
    
    
    &lt;figcaption&gt;
        &lt;p&gt;Yes, that&amp;rsquo;s my handwriting. No, that&amp;rsquo;s not my mnemonic phrase.&lt;/p&gt;
    &lt;/figcaption&gt;
    
&lt;/figure&gt;

&lt;p&gt;This phrase can be used to recover your blockchain account, so you might imagine it&amp;rsquo;s some sort of encoding of your private key. This is pretty much right, but there&amp;rsquo;s a little more to it.&lt;/p&gt;
&lt;p&gt;This type of seed phrase is described by &lt;a href=&#34;https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki&#34;&gt;Bitcoin Improvement Proposal 39&lt;/a&gt; (BIP39), and it has a number of desirable properties for its intended use case: securing a private key, particularly using offline storage.&lt;/p&gt;
&lt;h2 id=&#34;algorithm&#34;&gt;Algorithm&lt;/h2&gt;
&lt;p&gt;The BIP39 algorithm to generate a new seed is as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Generate the desired number of bits of entropy.&lt;/li&gt;
&lt;li&gt;Append a checksum.&lt;/li&gt;
&lt;li&gt;Encode using a standard word list.&lt;/li&gt;
&lt;li&gt;Use PBKDF2 with an optional password to derive a &lt;em&gt;seed&lt;/em&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The seed can then be used directly as a private key, but in typical blockchain usage, it&amp;rsquo;s actually the seed for &lt;a href=&#34;https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki&#34;&gt;BIP32: Hierarchical Deterministic Wallets&lt;/a&gt;. BIP32 is cool in its own right, but I&amp;rsquo;m not going to describe it here.&lt;/p&gt;
&lt;h2 id=&#34;why-all-the-steps&#34;&gt;Why all the steps?&lt;/h2&gt;
&lt;p&gt;Before I get into the details of implementing BIP39, I want to take a moment to explain what these steps are doing in terms of user benefits:&lt;/p&gt;
&lt;h3 id=&#34;checksum&#34;&gt;Checksum&lt;/h3&gt;
&lt;p&gt;BIP39 was conceived as a way for users to keep their cryptocurrency holdings safe. The checksum helps in two ways:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;It protects against mistakes.&lt;/strong&gt; Typing an incorrect word is unlikely to yield a valid seed phrase because the checksum wouldn&amp;rsquo;t match. (The carefully chosen word list also helps prevent typos.)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;It serves as error correction.&lt;/strong&gt; If you write down your seed phrase on a napkin but immediately wipe mustard off of your mustache, obscuring one of the words, don&amp;rsquo;t despair! The checksum will help you recover the lost word.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;a-good-word-list&#34;&gt;A good word list&lt;/h3&gt;
&lt;p&gt;The encoding step involves translating a big long number into words. This is because it&amp;rsquo;s easier to write down or remember a phrase in your native language.&lt;/p&gt;
&lt;p&gt;The word list is carefully selected to help avoid mistakes. Similar words (like &amp;ldquo;bean&amp;rdquo; and &amp;ldquo;bear&amp;rdquo;) are avoided so sloppy handwriting isn&amp;rsquo;t an issue, and the first four letters of each word are unique.&lt;/p&gt;
&lt;h3 id=&#34;key-derivation-function&#34;&gt;Key derivation function&lt;/h3&gt;
&lt;p&gt;The final step gives the user the option to add a password. This protects the secret even if the seed phrase is compromised.&lt;/p&gt;
&lt;p&gt;This step is also computationally expensive (as all password hashing should be) to keep hackers from being able to attack weak passwords.&lt;/p&gt;
&lt;h2 id=&#34;implementation-details&#34;&gt;Implementation details&lt;/h2&gt;
&lt;p&gt;I outlined the algorithm in broad strokes. In this section, I&amp;rsquo;ll give the details of the algorithm along with working Go code that implements it.&lt;/p&gt;
&lt;h3 id=&#34;checksum-1&#34;&gt;Checksum&lt;/h3&gt;
&lt;p&gt;The checksum is the first few bits of the SHA256 hash of the entropy. The number of bits used ensure that the total bits in &lt;code&gt;entropy + checksum&lt;/code&gt; is a multiple of 11. We&amp;rsquo;ll see why in the next section.&lt;/p&gt;
&lt;p&gt;In Go:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;computeChecksum&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;entropy&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;kt&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;big&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;Int&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
	&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;checksumBits&lt;/span&gt; &lt;span class=&#34;kt&#34;&gt;uint&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;uint&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;len&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;entropy&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;/&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;4&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

	&lt;span class=&#34;nx&#34;&gt;hash&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;sha256&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;Sum256&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;entropy&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

	&lt;span class=&#34;nx&#34;&gt;checksum&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;new&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;big&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;Int&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;SetBytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;hash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[:])&lt;/span&gt;
	&lt;span class=&#34;c1&#34;&gt;// Right-shift until only checksumBits bits remains.
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;	&lt;span class=&#34;nx&#34;&gt;checksum&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;Rsh&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;checksum&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;uint&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;len&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;hash&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;checksumBits&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

	&lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;checksum&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;encoding&#34;&gt;Encoding&lt;/h3&gt;
&lt;p&gt;With the checksum appended, the whole sequence is encoded in base-2048. This just means every 11 bits (2&lt;sup&gt;11&lt;/sup&gt; = 2048) is replaced with a word from the word list.&lt;/p&gt;
&lt;p&gt;In Go:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span class=&#34;nx&#34;&gt;wordList&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;loadWords&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;english-wordlist.txt&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

&lt;span class=&#34;nx&#34;&gt;mnemonic&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;make&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([]&lt;/span&gt;&lt;span class=&#34;kt&#34;&gt;string&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;mnemonicLength&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;c1&#34;&gt;// We&amp;#39;re taking bits from the right each time, so our loop has to fill
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;// the array backwards.
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;i&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;int&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;mnemonicLength&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;i&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;i&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;--&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;m&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;:=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;new&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;big&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;Int&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;DivMod&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;bytes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;big&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;NewInt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;2048&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;m&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

    &lt;span class=&#34;nx&#34;&gt;mnemonic&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;i&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;wordList&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;m&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;Uint64&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()]&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;strings&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;Join&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;mnemonic&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s&#34;&gt;&amp;#34; &amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;key-derivation-with-pbkdf2&#34;&gt;Key derivation with PBKDF2&lt;/h3&gt;
&lt;p&gt;PBKDF2 is used to derive the final seed. The mnemonic phrase is used as the password, and the seed is the string &amp;ldquo;mnemonic&amp;rdquo; with an optional password appended. 2048 rounds of HMAC-SHA512 are applied, and the derived key is 64 bytes long.&lt;/p&gt;
&lt;p&gt;In Go:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-go&#34; data-lang=&#34;go&#34;&gt;&lt;span class=&#34;kd&#34;&gt;func&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;deriveSeed&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;mnemonic&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;password&lt;/span&gt; &lt;span class=&#34;kt&#34;&gt;string&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;kt&#34;&gt;byte&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
	&lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;pbkdf2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;Key&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
		&lt;span class=&#34;p&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;mnemonic&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;            &lt;span class=&#34;c1&#34;&gt;// password
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;		&lt;span class=&#34;p&#34;&gt;[]&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;byte&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;#34;mnemonic&amp;#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;password&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// salt
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;		&lt;span class=&#34;mi&#34;&gt;2048&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;                        &lt;span class=&#34;c1&#34;&gt;// iterations
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;		&lt;span class=&#34;mi&#34;&gt;64&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;                          &lt;span class=&#34;c1&#34;&gt;// key length
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;		&lt;span class=&#34;nx&#34;&gt;sha512&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;New&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;                  &lt;span class=&#34;c1&#34;&gt;// hash function
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;full-source-code&#34;&gt;Full source code&lt;/h3&gt;
&lt;p&gt;You can find the complete source code here: &lt;a href=&#34;https://github.com/smarx/bip39&#34;&gt;https://github.com/smarx/bip39&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;lets-use-bip39-more&#34;&gt;Let&amp;rsquo;s use BIP39 more&lt;/h2&gt;
&lt;p&gt;BIP39 does a lot of great things:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;It makes a long seed easy to record, remember, and communicate, especially on paper.&lt;/li&gt;
&lt;li&gt;It helps prevent errors and correct errors that occur.&lt;/li&gt;
&lt;li&gt;It safeguards the secret against brute-force cracking.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;I haven&amp;rsquo;t seen it used outside of blockchain applications yet, but I&amp;rsquo;d like to see it used in other cryptography contexts, like securing PGP private keys and verifying fingerprints (e.g. Signal&amp;rsquo;s &amp;ldquo;safety numbers&amp;rdquo;).&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>TOTP: How Most 2FA Apps Work</title>
      <link>https://smarx.com/posts/2020/08/totp-how-most-2fa-apps-work/</link>
      <pubDate>Tue, 18 Aug 2020 00:00:00 +0000</pubDate>
      <author>smarx@smarx.com (Steve Marx)</author>
      <guid>https://smarx.com/posts/2020/08/totp-how-most-2fa-apps-work/</guid>
      <description>&lt;p&gt;If you&amp;rsquo;ve ever used a two-factor auth (2FA) app like Google Authenticator, you&amp;rsquo;ve probably used the &amp;ldquo;time-based one-time password&amp;rdquo; (TOTP) algorithm. It&amp;rsquo;s a conceptually simple (and awesome!) algorithm, but it&amp;rsquo;s harder to implement than you&amp;rsquo;d expect.&lt;/p&gt;
&lt;p&gt;Read on, and I&amp;rsquo;ll show you how it works.&lt;/p&gt;





    




    


&lt;figure class=&#34;flex flex-col items-center&#34;&gt;
    
    &lt;a href=&#34;https://smarx.com/posts/2020/08/totp-how-most-2fa-apps-work/totp-featured.png&#34; data-lightbox=&#34;dc532ba0c0b00b41963127218102b7f5&#34; &gt;
    
    &lt;img class=&#34;mx-auto&#34; src=&#34;https://smarx.com/posts/2020/08/totp-how-most-2fa-apps-work/totp-featured_hube2a73e060ffff761110ed28eb66daa9_65573_384x0_resize_lanczos_2.png&#34; width=&#34;384&#34; height=&#34;173&#34;&gt;
    
    &lt;/a&gt;
    
    
&lt;/figure&gt;

&lt;h2 id=&#34;demo&#34;&gt;Demo&lt;/h2&gt;
&lt;p&gt;Just to make sure I got all of this right, give the demo below a whirl. It should work with whatever 2FA app you have on your phone.&lt;/p&gt;
&lt;iframe id=&#34;totp-frame&#34; src=&#34;https://smarx.com/demos/totp/index.html&#34; class=&#34;w-full border&#34; style=&#34;height: 504px&#34;&gt;&lt;/iframe&gt;
&lt;script&gt;
    (function() {
        const iframe = document.getElementById(&#34;totp-frame&#34;);
        iframe.onload = function () {
            iframe.style.height = iframe.contentWindow.document.body.scrollHeight + 2 + &#34;px&#34;;
        }
    })();
&lt;/script&gt;
&lt;h2 id=&#34;what-problem-is-totp-solving&#34;&gt;What problem is TOTP solving?&lt;/h2&gt;
&lt;p&gt;TOTP is most commonly used as a &lt;em&gt;second factor&lt;/em&gt; in authentication. Your password is the first factor, &amp;ldquo;something you know&amp;rdquo;, and your device is the second factor, &amp;ldquo;something you have&amp;rdquo;. You prove that you have the device by generating a one-time password on it. As the name suggests, a one-time password won&amp;rsquo;t work later. This means somebody who wants to break into your account really needs to steal your device&amp;mdash;or at least the secret stored on it.&lt;/p&gt;
&lt;p&gt;A further design requirement for TOTP&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; was that it produce short, numeric codes. These are easy for a user to type.&lt;/p&gt;
&lt;h2 id=&#34;the-totp-algorithm&#34;&gt;The TOTP algorithm&lt;/h2&gt;
&lt;p&gt;The basic idea behind TOTP is to use a &lt;strong&gt;shared secret&lt;/strong&gt; to produce &lt;strong&gt;digital signatures&lt;/strong&gt;&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt; of &lt;strong&gt;timestamps&lt;/strong&gt;. At a high level, the algorithm is as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The secret is produced on the server and communicated to the 2FA client. This is usually done via a QR code, but you can also just type it in.&lt;/li&gt;
&lt;li&gt;To authenticate, both the server and client use that secret to produce a signature of the current time. An exact time would be hard to match, so the timestamp is (typically) at the granularity of 30 second intervals.&lt;/li&gt;
&lt;li&gt;The user enters the signature produced on the client. If it matches the value the server produced, then the user has been successfully authenticated.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The signature method used is a hash-based message authentication code (&lt;a href=&#34;https://en.wikipedia.org/wiki/HMAC&#34;&gt;HMAC&lt;/a&gt;), specifically HMAC-SHA1. A secure message authentication codes (MAC) is unforgeable, so producing a valid MAC is proof that you are in possession of the correct secret key.&lt;/p&gt;
&lt;p&gt;In pseudocode, producing a one-time password looks like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span class=&#34;nx&#34;&gt;truncate&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;HMAC&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;SHA1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
        &lt;span class=&#34;nx&#34;&gt;secret&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
        &lt;span class=&#34;nx&#34;&gt;uint64&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;time&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;/&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;INTERVAL&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;mod&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;**&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;DIGITS&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;where &lt;code&gt;time()&lt;/code&gt; yields the number of seconds since &lt;a href=&#34;https://en.wikipedia.org/wiki/Unix_time&#34;&gt;the Unix epoch&lt;/a&gt;, &lt;code&gt;INTERVAL&lt;/code&gt; is the number of seconds each one-time password should be valid, and &lt;code&gt;DIGITS&lt;/code&gt; is the length of the code.&lt;/p&gt;
&lt;p&gt;The only part I haven&amp;rsquo;t explained is the &lt;code&gt;truncate()&lt;/code&gt; function, and that&amp;rsquo;s because it&amp;rsquo;s awful. (But I&amp;rsquo;ll explain it in the next section.)&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s some JavaScript code that implements everything up until &lt;code&gt;truncate()&lt;/code&gt;. It uses the &lt;a href=&#34;https://caligatio.github.io/jsSHA/&#34;&gt;jsSHA&lt;/a&gt; library:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// compute a random secret
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;kr&#34;&gt;const&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;secret&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;new&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;Uint8Array&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;20&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;crypto&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;getRandomValues&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;secret&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;

&lt;span class=&#34;c1&#34;&gt;// HMAC-SHA1 with our generated secret
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;kr&#34;&gt;const&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;hmac&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;new&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;jsSHA&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;SHA-1&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;HEX&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;hmac&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;setHMACKey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;secret&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;UINT8ARRAY&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;

&lt;span class=&#34;c1&#34;&gt;// current time, in seconds since the Unix epoch
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;kr&#34;&gt;const&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;time&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Math&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;floor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;((&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;new&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Date&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()).&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;getTime&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;/&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1000&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;

&lt;span class=&#34;c1&#34;&gt;// how many 30-second intervals
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;kr&#34;&gt;const&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;interval&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Math&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;floor&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;time&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;/&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;30&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;

&lt;span class=&#34;c1&#34;&gt;// pad to 8 bytes = 16 hex characters
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;hmac&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;update&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;interval&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;toString&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;16&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;padStart&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;16&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;0&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;));&lt;/span&gt;

&lt;span class=&#34;kr&#34;&gt;const&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;digest&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;hmac&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;getHMAC&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;UINT8ARRAY&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;the-silliness-of-totps-_truncate_&#34;&gt;The silliness of TOTP&amp;rsquo;s &lt;em&gt;truncate()&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;At this point, we have &lt;code&gt;digest&lt;/code&gt;, the 20-byte signature from HMAC-SHA1. We need to get to a short numeric code that a user can type in. This is typically a 6-digit number.&lt;/p&gt;
&lt;p&gt;You might be thinking to yourself, &amp;ldquo;That seems easy. Let&amp;rsquo;s just treat the digest as a number and take the last 6 digits.&amp;rdquo; If you were thinking that, congratulations! You just invented a better algorithm than TOTP.&lt;/p&gt;
&lt;p&gt;TOTP does use a simple modulo to get 6 digits, but first it&lt;sup id=&#34;fnref:3&#34;&gt;&lt;a href=&#34;#fn:3&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;3&lt;/a&gt;&lt;/sup&gt; does something stupefying, and as far as I can tell &lt;a href=&#34;https://crypto.stackexchange.com/questions/5249/why-does-hotp-use-such-a-complex-truncate-function&#34;&gt;there&amp;rsquo;s no reason for it&lt;/a&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Grab the last 4 bits of the digest. Call that &lt;code&gt;offset&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Starting at byte &lt;code&gt;offset&lt;/code&gt;, extract 32 bits of the digest.&lt;/li&gt;
&lt;li&gt;Clear the first bit of that result.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For some reason, this process is called &amp;ldquo;truncation&amp;rdquo; in TOTP, even though it has nothing to do with truncation.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s some code that does it:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// &amp;#34;truncate&amp;#34; to 31 bits
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;kr&#34;&gt;const&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;offset&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;digest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;19&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&#34;mh&#34;&gt;0xF&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// last 4 bits
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;
&lt;span class=&#34;c1&#34;&gt;// take 4 bytes starting at the specified byte offset,
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;// but chop off the first bit so we have only 31 bits
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;kr&#34;&gt;const&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;v&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;((&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;digest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;offset&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&#34;mh&#34;&gt;0x7F&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;24&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// 0x7F = 01111111
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;    &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;digest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;offset&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;16&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;digest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;offset&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;2&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;8&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;digest&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;offset&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;3&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is probably the most difficult part of the implementation to get right. If you&amp;rsquo;re reading this because you&amp;rsquo;re struggling with your own TOTP implementation, I hope the preceding code is helpful.&lt;/p&gt;
&lt;p&gt;With this &amp;ldquo;truncated&amp;rdquo; value in hand, we can finally extract our 6-digit code:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span class=&#34;c1&#34;&gt;// take the last 6 digits, with leading 0s as necessary
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;kr&#34;&gt;const&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;codeString&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;v&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;%&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;**&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;toString&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;10&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;padStart&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;6&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;0&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;what-about-the-qr-code&#34;&gt;What about the QR code?&lt;/h2&gt;
&lt;p&gt;To me, one of the most magical parts of using TOTP is the QR code. Just point your phone at the screen, and you&amp;rsquo;re set! The QR code saves the user the trouble of having to type in the secret that was generated on the server.&lt;/p&gt;
&lt;p&gt;The content of the QR code is simply this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;otpauth://totp/LABEL?secret=SECRET
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;LABEL&lt;/code&gt; is just a friendly name that shows up in your 2FA app. In my demo above, the label is always &amp;ldquo;smarx-totp-demo&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;SECRET&lt;/code&gt; is the shared secret that the server generated. It&amp;rsquo;s encoded in base32, which I&amp;rsquo;ll describe in the next section.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://github.com/google/google-authenticator/wiki/Key-Uri-Format&#34;&gt;Other parameters are possible&lt;/a&gt;, such as the number of digits the code should be, but they seem to be rarely used in the wild.&lt;/p&gt;
&lt;p&gt;Here&amp;rsquo;s the part of my demo code that displays the QR code, using the &lt;a href=&#34;https://github.com/davidshimjs/qrcodejs&#34;&gt;QRCode.js library&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span class=&#34;k&#34;&gt;new&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;QRCode&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;document&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;getElementById&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;qrcode&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;
    &lt;span class=&#34;s2&#34;&gt;&amp;#34;otpauth://totp/smarx-totp-demo?secret=&amp;#34;&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;base32&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;encode&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;secret&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;secret-encoding-base32&#34;&gt;Secret encoding: base32&lt;/h3&gt;
&lt;p&gt;A cryptographic secret is just a bunch of random bytes. If such a secret is to be displayed to the user, or transmitted through a URL, it needs to be encoded somehow. The most typical encodings you&amp;rsquo;ll see are hexadecimal, which is straightforward, and base64, which is more compact.&lt;/p&gt;
&lt;p&gt;TOTP uses base32 as something of a compromise. It isn&amp;rsquo;t as efficient as base64&amp;mdash;secrets will take more characters to encode&amp;mdash;but it avoids some visual ambiguities of base64. For example, a base64-encoded value might include the sequence &amp;ldquo;Il1O0&amp;rdquo;. Depending on the font, you might not be sure what each character is.&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;s no official standard for what alphabet is used for base32 encoding, but apps that use TOTP seem to have standardized around the following from &lt;a href=&#34;https://tools.ietf.org/html/rfc4648#section-6&#34;&gt;RFC 4648&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;ABCDEFGHIJKLMNOPQRSTUVWXYZ234567
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It&amp;rsquo;s just the English alphabet with the digits 2-7 added to the end.&lt;/p&gt;
&lt;div class=&#34;aside px-3 sm:px-8 py-1 bg-cool-gray-100 border sm:m-6&#34;&gt;
    &lt;h2 style=&#34;font-family: Nunito Sans;&#34;&gt;Tip: Cryptography and encoding&lt;/h2&gt;
    &lt;p&gt;Getting encoding and decoding right is one of the most frustrating parts of implementing cryptography.&lt;/p&gt;
&lt;p&gt;A good rule of thumb is to, as much as possible, work directly with bytes. Treat hexadecimal, base64, etc., as &lt;em&gt;serialization formats&lt;/em&gt;. You should use them to transport or store values, e.g. in a URL or a JSON configuration file. But internally, you want to use bytes.&lt;/p&gt;
&lt;p&gt;This keeps things simpler and helps to avoid mistakes like hashing a hexadecimal string when you meant to hash the actual bytes it represents.&lt;/p&gt;

&lt;/div&gt;

&lt;h2 id=&#34;universal-2nd-factor-u2f-is-better&#34;&gt;Universal 2nd Factor (U2F) is better&lt;/h2&gt;
&lt;p&gt;Aside from the silly truncation process, TOTP suffers from a genuine security drawback: &lt;strong&gt;it&amp;rsquo;s based on &lt;em&gt;symmetric cryptography&lt;/em&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Symmetric cryptography just means there&amp;rsquo;s a single secret that both the server and the client know. If a hacker can read this secret from the server&amp;rsquo;s database, they can generate 2FA codes themselves.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Asymmetric&lt;/em&gt; (or &amp;ldquo;public key&amp;rdquo;) cryptography would be better. In that model, the user generates a private key and shares the corresponding public key with the server at the time of sign-up. From then on, the server can always authenticate the user by issuing a challenge (e.g. &amp;ldquo;sign this random string&amp;rdquo;) that can only be solved by knowing the private key. Because the private key stays on the local device, there&amp;rsquo;s no opportunity for it to be stolen by a hacker.&lt;/p&gt;
&lt;p&gt;This is precisely how Universal 2nd Factor (U2F) works. With the addition of &lt;a href=&#34;https://en.wikipedia.org/wiki/YubiKey&#34;&gt;hardware dongles&lt;/a&gt; that do the typing for you, this solution can be both more convenient &lt;em&gt;and&lt;/em&gt; more secure.&lt;/p&gt;
&lt;p&gt;Let me know if you&amp;rsquo;d be interested in a follow-up post digging into U2F!&lt;/p&gt;
&lt;h2 id=&#34;further-reading&#34;&gt;Further reading&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://tools.ietf.org/html/rfc4226&#34;&gt;RFC 4226: HOTP spec&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://tools.ietf.org/html/rfc6238&#34;&gt;RFC 6238: TOTP spec&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://tools.ietf.org/html/rfc4648#section-6&#34;&gt;RFC 4648: the base16, base32, and base64 data encodings&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://blog.trezor.io/why-you-should-never-use-google-authenticator-again-e166d09d4324&#34;&gt;Why You Should Never Use Google Authenticator Again&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;section class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;Technically, TOTP was designed to use the HMAC-based one-time password (HOTP) algorithm, which in turn had this design goal. &lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:2&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;I&amp;rsquo;m being a little sloppy with terminology here. At least under some definitions, a &amp;ldquo;digital signature&amp;rdquo; refers only to the use of public key cryptography. This would more accurately be called a message authentication code (MAC), but I think &amp;ldquo;signature&amp;rdquo; is easier to understand. &lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:3&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;Again, technically this is inherited from HOTP. &lt;a href=&#34;#fnref:3&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</description>
    </item>
    
  </channel>
</rss>
