MPHELL  5.0.0
mphell_tuto_drbg.c
Go to the documentation of this file.
1 /*
2  MPHELL-5.0
3  Author(s): The MPHELL team
4 
5  (C) Copyright 2015-2021 - 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  uint8_t returned_bytes[512/8];
48  char returned_bytes_hex[512/4];
49 
50  uint8_t entropy_byte[128];
51  uint8_t nonce_byte[64];
52  uint8_t entropy_reseed_byte[128];
53  uint8_t additionnal_input_reseed_byte[128];
54  uint8_t additionnal_input1_byte[128];
55  uint8_t additionnal_input2_byte[128];
56  uint8_t personalization_string_byte[128];
57 
58  /* Convert to bytes strings */
59  hex_string2bytes(entropy_byte, "03af6c44c7101544ca7888831f3f9f95", 128/8);
60  hex_string2bytes(nonce_byte, "58d80fc378569346", 64/8);
61 
62  hex_string2bytes(entropy_reseed_byte, "410737c3c6112b5113f0ef66b0fa6a07", 128/8);
63  hex_string2bytes(additionnal_input_reseed_byte, "ae2ce6d1dba56775db17b8c6d9379f14", 128/8);
64  hex_string2bytes(additionnal_input1_byte, "57866375238ea56e97dd6af8c5010618", 128/8);
65  hex_string2bytes(additionnal_input2_byte, "34a449f860c0c18fdf854b32de572917", 128/8);
66  hex_string2bytes(personalization_string_byte, "7ec2b597c845ae8bbb63609a80c2ab4f", 128/8);
67 
68  /* Instantiate the DRBG, with reseed interval = 1000 */
69 
70  drbg_instantiate(ret, &state_handle, 128, DRBG_AES128, DEVURANDOM, 1000, entropy_byte, 128/8, nonce_byte, 64/8, personalization_string_byte, 128/8);
71  if(ret->flag != MPHELL_SUCCESS)
72  {
73  printf("ERROR drbg_instantiate_test_drbg, state_handle = %d\n", state_handle);
74  printf("ret->info: %s\n", ret->info);
75  return -1;
76  }
77 
78  /* Reseed the DRBG (optional) */
79 
80  drbg_reseed(ret, state_handle, entropy_reseed_byte, 128/8, additionnal_input_reseed_byte, 128/8);
81  if(ret->flag != MPHELL_SUCCESS)
82  {
83  printf("ERROR drbg_reseed, state_handle = %d\n", state_handle);
84  printf("ret->info: %s\n", ret->info);
85  return -1;
86  }
87 
88  /* Generate random number */
89 
90  drbg_generate(ret, returned_bytes, state_handle, 512/8, 128, additionnal_input1_byte, 128/8);
91  if(ret->flag != MPHELL_SUCCESS)
92  {
93  printf("ERROR drbg_generate 1st call, state_handle = %d\n", state_handle);
94  printf("ret->info: %s\n", ret->info);
95  return -1;
96  }
97 
98  bytes_string2hex(returned_bytes_hex, returned_bytes, 512/8);
99  printf("returned_bytes_hex: %s \n", returned_bytes_hex);
100 
101  /* Generate random number */
102 
103  drbg_generate(ret, returned_bytes, state_handle, 512/8, 128, additionnal_input2_byte, 128/8);
104  if(ret->flag != MPHELL_SUCCESS)
105  {
106  printf("ERROR drbg_generate 2nd call, state_handle = %d\n", state_handle);
107  printf("ret->info: %s\n", ret->info);
108  return -1;
109  }
110 
111  bytes_string2hex(returned_bytes_hex, returned_bytes, 512/8);
112  printf("returned_bytes_hex: %s \n", returned_bytes_hex);
113 
114  /* Uninstantiate the DRBG */
115  drbg_uninstantiate(ret, state_handle);
116  if(ret->flag != MPHELL_SUCCESS)
117  {
118  printf("ERROR drbg_uninstantiate, state_handle = %d\n", state_handle);
119  printf("ret->info: %s\n", ret->info);
120  return -1;
121  }
122 
123  free_mphell();
124 }
void drbg_generate(mphell_status ret, uint8_t *returned_bytes, const int8_t state_handle, const uint32_t requested_number_of_bytes, const uint16_t requested_security_strength, const uint8_t *additional_input, const uint64_t add_length)
"Generates pseudorandom bits upon request, using the current internal state, and generates a new inte...
Definition: mphell-drbg.c:317
void drbg_reseed(mphell_status ret, const int8_t state_handle, const uint8_t *entropy, const uint16_t entropy_length, const uint8_t *additional_input, const uint64_t add_length)
Reseed the DRBG, "acquires new entropy input and combines it with the current internal state and any ...
Definition: mphell-drbg.c:248
void drbg_instantiate(mphell_status ret, int8_t *state_handle, const uint16_t requested_instantiation_security_strength, const drbg_type type, const entropy_type entropy_src, uint64_t reseed_interval, const uint8_t *entropy, const uint16_t entropy_length, const uint8_t *nonce, const uint16_t nonce_length, const uint8_t *personalization_string, const uint64_t pers_length)
Instantiate one DRBG state, "acquires entropy input and may combine it with a nonce and a personaliza...
Definition: mphell-drbg.c:99
void drbg_uninstantiate(mphell_status ret, const int8_t state_handle)
Uninstantiate a DRBG state.
Definition: mphell-drbg.c:459
@ DRBG_AES128
@ DEVURANDOM
mphell_status_t mphell_status[1]
The status is a couple (flag, information)
Definition: mphell-errors.h:95
@ MPHELL_SUCCESS
Definition: mphell-errors.h:69
void free_mphell()
Free MPHELL memory, especially the big amount of temporary memory.
Definition: mphell-init.c:97
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
@ RANDOM_AES128
Definition: mphell-random.h:37
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 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