-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpaillier_io.c
243 lines (202 loc) · 5.96 KB
/
paillier_io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* @file paillier_io.c
*
* @date Created on: Sep 06, 2012
* @author Camille Vuillaume
* @copyright Camille Vuillaume, 2012
*
* This file is part of Paillier-GMP.
*
* Paillier-GMP is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* Paillier-GMP is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Paillier-GMP. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "tools.h"
#include "paillier.h"
/**
* Wrapper to the key generation function using stdio streams as inputs and output.
* @see paillier_keygen
*/
int paillier_keygen_str(FILE *public_key, FILE *private_key, int len) {
int result;
paillier_public_key pub;
paillier_private_key priv;
paillier_public_init(&pub);
paillier_private_init(&priv);
//generate keys
result = paillier_keygen(&pub, &priv, len);
//export public key
debug_msg("export public key: \n");
result |= paillier_public_out_str(public_key, &pub);
//export private key
debug_msg("export private key: \n");
result |= paillier_private_out_str(private_key, &priv);
debug_msg("freeing memory\n");
paillier_public_clear(&pub);
paillier_private_clear(&priv);
debug_msg("exiting\n");
return result;
}
/**
* Wrapper to the encryption function using stdio streams as inputs and output.
* @see paillier_encrypt
*/
int paillier_encrypt_str(FILE *ciphertext, FILE *plaintext, FILE *public_key) {
mpz_t c, m;
int result;
paillier_public_key pub;
mpz_init(c);
mpz_init(m);
paillier_public_init(&pub);
//import public key
debug_msg("importing public key: \n");
paillier_public_in_str(&pub, public_key);
//convert plaintext from stream
debug_msg("importing plaintext: \n");
gmp_fscanf(plaintext, "%Zx\n", m);
if(mpz_cmp(m, pub.n) >= 0) {
fputs("Warning, plaintext is larger than modulus n!\n", stderr);
}
//calculate encryption
result = paillier_encrypt(c, m, &pub);
//convert ciphertext to stream
debug_msg("exporting ciphertext: \n");
gmp_fprintf(ciphertext, "%Zx\n", c);
debug_msg("freeing memory\n");
mpz_clear(c);
mpz_clear(m);
paillier_public_clear(&pub);
debug_msg("exiting\n");
return result;
}
/**
* Wrapper to the decryption function using stdio streams as inputs and output.
* @see paillier_decrypt
*/
int paillier_decrypt_str(FILE *plaintext, FILE *ciphertext, FILE *private_key) {
mpz_t c, m, n2;
paillier_private_key priv;
int result;
mpz_init(c);
mpz_init(m);
mpz_init(n2);
paillier_private_init(&priv);
//import private key
debug_msg("importing private key: \n");
paillier_private_in_str(&priv, private_key);
//compute n^2
mpz_mul(n2, priv.n, priv.n);
//convert ciphertext from stream
debug_msg("importing ciphertext: \n");
gmp_fscanf(ciphertext, "%Zx\n", c);
if(mpz_cmp(c, n2) >= 0) {
fputs("Warning, ciphertext is larger than modulus n^2!\n", stderr);
}
//calculate decryption
result = paillier_decrypt(m, c, &priv);
//convert plaintext to stream
debug_msg("exporting plaintext: \n");
gmp_fprintf(plaintext, "%Zx\n", m);
debug_msg("freeing memory\n");
mpz_clear(c);
mpz_clear(m);
mpz_clear(n2);
paillier_private_clear(&priv);
debug_msg("exiting\n");
return result;
}
/**
* Wrapper to the homomorphic addition function using stdio streams as inputs and output.
* @see paillier_homomorphic_add
*/
int paillier_homomorphic_add_str(FILE *ciphertext3, FILE *ciphertext1, FILE *ciphertext2, FILE *public_key) {
mpz_t c3, c1, c2, n2;
paillier_public_key pub;
int result;
mpz_init(c3);
mpz_init(c1);
mpz_init(c2);
mpz_init(n2);
paillier_public_init(&pub);
//import public key
debug_msg("importing public key: \n");
paillier_public_in_str(&pub, public_key);
//compute n^2
mpz_mul(n2, pub.n, pub.n);
//convert ciphertexts from stream
debug_msg("importing ciphertexts: \n");
gmp_fscanf(ciphertext1, "%Zx\n", c1);
if(mpz_cmp(c1, n2) >= 0) {
fputs("Warning, first ciphertext is larger than modulus n^2!\n", stderr);
}
gmp_fscanf(ciphertext2, "%Zx\n", c2);
if(mpz_cmp(c2, n2) >= 0) {
fputs("Warning, second ciphertext is larger than modulus n^2!\n", stderr);
}
//calculate decryption
result = paillier_homomorphic_add(c3, c1, c2, &pub);
//convert result to stream
debug_msg("exporting result: \n");
gmp_fprintf(ciphertext3, "%Zx\n", c3);
debug_msg("freeing memory\n");
mpz_clear(c3);
mpz_clear(c1);
mpz_clear(c2);
mpz_clear(n2);
paillier_public_clear(&pub);
debug_msg("exiting\n");
return result;
}
/**
* Wrapper to the homomorphic multiplication function using stdio streams as inputs and output.
* @see paillier_homomorphic_add
*/
int paillier_homomorphic_multc_str(FILE *ciphertext2, FILE *ciphertext1, FILE *constant, FILE *public_key) {
mpz_t c2, c1, k, n2;
paillier_public_key pub;
int result;
mpz_init(c2);
mpz_init(c1);
mpz_init(k);
mpz_init(n2);
paillier_public_init(&pub);
//import public key
debug_msg("importing public key: \n");
paillier_public_in_str(&pub, public_key);
//compute n^2
mpz_mul(n2, pub.n, pub.n);
//convert ciphertext from stream
debug_msg("importing ciphertexts: \n");
gmp_fscanf(ciphertext1, "%Zx\n", c1);
if(mpz_cmp(c1, n2) >= 0) {
fputs("Warning, first ciphertext is larger than modulus n^2!\n", stderr);
}
gmp_fscanf(constant, "%Zx\n", k);
if(mpz_cmp(k, pub.n) >= 0) {
fputs("Warning, constant is larger than modulus n!\n", stderr);
}
//calculate decryption
result = paillier_homomorphic_multc(c2, c1, k, &pub);
//convert result to stream
debug_msg("exporting result: \n");
gmp_fprintf(ciphertext2, "%Zx\n", c2);
debug_msg("freeing memory\n");
mpz_clear(c2);
mpz_clear(c1);
mpz_clear(k);
mpz_clear(n2);
paillier_public_clear(&pub);
debug_msg("exiting\n");
return result;
}