<?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>Forem: Max Jiang</title>
    <description>The latest articles on Forem by Max Jiang (@max_jiang).</description>
    <link>https://forem.com/max_jiang</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%2F3640766%2F0303766b-90e8-4176-af84-68a92901d121.png</url>
      <title>Forem: Max Jiang</title>
      <link>https://forem.com/max_jiang</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/max_jiang"/>
    <language>en</language>
    <item>
      <title>(Part 5) Sealing Secrets: How to Survive a Reboot (And Why It's Dangerous) 💾</title>
      <dc:creator>Max Jiang</dc:creator>
      <pubDate>Wed, 31 Dec 2025 02:58:17 +0000</pubDate>
      <link>https://forem.com/max_jiang/part-5-sealing-secrets-how-to-survive-a-reboot-and-why-its-dangerous-19io</link>
      <guid>https://forem.com/max_jiang/part-5-sealing-secrets-how-to-survive-a-reboot-and-why-its-dangerous-19io</guid>
      <description>&lt;p&gt;Welcome back to the &lt;strong&gt;Confidential Computing Chronicles&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/max_jiang/part-4-remote-attestation-how-to-prove-you-arent-a-dog-on-the-internet-c9p"&gt;Part 4&lt;/a&gt;, we learned how to prove our identity using Remote Attestation. We established trust. We are running securely in RAM. Life is good.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Then, the janitor trips over the power cord.&lt;/strong&gt; 🔌&lt;/p&gt;

&lt;p&gt;The server shuts down. The RAM is wiped. All your in-memory secrets—the session keys, the customer data, the AI weights—are gone forever.&lt;/p&gt;

&lt;p&gt;Welcome to the problem of &lt;strong&gt;Persistence&lt;/strong&gt;.&lt;br&gt;
In a normal app, you just write to a file: &lt;code&gt;f.write(secret)&lt;/code&gt;.&lt;br&gt;
In SGX, you can't do that. The disk belongs to the Untrusted OS. If you write plain text to disk, the OS reads it, and you fail.&lt;/p&gt;

&lt;p&gt;You need to encrypt the data before it leaves the Enclave. But &lt;strong&gt;where do you store the encryption key?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On the disk? No, the OS will see it.&lt;/li&gt;
&lt;li&gt;Hardcoded in the source? No, reverse engineers will find it.&lt;/li&gt;
&lt;li&gt;Ask the user every time? Terrible UX.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The solution is a hardware feature called &lt;strong&gt;Sealing&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Magic Key: CPU-Level Encryption
&lt;/h2&gt;

&lt;p&gt;SGX provides a function called &lt;code&gt;sgx_seal_data()&lt;/code&gt;.&lt;br&gt;
It asks the CPU: &lt;em&gt;"Hey, give me a unique encryption key that ONLY I can use."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The CPU derives a key from hardware fuses and the Enclave's identity. The Enclave uses this key to encrypt the data and hands the blob to the OS to store on disk.&lt;/p&gt;

&lt;p&gt;When the system reboots and the Enclave starts up again, it asks the CPU for the key again to &lt;strong&gt;Unseal&lt;/strong&gt; the blob.&lt;/p&gt;

&lt;p&gt;If &lt;strong&gt;ANYTHING&lt;/strong&gt; has changed—if the hardware is different, or if the Enclave code has been tampered with—the CPU &lt;strong&gt;refuses to generate the same key&lt;/strong&gt;. The decryption fails. The data is effectively trash to anyone else.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Developer's Dilemma: MRENCLAVE vs. MRSIGNER
&lt;/h2&gt;

&lt;p&gt;This sounds great until you release &lt;strong&gt;Version 2.0&lt;/strong&gt; of your app.&lt;/p&gt;

&lt;p&gt;Remember &lt;strong&gt;MRENCLAVE&lt;/strong&gt; from Part 4? It’s the hash of your binary.&lt;br&gt;
If you fix a bug and recompile, your hash changes.&lt;br&gt;
If you sealed data using the &lt;strong&gt;MRENCLAVE policy&lt;/strong&gt;, the CPU says: &lt;em&gt;"Sorry, you are a different binary. I cannot give you the old key."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Congratulations, you just bricked your user's data.&lt;/strong&gt; You cannot decrypt the database created by Version 1.0.&lt;/p&gt;

&lt;p&gt;To solve this, we use the &lt;strong&gt;MRSIGNER policy&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%2F7wb2hli4aad98xe6icvo.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%2F7wb2hli4aad98xe6icvo.png" alt="section5-1" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;MRENCLAVE Policy:&lt;/strong&gt; The key is bound to the &lt;em&gt;exact binary hash&lt;/em&gt;.

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Use case:&lt;/em&gt; Ultra-strict security. Even the original developer cannot read the data with a new version.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;MRSIGNER Policy:&lt;/strong&gt; The key is bound to the &lt;em&gt;Developer's Private Signing Key&lt;/em&gt;.

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Use case:&lt;/em&gt; Software updates. As long as Version 2.0 is signed by the same author (you) as Version 1.0, the CPU allows access to the old keys.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pro Tip:&lt;/strong&gt; Always use &lt;code&gt;MRSIGNER&lt;/code&gt; for persistent data, unless you enjoy angry emails from customers who lost everything after an update.&lt;/p&gt;

&lt;h2&gt;
  
  
  The "Save Scumming" Attack (Rollback)
&lt;/h2&gt;

&lt;p&gt;Sealing protects &lt;strong&gt;Confidentiality&lt;/strong&gt; (OS can't read it) and &lt;strong&gt;Integrity&lt;/strong&gt; (OS can't modify it).&lt;br&gt;
But it does NOT protect against &lt;strong&gt;Time Travel&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here is the "Rollback Attack" (a.k.a. Replay Attack):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Day 1:&lt;/strong&gt; Your Enclave seals a file: &lt;code&gt;Balance = $100&lt;/code&gt;. The OS saves it as &lt;code&gt;data.blob&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Day 2:&lt;/strong&gt; You withdraw $50. The Enclave updates the file: &lt;code&gt;Balance = $50&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The Attack:&lt;/strong&gt; The malicious OS administrator (who made a copy of the Day 1 file) simply &lt;strong&gt;overwrites&lt;/strong&gt; the new file with the old one.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Result:&lt;/strong&gt; The Enclave unseals the old file (the key is still valid!). It reads &lt;code&gt;Balance = $100&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Profit:&lt;/strong&gt; The hacker just duplicated money.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is exactly like "Save Scumming" in video games—reloading an old save file to undo a mistake.&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%2Fkcnwdw58xvgyt2nncpbm.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%2Fkcnwdw58xvgyt2nncpbm.png" alt="Section 5-2" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Fix It? (Monotonic Counters)
&lt;/h2&gt;

&lt;p&gt;To prevent this, SGX provides &lt;strong&gt;Monotonic Counters&lt;/strong&gt;. These are special counters stored in the hardware (or a secure replay-protected block).&lt;/p&gt;

&lt;p&gt;Every time you write a file, you increment the counter in hardware (&lt;code&gt;Counter = 5&lt;/code&gt;) and embed the number &lt;code&gt;5&lt;/code&gt; inside the sealed file.&lt;br&gt;
When you read the file back, you check:&lt;br&gt;
&lt;code&gt;if (File.Counter &amp;lt; Hardware.Counter) { Panic("Rollback Detected!"); }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It sounds simple, but utilizing Monotonic Counters is notoriously slow and limited in quantity. It’s one of the biggest bottlenecks in SGX storage design.&lt;/p&gt;

&lt;h2&gt;
  
  
  Up Next
&lt;/h2&gt;

&lt;p&gt;We have covered the good parts: Security, Identity, Persistence.&lt;br&gt;
Now it’s time to talk about the &lt;strong&gt;Nightmare Scenario&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;What if the hardware &lt;em&gt;itself&lt;/em&gt; is leaking secrets? What if the CPU is betraying you?&lt;/p&gt;

</description>
      <category>security</category>
      <category>persistence</category>
      <category>cryptography</category>
      <category>backend</category>
    </item>
    <item>
      <title>(Part 4) Remote Attestation: How to Prove You Aren't a Dog on the Internet 🐶</title>
      <dc:creator>Max Jiang</dc:creator>
      <pubDate>Mon, 15 Dec 2025 06:59:32 +0000</pubDate>
      <link>https://forem.com/max_jiang/part-4-remote-attestation-how-to-prove-you-arent-a-dog-on-the-internet-c9p</link>
      <guid>https://forem.com/max_jiang/part-4-remote-attestation-how-to-prove-you-arent-a-dog-on-the-internet-c9p</guid>
      <description>&lt;p&gt;Welcome back to the &lt;strong&gt;Confidential Computing Chronicles&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/max_jiang/part-3-the-memory-wall-why-your-enclave-is-slow-and-how-to-fix-it-20i3"&gt;Part 3&lt;/a&gt;, we learned how to optimize our code to fit inside the tiny memory limits of an Enclave. Now, we face a bigger problem: &lt;strong&gt;Trust.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine I tell you: &lt;em&gt;"Hey, send me your credit card number. I promise I'm running your payment code inside a secure Intel SGX Enclave. Nobody can see it."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You would be an idiot to believe me.&lt;/p&gt;

&lt;p&gt;How do you know I'm not just running a &lt;code&gt;printf()&lt;/code&gt; script on a hacked laptop in a basement? How do you know I didn't modify the code to email me your CC number before processing it?&lt;/p&gt;

&lt;p&gt;This is the classic "On the Internet, nobody knows you're a dog" problem.&lt;br&gt;
&lt;strong&gt;Remote Attestation&lt;/strong&gt; is how we solve it. It is the cryptographic proof that I am not a dog.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem: The Evil Twin
&lt;/h2&gt;

&lt;p&gt;Let's say you compiled your secure Enclave. It has a specific binary hash (we call this the &lt;strong&gt;Measurement&lt;/strong&gt;).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Good Enclave:&lt;/strong&gt; Hash &lt;code&gt;0xABC...&lt;/code&gt; (Does honest math)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Evil Enclave:&lt;/strong&gt; Hash &lt;code&gt;0x666...&lt;/code&gt; (Steals your data)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the server, they look identical. They are both just binaries running in memory. The Operating System can't be trusted to tell you the truth, because the OS might be the attacker.&lt;/p&gt;

&lt;p&gt;We need a way for the &lt;strong&gt;CPU itself&lt;/strong&gt; to speak up and say: &lt;em&gt;"I certify that the code running in this protected region has the hash &lt;code&gt;0xABC...&lt;/code&gt;."&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution: The "Quote"
&lt;/h2&gt;

&lt;p&gt;Remote Attestation relies on a special digital signature called a &lt;strong&gt;Quote&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Think of the CPU as a Notary Public.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Measurement:&lt;/strong&gt; When your Enclave starts, the CPU records a cryptographic hash of the initial code and data. This is called &lt;strong&gt;MRENCLAVE&lt;/strong&gt; (The Enclave Identity).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The Challenge:&lt;/strong&gt; The User (Client) sends a random challenge (nonce) to the Server.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The Report:&lt;/strong&gt; The Enclave creates a "Report" containing:

&lt;ul&gt;
&lt;li&gt;Its Measurement (&lt;code&gt;MRENCLAVE&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;The User's challenge (to prevent replay attacks).&lt;/li&gt;
&lt;li&gt;Custom data (like a public key generated inside the Enclave).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The Signature:&lt;/strong&gt; The Enclave asks a special architectural enclave (the &lt;strong&gt;Quoting Enclave&lt;/strong&gt;) to sign this report using a hardware-fused private key that only Intel possesses.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This signed document is the &lt;strong&gt;Quote&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%2Fb9pxjos7hucuhz6ec21u.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%2Fb9pxjos7hucuhz6ec21u.png" alt="RA PROCESS" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Interpreting the ID Card
&lt;/h2&gt;

&lt;p&gt;So, the server sends you this "Quote". What do you do with it?&lt;/p&gt;

&lt;p&gt;You (the Client) look at it and verify three things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;The Signature is Valid:&lt;/strong&gt; This proves the code is running on genuine Intel SGX hardware (not a simulator, not an AMD chip).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The Measurement Matches:&lt;/strong&gt; The hash inside the Quote matches the hash of the source code you audit and trust. This proves the code hasn't been tampered with.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The Data is Fresh:&lt;/strong&gt; The nonce matches the one you sent.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If all three pass, you have established a &lt;strong&gt;Secure Channel&lt;/strong&gt;. You can now encrypt your secrets with the Enclave's public key (embedded in the Quote) and send them over.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Congratulations. You have just established a root of trust that bypasses the Cloud Provider entirely.&lt;/strong&gt; AWS, Azure, or Google Cloud physically cannot see your data, and you have mathematical proof of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Two Flavors: EPID vs. DCAP
&lt;/h2&gt;

&lt;p&gt;If you start Googling this, you will see two acronyms fighting each other.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;EPID (Enhanced Privacy ID):&lt;/strong&gt; The "Old School" way. You send the Quote to Intel's Attestation Service (IAS) to verify it.

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Pros:&lt;/em&gt; Easier to set up initially.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Cons:&lt;/em&gt; Depends on Intel's servers being online. Slow.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;DCAP (Data Center Attestation Primitives):&lt;/strong&gt; The "Modern" way. You verify the Quote locally using a caching service (PCCS).

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Pros:&lt;/em&gt; Fast, works offline (mostly), scalable for data centers.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Cons:&lt;/em&gt; Setup is a nightmare of configuration files.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;For modern production systems, &lt;strong&gt;DCAP is the standard&lt;/strong&gt;. But be warned: setting up the PCCS (Provisioning Certificate Caching Service) is a rite of passage for SGX developers. It involves API keys, confusing docs, and lots of caffeine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why This Matters for Web3 &amp;amp; AI
&lt;/h2&gt;

&lt;p&gt;Why should you care?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Secret Network / Oasis Network:&lt;/strong&gt; These blockchains use Attestation to prove that the nodes aren't peaking at private transaction data.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Confidential AI:&lt;/strong&gt; When you use ChatGPT, you trust OpenAI. But if you want to run a model on &lt;em&gt;sensitive medical data&lt;/em&gt; in the cloud, you need Attestation to prove the model is running in an Enclave and won't leak the patient info.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Up Next
&lt;/h2&gt;

&lt;p&gt;Now we have a secure Enclave (Part 2), we know how to manage its memory (Part 3), and we have proved to the world that it's genuine (Part 4).&lt;/p&gt;

&lt;p&gt;But what happens when the power goes out? RAM is volatile. When the Enclave dies, your data dies.&lt;br&gt;
We need a way to save secrets to the disk &lt;strong&gt;without&lt;/strong&gt; the OS reading them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part 5: Sealing Secrets &amp;amp; Data Persistence&lt;/strong&gt; is coming next.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Leave a Comment 👇&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Quiz time:&lt;/strong&gt; If I change &lt;em&gt;one single character&lt;/em&gt; in my C++ source code and recompile, does the MRENCLAVE hash change?&lt;br&gt;
(Hint: Yes/No?)&lt;/p&gt;

&lt;p&gt;Also, has anyone actually managed to set up a local PCCS without throwing their keyboard out the window? Let me know in the comments. I need emotional support.&lt;/p&gt;

</description>
      <category>security</category>
      <category>cryptograpy</category>
      <category>web3</category>
      <category>backend</category>
    </item>
    <item>
      <title>(Part 3) The Memory Wall: Why Your Enclave is Slow and How to Fix It</title>
      <dc:creator>Max Jiang</dc:creator>
      <pubDate>Mon, 08 Dec 2025 04:30:53 +0000</pubDate>
      <link>https://forem.com/max_jiang/part-3-the-memory-wall-why-your-enclave-is-slow-and-how-to-fix-it-20i3</link>
      <guid>https://forem.com/max_jiang/part-3-the-memory-wall-why-your-enclave-is-slow-and-how-to-fix-it-20i3</guid>
      <description>&lt;p&gt;Welcome to Part 3 of the &lt;strong&gt;Confidential Computing Chronicles&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/max_jiang/hello-world-is-hard-surviving-the-sgx-setup-4il9"&gt;Part 2&lt;/a&gt;, we fought the SDK and won. We have a running Enclave. But if you’re trying to move beyond "Hello World" to something real—like running an image processing algorithm or a small database—you’re about to hit a literal, hardware-encoded brick wall.&lt;/p&gt;

&lt;p&gt;It’s called the &lt;strong&gt;Memory Wall&lt;/strong&gt;, and it exists because Intel is paranoid (for good reason).&lt;/p&gt;

&lt;h2&gt;
  
  
  The 128MB Problem (The EPC)
&lt;/h2&gt;

&lt;p&gt;In a traditional app, you treat RAM like a vast, flat ocean. You &lt;code&gt;malloc&lt;/code&gt; a gigabyte? The OS says "Sure, whatever."&lt;/p&gt;

&lt;p&gt;In SGX, the CPU reserved a special, isolated region of RAM for Enclaves. This is the &lt;strong&gt;Enclave Page Cache (EPC)&lt;/strong&gt;. On older machines (pre-Ice Lake), this pool was strictly capped at &lt;strong&gt;128MB&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;But wait, it gets worse! After some administrative overhead, you only have about &lt;strong&gt;90MB&lt;/strong&gt; of usable space for your code, stack, and heap.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"90MB? My Node.js app consumes that just waking up!"&lt;/strong&gt; Precisely. This is why SGX is for &lt;em&gt;Confidential Computing&lt;/em&gt;, not for &lt;em&gt;Lazy Computing&lt;/em&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%2Fcod0oqlolegb4kjau3j8.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%2Fcod0oqlolegb4kjau3j8.png" alt="EPCVSNormalRam" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Performance Cliff: What happens when you leak?
&lt;/h2&gt;

&lt;p&gt;If you exceed that 128MB limit, the hardware doesn't just crash. It starts &lt;strong&gt;Paging&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The CPU moves encrypted pages of your Enclave out to normal RAM. When you need them again, it pulls them back, &lt;strong&gt;decrypts them&lt;/strong&gt;, verifies the integrity hash, and swaps them in.&lt;/p&gt;

&lt;p&gt;This "Swapping" in SGX is roughly &lt;strong&gt;10x to 100x slower&lt;/strong&gt; than standard OS paging because of the constant encryption/decryption overhead. If your app frequently crosses that 128MB threshold, you will see a performance cliff that looks like a vertical drop into hell.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pro-Tips for Tuning your Enclave
&lt;/h2&gt;

&lt;p&gt;So, how do we survive inside a 90MB box? Here are the battle-hardened rules I follow:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Reuse, Don't Reallocate
&lt;/h3&gt;

&lt;p&gt;Inside an Enclave, &lt;code&gt;malloc&lt;/code&gt; and &lt;code&gt;free&lt;/code&gt; are not just expensive—they are dangerous for memory fragmentation. If you have a 1MB buffer for processing data, allocate it &lt;strong&gt;once&lt;/strong&gt; at startup and reuse it forever.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Stream, Don't Load
&lt;/h3&gt;

&lt;p&gt;Don't read a 500MB database file into the Enclave memory. Keep it outside (Untrusted RAM), and pull it in chunks of 64KB, process it, and send the result back out via OCALL. Keep the Enclave as a "Processing Factory," not a "Storage Warehouse."&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Check your Enclave Configuration
&lt;/h3&gt;

&lt;p&gt;Every Enclave has a configuration file (&lt;code&gt;Enclave.config.xml&lt;/code&gt;). If you don't adjust this, you're using default values that might be crippling you.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;EnclaveConfiguration&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;ProdID&amp;gt;&lt;/span&gt;0&lt;span class="nt"&gt;&amp;lt;/ProdID&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;ISVSVN&amp;gt;&lt;/span&gt;0&lt;span class="nt"&gt;&amp;lt;/ISVSVN&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;HeapMaxSize&amp;gt;&lt;/span&gt;0x4000000&lt;span class="nt"&gt;&amp;lt;/HeapMaxSize&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;StackMaxSize&amp;gt;&lt;/span&gt;0x40000&lt;span class="nt"&gt;&amp;lt;/StackMaxSize&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;ReservedMemSize&amp;gt;&lt;/span&gt;0x1000000&lt;span class="nt"&gt;&amp;lt;/ReservedMemSize&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;/EnclaveConfiguration&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Max's Golden Rule:&lt;/strong&gt; Set your Heap to just under your machine's EPC hardware limit to avoid the paging trap.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing for the Cliff
&lt;/h2&gt;

&lt;p&gt;If you want to feel the pain, try this exercise:&lt;br&gt;
Write a loop that allocates 1MB blocks inside the Enclave and times each allocation. Watch the time stay steady until you reach ~90MB, and then watch the latency skyrocket.&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%2Fvefo950mdl6u3twhp45f.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%2Fvefo950mdl6u3twhp45f.png" alt="Memory Usage" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this makes you a better engineer
&lt;/h2&gt;

&lt;p&gt;Learning to code for SGX is like learning to code for a 1980s game console with 64KB of RAM. It forces you to think about &lt;strong&gt;data locality&lt;/strong&gt;, &lt;strong&gt;buffer management&lt;/strong&gt;, and &lt;strong&gt;overhead&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In a world where developers throw RAM at problems like it's free, being the person who can fit a secure machine learning model into 90MB makes you a unicorn in the security industry.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's Next?
&lt;/h2&gt;

&lt;p&gt;We've mastered memory. Now we need to prove to the world that our Enclave is actually running on real hardware and hasn't been tampered with.&lt;/p&gt;

&lt;p&gt;Next up is the most mysterious and crucial part of SGX: &lt;strong&gt;Remote Attestation.&lt;/strong&gt; This is how you prove your identity over the internet without trusting the guy holding the other end of the wire.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part 4: Remote Attestation - The Digital Handshake of Trust&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Leave a Comment 👇&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;What's the smallest memory footprint you've ever managed to squeeze your code into? &lt;br&gt;
Are you getting &lt;code&gt;SGX_ERROR_OUT_OF_MEMORY&lt;/code&gt;? Post your stack size configuration below and let's find that memory leak together!&lt;/p&gt;

</description>
      <category>security</category>
      <category>systems</category>
      <category>cpp</category>
      <category>performance</category>
    </item>
    <item>
      <title>Hello World is Hard: Surviving the SGX Setup</title>
      <dc:creator>Max Jiang</dc:creator>
      <pubDate>Fri, 05 Dec 2025 03:00:50 +0000</pubDate>
      <link>https://forem.com/max_jiang/hello-world-is-hard-surviving-the-sgx-setup-4il9</link>
      <guid>https://forem.com/max_jiang/hello-world-is-hard-surviving-the-sgx-setup-4il9</guid>
      <description>&lt;p&gt;Welcome back to the Confidential Computing Chronicles.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/max_jiang/i-dont-trust-aws-and-neither-should-you-dive-into-intel-sgx-53e5"&gt;last post&lt;/a&gt;, we discussed why you should be paranoid about your cloud provider. Today, we stop philosophizing and &lt;strong&gt;start typing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you are a Python developer who complains when pip install takes more than 10 seconds, take a deep breath. Intel SGX (Software Guard Extensions) is not a library; it’s an architectural lifestyle change.&lt;/p&gt;

&lt;p&gt;Building a "Hello World" in SGX isn't just print("Hello"). It involves:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Checking if your CPU even supports it (&lt;strong&gt;spoiler: it’s complicated&lt;/strong&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fighting with Linux Kernel drivers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Signing cryptographic contracts. :D&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Writing a "Bridge" between two worlds.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Let’s get into it.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 0: The Hardware Lottery (Do I have SGX?)
&lt;/h2&gt;

&lt;p&gt;Here is the sad truth: Intel deprecated SGX on consumer CPUs (11th Gen Core and newer) to focus on Server CPUs (Xeon Scalable).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;If you are on a new MacBook: You are on ARM. No hardware SGX for you.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you are on AMD: Check out SEV (we'll cover that in Part I don't know).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you are on a specialized Cloud Instance (e.g., Azure DC-series): You are golden.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;"But Max, I don't have a Xeon server in my bedroom!"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Relax. Intel provides a &lt;strong&gt;Simulation Mode&lt;/strong&gt;. It emulates the Enclave in software. It offers zero security guarantees (because the OS can see the emulated memory), but it lets you write and debug code. &lt;strong&gt;We will use Simulation Mode for this tutorial&lt;/strong&gt;. So, yes, you can follow along on your standard Linux machine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Installing the SDK (The Toolkit)
&lt;/h2&gt;

&lt;p&gt;We need the Intel SGX SDK for Linux. I’m assuming you are on Ubuntu 20.04 or 22.04.&lt;/p&gt;

&lt;p&gt;Open your terminal and pretend you are a sysadmin:&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="c"&gt;# Install the prerequisites (The usual suspects)&lt;/span&gt;
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;build-essential python-is-python3

&lt;span class="c"&gt;# Download the SGX SDK installer (Check 01.org for the latest version)&lt;/span&gt;
wget https://download.01.org/intel-sgx/sgx-linux/2.20/distro/ubuntu22.04-server/sgx_linux_x64_sdk_2.20.100.4.bin

&lt;span class="c"&gt;# Make it executable and run it&lt;/span&gt;
&lt;span class="nb"&gt;chmod&lt;/span&gt; +x sgx_linux_x64_sdk_&lt;span class="k"&gt;*&lt;/span&gt;.bin
./sgx_linux_x64_sdk_&lt;span class="k"&gt;*&lt;/span&gt;.bin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will ask you where to install. Just say "yes" to /opt/intel. Then, source the environment variables. &lt;strong&gt;Do not forget this, or nothing will work and you will cry.&lt;/strong&gt;&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;source&lt;/span&gt; /opt/intel/sgxsdk/environment
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: The Architecture of a "Hello World"
&lt;/h2&gt;

&lt;p&gt;In a normal program, &lt;code&gt;main()&lt;/code&gt; calls &lt;code&gt;printf()&lt;/code&gt;. Simple.&lt;br&gt;
In SGX, &lt;code&gt;main()&lt;/code&gt; (Untrusted) cannot call &lt;code&gt;printf()&lt;/code&gt; inside the Enclave (Trusted) because the Enclave doesn't have a screen, a keyboard, or an OS. It is a black box.&lt;/p&gt;

&lt;p&gt;We need three files to make this happen:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;App.cpp&lt;/strong&gt; (The Untrusted Host)&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Enclave.cpp&lt;/strong&gt; (The Secret Logic)&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Enclave.edl&lt;/strong&gt; (The Contract)&lt;/li&gt;
&lt;/ol&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%2Fszppti9nnmu9mon7atra.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%2Fszppti9nnmu9mon7atra.png" alt="transfer" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  The Contract: &lt;code&gt;Enclave.edl&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This is the &lt;strong&gt;Enclave Definition Language&lt;/strong&gt;. It defines what functions can cross the boundary.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ECALL (Enclave Call):&lt;/strong&gt; App calls Enclave (Entering).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OCALL (Outside Call):&lt;/strong&gt; Enclave calls App (Exiting).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Create a file named &lt;code&gt;Enclave.edl&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="n"&gt;enclave&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Import standard C library types&lt;/span&gt;
    &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="s"&gt;"sgx_tstdc.edl"&lt;/span&gt; &lt;span class="n"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="n"&gt;trusted&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// ECALL: The App will call this function to say Hello&lt;/span&gt;
        &lt;span class="n"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;trusted_hello_world&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="n"&gt;untrusted&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// OCALL: The Enclave will call this to print to the screen&lt;/span&gt;
        &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="n"&gt;untrusted_print&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="n"&gt;in&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;str&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See what we did? We defined a &lt;code&gt;trusted_hello_world&lt;/code&gt; function that we can call from the outside. But since the Enclave can't print, we also defined an &lt;code&gt;untrusted_print&lt;/code&gt; that the Enclave can call to ask the App to print for it. &lt;strong&gt;It's like a prisoner asking a guard to mail a letter.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Secret: &lt;code&gt;Enclave.cpp&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This code runs inside the protected memory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;"Enclave_t.h"&lt;/span&gt;&lt;span class="c1"&gt; // Auto-generated header (we'll see this later)&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"sgx_trts.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;string.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;trusted_hello_world&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;secret_msg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Hello from the Secure Enclave!"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// We cannot use printf here! We must use the OCALL.&lt;/span&gt;
    &lt;span class="n"&gt;untrusted_print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secret_msg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Host: &lt;code&gt;App.cpp&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This is the normal application that sets everything up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"sgx_urts.h"&lt;/span&gt;&lt;span class="cp"&gt;
#include&lt;/span&gt; &lt;span class="cpf"&gt;"Enclave_u.h"&lt;/span&gt;&lt;span class="c1"&gt; // Auto-generated header for untrusted code&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;
&lt;span class="c1"&gt;// Global Enclave ID&lt;/span&gt;
&lt;span class="n"&gt;sgx_enclave_id_t&lt;/span&gt; &lt;span class="n"&gt;global_eid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// This is the OCALL implementation&lt;/span&gt;
&lt;span class="c1"&gt;// The Enclave calls this when it wants to print something&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;untrusted_print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Enclave says: %s&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;str&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;sgx_status_t&lt;/span&gt; &lt;span class="n"&gt;ret&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// 1. Initialize the Enclave&lt;/span&gt;
    &lt;span class="c1"&gt;// "enclave.signed.so" is the signed library we will build&lt;/span&gt;
    &lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sgx_create_enclave&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"enclave.signed.so"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;SGX_DEBUG_FLAG&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&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;global_eid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ret&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;SGX_SUCCESS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Failed to create Enclave. Error: %x&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ret&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;printf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"App: Enclave created. Calling into the void...&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// 2. Make the ECALL&lt;/span&gt;
    &lt;span class="n"&gt;trusted_hello_world&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;global_eid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// 3. Destroy the Enclave&lt;/span&gt;
    &lt;span class="n"&gt;sgx_destroy_enclave&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;global_eid&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: The Build Nightmare (Edger8r)
&lt;/h2&gt;

&lt;p&gt;You can't just &lt;code&gt;gcc App.cpp&lt;/code&gt;. Oh no.&lt;br&gt;
You have to use a tool called &lt;code&gt;sgx_edger8r&lt;/code&gt;. It parses your &lt;code&gt;.edl&lt;/code&gt; file and generates "Edge Routines" (the glue code that handles the context switching between secure and insecure worlds).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;code&gt;sgx_edger8r&lt;/code&gt; reads &lt;code&gt;Enclave.edl&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt; It generates &lt;code&gt;Enclave_t.c&lt;/code&gt; (Trusted proxy) and &lt;code&gt;Enclave_u.c&lt;/code&gt; (Untrusted proxy).&lt;/li&gt;
&lt;li&gt; You compile the Enclave code + Trusted Proxy into a library.&lt;/li&gt;
&lt;li&gt; You &lt;strong&gt;Sign&lt;/strong&gt; the library using a private key (OpenSSL).&lt;/li&gt;
&lt;li&gt; You compile the App code + Untrusted Proxy.&lt;/li&gt;
&lt;li&gt; Link them together.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I won't paste the 50-line &lt;code&gt;Makefile&lt;/code&gt; here because it looks like ancient hieroglyphics.&lt;/p&gt;

&lt;p&gt;For now, just imagine running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;make &lt;span class="nv"&gt;SGX_MODE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;SIM
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Run it!
&lt;/h2&gt;

&lt;p&gt;If the gods of compilation smile upon you, you will see an executable &lt;code&gt;app&lt;/code&gt;.&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="nv"&gt;$ &lt;/span&gt;./app

App: Enclave created. Calling into the void...
Enclave says: Hello from the Secure Enclave!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Victory.&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%2Fkop5dmzwfujsmuaz6ros.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%2Fkop5dmzwfujsmuaz6ros.png" alt="victory" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why was that so hard?
&lt;/h2&gt;

&lt;p&gt;You might ask: &lt;em&gt;"Max, that was 200 lines of setup for a print statement."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Yes. But think about what actually happened.&lt;br&gt;
That "Hello" string was created in a region of memory that the Operating System &lt;strong&gt;could not read&lt;/strong&gt;.&lt;br&gt;
When &lt;code&gt;untrusted_print&lt;/code&gt; was called, the CPU performed a hardware context switch, exited the protected mode, scrubbed the registers, and handed control back to the OS just to print those characters.&lt;/p&gt;

&lt;p&gt;We just defied the laws of traditional OS hierarchy.&lt;/p&gt;

&lt;h2&gt;
  
  
  Up Next
&lt;/h2&gt;

&lt;p&gt;Now that we have the environment, we need to talk about the &lt;strong&gt;Memory Wall&lt;/strong&gt;.&lt;br&gt;
In the next post, I will show you how to crash your Enclave by allocating too much RAM, and why "Paging" in SGX is a performance killer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part 3: The Memory Wall &amp;amp; Performance Tuning&lt;/strong&gt; is coming soon.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;Leave a Comment 👇&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Did you get the &lt;code&gt;SGX_ERROR_NO_DEVICE&lt;/code&gt; error?&lt;/strong&gt; Or did you actually manage to run it in Simulation Mode?&lt;br&gt;
Drop your error logs in the comments and let's debug this together. &lt;/p&gt;

</description>
      <category>security</category>
      <category>cpp</category>
      <category>linux</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>I Don't Trust AWS (And Neither Should You): Dive into Intel SGX</title>
      <dc:creator>Max Jiang</dc:creator>
      <pubDate>Wed, 03 Dec 2025 06:31:37 +0000</pubDate>
      <link>https://forem.com/max_jiang/i-dont-trust-aws-and-neither-should-you-dive-into-intel-sgx-53e5</link>
      <guid>https://forem.com/max_jiang/i-dont-trust-aws-and-neither-should-you-dive-into-intel-sgx-53e5</guid>
      <description>&lt;p&gt;Let’s be honest for a second. We all deploy our code to the cloud. It’s easy, it’s scalable, and it’s someone else’s problem when the hard drive fails.&lt;/p&gt;

&lt;p&gt;But here is the question that keeps me up at night (okay, maybe it's the caffeine, but this helps): &lt;strong&gt;Who owns the computer you're running on?&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%2Fpmx5vzrfrs0fsjd472sd.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%2Fpmx5vzrfrs0fsjd472sd.png" alt="Stolen data" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Jeff Bezos owns it. Or Satya Nadella. Or some sysadmin named Dave who hasn't had his coffee yet and has root access to the hypervisor hosting your Docker container.&lt;/p&gt;

&lt;p&gt;If you are processing generic web traffic, you probably don't care. But what if you are handling biometric data? Or a bank’s private keys? Or a fine-tuned LLM weight matrix that cost &lt;strong&gt;$5 million&lt;/strong&gt; to train?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Traditional security&lt;/strong&gt; says: "Harden the OS, set up firewalls, trust &lt;code&gt;root&lt;/code&gt;."&lt;br&gt;
&lt;strong&gt;Confidential Computing&lt;/strong&gt; says: "Trust no one. Not even the operating system. Especially not Dave."&lt;/p&gt;

&lt;p&gt;Welcome to my series on Trusted Execution Environments (TEEs). Over the next few posts, I’m going to drag you through the mud of learning &lt;strong&gt;Intel SGX&lt;/strong&gt;. Why? Because it’s painful, it’s powerful, and it might just be the future of cloud security.&lt;/p&gt;
&lt;h2&gt;
  
  
  The "God Mode" Problem
&lt;/h2&gt;

&lt;p&gt;In the classic x86 architecture, the Operating System (Ring 0) is God. It can read any memory address, pause any process, and inspect any register. Your application (Ring 3) lives at the mercy of the kernel.&lt;/p&gt;

&lt;p&gt;If a hacker gets root access, or if a malicious insider at the cloud provider decides to dump the RAM, your secrets are gone. Plain text. Game over.&lt;/p&gt;

&lt;p&gt;This is where &lt;strong&gt;Intel SGX (Software Guard Extensions)&lt;/strong&gt; flips the table.&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%2Fl6zt7qvgzfnlw8tjupkf.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%2Fl6zt7qvgzfnlw8tjupkf.png" alt="Normal World VS Secure World" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine you are staying in a hotel (&lt;strong&gt;the Cloud Server&lt;/strong&gt;). The hotel manager (&lt;strong&gt;the OS&lt;/strong&gt;) has a master key. They can enter your room anytime.&lt;br&gt;
SGX is like bringing a titanium safe (&lt;strong&gt;the Enclave&lt;/strong&gt;) into that hotel room.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The manager lets you into the room.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You go inside the safe and lock it from the inside.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You do your work inside the safe.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Even though the manager owns the building, &lt;strong&gt;they cannot see inside the safe.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If they try to drill into it (read the memory), the CPU literally returns garbage data (&lt;code&gt;0xFF&lt;/code&gt;) or crashes the system. It’s hardware-level enforcement.&lt;/p&gt;
&lt;h2&gt;
  
  
  Under the Hood: The Enclave
&lt;/h2&gt;

&lt;p&gt;So, how does this actually look in code? It's not magic, it's just... complicated.&lt;/p&gt;

&lt;p&gt;When you write an SGX application, you split your brain (and your code) into two parts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;The Untrusted Part (App):&lt;/strong&gt; This is your normal C/C++ code. It talks to the network, reads files, and prints to the console. It lives in the dangerous world.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;The Trusted Part (Enclave):&lt;/strong&gt; This is the fortress. It contains your secrets and the algorithms that process them.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The communication between them is strictly controlled. You can't just call a function. You have to perform an &lt;strong&gt;ECALL&lt;/strong&gt; (Entering the Enclave) or an &lt;strong&gt;OCALL&lt;/strong&gt; (Calling out of the Enclave).&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%2F9r1l9ra4sejl9brtj8wa.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%2F9r1l9ra4sejl9brtj8wa.png" alt="The wall" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It looks something like this (simplified for sanity):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// UNTRUSTED WORLD (Main App)&lt;/span&gt;
&lt;span class="c1"&gt;// "Hey Enclave, please take this encrypted data and process it."&lt;/span&gt;
&lt;span class="n"&gt;sgx_status_t&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;trusted_process_secret&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;eid&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;encrypted_data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// ---------------- THE HARDWARE WALL ----------------&lt;/span&gt;

&lt;span class="c1"&gt;// TRUSTED WORLD (Enclave)&lt;/span&gt;
&lt;span class="c1"&gt;// The CPU encrypts memory access here. Even the OS sees scrambled eggs.&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;trusted_process_secret&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;char&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="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// 1. Decrypt data inside CPU cache (never hits RAM in plain text)&lt;/span&gt;
    &lt;span class="c1"&gt;// 2. Do the math&lt;/span&gt;
    &lt;span class="c1"&gt;// 3. Encrypt result&lt;/span&gt;
    &lt;span class="c1"&gt;// 4. Return&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The crazy part? The data inside the Enclave is encrypted in RAM. It is only decrypted inside the CPU die. If you put a logic analyzer on the memory bus, you see encrypted noise.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is this relevant to our career?
&lt;/h2&gt;

&lt;p&gt;You might be thinking, "I build React apps, why do I care?"&lt;/p&gt;

&lt;p&gt;Because Privacy-Enhancing Technologies (PETs) are exploding.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Web3 &amp;amp; Blockchain: Many Layer 2 solutions use TEEs for off-chain computation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;AI Security: How do you let a hospital use your AI model on patient data without seeing the patient data and without the hospital stealing your model? SGX is the answer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Regulation: GDPR and industry standards are pushing for "Data in Use" protection.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Knowing how to architect systems where you mathematically prove to a user that you can't steal their data? That’s a superpower.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Catch (There's always a catch)
&lt;/h2&gt;

&lt;p&gt;If SGX is so good, why isn't everyone using it?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Performance: Jumping in and out of an Enclave is expensive.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Memory Limits: For a long time, you only had 128MB of protected memory (EPC). Now with SGX2 we have more, but it's still precious real estate.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Complexity: Debugging inside an Enclave is a nightmare. You can't just printf. (Well, you can, but it requires an OCALL and it’s slow).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Vulnerabilities: Ever heard of Spectre or Foreshadow? We'll talk about those later. It’s an arms race between Intel and security researchers.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What's Next?
&lt;/h2&gt;

&lt;p&gt;This was just the high-level pitch. In the next post, I’m going to open my terminal, and we are going to write a "Hello World" that requires a kernel driver, a signing key, and about 200 lines of boilerplate code.&lt;/p&gt;

&lt;p&gt;It’s going to be fun. Or painful. Probably both.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stay tuned for Part 2: Installing the Beast.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>security</category>
      <category>cloud</category>
      <category>systems</category>
      <category>cpp</category>
    </item>
  </channel>
</rss>
