ABY Framework  1.0
Arithmetic Bool Yao Framework
 All Classes Files Functions Variables Enumerations Enumerator Macros
djn.h
Go to the documentation of this file.
1 
12 /*
13  libdjn - v0.9
14  A library implementing the Damgaard Jurik Nielsen cryptosystem with s=1 (~Paillier).
15  based on:
16  libpaillier - A library implementing the Paillier cryptosystem.
17  (http://hms.isi.jhu.edu/acsc/libpaillier/)
18 
19  2015 EC SPRIDE
20  daniel.demmler@ec-spride.de
21 
22  This program is free software; you can redistribute it and/or modify
23  it under the terms of the GNU General Public License as published by
24  the Free Software Foundation; either version 2 of the License, or
25  (at your option) any later version.
26 
27  This program is distributed in the hope that it will be useful, but
28  WITHOUT ANY WARRANTY; without even the implied warranty of
29  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30  General Public License for more details.
31  */
32 
33 #ifndef _DJN_H_
34 #define _DJN_H_
35 #include <gmp.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include "powmod.h"
40 
41 /*
42  On memory handling:
43 
44  At no point is any special effort made to securely "shred" sensitive
45  memory or prevent it from being paged out to disk. This means that
46  it is important that functions dealing with private keys and
47  plaintexts (e.g., djn_keygen and djn_enc) only be run on
48  trusted machines. The resulting ciphertexts and public keys,
49  however, may of course be handled in an untrusted manner.
50 
51  */
52 
53 /******
54  TYPES
55  *******/
56 
57 /*
58  This represents a public key, which is the modulus n plus a generator h.
59  */
60 typedef struct {
61  int bits; /* e.g., 1024 */
62  int rbits; /* e.g., 512 */
63  mpz_t n; /* public modulus n = p q */
64  mpz_t n_squared; /* cached to avoid recomputing */
65  mpz_t h; /* generator h = -x^2 mod n */
66  mpz_t h_s; /* h_s = h^n mod n^2 */
67 } djn_pubkey_t;
68 
69 /*
70  This represents a Paillier private key; it needs to be used with a
71  djn_pubkey_t to be meaningful. It includes the Carmichael
72  function (lambda) of the modulus. The other value is kept for
73  efficiency and should be considered private.
74  */
75 typedef struct {
76  mpz_t lambda; /* lambda(n), i.e., lcm(p-1,q-1) */
77  mpz_t lambda_inverse; /* inverse of lambda (mod n)*/
78  mpz_t p; /* cached to avoid recomputing */
79  mpz_t q; /* cached to avoid recomputing */
80  mpz_t q_inverse; /* inverse of q (mod p) */
81  mpz_t q_squared_inverse; /* inverse of q^2 (mod p^2) */
82  mpz_t p_minusone; /* cached to avoid recomputing */
83  mpz_t q_minusone; /* cached to avoid recomputing */
84  mpz_t p_squared; /* cached to avoid recomputing */
85  mpz_t q_squared; /* cached to avoid recomputing */
86  mpz_t ordpsq; /* p^2-p */
87  mpz_t ordqsq; /* q^2-q */
88 } djn_prvkey_t;
89 
90 /*
91  This is the type of the callback functions used to obtain the
92  randomness needed by the probabilistic algorithms. The functions
93  djn_get_rand_devrandom and djn_get_rand_devurandom
94  (documented later) may be passed to any library function requiring a
95  djn_get_rand_t, or you may implement your own. If you implement
96  your own such function, it should fill in "len" random bytes in the
97  array "buf".
98  */
99 typedef void (*djn_get_rand_t)(void* buf, int len);
100 
101 /*****************
102  BASIC OPERATIONS
103  *****************/
104 
105 /*
106  Generate a keypair of length modulusbits using randomness from the
107  provided get_rand function. Space will be allocated for each of the
108  keys, and the given pointers will be set to point to the new
109  djn_pubkey_t and djn_prvkey_t structures. The functions
110  djn_get_rand_devrandom and djn_get_rand_devurandom may be
111  passed as the final argument.
112  */
113 void djn_keygen(unsigned int modulusbits, djn_pubkey_t** pub, djn_prvkey_t** prv);
114 
115 /*
116  Encrypt the given plaintext with the given public key using
117  randomness from get_rand for blinding. If res is not null, its
118  contents will be overwritten with the result. Otherwise, a new
119  djn_ciphertext_t will be allocated and returned.
120  */
121 void djn_encrypt(mpz_t res, djn_pubkey_t* pub, mpz_t pt, gmp_randstate_t rnd);
122 
123 /*
124  Encrypt the given plaintext with the given public key using
125  randomness from get_rand for blinding. If res is not null, its
126  contents will be overwritten with the result. Otherwise, a new
127  djn_ciphertext_t will be allocated and returned.
128  */
129 void djn_encrypt_crt(mpz_t res, djn_pubkey_t* pub, djn_prvkey_t* prv, mpz_t pt, gmp_randstate_t rnd);
130 
134 void djn_encrypt_fb(mpz_t res, djn_pubkey_t* pub, mpz_t plaintext, gmp_randstate_t rnd);
135 
136 /*
137  Decrypt the given ciphertext with the given key pair. If res is not
138  null, its contents will be overwritten with the result. Otherwise, a
139  new djn_plaintext_t will be allocated and returned.
140  */
141 void djn_decrypt(mpz_t res, djn_pubkey_t* pub, djn_prvkey_t* prv, mpz_t ct);
142 
143 /**********************
144  KEY IMPORT AND EXPORT
145  **********************/
146 
147 /*
148  Import or export public and private keys from or to hexadecimal,
149  ASCII strings, which are suitable for I/O. Note that the
150  corresponding public key is necessary to initialize a private key
151  from a hex string. In all cases, the returned value is allocated for
152  the caller and the values passed are unchanged.
153  */
154 char* djn_pubkey_to_hex(djn_pubkey_t* pub);
155 char* djn_prvkey_to_hex(djn_prvkey_t* prv);
156 djn_pubkey_t* djn_pubkey_from_hex(char* str);
157 djn_prvkey_t* djn_prvkey_from_hex(char* str, djn_pubkey_t* pub);
158 
159 /********
160  CLEANUP
161  ********/
162 
163 /*
164  These free the structures allocated and returned by various
165  functions within library and should be used when the structures are
166  no longer needed.
167  */
168 void djn_freepubkey(djn_pubkey_t* pub);
169 void djn_freeprvkey(djn_prvkey_t* prv);
170 
171 /***********
172  MISC STUFF
173  ***********/
174 
175 /*
176  Just a utility used internally when we need round a number of bits
177  up the number of bytes necessary to hold them.
178  */
179 #define PAILLIER_BITS_TO_BYTES(n) ((n) % 8 ? (n) / 8 + 1 : (n) / 8)
180 
181 void djn_pow_mod_n_crt(mpz_t res, const mpz_t b, const mpz_t e, const djn_pubkey_t* pub, const djn_prvkey_t* prv);
182 void djn_pow_mod_n_squared_crt(mpz_t res, const mpz_t b, const mpz_t e, const djn_pubkey_t* pub, const djn_prvkey_t* prv);
183 
187 void djn_complete_pubkey(unsigned int modulusbits, djn_pubkey_t** pub, mpz_t n, mpz_t h);
188 
189 #endif
void djn_encrypt(mpz_t res, djn_pubkey_t *pub, mpz_t pt, gmp_randstate_t rnd)
Definition: djn.cpp:187
void djn_decrypt(mpz_t res, djn_pubkey_t *pub, djn_prvkey_t *prv, mpz_t ct)
Definition: djn.cpp:284
Powmod Implementation.
void djn_complete_pubkey(unsigned int modulusbits, djn_pubkey_t **pub, mpz_t n, mpz_t h)
Definition: djn.cpp:38
Definition: djn.h:75
void djn_encrypt_fb(mpz_t res, djn_pubkey_t *pub, mpz_t plaintext, gmp_randstate_t rnd)
Definition: djn.cpp:251
Definition: djn.h:60
void djn_encrypt_crt(mpz_t res, djn_pubkey_t *pub, djn_prvkey_t *prv, mpz_t pt, gmp_randstate_t rnd)
Definition: djn.cpp:219