PS-lib Reference
On this site we document the available functions in libps.so.
Random distributions
double pslib_distr_laplace(double mean, double scale);
Generate a random value according to the Laplace distribution with the given mean and the given scale.
double pslib_distr_exponential(double scale);
Generate a random value according to the exponential distribution with the given scale.
Cryptographic primitives
void pslib_sha256(const char *data, uintptr_t length, char *output);
Compute the SHA-256 hash of the given input data.
data is expected to be a pointer to length bytes of data.
output is expected to be a pointer to 32 bytes of output buffer to which the hash digest will be written.
void pslib_aes_ctr(const char *key, uintptr_t length, char *data);
Encrypt data with AES-CTR.
key is expected to be a pointer to the key bytes, which must have 16 bytes.
data is the pointer to the data of length length.
Note that this function overwrites the data.
Diffie-Hellman
struct DhKeypair *pslib_dh_keypair_generate(void);
Generate a new keypair for a Diffie-Hellman key exchange.
The returned keypair can be free'd by using pslib_dh_keypair_free.
void pslib_dh_keypair_free(struct DhKeypair *keypair);
Releases the memory held by this keypair.
const char *pslib_dh_private_key(const struct DhKeypair *keypair);
Returns the private key of this keypair. The returned pointer points to a null-terminated string. The returned memory is owned by the keypair and must not be free'd.
const char *pslib_dh_public_key(const struct DhKeypair *keypair);
Returns the public key of this keypair. The returned pointer points to a null-terminated string. The returned memory is owned by the keypair and must not be free'd.
int pslib_dh_shared_secret(const char *private_key, const char *public_key, char *output);
Compute the shared secret for the given private key and public key.
The resulting secret will be written to the memory pointed to by output, which is expected to have 32 bytes.
If the secret was computed successfully, 0 is returned.
On error (e.g. because the input key was malformed), -1 is returned.
Homomorphic encryption
struct Keypair *pslib_he_keypair_generate(void);
Generate a new keypair, consisting of a private and a public key.
The returned keypair can be free'd by using pslib_he_keypair_free.
void pslib_he_keypair_free(struct Keypair *keypair);
Releases the memory held by this keypair.
const char *pslib_he_private_key(const struct Keypair *keypair);
Returns the private key of the given keypair. The returned pointer points to a null-terminated string. The returned memory is owned by the keypair and must not be free'd.
const char *pslib_he_public_key(const struct Keypair *keypair);
Returns the public key of the given keypair. The returned pointer points to a null-terminated string. The returned memory is owned by the keypair and must not be free'd.
char *pslib_he_encrypt(const char *public_key, uint8_t data);
Encrypts the given byte using the given public key.
Returns a null-terminated string representing the ciphertext.
The returned ciphertext can be free'd by using pslib_string_free.
int16_t pslib_he_decrypt(const char *private_key, const char *ciphertext);
Decrypts the given ciphertext using the given private key. Returns the byte (an integer in the range 0 to 255), or -1 on error.
const char *pslib_he_add(const char *cipher_1, const char *cipher_2);
Homomorphically adds two ciphertexts together.
Returns a null-terminated string that contains the ciphertext representing the sum.
Note that this function cannot determine overflows
:
If the sum is larger than a byte, the function will successfully return a ciphertext;
however, the resulting ciphertext will cause an error on decryption.
The returned ciphertext can be free'd by using pslib_string_free.
Miscellaneous
void pslib_string_free(char *string);
Releases the memory held by a null-terminated string returned from PS-lib.