MPHELL  4.0.0
mphell-init.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 <stdlib.h>
27 #include <time.h>
28 
29 #include "mphell-init.h"
30 
31 random_state_t *random_working_state;
32 uint16_t random_security_strength;
33 
34 void
35 init_mphell(const uint16_t security_strength, const random_type type,
36  const entropy_type entropy)
37 {
38  mphell_status ret;
39  random_working_state = (random_state_t*)malloc(sizeof(random_state_t));
40  /* Instantiate the DRBG with reseed_interval to 2^20 = 1048576, if an MPHELL DRBG is used */
41  random_instantiate(ret, random_working_state, security_strength, NULL, 0,
42  type, entropy, 1048576);
43  if(ret->flag != MPHELL_SUCCESS)
44  {
45  mphell_error_free(ret->info);
46  }
47  random_security_strength = security_strength;
48 
50 }
51 
52 #if BLOCK_SIZE == 64
53 
54 uint64_t
55 rand64()
56 {
57  mphell_status ret;
58  uint64_t res;
59  random_generate(ret, (uint8_t*)&res, random_working_state, 8,
60  random_security_strength, NULL, 0);
61  if(ret->flag != MPHELL_SUCCESS)
62  {
63  mphell_error_free(ret->info);
64  }
65  return res;
66 }
67 #endif
68 
69 uint32_t
71 {
72  mphell_status ret;
73  uint32_t res;
74  random_generate(ret, (uint8_t*)&res, random_working_state, 4,
75  random_security_strength, NULL, 0);
76  if(ret->flag != MPHELL_SUCCESS)
77  {
78  mphell_error_free(ret->info);
79  }
80  return res;
81 }
82 
83 int
84 mphell_rng(void * param, unsigned char * res, size_t n)
85 {
86  mphell_status ret;
87  random_generate(ret, (uint8_t*)res, random_working_state, n,
88  random_security_strength, NULL, 0);
89  if(ret->flag != MPHELL_SUCCESS)
90  {
91  mphell_error_free(ret->info);
92  }
93  return 0;
94 }
95 
96 void
98 {
99  mphell_status ret;
100  random_uninstantiate(ret, random_working_state);
101  if(ret->flag != MPHELL_SUCCESS)
102  {
103  mphell_error_free(ret->info);
104  }
105  random_security_strength = 0;
106  free(random_working_state);
107 
108  free_tmp_memory();
109 }
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
uint32_t rand32()
Return a 32 bits random number using MPHELL random process.
Definition: mphell-init.c:70
enum random_algo random_type
Define the random algorithm (DRBG algorithm)
Definition: mphell-random.h:50
Declaration of MPHELL initialisation function.
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
void free_tmp_memory()
Free the stack for temporary elements.
int mphell_rng(void *param, unsigned char *res, size_t n)
Set res to n random bytes.
Definition: mphell-init.c:84
void mphell_error_free(char *expr)
Write in stderr, filename, line and expr, free expr and mphell.
Definition: mphell-errors.c:61
mphell_status_t mphell_status[1]
The status is a couple (flag, information)
Definition: mphell-errors.h:94
enum entropy_source entropy_type
Define the entropy source.
Define a random state.
Definition: mphell-random.h:56
void random_uninstantiate(mphell_status ret, random_state working_state)
Uninstantiate a random state.
void alloc_tmp_memory()
Allocate the stack for temporary elements, its size is defined in mphell-define.h.