Mayank Gupta / Mbed OS pelion-example-frdm

Dependencies:   FXAS21002 FXOS8700Q

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pal_entropy_test.c Source File

pal_entropy_test.c

00001 // ----------------------------------------------------------------------------
00002 // Copyright 2019 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #include "pal.h"
00020 #include "unity.h"
00021 #include "unity_fixture.h"
00022 #include "pal_plat_entropy.h"
00023 #include "pal_sst.h"
00024 #ifdef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00025 #include "pal_sst.h"
00026 #else
00027 #include "sotp.h"
00028 #endif
00029 #include <stdlib.h>
00030 
00031 
00032 TEST_GROUP(pal_entropy);
00033 
00034 
00035 TEST_SETUP(pal_entropy)
00036 {
00037     palStatus_t status = PAL_SUCCESS;
00038 
00039     //init pal
00040     status = pal_init();
00041     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00042 // reset storage before each tests to avoid possible RoT leftovers
00043 #ifdef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00044     pal_SSTReset ();
00045 #else
00046     sotp_reset();
00047 #endif //MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00048 }
00049 
00050 TEST_TEAR_DOWN(pal_entropy)
00051 {
00052 
00053 #ifdef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00054     pal_SSTReset ();
00055 #else
00056     sotp_reset();
00057 #endif //MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00058 
00059     pal_destroy();
00060 }
00061 
00062 /*! \brief Entropy and DRBG test when entropy is not needed for random number generation (TRNG exists)
00063  *
00064  * 
00065  *
00066 * | # |    Step                                                                        |   Expected  |
00067 * |---|--------------------------------------------------------------------------------|-------------|
00068 * | 1 | Inject entropy.                                                                | PAL_SUCCESS or PAL_ERR_NOT_SUPPORTED if compiled out |
00069 * | 2 | Generate a random buffer using `pal_osRandomBuffer`.                           | PAL_SUCCESS |
00070 */
00071 
00072 #if PAL_USE_HW_TRNG
00073 TEST(pal_entropy, inject)
00074 {
00075     palStatus_t status = PAL_SUCCESS;
00076     uint8_t entropy[PAL_PLAT_MAX_ENTROPY_SIZE + 1] = { 2 };
00077     uint8_t out_buf[64] = { 0 };
00078 
00079     /*#1*/
00080     status = pal_osEntropyInject(entropy, PAL_PLAT_MAX_ENTROPY_SIZE); 
00081 #ifdef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00082     TEST_ASSERT_EQUAL_HEX(PAL_ERR_NOT_SUPPORTED , status);
00083 #else 
00084     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00085 #endif
00086     /*#2*/
00087     status = pal_osRandomBuffer(out_buf, sizeof(out_buf));
00088     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00089 }
00090 
00091 #else // PAL_USE_HW_TRNG
00092 
00093 /*! \brief Entropy and DRBG test when entropy is needed for random number generation (no TRNG)
00094  *
00095  * 
00096  *
00097 * | # |    Step                                                                        |   Expected  |
00098 * |---|--------------------------------------------------------------------------------|-------------|
00099 * | 1 | No entropy, attempt to generate a random buffer and fail.                      | PAL_ERR_CTR_DRBG_NOT_SEEDED |
00100 * | 2 | Set an entropy that is too large and fail.                                     | PAL_ERR_ENTROPY_TOO_LARGE |
00101 * | 3 | Again - try to generate a random buffer and fail.                              | PAL_ERR_CTR_DRBG_NOT_SEEDED |
00102 * | 4 | Successfully inject the entropy.                                               | PAL_SUCCESS |
00103 * | 5 | Now that entropy exists, we may successfully generate a random buffer.         | PAL_SUCCESS |
00104 * | 6 | Fail to set the entropy a second time.                                         | PAL_ERR_ENTROPY_EXISTS |
00105 * | 7 | Delete the entropy by force, by resetting the storage.                         | PAL_SUCCESS |
00106 * | 8 | Set entropy again - will also reseed the DRBG since no entropy in storage.     | PAL_SUCCESS |
00107 * | 9 | Successfully generate a random buffer.                                         | PAL_SUCCESS |
00108  */
00109 TEST(pal_entropy, inject)
00110 {
00111     palStatus_t status = PAL_SUCCESS;
00112     uint8_t entropy[PAL_PLAT_MAX_ENTROPY_SIZE + 1] = { 2 };
00113     uint8_t out_buf[64] = { 0 };
00114 
00115     // No entropy, attempt to generate a random buffer and fail if no trng
00116     /*#1*/
00117     status = pal_osRandomBuffer(out_buf, sizeof(out_buf));
00118     TEST_ASSERT_EQUAL_HEX(PAL_ERR_CTR_DRBG_NOT_SEEDED, status);
00119 
00120     // Set an entropy that is too large and fail
00121     /*#2*/
00122     status = pal_osEntropyInject(entropy, PAL_PLAT_MAX_ENTROPY_SIZE + 1); 
00123     TEST_ASSERT_EQUAL_HEX(PAL_ERR_ENTROPY_TOO_LARGE, status);
00124 
00125     // Again - try to generate a random buffer and fail
00126     /*#3*/
00127     status = pal_osRandomBuffer(out_buf, sizeof(out_buf));
00128     TEST_ASSERT_EQUAL_HEX(PAL_ERR_CTR_DRBG_NOT_SEEDED, status);
00129 
00130     // Successfully inject the entropy
00131     /*#4*/
00132     status = pal_osEntropyInject(entropy, PAL_PLAT_MAX_ENTROPY_SIZE); 
00133     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00134 
00135     // Now that entropy exists, we may successfully generate a random buffer
00136     /*#5*/
00137     status = pal_osRandomBuffer(out_buf, sizeof(out_buf));
00138     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00139 
00140     // Fail to set the entropy a second time
00141     /*#6*/
00142     status = pal_osEntropyInject(entropy, PAL_PLAT_MAX_ENTROPY_SIZE); 
00143     TEST_ASSERT_EQUAL_HEX(PAL_ERR_ENTROPY_EXISTS, status);
00144 
00145     // Delete the entropy by force, now entropy will not exist anymore, yet DRBG will still be initialized and seeded
00146     /*#7*/
00147 #ifdef MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00148     status = pal_SSTReset ();
00149     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00150 #else
00151     sotp_result_e sotpResult = sotp_reset();
00152     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00153 #endif //MBED_CONF_MBED_CLOUD_CLIENT_EXTERNAL_SST_SUPPORT
00154 
00155     // Generating a random buffer should still succeed assuming it the reseeding interval has not been exceeded yet.
00156     // However we do not test this as it isn't really a well defined behavior
00157     // pal_status = pal_osRandomBuffer(out_buf, sizeof(out_buf));
00158     // TEST_ASSERT_EQUAL_INT(PAL_SUCCESS, pal_status);
00159 
00160     // Set entropy again, this will also reseed the DRBG since there is no entropy in the storage
00161     /*#8*/
00162     status = pal_osEntropyInject(entropy, PAL_PLAT_MAX_ENTROPY_SIZE); 
00163     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00164 
00165     // Successfully generate a random buffer
00166     /*#9*/
00167     status = pal_osRandomBuffer(out_buf, sizeof(out_buf));
00168     TEST_ASSERT_EQUAL_HEX(PAL_SUCCESS, status);
00169 }
00170 #endif // PAL_USE_HW_TRNG