no_prng.c 4.3 KB

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