ASCON Suite
ascon-prng.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2022 Southern Storm Software, Pty Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include <ascon/random.h>
24 #include <ascon/utility.h>
25 #include "random/ascon-trng.h"
26 #include "core/ascon-util-snp.h"
27 
31 #define ASCON_RANDOM_RESEED_LIMIT 16384
32 
51 static void ascon_random_rekey(ascon_random_state_t *state)
52 {
53  int temp;
54 
55  /* Zero out part of the state and run the permutation several times.
56  * This enforces forward security on the SpongePRNG state. */
57  ascon_xof_pad(&(state->xof));
58  ascon_acquire(&(state->xof.state));
59  for (temp = 0; temp < (40 - ASCON_XOF_RATE); temp += ASCON_XOF_RATE) {
61  ascon_permute(&(state->xof.state), 0);
62  }
63  ascon_release(&(state->xof.state));
64 }
65 
67 {
68  unsigned char seed[ASCON_SYSTEM_SEED_SIZE];
69  int ok;
70  if (!state)
71  return 0;
72  ascon_xof_init_custom(&(state->xof), "SpongePRNG", 0, 0, 0);
73  state->counter = 0;
74  state->reserved = 0;
75  ok = ascon_trng_generate(seed, sizeof(seed));
76  ascon_xof_absorb(&(state->xof), seed, sizeof(seed));
77  ascon_clean(seed, sizeof(seed));
78  ascon_random_rekey(state);
79  return ok;
80 }
81 
83 {
84  if (state) {
85  state->counter = 0;
86  ascon_xof_free(&(state->xof));
87  }
88 }
89 
91  (ascon_random_state_t *state, unsigned char *out, size_t outlen)
92 {
93  /* If there is no state, use the global ascon_random() function
94  * so that we return something. Safer than returning nothing
95  * to the caller by accident and having them use that nothing. */
96  if (!state) {
97  ascon_random(out, outlen);
98  return;
99  }
100 
101  /* Force a re-seed if we have generated too many bytes so far */
102  if (state->counter >= ASCON_RANDOM_RESEED_LIMIT)
104 
105  /* Squeeze data out of the PRNG state */
106  ascon_xof_squeeze(&(state->xof), out, outlen);
107  if (outlen < ASCON_RANDOM_RESEED_LIMIT)
108  state->counter += outlen;
109  else
110  state->counter = ASCON_RANDOM_RESEED_LIMIT;
111 
112  /* Re-key the PRNG to enforce forward security */
113  ascon_random_rekey(state);
114 }
115 
117 {
118  if (state) {
119  /* Generate a new system seed and absorb it into the state */
120  unsigned char seed[ASCON_SYSTEM_SEED_SIZE];
121  int ok = ascon_trng_generate(seed, sizeof(seed));
122  ascon_xof_absorb(&(state->xof), seed, sizeof(seed));
123  ascon_clean(seed, sizeof(seed));
124 
125  /* Reset the re-seed counter to 0 */
126  state->counter = 0;
127 
128  /* Re-key the PRNG to enforce forward security */
129  ascon_random_rekey(state);
130  return ok;
131  }
132  return 0;
133 }
134 
136  (ascon_random_state_t *state, const unsigned char *entropy, size_t size)
137 {
138  if (state) {
139  ascon_xof_absorb(&(state->xof), entropy, size);
140  ascon_xof_pad(&(state->xof));
141  ascon_random_rekey(state);
142  }
143 }
int ascon_random_reseed(ascon_random_state_t *state)
Explicitly re-seeds a pseudorandom number generator from the system random number source.
Definition: ascon-prng.c:116
void ascon_random_fetch(ascon_random_state_t *state, unsigned char *out, size_t outlen)
Fetches data from a pseudorandom number generator.
Definition: ascon-prng.c:91
#define ASCON_RANDOM_RESEED_LIMIT
Automatically re-seed after generating more than this many bytes.
Definition: ascon-prng.c:31
void ascon_random_free(ascon_random_state_t *state)
Frees a pseudorandom number generator and destroys any sensitive values.
Definition: ascon-prng.c:82
int ascon_random_init(ascon_random_state_t *state)
Initializes a pseudorandom number generator from the system random number source.
Definition: ascon-prng.c:66
void ascon_random_feed(ascon_random_state_t *state, const unsigned char *entropy, size_t size)
Feeds entropy into a pseudorandom number generator.
Definition: ascon-prng.c:136
int ascon_trng_generate(unsigned char *out, size_t outlen)
Generates a buffer of bytes from the system TRNG source.
Access to the system's random number source.
#define ASCON_SYSTEM_SEED_SIZE
Number of bytes to request from the system TRNG to seed a PRNG.
Definition: ascon-trng.h:58
void ascon_overwrite_with_zeroes(ascon_state_t *state, unsigned offset, unsigned size)
Overwrites a part of the ASCON state with zeroes.
void ascon_release(ascon_state_t *state)
Temporarily releases access to any shared hardware resources that a permutation state was using.
void ascon_permute(ascon_state_t *state, uint8_t first_round)
Permutes the ASCON state with a specified number of rounds.
Definition: ascon-c32.c:36
void ascon_acquire(ascon_state_t *state)
Re-acquires access to any shared hardware resources that a permutation state was using.
Pseudorandom number generator (PRNG) built around ASCON.
int ascon_random(unsigned char *out, size_t outlen)
Gets a block of random data from the system.
Definition: ascon-random.c:27
ascon_state_t state
[snippet_key]
Definition: snippets.c:2
State information for a pseudorandom number generator.
Definition: random.h:49
System utilities of use to applications that use ASCON.
void ascon_clean(void *buf, unsigned size)
Cleans a buffer that contains sensitive material.
Definition: ascon-clean.c:38
void ascon_xof_free(ascon_xof_state_t *state)
Frees the ASCON-XOF state and destroys any sensitive material.
Definition: ascon-xof.c:218
void ascon_xof_pad(ascon_xof_state_t *state)
Absorbs enough zeroes into an ASCON-XOF state to pad the input to the next multiple of the block rate...
Definition: ascon-xof.c:329
void ascon_xof_absorb(ascon_xof_state_t *state, const unsigned char *in, size_t inlen)
Absorbs more input data into an ASCON-XOF state.
Definition: ascon-xof.c:229
#define ASCON_XOF_RATE
Rate of absorbing and squeezing data for ASCON-XOF, ASCON-XOFA, ASCON-HASH, and ASCON-HASHA.
Definition: xof.h:55
void ascon_xof_init_custom(ascon_xof_state_t *state, const char *function_name, const unsigned char *custom, size_t customlen, size_t outlen)
Initializes the state for an incremental ASCON-XOF operation, with a named function,...
Definition: ascon-xof.c:146
void ascon_xof_squeeze(ascon_xof_state_t *state, unsigned char *out, size_t outlen)
Squeezes output data from an ASCON-XOF state.
Definition: ascon-xof.c:279