<?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: GemXD</title>
    <description>The latest articles on DEV Community by GemXD (@rishabh_patel_99d02012ced).</description>
    <link>https://dev.to/rishabh_patel_99d02012ced</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4286278%2F6059e79a-50d5-4450-b605-2d6ae2e1f183.png</url>
      <title>DEV Community: GemXD</title>
      <link>https://dev.to/rishabh_patel_99d02012ced</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rishabh_patel_99d02012ced"/>
    <language>en</language>
    <item>
      <title>Seekable encrypted video on Telegram: 4 MB AES-GCM segments in Rust</title>
      <dc:creator>GemXD</dc:creator>
      <pubDate>Sun, 06 Dec 2026 12:05:57 +0000</pubDate>
      <link>https://dev.to/rishabh_patel_99d02012ced/seekable-encrypted-video-on-telegram-4-mb-aes-gcm-segments-in-rust-kom</link>
      <guid>https://dev.to/rishabh_patel_99d02012ced/seekable-encrypted-video-on-telegram-4-mb-aes-gcm-segments-in-rust-kom</guid>
      <description>&lt;p&gt;I'm building an Android photo vault that keeps encrypted copies of your photos and videos in your own Telegram account. The phone encrypts everything before upload, so Telegram only ever holds ciphertext. This post covers the one design problem that took the most thought: &lt;strong&gt;how do you stream and seek inside an encrypted 1.9 GB video without downloading and decrypting all of it?&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem with one big AES-GCM blob
&lt;/h2&gt;

&lt;p&gt;AES-GCM is the obvious choice for file encryption: it's fast on every modern ARM CPU and it authenticates the data, so tampering is detected. But it authenticates the &lt;em&gt;whole&lt;/em&gt; ciphertext with one tag. If you encrypt a 1.9 GB file as a single GCM message, you can't safely hand byte 1,500,000,000 to a video player until you've processed everything before it and checked the tag at the end.&lt;/p&gt;

&lt;p&gt;For a gallery app that's a deal-breaker. Tapping the middle of a video's seek bar has to work in well under a second, on mobile data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Split into independently encrypted 4 MB segments
&lt;/h2&gt;

&lt;p&gt;The fix is to stop treating a file as one message. Each uploaded chunk is split into fixed &lt;strong&gt;4 MB plaintext segments&lt;/strong&gt;, and each segment is its own AES-256-GCM message with its own 16-byte tag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌──────────────────────────────────────────────┐
│ Header (8 bytes)                             │
│   magic: "TVSEG\x01"   segment_count: u16    │
├──────────────────────────────────────────────┤
│ Segment 0: ciphertext (≤ 4 MB) || tag (16 B) │
│ Segment 1: ciphertext (≤ 4 MB) || tag (16 B) │
│ ...                                          │
└──────────────────────────────────────────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because every segment except the last has the same size, the byte offset of segment &lt;em&gt;n&lt;/em&gt; is simple arithmetic: &lt;code&gt;8 + n × (4 MB + 16)&lt;/code&gt;. To serve a range request the player makes, the app works out which segments cover it, downloads only those byte ranges from Telegram, decrypts and verifies each one, and returns the slice. Seeking costs at most two segments of work, no matter how big the file is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Nonces without storing a nonce per segment
&lt;/h2&gt;

&lt;p&gt;GCM's one hard rule: never reuse a (key, nonce) pair. Storing a random 12-byte nonce per segment would work, but it adds bookkeeping. Instead, each chunk gets one random base IV, and segment nonces are derived from it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;/// segment_iv = chunk_iv[0..8] || segment_index (u32, big-endian)&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;derive_segment_iv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;chunk_iv&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;segment_index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="kd"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0u8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;iv&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="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy_from_slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;chunk_iv&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="mi"&gt;8&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;iv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;8&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy_from_slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;segment_index&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;to_be_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="p"&gt;;&lt;/span&gt;
    &lt;span class="n"&gt;iv&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first 8 bytes are random per chunk, the last 4 are the segment index, so nonces never repeat within a chunk and collide across chunks only with the probability of two random 64-bit values matching. The index being part of the nonce also means segments can't be silently reordered: swap two and their tags fail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keys: the passphrase never encrypts your photos directly
&lt;/h2&gt;

&lt;p&gt;There are two keys:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A random 32-byte &lt;strong&gt;upload key&lt;/strong&gt; encrypts everything stored in Telegram.&lt;/li&gt;
&lt;li&gt;Your passphrase goes through &lt;strong&gt;Argon2id&lt;/strong&gt; (64 MiB memory, 3 iterations, 4 lanes, a random 32-byte salt per vault) to derive a key that &lt;em&gt;wraps&lt;/em&gt; the upload key.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Changing your passphrase only re-wraps one 32-byte key instead of re-encrypting gigabytes. The wrapped key is also mirrored to your Telegram storage, so on a new phone you sign in to Telegram, type your passphrase, and the vault comes back. A recovery phrase encodes the upload key directly for the "I forgot my passphrase" case, and if you lose both, nobody (including me) can decrypt your files. That's the point.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Rust
&lt;/h2&gt;

&lt;p&gt;The crypto, chunking, SQLite index and Telegram client live in a Rust core shared with the Android UI through UniFFI. RustCrypto's &lt;code&gt;aes-gcm&lt;/code&gt; and &lt;code&gt;argon2&lt;/code&gt; crates, one tested implementation of the segment format, and no JNI boilerplate to keep in sync by hand. The Kotlin side only sees &lt;code&gt;VaultEngine&lt;/code&gt; methods and byte ranges.&lt;/p&gt;

&lt;h2&gt;
  
  
  Small files skip all this
&lt;/h2&gt;

&lt;p&gt;Thumbnails and small photos fit in one segment anyway, so they're encrypted as a single AES-GCM message without the segment header. The 8-byte &lt;code&gt;TVSEG\x01&lt;/code&gt; header (magic plus a version byte) is what tells the reader which format it's looking at, and leaves room to change the layout later.&lt;/p&gt;

&lt;p&gt;The app is Obsidian Roll, in public beta on Google Play: &lt;a href="https://play.google.com/store/apps/details?id=com.gemxd.obsidianroll" rel="noopener noreferrer"&gt;https://play.google.com/store/apps/details?id=com.gemxd.obsidianroll&lt;/a&gt;. It's an independent app, not affiliated with Telegram. Questions about the format or the threat model are welcome in the comments.&lt;/p&gt;

</description>
      <category>android</category>
      <category>architecture</category>
      <category>rust</category>
      <category>security</category>
    </item>
  </channel>
</rss>
