You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference/contributing/target/entropy.md
+16-17Lines changed: 16 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,26 @@
1
-
###Mbed TLS entropy
1
+
## Mbed TLS entropy
2
2
3
3
This document explains how to port [Arm Mbed TLS](https://github.com/ARMmbed/mbedtls) to a new Arm Mbed development board.
4
4
5
5
<spanclass="notes">**Note:** This part is critical for the security of your product, and you should consult a cryptography expert while considering the choices and implementing them.</span>
6
6
7
-
####Why Mbed TLS needs entropy
7
+
### Why Mbed TLS needs entropy
8
8
9
9
Almost every cryptographic protocol requires random values that no one should be able to predict. A striking example is their use as session keys: It is easy to see that if an adversary can predict the session key, then he can decrypt the whole session. Even if the adversary can't predict it exactly, just with a relatively high probability, he can still recover the contents of the session. For example, if the adversary has a 0.00001% chance of predicting the 256 bit AES session key, then he can break it as easily as if we had used a 23 bit key (that is - very easily).
10
10
11
11
Creating session keys is only one use for random values; they have far more complicated applications. In these more complex use cases, the connection between the predictability of the values and the security of the protocol is not as obvious, but it is still crucial.
12
12
13
-
####Which entropy source to choose
13
+
### Which entropy source to choose
14
14
15
15
- If you have a target with a True Random Number Generator (TRNG), then follow Section 3 to allow Mbed TLS to use it.
16
16
17
17
- If you have a target without a TRNG, but with a non-volatile (NV) storage, then read Section 4 for instructions on making Mbed TLS use a random seed as entropy. This seed should be separately initialized with a true random number for each device at manufacturing time.
18
18
19
19
- If you just want to test Mbed TLS on your target without implementing either of the above, and having no security at all is acceptable, then go to Section 5.
20
20
21
-
####How to provide Mbed TLS entropy from a hardware entropy source
21
+
### How to provide Mbed TLS entropy from a hardware entropy source
22
22
23
-
#####What kind of a source you can add
23
+
#### What kind of a source you can add
24
24
25
25
It is important that you only add a TRNG as described in this section. For the purposes of this document a device is considered a TRNG only if:
26
26
@@ -32,7 +32,7 @@ It is important that you only add a TRNG as described in this section. For the p
32
32
33
33
For example, an integrated circuit extracting statistically random data from two oscillators of unknown frequencies and independent phases is considered a TRNG, but anything derived from a real time clock is NOT.
34
34
35
-
#####How to add an entropy source
35
+
#### How to add an entropy source
36
36
37
37
Mbed TLS distinguishes between strong and weak entropy sources. Of the sources registered by default, two are strong: /dev/urandom and Windows CryptoAPI. However, these resources are not available on many embedded platforms, and the default behaviour of Mbed TLS is to refuse to work if there are no strong sources present. To get around this, Mbed TLS assumes that the hardware entropy source you register (as explained in this section) is a TRNG and thus treats it as strong.
38
38
@@ -43,15 +43,15 @@ The preferred way to provide a custom entropy source:
43
43
44
44
The next two sections explain how to do this.
45
45
46
-
####How to implement the TRNG API
46
+
### How to implement the TRNG API
47
47
48
48
The implementation of this interface has to be located in the Arm Mbed OS directory specific to your target. The name of this directory is of the form `targets/.../TARGET_<target name>`. For example, in the case of K64F targets, it is `targets/TARGET_Freescale/TARGET_KSDK2_MCUS/TARGET_MCU_K64F/`.
49
49
50
-
#####Data structure
50
+
#### Data structure
51
51
52
52
You have to define a structure `trng_s` that holds all the information needed to operate the peripheral and describe its state.
53
53
54
-
#####Initialization and release
54
+
#### Initialization and release
55
55
56
56
To enable initializing and releasing the peripheral, you must implement the following functions:
57
57
@@ -60,7 +60,7 @@ void trng_init(trng_t *obj);
60
60
void trng_free(trng_t *obj);
61
61
```
62
62
63
-
##### The entropy collector function
63
+
#### The entropy collector function
64
64
65
65
The function `trng_get_bytes()` serves as the primary interface to the entropy source. It is expected to load the collected entropy to the buffer and is declared as follows:
-``size_t *output_length``: the length of the data written into the output buffer. It tells the caller how much entropy has been collected and how many bytes of the output buffer it can use. It should always reflect the exact amount of entropy collected; setting it higher than the actual number of bytes collected is a serious security risk.
80
80
81
-
82
-
##### Indicating the presence of a TRNG
81
+
#### Indicating the presence of a TRNG
83
82
84
83
To indicate that the target has an entropy source, you have to add `TRNG` to the capabilities of the target in `targets/targets.json`:
85
84
86
85
```
87
86
"device_has": ["TRNG", etc.]
88
87
```
89
88
90
-
####How to implement the non-volatile seed entropy source
89
+
### How to implement the non-volatile seed entropy source
91
90
92
91
If a hardware platform does not have a hardware entropy source to leverage into the entropy pool, alternatives have to be considered. As stated above, a strong entropy source is crucial for security of cryptographic and TLS operations. For platforms that support non-volatile memory, an option is to use the NV seed entropy source that Mbed TLS provides.
93
92
94
93
This makes Mbed TLS use a fixed amount of entropy as a seed and update this seed each time entropy is gathered with an Mbed TLS entropy collector for the first time. In a simple case it means that the seed is updated after reset at the start of the first TLS connection.
95
94
96
95
<spanclass="notes">**Note:** To make this option a relatively strong compromize, the seed should be initialized separately for each device with true random data at manufacturing time. It has to be true random data, something dependant on, for example the serial number is **not** secure. </span>
97
96
98
-
#####Enabling NV seed entropy source support
97
+
#### Enabling NV seed entropy source support
99
98
100
99
To enable the NV seed entropy source, you have to add `MBEDTLS_ENTROPY_NV_SEED` to your macros in `targets.json`:
101
100
@@ -107,7 +106,7 @@ This ensures the entropy pool knows it can use the NV seed entropy source.
107
106
108
107
By default the platform adaptation functions write/read a seed file called *seedfile*. If you have a system that does not support regular POSIX file operations (Arm Mbed OS does not support them by default), the default platform-adaptation functions will not be useful to you, and you will need to provide platform-adaptation functions (see next section).
109
108
110
-
#####Providing platform-adaptation functions
109
+
#### Providing platform-adaptation functions
111
110
112
111
The NV seed entropy source needs to know how to retrieve and store the seed in non-volatile memory. So in order to make the NV seed entropy source work, two platform-layer functions need to be provided.
113
112
@@ -126,11 +125,11 @@ There are three methods for setting those functions pointers (similar to all pla
126
125
*`MBEDTLS_PLATFORM_STD_NV_SEED_READ` and `MBEDTLS_PLATFORM_STD_NV_SEED_WRITE` (requires `MBEDTLS_PLATFORM_NV_SEED_ALT`). By setting these two macros to the relevant function names, the default read/write functions are replaced at compile-time, and you still have the option to replace them at runtime as well.
127
126
*`MBEDTLS_PLATFORM_NV_SEED_READ_MACRO` and `MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO`. By setting these two macros to the relevant functions names, the read/write functions are replaced at compile-time.
128
127
129
-
####How to test without entropy sources
128
+
### How to test without entropy sources
130
129
131
130
Both of the above options are secure if done properly, and depending on the platform may need more or less development work. In some cases it may be necessary to test Mbed TLS on boards without entropy. For these kinds of scenarios, Mbed TLS provides a compile time switch to enable testing without entropy sources.
132
131
133
-
#####Setting the macros
132
+
#### Setting the macros
134
133
135
134
This option is very dangerous because compiling with it results in a build that is not secure! You have to let Mbed TLS know that you are using it deliberately and you are aware of the consequences. That is why you have to turn off any entropy sources explicitly first.
0 commit comments