### 5 unstable releases

0.3.1 | Dec 10, 2023 |
---|---|

0.3.0 | Dec 7, 2022 |

0.2.0 | Nov 26, 2022 |

0.1.1 | Nov 25, 2022 |

0.1.0 | Oct 12, 2022 |

#**104** in Command-line interface

**24** downloads per month

**Apache-2.0**

71KB

1K
SLoC

# libpep: Library for polymorphic pseudonimisation and encryption

Author: Bernard van Gastel

License: Apache License 2.0

Same library in different languages:

- libpep-cpp (C++);
- libpep on crates.io (Rust).

This library implements the PEP encryption based on ElGamal, and operations on these encrypted messages. A message

can be encrypted for a receiver which has public key `M`

associated with it, belonging to secret key `Y`

. This encryption is random: every time a different random `y`

is used, resulting in different ciphertexts (encrypted messages). We represent this encryption function as `r`

.`EG``(`r`,` M`,` Y`)`

The library supports three operations on ciphertext

(= `in`

, encrypting message `EG``(`r`,` M`,` Y`)`

for public key `M`

with random `Y`

):`r`

: scrambles a ciphertext. Both`out``=``rerandomize``(``in``,`s`)`

and`in`

can be decrypted by the same secret key`out`

, both resulting in the same decrypted message`y`

. However, the binary form of`M`

and`in`

differs. Spec:`out`

is transformed to`in``=``EG``(`r`,`M`,`Y`)`

;`out``=``EG``(`r`+`s`,`M`,`Y`)`

: modifies a ciphertext`out``=``reshuffle``(``in``,`n`)`

(an encrypted form of`in`

), so that after decryption of`M`

the decrypted message will be equal to`out`

. Spec:`n``*`M

is transformed to`in``=``EG``(`r`,`M`,`Y`)`

.`out``=``EG``(`r`,`n`*`M`,`Y`)`

: if`out``=``rekey``(``in``,`k`)`

can be decrypted by secret key`in`

, then`y`

can be decrypted by secret key`out`

. Decryption will both result in message`k``*`y

. Spec:`M`

is transformed to`in``=``EG``(`r`,`M`,`Y`)`

.`out``=``EG``(`r`,`M`,`k`*`Y`)`

The

and `rekey``(``in``,` k`)`

can be combined in a `reshuffle``(``in``,` n`)`

.`rks``(``in``,` k`,` n`)`

There are also zero knowledge proof version of these operations. These are needed so that a party can prove to another party that it has applied the operation on the input data, without revealing the factors used in the operation.

When distributing trust over multiple central servers, these zero knowledge proofs are essential, so that a malfunctioning server can not violate security guarantees of the system.

## Applications

For pseudonimisation, the core operation is *reshuffle* with

. It modifies a main pseudonym with a factor `n`

that is specific to a user (or user group) receiving the pseudonym. After applying a user specific factor `n`

, a pseudonym is called a `n`*local pseudonym*. The factor

is typically tied to the `n`*access group of a user*.

Using only a reshuffle is insufficient, as the pseudonym is still encrypted with the public key

(which can be decrypted by the secret key `Y`

). To allow a user to decrypt the encrypted pseudonym, a `y`*rekey* with

is needed, in combination with a protocol to hand the user the secret key `k`

. The factor `k *y`

`k`

is typically tied to the *current session of a user*.

To make pseudonyms harder to trace, rerandomize is applied frequently. This way a binary compare of the encrypted pseudonym will not leak any information.

## Implementation

This library is using the Ristretto encoding on Curve25519, implemented in the curve25519-dalek crate. There are a number of arithmetic rules for scalars and group elements: group elements can be added and subtracted from each other. Scalars support addition, subtraction, and multiplication. Division can be done by multipling with the inverse (using

for non-zero scalar `s .invert()`

`s`

). A scalar can be converted to a group element (by multiplying with the special generator `G`

), but not the other way around. Group elements can also be multiplied by a scalar.Group elements have an *almost* 32 byte range (top bit is always zero, and some other values are invalid). Therefore, not all AES-256 keys (using the full 32 bytes range) are valid group elements. But all group elements are valid AES-256 keys. Group elements can be generated by

or `GroupElement ::`random

`(`

`..`

`)`

`GroupElement``::`from_hash`(``..``)`

. Scalars are also 32 bytes, and can be generated with `Scalar``::`random`(``..``)`

or `Scalar``::`from_hash`(``..``)`

.The zero knowledge proofs are offline Schnorr proofs, based on a Fiat-Shamir transform. The hashing algorithm used is SHA512.

## Building and running

Build using cargo:

`cargo`` test`

Run using cargo:

`cargo`` run`` --`bin peppy` --`features`=`build-binary

## Install

Install using

`cargo`` install libpep`` --`bin peppy` --`features`=`build-binary

## Background

Based on the article by Eric Verheul and Bart Jacobs, *Polymorphic Encryption and Pseudonymisation in Identity Management and Medical Research*. In **Nieuw Archief voor Wiskunde (NAW)**, 5/18, nr. 3, 2017, p. 168-172. This article does not contain the zero knowledge proofs.

#### Dependencies

~2–12MB

~158K SLoC