MPHELL  4.0.0
mphell_tuto_random.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 <stdio.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <unistd.h>
30 #include <string.h>
31 
32 #include "mphell/mphell.h"
33 #include "mphell/mphell-util.h"
34 
35 int main()
36 {
37  /* AES128 */
38 
39  printf("CTR DRBG AES 128 test \n");
40 
41  /* Initialise MPHELL with 128 bits of security strength for the entropy, RANDOM_AES128 as DRBG and DEVURANDOM as entropy source */
43 
44  mphell_status ret;
45  int8_t state_handle;
46 
47  random_state_t *random_working_state = (random_state_t*)malloc(sizeof(random_state_t));
48 
49  uint8_t returned_bytes[512/8];
50  char returned_bytes_hex[512/4];
51 
52  uint8_t additionnal_input_reseed_byte[128];
53  uint8_t additionnal_input1_byte[128];
54  uint8_t additionnal_input2_byte[128];
55  uint8_t personalization_string_byte[128];
56 
57  /* Convert to bytes strings */
58 
59  hex_string2bytes(personalization_string_byte, "7ec2b597c845ae8bbb63609a80c2ab4f", 128/8);
60  hex_string2bytes(additionnal_input_reseed_byte, "ae2ce6d1dba56775db17b8c6d9379f14", 128/8);
61  hex_string2bytes(additionnal_input1_byte, "57866375238ea56e97dd6af8c5010618", 128/8);
62 
63  /* Instantiate the DRBG with reseed_interval to 2^20 = 1048576, and DEVURANDOM as entropy source */
64 
65  random_instantiate(ret, random_working_state, 128, personalization_string_byte, 128/8, RANDOM_AES128, DEVURANDOM, 1048576);
66  if(ret->flag != MPHELL_SUCCESS)
67  {
68  printf("ERROR random_instantiate, state_handle = %d\n", random_working_state->state_handle);
69  printf("ret->info: %s\n", ret->info);
70  return -1;
71  }
72 
73  /* Reseed the DRBG (optional) */
74 
75  random_reseed(ret, random_working_state, 128, DEVURANDOM, additionnal_input_reseed_byte, 128/8);
76 
77  if(ret->flag != MPHELL_SUCCESS)
78  {
79  printf("ERROR random_reseed, state_handle = %d\n", random_working_state->state_handle);
80  printf("ret->info: %s\n", ret->info);
81  return -1;
82  }
83 
84  /* Generate random number */
85 
86  random_generate(ret, returned_bytes, random_working_state, 512/8, 128, additionnal_input1_byte, 128/8);
87 
88  if(ret->flag != MPHELL_SUCCESS)
89  {
90  printf("ERROR random_generate 1st call, state_handle = %d\n", random_working_state->state_handle);
91  printf("ret->info: %s\n", ret->info);
92  return -1;
93  }
94 
95  bytes_string2hex(returned_bytes_hex, returned_bytes, 512/8);
96  printf("returned_bytes_hex: %s \n", returned_bytes_hex);
97 
98 
99  /* Uninstantiate the DRBG */
100  random_uninstantiate(ret, random_working_state);
101  if(ret->flag != MPHELL_SUCCESS)
102  {
103  printf("ERROR random_uninstantiate, state_handle = %d\n", random_working_state->state_handle);
104  printf("ret->info: %s\n", ret->info);
105  return -1;
106  }
107 
108  free(random_working_state);
109  free_mphell();
110 }
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
void free_mphell()
Free MPHELL memory, especially the big amount of temporary memory.
Definition: mphell-init.c:97
void random_generate(mphell_status ret, uint8_t *returned_bytes, random_state working_state, const uint32_t requested_number_of_bytes, const uint16_t requested_security_strength, const uint8_t *additional_input, const uint64_t add_length)
Gererate an array of random bits.
void init_mphell(const uint16_t security_strength, const random_type type, const entropy_type entropy)
Initialise MPHELL with security_strength bits of security (for random number only).
Definition: mphell-init.c:35
void random_reseed(mphell_status ret, random_state working_state, const uint16_t entropy_length, const entropy_type entropy_src, const uint8_t *additional_input, const uint64_t add_length)
Reseed the DRBG (if you want to reseed it before it does by itself)
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
void random_instantiate(mphell_status ret, random_state initial_state, const uint16_t requested_instantiation_security_strength, const uint8_t *personalization_string, const uint64_t pers_length, const random_type type, const entropy_type entropy, uint64_t reseed_interval)
Instantiate one random state.
Definition: mphell-random.c:29
mphell_status_t mphell_status[1]
The status is a couple (flag, information)
Definition: mphell-errors.h:94
int8_t state_handle
Definition: mphell-random.h:57
Define a random state.
Definition: mphell-random.h:56
void random_uninstantiate(mphell_status ret, random_state working_state)
Uninstantiate a random state.