ssl_c.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <random>
  5. #include "core/hle/hle.h"
  6. #include "core/hle/service/ssl_c.h"
  7. ////////////////////////////////////////////////////////////////////////////////////////////////////
  8. // Namespace SSL_C
  9. namespace SSL_C {
  10. // TODO: Implement a proper CSPRNG in the future when actual security is needed
  11. static std::mt19937 rand_gen;
  12. static void Initialize(Service::Interface* self) {
  13. u32* cmd_buff = Kernel::GetCommandBuffer();
  14. // Seed random number generator when the SSL service is initialized
  15. std::random_device rand_device;
  16. rand_gen.seed(rand_device());
  17. // Stub, return success
  18. cmd_buff[1] = RESULT_SUCCESS.raw;
  19. }
  20. static void GenerateRandomData(Service::Interface* self) {
  21. u32* cmd_buff = Kernel::GetCommandBuffer();
  22. u32 size = cmd_buff[1];
  23. VAddr address = cmd_buff[3];
  24. u8* output_buff = Memory::GetPointer(address);
  25. // Fill the output buffer with random data.
  26. u32 data = 0;
  27. u32 i = 0;
  28. while (i < size) {
  29. if ((i % 4) == 0) {
  30. // The random number generator returns 4 bytes worth of data, so generate new random data when i == 0 and when i is divisible by 4
  31. data = rand_gen();
  32. }
  33. if (size > 4) {
  34. // Use up the entire 4 bytes of the random data for as long as possible
  35. *(u32*)(output_buff + i) = data;
  36. i += 4;
  37. } else if (size == 2) {
  38. *(u16*)(output_buff + i) = (u16)(data & 0xffff);
  39. i += 2;
  40. } else {
  41. *(u8*)(output_buff + i) = (u8)(data & 0xff);
  42. i++;
  43. }
  44. }
  45. // Stub, return success
  46. cmd_buff[1] = RESULT_SUCCESS.raw;
  47. }
  48. const Interface::FunctionInfo FunctionTable[] = {
  49. {0x00010002, Initialize, "Initialize"},
  50. {0x000200C2, nullptr, "CreateContext"},
  51. {0x00030000, nullptr, "CreateRootCertChain"},
  52. {0x00040040, nullptr, "DestroyRootCertChain"},
  53. {0x00050082, nullptr, "AddTrustedRootCA"},
  54. {0x00060080, nullptr, "RootCertChainAddDefaultCert"},
  55. {0x00110042, GenerateRandomData, "GenerateRandomData"},
  56. {0x00150082, nullptr, "Read"},
  57. {0x00170082, nullptr, "Write"},
  58. {0x00180080, nullptr, "ContextSetRootCertChain"},
  59. };
  60. ////////////////////////////////////////////////////////////////////////////////////////////////////
  61. // Interface class
  62. Interface::Interface() {
  63. Register(FunctionTable);
  64. }
  65. } // namespace