main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* main.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #include <wolfssl/wolfcrypt/settings.h>
  22. #include <wolfcrypt/test/test.h>
  23. #include <wolfcrypt/benchmark/benchmark.h>
  24. /* wolfCrypt_Init/wolfCrypt_Cleanup */
  25. #include <wolfssl/wolfcrypt/wc_port.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stdint.h>
  29. #ifndef __METAL_MACHINE_HEADER
  30. #define __METAL_MACHINE_HEADER "../../../../bsp/sifive-hifive1-revb/metal.h"
  31. #endif
  32. #include <metal/machine.h>
  33. #ifndef NO_CRYPT_BENCHMARK
  34. /*-specs=nano.specs doesn’t include support for floating point in printf()*/
  35. asm (".global _printf_float");
  36. #ifndef RTC_FREQ
  37. #define RTC_FREQ 32768UL
  38. #endif
  39. /* CLINT Registers (Core Local Interruptor) for time */
  40. #define CLINT_BASE 0x02000000UL
  41. #define CLINT_REG_MTIME (*((volatile uint32_t *)(CLINT_BASE + 0xBFF8)))
  42. #define WOLFSSL_SIFIVE_RISC_V_DEBUG 0
  43. double current_time(int reset)
  44. {
  45. double now = CLINT_REG_MTIME;
  46. (void)reset;
  47. return now/RTC_FREQ;
  48. }
  49. #endif /* !NO_CRYPT_BENCHMARK */
  50. #if WOLFSSL_SIFIVE_RISC_V_DEBUG
  51. void check(int depth) {
  52. char ch;
  53. char *ptr = malloc(1);
  54. printf("stack at %p, heap at %p\n", &ch, ptr);
  55. if (depth <= 0)
  56. return;
  57. check(depth-1);
  58. free(ptr);
  59. }
  60. void mtime_sleep(uint32_t ticks) {
  61. uint32_t start = CLINT_REG_MTIME;
  62. while((CLINT_REG_MTIME - start) < ticks) {
  63. }
  64. }
  65. void delay(uint32_t sec) {
  66. uint32_t ticks = sec * RTC_FREQ;
  67. mtime_sleep(ticks);
  68. }
  69. #endif /* WOLFSSL_SIFIVE_RISC_V_DEBUG */
  70. /* RNG CODE */
  71. /* TODO: Implement real RNG */
  72. static unsigned int gCounter;
  73. unsigned int hw_rand(void)
  74. {
  75. /* #warning Must implement your own random source */
  76. return ++gCounter;
  77. }
  78. unsigned int my_rng_seed_gen(void)
  79. {
  80. return hw_rand();
  81. }
  82. int my_rng_gen_block(unsigned char* output, unsigned int sz)
  83. {
  84. uint32_t i = 0;
  85. uint32_t randReturnSize = sizeof(CUSTOM_RAND_TYPE);
  86. while (i < sz)
  87. {
  88. /* If not aligned or there is odd/remainder */
  89. if((i + randReturnSize) > sz ||
  90. ((uint32_t)&output[i] % randReturnSize) != 0 ) {
  91. /* Single byte at a time */
  92. output[i++] = (unsigned char)my_rng_seed_gen();
  93. }
  94. else {
  95. /* Use native 8, 16, 32 or 64 copy instruction */
  96. *((CUSTOM_RAND_TYPE*)&output[i]) = my_rng_seed_gen();
  97. i += randReturnSize;
  98. }
  99. }
  100. return 0;
  101. }
  102. #if !defined(NO_CLOCK_SPEEDUP) && !defined(USE_CLOCK_HZ)
  103. /* 320MHz */
  104. #define USE_CLOCK_HZ 320000000UL
  105. #endif
  106. int main(void)
  107. {
  108. int ret;
  109. long clk_Hz = 16000000; /* default */
  110. #if WOLFSSL_SIFIVE_RISC_V_DEBUG
  111. printf("check stack and heap addresses\n");
  112. check(8);
  113. printf("sleep for 10 seconds to verify timer, measure using a stopwatch\n");
  114. delay(10);
  115. printf("awake after sleeping for 10 seconds\n");
  116. #endif
  117. #ifdef USE_CLOCK_HZ
  118. /* Speed up clock */
  119. printf("SiFive HiFive1 Demo\n");
  120. printf("Setting clock to %dMHz\n", (int)(USE_CLOCK_HZ/1000000));
  121. clk_Hz = metal_clock_set_rate_hz(
  122. &__METAL_DT_SIFIVE_FE310_G000_PLL_HANDLE->clock, USE_CLOCK_HZ
  123. );
  124. #endif
  125. printf("Actual Clock %dMHz\n", (int)(clk_Hz/1000000));
  126. /* Reconfigure the SPI Bus for dual mode */
  127. #define QSPI0_CTRL 0x10014000UL
  128. #define FESPI_REG_FFMT (*((volatile uint32_t *)(QSPI0_CTRL + 0x64)))
  129. FESPI_REG_FFMT = 0xbb1447;
  130. #ifdef DEBUG_WOLFSSL
  131. wolfSSL_Debugging_ON();
  132. #endif
  133. if ((ret = wolfCrypt_Init()) != 0) {
  134. printf("wolfCrypt_Init failed %d\n", ret);
  135. return -1;
  136. }
  137. #ifndef NO_CRYPT_TEST
  138. printf("\nwolfCrypt Test Started\n");
  139. wolfcrypt_test(NULL);
  140. printf("\nwolfCrypt Test Completed\n");
  141. #endif
  142. #ifndef NO_CRYPT_BENCHMARK
  143. printf("\nBenchmark Test Started\n");
  144. benchmark_test(NULL);
  145. printf("\nBenchmark Test Completed\n");
  146. #endif
  147. if ((ret = wolfCrypt_Cleanup()) != 0) {
  148. printf("wolfCrypt_Cleanup failed %d\n", ret);
  149. return -1;
  150. }
  151. return 0;
  152. }