BitPunch McEliece  v0.0.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mecsbasic.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 "mecsbasic.h"
19 
20 #include <bitpunch/math/gf2.h>
21 #include <bitpunch/debugio.h>
22 
23 #ifdef BPU_CONF_ENCRYPTION
25  int rc;
26  BPU_T_GF2_Vector *e = ctx->code_ctx->e;
27  BPU_gf2VecNull(out);
28 
29  // test the size of message and g_m
30  if (in->len != ctx->code_ctx->msg_len) {
31  BPU_printError("message length has to be of length %d", ctx->code_ctx->msg_len);
32 
33  return -1;
34  }
35 
36  rc = ctx->code_ctx->_encode(out, in, ctx->code_ctx);
37  if (rc) {
38  BPU_printError("can not encode");
39  return rc;
40  }
41 
42  // generate random error vector e
43  rc += BPU_gf2VecRand(e, ctx->code_ctx->t);
44  if (rc) {
45  BPU_printError("can not init rand vector");
46  return rc;
47  }
48 
49  // z' XOR e
50  rc = BPU_gf2VecXor(out, e);
51  if (rc) {
52  BPU_printError("can not add error vector");
53  return rc;
54  }
55  return rc;
56 }
57 #endif
58 
59 #ifdef BPU_CONF_DECRYPTION
61  int rc = 0;
62  BPU_T_GF2_Vector *temp;
63 
64  BPU_gf2VecMalloc(&temp, in->len);
65  BPU_gf2VecCopy(temp, in);
66 
67  rc = ctx->code_ctx->_decode(out, temp, ctx->code_ctx);
68 
69  BPU_gf2VecFree(&temp);
70 
71  return rc;
72 }
73 #endif
int BPU_gf2VecMalloc(BPU_T_GF2_Vector **v, int len)
Definition: gf2types.c:97
BPU_T_Code_Ctx * code_ctx
Definition: mecsctx.h:44
#define BPU_gf2VecNull(v_pointer)
Null GF2 vector.
Definition: gf2types.h:112
int BPU_gf2VecXor(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in)
Xor two Vectors GF2 and store result in first vector.
Definition: gf2.c:481
void BPU_gf2VecFree(BPU_T_GF2_Vector **v)
Free dynamically or statically allocated vector.
Definition: gf2types.c:45
int BPU_gf2VecRand(BPU_T_GF2_Vector *out, int w)
Definition: gf2.c:240
int BPU_mecsBasicDecrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx)
Definition: mecsbasic.c:60
uint16_t msg_len
Code dimenzion.
Definition: codectx.h:62
#define BPU_printError(fmt,...)
print error message with filename, line
Definition: debugio.h:47
BPU_T_GF2_Vector * e
Error vector.
Definition: codectx.h:58
void BPU_gf2VecCopy(BPU_T_GF2_Vector *dest, const BPU_T_GF2_Vector *src)
Copy VectorGF2.
Definition: gf2.c:454
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(* _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_mecsBasicEncrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx)
Definition: mecsbasic.c:24
uint32_t len
cols
Definition: gf2types.h:36
uint8_t t
Error code correction capability.
Definition: codectx.h:63