BitPunch McEliece  v0.0.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
main.c
Go to the documentation of this file.
1 /*
2  This file is part of BitPunch
3  Copyright (C) 2014-2015 Frantisek Uhrecky <frantisek.uhrecky[what here]gmail.com>
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <bitpunch/bitpunch.h>
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <time.h>
23 #include <sys/time.h>
24 
26 #include <bitpunch/asn1/asn1.h>
27 
28 int testCmpMecsCtx(const BPU_T_Mecs_Ctx *ctx1, const BPU_T_Mecs_Ctx *ctx2) {
29  int i, j, rc = 0;
30  if (ctx1->type != ctx2->type) {
31  BPU_printError("type");
32  }
33  if (ctx1->ct_len != ctx2->ct_len) {
34  BPU_printError("ct_len");
35  }
36  if (ctx1->pt_len != ctx2->pt_len) {
37  BPU_printError("pt_len");
38  }
39  if (ctx1->_decrypt != ctx2->_decrypt) {
40  BPU_printError("_decrypt");
41  }
42  if (ctx1->_encrypt != ctx2->_encrypt) {
43  BPU_printError("_encrypt");
44  }
45  if (ctx1->code_ctx->code_len != ctx2->code_ctx->code_len) {
46  BPU_printError("code_len");
47  }
48  if (ctx1->code_ctx->msg_len != ctx2->code_ctx->msg_len) {
49  BPU_printError("msg_len");
50  }
51  if (ctx1->code_ctx->t != ctx2->code_ctx->t) {
52  BPU_printError("t");
53  }
54  if (ctx1->code_ctx->type != ctx2->code_ctx->type) {
55  BPU_printError("code type");
56  }
57  if (ctx1->code_ctx->_decode != ctx2->code_ctx->_decode) {
58  BPU_printError("_decode");
59  }
60  if (ctx1->code_ctx->_encode != ctx2->code_ctx->_encode) {
61  BPU_printError("_encode");
62  }
63  if (ctx1->code_ctx->e->len != ctx2->code_ctx->e->len) {
64  BPU_printError("e.len");
65  }
66  if (BPU_gf2xPolyCmp(ctx1->code_ctx->code_spec->goppa->g, ctx2->code_ctx->code_spec->goppa->g)) {
67  BPU_printError("g poly");
68  }
70  BPU_printError("support len");
71  }
73  BPU_printError("perm size");
74  }
76  BPU_printError("g_mat elements_in_row");
77  }
79  BPU_printError("g_mat element_bit_size");
80  }
81  if (ctx1->code_ctx->code_spec->goppa->g_mat->k != ctx2->code_ctx->code_spec->goppa->g_mat->k) {
82  BPU_printError("g_mat k");
83  }
84  if (ctx1->code_ctx->code_spec->goppa->g_mat->n != ctx2->code_ctx->code_spec->goppa->g_mat->n) {
85  BPU_printError("g_mat n");
86  }
87  for (i = 0; i < ctx1->code_ctx->code_spec->goppa->permutation->size; i++) {
89  BPU_printError("perm diff");
90  break;
91  }
92  }
93  if (ctx1->code_ctx->code_spec->goppa->h_mat->k != ctx2->code_ctx->code_spec->goppa->h_mat->k) {
94  BPU_printError("h_mat k");
95  }
96  if (ctx1->code_ctx->code_spec->goppa->h_mat->n != ctx2->code_ctx->code_spec->goppa->h_mat->n) {
97  BPU_printError("h_mat n");
98  }
99  for (i = 0; i < ctx1->code_ctx->code_spec->goppa->g_mat->elements_in_row; i++) {
100  for (j = 0; j < ctx1->code_ctx->code_spec->goppa->g_mat->k; j++) {
101  if (ctx1->code_ctx->code_spec->goppa->g_mat->elements[j][i] != ctx2->code_ctx->code_spec->goppa->g_mat->elements[j][i]) {
102  BPU_printError("g_mat diff");
103  j = -1;
104  break;
105  }
106  }
107  if (j == -1) {
108  break;
109  }
110  }
111  for (i = 0; i < ctx1->code_ctx->code_spec->goppa->h_mat->n; i++) {
112  for (j = 0; j < ctx1->code_ctx->code_spec->goppa->h_mat->k; j++) {
113  if (ctx1->code_ctx->code_spec->goppa->h_mat->elements[j][i] != ctx2->code_ctx->code_spec->goppa->h_mat->elements[j][i]) {
114  BPU_printError("h_mat diff");
115  j = -1;
116  break;
117  }
118  }
119  if (j == -1) {
120  break;
121  }
122  }
123  return rc;
124 }
125 
127 // BPU_T_Mecs_Ctx *ctx = NULL;
128  BPU_T_GF2_Vector *ct, *pt_in, *pt_out;
129  int rc = 0;
130 
131  /***************************************/
132  fprintf(stderr, "Key generation...\n");
133  // key pair generation
134  if (BPU_mecsGenKeyPair(ctx)) {
135 // if (BPU_asn1LoadKeyPair(&ctx, "prikey.der", "pubkey.der")) {
136  BPU_printError("Key generation error");
137 
138  return 1;
139  }
140  /***************************************/
141  // prepare plain text, allocate memory and init random plaintext
142  if (BPU_gf2VecMalloc(&pt_in, ctx->pt_len)) {
143  BPU_printError("PT initialisation error");
144 
145  return 1;
146  }
147  BPU_gf2VecRand(pt_in, 0);
148 
149  // alocate cipher text vector
150  if (BPU_gf2VecMalloc(&ct, ctx->ct_len)) {
151  BPU_printError("CT vector allocation error");
152 
153  BPU_gf2VecFree(&pt_in);
154  return 1;
155  }
156  // prepare plain text, allocate memory and init random plaintext
157  if (BPU_gf2VecMalloc(&pt_out, ctx->pt_len)) {
158  BPU_printError("PT out initialisation error");
159 
160  return 1;
161  }
162  BPU_gf2VecRand(pt_out, 0);
163  /***************************************/
164  fprintf(stderr, "Encryption...\n");
165  // BPU_encrypt plain text
166  if (BPU_mecsEncrypt(ct, pt_in, ctx)) {
167  BPU_printError("Encryption error");
168 
169  BPU_gf2VecFree(&ct);
170  BPU_gf2VecFree(&pt_in);
171  BPU_gf2VecFree(&pt_out);
172  return 1;
173  }
174  // exit(0);
175  /***************************************/
176  fprintf(stderr, "Decryption...\n");
177  // decrypt cipher text
178  if (BPU_mecsDecrypt(pt_out, ct, ctx)) {
179  BPU_printError("Decryption error");
180 
181  BPU_gf2VecFree(&ct);
182  BPU_gf2VecFree(&pt_in);
183  BPU_gf2VecFree(&pt_out);
184  return 1;
185  }
186  /***************************************/
187 
188  // check for correct decryption
189  if (BPU_gf2VecCmp(pt_in, pt_out)) {
190  BPU_printError("\nOutput plain text differs from input");
191 
192  rc = 2;
193  }
194  else {
195  fprintf(stderr, "\nSUCCESS: Input plain text is equal to output plain text.\n");
196  }
197  // clean up
198  /***************************************/
199  fprintf(stderr, "\nCleaning up...\n");
200  BPU_gf2VecFree(&pt_in);
201  BPU_gf2VecFree(&pt_out);
202  BPU_gf2VecFree(&ct);
203  return rc;
204 }
205 
206 #ifdef BPU_CONF_ASN1
208  int rc = 0;
209  // MUST BE NULL
210  BPU_T_Mecs_Ctx *ctx = NULL;
211  BPU_T_Mecs_Ctx *ctx_2 = NULL;
212  BPU_T_UN_Mecs_Params params;
213 
214  /***************************************/
215  // mce initialisation t = 50, m = 11
216  fprintf(stderr, "Basic GOPPA Initialisation...\n");
217  if (BPU_mecsInitParamsGoppa(&params, 11, 50, 0)) {
218  return 1;
219  }
220 
221  if (BPU_mecsInitCtx(&ctx, &params, BPU_EN_MECS_BASIC_GOPPA)) {
222 // if (BPU_mecsInitCtx(&ctx, 11, 50, BPU_EN_MECS_CCA2_POINTCHEVAL_GOPPA)) {
223  return 1;
224  }
225  /***************************************/
226  fprintf(stderr, "Key generation...\n");
227  // key pair generation
228  if (BPU_mecsGenKeyPair(ctx)) {
229  BPU_printError("Key generation error");
230 
231  return 1;
232  }
233  rc = BPU_asn1SaveKeyPair(ctx, "prikey.der", "pubkey.der");
234  if (rc) {
235  asn1_perror(rc);
236  }
237  rc = BPU_asn1LoadKeyPair(&ctx_2, "prikey.der", "pubkey.der");
238  if (rc) {
239  asn1_perror(rc);
240  }
241  testCmpMecsCtx(ctx, ctx_2);
242 
243  BPU_mecsFreeCtx(&ctx);
244  BPU_mecsFreeCtx(&ctx_2);
245  BPU_mecsFreeParamsGoppa(&params);
246  return rc;
247 }
248 #endif
249 int main(int argc, char **argv) {
250  int rc = 0;
251  // MUST BE NULL
252  BPU_T_Mecs_Ctx *ctx = NULL;
253  BPU_T_UN_Mecs_Params params;
254 
255  srand(time(NULL));
256 #if !defined(BPU_CONF_GOPPA_WO_H) && defined(BPU_CONF_ASN1)
257  testKeyGenAsn1();
258 #endif
259 
260 // /***************************************/
261 // // mce initialisation t = 50, m = 11
262  fprintf(stderr, "Basic GOPPA Initialisation...\n");
263  if (BPU_mecsInitParamsGoppa(&params, 11, 50, 0)) {
264  return 1;
265  }
266  if (BPU_mecsInitCtx(&ctx, &params, BPU_EN_MECS_BASIC_GOPPA)) {
267  return 1;
268  }
269  rc += testKeyGenEncDec(ctx);
270  BPU_mecsFreeCtx(&ctx);
271 
272 #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA
273  fprintf(stderr, "\nCCA2 Pointcheval GOPPA Initialisation...\n");
275  return 1;
276  }
277  rc += testKeyGenEncDec(ctx);
278  BPU_mecsFreeCtx(&ctx);
279  BPU_mecsFreeParamsGoppa(&params);
280 #endif
281 
282 // /***************************************/
283 // mce initialisation of 80-bit security
284  fprintf(stderr, "Basic QC-MDPC Initialisation...\n");
285  if (BPU_mecsInitParamsQcmdpc(&params, 4801, 2, 90, 84)) {
286  return 1;
287  }
288  if (BPU_mecsInitCtx(&ctx, &params, BPU_EN_MECS_BASIC_QCMDPC)) {
289  return 1;
290  }
291  rc += testKeyGenEncDec(ctx);
292  BPU_mecsFreeCtx(&ctx);
293  BPU_mecsFreeParamsQcmdpc(&params);
294 
295  #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA
296  fprintf(stderr, "\nCCA2 Pointcheval QC-MDPC Initialisation...\n");
297  if (BPU_mecsInitParamsQcmdpc(&params, 4801, 2, 90, 84)) {
298  return 1;
299  }
301  return 1;
302  }
303  rc += testKeyGenEncDec(ctx);
304  BPU_mecsFreeCtx(&ctx);
305  BPU_mecsFreeParamsQcmdpc(&params);
306  #endif
307 
308  return rc;
309 }
int BPU_gf2VecMalloc(BPU_T_GF2_Vector **v, int len)
Definition: gf2types.c:97
BPU_T_Code_Ctx * code_ctx
Definition: mecsctx.h:44
BPU_T_Perm_Element * elements
permutation vector
Definition: permtypes.h:32
basic qc-mdpc mecs, without any conversion
Definition: mecsctx.h:32
void BPU_gf2VecFree(BPU_T_GF2_Vector **v)
Free dynamically or statically allocated vector.
Definition: gf2types.c:45
BPU_T_GF2_Matrix * g_mat
Generator matrix.
Definition: goppatypes.h:28
BPU_T_GF2_16x ** elements
all element of matrix
Definition: gf2xtypes.h:45
uint16_t pt_len
PT len in bits.
Definition: mecsctx.h:45
uint16_t elements_in_row
number of elements in one row
Definition: gf2types.h:46
int BPU_gf2VecRand(BPU_T_GF2_Vector *out, int w)
Definition: gf2.c:240
BPU_T_Perm_Vector * permutation
permutation "matrix"
Definition: goppatypes.h:31
uint32_t k
rows
Definition: gf2types.h:47
int testKeyGenAsn1()
Definition: main.c:207
int testCmpMecsCtx(const BPU_T_Mecs_Ctx *ctx1, const BPU_T_Mecs_Ctx *ctx2)
Definition: main.c:28
int BPU_gf2VecCmp(const BPU_T_GF2_Vector *v1, const BPU_T_GF2_Vector *v2)
BPU_gf2VecCmp Compare two vectors.
Definition: gf2.c:467
BPU_T_GF2_16x_Matrix * h_mat
Control matrix H.
Definition: goppatypes.h:29
int(* _encrypt)(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Mecs_Ctx *ctx)
Definition: mecsctx.h:40
int BPU_mecsInitParamsGoppa(BPU_T_UN_Mecs_Params *params, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod)
Definition: mecsctx.c:192
uint16_t n
cols
Definition: gf2xtypes.h:47
uint16_t msg_len
Code dimenzion.
Definition: codectx.h:62
#define BPU_printError(fmt,...)
print error message with filename, line
Definition: debugio.h:47
uint16_t support_len
number of elements in support
Definition: goppatypes.h:32
int BPU_mecsDecrypt(BPU_T_GF2_Vector *pt, BPU_T_GF2_Vector *ct, const BPU_T_Mecs_Ctx *ctx)
Decrypt cipher text (ct) and save it to plain text.
Definition: mecs.c:45
int BPU_mecsInitCtx(BPU_T_Mecs_Ctx **ctx, const BPU_T_UN_Mecs_Params *params, const BPU_T_EN_Mecs_Types type)
Initialize (register) mecs functions for encryption, decryption and key gen based on type...
Definition: mecsctx.c:37
basic mecs, without any conversion
Definition: mecsctx.h:28
uint8_t element_bit_size
element size, is sizeof(BPU_T_GF2) i.e. 64 bits
Definition: gf2types.h:45
void BPU_mecsFreeParamsQcmdpc(BPU_T_UN_Mecs_Params *params)
Definition: mecsctx.c:204
BPU_T_Perm_Element size
permutation size
Definition: permtypes.h:33
BPU_T_GF2_Vector * e
Error vector.
Definition: codectx.h:58
int BPU_mecsFreeCtx(BPU_T_Mecs_Ctx **ctx)
Definition: mecsctx.c:158
int(* _decode)(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx)
This is reference to decode function. It will be set in BPU_codeInitCtx.
Definition: codectx.h:56
int BPU_asn1SaveKeyPair(const BPU_T_Mecs_Ctx *ctx, const char *pri_key_file, const char *pub_key_file)
Definition: asn1.c:561
int(* _decrypt)(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Mecs_Ctx *ctx)
Definition: mecsctx.h:41
uint16_t ct_len
CT len in bits.
Definition: mecsctx.h:46
void BPU_mecsFreeParamsGoppa(BPU_T_UN_Mecs_Params *params)
Definition: mecsctx.c:196
int(* _encode)(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const struct _BPU_T_Code_Ctx *ctx)
This is reference to encode function. It will be set in BPU_codeInitCtx.
Definition: codectx.h:55
int BPU_mecsInitParamsQcmdpc(BPU_T_UN_Mecs_Params *params, const uint16_t m, const uint16_t n0, const uint16_t w, const uint16_t t)
Definition: mecsctx.c:200
uint32_t n
cols
Definition: gf2types.h:48
BPU_T_EN_Code_Types type
Definition: codectx.h:54
uint16_t k
rows
Definition: gf2xtypes.h:46
int BPU_asn1LoadKeyPair(BPU_T_Mecs_Ctx **ctx, const char *pri_key_file, const char *pub_key_file)
Definition: asn1.c:519
int BPU_gf2xPolyCmp(const BPU_T_GF2_16x_Poly *p1, const BPU_T_GF2_16x_Poly *p2)
Definition: gf2x.c:645
adapted Pointcheval's cca2 conversion
Definition: mecsctx.h:30
int BPU_mecsGenKeyPair(BPU_T_Mecs_Ctx *ctx)
Key generation, first must be initialized context using BPU_mecsInitCtx().
Definition: mecs.c:55
int BPU_mecsEncrypt(BPU_T_GF2_Vector *ct, const BPU_T_GF2_Vector *pt, const BPU_T_Mecs_Ctx *ctx)
Encrypt plaintext (pt) and save it to cipher text.
Definition: mecs.c:33
uint32_t len
cols
Definition: gf2types.h:36
BPU_T_EN_Mecs_Types type
Definition: mecsctx.h:39
adapted Pointcheval's cca2 conversion for qcmdpc
Definition: mecsctx.h:34
int testKeyGenEncDec(BPU_T_Mecs_Ctx *ctx)
Definition: main.c:126
BPU_T_Goppa_Spec * goppa
Definition: codectx.h:42
uint8_t t
Error code correction capability.
Definition: codectx.h:63
uint16_t code_len
Code len.
Definition: codectx.h:61
int main(int argc, char **argv)
Definition: main.c:249
BPU_T_UN_Code_Spec * code_spec
Code specific structure, like generator matrix, control matrix, gen. poly ...
Definition: codectx.h:59
BPU_T_GF2_16x_Poly * g
gen. polynomial
Definition: goppatypes.h:30
BPU_T_GF2 ** elements
all element of matrix
Definition: gf2types.h:44