BitPunch McEliece  v0.0.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
asn1.c
Go to the documentation of this file.
1 /*
2  This file is part of BitPunch
3  Copyright (C) 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 "asn1.h"
19 
20 #ifdef BPU_CONF_ASN1
21 #include <bitpunch/debugio.h>
22 
23 #include <libtasn1.h>
24 #include <stdlib.h>
25 
26 int BPU_asn1EncodePriKey(char **buffer, int *size, const BPU_T_Mecs_Ctx *ctx) {
27  int rc = 0;
28  asn1_node definitions = NULL;
29  asn1_node asn1_element = NULL;
30  char error_desc[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
31  BPU_T_GF2_16x *h_mat = NULL;
32  uint32_t h_mat_bs;
33  int i, j;
34  *size = 0;
35  // TODO: structure cleanup on error
36  rc = asn1_parser2tree(BPU_STR_ASN1_MECS_PRI_KEY, &definitions, error_desc);
37 
38  if (rc != ASN1_SUCCESS) {
39  BPU_printError("ErrorDescription = %s\n\n", error_desc);
40  return rc;
41  }
42  rc = asn1_create_element(definitions, BPU_STR_ASN1_MECS_PRI_KEY_NAME, &asn1_element);
43  if (rc != ASN1_SUCCESS) {
44  BPU_printError("asn1_create_element(): %d", rc);
45  return rc;
46  }
47  // write elements
48  rc = -1;
49  if (ctx->type == BPU_EN_MECS_BASIC_GOPPA) {
50  rc = asn1_write_value(asn1_element, "oid", BPU_STR_ASN1_OID_MECS_BASIC_GOPPA, strlen(BPU_STR_ASN1_OID_MECS_BASIC_GOPPA));
51  }
52 #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA
54  rc = asn1_write_value(asn1_element, "oid", BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA, strlen(BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA));
55  }
56 #endif
57  if (rc != ASN1_SUCCESS) {
58  BPU_printError("asn1_write_value(): oid %d", rc);
59  return rc;
60  }
61  rc = asn1_write_value(asn1_element, "m", (void *)&(ctx->code_ctx->math_ctx->mod_deg), sizeof((ctx->code_ctx->math_ctx->mod_deg)));
62  if (rc != ASN1_SUCCESS) {
63  BPU_printError("asn1_write_value(): m %d", rc);
64  return rc;
65  }
66  rc = asn1_write_value(asn1_element, "t", (void *)&(ctx->code_ctx->t), sizeof((ctx->code_ctx->t)));
67  if (rc != ASN1_SUCCESS) {
68  BPU_printError("asn1_write_value(): t %d", rc);
69  return rc;
70  }
71  rc = asn1_write_value(asn1_element, "mod", (void *)&(ctx->code_ctx->math_ctx->mod), sizeof(ctx->code_ctx->math_ctx->mod));
72  if (rc != ASN1_SUCCESS) {
73  BPU_printError("asn1_write_value(): mod %d", rc);
74  return rc;
75  }
76  rc = asn1_write_value(asn1_element, "g", (void *)(ctx->code_ctx->code_spec->goppa->g->coef), sizeof(ctx->code_ctx->code_spec->goppa->g->coef[0]) * (ctx->code_ctx->code_spec->goppa->g->deg + 1));
77  if (rc != ASN1_SUCCESS) {
78  BPU_printError("asn1_write_value(): g %d", rc);
79  return rc;
80  }
81  rc = asn1_write_value(asn1_element, "p", (void *)(ctx->code_ctx->code_spec->goppa->permutation->elements), sizeof(ctx->code_ctx->code_spec->goppa->permutation->elements[0]) * ctx->code_ctx->code_spec->goppa->permutation->size);
82  if (rc != ASN1_SUCCESS) {
83  BPU_printError("asn1_write_value(): p %d", rc);
84  return rc;
85  }
86  if (ctx->code_ctx->type == BPU_EN_CODE_GOPPA) {
87  if (!ctx->code_ctx->code_spec->goppa->h_mat) {
88  BPU_printError("not supported w/o H");
89  return -1;
90  }
91  // prepare H matrix
92  h_mat_bs = sizeof(BPU_T_GF2_16x) * (ctx->code_ctx->code_spec->goppa->h_mat->k * ctx->code_ctx->code_spec->goppa->h_mat->n);
93  h_mat = (BPU_T_GF2_16x *) malloc(h_mat_bs);
94 
95  if (!h_mat) {
96  BPU_printError("Can not allocate memory for H matrix export");
97  return -1;
98  }
99  j = 0;
100  for (i = 0; i < ctx->code_ctx->code_spec->goppa->h_mat->k; i++) {
101  memcpy(h_mat + j, ctx->code_ctx->code_spec->goppa->h_mat->elements[i], ctx->code_ctx->code_spec->goppa->h_mat->n * sizeof(BPU_T_GF2_16x));
102  j += ctx->code_ctx->code_spec->goppa->h_mat->n;
103  }
104  }
105  else {
106  BPU_printError("Not supported for this CODE type.");
107  return -1;
108  }
109  rc = asn1_write_value(asn1_element, "h_mat", (void *)(h_mat), h_mat_bs);
110  free(h_mat);
111 
112  if (rc != ASN1_SUCCESS) {
113  BPU_printError("asn1_write_value(): h_mat %d", rc);
114 
115  return rc;
116  }
117  /* Clear the definition structures */
118  asn1_delete_structure(&definitions);
119 
120  // get size of data
121  asn1_der_coding(asn1_element, "", NULL, size, NULL);
122  *buffer = (char *) malloc(*size);
123 
124  rc = asn1_der_coding(asn1_element, "", *buffer, size, NULL);
125  if (rc != ASN1_SUCCESS) {
126  BPU_printError("asn1_der_coding(): %d", rc);
127  return rc;
128  }
129  /* Clear the definition structures */
130  asn1_delete_structure(&asn1_element);
131  return rc;
132 }
133 
134 int BPU_asn1DecodePriKey(BPU_T_Mecs_Ctx **ctx, const char *buffer, const int size) {
135  int rc = 0;
136  asn1_node definitions = NULL;
137  asn1_node asn1_element = NULL;
138  int tmp_len;
139  char error_desc[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
140  char *tmp_buf;
141  BPU_T_EN_Mecs_Types type;
142  BPU_T_UN_Mecs_Params params;
143  uint8_t m, t;
144  int i, j;
145 
146  // TODO:structure cleanup on error
147  rc = asn1_parser2tree(BPU_STR_ASN1_MECS_PRI_KEY, &definitions, error_desc);
148 
149  if (rc != ASN1_SUCCESS) {
150  BPU_printError("ErrorDescription = %s", error_desc);
151  return rc;
152  }
153  rc = asn1_create_element(definitions, BPU_STR_ASN1_MECS_PRI_KEY_NAME, &asn1_element);
154  if (rc != ASN1_SUCCESS) {
155  BPU_printError("asn1_create_element(): %d", rc);
156  return rc;
157  }
158  /* Clear the definition structures */
159  asn1_delete_structure(&definitions);
160 
161  // decode buffer
162  rc = asn1_der_decoding(&asn1_element, buffer, size, error_desc);
163  if (rc != ASN1_SUCCESS) {
164  BPU_printError("Decoding error %d %s", rc, error_desc);
165  return rc;
166  }
167  // section of data decoding
168  type = BPU_asn1GetMecsTypeFromOid(asn1_element);
169  if (type == -1) {
170  BPU_printError("Wrong OID");
171  return -1;
172  }
173  if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, 1, "m", asn1_element)) {
174  return -1;
175  }
176  m = *(uint8_t *) tmp_buf;
177  free(tmp_buf);
178 
179  if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, 1, "t", asn1_element)) {
180  return -1;
181  }
182  t = *(uint8_t *) tmp_buf;
183  free(tmp_buf);
184 
185  if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, sizeof(BPU_T_GF2_16x), "mod", asn1_element)) {
186  return -1;
187  }
189  if (BPU_mecsInitParamsGoppa(&params, m, t, *((BPU_T_GF2_16x *) tmp_buf))) {
190  return -1;
191  }
192  free(tmp_buf);
193  if (BPU_mecsInitCtx(ctx, &params, type)) {
194  return -1;
195  }
196  BPU_mecsFreeParamsGoppa(&params);
197  if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, sizeof(BPU_T_GF2_16x) * (t + 1), "g", asn1_element)) {
198  return -1;
199  }
200  if (BPU_gf2xPolyMalloc(&((*ctx)->code_ctx->code_spec->goppa->g), t)) {
201  return -1;
202  }
203  memcpy((*ctx)->code_ctx->code_spec->goppa->g->coef, tmp_buf, tmp_len);
204  (*ctx)->code_ctx->code_spec->goppa->g->deg = t;
205  free(tmp_buf);
206  }
207  else {
208  BPU_printError("Not supported for this code type.");
209  return -1;
210  }
211  if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, sizeof(BPU_T_Perm_Element) * (*ctx)->code_ctx->code_len, "p", asn1_element)) {
212  return -1;
213  }
214  if (BPU_permMalloc(&((*ctx)->code_ctx->code_spec->goppa->permutation), (*ctx)->code_ctx->code_len)) {
215  return -1;
216  }
217  memcpy((*ctx)->code_ctx->code_spec->goppa->permutation->elements, tmp_buf, tmp_len);
218  free(tmp_buf);
219 
220  if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, sizeof(BPU_T_GF2_16x) * (*ctx)->code_ctx->t * (*ctx)->code_ctx->code_len, "h_mat", asn1_element)) {
221  return -1;
222  }
223  if (BPU_gf2xMatMalloc(&((*ctx)->code_ctx->code_spec->goppa->h_mat), (*ctx)->code_ctx->t, (*ctx)->code_ctx->code_len)) {
224  return -1;
225  }
226  j = 0;
227  for (i = 0; i < (*ctx)->code_ctx->code_spec->goppa->h_mat->k; i++) {
228  memcpy((*ctx)->code_ctx->code_spec->goppa->h_mat->elements[i], tmp_buf + j, (*ctx)->code_ctx->code_spec->goppa->h_mat->n * sizeof(BPU_T_GF2_16x));
229  j += (*ctx)->code_ctx->code_spec->goppa->h_mat->n * sizeof(BPU_T_GF2_16x);
230  }
231  free(tmp_buf);
232 
233  asn1_delete_structure(&asn1_element);
234  return rc;
235 }
236 
237 int BPU_asn1EncodePubKey(char **buffer, int *size, const BPU_T_Mecs_Ctx *ctx) {
238  int rc = 0;
239  asn1_node definitions = NULL;
240  asn1_node asn1_element = NULL;
241  char error_desc[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
242  BPU_T_GF2 *g_mat = NULL;
243  uint32_t g_mat_bs;
244  int i, j;
245  *size = 0;
246  // TODO:structure cleanup on error
247  rc = asn1_parser2tree(BPU_STR_ASN1_MECS_PUB_KEY, &definitions, error_desc);
248 
249  if (rc != ASN1_SUCCESS) {
250  BPU_printError("ErrorDescription = %s", error_desc);
251  return rc;
252  }
253  rc = asn1_create_element(definitions, BPU_STR_ASN1_MECS_PUB_KEY_NAME, &asn1_element);
254  if (rc != ASN1_SUCCESS) {
255  BPU_printError("asn1_create_element(): %d", rc);
256  return rc;
257  }
258  // write elements
259  rc = -1;
260  if (ctx->type == BPU_EN_MECS_BASIC_GOPPA) {
261  rc = asn1_write_value(asn1_element, "oid", BPU_STR_ASN1_OID_MECS_BASIC_GOPPA, strlen(BPU_STR_ASN1_OID_MECS_BASIC_GOPPA));
262  }
263 #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA
265  rc = asn1_write_value(asn1_element, "oid", BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA, strlen(BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA));
266  }
267 #endif
268  if (rc != ASN1_SUCCESS) {
269  BPU_printError("asn1_write_value(): oid %d", rc);
270  return rc;
271  }
272  rc = asn1_write_value(asn1_element, "m", (void *)&(ctx->code_ctx->math_ctx->mod_deg), sizeof((ctx->code_ctx->math_ctx->mod_deg)));
273  if (rc != ASN1_SUCCESS) {
274  BPU_printError("asn1_write_value(): m %d", rc);
275  return rc;
276  }
277  rc = asn1_write_value(asn1_element, "t", (void *)&(ctx->code_ctx->t), sizeof((ctx->code_ctx->t)));
278  if (rc != ASN1_SUCCESS) {
279  BPU_printError("asn1_write_value(): t %d", rc);
280  return rc;
281  }
282  if (ctx->code_ctx->type == BPU_EN_CODE_GOPPA) {
283  g_mat_bs = sizeof(BPU_T_GF2) * (ctx->code_ctx->code_spec->goppa->g_mat->k * ctx->code_ctx->code_spec->goppa->g_mat->elements_in_row);
284 
285  // prepare G matrix
286  g_mat = (BPU_T_GF2 *) malloc(g_mat_bs);
287 
288  if (!g_mat) {
289  BPU_printError("Can not allocate memory for G matrix export");
290  return -1;
291  }
292  j = 0;
293  for (i = 0; i < ctx->code_ctx->code_spec->goppa->g_mat->k; i++) {
294  memcpy(g_mat + j, ctx->code_ctx->code_spec->goppa->g_mat->elements[i], ctx->code_ctx->code_spec->goppa->g_mat->elements_in_row * sizeof(BPU_T_GF2));
296  }
297  }
298  else {
299  BPU_printError("Not supported for this MECS type.");
300  return -1;
301  }
302  rc = asn1_write_value(asn1_element, "g_mat", (void *)(g_mat), g_mat_bs);
303  free(g_mat);
304 
305  if (rc != ASN1_SUCCESS) {
306  BPU_printError("asn1_write_value(): g_mat %d", rc);
307  return rc;
308  }
309  /* Clear the definition structures */
310  asn1_delete_structure(&definitions);
311 
312  // get size of data
313  asn1_der_coding(asn1_element, "", NULL, size, NULL);
314  *buffer = (char *) malloc(*size);
315 
316  rc = asn1_der_coding(asn1_element, "", *buffer, size, NULL);
317  if (rc != ASN1_SUCCESS) {
318  BPU_printError("asn1_der_coding(): %d", rc);
319  }
320  /* Clear the definition structures */
321  asn1_delete_structure(&asn1_element);
322  return rc;
323 }
324 
325 int BPU_asn1DecodePubKey(BPU_T_Mecs_Ctx **ctx, const char *buffer, const int size) {
326  int rc = 0;
327  asn1_node definitions = NULL;
328  asn1_node asn1_element = NULL;
329  int tmp_len;
330  char error_desc[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
331  char *tmp_buf = NULL;
332  BPU_T_EN_Mecs_Types type;
333  BPU_T_UN_Mecs_Params params;
334  uint8_t m, t;
335  int i, j, tmp;
336 
337  // TODO:structure cleanup on error
338  rc = asn1_parser2tree(BPU_STR_ASN1_MECS_PUB_KEY, &definitions, error_desc);
339 
340  if (rc != ASN1_SUCCESS) {
341  BPU_printError("ErrorDescription = %s", error_desc);
342  return rc;
343  }
344  rc = asn1_create_element(definitions, BPU_STR_ASN1_MECS_PUB_KEY_NAME, &asn1_element);
345  if (rc != ASN1_SUCCESS) {
346  BPU_printError("asn1_create_element(): %d", rc);
347  return rc;
348  }
349  /* Clear the definition structures */
350  asn1_delete_structure(&definitions);
351 
352  // decode buffer
353  rc = asn1_der_decoding(&asn1_element, buffer, size, error_desc);
354  if (rc != ASN1_SUCCESS) {
355  BPU_printError("Decoding error %d, %s", rc, error_desc);
356  return rc;
357  }
358  // section of data decoding
359  type = BPU_asn1GetMecsTypeFromOid(asn1_element);
360  if (type == -1) {
361  BPU_printError("Wrong OID");
362  return -1;
363  }
364  if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, 1, "m", asn1_element)) {
365  return -1;
366  }
367  m = *(uint8_t *) tmp_buf;
368  free(tmp_buf);
369 
370  if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, 1, "t", asn1_element)) {
371  return -1;
372  }
373  t = *(uint8_t *) tmp_buf;
374  free(tmp_buf);
375 
376  // init mecs context without mod
378  if (BPU_mecsInitParamsGoppa(&params, m, t, -1)) {
379  return -1;
380  }
381  }
382  else {
383  BPU_printError("Type not supported");
384  return -1;
385  }
386  if ((rc = BPU_mecsInitCtx(ctx, &params, type))) {
387  BPU_printError("can not initialize mecs ctx");
388 
389  return rc;
390  }
391  BPU_mecsFreeParamsGoppa(&params);
392 
393  if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, (*ctx)->code_ctx->code_len * (*ctx)->code_ctx->msg_len, "g_mat", asn1_element)) {
394  return -1;
395  }
396  if (BPU_gf2MatMalloc(&((*ctx)->code_ctx->code_spec->goppa->g_mat), (*ctx)->code_ctx->code_len - (*ctx)->code_ctx->msg_len, (*ctx)->code_ctx->msg_len)) {
397  return -1;
398  }
399  j = 0;
400  tmp = (*ctx)->code_ctx->code_spec->goppa->g_mat->elements_in_row * ((*ctx)->code_ctx->code_spec->goppa->g_mat->element_bit_size / 8);
401  for (i = 0; i < (*ctx)->code_ctx->code_spec->goppa->g_mat->k; i++) {
402  memcpy((*ctx)->code_ctx->code_spec->goppa->g_mat->elements[i], tmp_buf + j, tmp);
403  j += tmp;
404  }
405  free(tmp_buf);
406 
407  asn1_delete_structure(&asn1_element);
408  return rc;
409 }
410 
411 int BPU_asn1ReadValue(char **buffer, int *len, const int max_len, const char *name, const asn1_node node) {
412  int rc;
413 
414  // get size and malloc buffer
415  if ((rc = BPU_asn1MallocBuffer(buffer, len, max_len, node, name))) {
416  return rc;
417  }
418  // decode data
419  rc = asn1_read_value(node, name, *buffer, len);
420  BPU_printDebug("data len: %d", *len);
421  if (rc != ASN1_SUCCESS) {
422  BPU_printError("asn1_read_value(): %s %d", name, rc);
423  return -1;
424  }
425  BPU_printDebug("data %s: %02x", name, **buffer);
426  return rc;
427 }
428 
430  int rc, tmp_len;
431  char *tmp_buf = NULL;
432 
433  if (BPU_asn1ReadValue(&tmp_buf, &tmp_len, 60, "oid", node)) {
434  return -1;
435  }
436  tmp_buf[tmp_len - 1] = '\0';
437  BPU_printDebug("data oid: %s", tmp_buf);
438 
439  if (!strcmp(tmp_buf, BPU_STR_ASN1_OID_MECS_BASIC_GOPPA)) {
441  }
442 #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA
443  else if (!strcmp(tmp_buf, BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA)) {
445  }
446 #endif
447  else {
448  rc = -1;
449  }
450  free(tmp_buf);
451 
452  return rc;
453 }
454 
455 int BPU_asn1MallocBuffer(char **buffer, int *len, const int max_len, const asn1_node node, const char *name) {
456  int rc;
457 
458  rc = asn1_read_value(node, name, NULL, len);
459 
460  if (*len > max_len) {
461  BPU_printError("requested length %d is bigger than max expected %d", *len, max_len);
462  return -1;
463  }
464  else if (*len > 0) {
465  // malloc data
466  *buffer = (char *) malloc(*len);
467 
468  if (!(*buffer)) {
469  BPU_printError("can not allocate memory.");
470  return -1;
471  }
472  }
473  else {
474  BPU_printError("can not get size of data");
475  return rc;
476  }
477  return 0;
478 }
479 
480 int BPU_asn1LoadFileToBuffer(char **buffer, long *size, const char *file_name) {
481  FILE *fp = fopen(file_name, "rb");
482  long tmp;
483 
484  if (!fp) {
485  BPU_printError("can not open private key file: %s", file_name);
486  return -1;
487  }
488  fseek(fp, 0L, SEEK_END);
489  *size = ftell(fp);
490  fseek(fp, 0L, SEEK_SET);
491 
492  if (*size == -1L) {
493  BPU_printError("can get file size");
494  fclose(fp);
495 
496  return -1;
497  }
498  *buffer = (char *) malloc(*size);
499 
500  if (!*buffer) {
501  BPU_printError("can malloc buffer");
502  fclose(fp);
503 
504  return -1;
505  }
506  tmp = fread(*buffer, 1, *size, fp);
507 
508  if (tmp != *size) {
509  BPU_printError("can not read data from file, read only %ld B, instead of %ld B", tmp, *size);
510  free(*buffer);
511  fclose(fp);
512 
513  return -1;
514  }
515  fclose(fp);
516  return 0;
517 }
518 
519 int BPU_asn1LoadKeyPair(BPU_T_Mecs_Ctx **ctx, const char *pri_key_file, const char *pub_key_file) {
520  if (BPU_asn1LoadPriKey(ctx, pri_key_file) || BPU_asn1LoadPubKey(ctx, pub_key_file)) {
521  BPU_printError("can not load key pair");
522  return -1;
523  }
524  return 0;
525 }
526 
527 int BPU_asn1LoadPriKey(BPU_T_Mecs_Ctx **ctx, const char *pri_key_file) {
528  long size;
529  char *buffer;
530 
531  if (BPU_asn1LoadFileToBuffer(&buffer, &size, pri_key_file)) {
532  return -1;
533  }
534  if (BPU_asn1DecodePriKey(ctx, buffer, size)) {
535  free(buffer);
536 
537  return -1;
538  }
539  free(buffer);
540 
541  return 0;
542 }
543 
544 int BPU_asn1LoadPubKey(BPU_T_Mecs_Ctx **ctx, const char *pub_key_file) {
545  long size;
546  char *buffer;
547 
548  if (BPU_asn1LoadFileToBuffer(&buffer, &size, pub_key_file)) {
549  return -1;
550  }
551  if (BPU_asn1DecodePubKey(ctx, buffer, size)) {
552  free(buffer);
553 
554  return -1;
555  }
556  free(buffer);
557 
558  return 0;
559 }
560 
561 int BPU_asn1SaveKeyPair(const BPU_T_Mecs_Ctx *ctx, const char *pri_key_file, const char *pub_key_file) {
562  if (BPU_asn1SavePriKey(ctx, pri_key_file) || BPU_asn1SavePubKey(ctx, pub_key_file)) {
563  BPU_printError("can not save key pair");
564  return -1;
565  }
566  return 0;
567 }
568 
569 int BPU_asn1SavePriKey(const BPU_T_Mecs_Ctx *ctx, const char *pri_key_file) {
570  char *buffer = NULL;
571  int size;
572 
573  if (BPU_asn1EncodePriKey(&buffer, &size, ctx)) {
574  return -1;
575  }
576  if (BPU_asn1WriteBufferToFile(buffer, size, pri_key_file)) {
577  BPU_printError("can not write pri key data");
578  free(buffer);
579 
580  return -1;
581  }
582  free(buffer);
583 
584  return 0;
585 }
586 
587 int BPU_asn1SavePubKey(const BPU_T_Mecs_Ctx *ctx, const char *pub_key_file) {
588  char *buffer = NULL;
589  int size;
590 
591  if (BPU_asn1EncodePubKey(&buffer, &size, ctx)) {
592  return -1;
593  }
594  if (BPU_asn1WriteBufferToFile(buffer, size, pub_key_file)) {
595  BPU_printError("can not write pub key data");
596  free(buffer);
597 
598  return -1;
599  }
600  free(buffer);
601 
602  return 0;
603 }
604 
605 int BPU_asn1WriteBufferToFile(const char *buffer, const long size, const char *file_name) {
606  FILE *fp;
607  long tmp;
608 
609  fp = fopen(file_name, "wb");
610 
611  if (!fp) {
612  BPU_printError("can not open file %s", file_name);
613 
614  return -1;
615  }
616  tmp = fwrite(buffer, 1, size, fp);
617 
618  if (tmp != size) {
619  BPU_printError("can not write buffer to file, written %ld B, instead of %ld B", tmp, size);
620  fclose(fp);
621 
622  return -1;
623  }
624  fclose(fp);
625 
626  return 0;
627 }
628 #endif // BPU_CONF_ASN1
BPU_T_Code_Ctx * code_ctx
Definition: mecsctx.h:44
int BPU_asn1EncodePubKey(char **buffer, int *size, const BPU_T_Mecs_Ctx *ctx)
Definition: asn1.c:237
#define BPU_STR_ASN1_OID_MECS_BASIC_GOPPA
src: http://www.hrz.tu-darmstadt.de/itsicherheit/object_identifier/oids_der_informatik__cdc/oids_des_...
Definition: asn1.h:35
BPU_T_GF2_16x mod
polynomial modulus
Definition: mathctx.h:35
BPU_T_Math_Ctx * math_ctx
Math context.
Definition: codectx.h:57
BPU_T_Perm_Element * elements
permutation vector
Definition: permtypes.h:32
int BPU_asn1WriteBufferToFile(const char *buffer, const long size, const char *file_name)
Definition: asn1.c:605
int BPU_asn1MallocBuffer(char **buffer, int *len, const int max_len, const asn1_node node, const char *name)
Definition: asn1.c:455
int BPU_asn1LoadPubKey(BPU_T_Mecs_Ctx **ctx, const char *pub_key_file)
Definition: asn1.c:544
int BPU_gf2xMatMalloc(BPU_T_GF2_16x_Matrix **m, int rows, int cols)
Allocate memory for matrix.
Definition: gf2xtypes.c:28
#define BPU_STR_ASN1_OID_MECS_CCA2_POINTCHEVAL_GOPPA
src: http://www.hrz.tu-darmstadt.de/itsicherheit/object_identifier/oids_der_informatik__cdc/oids_des_...
Definition: asn1.h:37
BPU_T_GF2_Matrix * g_mat
Generator matrix.
Definition: goppatypes.h:28
BPU_T_EN_Mecs_Types BPU_asn1GetMecsTypeFromOid(asn1_node node)
Definition: asn1.c:429
BPU_T_GF2_16x ** elements
all element of matrix
Definition: gf2xtypes.h:45
int BPU_gf2xPolyMalloc(BPU_T_GF2_16x_Poly **p, int16_t max_deg)
Definition: gf2xtypes.c:111
#define BPU_STR_ASN1_MECS_PRI_KEY
Definition: asn1.h:29
int BPU_asn1LoadPriKey(BPU_T_Mecs_Ctx **ctx, const char *pri_key_file)
Definition: asn1.c:527
uint16_t elements_in_row
number of elements in one row
Definition: gf2types.h:46
uint16_t BPU_T_GF2_16x
Definition: gf2xtypes.h:26
BPU_T_Perm_Vector * permutation
permutation "matrix"
Definition: goppatypes.h:31
uint32_t k
rows
Definition: gf2types.h:47
#define BPU_STR_ASN1_MECS_PUB_KEY_NAME
Definition: asn1.h:32
BPU_T_GF2_16x_Matrix * h_mat
Control matrix H.
Definition: goppatypes.h:29
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
int BPU_asn1DecodePubKey(BPU_T_Mecs_Ctx **ctx, const char *buffer, const int size)
Definition: asn1.c:325
#define BPU_printError(fmt,...)
print error message with filename, line
Definition: debugio.h:47
uint32_t BPU_T_GF2
Definition: gf2types.h:26
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
#define BPU_printDebug(fmt,...)
print debug message with filename, line
Definition: debugio.h:63
enum _BPU_T_EN_Mecs_Types BPU_T_EN_Mecs_Types
Possible types of MECS.
BPU_T_Perm_Element size
permutation size
Definition: permtypes.h:33
int BPU_asn1ReadValue(char **buffer, int *len, const int max_len, const char *name, const asn1_node node)
Definition: asn1.c:411
int BPU_asn1SaveKeyPair(const BPU_T_Mecs_Ctx *ctx, const char *pri_key_file, const char *pub_key_file)
Definition: asn1.c:561
int BPU_gf2MatMalloc(BPU_T_GF2_Matrix **m, int rows, int cols)
Definition: gf2types.c:54
void BPU_mecsFreeParamsGoppa(BPU_T_UN_Mecs_Params *params)
Definition: mecsctx.c:196
BPU_T_GF2_16x * coef
Polynomial over GF2m.
Definition: gf2xtypes.h:54
int BPU_asn1EncodePriKey(char **buffer, int *size, const BPU_T_Mecs_Ctx *ctx)
BPU_asn1EncodePriKey dsadsad.
Definition: asn1.c:26
BPU_T_EN_Code_Types type
Definition: codectx.h:54
int BPU_asn1DecodePriKey(BPU_T_Mecs_Ctx **ctx, const char *buffer, const int size)
Definition: asn1.c:134
#define BPU_STR_ASN1_MECS_PUB_KEY
Definition: asn1.h:30
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
int16_t deg
degree
Definition: gf2xtypes.h:55
adapted Pointcheval's cca2 conversion
Definition: mecsctx.h:30
int BPU_asn1LoadFileToBuffer(char **buffer, long *size, const char *file_name)
Definition: asn1.c:480
#define BPU_STR_ASN1_MECS_PRI_KEY_NAME
Definition: asn1.h:31
BPU_T_EN_Mecs_Types type
Definition: mecsctx.h:39
int BPU_asn1SavePriKey(const BPU_T_Mecs_Ctx *ctx, const char *pri_key_file)
Definition: asn1.c:569
int BPU_asn1SavePubKey(const BPU_T_Mecs_Ctx *ctx, const char *pub_key_file)
Definition: asn1.c:587
uint16_t BPU_T_Perm_Element
Definition of permutation element type.
Definition: permtypes.h:24
BPU_T_Goppa_Spec * goppa
Definition: codectx.h:42
uint8_t t
Error code correction capability.
Definition: codectx.h:63
int BPU_permMalloc(BPU_T_Perm_Vector **p, int size)
Allocate permutation vector elements of size size.
Definition: permtypes.c:35
uint8_t mod_deg
modulo degree, galois finite field GF(2^m)
Definition: mathctx.h:36
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