ASCON Suite
ascon-trng-due.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_DUE)
27 
28 #include <Arduino.h>
29 
30 static int volatile due_init_done = 0;
31 
32 static inline void ascon_trng_init_internal(void)
33 {
34  if (!due_init_done) {
35  /* Once-only initialization of the TRNG peripheral */
36  pmc_enable_periph_clk(ID_TRNG);
37  REG_TRNG_CR = TRNG_CR_KEY(0x524E47) | TRNG_CR_ENABLE;
38  REG_TRNG_IDR = TRNG_IDR_DATRDY;
39  due_init_done = 1;
40  }
41 }
42 
43 static inline int ascon_trng_generate_word(uint32_t *x)
44 {
45  /* SAM3X8E's TRNG returns a new random word every 84 clock cycles.
46  * If the TRNG is not ready after 100 iterations, assume it has failed. */
47  int count = 100;
48  while ((REG_TRNG_ISR & TRNG_ISR_DATRDY) == 0) {
49  if ((--count) <= 0) {
50  *x = 0xABADBEEF; /* This is a problem! */
51  return 0;
52  }
53  }
54  *x = REG_TRNG_ODATA;
55  return 1;
56 }
57 
58 int ascon_trng_generate(unsigned char *out, size_t outlen)
59 {
60  uint32_t x;
61  int ok = 1;
62  ascon_trng_init_internal();
63  while (outlen >= sizeof(x)) {
64  if (!ascon_trng_generate_word(&x))
65  ok = 0;
66  memcpy(out, &x, sizeof(x));
67  out += sizeof(x);
68  outlen -= sizeof(x);
69  }
70  if (outlen > 0) {
71  if (!ascon_trng_generate_word(&x))
72  ok = 0;
73  memcpy(out, &x, outlen);
74  }
75  return ok;
76 }
77 
79 {
80  uint32_t x;
81 
82  /* Make sure that the peripheral is initialized */
83  ascon_trng_init_internal();
84 
85  /* Test that the TRNG works by generating a single word */
86  return ascon_trng_generate_word(&x);
87 }
88 
90 {
91  (void)state;
92 }
93 
95 {
96  uint32_t word;
97  (void)state;
98  ascon_trng_generate_word(&word);
99  return word;
100 }
101 
103 {
104  uint32_t low, high;
105  (void)state;
106  ascon_trng_generate_word(&low);
107  ascon_trng_generate_word(&high);
108  return ((uint64_t)low) | (((uint64_t)high) << 32);
109 }
110 
112 {
113  return ascon_trng_init(state);
114 }
115 
116 #endif /* ASCON_TRNG_DUE */
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