# Encryption

> Hide push content from Apple, Google, and the Opush server with shared-key or end-to-end encryption.

A normal push passes through Opush and through Apple's or Google's push service in plain text. Encryption hides the content on the way. Only your devices can read it.

Opush has three options:

| Option | Who encrypts | Who can read it |
|---|---|---|
| [Shared-key encryption](#shared-key-encryption) | Your script, with an AES key | Anyone with the key |
| [Server encryption](#server-encryption) | The Opush server, with a key your device set | Your devices (the server sees plain text when it arrives) |
| [End-to-end encryption](#end-to-end-encryption-hpke) | Your script, with your device's public key | Only the device that holds the private key |

## Shared-key encryption [#shared-key-encryption]

This is the common way to encrypt pushes. If you already have a script that sends `ciphertext`, change the server and key and it works.

1. Open the encryption settings in the Opush app. Pick an algorithm and a key. New users get AES-256-GCM with a key the app creates for you.
2. In your script, write the push parameters as JSON, for example `{"title":"Login code","body":"381920","copy":"381920"}`.
3. Encrypt the JSON with the same algorithm and key, then base64 the result.
4. Send it as `ciphertext`, with `iv` for GCM and CBC.

### Algorithms [#algorithms]

| Algorithm | Key | IV |
|---|---|---|
| AES-128-GCM / AES-192-GCM / AES-256-GCM | 16 / 24 / 32 characters | 12 characters |
| AES-128-CBC / AES-192-CBC / AES-256-CBC | 16 / 24 / 32 characters | 16 characters, PKCS#7 padding |
| AES-128-ECB / AES-192-ECB / AES-256-ECB | 16 / 24 / 32 characters | Not used |

- The key and IV are plain ASCII strings, used as bytes directly. They are not hex.
- For GCM, `ciphertext` is base64 of the encrypted bytes followed by the 16-byte authentication tag.
- If a push has no `iv`, the app uses the IV saved in its encryption settings.

<Callout type="warn" title="Use a new IV for every push">
  Reusing an IV with GCM can leak your content. Generate a random IV for each push and send it as `iv`. ECB is not secure. It exists only for old scripts.
</Callout>

### Examples [#examples]

<Tabs items={["curl (CBC)", "Node.js (GCM)", "Python (GCM)"]}>
<Tab value="curl (CBC)">

```bash
KEY='your16charkey123'          # AES-128-CBC: 16 characters
IV=$(openssl rand -hex 8)        # 16 characters
JSON='{"title":"Login code","body":"381920","copy":"381920"}'

CIPHERTEXT=$(printf '%s' "$JSON" | openssl enc -aes-128-cbc \
  -K "$(printf '%s' "$KEY" | xxd -p -c 256)" \
  -iv "$(printf '%s' "$IV" | xxd -p -c 256)" | openssl base64 -A)

curl https://opu.sh/YOUR_KEY \
  --data-urlencode "ciphertext=$CIPHERTEXT" \
  --data-urlencode "iv=$IV"
```

`openssl enc` can't do GCM, so this example uses CBC. Set the app to the same algorithm.

</Tab>
<Tab value="Node.js (GCM)">

```js
import { createCipheriv, randomBytes } from "node:crypto";

const KEY = "your-32-character-key-goes-here!"; // AES-256-GCM: 32 characters
const iv = randomBytes(6).toString("hex"); // 12 characters

const json = JSON.stringify({ title: "Login code", body: "381920", copy: "381920" });
const cipher = createCipheriv("aes-256-gcm", Buffer.from(KEY, "utf8"), Buffer.from(iv, "utf8"));
const encrypted = Buffer.concat([cipher.update(json, "utf8"), cipher.final(), cipher.getAuthTag()]);

await fetch("https://opu.sh/push", {
  method: "POST",
  headers: { "content-type": "application/json" },
  body: JSON.stringify({ device_key: "YOUR_KEY", ciphertext: encrypted.toString("base64"), iv }),
});
```

</Tab>
<Tab value="Python (GCM)">

```python
import base64, json, secrets
import requests
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

KEY = "your-32-character-key-goes-here!"  # AES-256-GCM: 32 characters
iv = secrets.token_hex(6)  # 12 characters

plaintext = json.dumps({"title": "Login code", "body": "381920", "copy": "381920"}).encode()
# AESGCM returns the encrypted bytes with the 16-byte tag at the end
encrypted = AESGCM(KEY.encode()).encrypt(iv.encode(), plaintext, None)

requests.post("https://opu.sh/push", json={
    "device_key": "YOUR_KEY",
    "ciphertext": base64.b64encode(encrypted).decode(),
    "iv": iv,
})
```

</Tab>
</Tabs>

### What goes inside, what stays outside [#what-goes-inside-what-stays-outside]

You can put any parameter inside the encrypted JSON: content, `sound`, `badge`, `call`, `action`, `isArchive`, `ttl`, buttons, and so on. The app applies them after it decrypts. Values inside the JSON override the same values outside.

Some parameters must stay outside in plain text, because the server needs them to deliver the push:

| Parameter | Why |
|---|---|
| `device_key`, `device_keys` | The server needs to know where to send it |
| `id`, `delete` | They decide which notification to replace or [recall](/docs/push/update-and-recall) |
| `level`, `volume` | They set the interruption level and critical sound |
| `at`, `delay`, `repeat`, `tz` | The server holds [scheduled pushes](/docs/push/schedule) until it's time |
| `call`, `voice`, `sms`, `phone`, `escalate`, `escalate_after` | The server places [calls and texts](/docs/push/call-alerts) |

Apple and Google only see a placeholder text ("You received an encrypted message"). The app replaces it with the real content after it decrypts. If decryption fails, the notification says so. Check that the algorithm, key, and IV match.

## Server encryption [#server-encryption]

If your tool can't encrypt (for example, it only takes a URL), let the server do it. Turn on **Server encryption** in the app's encryption settings. From then on, Opush encrypts every plain-text push to that device before it hands it to Apple or Google.

- Supported: AES-128-GCM, AES-256-GCM, AES-128-CBC, AES-256-CBC, and your end-to-end public key. ECB and 192-bit keys are not available here.
- Encrypted fields: `title`, `subtitle`, `body`, `markdown`, `url`, `copy`, `icon`, `image`, `action`, and buttons (button URLs often carry tokens). Fields like `group`, `sound`, `badge`, `level`, and `id` stay outside.
- The server makes a new random IV for every push.
- Pushes that already have `ciphertext` or `sealed` are not encrypted again. Recalls are not encrypted.

<Callout type="info">
  The server still sees the plain text at the moment it receives it. If you don't want the server to see your content at all, encrypt in your script.
</Callout>

## End-to-end encryption (HPKE) [#end-to-end-encryption-hpke]

With a shared key, anyone who gets the key can read every push: your script, a relay, or a leaked config file. End-to-end encryption uses a key pair instead. Scripts and the server only get the public key. Only the device with the private key can open the push.

1. In the app's encryption settings, open **End-to-end encryption** and tap **Create a key pair**. The private key stays in the device's keychain.
2. Share the public key with your script. It's base64 of a 65-byte uncompressed P-256 point (starts with `0x04`). You can also export it as PEM.
3. Seal the parameter JSON with the public key and send it as `sealed`.

| | |
|---|---|
| Scheme | HPKE (RFC 9180), base mode |
| KEM / KDF / AEAD | DHKEM(P-256, HKDF-SHA256) / HKDF-SHA256 / AES-256-GCM |
| `info` | `opush v1` (UTF-8) |
| AAD | None |
| `sealed` | base64(`enc` ‖ ciphertext), where `enc` is the 65-byte encapsulated key |

The plain text is the same parameter JSON as `ciphertext`, and the same rules apply for what stays outside. If a push has both `sealed` and `ciphertext`, Opush uses `sealed`.

```js
import { Aes256Gcm, CipherSuite, DhkemP256HkdfSha256, HkdfSha256 } from "@hpke/core";

const PUBLIC_KEY = "BASE64_PUBLIC_KEY_FROM_THE_APP";

const suite = new CipherSuite({ kem: new DhkemP256HkdfSha256(), kdf: new HkdfSha256(), aead: new Aes256Gcm() });
const recipient = await suite.kem.deserializePublicKey(Buffer.from(PUBLIC_KEY, "base64"));
const sender = await suite.createSenderContext({ recipientPublicKey: recipient, info: new TextEncoder().encode("opush v1") });
const ct = await sender.seal(new TextEncoder().encode(JSON.stringify({ title: "Login code", body: "381920" })));
const sealed = Buffer.concat([Buffer.from(sender.enc), Buffer.from(ct)]).toString("base64");

await fetch("https://opu.sh/push", {
  method: "POST",
  headers: { "content-type": "application/json" },
  body: JSON.stringify({ device_key: "YOUR_KEY", sealed }),
});
```

Python and Go have HPKE libraries too. Use the same suite and `info`.

### More than one device [#more-than-one-device]

Each device makes its own key pair. To read the same pushes on several devices, export the private key from one device and import it on the others (PEM, base64 DER, or the raw 32-byte key in base64).

### In the app [#in-the-app]

- **Server encryption with your public key**: pick the public key as the server encryption method. The server stores only the public key, so even a database leak can't open past pushes.
- **Send tab**: when you add a recipient with a public key, the app seals each push on your phone before sending it. The server only sees `sealed`.
- **Receive tab**: the "let others push to you" QR code and link include your public key when you have a key pair. Apple Watch seals pushes the same way.

## Limits [#limits]

- [Live progress](/docs/push/live-progress) can't be encrypted. The Lock Screen card has no way to decrypt it.
- An encrypted push still has to fit the 4096-byte push size limit after base64.
- [SMS](/docs/push/call-alerts#sms-and-phone-calls) for a push you encrypted yourself (`ciphertext` or `sealed`) only says that you have a new encrypted push. With server encryption, the server writes the text before it encrypts the push, so the text includes the title and body. Leave out `sms=1` if that content shouldn't go out by text.
