ASCON Suite
ascon-trng-esp.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-trng.h"
24 #include <string.h>
25 
26 #if defined(ASCON_TRNG_ESP)
27 
28 #if defined(ESP8266)
29 #define esp_random() (*((volatile uint32_t *)0x3FF20E44))
30 #else
31 /* It is variable from one ESP32 SDK to the next which header this
32  * function is declared in, so we declare it ourselves. */
33 extern uint32_t esp_random(void);
34 #endif
35 
37 int ascon_trng_generate(unsigned char *out, size_t outlen)
38 {
39  uint32_t x;
40  while (outlen >= sizeof(x)) {
41  x = esp_random();
42  memcpy(out, &x, sizeof(x));
43  out += sizeof(x);
44  outlen -= sizeof(x);
45  }
46  if (outlen > 0) {
47  x = esp_random();
48  memcpy(out, &x, outlen);
49  }
50  return 1; /* Assume that it works */
51 }
53 
55 {
56  /* Assume that it works */
57  return 1;
58 }
59 
61 {
62  (void)state;
63 }
64 
67 {
68  (void)state;
69  return esp_random();
70 }
71 
73 {
74  (void)state;
75  return ((uint64_t)esp_random()) | (((uint64_t)esp_random()) << 32);
76 }
78 
81 {
82  (void)state;
83  return 1;
84 }
86 
87 #endif /* ASCON_TRNG_ESP */
uint32_t ascon_trng_generate_32(ascon_trng_state_t *state)
Generates a 32-bit random value for masking operations.
uint64_t ascon_trng_generate_64(ascon_trng_state_t *state)
Generates a 64-bit random value for masking operations.
int ascon_trng_init(ascon_trng_state_t *state)
Initializes the random number source for generating a sequence of masking material at high speed.
int ascon_trng_reseed(ascon_trng_state_t *state)
Reseeds the random number source.
void ascon_trng_free(ascon_trng_state_t *state)
Frees the random number source and destroys any sensitive material.
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.
ascon_state_t state
[snippet_key]
Definition: snippets.c:2
State of the random number source.
Definition: ascon-trng.h:64