aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Services/Spl/IRandomInterface.cs
blob: 6391d981ce47efaddcfbb4debe86a82be5c8b12f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using Ryujinx.HLE.HOS.Ipc;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;

namespace Ryujinx.HLE.HOS.Services.Spl
{
    class IRandomInterface : IpcService, IDisposable
    {
        private Dictionary<int, ServiceProcessRequest> m_Commands;

        public override IReadOnlyDictionary<int, ServiceProcessRequest> Commands => m_Commands;

        private RNGCryptoServiceProvider Rng;

        public IRandomInterface()
        {
            m_Commands = new Dictionary<int, ServiceProcessRequest>()
            {
                { 0, GetRandomBytes }
            };

            Rng = new RNGCryptoServiceProvider();
        }

        public long GetRandomBytes(ServiceCtx Context)
        {
            byte[] RandomBytes = new byte[Context.Request.ReceiveBuff[0].Size];

            Rng.GetBytes(RandomBytes);

            Context.Memory.WriteBytes(Context.Request.ReceiveBuff[0].Position, RandomBytes);

            return 0;
        }

        public void Dispose()
        {
            Dispose(true);
        }

        protected virtual void Dispose(bool Disposing)
        {
            if (Disposing)
            {
                Rng.Dispose();
            }
        }
    }
}