Author: Rich FitzJohn
Date: 2025-02-07

# Data Encryption

#### Rich FitzJohn

#### 2025-02-07

The scenario:

A group of people are working on a sensitive data set that for practical reasons needs to be stored in a place that we’re not 100% happy with the security (e.g., Dropbox), or we’re concerned that files stored in plain text on users computers (e.g. laptops) may lead to the data being compromised.

If the data can be stored encrypted but everyone in the group can still read and write the data then we’ve improved the situation somewhat. But organising for everyone to get a copy of the key to decrypt the data files is non-trivial. The workflow described here aims to simplify this procedure using lower-level functions in the `cyphr` package.

The general procedure is this:

1. A person will set up a set of personal keys and a key for the data. The data key will be encrypted with their personal key so they have access to the data but nobody else does. At this point the data can be encrypted.

2. Additional users set up personal keys and request access to the data. Anyone with access to the data can grant access to anyone else.

Before doing any of this, everyone needs to have ssh keys set up. By default the package will use your ssh keys found at “~/.ssh”; see the main package vignette for how to use this.

For clarity here we will generate two sets of key pairs for two actors Alice and Bob:

    path_key_alice <- cyphr::ssh_keygen(password = FALSE)
    path_key_bob <- cyphr::ssh_keygen(password = FALSE)

These would ordinarily be on different machines (nobody has access to anyone else’s private key) and they would be password protected. In the function calls below, all the `path_user` arguments would be omitted.

We’ll store data in the directory `data`; at present there is nothing there (this is in a temporary directory for compliance with CRAN policies but would ordinarily be somewhere persistent and under version control ideally).

    data_dir <- file.path(tempdir(), "data")
    dir.create(data_dir)
    dir(data_dir)

    ## character(0)

First, create a personal set of keys. These will be shared across all projects and stored away from the data. Ideally one would do this with `ssh-keygen` at the command line, following one of the many guides available. A utility function `ssh_keygen` (which simply calls `ssh-keygen` for you) is available in this package though. You will need to generate a key on each computer you want access from. Don’t copy the key around. If you lose your user key you will lose access to the data!

Second, create a key for the data and encrypt that key with your personal key. Note that the data key is never stored directly - it is always stored encrypted by a personal key.

    cyphr::data_admin_init(data_dir, path_user = path_key_alice)

    ## Generating data key

    ## Authorising ourselves

    ## Adding key 81:36:40:de:90:10:25:b7:5c:c6:ad:27:ee:0a:43:89:e8:e5:e1:3f:76:6d:bb:f3:40:a0:5c:86:d6:66:09:aa
    ##   user: rfitzjoh
    ##   host: wpia-dide300
    ##   date: 2025-02-07 10:14:05.599858

    ## Verifying

The data key is very important. If it is deleted, then the data cannot be decrypted. So do not delete the directory `data_dir/.cyphr`! Ideally add it to your version control system so that it cannot be lost. Of course, if you’re working in a group, there are multiple copies of the data key (each encrypted with a different person’s personal key) which reduces the chance of total loss.

This command can be run multiple times safely; if it detects it has been rerun and the data key will not be regenerated.

    cyphr::data_admin_init(data_dir, path_user = path_key_alice)

    ## Already set up at /tmp/Rtmpj3XoZv/data

    ## Verifying

Third, you can add encrypted data to the directory (or to anywhere really). When run, `cyphr::config_data` will verify that it can actually decrypt things.

    key <- cyphr::data_key(data_dir, path_user = path_key_alice)

This object can be used with all the `cyphr` functions (see the “cyphr” vignette; `vignette("cyphr")`)

    filename <- file.path(data_dir, "iris.rds")
    cyphr::encrypt(saveRDS(iris, filename), key)
    dir(data_dir)

    ## [1] "iris.rds"

The file is encrypted and so cannot be read with `readRDS`:

    readRDS(filename)

    ## Error in readRDS(filename): unknown input format

But we can decrypt and read it:

    head(cyphr::decrypt(readRDS(filename), key))

    ##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    ## 1          5.1         3.5          1.4         0.2  setosa
    ## 2          4.9         3.0          1.4         0.2  setosa
    ## 3          4.7         3.2          1.3         0.2  setosa
    ## 4          4.6         3.1          1.5         0.2  setosa
    ## 5          5.0         3.6          1.4         0.2  setosa
    ## 6          5.4         3.9          1.7         0.4  setosa

