libhcs
Data Structures | Functions
djcs.h File Reference

The Damgard-Jurik scheme is a scheme which provides homomorphic addition, and limited multiplication on encrypted data. More...

#include <gmp.h>
#include "hcs_random.h"
Include dependency graph for djcs.h:

Go to the source code of this file.

Data Structures

struct  djcs_public_key
 Public key for use in the Paillier system. More...
 
struct  djcs_private_key
 Private key for use in the Paillier system. More...
 

Functions

djcs_public_keydjcs_init_public_key (void)
 Initialise a djcs_public_key and return a pointer to the newly created structure. More...
 
djcs_private_keydjcs_init_private_key (void)
 Initialise a djcs_private_key and return a pointer to the newly created structure. More...
 
int djcs_generate_key_pair (djcs_public_key *pk, djcs_private_key *vk, hcs_random *hr, unsigned long s, unsigned long bits)
 Initialise a key pair with modulus size bits. More...
 
void djcs_encrypt (djcs_public_key *pk, hcs_random *hr, mpz_t rop, mpz_t plain1)
 Encrypt a value plain1, and set rop to the encrypted result. More...
 
void djcs_reencrypt (djcs_public_key *pk, hcs_random *hr, mpz_t rop, mpz_t op)
 Reencrypt an encrypted value op. More...
 
void djcs_ep_add (djcs_public_key *pk, mpz_t rop, mpz_t cipher1, mpz_t plain1)
 Add a plaintext value plain1 to an encrypted value cipher1, storing the result in rop. More...
 
void djcs_ee_add (djcs_public_key *pk, mpz_t rop, mpz_t cipher1, mpz_t cipher2)
 Add an encrypted value cipher2 to an encrypted value cipher1, storing the result in rop. More...
 
void djcs_ep_mul (djcs_public_key *pk, mpz_t rop, mpz_t cipher1, mpz_t plain1)
 Multiply a plaintext value plain1 with an encrypted value cipher1, storing the result in rop. More...
 
void djcs_decrypt (djcs_private_key *vk, mpz_t rop, mpz_t cipher1)
 Decrypt a value cipher1, and set rop to the decrypted result. More...
 
void djcs_clear_public_key (djcs_public_key *pk)
 Clears all data in a djcs_public_key. More...
 
void djcs_clear_private_key (djcs_private_key *vk)
 Clears all data in a djcs_private_key. More...
 
void djcs_free_public_key (djcs_public_key *pk)
 Frees a djcs_public_key and all associated memory. More...
 
void djcs_free_private_key (djcs_private_key *vk)
 Frees a djcs_private_key and all associated memory. More...
 
int djcs_verify_key_pair (djcs_public_key *pk, djcs_private_key *vk)
 Check certain values shared between public and private keys to ensure they indeed are pairs. More...
 
char * djcs_export_public_key (djcs_public_key *pk)
 Export a public key as a string. More...
 
char * djcs_export_private_key (djcs_private_key *vk)
 Export a private key as a string. More...
 
int djcs_import_public_key (djcs_public_key *pk, const char *json)
 Import a public key from a string. More...
 
int djcs_import_private_key (djcs_private_key *vk, const char *json)
 Import a private key from a string. More...
 

Detailed Description

The Damgard-Jurik scheme is a scheme which provides homomorphic addition, and limited multiplication on encrypted data.

It is a generalisation of the Paillier cryptoscheme, where the modulus n^2 is extended to n^(s+1) for s > 0.

Function Documentation

djcs_public_key* djcs_init_public_key ( void  )

Initialise a djcs_public_key and return a pointer to the newly created structure.

Returns
A pointer to an initialised djcs_public_key, NULL on allocation failure
djcs_private_key* djcs_init_private_key ( void  )

Initialise a djcs_private_key and return a pointer to the newly created structure.

Returns
A pointer to an initialised djcs_private_key, NULL on allocation failure
int djcs_generate_key_pair ( djcs_public_key pk,
djcs_private_key vk,
hcs_random hr,
unsigned long  s,
unsigned long  bits 
)

Initialise a key pair with modulus size bits.

It is required that pk and vk are initialised before calling this function. pk and vk are expected to not be NULL.

In practice the bits value should usually be greater than 2048 to ensure sufficient security.

Whilst s is variable and and can be any value upto a minimum of 32 bits, beware when calling the function with large values. The modulus grows exponentially given s, so even small values greater than 7 will incur massive performance penalties, and increase the risk of overflow (~40 million digits for the modulus for this to occur).

Warning
This function attempts to allocate memory, so calling this twice in sucession with the same keys will cause your program to lose a pointer to this allocated memory, resulting in a memory leak. If you wish to call this function in this manner, ensure djcs_clear_public_key and/or djcs_clear_private_key are called prior.
Parameters
pkA pointer to an initialised djcs_public_key
vkA pointer to an initialised djcs_private_key
hrA pointer to an initialised hcs_random type
sThe size exponent for the ciphertext space we wish to work in
bitsThe number of bits for the modulus of the key
void djcs_encrypt ( djcs_public_key pk,
hcs_random hr,
mpz_t  rop,
mpz_t  plain1 
)

