# Android keys cloud backup
## Overview
The Autonomy app Android version uses the [Keystore system](https://developer.android.com/training/articles/keystore) to store cryptographic keys (Wallet seeds) and perform signing. One of the downsides of Keystore is [lack of backup ability](https://www.thecodeside.com/2020/09/14/android-auto-backup-keystore-encryption-broken-heart-love-story/). Thus we could not provide a secured backup method in the app.
From the document, the Blockstore provides an end-to-end backup (on capable devices) mechanism in Android. Using it in conjunction with the Keystore, we can achieve the security and redundancy level similar to what iOS offers with the iCloud keychain.
## Security goals
- The Wallet keys are backed up safely in independent storage. App deletion and reinstallation won't remove the keys.
- Only the app (with the correct application signing key associated with the app bundle id) can access the Wallet keys from the storage.
- The Wallet keys in the storage are encrypted by default and only be accessible when the app requests under user biometric verification.
- The Wallet keys are synced to Cloud storage with end-to-end encryption provided by the device passcode. The cloud services (even are provided by the OS) can't access the keys by any means.
- The Wallet keys can be restored to a new device when the user provides the correct cloud storage information and end-to-end passcode to decrypt the data.
## Design
### Parties
1. Android Security Crypto (LibAuk): https://developer.android.com/jetpack/androidx/releases/security
2. Block Store: https://developers.google.com/identity/blockstore/android
### Flows
```mermaid
sequenceDiagram
title: Anroid keys backup
participant App
participant Android Security Crypto
participant Block Store
App-->>+Android Security Crypto: create/import delete key
Android Security Crypto->>Android Security Crypto: encrypt and update to files using local keystore.
Android Security Crypto->>Android Security Crypto: get and encode seeds & seeds's information to json array
Android Security Crypto->>Block Store: send data to backup
Block Store->>Block Store: encrypt and back up to cloud.
```
```mermaid
sequenceDiagram
title: Anroid keys restore
participant App
participant Android Security Crypto
participant Block Store
App-->>Block Store: restore from cloud
Block Store->>Block Store: get backup data from cloud
Block Store->>Android Security Crypto: return the data.
Android Security Crypto->>Android Security Crypto: decode json array and import every seed & seed's information to keystore
Android Security Crypto->>Android Security Crypto: encrypt and save into keystore.
```
- Autonomy stores keys using the Android KeyStore system with the AES-256 cipher
- The json encode/decode seeds and information process are executing on Android IO thread.
- Using `blockstoreClient.storeBytes` and `blockstoreClient.retrieveBytes` to backup and restore data from BlockStore.
## Limitation
1. Require Google Play service.
2. The Block Store API might not offer end-to-end encryption. The app needs to check whether the E2EE is available before doing the backup. The user needs to acknowledge they are trying to backup without E2EE. The common scenarios are:
- Using Android 8 or below.
- Screen lock (PIN, pattern, or password) is not enabled.
###### tags: `Autonomy`