# Developing Quantum Trusted Platform Module (QTPM) to Advance IoT Security
## 3 Quantum Trusted Platform Module (QTPM)
### QTPM Implementation  
The QTPM testbed is built with a Raspberry Pi 4 and an IDQ QRNG PCIe card, which is shown in Figure 1. The IDQ QRNG leverages the inherent unpredictability of quantum processes to generate true random numbers. The IDQ QRNG is certified to pass all major NIST randomness tests, ensuring a high level of security and unpredictability. One key advantage of QRNG is that, unlike PRNG, it is free from environmental biases that can affect the entropy source and reduce the randomness of generated numbers. The IDQ Quantis library is a software library that interfaces with and utilizes the IDQ QRNG card. The library supports multiple programming languages, including Java, C, C++, and C++11. However, it requires Ubuntu 18.04 due to the specific kernel requirements for the QRNG driver. During the installation process, an obsolete function within the source code file (xdma-core.c) has to be configured, which is the memory-mapped I/O write barrier. Both the EasyQuantis application and the Quantis library are recompiled using the buildx tool in Docker. Visual Studio Code (VSCode) (1.74.3) is used as the IDE for the integration. The C/C++ Microsoft extension and code runner are adopted to simplify compilation and debugging. The \"onRun\" function in the IBM TPM is responsible for calling the random number generation function during runtime. A customizable function is created to replace the \"onRun\" function to call the IDQ QRNG instead of the inbuilt PRNG when randomness is required. Furthermore, an additional function is developed that enables the switch between the QRNG and the default PRNG, thereby providing greater flexibility for the randomness source. In addition, a fallback mechanism is implemented in case the IDQ QRNG becomes unfunctional due to hardware or driver issues, and the TPM can revert to using the default PRNG without crashing or causing failures in security operations. Below is an example Listing 1.  
```
statictool_rcget_random_custom(ESYS_CONTEXT*ectx,unit8_t*custom_bytes,UINT16 custom_size){ tool_rcrc=tp2_getrandom(ectx,custom_size,&ctx.random_bytes,&ctx.cp_hash,&ctx.rp_hash,ctx.aux_session_handle[0],ctx.aux_session_handle[1],ctx.parameter_hash_algorithm); if(rc!=tool_rc_success){ LOG_ERR(''Failedgetrandom')'); returnrc; } //Replacethegeneratedrandombyteswithcustom_bytes memcpy(ctx.random_bytes->buffer, Quantis.Read(size)); ctx.random_bytes->size=custom_size; returnrc; }
```  
The Listing 1 is part of the TPM Software Stack (TSS), which is to generate custom size random bytes. This function takes three parameters:  
* _ectx_: It is a pointer to TPM's Enhanced System API and is responsible for executing commands.
* _custom_bytes_: It is a pointer to an array of 8-bit unsigned integers. This is where the generated random bytes are stored.
* _custom_size_: It is a 16-bit unsigned integer representing the number of random bytes to be generated.  
The tpm2_getrandom function generates random bytes by receiving all the necessary parameters for context, and the result is stored in the rc variable, which represents the return code. This is a static function that outputs a success or failure return code. Errorhandling is implemented to ensure the operation is successful. If the return code indicates failure, the system returns the message \"FAILED getrandom\". If the return code indicates success, the TPM's random bytes are replaced with those obtained from the Quantis library. The Quantis.Read() function is responsible for generating random raw bytes from the QRNG. It automatically generates the correct number of bytes by matching the size originally requested by the user, ensuring that the byte size requested and the output generated always correspond correctly.