My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
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
#include "perseus.h"
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>

#undef DEBUG

/******************************************************/
/* Main test procedure for the libperseus */
/* The file to protect is given as first argument to */
/* the program (argv[1]) */
/******************************************************/
int main(int argc, char * argv[])
{
struct stat st;
PUNCT_CONC_CODE * Pcc;
NOISE_GEN * NGen;
INIT_NOISE_GEN * aKey;
unsigned long int i, k, data_size, dataLength;
unsigned long int encoded_data_size;
unsigned char * data, * encoded_data, * dataDecoded;
char buf[512];
FILE * fin, * fout;

/* Initialization steps: check whether a file name is given */
if(stat(argv[1], &st))
{
perror("stat");
exit(0);
}

/* If exists, open the file */
fin = fopen(argv[1], "r");
if(fin == NULL)
{
perror("File opening on error!");
exit(0);
}

/* Get the file size and allocate the array */
data_size = (unsigned long int)(st.st_size);
printf("Size according to stat %lu\n", data_size);
data = (unsigned char *)calloc(data_size, sizeof(unsigned char));

/* Read data from file */
data_size--;
for(i = 0L;i < data_size;i++) data[i] = (unsigned char)(getc(fin));
fclose(fin);

#ifdef DEBUG
printf("Data to protect = %s\n", data);
#endif

/************************************************************/
/* Generate the secret elements: */
/* - a PCC encoder */
/* - the Noise generator secret key */
/* Whenever the transmission is reset we need to generate */
/* these elements and send them through an https session */
/* before coding/decoding (https part not played here) */
/************************************************************/

/* Generate the PCC encoder */
Pcc = generateCode();
if(!Pcc)
{
perror("Encoder variable allocation on error!");
free(data);
exit(0);
}

/* Noise generator secret key generation */
aKey = (INIT_NOISE_GEN *)calloc(1, sizeof(INIT_NOISE_GEN));
if(aKey == NULL)
{
perror("Noise generator secret key variable allocation on error!");
free(data);
for(k = 0;k < Pcc->mK;k++) free(Pcc->mPoly[k]);
free(Pcc->mPoly);
free(Pcc->mMatrix);
free(Pcc);
exit(0);
}

aKey->INIT1 = (unsigned long int)((float)(0xFFFFFFFFL) * alea());
aKey->INIT2 = (unsigned long int)((float)(0xFFFFFFFFL) * alea());
aKey->INIT3 = (unsigned long int)((float)(0xFFFFFFFFL) * alea());
aKey->INIT4 = (unsigned long int)((float)(0xFFFFFFFFL) * alea());

/* Noise generator variable allocation */
NGen = (NOISE_GEN *)calloc(1, sizeof(NOISE_GEN));
if(!NGen)
{
perror("Noise generator variable allocation on error!");
free(data);
for(k = 0;k < Pcc->mK;k++) free(Pcc->mPoly[k]);
free(Pcc->mPoly);
free(Pcc->mMatrix);
free(Pcc);
free(aKey);
exit(0);
}

/* Noise generator init */
if(!Gen_Noise_Generator(NGen, aKey))
{
perror("Noise encoder generation on error!");
free(data);
for(k = 0;k < Pcc->mK;k++) free(Pcc->mPoly[k]);
free(Pcc->mPoly);
free(Pcc->mMatrix);
free(Pcc);
free(aKey);
free(NGen->Bf);
free(NGen);
exit(0);
}
/************************************************************/
/* End of secret elements generation */
/************************************************************/

/***********************************************************/
/* Http session to share the secret elements */
/* (not played here) */
/***********************************************************/

/************************************************************/
/* Now let us proceed with the PCC encoding. It includes: */
/* - char2bin encoding */
/* - the PCC coding itself */
/* - data puncturing */
/* - bin2hex encoding */
/* - Addition of deterministic noise */
/* */
/* Message to protect is contained in the array "data" */
/* Final result of the PCC encoding is contained in the */
/* array "encoded_data" */
/************************************************************/
encoded_data_size = 0L;
if(!pcc_Code(Pcc, data, data_size, &encoded_data, &encoded_data_size, NGen, aKey))
{
perror("Encoding error\n");
free(data);
for(k = 0;k < Pcc->mK;k++) free(Pcc->mPoly[k]);
free(Pcc->mPoly);
free(Pcc->mMatrix);
free(Pcc);
free(aKey);
free(NGen->Bf);
free(NGen);
exit(0);
}

#ifdef DEBUG
printf("Data after PCC encoding = %s\n", encoded_data);
#endif

/***********************************************************/
/* Encoded data (array encoded_data) of encoded_data_size */
/* bytes are then sent according any protocol you choose */
/***********************************************************/

/***********************************************************/
/* Received data must now be decoded. Let us proceed with */
/* the PCC decoding. It includes: */
/* - Remove the deterministic noise */
/* - hex2bin encoding */
/* - data unpuncturing */
/* - Viterbi decoding */
/* */
/* Encoded data are in the array "dataCoded". Decoded data */
/* are contained in the array "dataDecoded" */
/***********************************************************/
dataLength = 0L;
if(!pcc_decode(Pcc, NGen, aKey, encoded_data, encoded_data_size, &dataDecoded, &dataLength))
{
perror("Decoding error\n");
free(data);
for(k = 0;k < Pcc->mK;k++) free(Pcc->mPoly[k]);
free(Pcc->mPoly);
free(Pcc->mMatrix);
free(Pcc);
free(aKey);
free(NGen->Bf);
free(NGen);
free(encoded_data);
exit(1);
}

/* Write decoded data in a file */
strncpy(buf, argv[1], 500);
strncat(buf, ".decoded", 8);
fout = fopen(buf, "w");
if(fout == NULL)
{
perror("Error on opening output file\n");
free(data);
for(k = 0;k < Pcc->mK;k++) free(Pcc->mPoly[k]);
free(Pcc->mPoly);
free(Pcc->mMatrix);
free(Pcc);
free(aKey);
free(NGen->Bf);
free(NGen);
free(encoded_data);
free(dataDecoded);
exit(0);
}
fprintf(fout, "%s\n", dataDecoded);
fclose(fout);
free(data);
free(aKey);
free(NGen->Bf);
free(NGen);
free(encoded_data);
free(dataDecoded);

/* Remove the code and free the relevant fields/data */
/* structure. A new code can be generated */
delete_code(Pcc);
return(0);
}

Change log

0c6b27b37b94 by Eddy Deligne <eddy.deligne> on Feb 3, 2011   Diff
New library version
Java, Python and Ruby binding included
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 7293 bytes, 222 lines
Powered by Google Project Hosting