random.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* random.h
  2. *
  3. * Copyright (C) 2006-2014 wolfSSL Inc.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL 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. * CyaSSL 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-1301, USA
  20. */
  21. #ifndef CTAO_CRYPT_RANDOM_H
  22. #define CTAO_CRYPT_RANDOM_H
  23. #include <cyassl/ctaocrypt/types.h>
  24. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  25. #ifdef NO_SHA256
  26. #error "Hash DRBG requires SHA-256."
  27. #endif /* NO_SHA256 */
  28. #include <cyassl/ctaocrypt/sha256.h>
  29. #else /* HAVE_HASHDRBG || NO_RC4 */
  30. #include <cyassl/ctaocrypt/arc4.h>
  31. #endif /* HAVE_HASHDRBG || NO_RC4 */
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. #if defined(USE_WINDOWS_API)
  36. #if defined(_WIN64)
  37. typedef unsigned __int64 ProviderHandle;
  38. /* type HCRYPTPROV, avoid #include <windows.h> */
  39. #else
  40. typedef unsigned long ProviderHandle;
  41. #endif
  42. #endif
  43. /* OS specific seeder */
  44. typedef struct OS_Seed {
  45. #if defined(USE_WINDOWS_API)
  46. ProviderHandle handle;
  47. #else
  48. int fd;
  49. #endif
  50. } OS_Seed;
  51. CYASSL_LOCAL
  52. int GenerateSeed(OS_Seed* os, byte* seed, word32 sz);
  53. #if defined(CYASSL_MDK_ARM)
  54. #undef RNG
  55. #define RNG CyaSSL_RNG /* for avoiding name conflict in "stm32f2xx.h" */
  56. #endif
  57. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  58. #define DRBG_SEED_LEN (440/8)
  59. struct DRBG; /* Private DRBG state */
  60. /* Hash-based Deterministic Random Bit Generator */
  61. typedef struct RNG {
  62. OS_Seed seed;
  63. struct DRBG* drbg;
  64. byte status;
  65. } RNG;
  66. #else /* HAVE_HASHDRBG || NO_RC4 */
  67. #define CYASSL_RNG_CAVIUM_MAGIC 0xBEEF0004
  68. /* secure Random Number Generator */
  69. typedef struct RNG {
  70. OS_Seed seed;
  71. Arc4 cipher;
  72. #ifdef HAVE_CAVIUM
  73. int devId; /* nitrox device id */
  74. word32 magic; /* using cavium magic */
  75. #endif
  76. } RNG;
  77. #ifdef HAVE_CAVIUM
  78. CYASSL_API int InitRngCavium(RNG*, int);
  79. #endif
  80. #endif /* HAVE_HASH_DRBG || NO_RC4 */
  81. CYASSL_API int InitRng(RNG*);
  82. CYASSL_API int RNG_GenerateBlock(RNG*, byte*, word32 sz);
  83. CYASSL_API int RNG_GenerateByte(RNG*, byte*);
  84. #if defined(HAVE_HASHDRBG) || defined(NO_RC4)
  85. CYASSL_API int FreeRng(RNG*);
  86. CYASSL_API int RNG_HealthTest(int reseed,
  87. const byte* entropyA, word32 entropyASz,
  88. const byte* entropyB, word32 entropyBSz,
  89. byte* output, word32 outputSz);
  90. #endif /* HAVE_HASHDRBG || NO_RC4 */
  91. #ifdef HAVE_FIPS
  92. /* fips wrapper calls, user can call direct */
  93. CYASSL_API int InitRng_fips(RNG* rng);
  94. CYASSL_API int FreeRng_fips(RNG* rng);
  95. CYASSL_API int RNG_GenerateBlock_fips(RNG* rng, byte* buf, word32 bufSz);
  96. CYASSL_API int RNG_HealthTest_fips(int reseed,
  97. const byte* entropyA, word32 entropyASz,
  98. const byte* entropyB, word32 entropyBSz,
  99. byte* output, word32 outputSz);
  100. #ifndef FIPS_NO_WRAPPERS
  101. /* if not impl or fips.c impl wrapper force fips calls if fips build */
  102. #define InitRng InitRng_fips
  103. #define FreeRng FreeRng_fips
  104. #define RNG_GenerateBlock RNG_GenerateBlock_fips
  105. #define RNG_HealthTest RNG_HealthTest_fips
  106. #endif /* FIPS_NO_WRAPPERS */
  107. #endif /* HAVE_FIPS */
  108. #ifdef __cplusplus
  109. } /* extern "C" */
  110. #endif
  111. #endif /* CTAO_CRYPT_RANDOM_H */