Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 1 | // Copyright lowRISC contributors. |
| 2 | // Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | |
| 5 | #include "sw/device/lib/dif/dif_entropy_src.h" |
| 6 | |
| 7 | #include "gtest/gtest.h" |
| 8 | #include "sw/device/lib/base/mmio.h" |
| 9 | #include "sw/device/lib/base/testing/mock_mmio.h" |
Timothy Trippel | c5f72c6 | 2021-09-28 22:37:11 +0000 | [diff] [blame] | 10 | #include "sw/device/lib/dif/dif_base.h" |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 11 | |
| 12 | #include "entropy_src_regs.h" // Generated |
| 13 | |
| 14 | namespace dif_entropy_src_unittest { |
| 15 | namespace { |
| 16 | |
| 17 | class DifEntropySrcTest : public testing::Test, public mock_mmio::MmioTest { |
| 18 | protected: |
Timothy Trippel | c5f72c6 | 2021-09-28 22:37:11 +0000 | [diff] [blame] | 19 | const dif_entropy_src_t entropy_src_ = {.base_addr = dev().region()}; |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 20 | }; |
| 21 | |
| 22 | class InitTest : public DifEntropySrcTest {}; |
| 23 | |
| 24 | TEST_F(InitTest, BadArgs) { |
Timothy Trippel | c5f72c6 | 2021-09-28 22:37:11 +0000 | [diff] [blame] | 25 | EXPECT_EQ(dif_entropy_src_init(dev().region(), nullptr), kDifBadArg); |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | TEST_F(InitTest, Init) { |
| 29 | dif_entropy_src_t entropy; |
Timothy Trippel | c5f72c6 | 2021-09-28 22:37:11 +0000 | [diff] [blame] | 30 | EXPECT_EQ(dif_entropy_src_init(dev().region(), &entropy), kDifOk); |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | class ConfigTest : public DifEntropySrcTest { |
| 34 | protected: |
| 35 | dif_entropy_src_config_t config_ = { |
Mark Branstad | 1f43bf5 | 2021-10-05 08:32:54 -0700 | [diff] [blame] | 36 | .mode = kDifEntropySrcModePtrng, |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 37 | .tests = {0}, |
| 38 | .reset_health_test_registers = false, |
| 39 | .single_bit_mode = kDifEntropySrcSingleBitModeDisabled, |
| 40 | .route_to_firmware = false, |
Timothy Chen | d2b4c59 | 2021-09-15 20:36:37 -0700 | [diff] [blame] | 41 | .fips_mode = false, |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 42 | .test_config = {0}, |
Miguel Osorio | 6cc7811 | 2021-09-28 16:45:05 -0700 | [diff] [blame] | 43 | .fw_override = |
| 44 | { |
| 45 | .enable = false, |
| 46 | .entropy_insert_enable = false, |
| 47 | .buffer_threshold = kDifEntropyFifoIntDefaultThreshold, |
| 48 | }, |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 49 | }; |
| 50 | }; |
| 51 | |
| 52 | TEST_F(ConfigTest, NullArgs) { |
Timothy Trippel | c5f72c6 | 2021-09-28 22:37:11 +0000 | [diff] [blame] | 53 | EXPECT_EQ(dif_entropy_src_configure(nullptr, {}), kDifBadArg); |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Miguel Osorio | 6cc7811 | 2021-09-28 16:45:05 -0700 | [diff] [blame] | 56 | TEST_F(ConfigTest, InvalidFifoThreshold) { |
| 57 | config_.fw_override.buffer_threshold = 65; |
| 58 | EXPECT_EQ(dif_entropy_src_configure(&entropy_src_, config_), kDifBadArg); |
| 59 | } |
| 60 | |
| 61 | TEST_F(ConfigTest, InvalidFwOverrideSettings) { |
| 62 | config_.fw_override.enable = false; |
| 63 | config_.fw_override.entropy_insert_enable = true; |
| 64 | EXPECT_EQ(dif_entropy_src_configure(&entropy_src_, config_), kDifBadArg); |
| 65 | } |
| 66 | |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 67 | struct ConfigParams { |
| 68 | dif_entropy_src_mode_t mode; |
| 69 | dif_entropy_src_single_bit_mode_t single_bit_mode; |
| 70 | bool route_to_firmware; |
| 71 | bool reset_health_test_registers; |
| 72 | |
| 73 | uint32_t expected_mode; |
| 74 | bool expected_rng_bit_en; |
| 75 | uint32_t expected_rng_sel; |
| 76 | uint32_t expected_seed; |
| 77 | }; |
| 78 | |
| 79 | class ConfigTestAllParams : public ConfigTest, |
| 80 | public testing::WithParamInterface<ConfigParams> {}; |
| 81 | |
| 82 | TEST_P(ConfigTestAllParams, ValidConfigurationMode) { |
| 83 | const ConfigParams &test_param = GetParam(); |
| 84 | config_.mode = test_param.mode; |
| 85 | config_.single_bit_mode = test_param.single_bit_mode; |
| 86 | config_.route_to_firmware = test_param.route_to_firmware; |
| 87 | config_.reset_health_test_registers = test_param.reset_health_test_registers; |
| 88 | |
Miguel Osorio | 6cc7811 | 2021-09-28 16:45:05 -0700 | [diff] [blame] | 89 | EXPECT_WRITE32(ENTROPY_SRC_OBSERVE_FIFO_THRESH_REG_OFFSET, |
| 90 | config_.fw_override.buffer_threshold); |
| 91 | EXPECT_WRITE32( |
| 92 | ENTROPY_SRC_FW_OV_CONTROL_REG_OFFSET, |
| 93 | { |
| 94 | {ENTROPY_SRC_FW_OV_CONTROL_FW_OV_MODE_OFFSET, |
| 95 | (uint32_t)(config_.fw_override.enable ? 0xa : 0x5)}, |
| 96 | {ENTROPY_SRC_FW_OV_CONTROL_FW_OV_ENTROPY_INSERT_OFFSET, |
| 97 | (uint32_t)(config_.fw_override.entropy_insert_enable ? 0xa : 0x5)}, |
| 98 | }); |
| 99 | |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 100 | EXPECT_WRITE32(ENTROPY_SRC_ENTROPY_CONTROL_REG_OFFSET, |
| 101 | { |
Miguel Osorio | cb6904b | 2021-07-27 13:08:48 -0700 | [diff] [blame] | 102 | {ENTROPY_SRC_ENTROPY_CONTROL_ES_ROUTE_OFFSET, |
| 103 | (uint32_t)(test_param.route_to_firmware ? 0xa : 0x5)}, |
| 104 | {ENTROPY_SRC_ENTROPY_CONTROL_ES_TYPE_OFFSET, 0x5}, |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 105 | }); |
Miguel Osorio | cb6904b | 2021-07-27 13:08:48 -0700 | [diff] [blame] | 106 | |
Timothy Chen | ab23a91 | 2021-09-07 15:00:34 -0700 | [diff] [blame] | 107 | // Current dif does not perform a read modified write |
| 108 | // EXPECT_READ32(ENTROPY_SRC_CONF_REG_OFFSET, 0); |
| 109 | |
Miguel Osorio | cb6904b | 2021-07-27 13:08:48 -0700 | [diff] [blame] | 110 | uint32_t rng_bit_enable = test_param.expected_rng_bit_en ? 0xa : 0x5; |
Timothy Chen | d2b4c59 | 2021-09-15 20:36:37 -0700 | [diff] [blame] | 111 | uint32_t route_to_fw = test_param.route_to_firmware ? 0xa : 0x5; |
Timothy Chen | ab23a91 | 2021-09-07 15:00:34 -0700 | [diff] [blame] | 112 | uint32_t enable = |
| 113 | test_param.expected_mode != kDifEntropySrcModeDisabled ? 0xa : 0x5; |
Miguel Osorio | 6cc7811 | 2021-09-28 16:45:05 -0700 | [diff] [blame] | 114 | |
Timothy Chen | ab23a91 | 2021-09-07 15:00:34 -0700 | [diff] [blame] | 115 | uint32_t reset_ht = test_param.reset_health_test_registers ? 0xa : 0x5; |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 116 | EXPECT_WRITE32( |
| 117 | ENTROPY_SRC_CONF_REG_OFFSET, |
| 118 | { |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 119 | {ENTROPY_SRC_CONF_RNG_BIT_SEL_OFFSET, test_param.expected_rng_sel}, |
Miguel Osorio | cb6904b | 2021-07-27 13:08:48 -0700 | [diff] [blame] | 120 | {ENTROPY_SRC_CONF_RNG_BIT_ENABLE_OFFSET, rng_bit_enable}, |
Timothy Chen | ab23a91 | 2021-09-07 15:00:34 -0700 | [diff] [blame] | 121 | {ENTROPY_SRC_CONF_HEALTH_TEST_CLR_OFFSET, reset_ht}, |
| 122 | {ENTROPY_SRC_CONF_BOOT_BYPASS_DISABLE_OFFSET, 0x5}, |
Timothy Chen | d2b4c59 | 2021-09-15 20:36:37 -0700 | [diff] [blame] | 123 | {ENTROPY_SRC_CONF_ENTROPY_DATA_REG_ENABLE_OFFSET, route_to_fw}, |
Timothy Chen | ab23a91 | 2021-09-07 15:00:34 -0700 | [diff] [blame] | 124 | {ENTROPY_SRC_CONF_ENABLE_OFFSET, enable}, |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 125 | }); |
| 126 | |
Timothy Trippel | c5f72c6 | 2021-09-28 22:37:11 +0000 | [diff] [blame] | 127 | EXPECT_EQ(dif_entropy_src_configure(&entropy_src_, config_), kDifOk); |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | INSTANTIATE_TEST_SUITE_P( |
| 131 | ConfigTestAllParams, ConfigTestAllParams, |
| 132 | testing::Values( |
| 133 | // Test entropy mode. |
| 134 | ConfigParams{kDifEntropySrcModeDisabled, |
Timothy Chen | ab23a91 | 2021-09-07 15:00:34 -0700 | [diff] [blame] | 135 | kDifEntropySrcSingleBitModeDisabled, false, false, |
| 136 | kDifEntropySrcModeDisabled, false, 0, 0}, |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 137 | ConfigParams{kDifEntropySrcModePtrng, |
| 138 | kDifEntropySrcSingleBitModeDisabled, false, false, 1, |
| 139 | false, 0, 0}, |
Mark Branstad | 1f43bf5 | 2021-10-05 08:32:54 -0700 | [diff] [blame] | 140 | ConfigParams{kDifEntropySrcModePtrng, |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 141 | kDifEntropySrcSingleBitModeDisabled, false, false, 2, |
| 142 | false, 0, 4}, |
| 143 | // Test route_to_firmware |
Mark Branstad | 1f43bf5 | 2021-10-05 08:32:54 -0700 | [diff] [blame] | 144 | ConfigParams{kDifEntropySrcModePtrng, |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 145 | kDifEntropySrcSingleBitModeDisabled, true, false, 2, false, |
| 146 | 0, 4}, |
| 147 | // Test reset_health_test_registers |
Mark Branstad | 1f43bf5 | 2021-10-05 08:32:54 -0700 | [diff] [blame] | 148 | ConfigParams{kDifEntropySrcModePtrng, |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 149 | kDifEntropySrcSingleBitModeDisabled, true, true, 2, false, |
| 150 | 0, 4}, |
| 151 | // Test single_bit_mode |
Timothy Chen | d2b4c59 | 2021-09-15 20:36:37 -0700 | [diff] [blame] | 152 | ConfigParams{kDifEntropySrcModePtrng, kDifEntropySrcSingleBitMode0, |
| 153 | true, true, 2, true, 0, 4}, |
| 154 | ConfigParams{kDifEntropySrcModePtrng, kDifEntropySrcSingleBitMode1, |
| 155 | true, true, 2, true, 1, 4}, |
| 156 | ConfigParams{kDifEntropySrcModePtrng, kDifEntropySrcSingleBitMode2, |
| 157 | true, true, 2, true, 2, 4}, |
| 158 | ConfigParams{kDifEntropySrcModePtrng, kDifEntropySrcSingleBitMode3, |
| 159 | true, true, 2, true, 3, 4})); |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 160 | |
| 161 | class ReadTest : public DifEntropySrcTest {}; |
| 162 | |
| 163 | TEST_F(ReadTest, EntropyBadArg) { |
| 164 | uint32_t word; |
Timothy Trippel | c5f72c6 | 2021-09-28 22:37:11 +0000 | [diff] [blame] | 165 | EXPECT_EQ(dif_entropy_src_read(nullptr, &word), kDifBadArg); |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | TEST_F(ReadTest, WordBadArg) { |
Timothy Trippel | c5f72c6 | 2021-09-28 22:37:11 +0000 | [diff] [blame] | 169 | EXPECT_EQ(dif_entropy_src_read(&entropy_src_, nullptr), kDifBadArg); |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | TEST_F(ReadTest, ReadDataUnAvailable) { |
| 173 | EXPECT_READ32(ENTROPY_SRC_INTR_STATE_REG_OFFSET, 0); |
| 174 | uint32_t got_word; |
Timothy Trippel | c5f72c6 | 2021-09-28 22:37:11 +0000 | [diff] [blame] | 175 | EXPECT_EQ(dif_entropy_src_read(&entropy_src_, &got_word), kDifUnavailable); |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | TEST_F(ReadTest, ReadOk) { |
| 179 | const uint32_t expected_word = 0x65585497; |
| 180 | EXPECT_READ32(ENTROPY_SRC_INTR_STATE_REG_OFFSET, |
| 181 | 1 << ENTROPY_SRC_INTR_STATE_ES_ENTROPY_VALID_BIT); |
| 182 | EXPECT_READ32(ENTROPY_SRC_ENTROPY_DATA_REG_OFFSET, expected_word); |
| 183 | EXPECT_READ32(ENTROPY_SRC_INTR_STATE_REG_OFFSET, |
| 184 | 1 << ENTROPY_SRC_INTR_STATE_ES_ENTROPY_VALID_BIT); |
| 185 | EXPECT_WRITE32(ENTROPY_SRC_INTR_STATE_REG_OFFSET, |
| 186 | {{ENTROPY_SRC_INTR_STATE_ES_ENTROPY_VALID_BIT, true}}); |
| 187 | |
| 188 | uint32_t got_word; |
Timothy Trippel | c5f72c6 | 2021-09-28 22:37:11 +0000 | [diff] [blame] | 189 | EXPECT_EQ(dif_entropy_src_read(&entropy_src_, &got_word), kDifOk); |
Timothy Trippel | e9c0624 | 2021-08-18 23:04:02 +0000 | [diff] [blame] | 190 | EXPECT_EQ(got_word, expected_word); |
| 191 | } |
| 192 | |
| 193 | } // namespace |
| 194 | } // namespace dif_entropy_src_unittest |