no_prng.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  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. #include "tomcrypt.h"
  10. #include "tomcrypt_test.h"
  11. /**
  12. @file no_prng.c
  13. NO PRNG, Steffen Jaeckel
  14. */
  15. #ifdef LTC_PKCS_1
  16. typedef struct
  17. {
  18. struct ltc_prng_descriptor desc;
  19. char name[64];
  20. unsigned char entropy[1024];
  21. unsigned long len;
  22. unsigned long offset;
  23. } no_prng_desc_t;
  24. /**
  25. Start the PRNG
  26. @param prng [out] The PRNG state to initialize
  27. @return CRYPT_OK if successful
  28. */
  29. static int no_prng_start(prng_state *prng)
  30. {
  31. no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
  32. LTC_ARGCHK(no_prng != NULL);
  33. LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
  34. no_prng->len = 0;
  35. no_prng->offset = 0;
  36. return CRYPT_OK;
  37. }
  38. /**
  39. Add entropy to the PRNG state
  40. @param in The data to add
  41. @param inlen Length of the data to add
  42. @param prng PRNG state to update
  43. @return CRYPT_OK if successful
  44. */
  45. static int no_prng_add_entropy(const unsigned char *in, unsigned long inlen, prng_state *prng)
  46. {
  47. no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
  48. LTC_ARGCHK(no_prng != NULL);
  49. LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
  50. LTC_ARGCHK(in != NULL);
  51. LTC_ARGCHK(inlen <= sizeof(no_prng->entropy));
  52. no_prng->len = MIN(inlen, sizeof(no_prng->entropy));
  53. memcpy(no_prng->entropy, in, no_prng->len);
  54. no_prng->offset = 0;
  55. return CRYPT_OK;
  56. }
  57. /**
  58. Make the PRNG ready to read from
  59. @param prng The PRNG to make active
  60. @return CRYPT_OK if successful
  61. */
  62. static int no_prng_ready(prng_state *prng)
  63. {
  64. LTC_ARGCHK(prng != NULL);
  65. return CRYPT_OK;
  66. }
  67. /**
  68. Read from the PRNG
  69. @param out Destination
  70. @param outlen Length of output
  71. @param prng The active PRNG to read from
  72. @return Number of octets read
  73. */
  74. static unsigned long no_prng_read(unsigned char *out, unsigned long outlen, prng_state *prng)
  75. {
  76. no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
  77. LTC_ARGCHK(no_prng != NULL);
  78. LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
  79. LTC_ARGCHK(out != NULL);
  80. outlen = MIN(outlen, no_prng->len - no_prng->offset);
  81. memcpy(out, &no_prng->entropy[no_prng->offset], outlen);
  82. no_prng->offset += outlen;
  83. return outlen;
  84. }
  85. /**
  86. Terminate the PRNG
  87. @param prng The PRNG to terminate
  88. @return CRYPT_OK if successful
  89. */
  90. static int no_prng_done(prng_state *prng)
  91. {
  92. LTC_UNUSED_PARAM(prng);
  93. return CRYPT_OK;
  94. }
  95. /**
  96. Export the PRNG state
  97. @param out [out] Destination
  98. @param outlen [in/out] Max size and resulting size of the state
  99. @param prng The PRNG to export
  100. @return CRYPT_OK if successful
  101. */
  102. static int no_prng_export(unsigned char *out, unsigned long *outlen, prng_state *prng)
  103. {
  104. LTC_UNUSED_PARAM(out);
  105. LTC_UNUSED_PARAM(outlen);
  106. LTC_UNUSED_PARAM(prng);
  107. return CRYPT_OK;
  108. }
  109. /**
  110. Import a PRNG state
  111. @param in The PRNG state
  112. @param inlen Size of the state
  113. @param prng The PRNG to import
  114. @return CRYPT_OK if successful
  115. */
  116. static int no_prng_import(const unsigned char *in, unsigned long inlen, prng_state *prng)
  117. {
  118. LTC_UNUSED_PARAM(in);
  119. LTC_UNUSED_PARAM(inlen);
  120. LTC_UNUSED_PARAM(prng);
  121. return CRYPT_OK;
  122. }
  123. /**
  124. PRNG self-test
  125. @return CRYPT_OK if successful, CRYPT_NOP if self-testing has been disabled
  126. */
  127. static int no_prng_test(void)
  128. {
  129. return CRYPT_OK;
  130. }
  131. static const struct ltc_prng_descriptor no_prng_desc =
  132. {
  133. NULL, 0,
  134. &no_prng_start,
  135. &no_prng_add_entropy,
  136. &no_prng_ready,
  137. &no_prng_read,
  138. &no_prng_done,
  139. &no_prng_export,
  140. &no_prng_import,
  141. &no_prng_test
  142. };
  143. struct ltc_prng_descriptor* no_prng_desc_get(void)
  144. {
  145. no_prng_desc_t* no_prng = XMALLOC(sizeof(*no_prng));
  146. LTC_ARGCHK(no_prng != NULL);
  147. XMEMCPY(&no_prng->desc, &no_prng_desc, sizeof(no_prng_desc));
  148. LTC_ARGCHK(snprintf(no_prng->name, sizeof(no_prng->name), "no_prng@%p", no_prng) < (int)sizeof(no_prng->name));
  149. no_prng->desc.name = no_prng->name;
  150. return &no_prng->desc;
  151. }
  152. void no_prng_desc_free(struct ltc_prng_descriptor* prng)
  153. {
  154. no_prng_desc_t *no_prng = (no_prng_desc_t*) prng;
  155. LTC_ARGCHK(no_prng != NULL);
  156. LTC_ARGCHK(no_prng->name == (char*)no_prng + offsetof(no_prng_desc_t, name));
  157. XFREE(no_prng);
  158. }
  159. #endif
  160. /* ref: $Format:%D$ */
  161. /* git commit: $Format:%H$ */
  162. /* commit time: $Format:%ai$ */