2
0

no_prng.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* LibTomCrypt, modular cryptographic library
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. *
  9. * http://libtom.org
  10. */
  11. #include "tomcrypt.h"
  12. /**
  13. @file no_prng.c
  14. NO PRNG, Steffen Jaeckel
  15. */
  16. #ifdef LTC_PKCS_1
  17. static unsigned char no_prng_entropy[1024];
  18. static unsigned long no_prng_len = 0;
  19. static unsigned long no_prng_offset = 0;
  20. /**
  21. Start the PRNG
  22. @param prng [out] The PRNG state to initialize
  23. @return CRYPT_OK if successful
  24. */
  25. int no_prng_start(prng_state *prng)
  26. {
  27. LTC_UNUSED_PARAM(prng);
  28. no_prng_len = 0;
  29. no_prng_offset = 0;
  30. return CRYPT_OK;
  31. }
  32. /**
  33. Add entropy to the PRNG state
  34. @param in The data to add
  35. @param inlen Length of the data to add
  36. @param prng PRNG state to update
  37. @return CRYPT_OK if successful
  38. */
  39. int no_prng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
  40. {
  41. LTC_UNUSED_PARAM(prng);
  42. LTC_ARGCHK(in != NULL);
  43. LTC_ARGCHK(inlen <= sizeof(no_prng_entropy));
  44. no_prng_len = MIN(inlen, sizeof(no_prng_entropy));
  45. memcpy(no_prng_entropy, in, no_prng_len);
  46. no_prng_offset = 0;
  47. return CRYPT_OK;
  48. }
  49. /**
  50. Make the PRNG ready to read from
  51. @param prng The PRNG to make active
  52. @return CRYPT_OK if successful
  53. */
  54. int no_prng_ready(prng_state *prng)
  55. {
  56. LTC_ARGCHK(prng != NULL);
  57. return CRYPT_OK;
  58. }
  59. /**
  60. Read from the PRNG
  61. @param out Destination
  62. @param outlen Length of output
  63. @param prng The active PRNG to read from
  64. @return Number of octets read
  65. */
  66. unsigned long no_prng_read(unsigned char *out, unsigned long outlen, prng_state *prng)
  67. {
  68. LTC_UNUSED_PARAM(prng);
  69. LTC_ARGCHK(out != NULL);
  70. outlen = MIN(outlen, no_prng_len - no_prng_offset);
  71. memcpy(out, &no_prng_entropy[no_prng_offset], outlen);
  72. no_prng_offset += outlen;
  73. return outlen;
  74. }
  75. /**
  76. Terminate the PRNG
  77. @param prng The PRNG to terminate
  78. @return CRYPT_OK if successful
  79. */
  80. int no_prng_done(prng_state *prng)
  81. {
  82. LTC_UNUSED_PARAM(prng);
  83. return CRYPT_OK;
  84. }
  85. /**
  86. Export the PRNG state
  87. @param out [out] Destination
  88. @param outlen [in/out] Max size and resulting size of the state
  89. @param prng The PRNG to export
  90. @return CRYPT_OK if successful
  91. */
  92. int no_prng_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
  93. {
  94. LTC_UNUSED_PARAM(out);
  95. LTC_UNUSED_PARAM(outlen);
  96. LTC_UNUSED_PARAM(prng);
  97. return CRYPT_OK;
  98. }
  99. /**
  100. Import a PRNG state
  101. @param in The PRNG state
  102. @param inlen Size of the state
  103. @param prng The PRNG to import
  104. @return CRYPT_OK if successful
  105. */
  106. int no_prng_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
  107. {
  108. LTC_UNUSED_PARAM(in);
  109. LTC_UNUSED_PARAM(inlen);
  110. LTC_UNUSED_PARAM(prng);
  111. return CRYPT_OK;
  112. }
  113. /**
  114. PRNG self-test
  115. @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
  116. */
  117. int no_prng_test(void)
  118. {
  119. return CRYPT_OK;
  120. }
  121. const struct ltc_prng_descriptor no_prng_desc =
  122. {
  123. "no_prng", 0,
  124. &no_prng_start,
  125. &no_prng_add_entropy,
  126. &no_prng_ready,
  127. &no_prng_read,
  128. &no_prng_done,
  129. &no_prng_export,
  130. &no_prng_import,
  131. &no_prng_test
  132. };
  133. #endif
  134. /* $Source$ */
  135. /* $Revision$ */
  136. /* $Date$ */