IRandomInterface.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using Ryujinx.HLE.HOS.Ipc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Security.Cryptography;
  5. namespace Ryujinx.HLE.HOS.Services.Spl
  6. {
  7. [Service("csrng")]
  8. class IRandomInterface : IpcService, IDisposable
  9. {
  10. private Dictionary<int, ServiceProcessRequest> _commands;
  11. public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => _commands;
  12. private RNGCryptoServiceProvider _rng;
  13. public IRandomInterface(ServiceCtx context)
  14. {
  15. _commands = new Dictionary<int, ServiceProcessRequest>
  16. {
  17. { 0, GetRandomBytes }
  18. };
  19. _rng = new RNGCryptoServiceProvider();
  20. }
  21. public long GetRandomBytes(ServiceCtx context)
  22. {
  23. byte[] randomBytes = new byte[context.Request.ReceiveBuff[0].Size];
  24. _rng.GetBytes(randomBytes);
  25. context.Memory.WriteBytes(context.Request.ReceiveBuff[0].Position, randomBytes);
  26. return 0;
  27. }
  28. public void Dispose()
  29. {
  30. Dispose(true);
  31. }
  32. protected virtual void Dispose(bool disposing)
  33. {
  34. if (disposing)
  35. {
  36. _rng.Dispose();
  37. }
  38. }
  39. }
  40. }