BitPunch McEliece  v0.0.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mecsctx.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 Copyright (C) 2015 Andrej Gulyas <andrej.guly[what here]gmail.com>
5 
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "mecsctx.h"
20 
21 #include <stdlib.h>
22 
23 #include <bitpunch/debugio.h>
24 #include <bitpunch/errorcodes.h>
25 #include <bitpunch/code/codectx.h>
26 #include <bitpunch/math/mathctx.h>
27 
28 // additional encryption schemes
32 
33 #if defined(BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA) || defined(BPU_CONF_MECS_CCA2_POINTCHEVAL_QCMDPC)
35 #endif
36 
38  int rc = 0;
39  BPU_T_Mecs_Ctx *ctx_p;
40 
41  if (!*ctx) {
42  *ctx = (BPU_T_Mecs_Ctx *) calloc(sizeof(BPU_T_Mecs_Ctx), 1);
43 
44  if (!*ctx) {
45  BPU_printError("can not alloc Mecs ctx");
46  return -1;
47  }
48  }
49  else {
50  BPU_printDebug("Already initialized");
51  return 0;
52  }
53  ctx_p = *ctx;
54  ctx_p->type = type;
55 
56  switch (type) {
58 #ifdef BPU_CONF_ENCRYPTION
60 #endif
61 #ifdef BPU_CONF_DECRYPTION
63 #endif
64 #ifdef BPU_CONF_KEY_GEN
66 #endif
68  if (rc) {
69  return rc;
70  }
71  ctx_p->pt_len = ctx_p->code_ctx->msg_len;
72  ctx_p->ct_len = ctx_p->code_ctx->code_len;
73  break;
74 
75 #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA
77 #ifdef BPU_CONF_ENCRYPTION
79 #endif
80 #ifdef BPU_CONF_DECRYPTION
82 #endif
83 #ifdef BPU_CONF_KEY_GEN
85 #endif
87  if (rc) {
88  return rc;
89  }
90  ctx_p->pt_len = BPU_HASH_LEN * 8 < ctx_p->code_ctx->msg_len ? BPU_HASH_LEN * 8 : ctx_p->code_ctx->msg_len;
91  ctx_p->ct_len = ctx_p->code_ctx->code_len + 2 * ctx_p->ct_len;
92  break;
93 #endif
94 
96 #ifdef BPU_CONF_ENCRYPTION
98 #endif
99 #ifdef BPU_CONF_DECRYPTION
101 #endif
102 #ifdef BPU_CONF_KEY_GEN
104 #endif
106  if (rc) {
107  return rc;
108  }
109  ctx_p->pt_len = ctx_p->code_ctx->msg_len;
110  ctx_p->ct_len = ctx_p->code_ctx->code_len;
111  break;
112 
113 #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_QCMDPC
115 #ifdef BPU_CONF_ENCRYPTION
117 #endif
118 #ifdef BPU_CONF_DECRYPTION
120 #endif
121 #ifdef BPU_CONF_KEY_GEN
123 #endif
125  if (rc) {
126  return rc;
127  }
128  ctx_p->pt_len = BPU_HASH_LEN * 8 < ctx_p->code_ctx->msg_len ? BPU_HASH_LEN * 8 : ctx_p->code_ctx->msg_len;
129  ctx_p->ct_len = ctx_p->code_ctx->code_len + 2 * ctx_p->ct_len;
130  break;
131 #endif
132  /* EXAMPLE please DO NOT REMOVE
133  case BPU_EN_MECS_*****:
134 #ifdef BPU_CONF_ENCRYPTION
135  ctx_p->_encrypt = FUNC_FROM_YOUR_FILE;
136 #endif
137 #ifdef BPU_CONF_DECRYPTION
138  ctx_p->_decrypt = FUNC_FROM_YOUR_FILE;
139 #endif
140 #ifdef BPU_CONF_KEY_GEN
141  ctx_p->_genKeyPair = FUNC_FROM_YOUR_FILE;
142 #endif
143  rc = BPU_codeInitCtx(&ctx_p->code_ctx, m, t, BPU_EN_CODE_GOPPA, mod);
144  if (rc) {
145  return rc;
146  }
147  ctx->pt_len = PT_LEN;
148  ctx->ct_len = CT_LEN;
149  break;
150  */
151  default:
152  BPU_printError("MECS type not supported: %d", type);
154  }
155  return rc;
156 }
157 
159  BPU_T_Mecs_Ctx *ctx_p = *ctx;
160 
161  if (!ctx_p) {
162  return 0;
163  }
164  switch (ctx_p->type) {
166 #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA
168 #endif
169  ctx_p->_encrypt = NULL;
170  ctx_p->_decrypt = NULL;
171  ctx_p->_genKeyPair = NULL;
172  break;
174 #ifdef BPU_CONF_MECS_CCA2_POINTCHEVAL_QCMDPC
176 #endif
177  ctx_p->_encrypt = NULL;
178  ctx_p->_decrypt = NULL;
179  ctx_p->_genKeyPair = NULL;
180  break;
181  default:
182  BPU_printError("MECS type not supported: %d", ctx_p->type);
184  }
185  BPU_codeFreeCtx(&ctx_p->code_ctx);
186  free(ctx_p);
187  *ctx = NULL;
188 
189  return 0;
190 }
191 
192 int BPU_mecsInitParamsGoppa(BPU_T_UN_Mecs_Params *params, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod) {
193  return BPU_codeInitParamsGoppa((BPU_T_UN_Code_Params *)params, m, t, mod);
194 }
195 
198 }
199 
200 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) {
201  return BPU_codeInitParamsQcmdpc((BPU_T_UN_Code_Params *)params, m, n0, w, t);
202 }
203 
206 }
BPU_T_Code_Ctx * code_ctx
Definition: mecsctx.h:44
basic qc-mdpc mecs, without any conversion
Definition: mecsctx.h:32
uint16_t pt_len
PT len in bits.
Definition: mecsctx.h:45
void BPU_codeFreeParamsGoppa(BPU_T_UN_Code_Params *params)
BPU_codeFreeParamsGoppa.
Definition: codectx.c:191
uint16_t BPU_T_GF2_16x
Definition: gf2xtypes.h:26
int BPU_mecsPointchevalCCA2Encrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx)
BPU_mecsPointchevalCCA2Encrypt.
void BPU_codeFreeParamsQcmdpc(BPU_T_UN_Code_Params *params)
Definition: codectx.c:199
int BPU_mecsBasicDecrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx)
Definition: mecsbasic.c:60
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
int BPU_codeInitCtx(BPU_T_Code_Ctx **ctx, const BPU_T_UN_Code_Params *params, const BPU_T_EN_Code_Types type)
BPU_codeInitCtx Initialize (register) code functions encode, decode and code spec structure based on ...
Definition: codectx.c:31
int(* _genKeyPair)(struct _BPU_T_Code_Ctx *ctx)
Definition: mecsctx.h:42
uint16_t msg_len
Code dimenzion.
Definition: codectx.h:62
#define BPU_printError(fmt,...)
print error message with filename, line
Definition: debugio.h:47
int BPU_codeInitParamsQcmdpc(BPU_T_UN_Code_Params *params, const uint16_t m, const uint16_t n0, const uint16_t w, const uint16_t t)
Definition: codectx.c:195
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
void BPU_mecsFreeParamsQcmdpc(BPU_T_UN_Mecs_Params *params)
Definition: mecsctx.c:204
enum _BPU_T_EN_Mecs_Types BPU_T_EN_Mecs_Types
Possible types of MECS.
int BPU_mecsPointchevalCCA2Decrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx)
BPU_mecsPointchevalCCA2Decrypt.
int BPU_mecsFreeCtx(BPU_T_Mecs_Ctx **ctx)
Definition: mecsctx.c:158
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 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
void BPU_codeFreeCtx(BPU_T_Code_Ctx **ctx)
BPU_codeFreeCtx.
Definition: codectx.c:161
adapted Pointcheval's cca2 conversion
Definition: mecsctx.h:30
int BPU_codeInitParamsGoppa(BPU_T_UN_Code_Params *params, const uint16_t m, const uint16_t t, const BPU_T_GF2_16x mod)
BPU_codeInitParamsGoppa.
Definition: codectx.c:187
int BPU_mecsBasicEncrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx)
Definition: mecsbasic.c:24
BPU_T_EN_Mecs_Types type
Definition: mecsctx.h:39
#define BPU_EC_MECS_TYPE_NOT_SUPPORTED
Definition: errorcodes.h:31
#define BPU_HASH_LEN
Definition: sha512.h:28
adapted Pointcheval's cca2 conversion for qcmdpc
Definition: mecsctx.h:34
int BPU_mecsQcmdpcGenKeys(BPU_T_Code_Ctx *ctx)
Definition: qcmdpc.c:247
int BPU_goppaGenCode(BPU_T_Code_Ctx *ctx)
BPU_goppaGenCode Generate random code based on params.
Definition: goppa.c:249
uint16_t code_len
Code len.
Definition: codectx.h:61