ltc_prng.c 3.1 KB

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