MPHELL  4.0.0
mphell-util.c
Go to the documentation of this file.
1 /*
2  MPHELL-4.0
3  Author(s): The MPHELL team
4 
5  (C) Copyright 2015-2018 - Institut Fourier / Univ. Grenoble Alpes (France)
6 
7  This file is part of the MPHELL Library.
8  MPHELL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation, version 3 of the License.
11 
12  MPHELL is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with MPHELL. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
26 #include "mphell-util.h"
27 
28 uint8_t
29 bits_to_nblock (const uint16_t nbits)
30 {
31  return (uint8_t)((nbits / BLOCK_SIZE) + ((nbits % BLOCK_SIZE) != 0));
32 }
33 
34 uint8_t max_size(uint8_t a, uint8_t b)
35 {
36  return b < a ? a : b;
37 }
38 
39 time_t get_s(struct timespec * start, struct timespec * end)
40 {
41  if((end->tv_nsec - start->tv_nsec)>0)
42  {
43  return (end->tv_sec - start->tv_sec);
44  }
45  return (end->tv_sec - start->tv_sec - 1);
46 }
47 
48 unsigned int get_ns(struct timespec * start, struct timespec * end)
49 {
50  long t = end->tv_nsec - start->tv_nsec;
51  if (t>0)
52  {
53  return (unsigned int)floor(t/1000000);
54  }
55  return (unsigned int)floor((1000000000 + t)/1000000);
56 }
57 
58 void print_bytes_string_hex(const uint8_t * bytes_string, uint16_t length)
59 {
60  int i;
61  for (i=0; i<length; i++)
62  {
63  printf("%02x", bytes_string[i]);
64  }
65 }
66 
67 void bytes_string2hex(char * hex_string, const uint8_t * bytes_string, uint16_t length)
68 {
69  int i;
70  char * hex_string_pointer = hex_string;
71  for (i=0; i<length; i++)
72  {
73  sprintf(hex_string_pointer, "%02x", bytes_string[i]);
74  hex_string_pointer+=2;
75  }
76 }
77 
78 void hex_string2bytes(uint8_t * bytes_string, const char * hex_string, uint16_t length)
79 {
80  int i;
81  char buf[5];
82  buf[0]='0';
83  buf[1]='x';
84  buf[4]='\0';
85  const char * hex_string_pointer = hex_string;
86  for (i = 0; i < length; i++)
87  {
88  buf[2] = hex_string_pointer[0];
89  buf[3] = hex_string_pointer[1];
90  bytes_string[i] = strtol(buf, NULL, 0);
91  hex_string_pointer += 2;
92  }
93 }
94 
95 void bin2hex(const unsigned char *old, const uint32_t oldlen, unsigned char* result)
96 {
97  uint32_t i, j;
98  int32_t b = 0;
99 
100  for (i = j = 0; i < oldlen; i++)
101  {
102  b = old[i] >> 4;
103  result[j++] = (char) (87 + b + (((b - 10) >> 31) & -39));
104  b = old[i] & 0xf;
105  result[j++] = (char) (87 + b + (((b - 10) >> 31) & -39));
106  }
107  result[j] = '\0';
108 }
109 
110 
111 uint32_t hex2bin(const char *in, unsigned char *out)
112 {
113  uint32_t n1, n2;
114  unsigned char ch;
115  uint32_t len = strlen(in);
116 
117  n2 = n1 = 0;
118  if (len % 2 != 0)
119  {
120  if ((in[n1] >= '0') && (in[n1] <= '9'))
121  ch = in[n1++] - '0';
122  else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
123  ch = in[n1++] - 'A' + 10;
124  else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
125  ch = in[n1++] - 'a' + 10;
126  else
127  return -1;
128 
129  out[n2++] = ch;
130  }
131 
132  for (; in[n1] && in[n1] != '\n' && in[n1] != '\r';)
133  { /* first byte */
134  if ((in[n1] >= '0') && (in[n1] <= '9'))
135  ch = in[n1++] - '0';
136  else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
137  ch = in[n1++] - 'A' + 10;
138  else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
139  ch = in[n1++] - 'a' + 10;
140  else
141  return -1;
142  if (!in[n1])
143  {
144  out[n2++] = ch;
145  break;
146  }
147  out[n2] = ch << 4;
148  /* second byte */
149  if ((in[n1] >= '0') && (in[n1] <= '9'))
150  ch = in[n1++] - '0';
151  else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
152  ch = in[n1++] - 'A' + 10;
153  else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
154  ch = in[n1++] - 'a' + 10;
155  else
156  return -1;
157  out[n2++] |= ch;
158  }
159  return n2;
160 }
161 
162 /* For aes test */
163 
164 #define MAX_MARKER_LEN 50
165 
166 int
167 FindMarker (FILE *infile, const char *marker)
168 {
169  char line[MAX_MARKER_LEN];
170  int i, len;
171 
172  len = (int)strlen(marker);
173  if ( len > MAX_MARKER_LEN-1 )
174  len = MAX_MARKER_LEN-1;
175 
176  for ( i=0; i<len; i++ )
177  if ( (line[i] = fgetc(infile)) == EOF )
178  return 0;
179  line[len] = '\0';
180 
181  while ( 1 )
182  {
183  if ( !strncmp(line, marker, len) )
184  return 1;
185 
186  for ( i=0; i<len-1; i++ )
187  line[i] = line[i+1];
188  if ( (line[len-1] = fgetc(infile)) == EOF )
189  return 0;
190  line[len] = '\0';
191  }
192  return 0;
193 }
194 
195 int
196 ReadHex (FILE *infile, char *A, const int Length, char *str)
197 {
198  int i, ch, started;
199  uint8_t ich = 0;
200 
201  if ( Length == 0 ) {
202  A[0] = 0x00;
203  return 1;
204  }
205  memset(A, '0', Length/2);
206  started = 0;
207  i = 0;
208  if ( FindMarker(infile, str) )
209  while ( (ch = fgetc(infile)) != EOF )
210  {
211  if ( !isxdigit(ch) )
212  {
213  if ( !started )
214  {
215  if ((ch == '\n') || (ch =='\0'))
216  break;
217  else
218  continue;
219  }
220  else
221  break;
222  }
223  started = 1;
224  if ( (ch >= '0') && (ch <= '9') )
225  ich = ch - '0';
226  else if ( (ch >= 'A') && (ch <= 'F') )
227  ich = ch - 'A' + 10;
228  else if ( (ch >= 'a') && (ch <= 'f') )
229  ich = ch - 'a' + 10;
230  A[i / 2] = (A[i / 2] << 4) | ich;
231  if ( (++i / 2) == Length )
232  break;
233  }
234  else
235  return 0;
236 
237  return 1;
238 }
239 
240 /* For ecdsa benchmark */
241 
242 void char2bin(char c, uint8_t * b)
243 {
244  int8_t i;
245  for (i = 7; i >= 0; --i)
246  {
247  if(c & (1 << i))
248  {
249  *b ^= (1 << i);
250  }
251  }
252 }
253 
254 void printchar2bin(uint8_t b)
255 {
256  int8_t i;
257  for (i = 7; i >= 0; --i)
258  {
259  if(b & (1 << i))
260  {
261  printf("1");
262  }
263  else
264  {
265  printf("0");
266  }
267  }
268  printf(" ");
269 }
270 
271 void string2bin(char* s, uint8_t * output)
272 {
273  uint32_t len = strlen(s);
274  uint32_t i;
275  for(i=0; i<len; i++)
276  {
277  char2bin(s[i], output+i);
278  }
279 }
280 
281 void printstring2bin(uint8_t * s, uint32_t len)
282 {
283  uint32_t i;
284  for(i=0; i<len; i++)
285  {
286  printchar2bin(s[i]);
287  }
288 }
289 
void hex_string2bytes(uint8_t *bytes_string, const char *hex_string, uint16_t length)
Convert the hexadecimal string "hex_string" under bytes string form.
Definition: mphell-util.c:78
time_t get_s(struct timespec *start, struct timespec *end)
Return the elapsed time in second between "start" and "end".
Definition: mphell-util.c:39
void char2bin(char c, uint8_t *b)
Convert a character into a byte (according to its ascii value)
Definition: mphell-util.c:242
unsigned int get_ns(struct timespec *start, struct timespec *end)
Return the elapsed time in nano second to add to the result of get_s().
Definition: mphell-util.c:48
uint32_t hex2bin(const char *in, unsigned char *out)
Convert an hexadecimal string into a binary string.
Definition: mphell-util.c:111
uint8_t bits_to_nblock(const uint16_t nbits)
Return the number of blocks required to store a nbits number.
Definition: mphell-util.c:29
int ReadHex(FILE *infile, char *A, const int Length, char *str)
Read an hexadecimal string in file "infile".
Definition: mphell-util.c:196
void bytes_string2hex(char *hex_string, const uint8_t *bytes_string, uint16_t length)
Convert the byte string "bytes_string" under hexadecimal form.
Definition: mphell-util.c:67
Declaration of miscellaneous function.
int FindMarker(FILE *infile, const char *marker)
Find a marker in a file.
Definition: mphell-util.c:167
void string2bin(char *s, uint8_t *output)
Convert a string into a byte array.
Definition: mphell-util.c:271
void printstring2bin(uint8_t *s, uint32_t len)
Print a byte array.
Definition: mphell-util.c:281
uint8_t max_size(uint8_t a, uint8_t b)
Return the max of a and b.
Definition: mphell-util.c:34
void print_bytes_string_hex(const uint8_t *bytes_string, uint16_t length)
Print the byte string "bytes_string" under hexadecimal form.
Definition: mphell-util.c:58
void printchar2bin(uint8_t b)
print a byte
Definition: mphell-util.c:254