BitPunch McEliece  v0.0.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
padding.c
Go to the documentation of this file.
1 /*
2 This file is part of BitPunch
3 Copyright (C) 2013-2015 Frantisek Uhrecky <frantisek.uhrecky[what here]gmail.com>
4 Copyright (C) 2013-2014 Andrej Gulyas <andrej.guly[what here]gmail.com>
5 Copyright (C) 2013-2014 Marek Klein <kleinmrk[what here]gmail.com>
6 Copyright (C) 2013-2014 Filip Machovec <filipmachovec[what here]yahoo.com>
7 Copyright (C) 2013-2014 Jozef Kudlac <jozef[what here]kudlac.sk>
8 
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "padding.h"
23 
24 #include <bitpunch/debugio.h>
25 #include <bitpunch/math/gf2.h>
26 
27 int BPU_padAdd(BPU_T_GF2_Vector *padded_message, const BPU_T_GF2_Vector *message, const uint16_t padding_len) {
28  int i;
29 
30  if (message->len + padding_len != padded_message->len) {
31  BPU_printError("Wrong message len");
32  return -1;
33  }
34  // copy message into padded message
35  for (i = 0; i < message->elements_in_row; i++){
36  padded_message->elements[i] = message->elements[i];
37  }
38  // add padding - first padded bit set to 1, rest keep 0
39  BPU_gf2VecSetBit(padded_message, message->len, 1);
40 
41  return 0;
42 }
43 
44 int BPU_padDel(BPU_T_GF2_Vector *message, const BPU_T_GF2_Vector *padded_message) {
45  int i, message_size = 0;
46 
47  // count the message size
48  for (i = padded_message->len-1; i >= 0; i--) {
49  // nula - padding
50  if (BPU_gf2VecGetBit(padded_message, i) == 1) {
51 
52  // ci bola aspon jedna 0 pred 1
53  //if (i <= padded_message->len-3) {
54  message_size = i;
55  break;
56  //}
57  // inak zly padding
58  /*else {
59  BPU_printError("del_padding: message padding is incorrect");
60  return -1;
61  }*/
62  }
63  }
64  if (message->len < message_size) {
65  BPU_printError("Wrong message size.");
66  return -1;
67  }
68  message->len = message_size;
69 
70  // copy n-1 elements of padded message into message
71  for (i = 0; i < padded_message->elements_in_row - 1; i++){
72  message->elements[i] = padded_message->elements[i];
73  }
74  // copy the rest of message
75  for (i = (padded_message->elements_in_row - 1) * padded_message->element_bit_size; i < message->len; i++){
76  BPU_gf2VecSetBit(message, i, BPU_gf2VecGetBit(padded_message, i));
77  }
78  return 0;
79 }
int BPU_padAdd(BPU_T_GF2_Vector *padded_message, const BPU_T_GF2_Vector *message, const uint16_t padding_len)
Add padding to message.
Definition: padding.c:27
#define BPU_printError(fmt,...)
print error message with filename, line
Definition: debugio.h:47
BPU_T_GF2 * elements
all element of matrix
Definition: gf2types.h:33
#define BPU_gf2VecSetBit(v_pointer, i, bit)
Definition: gf2.h:215
uint16_t elements_in_row
number of elements in one row
Definition: gf2types.h:35
int BPU_padDel(BPU_T_GF2_Vector *message, const BPU_T_GF2_Vector *padded_message)
Delete padding from message.
Definition: padding.c:44
uint32_t len
cols
Definition: gf2types.h:36
#define BPU_gf2VecGetBit(v_pointer, i)
Check if is set bit at i-th position in vector.
Definition: gf2.h:192
uint8_t element_bit_size
element size, is sizeof(BPU_T_GF2) i.e. 64 bits
Definition: gf2types.h:34