---
title: 'Post-Quantum Cryptography (Part 1)'
excerpt: 'Diving into post-quantum cryptography and configuring it for iOS.'
date: 2025-02-01
categories: ios, security
featureImage: '/images/post-quantum-cryptography.webp'

prev:
  text: 'Making the Hardware Tick'
  link: '/blog/making-the-hardware-tick'

next:
  text: 'Post-Quantum Cryptography (Part 2)'
  link: '/blog/post-quantum-cryptography-2'
---

> [!TIP] Something I follow around -
> _Read, explore, build, break, fix — that’s how I understand._

Last Friday evening, I opened my Notes app to find a password I had saved a while ago. Scrolling through the list, I
realized: this isn't ideal. I needed a password manager. Sure, there are plenty out there, but what would I actually
learn from that? Might as well build something myself.

I started looking into **how to encrypt passwords** while only remembering one thing: a `Master Password`. The usual
stuff came up: **public/private keys, key derivation, encryption**. Then I came across **Shor's algorithm** and
**Post-Quantum Cryptography (PQC)**, and that sent me straight down the rabbit hole. Two days later, I was deep into
**quantum-safe encryption**, figuring out how to **future-proof** passwords.

[//]: # (add tooltips here)
Scrolling and searching around, I found [NIST](https://www.nist.gov/) and
**[Open Quantum Safe](https://openquantumsafe.org/)**. Eventually, I landed on
[`liboqs`](https://github.com/open-quantum-safe/liboqs), but there was a catch - no cross-platform precompiled
libraries. I found a few implementations on `pub.dev` for Flutter, but they looked way too sketchy.

---

### Taking the hard way

Compiling `liboqs` for iOS and the iOS Simulator (for preview) was a hassle, something I usually try to avoid.

Each target required separate architectures: `arm64` for my physical iOS device and `arm64 + x86_64` for the simulator

Then came the intricacies of `cmake`, `ninja` and `openssl`, and of course, `Xcode` being a complete **fuck-up** in what
it does.

---

### Build Process

1. Setup `OpenSSL`
    ```shell
    git clone https://github.com/krzyzanowskim/OpenSSL ./openssl
    ```

2. Clone and build `liboqs`

   ```shell
   git clone https://github.com/open-quantum-safe/liboqs ./liboqs
   cd ./liboqs
   ```

    - **Build for iOS (arm64)**
   ```shell
   cmake -G Ninja ../ \
       -DCMAKE_SYSTEM_NAME=iOS \
       -DCMAKE_OSX_ARCHITECTURES="arm64" \
       -DCMAKE_OSX_SYSROOT="$(xcrun --sdk iphoneos --show-sdk-path)" \
       -DCMAKE_INSTALL_PREFIX=../iphoneos \
       -DOQS_DIST_BUILD=ON \
       -DOQS_PERMIT_UNSUPPORTED_ARCHITECTURE=ON \
       -DOPENSSL_ROOT_DIR=../openssl/iphoneos \
       -DOPENSSL_CRYPTO_LIBRARY=../openssl/iphoneos/lib/libcrypto.a \
       -DOPENSSL_SSL_LIBRARY=../openssl/iphoneos/lib/libssl.a \
       -DOPENSSL_INCLUDE_DIR=../openssl/iphoneos/include
   
   ninja
   ninja install
   ```

    - **Build for iOS Simulator (arm64, x86_64)**
   ```shell    
   cmake -G Ninja ../ \
      -DCMAKE_SYSTEM_NAME=iOS \
      -DCMAKE_OSX_ARCHITECTURES="x86_64;arm64" \
      -DCMAKE_OSX_SYSROOT="$(xcrun --sdk iphonesimulator --show-sdk-path)" \
      -DCMAKE_INSTALL_PREFIX=../iphonesimulator \
      -DOQS_DIST_BUILD=ON \
      -DOQS_PERMIT_UNSUPPORTED_ARCHITECTURE=ON \
      -DOPENSSL_ROOT_DIR=../openssl/iphonesimulator \
      -DOPENSSL_CRYPTO_LIBRARY=../openssl/iphonesimulator/lib/libcrypto.a \
      -DOPENSSL_INCLUDE_DIR=../openssl/iphonesimulator/include
   
   ninja
   ninja install
   ```

    - **Create `xcframework`**
   ```shell
   xcodebuild -create-xcframework \
    -library ./iphoneos/lib/liboqs.a -headers ./iphoneos/include \
    -library ./iphonesimulator/lib/liboqs.a -headers ./iphonesimulator/include \
    -output ./liboqs.xcframework
   ```

3. Import `liboqs.xcframework` into the app\
   Once the `.xcframework` is created, it can be easily integrated into your app.

4. Create a bridging header:
   ```objective-c
   #ifndef Bridging_Header_h
   #define Bridging_Header_h

   // for swift lang
   #include <oqs/oqs.h>

   #endif
   ```
   Then, specify the **Objective-C Bridging Header** under\
   **Targets** > **Your Target** > **Build Settings** > **Objective-C Bridging Header** >
   `$(PROJECT_DIR)/PATH_TO_FOLDER_IF_NESTED/Bridging_Header.h`.

---

### Next Steps

In Part 2, I walk through building a Proof of Concept (POC) app that encrypts and decrypts data using the
quantum-safe libraries we set up while also covering the challenges encountered along the way and how they were
resolved.