random.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2014-2018 K. Lange
  5. *
  6. * Provides access to the kernel RNG
  7. *
  8. */
  9. #include <kernel/system.h>
  10. #include <kernel/logging.h>
  11. #include <kernel/fs.h>
  12. #include <kernel/module.h>
  13. static uint32_t read_random(fs_node_t *node, uint64_t offset, uint32_t size, uint8_t *buffer) {
  14. uint32_t s = 0;
  15. while (s < size) {
  16. buffer[s] = krand() % 0xFF;
  17. s++;
  18. }
  19. return size;
  20. }
  21. static uint32_t write_random(fs_node_t *node, uint64_t offset, uint32_t size, uint8_t *buffer) {
  22. return size;
  23. }
  24. static void open_random(fs_node_t * node, unsigned int flags) {
  25. return;
  26. }
  27. static void close_random(fs_node_t * node) {
  28. return;
  29. }
  30. static fs_node_t * random_device_create(void) {
  31. fs_node_t * fnode = malloc(sizeof(fs_node_t));
  32. memset(fnode, 0x00, sizeof(fs_node_t));
  33. fnode->inode = 0;
  34. strcpy(fnode->name, "random");
  35. fnode->uid = 0;
  36. fnode->gid = 0;
  37. fnode->mask = 0444;
  38. fnode->length = 1024;
  39. fnode->flags = FS_CHARDEVICE;
  40. fnode->read = read_random;
  41. fnode->write = write_random;
  42. fnode->open = open_random;
  43. fnode->close = close_random;
  44. fnode->readdir = NULL;
  45. fnode->finddir = NULL;
  46. fnode->ioctl = NULL;
  47. return fnode;
  48. }
  49. static int random_initialize(void) {
  50. vfs_mount("/dev/random", random_device_create());
  51. vfs_mount("/dev/urandom", random_device_create());
  52. return 0;
  53. }
  54. static int random_finalize(void) {
  55. return 0;
  56. }
  57. MODULE_DEF(random, random_initialize, random_finalize);