-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
577 lines (498 loc) · 17.4 KB
/
main.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include <time.h>
#define SECURITY_BITS 512
/****************************************************************************
* Crypto & Watermarking project
*
*
* Quentin Bourgue
* Paul-Henri Mignot
*
* Data Hiding in Homomorphically Encrypted Medical Image
*
****************************************************************************/
/****************************************************************************
*
* Paillier
*
****************************************************************************/
void generatePrime(mpz_t output, unsigned int bits, gmp_randstate_t state) {
do {
mpz_urandomb(output, state, bits);
mpz_nextprime(output, output);
}
while (mpz_sizeinbase(output, 2) != bits);
}
void computePhi(mpz_t p, mpz_t q, mpz_t output) {
mpz_t pMinusOne, qMinusOne;
mpz_inits(pMinusOne, qMinusOne, NULL);
mpz_sub_ui(pMinusOne, p, 1);
mpz_sub_ui(qMinusOne, q, 1);
mpz_mul(output, pMinusOne, qMinusOne);
mpz_clears(pMinusOne, qMinusOne, NULL);
}
void generateKeys(mpz_t p, mpz_t q, mpz_t e, mpz_t d, mpz_t N, gmp_randstate_t state) {
mpz_t phi, gcd;
mpz_inits(phi, gcd, NULL);
computePhi(p, q, phi);
mpz_mul(N, p, q);
do {
mpz_urandomm(e, state, phi);
mpz_gcd(gcd, e, phi);
} while (mpz_cmp_ui(gcd, 1) != 0);
mpz_invert(d, e, phi);
mpz_t test1, test2;
mpz_inits(test1, test2, NULL);
mpz_mul(test2, d, e);
mpz_mod(test1, test2, phi);
gmp_printf("e * d mod phi = %Zu", test1);
gmp_printf("e = %Zu, d = %Zu, phi = %Zu\n", e, d, phi);
mpz_clears(phi, gcd, NULL);
}
void paillierEncrypt(mpz_t m, mpz_t r, mpz_t N, mpz_t c){
mpz_t plus1, Nsquare, rn, nm ;
mpz_inits(plus1, Nsquare, rn, nm, NULL);
mpz_add_ui(plus1, N, 1);
mpz_pow_ui(Nsquare, N, 2);
mpz_powm(nm, plus1, m, Nsquare);
mpz_powm(rn, r, N, Nsquare);
mpz_mul(c, nm, rn);
mpz_mod(c, c, Nsquare);
mpz_clears(plus1, Nsquare, rn, nm, NULL);
}
// Baptiste and Vincent Decrypt
/*
void paillierDecrypt(mpz_t c, mpz_t N, mpz_t m, mpz_t phi){
mpz_t rInvert, product, Nsquare, r, rminus, minusN, temp, minusOne;
mpz_inits(rInvert, product, Nsquare, r, rminus, minusN, temp, minusOne, NULL);
mpz_set_si(minusOne, -1);
mpz_pow_ui(Nsquare, N, 2);
mpz_powm(temp, N, minusOne, phi);
mpz_powm(r, c, temp, N);
mpz_mul_si(minusN, N,-1);
mpz_powm(rminus, r, minusN, Nsquare);
mpz_mul(product, c, rminus);
mpz_mod(product, product, Nsquare);
mpz_sub_ui(product, product, 1);
mpz_div(m, product, N);
mpz_clears(rInvert, product, Nsquare, r, rminus, minusN, temp, minusOne, NULL);
}
*/
void paillierDecrypt(mpz_t cipher, mpz_t N, mpz_t output, mpz_t phi){
mpz_t Nsquare, mu, dividende;
mpz_inits(Nsquare, mu, dividende, NULL);
mpz_pow_ui(Nsquare, N, 2);
mpz_powm(dividende, cipher, phi, Nsquare);
mpz_sub_ui(dividende, dividende, 1);
mpz_div(output, dividende, N);
// mu is the inv of phi modulo N
mpz_invert(mu, phi, N);
mpz_mul(output, output, mu);
mpz_mod(output, output, N);
mpz_clears(Nsquare, mu, dividende, NULL);
}
/*
void paillierDecrypt(mpz_t cipher, mpz_t N, mpz_t output, mpz_t phi){
mpz_t Nsquare, dividende;
mpz_inits(Nsquare, dividende, NULL);
mpz_pow_ui(Nsquare, N, 2);
mpz_powm(dividende, cipher, phi, Nsquare);
mpz_sub_ui(dividende, dividende, 1);
// (N exp phi mod N**2 - 1) / N
mpz_div(output, dividende, N);
mpz_mod(output, output, N);
mpz_clears(Nsquare, dividende, NULL);
}
*/
int testPaillier() {
gmp_randstate_t state;
gmp_randinit_default(state);
gmp_randseed_ui(state, time(NULL));
mpz_t N, m, c, r, p, q, phi, o;
mpz_t m2, m3, c2, c3, r2, r3, N2;
mpz_inits(N, m, c, r, p, q, phi, o, NULL);
mpz_inits(m2, m3, c2, c3, r2, r3, N2, NULL);
generatePrime(p, SECURITY_BITS, state);
generatePrime(q, SECURITY_BITS, state);
mpz_mul(N, p, q);
computePhi(p, q, phi);
printf("m : ");
gmp_scanf("%Zu", m);
printf("\nr : ");
gmp_scanf("%Zu", r);
printf("m2 : ");
gmp_scanf("%Zu", m2);
printf("\nr2 : ");
gmp_scanf("%Zu", r2);
paillierEncrypt(m, r, N, c);
gmp_printf("\nEncrypting %Zu gives %Zu\n", m, c);
paillierEncrypt(m2, r2, N, c2);
gmp_printf("\nEncrypting %Zu gives %Zu\n", m2, c2);
//paillierDecrypt(c2, N, m2, phi);
//gmp_printf("Decrypting c2 %Zu __ gives __ %Zu\n", c2, m2);
mpz_t mul_e;
mpz_init(mul_e);
mpz_mul(mul_e, c, c2);
mpz_pow_ui(N2, N, 2);
mpz_mod(mul_e, mul_e, N2);
gmp_printf("\nMUL c et c2: %Zu \n", mul_e);
paillierDecrypt(mul_e, N, o, phi);
gmp_printf("Decrypting %Zu __ gives __ %Zu\n", mul_e, o);
mpz_add(m, m, m2);
mpz_mul(r, r, r2);
paillierEncrypt(m, r, N, c);
gmp_printf("\n Encrypting from m1+m2 %Zu, and r1+r2 %Zu gives %Zu\n", m,r, c);
paillierDecrypt(c, N, m, phi);
gmp_printf("Decrypting %Zu gives %Zu\n", c, m);
mpz_clears(N, m, c, r, p, q, phi, o, NULL);
mpz_clears(m2, m3, c2, c3, r2, r3, N2, NULL);
mpz_clear(mul_e);
gmp_randclear(state);
return 0;
}
/****************************************************************************
*
* Data Generation
*
* (Create an array of V elements, which are random integers of 8 bytes)
****************************************************************************/
void data_generation(mpz_t * data, unsigned long int V){
gmp_randstate_t state;
gmp_randinit_default(state);
gmp_randseed_ui(state, time(NULL));
mpz_t random_bytes;
mpz_inits(random_bytes, NULL);
unsigned long int i = 0;
unsigned int bitcnt = 8;
do {
mpz_urandomb(random_bytes, state, bitcnt);
mpz_init_set(data[i], random_bytes);
//gmp_printf("random_bytes n°%d: %Zu\n", i, random_bytes);
i++;
}
while (i<V);
mpz_clears(random_bytes, NULL);
gmp_randclear(state);
}
/****************************************************************************
*
* Pre-watermarking
*
* (Create an array of p elements, which are random bits)
*
* (Embed the watermark on the data)
****************************************************************************/
void watermark_generation(mpz_t *watermark, unsigned int p){
gmp_randstate_t state;
gmp_randinit_default(state);
gmp_randseed_ui(state, time(NULL));
mpz_t random_bit;
mpz_inits(random_bit, NULL);
unsigned int j = 0;
unsigned int bitcnt = 1;
do {
mpz_urandomb(random_bit, state, bitcnt);
mpz_init_set(watermark[j], random_bit);
//gmp_printf("random_bit n°%d: %Zu\n",j, random_bit);
j++;
}
while (j<p);
mpz_clears(random_bit, NULL);
gmp_randclear(state);
}
void pre_watermarking(mpz_t * watermark, mpz_t * data, int N, int p){
// embed the watermark
mpz_t pixel, two;
mpz_inits(pixel, two, NULL);
mpz_set_ui(two, 2);
int even;
for (int in = 0; in<N; in++){
for (int jp = 0; jp<p; jp++){
mpz_set(pixel,data[in*p+jp]);
// get the parity of the pixel, 0 if odd(LSB=1), 1 if even(LSB=0)
even = mpz_divisible_p(pixel,two);
// embed the watermark on the LSB
if ((mpz_cmp_ui(watermark[jp],1) != 0)&&(even==1)){ //Wi=0 and LSB=0 -> OK, nothing to do
}
else if ((mpz_cmp_ui(watermark[jp],1) != 0)&&(even==0)){ //Wi=0 and LSB=1 -> Insert Wi instead of the LSB --> substract one
mpz_sub_ui(data[in*p+jp], data[in*p+jp], 1);
}
else if ((mpz_cmp_ui(watermark[jp],0) != 0)&&(even==1)){ //Wi=1 and LSB=0 -> Insert Wi instead of the LSB --> add one
mpz_add_ui(data[in*p+jp], data[in*p+jp], 1);
}
else if ((mpz_cmp_ui(watermark[jp],0) != 0)&&(even==0)){ //Wi=1 and LSB=1 -> OK, nothing to do
}
else{
printf("ERROR, THIS CASE SHOULD NOT HAPPENED !");
}
}
}
mpz_clears(pixel, two, NULL);
}
/****************************************************************************
*
* Encryption
*
****************************************************************************/
void paillier_init(mpz_t p1, mpz_t q1, mpz_t N1, mpz_t r, mpz_t phi){
gmp_randstate_t state;
gmp_randinit_default(state);
gmp_randseed_ui(state, time(NULL));
generatePrime(p1, SECURITY_BITS, state);
generatePrime(q1, SECURITY_BITS, state);
mpz_mul(N1, p1, q1);
computePhi(p1, q1, phi);
gmp_randclear(state);
}
void data_encryption(mpz_t * data, unsigned long int V, mpz_t * encrypted_data, mpz_t N1, mpz_t r, mpz_t phi){
mpz_t encrypted_value, decrypted_value;
mpz_inits(encrypted_value, decrypted_value, NULL);
for (int i = 0; i<V; i++){
paillierEncrypt(data[i], r, N1, encrypted_value);
mpz_init_set(encrypted_data[i], encrypted_value);
//paillierDecrypt(encrypted_value, N1, decrypted_value, phi);
}
mpz_clears(encrypted_value, decrypted_value, NULL);
}
/****************************************************************************
*
* Message embedding
*
****************************************************************************/
void embed_message_to_pixel(mpz_t enc_emb_pixel, mpz_t enc_pixel, mpz_t embedding, mpz_t distortion, mpz_t N, gmp_randstate_t state){
mpz_t r, enc_distortion, Nsquare;
mpz_inits(r, enc_distortion, Nsquare, NULL);
mpz_pow_ui(Nsquare, N, 2);
int even;
do {
//Pick random r : 0<r<N
do{
mpz_urandomm(r, state, N);
}
while (mpz_cmp_ui(r,0)==0);
paillierEncrypt(distortion, r, N, enc_distortion);
mpz_mul(enc_emb_pixel ,enc_pixel, enc_distortion);
// The multiplication of Paillet encrypted value is modulo N squared.
mpz_mod(enc_emb_pixel, enc_emb_pixel, Nsquare);
even = mpz_divisible_ui_p(enc_emb_pixel,2);
}
while(mpz_cmp_ui(embedding,even) == 0);
// when embeeding = 0, enc_emb_pixel must be even, so even must be 1;
// when embeeding = 1, enc_emb_pixel must be odd, so even must be 0;
mpz_clears(r, enc_distortion, Nsquare, NULL);
}
void message_embedding(mpz_t * enc_emb_data, mpz_t * enc_data, unsigned long int V, int p, int N, mpz_t N1, mpz_t * watermark){
gmp_randstate_t state;
gmp_randinit_default(state);
gmp_randseed_ui(state, time(NULL));
mpz_t enc_pixel, enc_emb_pixel, embedding_p, distortion_p;
mpz_inits(enc_pixel, enc_emb_pixel, embedding_p, distortion_p, NULL);
for (int in = 0; in<N; in++){
for (int jp = 0; jp<p; jp++){
mpz_set(enc_pixel,enc_data[in*p+jp]);
mpz_set(embedding_p, watermark[in]);
// distortion = embedding * delta / 2;
mpz_set(distortion_p, watermark[in]);
embed_message_to_pixel(enc_emb_pixel, enc_pixel, embedding_p, distortion_p, N1, state);
mpz_init_set(enc_emb_data[in*p+jp], enc_emb_pixel);
}
}
mpz_clears(enc_pixel, enc_emb_pixel, embedding_p, distortion_p, NULL);
gmp_randclear(state);
}
/****************************************************************************
*
* Message extraction from encrypted domain
*
****************************************************************************/
void message_extraction_enc(mpz_t * enc_emb_data, unsigned long int V, mpz_t * extracted_message, unsigned int p, unsigned int N){
mpz_t enc_pixel, two;
mpz_inits(enc_pixel, two, NULL);
mpz_set_ui(two, 2);
unsigned int even;
for (int in = 0; in<N; in++){ // define the message values
mpz_set(enc_pixel,enc_emb_data[in*p]);
// get the parity of the pixel, 0 if odd(LSB=1), 1 if even(LSB=0)
even = mpz_divisible_p(enc_pixel,two);
// if the encrypted pixel is even, the message is 0
if (even==1){ mpz_init_set_ui(extracted_message[in], 0); }
// if the encrypted pixel is odd, the message is 1
else { mpz_init_set_ui(extracted_message[in], 1); }
for (int jp = 1; jp<p; jp++){ // redundancy, check for each subset that the message value defined is correct
mpz_set(enc_pixel,enc_emb_data[in*p+jp]);
even = mpz_divisible_p(enc_pixel,two);
if ((even==1) && (mpz_cmp_ui(extracted_message[in],0) == 0)){
// redundancy: the message value i_n corresponds
}
// if the encrypted pixel is odd, the message is 1
else if ((even==0) && (mpz_cmp_ui(extracted_message[in],1) == 0)) {
// redundancy: the message value i_n corresponds
}
else {
printf("PROBLEM !\n");
}
}
}
mpz_clears(enc_pixel, two, NULL);
}
/****************************************************************************
*
* Data decryption
*
****************************************************************************/
void data_decryption(mpz_t * encrypted_data, unsigned long int V, mpz_t * decrypted_data, mpz_t N1, mpz_t phi){
mpz_t decrypted_value, modulo;
mpz_inits(decrypted_value, modulo, NULL);
mpz_set_ui(modulo, 256);
for (int i = 0; i<V; i++){
paillierDecrypt(encrypted_data[i], N1, decrypted_value, phi);
// If pixel decrypted is out of bound, reduce by two its value
if (mpz_cmp_ui(decrypted_value, 256) == 0) {
mpz_set_ui(decrypted_value, 254);
}
mpz_init_set(decrypted_data[i], decrypted_value);
}
mpz_clears(decrypted_value, modulo, NULL);
}
/****************************************************************************
*
* Message extraction from spatial domain
*
****************************************************************************/
void message_extraction_spa(mpz_t * decrypted_data, unsigned long int V, mpz_t * extracted_message, mpz_t * watermark, unsigned int p, unsigned int N){
mpz_t spa_pixel, two;
mpz_inits(spa_pixel, two, NULL);
mpz_set_ui(two,2);
unsigned int even;
for (int in = 0; in<N; in++){
mpz_set(spa_pixel,decrypted_data[in*p]);
even = mpz_divisible_p(spa_pixel, two);
// if the pixel in the spa. domain is the same than the one in the prewatermark -> the message is 0
// get the parity of the pixel, 0 if odd(LSB=1), 1 if even(LSB=0)
if ((mpz_cmp_ui(watermark[0],1) == 0)&&(even==1)){ //Wi=1 and LSB=0 -> the message is 1
mpz_init_set_ui(extracted_message[in], 1);
}
else if ((mpz_cmp_ui(watermark[0],1) == 0)&&(even==0)){ //Wi=1 and LSB=1 -> the message is 0
mpz_init_set_ui(extracted_message[in], 0);
}
else if ((mpz_cmp_ui(watermark[0],0) == 0)&&(even==1)){ //Wi=0 and LSB=0 -> the message is 0
mpz_init_set_ui(extracted_message[in], 0);
}
else if ((mpz_cmp_ui(watermark[0],0) == 0)&&(even==0)){ //Wi=0 and LSB=1 -> the message is 1
mpz_init_set_ui(extracted_message[in], 1);
}
else{
printf("ERROR, THIS CASE SHOULD NOT HAPPENED !");
}
for (int jp = 1; jp<p; jp++){
mpz_set(spa_pixel,decrypted_data[in*p+jp]);
even = mpz_divisible_p(spa_pixel, two);
// if the pixel in the spa. domain is the same than the one in the prewatermark -> the message is 0
// get the parity of the pixel, 0 if odd(LSB=1), 1 if even(LSB=0)
if ((mpz_cmp_ui(watermark[jp],1) == 0)&&(even==1)){ //Wi=1 and LSB=0 -> the message is 1
mpz_init_set_ui(extracted_message[in], 1);
}
else if ((mpz_cmp_ui(watermark[jp],1) == 0)&&(even==0)){ //Wi=1 and LSB=1 -> the message is 0
mpz_init_set_ui(extracted_message[in], 0);
}
else if ((mpz_cmp_ui(watermark[jp],0) == 0)&&(even==1)){ //Wi=0 and LSB=0 -> the message is 0
mpz_init_set_ui(extracted_message[in], 0);
}
else if ((mpz_cmp_ui(watermark[jp],0) == 0)&&(even==0)){ //Wi=0 and LSB=1 -> the message is 1
mpz_init_set_ui(extracted_message[in], 1);
}
else{
printf("ERROR, THIS CASE SHOULD NOT HAPPENED !");
}
}
}
mpz_clears(spa_pixel, two, NULL);
}
/****************************************************************************
*
* DEBUG: display data
*
****************************************************************************/
void display_array(mpz_t *array, unsigned long int V){
for (int i=0; i<V;i++){
gmp_printf("array[%d]=%Zu\n",i,array[i]);
}
printf("\n");
}
/****************************************************************************
*
* Main
*
****************************************************************************/
int main(int argc, char* argv[]) {
// Init parameters
unsigned int p = 20;
unsigned int N = 40;
unsigned long int V=p*N;
// Data Generation
mpz_t * data;
data = malloc(sizeof(mpz_t) * V);
data_generation(data, V);
// Pre-watermarking
mpz_t * watermark;
watermark = malloc(sizeof(mpz_t) * p);
watermark_generation(watermark, p);
printf("##########\nOriginal data:\n");
display_array(data, V);
printf("##########\nWatermark:\n");
display_array(watermark, p);
pre_watermarking(watermark, data, N, p);
printf("##########\nPrewatermarked data:\n");
display_array(data, V);
// Encryption
mpz_t * encrypted_data;
encrypted_data = malloc(sizeof(mpz_t) * V);
// Paillier parameters
mpz_t N1, r, p1, q1, phi, o;
mpz_inits(N1, r, p1, q1, phi, o, NULL);
printf("r: ");
gmp_scanf("%Zu",r);
paillier_init(p1,q1,N1,r,phi);
// Paillier encryption
data_encryption(data, V, encrypted_data, N1, r, phi);
//printf("##########\nEncrypted data:\n");
//display_array(encrypted_data, V);
// Message embedding
mpz_t * enc_emb_data;
enc_emb_data = malloc(sizeof(mpz_t) * V);
mpz_t * message;
message = malloc(sizeof(mpz_t) * N);
watermark_generation(message, N);
printf("##########\nMessage:\n");
display_array(message, N);
message_embedding(enc_emb_data, encrypted_data, V, p, N, N1, message);
//printf("##########\nEncrypted emb. data:\n");
//display_array(enc_emb_data, V);
// Mesage extraction from encrypted domain
mpz_t * extracted_message;
extracted_message = malloc(sizeof(mpz_t) * N);
message_extraction_enc(enc_emb_data, V, extracted_message, p, N);
printf("##########\nExtracted message from encrypted domain:\n");
display_array(extracted_message, N);
// Data decryption
mpz_t * decrypted_data;
decrypted_data = malloc(sizeof(mpz_t) * V);
//data_decryption(encrypted_data, V, decrypted_data, N1, phi);
data_decryption(enc_emb_data, V, decrypted_data, N1, phi);
printf("##########\nDecrypted data:\n");
display_array(decrypted_data, V);
// Mesage extraction from spatial domain
message_extraction_spa(decrypted_data, V, extracted_message, watermark, p, N);
printf("##########\nExtracted message from spatial domain:\n");
display_array(extracted_message, N);
// clear/free
mpz_clears(N1, r, p1, q1, phi, o, NULL);
free(data);
free(watermark);
free(encrypted_data);
free(message);
free(extracted_message);
return 0;
}