Fourth, have someone else join in. Recall that to simulate another person here, I’m going to pass an argument `path_user = path_key_bob` though to the functions. This contains the path to “Bob”’s ssh keypair. If run on an actually different computer this would not be needed; this is just to simulate two users in a single session for this vignette (see minimal example below where this is simulated). Again, typically this user would also not use the `cyphr::ssh_keygen` function but use the `ssh-keygen` command from their shell.

We’re going to assume that the user can read and write to the data. This is the case for my use case where the data are stored on dropbox and will be the case with GitHub based distribution, though there would be a pull request step in here.

This user cannot read the data, though trying to will print a message explaining how you might request access:

    key_bob <- cyphr::data_key(data_dir, path_user = path_key_bob)

But `bob` is your collaborator and needs access! What they need to do is run:

    cyphr::data_request_access(data_dir, path_user = path_key_bob)

    ## A request has been added

    ## Email someone with access to add you
    ##
    ##     hash: 5d:ad:fd:f0:ec:a8:b4:01:02:87:c0:ef:4e:b7:ce:b9:6e:8b:3d:10:3d:9e:91:67:78:20:a0:6e:50:c9:a3:fc
    ##
    ## If you are using git, you will need to commit and push first:
    ##
    ##     git add .cyphr
    ##     git commit -m "Please add me to the dataset"
    ##     git push

(again, ordinarily you would not need the `bob` bit here)

The user should the send an email to someone with access and quote the hash in the message above.

Fifth, back on the first computer we can authorise the second user. First, see who has requested access:

    req <- cyphr::data_admin_list_requests(data_dir)
    req

    ## 1 key:
    ##   5d:ad:fd:f0:ec:a8:b4:01:02:87:c0:ef:4e:b7:ce:b9:6e:8b:3d:10:3d:9e:91:67:78:20:a0:6e:50:c9:a3:fc
    ##     user: rfitzjoh
    ##     host: wpia-dide300
    ##     date: 2025-02-07 10:14:05.693398

We can see the same hash here as above (`5dadfdf0eca8b4010287c0ef4eb7ceb96e8b3d103d9e91677820a06e50c9a3fc`)

…and then grant access to them with the `cyphr::data_admin_authorise` function.

    cyphr::data_admin_authorise(data_dir, yes = TRUE, path_user = path_key_alice)

    ## There is 1 request for access

    ## Adding key 5d:ad:fd:f0:ec:a8:b4:01:02:87:c0:ef:4e:b7:ce:b9:6e:8b:3d:10:3d:9e:91:67:78:20:a0:6e:50:c9:a3:fc
    ##   user: rfitzjoh
    ##   host: wpia-dide300
    ##   date: 2025-02-07 10:14:05.693398

    ## Added 1 key

    ## If you are using git, you will need to commit and push:
    ##
    ##     git add .cyphr
    ##     git commit -m "Authorised rfitzjoh"
    ##     git push

If you do not specify `yes = TRUE` will prompt for confirmation at each key added.

This has cleared the request queue:

    cyphr::data_admin_list_requests(data_dir)

    ## (empty)

and added it to our set of keys:

    cyphr::data_admin_list_keys(data_dir)

    ## 2 keys:
    ##   5d:ad:fd:f0:ec:a8:b4:01:02:87:c0:ef:4e:b7:ce:b9:6e:8b:3d:10:3d:9e:91:67:78:20:a0:6e:50:c9:a3:fc
    ##     user: rfitzjoh
    ##     host: wpia-dide300
    ##     date: 2025-02-07 10:14:05.693398
    ##   81:36:40:de:90:10:25:b7:5c:c6:ad:27:ee:0a:43:89:e8:e5:e1:3f:76:6d:bb:f3:40:a0:5c:86:d6:66:09:aa
    ##     user: rfitzjoh
    ##     host: wpia-dide300
    ##     date: 2025-02-07 10:14:05.599858

Finally, as soon as the authorisation has happened, the user can encrypt and decrypt files:

    key_bob <- cyphr::data_key(data_dir, path_user = path_key_bob)
    head(cyphr::decrypt(readRDS(filename), key_bob))

    ##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    ## 1          5.1         3.5          1.4         0.2  setosa
    ## 2          4.9         3.0          1.4         0.2  setosa
    ## 3          4.7         3.2          1.3         0.2  setosa
    ## 4          4.6         3.1          1.5         0.2  setosa
    ## 5          5.0         3.6          1.4         0.2  setosa
    ## 6          5.4         3.9          1.7         0.4  setosa

## Minimal example

As above, but with less discussion:

Setup, on Alice’s computer:

    cyphr::data_admin_init(data_dir, path_user = path_key_alice)

    ## Generating data key

    ## Authorising ourselves

    ## Adding key 81:36:40:de:90:10:25:b7:5c:c6:ad:27:ee:0a:43:89:e8:e5:e1:3f:76:6d:bb:f3:40:a0:5c:86:d6:66:09:aa
    ##   user: rfitzjoh
    ##   host: wpia-dide300
    ##   date: 2025-02-07 10:14:05.781558

    ## Verifying

Get the data key key:

    key <- cyphr::data_key(data_dir, path_user = path_key_alice)

Encrypt a file:

    cyphr::encrypt(saveRDS(iris, filename), key)

Request access, on Bob’s computer:

    hash <- cyphr::data_request_access(data_dir, path_user = path_key_bob)

    ## A request has been added

    ## Email someone with access to add you
    ##
    ##     hash: 5d:ad:fd:f0:ec:a8:b4:01:02:87:c0:ef:4e:b7:ce:b9:6e:8b:3d:10:3d:9e:91:67:78:20:a0:6e:50:c9:a3:fc
    ##
    ## If you are using git, you will need to commit and push first:
    ##
    ##     git add .cyphr
    ##     git commit -m "Please add me to the dataset"
    ##     git push

Alice authorises this request::

    cyphr::data_admin_authorise(data_dir, yes = TRUE, path_user = path_key_alice)

    ## There is 1 request for access

    ## Adding key 5d:ad:fd:f0:ec:a8:b4:01:02:87:c0:ef:4e:b7:ce:b9:6e:8b:3d:10:3d:9e:91:67:78:20:a0:6e:50:c9:a3:fc
    ##   user: rfitzjoh
    ##   host: wpia-dide300
    ##   date: 2025-02-07 10:14:05.822533

    ## Added 1 key

    ## If you are using git, you will need to commit and push:
    ##
    ##     git add .cyphr
    ##     git commit -m "Authorised rfitzjoh"
    ##     git push

Bob can get the data key:

    key <- cyphr::data_key(data_dir, path_user = path_key_bob)

Bob can read the secret data:

    head(cyphr::decrypt(readRDS(filename), key))

    ##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    ## 1          5.1         3.5          1.4         0.2  setosa
    ## 2          4.9         3.0          1.4         0.2  setosa
    ## 3          4.7         3.2          1.3         0.2  setosa
    ## 4          4.6         3.1          1.5         0.2  setosa
    ## 5          5.0         3.6          1.4         0.2  setosa
    ## 6          5.4         3.9          1.7         0.4  setosa

## Details & disclosure

Encryption does not work through security through obscurity; it works because we can rely on the underlying maths enough to be open about how things are stored and where.

Most encryption libraries require some degree of security in the underlying software. Because of the way R works this is very difficult to guarantee; it is trivial to rewrite code in running packages to skip past verification checks. So this package is not designed to (or able to) avoid exploits in your running code; an attacker could intercept your private keys, the private key to the data, or skip the verification checks that are used to make sure that the keys you load are what they say they are. However, the data are safe; only people who have keys to the data will be able to read it.

`cyphr` uses two different encryption algorithms; it uses RSA encryption via the `openssl` package for user keys, because there is a common file format for these keys so it makes user configuration easier. It uses the modern sodium package (and through that the libsodium library) for data encryption because it is very fast and simple to work with. This does leave two possible points of weakness as a vulnerability in either of these libraries could lead to an exploit that could allow decryption of your data.

Each user has a public/private key pair. Typically this is in `~/.ssh/id_rsa.pub` and `~/.ssh/id_rsa`, and if found these will be used. Alternatively the location of the keypair can be stored elsewhere and pointed at with the `USER_KEY` or `USER_PUBKEY` environment variables. The key may be password protected (and this is recommended!) and the password will be requested without ever echoing it to the terminal.

The data directory has a hidden directory `.cyphr` in it.

    dir(data_dir, all.files = TRUE, no.. = TRUE)

    ## [1] ".cyphr"   "iris.rds"

This does not actually need to be stored with the data but it makes sense to (there are workflows where data is stored remotely where storing this directory might make sense). The “keys” directory contains a number of files; one for each person who has access to the data.

    dir(file.path(data_dir, ".cyphr", "keys"))

    ## [1] "5dadfdf0eca8b4010287c0ef4eb7ceb96e8b3d103d9e91677820a06e50c9a3fc"
    ## [2] "813640de901025b75cc6ad27ee0a4389e8e5e13f766dbbf340a05c86d66609aa"

    names(cyphr::data_admin_list_keys(data_dir))

    ## [1] "5dadfdf0eca8b4010287c0ef4eb7ceb96e8b3d103d9e91677820a06e50c9a3fc"
    ## [2] "813640de901025b75cc6ad27ee0a4389e8e5e13f766dbbf340a05c86d66609aa"

