random.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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, uint32_t offset, uint32_t size, uint8_t *buffer);
  14. static uint32_t write_random(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer);
  15. static void open_random(fs_node_t *node, unsigned int flags);
  16. static void close_random(fs_node_t *node);
  17. static uint32_t read_random(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
  18. uint32_t s = 0;
  19. while (s < size) {
  20. buffer[s] = krand() % 0xFF;
  21. offset++;
  22. s++;
  23. }
  24. return size;
  25. }
  26. static uint32_t write_random(fs_node_t *node, uint32_t offset, uint32_t size, uint8_t *buffer) {
  27. return size;
  28. }
  29. static void open_random(fs_node_t * node, unsigned int flags) {
  30. return;
  31. }
  32. static void close_random(fs_node_t * node) {
  33. return;
  34. }
  35. static fs_node_t * random_device_create(void) {
  36. fs_node_t * fnode = malloc(sizeof(fs_node_t));
  37. memset(fnode, 0x00, sizeof(fs_node_t));
  38. fnode->inode = 0;
  39. strcpy(fnode->name, "random");
  40. fnode->uid = 0;
  41. fnode->gid = 0;
  42. fnode->mask = 0444;
  43. fnode->length = 1024;
  44. fnode->flags = FS_CHARDEVICE;
  45. fnode->read = read_random;
  46. fnode->write = write_random;
  47. fnode->open = open_random;
  48. fnode->close = close_random;
  49. fnode->readdir = NULL;
  50. fnode->finddir = NULL;
  51. fnode->ioctl = NULL;
  52. return fnode;
  53. }
  54. static int random_initialize(void) {
  55. vfs_mount("/dev/random", random_device_create());
  56. vfs_mount("/dev/urandom", random_device_create());
  57. return 0;
  58. }
  59. static int random_finalize(void) {
  60. return 0;
  61. }
  62. MODULE_DEF(random, random_initialize, random_finalize);