I2C toy code
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

186 строки
4.4 KiB

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