Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

143 lines
3.8 KiB

  1. /*-
  2. * Copyright 2021 John-Mark Gurney.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  14. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  17. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  19. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  20. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. * SUCH DAMAGE.
  24. *
  25. */
  26. #include <strobe.h>
  27. #include <board/simpflash.h>
  28. #include <strobe_rng_init.h>
  29. #include "stm32f1xx.h"
  30. #include "stm32f1xx_hal.h"
  31. #include <stm32f1xx_hal_adc.h>
  32. #include <sysinit.h>
  33. #include <unistd.h>
  34. #define NBYTESENTROPY (256 / 8)
  35. #define DEFINE_RNG_SAVE 1
  36. #if DEFINE_RNG_SAVE
  37. const rng_word_t rng_save[roundup(NBYTESENTROPY, sizeof(rng_word_t)) / sizeof(rng_word_t)] __attribute__ ((section (".savestate")));
  38. #endif
  39. static void
  40. strobe_rng_init(void)
  41. {
  42. /*
  43. * Seed RNG
  44. * Seed w/ saved state.
  45. */
  46. strobe_seed_prng((uint8_t *)rng_save, sizeof rng_save);
  47. /*
  48. * On first boot, SRAM is uninitialized and randomness from
  49. * it is used.
  50. *
  51. * Note: This depends upon sbrk pointing to uinitalized SRAM.
  52. */
  53. strobe_seed_prng(sbrk(0), 2*1024);
  54. }
  55. SYSINIT_VF(rng_init, SI_SUB_HAL, SI_ORDER_LAST, strobe_rng_init);
  56. uint32_t times[20];
  57. static void
  58. analog_seed_rng(void)
  59. {
  60. ADC_HandleTypeDef ah;
  61. ADC_ChannelConfTypeDef chanconf;
  62. uint16_t v;
  63. int timespos;
  64. int i;
  65. /* Enable ADC1 Clock */
  66. __HAL_RCC_ADC1_CLK_ENABLE();
  67. /* Configure */
  68. ah = (ADC_HandleTypeDef){
  69. .Instance = ADC1,
  70. .Init.DataAlign = ADC_DATAALIGN_RIGHT,
  71. .Init.ScanConvMode = ADC_SCAN_DISABLE,
  72. .Init.ContinuousConvMode = DISABLE,
  73. .Init.NbrOfConversion = 1,
  74. .Init.DiscontinuousConvMode = DISABLE,
  75. .Init.NbrOfDiscConversion = 1,
  76. .Init.ExternalTrigConv = ADC_SOFTWARE_START,
  77. };
  78. HAL_ADC_Init(&ah);
  79. chanconf = (ADC_ChannelConfTypeDef){
  80. .Rank = ADC_REGULAR_RANK_1,
  81. .Channel = ADC_CHANNEL_TEMPSENSOR, /* temp sensor */
  82. .SamplingTime = ADC_SAMPLETIME_71CYCLES_5,
  83. };
  84. HAL_ADC_ConfigChannel(&ah, &chanconf);
  85. HAL_Delay(1);
  86. timespos = 0;
  87. /* Capture */
  88. for (i = 0; i < 256 / 2; i++) {
  89. times[timespos++] = HAL_GetTick();
  90. timespos %= sizeof times / sizeof *times;
  91. /*
  92. * Capture some ADC data. If pin is floating, 0xfff
  93. * happens frequently, if pin is grounded, 0 happens
  94. * frequently, filter these values out.
  95. */
  96. do {
  97. HAL_ADC_Start(&ah);
  98. HAL_ADC_PollForConversion(&ah, 1);
  99. v = HAL_ADC_GetValue(&ah);
  100. } while (v == 0 || v == 0xfff);
  101. strobe_seed_prng((uint8_t *)&v, sizeof v);
  102. }
  103. /* Deinit */
  104. HAL_ADC_DeInit(&ah);
  105. }
  106. SYSINIT_VF(analog_seed_rng, SI_SUB_HAL, SI_ORDER_LAST, analog_seed_rng);
  107. static void
  108. strobe_rng_save(void)
  109. {
  110. rng_word_t tmp[sizeof rng_save / sizeof(rng_word_t)];
  111. int r;
  112. /*
  113. * Save entropy for next reset.
  114. */
  115. r = strobe_randomize((uint8_t *)tmp, sizeof tmp);
  116. (void)r;
  117. doflash(rng_save, tmp, sizeof(rng_save));
  118. }
  119. SYSINIT_VF(rng_save, SI_SUB_LAST, SI_ORDER_LAST, strobe_rng_save);