Encrypting and Decrypting Files

Most nontrivial applications work with files as inputs, or outputs, or both. Such files are one of the first places an attacker looks for sensitive data. The Anjuna SGX Runtime provides features that make those files unreadable to attackers, even when they have privileged access to the host.

This section shows how to use the Anjuna SGX Runtime’s tools to encrypt and decrypt sensitive files.

Encrypt an Input File

In this section we show how to encrypt a file input to an application so that it’s unreadable by any process except an application protected by the Anjuna SGX Runtime.

Begin by creating an HTML file named index.html with the following contents:

<html>
  <body>
    Hello from Anjuna!
  </body>
</html>

We’ll serve this HTML file from a simple web server protected by the Anjuna SGX Runtime. The web server we’ll use is provided as part of the Python 3 standard library. So that we can run it with the protection of a secure enclave, we’ll create a manifest template for Python 3.

Create the manifest template by running the following command:

$ anjuna-sgxrun --setup python3

Next, we’ll encrypt index.html with the Anjuna tools so that only the web server process running in the secure enclave can read it. In order to configure the secure enclave to decrypt the file automatically, we must add its absolute path to the encrypted_files section of the manifest template.

First, use the following command to get the absolute path of the file:

$ echo $PWD/index.html

Then edit manifest.template.yaml, the manifest template file. Find the keys and encrypted_files keys and add the following entries:

keys:
- id: html_encryption
  source: enclave_generated

encrypted_files:
- path: <PASTE ABSOLUTE PATH HERE>
  key: html_encryption

Replace the text <PASTE ABSOLUTE PATH HERE> with the absolute path returned by the echo command, above.

Now, create an enclave key pair by running the following command:

$ anjuna-sgxrun --provision python3

The command creates three files:

provision/python3.pubkey

The secure enclave’s public key

provision/python3.key.sealed

The secure enclave’s private key, encrypted so that only the enclave can decrypt it

provision/python3.quote.bin

An attestation quote that enables you to verify that the public key matches the secure enclave

You can give the public key and the attestation quote to any client that needs to encrypt files to be used by the protected web server. For example, we will shortly encrypt index.html, making it unreadable to any process other than the protected Python 3 process running in the secure enclave.

A client can use the attestation quote to verify that the public key matches the secure enclave by running the following command:

$ anjuna-check-attestation --quote-file provision/python3.quote.bin
                           --rsa-key-file provision/python3.pubkey

The tool’s output indicates whether the provided key correctly matches the quote file’s enclave.

Having verified the correctness of the public key, we can use it to encrypt index.html for the secure enclave that we configured for Python 3. Encrypt it by running the following command:

$ anjuna-encrypt --public-key provision/python3.pubkey index.html

anjuna-encrypt creates the file index.html.sealed, containing the encrypted contents of index.html. You can now delete the original index.html and rename index.html.sealed to index.html. At this point, index.html contains the encrypted contents of the original index.html. Its decrypted cleartext is completely inaccessible to any process or client, except for Python 3 running inside the secure enclave.

You can check whether the file has been encrypted by anjuna-encrypt by running the following command:

$ hexdump -C index.html

The contents of a file encrypted by anjuna-encrypt start with the text ANJUNAFS.

Serve the Encrypted File

You can serve the encrypted index.html by running Python’s built-in web server in the directory that contains the encrypted file.

Run the following command:

$ anjuna-sgxrun python3 -m http.server 8000

While the Python web server runs, use a browser on the same host to visit the URL http://localhost:8000. You should see a page that displays the text "Hello from Anjuna!". The Python web server can read the contents of the encrypted index.html because we’ve configured the secure enclave to decrypt it automatically. No other process, regardless of what privileges it may have on the host, can read the contents of index.html.