aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.HLE/HOS/Services/Spl/IRandomInterface.cs
blob: cae8d8d02ec47c8de877c9cb353644b9829dfe11 (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> _commands;

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

        private RNGCryptoServiceProvider _rng;

        public IRandomInterface()
        {
            _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();
            }
        }
    }
}