tiny-crypto-suite
Version:
Tiny tools, big crypto β seamless encryption and certificate handling for modern web and Node apps.
274 lines (167 loc) β’ 8.98 kB
Markdown
# π TinyOlm IndexedDB Encryption Mode
This tutorial provides a info around the TinyOlm API to perform secure encryption and decryption using only **IndexedDB**.
It includes support for both **individual Olm sessions** and **group Megolm sessions**, with all cryptographic sessions serialized and retrieved directly from `indexedDB`.
## π Local Storage Mode
To use the IndexedDB-based encryption mode, make sure to **disable the default local memory session system**:
```js
tinyOlm.setUseLocal(false);
```
This is required so that all session operations (such as loading or saving sessions) will exclusively use IndexedDB instead of in-memory storage.
This ensures persistence across browser reloads and isolates the module for more consistent and stateful encryption behavior.
---
## π¦ Features
This module offers the following methods for secure messaging and data exchange:
### π§ One-to-One Communication
- `encryptMessageV2(toUsername, plaintext)`
- Encrypts a plaintext string for a specific user using an existing Olm session from IndexedDB.
- `decryptMessageV2(fromUsername, messageType, ciphertext)`
- Decrypts an encrypted message received from a specific user.
- `encryptV2(toUsername, data)`
- Encrypts any content (object, number, etc.) to a user.
- `decryptV2(fromUsername, messageType, plaintext, expectedType?)`
- Decrypts content and optionally verifies the data type after deserialization.
### π₯ Group Communication (Megolm)
- `encryptGroupMessageV2(roomId, plaintext)`
- Encrypts a plaintext string using the outbound group session for a room.
- `decryptGroupMessageV2(roomId, userId, encryptedMessage)`
- Decrypts a group message using the appropriate inbound session from IndexedDB.
- `encryptGroupContentV2(roomId, data)`
- Encrypts any structured content (not just strings) for a group session.
- `decryptGroupContentV2(roomId, userId, encryptedMessage, expectedType?)`
- Decrypts and optionally verifies the type of decrypted group content.
---
## β οΈ Requirements
Before calling any of the encryption/decryption methods:
- You **must** already have a pickled session stored in IndexedDB.
- This module assumes session management (creation and storage) has been done ahead of time.
---
## π§ Note
All encryption and decryption operations internally:
- Retrieve the session from IndexedDB
- Unpickle the session with a password
- Free the session memory securely after use
---
## π¬ Example
```js
await tinyOlm.setUseLocal(false); // Required
const encrypted = await myModule.encryptMessageV2('user123', 'Hello there!');
const decrypted = await myModule.decryptMessageV2('user123', 1, encrypted.body);
```
---
### π `setUseLocal(value: boolean): void`
Enables or disables **in-memory storage**.
When set to `true`, operations will run using volatile memory with persistent storage.
Ideal for tests π§ͺ or temporary sessions that don't need to be saved.
- **Parameter**:
- `value` (`boolean`): `true` to enable memory mode π§ , `false` to disable it.
---
### β `isUseLocal(): boolean`
Checks whether the system is currently using **in-memory storage**.
- **Returns**: `boolean` β `true` if memory mode is active π’, `false` otherwise π΄.
---
### π¦ `exportDbSession(userId: string): Promise<string>`
Exports a specific **Olm session** π from `indexedDb` for a given user.
- **Parameter**:
- `userId` (`string`): The remote deviceβs user ID π€.
- **Returns**: `Promise<string>` β The serialized (pickled) session π§΄.
- **Throws**: `Error` if the session cannot be found β.
---
### π§βπ€βπ§ `exportDbGroupSession(roomId: string): Promise<string>`
Exports an **outbound group session** π€ from `indexedDb` for a specific room.
- **Parameter**:
- `roomId` (`string`): The roomβs unique ID π .
- **Returns**: `Promise<string>` β The pickled outbound group session.
- **Throws**: `Error` if the session does not exist β.
---
### π `exportDbInboundGroupSession(roomId: string, userId: string): Promise<string>`
Exports an **inbound group session** π₯ from `indexedDb`, filtered by room and sender.
- **Parameters**:
- `roomId` (`string`): The room ID π .
- `userId` (`string`): The senderβs user ID π€.
- **Returns**: `Promise<string>` β The serialized inbound group session.
- **Throws**: `Error` if the session is missing β οΈ.
---
### π€ `exportDbInstance(): Promise<ExportedOlmInstance>`
Exports the entire Olm data instance π from `indexedDb`, including:
- Account
- Sessions
- Outbound group sessions
- Inbound group sessions
- **Returns**: `Promise<ExportedOlmInstance>` β A full export of the Olm structure π§©.
---
### ποΈ `exportDbGroupSessionId(roomId: string): Promise<string>`
Retrieves the **session key** π of the current outbound group session for a room in the `indexedDb`.
- **Parameter**:
- `roomId` (`string`): The roomβs ID π .
- **Returns**: `Promise<string>` β The active session key.
- **Throws**: `Error` if no outbound session exists for the room π.
---
### π `encryptMessageV2(toUsername: string, plaintext: string): Promise<EncryptedMessage>`
Encrypts a **plaintext message** π to a specific user using their stored session.
- **Parameters**:
- `toUsername` (`string`): Recipientβs user ID π€
- `plaintext` (`string`): The message content to encrypt βοΈ
- **Returns**: `Promise<EncryptedMessage>` β Encrypted message object π
- **Throws**: `Error` if session with the user does not exist β
---
### π `decryptMessageV2(fromUsername: string, messageType: number, ciphertext: string): Promise<string>`
Decrypts a **received message** π§Ύ from a specific user.
- **Parameters**:
- `fromUsername` (`string`): Senderβs user ID π€
- `messageType` (`number`): `0` for pre-key π, `1` for message π¬
- `ciphertext` (`string`): Encrypted message π
- **Returns**: `Promise<string>` β Decrypted message π¬
- **Throws**: `Error` if session with the user is missing π«
---
### π¦ `encryptV2(toUsername: string, data: any): Promise<EncryptedMessage>`
Encrypts **any data** π§ to a specific user.
- **Parameters**:
- `toUsername` (`string`): Recipientβs user ID π€
- `data` (`any`): Serializable data to encrypt ποΈ
- **Returns**: `Promise<EncryptedMessage>` β Encrypted package π
- **Throws**: `Error` if no session is found β
---
### π§ͺ `decryptV2(fromUsername: string, messageType: number, plaintext: string, expectedType?: string|null): Promise<any>`
Decrypts serialized content π and optionally checks its type.
- **Parameters**:
- `fromUsername` (`string`): Senderβs user ID π€
- `messageType` (`number`): 0 (pre-key) or 1 (message) π οΈ
- `plaintext` (`string`): Encrypted payload π
- `expectedType` (`string|null`, optional): Validates expected object type π§©
- **Returns**: `Promise<any>` β Decrypted value π¦
- **Throws**: `Error` if type mismatch or session missing β οΈ
---
### π `encryptGroupMessageV2(roomId: string, plaintext: string): Promise<EncryptedData>`
Encrypts a plaintext message π§ for a room using its outbound group session.
- **Parameters**:
- `roomId` (`string`): The target room ID π
- `plaintext` (`string`): Message to encrypt π
- **Returns**: `Promise<EncryptedData>` β Encrypted message π
- **Throws**: `Error` if no group session exists π«
---
### π£οΈ `decryptGroupMessageV2(roomId: string, userId: string, encryptedMessage: EncryptedData): Promise<DecryptedGroupMessage>`
Decrypts a **group message** π¬ using an inbound session.
- **Parameters**:
- `roomId` (`string`): Room ID π
- `userId` (`string`): Senderβs user ID π€
- `encryptedMessage` (`EncryptedData`): Encrypted payload π
- **Returns**: `Promise<DecryptedGroupMessage>` β Decrypted group message π
- **Throws**: `Error` if inbound session is not found β
---
### ποΈ `encryptGroupContentV2(roomId: string, data: any): Promise<EncryptedData>`
Encrypts content π§ for a room using group encryption.
- **Parameters**:
- `roomId` (`string`): Room ID π
- `data` (`any`): Serializable data π
- **Returns**: `Promise<EncryptedData>` β Encrypted data string π
- **Throws**: `Error` if outbound session is missing β
---
### π₯ `decryptGroupContentV2(roomId: string, userId: string, encryptedMessage: EncryptedData, expectedType?: string|null): Promise<DecryptedGroupContent>`
Decrypts serialized group content π§Ύ with optional type checking.
- **Parameters**:
- `roomId` (`string`): Room ID π
- `userId` (`string`): Sender ID π€
- `encryptedMessage` (`EncryptedData`): Encrypted input π
- `expectedType` (`string|null`, optional): Validate expected object type π―
- **Returns**: `Promise<DecryptedGroupContent>` β Decrypted and validated content β
- **Throws**: `Error` if session missing or type mismatch π