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/advanced/tls_porting.md
+27-25Lines changed: 27 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -2,32 +2,35 @@
2
2
3
3
This document explains how to port [mbed TLS](https://github.com/ARMmbed/mbedtls) to a new mbed development board.
4
4
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>
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
7
## 1. Why mbed TLS needs entropy
8
8
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).
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
-
Creating session keys is only one use for random values; they have far more complicated applications. And although in these more complex use cases the connection between the predictability of the values and the security of the protocol is not so obvious, it is no less crucial.
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
13
## 2. 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
-
- If you have a target without a TRNG, but with a non-volatile 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 manufacture time.
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
21
## 3. How to provide mbed TLS entropy from a hardware entropy source
22
22
23
23
### 3.1 What kind of a source can be added
24
24
25
-
It is very 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
-
- It is dedicated to generating entropy to be used in cryptographic applications.
27
-
- Careful consideration has been given to how much the data generated is subject to adversarial manipulation.
28
-
- A thorough engineering study has been made to determine how much entropy it can actually provide.
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:
29
26
30
-
For example, an integrated circuit extracting statistically random data from two oscillators of unknown frequencies and independent phases is considered a TRNG, while anything derived from a real time clock is NOT.
27
+
- It is dedicated to generating entropy to be used in cryptographic applications.
28
+
29
+
- Careful consideration has been given to how much the data generated is subject to adversarial manipulation.
30
+
31
+
- A thorough engineering study has been made to determine how much entropy it can actually provide.
32
+
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.
31
34
32
35
### 3.2 How to add an entropy source
33
36
@@ -42,15 +45,15 @@ The next two sections explain how to do this.
42
45
43
46
## How to implement the TRNG API
44
47
45
-
The implementation of this interface has to be located in the 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/`.
48
+
The implementation of this interface has to be located in the 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/`.
46
49
47
50
### Data structure
48
51
49
52
You have to define a structure `trng_s` that holds all the information needed to operate the peripheral and describe its state.
50
53
51
-
### Initialisation and release
54
+
### Initialization and release
52
55
53
-
To enable initializing and releasing the peripherial, you must implement the following functions:
56
+
To enable initializing and releasing the peripheral, you must implement the following functions:
54
57
55
58
```C
56
59
voidtrng_init(trng_t *obj);
@@ -65,15 +68,15 @@ The function `trng_get_bytes()` serves as the primary interface to the entropy s
65
68
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length);
66
69
```
67
70
68
-
-``trng_t *obj``: `trng_t` is an alias to `trng_s` and it is the caller's responsibility to initialize it before passing it to this function and release it (with the help of `trng_init()` and `trng_free()` respectively) when it is not required anymore.
71
+
-``trng_t *obj``: `trng_t` is an alias to `trng_s`, and it is the caller's responsibility to initialize it before passing it to this function and release it (with the help of `trng_init()` and `trng_free()`, respectively) when it is not required anymore.
69
72
70
-
-``uint8_t *output``: A pointer to the output buffer. The function should write the entropy it collected to the buffer; mbed TLS then uses this data as entropy. Please consult your board's manual and write only the strongest entropy possible in this buffer.
73
+
-``uint8_t *output``: a pointer to the output buffer. The function should write the entropy it collected to the buffer; mbed TLS then uses this data as entropy. Please consult your board's manual, and write only the strongest entropy possible in this buffer.
71
74
72
-
**Warning:** Although it is possible to fill this buffer without a strong hardware entropy source, we strongly advise against it, because it will nullify any security provided by mbed TLS.
75
+
**Warning:** Although it is possible to fill this buffer without a strong hardware entropy source, we strongly advise against it because it will nullify any security provided by mbed TLS.
73
76
74
-
-``size_t length``: The length of the output buffer. The function shouldn't write more data than this to the output buffer under any circumstances.
77
+
-``size_t length``: the length of the output buffer. The function shouldn't write more data than this to the output buffer under any circumstances.
75
78
76
-
-``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.
79
+
-``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.
77
80
78
81
79
82
### Indicating the presence of a TRNG
@@ -84,7 +87,7 @@ To indicate that the target has an entropy source, you have to add `TRNG` to the
84
87
"device_has": ["TRNG", etc.]
85
88
```
86
89
87
-
## 4. How to implement the Non-Volatile seed entropy source
90
+
## 4. How to implement the non-volatile seed entropy source
88
91
89
92
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 is provided with mbed TLS.
90
93
@@ -104,7 +107,7 @@ This ensures the entropy pool knows it can use the NV seed entropy source.
104
107
105
108
You can read more about how to add a macro for your target [here](mbed_targets.md).
106
109
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 (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).
110
+
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 (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).
108
111
109
112
### 5. Providing platform-adaptation functions
110
113
@@ -121,19 +124,19 @@ Where `buf` is a pointer to the buffer to read/write a seed, and `buf_len` is th
121
124
122
125
There are three methods for setting those functions pointers (similar to all platform adaptation functions in mbed TLS):
123
126
124
-
*`MBEDTLS_PLATFORM_NV_SEED_ALT`. By enabling this macro, the `mbedtls_platform_set_nv_seed(nv_seed_read_func *, nv_seed_write_func*)` function becomes available and lets you set the pointers at run-time.
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 run-time as well.
127
+
*`MBEDTLS_PLATFORM_NV_SEED_ALT`. By enabling this macro, the `mbedtls_platform_set_nv_seed(nv_seed_read_func *, nv_seed_write_func*)` function becomes available and lets you set the pointers at runtime.
128
+
*`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.
126
129
*`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.
127
130
128
131
## How to test without entropy sources
129
132
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 kind of scenarios mbed TLS provides a compile time switch to enable testing without entropy sources.
133
+
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.
131
134
132
135
### Setting the macros
133
136
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.
137
+
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.
135
138
136
-
Since it is a very dangerous option and no one should use it in production, we recommend to limit its scope as much as possible; you should apply these settings to the application specific configuration file, instead of the target related configuration as we did it above. You can read more about how to add a macro for your application [here](config_system.md).
139
+
Because it is a very dangerous option and no one should use it in production, we recommend you limit its scope as much as possible; you should apply these settings to the application specific configuration file, instead of the target related configuration as we did it above. You can read more about how to add a macro for your application [here](config_system.md).
137
140
138
141
To turn the unsafe testing mode on:
139
142
@@ -145,4 +148,3 @@ To turn the unsafe testing mode on:
145
148
146
149
### Warning!
147
150
_**The `MBEDTLS_TEST_NULL_ENTROPY` option nullifies any security provided by mbed TLS! It is there exclusively for testing purposes and should never be used in production. It cannot be stressed enough: a library built with this option does not provide any security whatsoever!**_
0 commit comments