DeterminentRandomSeed.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "crypto/random/test/DeterminentRandomSeed.h"
  16. #include "util/Bits.h"
  17. #include "util/Identity.h"
  18. /**
  19. * A random seed which always outputs the same number,
  20. * obviously you don't want to use this in the real world.
  21. */
  22. struct DeterminentRandomSeed {
  23. RandomSeed_t rs;
  24. uint8_t buff[64];
  25. Identity
  26. };
  27. static int get(RandomSeed_t* rand, uint64_t buff[8])
  28. {
  29. // chosen by fair dice roll, guaranteed random.
  30. struct DeterminentRandomSeed* drs = Identity_check((struct DeterminentRandomSeed*) rand);
  31. Bits_memcpy(buff, drs->buff, 64);
  32. return 0;
  33. }
  34. RandomSeed_t* DeterminentRandomSeed_new(struct Allocator* alloc, uint8_t buff[64])
  35. {
  36. struct DeterminentRandomSeed* drs = Allocator_clone(alloc, (&(struct DeterminentRandomSeed) {
  37. .rs = {
  38. .get = get,
  39. .name = "DeterminentRandomSeed TESTING ONLY, THIS SHOULD NEVER BE USED IN PRODUCTION!"
  40. }
  41. }));
  42. Identity_set(drs);
  43. if (buff) {
  44. Bits_memcpy(drs->buff, buff, 64);
  45. } else {
  46. Bits_memset(drs->buff, 4, 64);
  47. }
  48. return &drs->rs;
  49. }