ltc_prng.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copied from libtomcrypt/src/prngs/sprng.c and modified to
  2. * use Dropbear's genrandom(). */
  3. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  4. *
  5. * LibTomCrypt is a library that provides various cryptographic
  6. * algorithms in a highly modular and flexible manner.
  7. *
  8. * The library is free for all purposes without any express
  9. * guarantee it works.
  10. *
  11. * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.com
  12. */
  13. #include "includes.h"
  14. #include "dbrandom.h"
  15. #include "ltc_prng.h"
  16. /**
  17. @file sprng.c
  18. Secure PRNG, Tom St Denis
  19. */
  20. /* A secure PRNG using the RNG functions. Basically this is a
  21. * wrapper that allows you to use a secure RNG as a PRNG
  22. * in the various other functions.
  23. */
  24. #if DROPBEAR_LTC_PRNG
  25. /**
  26. Start the PRNG
  27. @param prng [out] The PRNG state to initialize
  28. @return CRYPT_OK if successful
  29. */
  30. int dropbear_prng_start(prng_state* UNUSED(prng))
  31. {
  32. return CRYPT_OK;
  33. }
  34. /**
  35. Add entropy to the PRNG state
  36. @param in The data to add
  37. @param inlen Length of the data to add
  38. @param prng PRNG state to update
  39. @return CRYPT_OK if successful
  40. */
  41. int dropbear_prng_add_entropy(const unsigned char* UNUSED(in), unsigned long UNUSED(inlen), prng_state* UNUSED(prng))
  42. {
  43. return CRYPT_OK;
  44. }
  45. /**
  46. Make the PRNG ready to read from
  47. @param prng The PRNG to make active
  48. @return CRYPT_OK if successful
  49. */
  50. int dropbear_prng_ready(prng_state* UNUSED(prng))
  51. {
  52. return CRYPT_OK;
  53. }
  54. /**
  55. Read from the PRNG
  56. @param out Destination
  57. @param outlen Length of output
  58. @param prng The active PRNG to read from
  59. @return Number of octets read
  60. */
  61. unsigned long dropbear_prng_read(unsigned char* out, unsigned long outlen, prng_state* UNUSED(prng))
  62. {
  63. LTC_ARGCHK(out != NULL);
  64. genrandom(out, outlen);
  65. return outlen;
  66. }
  67. /**
  68. Terminate the PRNG
  69. @param prng The PRNG to terminate
  70. @return CRYPT_OK if successful
  71. */
  72. int dropbear_prng_done(prng_state* UNUSED(prng))
  73. {
  74. return CRYPT_OK;
  75. }
  76. /**
  77. Export the PRNG state
  78. @param out [out] Destination
  79. @param outlen [in/out] Max size and resulting size of the state
  80. @param prng The PRNG to export
  81. @return CRYPT_OK if successful
  82. */
  83. int dropbear_prng_export(unsigned char* UNUSED(out), unsigned long* outlen, prng_state* UNUSED(prng))
  84. {
  85. LTC_ARGCHK(outlen != NULL);
  86. *outlen = 0;
  87. return CRYPT_OK;
  88. }
  89. /**
  90. Import a PRNG state
  91. @param in The PRNG state
  92. @param inlen Size of the state
  93. @param prng The PRNG to import
  94. @return CRYPT_OK if successful
  95. */
  96. int dropbear_prng_import(const unsigned char* UNUSED(in), unsigned long UNUSED(inlen), prng_state* UNUSED(prng))
  97. {
  98. return CRYPT_OK;
  99. }
  100. /**
  101. PRNG self-test
  102. @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
  103. */
  104. int dropbear_prng_test(void)
  105. {
  106. return CRYPT_OK;
  107. }
  108. const struct ltc_prng_descriptor dropbear_prng_desc =
  109. {
  110. "dropbear_prng", 0,
  111. dropbear_prng_start,
  112. dropbear_prng_add_entropy,
  113. dropbear_prng_ready,
  114. dropbear_prng_read,
  115. dropbear_prng_done,
  116. dropbear_prng_export,
  117. dropbear_prng_import,
  118. dropbear_prng_test
  119. };
  120. #endif /* DROPBEAR_LTC_PRNG */