2021-08-26 04:36:37 +00:00
# Encrypt Messages Using Waku Message Version 1
The Waku Message format provides an easy way to encrypt messages using symmetric or asymmetric encryption.
The encryption comes with several handy [design requirements ](https://rfc.vac.dev/spec/26/#design-requirements ):
confidentiality, authenticity and integrity.
You can find more details about Waku Message Payload Encryption in [26/WAKU-PAYLOAD ](https://rfc.vac.dev/spec/26/ ).
## What data is encrypted
2021-08-26 06:27:17 +00:00
With Waku Message Version 1, the entire payload is encrypted.
2021-08-26 04:36:37 +00:00
Which means that the only discriminating data available in clear text is the content topic and timestamp (if present).
2021-08-26 06:27:17 +00:00
Hence, if Alice expects to receive messages under a given content topic, she needs to try to decrypt all messages received on said content topic.
2021-08-26 04:36:37 +00:00
This needs to be kept in mind for scalability and forward secrecy concerns:
2021-08-26 06:27:17 +00:00
- If there is high traffic on a given content topic then all clients need to process and attempt decryption of all messages with said content topic;
- If a content topic is only used by a given (group of) user(s) then it is possible to deduce some information about said user(s) communications such as sent time and frequency of messages.
2021-08-26 04:36:37 +00:00
## Key management
2021-08-26 06:27:17 +00:00
By using Waku Message Version 1, you will need to provide a way to your users to generate and store keys in a secure manner.
2021-08-26 04:36:37 +00:00
Storing, backing up and recovering key is out of the scope of this guide.
2021-08-27 04:40:46 +00:00
If key recovery is important for your dApp, then check out
2021-08-30 09:54:36 +00:00
[SubtleCrypto.wrapKey() ](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/wrapKey ) which can be used to securely store or export private keys.
2021-08-27 04:40:46 +00:00
An example to save and load a key pair in local storage, protected with a password, can be found in [Eth-PM ](https://github.com/status-im/js-waku/blob/main/examples/eth-pm/src/key_pair_handling/key_pair_storage.ts ).
2021-08-26 04:36:37 +00:00
2021-09-02 03:48:28 +00:00
## Which encryption method should I use?
2021-08-26 04:36:37 +00:00
Whether you should use symmetric or asymmetric encryption depends on your use case.
**Symmetric** encryption is done using a single key to encrypt and decrypt.
2021-08-26 06:27:17 +00:00
Which means that if Alice knows the symmetric key `K` and uses it to encrypt a message,
2021-08-26 04:36:37 +00:00
she can also use `K` to decrypt any message encrypted with `K` ,
even if she is not the sender.
Group chats is a possible use case for symmetric encryption:
2021-08-26 06:27:17 +00:00
All participants can use an out-of-band method to agree on a `K` .
2021-08-26 04:36:37 +00:00
Participants can then use `K` to encrypt and decrypt messages within the group chat.
Participants MUST keep `K` secret to ensure that no external party can decrypt the group chat messages.
**Asymmetric** encryption is done using a key pair:
the public key is used to encrypt messages,
the matching private key is used to decrypt messages.
For Alice to encrypt a message for Bob, she needs to know Bob's Public Key `K` .
Bob can then use his private key `k` to decrypt the message.
As long as Bob keep his private key `k` secret, then he, and only he, can decrypt messages encrypted with `K` .
2021-08-26 06:27:17 +00:00
Private 1:1 messaging is a possible use case for asymmetric encryption:
2021-08-26 04:36:37 +00:00
When Alice sends an encrypted message for Bob, only Bob can decrypt it.
## Symmetric Encryption
### Generate Key
To use symmetric encryption, you first need to generate a key.
2021-09-02 03:48:28 +00:00
Use `generateSymmetricKey` for secure key generation:
2021-08-26 04:36:37 +00:00
```js
2021-08-30 10:41:18 +00:00
import { generateSymmetricKey } from 'js-waku';
2021-08-26 04:36:37 +00:00
2021-09-02 03:48:28 +00:00
const symmetricKey = generateSymmetricKey();
2021-08-26 04:36:37 +00:00
```
### Encrypt Message
To encrypt a message with the previously generated key,
pass the key in the `symKey` property to `WakuMessage.fromBytes` .
2021-08-27 04:59:36 +00:00
Same as Waku Messages version 0 (unencrypted),
2021-08-26 04:36:37 +00:00
`payload` is your message payload and `contentTopic` is the content topic for your dApp.
See [Receive and Send Messages Using Waku Relay ](relay-receive-send-messages.md ) for details.
```js
import { WakuMessage } from 'js-waku';
const message = await WakuMessage.fromBytes(payload, contentTopic, {
2021-09-02 03:48:28 +00:00
symKey: symmetricKey
2021-08-26 04:36:37 +00:00
});
```
The Waku Message can then be sent to the Waku network using [Waku Relay ](relay-receive-send-messages.md ) or Waku Light Push:
```js
await waku.lightPush.push(message);
```
### Decrypt Messages
2021-09-02 03:48:28 +00:00
To decrypt messages,
whether they are received over Waku Relay or using Waku Store,
add the symmetric key as a decryption key to your Waku instance.
2021-08-26 04:36:37 +00:00
2021-09-02 03:48:28 +00:00
```js
waku.addDecryptionKey(symmetricKey);
```
Alternatively, you can pass the key when creating the instance:
2021-08-26 04:36:37 +00:00
```js
2021-09-02 03:48:28 +00:00
import { Waku } from 'js-waku';
const waku = Waku.create({ decryptionKeys: [symmetricKey] });
2021-08-26 04:36:37 +00:00
```
2021-09-02 03:48:28 +00:00
It will attempt to decrypt any message it receives using the key, for both symmetric and asymmetric encryption.
2021-08-26 04:36:37 +00:00
You can call `addDecryptionKey` several times if you are using multiple keys,
symmetric key and asymmetric private keys can be used together.
Messages that are not successfully decrypted are dropped.
## Asymmetric Encryption
### Generate Key Pair
2021-09-02 03:48:28 +00:00
To use asymmetric encryption, you first need to generate a private key and calculate the corresponding public key.
Use `generatePrivateKey` for secure key generation:
2021-08-26 04:36:37 +00:00
```js
import { generatePrivateKey, getPublicKey } from 'js-waku';
const privateKey = generatePrivateKey();
const publicKey = getPublicKey(privateKey);
```
The private key must be securely stored and remain private.
If leaked then other parties may be able to decrypt the user's messages.
The public key is unique for a given private key and can always be recovered given the private key,
hence it is not needed to save it as long as as the private key can be recovered.
### Encrypt Message
2021-08-26 06:27:17 +00:00
The public key is used to encrypt messages;
to do so, pass it in the `encPublicKey` property to `WakuMessage.fromBytes` .
2021-08-26 04:36:37 +00:00
Same as clear Waku Messages,
`payload` is your message payload and `contentTopic` is the content topic for your dApp.
See [Receive and Send Messages Using Waku Relay ](relay-receive-send-messages.md ) for details.
```js
import { WakuMessage } from 'js-waku';
const message = await WakuMessage.fromBytes(payload, contentTopic, {
encPublicKey: publicKey
});
```
The Waku Message can then be sent to the Waku network using [Waku Relay ](relay-receive-send-messages.md ) or Waku Light Push:
```js
await waku.lightPush.push(message);
```
### Decrypt Messages
The private key is needed to decrypt messages.
2021-09-02 03:48:28 +00:00
To decrypt messages,
whether they are received over Waku Relay or using Waku Store,
add the private key as a decryption key to your Waku instance.
```js
waku.addDecryptionKey(privateKey);
```
Alternatively, you can pass the key when creating the instance:
2021-08-26 04:36:37 +00:00
```js
2021-09-02 03:48:28 +00:00
import { Waku } from 'js-waku';
const waku = Waku.create({ decryptionKeys: [privateKey] });
2021-08-26 04:36:37 +00:00
```
2021-09-02 03:48:28 +00:00
It will attempt to decrypt any message it receives using the key, for both symmetric and asymmetric encryption.
2021-08-26 04:36:37 +00:00
You can call `addDecryptionKey` several times if you are using multiple keys,
symmetric key and asymmetric private keys can be used together.
Messages that are not successfully decrypted are dropped.
## Handling `WakuMessage` instances
When creating a Waku Message using `WakuMessage.fromBytes` with an encryption key (symmetric or asymmetric),
the payload gets encrypted.
Which means that `wakuMessage.payload` returns an encrypted payload:
```js
import { WakuMessage } from 'js-waku';
const message = await WakuMessage.fromBytes(payload, contentTopic, {
encPublicKey: publicKey
});
console.log(message.payload); // This is encrypted
```
2021-08-26 06:27:17 +00:00
However, `WakuMessage` instances returned by `WakuRelay` or `WakuStore` are always decrypted.
2021-08-26 04:36:37 +00:00
2021-08-26 06:27:17 +00:00
`WakuRelay` and `WakuStore` never return messages that are encrypted.
If a message was not successfully decrypted, then it will be dropped from the results.
2021-08-26 04:36:37 +00:00
2021-08-26 06:27:17 +00:00
Which means that `WakuMessage` instances returned by `WakuRelay` and `WakuStore` always have a clear payload (in regard to Waku Message version 1):
2021-08-26 04:36:37 +00:00
```js
2021-09-02 03:48:28 +00:00
import { Waku } from 'js-waku';
const waku = Waku.create({ decryptionKeys: [privateKey] });
const messages = await waku.store.queryHistory([contentTopic]);
2021-08-26 04:36:37 +00:00
if (messages & & messages[0]) {
console.log(messages[0].payload); // This payload is decrypted
}
waku.relay.addObserver((message) => {
console.log(message.payload); // This payload is decrypted
}, [contentTopic]);
```
2021-08-26 06:33:06 +00:00
## Code Example
The [Eth-PM ](https://github.com/status-im/js-waku/tree/main/examples/eth-pm ) Web App example demonstrates both the use of symmetric and asymmetric encryption.
Asymmetric encryption is used for private messages so that only the intended recipient can read said messages.
Symmetric encryption is used for the public key messages.
In this instance, the same key is used for all users: the Keccak-256 hash of the content topic (which results in 32 bytes array).
While this does not add functional value, it does demonstrate the usage of symmetric encryption in a web app.
A live version of Eth-PM can be found at https://status-im.github.io/js-waku/eth-pm/.
The specifications of the protocol it implements can be found at [20/TOY-ETH-PM ](https://rfc.vac.dev/spec/20/ ).