Encrypt a value plain1, and set rop to the encrypted result.

Parameters
pkA pointer to an initialised djcs_public_key
hrA pointer to an initialised hcs_random type
ropmpz_t where the encrypted result is stored
plain1mpz_t to be encrypted
void djcs_reencrypt ( djcs_public_key pk,
hcs_random hr,
mpz_t  rop,
mpz_t  op 
)

Reencrypt an encrypted value op.

Upon decryption, this newly encrypted value, rop, will retain the same value as op.

Parameters
pkA pointer to an initialised djcs_public_key
hrA pointer to an initialised hcs_random type
ropmpz_t where the newly encrypted value is stored
opmpz_t to be reencrypted
void djcs_ep_add ( djcs_public_key pk,
mpz_t  rop,
mpz_t  cipher1,
mpz_t  plain1 
)

Add a plaintext value plain1 to an encrypted value cipher1, storing the result in rop.

Parameters
pkA pointer to an initialised djcs_public_key
ropmpz_t where the newly encrypted value is stored
cipher1mpz_t to be added together
plain1mpz_t to be added together
void djcs_ee_add ( djcs_public_key pk,
mpz_t  rop,
mpz_t  cipher1,
mpz_t  cipher2 
)

Add an encrypted value cipher2 to an encrypted value cipher1, storing the result in rop.

Parameters
pkA pointer to an initialised djcs_public_key.
ropmpz_t where the newly encrypted value is stored
cipher1mpz_t to be added together
cipher2mpz_t to be added together
void djcs_ep_mul ( djcs_public_key pk,
mpz_t  rop,
mpz_t  cipher1,
mpz_t  plain1 
)

Multiply a plaintext value plain1 with an encrypted value cipher1, storing the result in rop.

Parameters
pkA pointer to an initialised djcs_public_key
ropWhere the new encrypted result is stored
cipher1The encrypted value which is to be multipled to
plain1The plaintext value which is to be multipled
void djcs_decrypt ( djcs_private_key vk,
mpz_t  rop,
mpz_t  cipher1 
)

Decrypt a value cipher1, and set rop to the decrypted result.

rop and cipher1 can aliases for the same mpz_t.

Parameters
vkA pointer to an initialised djcs_private_key
ropmpz_t where the decrypted result is stored
cipher1mpz_t to be decrypted
void djcs_clear_public_key ( djcs_public_key pk)

Clears all data in a djcs_public_key.

This does not free memory in the keys, only putting it into a state whereby they can be safely used to generate new key values.

Parameters
pkA pointer to an initialised djcs_public_key
void djcs_clear_private_key ( djcs_private_key vk)

Clears all data in a djcs_private_key.

This does not free memory in the keys, only putting it into a state whereby they can be safely used to generate new key values.

Parameters
vkA pointer to an initialised djcs_private_key
void djcs_free_public_key ( djcs_public_key pk)

Frees a djcs_public_key and all associated memory.

The key memory is not zeroed, so one must call djcs_clear_public_key if it is required. one does not need to call djcs_clear_public_key before using this function.

Parameters
pkA pointer to an initialised djcs_public_key
void djcs_free_private_key ( djcs_private_key vk)

Frees a djcs_private_key and all associated memory.

The key memory is not zeroed, so one must call djcs_clear_private_key if it is required. one does not need to call djcs_clear_private_key before using this function.

Parameters
vkv pointer to an initialised djcs_private_key
int djcs_verify_key_pair ( djcs_public_key pk,
djcs_private_key vk 
)

Check certain values shared between public and private keys to ensure they indeed are pairs.

This checks only the n and s values, and assumes that the caller has not altered other internal values. If the caller has only interacted with the keys through the usual functions, then this should guarantee the keys are pairs.

Parameters
pkA pointer to an initialised djcs_public_key
vkA pointer to an initialised djcs_private_key
Returns
non-zero if keys are valid, else zero
char* djcs_export_public_key ( djcs_public_key pk)

Export a public key as a string.

We only store the minimum required values to restore the key. In this case, these are the s and n values.

The format these strings export as is as a JSON object.

Parameters
pkA pointer to an initialised djcs_public_key
Returns
A string representing the given key, else NULL on error
char* djcs_export_private_key ( djcs_private_key vk)

Export a private key as a string.

We only store the minimum required values to restore the key. In this case, these are the s, n and d values. The remaining values are then computed from these on import.

Parameters
vkA pointer to an initialised djcs_private_key
Returns
A string representing the given key, else NULL on error
int djcs_import_public_key ( djcs_public_key pk,
const char *  json 
)

Import a public key from a string.

The input string is expected to match the format given by the export functions.

Parameters
pkA pointer to an initialised djcs_public_key
jsonA string storing the contents of a public key
Returns
non-zero if success, else zero on format error
int djcs_import_private_key ( djcs_private_key vk,
const char *  json 
)

Import a private key from a string.

The input string is expected to match the format given by the export functions.

Parameters
vkA pointer to an initialised djcs_private_key
jsonA string storing the contents of a private key
Returns
non-zero if success, else zero on format error