2
0

no_prng.c 4.3 KB

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