wolf_main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* wolf_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 <wolfssl/wolfcrypt/random.h> /* for CUSTOM_RAND_TYPE */
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include <stdarg.h>
  26. #include <string.h>
  27. /* TIME CODE */
  28. /* TODO: Implement real RTC */
  29. /* Optionally you can define NO_ASN_TIME to disable all cert time checks */
  30. static int gTimeMs;
  31. static int hw_get_time_sec(void)
  32. {
  33. #warning Must implement your own time source if validating certificates
  34. return ++gTimeMs;
  35. }
  36. /* This is used by wolfCrypt asn.c for cert time checking */
  37. unsigned long my_time(unsigned long* timer)
  38. {
  39. (void)timer;
  40. return hw_get_time_sec();
  41. }
  42. #ifndef WOLFCRYPT_ONLY
  43. /* This is used by TLS only */
  44. unsigned int LowResTimer(void)
  45. {
  46. return hw_get_time_sec();
  47. }
  48. #endif
  49. #ifndef NO_CRYPT_BENCHMARK
  50. /* This is used by wolfCrypt benchmark tool only */
  51. double current_time(int reset)
  52. {
  53. double timeNow;
  54. int timeMs = gTimeMs;
  55. (void)reset;
  56. timeNow = (timeMs / 1000); // sec
  57. timeNow += (double)(timeMs % 1000) / 1000; // ms
  58. return timeNow;
  59. }
  60. #endif
  61. /* RNG CODE */
  62. /* TODO: Implement real RNG */
  63. static unsigned int gCounter;
  64. unsigned int hw_rand(void)
  65. {
  66. #warning Must implement your own random source
  67. return ++gCounter;
  68. }
  69. unsigned int my_rng_seed_gen(void)
  70. {
  71. return hw_rand();
  72. }
  73. int my_rng_gen_block(unsigned char* output, unsigned int sz)
  74. {
  75. uint32_t i = 0;
  76. while (i < sz)
  77. {
  78. /* If not aligned or there is odd/remainder */
  79. if( (i + sizeof(CUSTOM_RAND_TYPE)) > sz ||
  80. ((uint32_t)&output[i] % sizeof(CUSTOM_RAND_TYPE)) != 0
  81. ) {
  82. /* Single byte at a time */
  83. output[i++] = (unsigned char)my_rng_seed_gen();
  84. }
  85. else {
  86. /* Use native 8, 16, 32 or 64 copy instruction */
  87. *((CUSTOM_RAND_TYPE*)&output[i]) = my_rng_seed_gen();
  88. i += sizeof(CUSTOM_RAND_TYPE);
  89. }
  90. }
  91. return 0;
  92. }
  93. #ifdef XMALLOC_OVERRIDE
  94. void *myMalloc(size_t n, void* heap, int type)
  95. {
  96. (void)n;
  97. (void)heap;
  98. (void)type;
  99. #warning Must implement your own malloc
  100. return NULL;
  101. }
  102. void myFree(void *p, void* heap, int type)
  103. {
  104. (void)p;
  105. (void)heap;
  106. (void)type;
  107. #warning Must implement your own free
  108. }
  109. /* Required for normal math (!USE_FAST_MATH) */
  110. void *myRealloc(void *p, size_t n, void* heap, int type)
  111. {
  112. (void)p;
  113. (void)n;
  114. (void)heap;
  115. (void)type;
  116. #warning Must implement your own realloc
  117. return NULL;
  118. }
  119. #endif /* XMALLOC_OVERRIDE */