(the file `test` is a small file encrypted with the data key used to verify everything is working OK).

Each file is stored in RDS format and is a list with elements:

- user: the reported user name of the person who created request for data
- host: the reported computer name
- date: the time the request was generated
- pub: the RSA public key of the user
- key: the data key, encrypted with the user key. Without the private key, this cannot be used. With the user’s private key this can be used to generate the symmetric key to the data.

    h <- names(cyphr::data_admin_list_keys(data_dir))[[1]]
    readRDS(file.path(data_dir, ".cyphr", "keys", h))

    ## $user
    ## [1] "rfitzjoh"
    ##
    ## $host
    ## [1] "wpia-dide300"
    ##
    ## $date
    ## [1] "2025-02-07 10:14:05 GMT"
    ##
    ## $pub
    ## [2048-bit rsa public key]
    ## md5: 541ae656fcb2be3be1d5c4c6a07590c7
    ## sha256: 5dadfdf0eca8b4010287c0ef4eb7ceb96e8b3d103d9e91677820a06e50c9a3fc
    ##
    ## $key
    ##   [1] 31 ec 45 d5 25 e7 9b de 66 5b 8f 52 35 e4 72 1c 06 3a 60 10 64 90 8b 55 7a
    ##  [26] 4f 5e 2f be 8d 03 c2 e4 ed e0 1b cc 72 91 30 37 5f 01 ce 24 a7 7e c8 e4 90
    ##  [51] 04 75 b3 23 12 3f ce bd 6b 90 26 ff 33 61 ff a1 a6 74 af ab 4e ee ff 11 81
    ##  [76] 0d d6 f3 a3 29 09 bf 2d 0e f4 c8 50 78 89 7a 4d 2e 68 ee 59 8b f2 29 48 38
    ## [101] 83 09 16 61 34 61 f0 7f d2 f6 8f f7 f4 a3 d5 82 93 0c 51 55 e0 6a 9c 44 4f
    ## [126] 99 d2 18 24 8b 7f d8 6c 40 31 4f 0f 01 2c 25 f7 c3 50 33 f6 08 72 9e 52 60
    ## [151] 40 9a 1e 1f 0f 18 e8 67 23 b5 9c fc 98 cc b0 70 f5 b8 29 ea 09 25 bb 18 42
    ## [176] 26 9f d8 19 b5 27 38 ef 63 67 98 30 55 63 09 c5 18 3e 1c 4d 7a d7 3d 45 db
    ## [201] 15 35 41 c6 e1 2e 13 8d 33 6b dd 04 76 c7 93 c1 33 25 8c 8c 8d 2e 2a 1a 40
    ## [226] d8 c8 26 69 68 81 38 e3 02 d8 6c 21 46 11 c5 20 0f 99 2c 00 74 66 55 ef 4a
    ## [251] 53 8d 1a fd c5 28

You can see that the hash of the public key is the same as name of the stored file here (which is used to prevent collisions when multiple people request access at the same time).

    h

    ## [1] "5dadfdf0eca8b4010287c0ef4eb7ceb96e8b3d103d9e91677820a06e50c9a3fc"

When a request is posted it is an RDS file with all of the above except for the `key` element, which is added during authorisation.

(Note that the verification relies on the package code not being attacked, and given R’s highly dynamic nature an attacker could easily swap out the definition for the verification function with something that always returns `TRUE`.)

When an authorised user creates the `data_key` object (which allows decryption of the data) `secret` will:

- read their private user key (probably from `~/.ssh/id_rsa`)
- read the encrypted data key from the data directory (the `$key` element from the list above).
- decrypt this data key using their user key to yield the the data symmetric key.

## Limitations

In the Dropbox scenario, non-password protected keys will afford only limited protection. This is because even though the keys and data are stored separately on Dropbox, they will be in the same place on a local computer; if that computer is lost then the only thing preventing an attacker recovering the data is security through obscurity (the data would appear to be random junk but they will be able to run your analysis scripts as easily as you can). Password protected keys will improve this situation considerably as without a password the data cannot be recovered.

The data is not encrypted during a running R session. R allows arbitrary modification of code at runtime so this package provides no security from the point where the data can be decrypted. If your computer was compromised then stealing the data while you are running R should be assumed to be straightforward.