BitPunch McEliece  v0.0.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
mecspointcheval.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 "mecspointcheval.h"
19 
20 #if defined(BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA) || defined(BPU_CONF_MECS_CCA2_POINTCHEVAL_QCMDPC)
21 #include <bitpunch/debugio.h>
22 #include <bitpunch/math/gf2.h>
24 
25 #ifdef BPU_CONF_ENCRYPTION
27  BPU_T_GF2_Vector *r1, *r2, *cca2_pt, *hash, *hash_in, *enc_pt, *tmp;
28  int rc = 0;
29 
30  // Generate a random (k − l)-bit vector r1 and a random l-bit vector r2
31  BPU_gf2VecMalloc(&r2, ctx->pt_len);
32  BPU_gf2VecMalloc(&r1, ctx->code_ctx->msg_len - ctx->pt_len);
33  BPU_gf2VecRand(r1, 0);
34  BPU_gf2VecRand(r2, 0);
35 
36  BPU_gf2VecMalloc(&hash, ctx->pt_len);
37  BPU_gf2VecMalloc(&hash_in, 2 * ctx->pt_len);
38  // Create CCA2-safe plaintext m= r1 ∥ hash (m ∥ r2 )
39  BPU_gf2VecConcat(hash_in, in, r2);
40 
41  BPU_gf2VecHash(hash, hash_in);
42  BPU_gf2VecFree(&hash_in);
43 
44  BPU_gf2VecMalloc(&cca2_pt, r1->len + hash->len);
45  BPU_gf2VecConcat(cca2_pt, r1, hash);
46 
47  BPU_gf2VecMalloc(&enc_pt, ctx->code_ctx->code_len);
48 
49  // encrypt with basic MECS
50  if (BPU_mecsBasicEncrypt(enc_pt, cca2_pt, ctx)) {
51  return -1;
52  }
53  BPU_gf2VecFree(&cca2_pt);
54 
55  // add CCA2-safe data extension z = (z′ ⊕ e) ∥ (hash (r1) ⊕ m) ∥ (hash (e) ⊕ r2 )
56  BPU_gf2VecMalloc(&tmp, enc_pt->len + hash->len);
57  BPU_gf2VecHash(hash, r1);
58  BPU_gf2VecFree(&r1);
59  BPU_gf2VecXor(hash, in);
60  BPU_gf2VecConcat(tmp, enc_pt, hash);
61  BPU_gf2VecFree(&enc_pt);
62 
63  BPU_gf2VecHash(hash, ctx->code_ctx->e);
64  BPU_gf2VecXor(hash, r2);
65  BPU_gf2VecFree(&r2);
66  BPU_gf2VecConcat(out, tmp, hash);
67 
68  BPU_gf2VecFree(&hash);
69  BPU_gf2VecFree(&tmp);
70 
71  return rc;
72 }
73 #endif // BPU_CONF_ENCRYPTION
74 
75 #ifdef BPU_CONF_DECRYPTION
77  BPU_T_GF2_Vector *z1, *z3; // n, l, l-bit
78  BPU_T_GF2_Vector *tmp_2, *pt_cca2;
79  BPU_T_GF2_Vector *r; // k - l
80  BPU_T_GF2_Vector *h, *h_tmp;
81  int rc = 0;
82 
83  // split ct in z1, z2, z3
85  // z2 is like out
86  BPU_gf2VecMalloc(&z3, ctx->pt_len);
87 
88  // Split z to ( z1 , z2 , z3 )
89  BPU_gf2VecCrop(z1, in, 0, z1->len);
90  BPU_gf2VecCrop(out, in, z1->len, out->len);
91  BPU_gf2VecCrop(z3, in, z1->len + out->len, z3->len);
92 
93  BPU_gf2VecMalloc(&pt_cca2, ctx->code_ctx->msg_len);
94  // decrypt z1 using basic mecs Reconstruct the CCA2-safe plaintext m′ = z1 ⊕ e
95  if (BPU_mecsBasicDecrypt(pt_cca2, z1, ctx)) {
96  return -1;
97  }
98  BPU_gf2VecFree(&z1);
99 
100  BPU_gf2VecMalloc(&r, ctx->code_ctx->msg_len - ctx->pt_len);
101  BPU_gf2VecMalloc(&h, ctx->pt_len);
102  BPU_gf2VecCrop(r, pt_cca2, 0, r->len);
103  BPU_gf2VecCrop(h, pt_cca2, r->len, h->len);
104  BPU_gf2VecFree(&pt_cca2);
105 
106  BPU_gf2VecMalloc(&h_tmp, ctx->pt_len);
107  // Reconstruct plaintext candidate m = z2 ⊕ hash (r)
108  BPU_gf2VecHash(h_tmp, r);
109  BPU_gf2VecFree(&r);
110  BPU_gf2VecXor(out, h_tmp);
111 
112  // Determine check value h′ = hash (m ∥ hash (e) ⊕ z3 ).
113  BPU_gf2VecHash(h_tmp, ctx->code_ctx->e);
114  BPU_gf2VecXor(h_tmp, z3);
115  BPU_gf2VecFree(&z3);
116 
117  BPU_gf2VecMalloc(&tmp_2, ctx->pt_len * 2);
118  BPU_gf2VecConcat(tmp_2, out, h_tmp);
119  BPU_gf2VecHash(h_tmp, tmp_2);
120  BPU_gf2VecFree(&tmp_2);
121 
122  if (BPU_gf2VecCmp(h, h_tmp)) {
123  BPU_printError("Wrong check value.");
124 
125  rc = -1;
126  }
127  BPU_gf2VecFree(&h);
128  BPU_gf2VecFree(&h_tmp);
129  return rc;
130 }
131 #endif // BPU_CONF_DECRYPTION
132 
133 #endif // BPU_CONF_MECS_CCA2_POINTCHEVAL_GOPPA
134 
int BPU_gf2VecMalloc(BPU_T_GF2_Vector **v, int len)
Definition: gf2types.c:97
BPU_T_Code_Ctx * code_ctx
Definition: mecsctx.h:44
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
int BPU_gf2VecHash(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in)
Definition: sha512.c:26
void BPU_gf2VecFree(BPU_T_GF2_Vector **v)
Free dynamically or statically allocated vector.
Definition: gf2types.c:45
uint16_t pt_len
PT len in bits.
Definition: mecsctx.h:45
int BPU_gf2VecRand(BPU_T_GF2_Vector *out, int w)
Definition: gf2.c:240
int BPU_mecsPointchevalCCA2Encrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx)
BPU_mecsPointchevalCCA2Encrypt.
int BPU_gf2VecCmp(const BPU_T_GF2_Vector *v1, const BPU_T_GF2_Vector *v2)
BPU_gf2VecCmp Compare two vectors.
Definition: gf2.c:467
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
int BPU_mecsPointchevalCCA2Decrypt(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const BPU_T_Mecs_Ctx *ctx)
BPU_mecsPointchevalCCA2Decrypt.
int BPU_gf2VecCrop(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *in, const int start, const int length)
Definition: gf2.c:424
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
int BPU_gf2VecConcat(BPU_T_GF2_Vector *out, const BPU_T_GF2_Vector *vec1, const BPU_T_GF2_Vector *vec2)
Concats two vectors without allocation ouput.
Definition: gf2.c:399
uint16_t code_len
Code len.
Definition: codectx.h:61