Cryptography.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "il2cpp-config.h"
  2. #if IL2CPP_TARGET_POSIX && !RUNTIME_TINY && !IL2CPP_USE_PLATFORM_SPECIFIC_CRYPTO
  3. #include "os/Cryptography.h"
  4. #include <errno.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #ifndef NAME_DEV_URANDOM
  8. #define NAME_DEV_URANDOM "/dev/urandom"
  9. #endif
  10. static int64_t file = -1;
  11. namespace il2cpp
  12. {
  13. namespace os
  14. {
  15. void* Cryptography::GetCryptographyProvider()
  16. {
  17. return (file < 0) ? NULL : (void*)file;
  18. }
  19. bool Cryptography::OpenCryptographyProvider()
  20. {
  21. #ifdef NAME_DEV_URANDOM
  22. file = open(NAME_DEV_URANDOM, O_RDONLY);
  23. #endif
  24. #ifdef NAME_DEV_RANDOM
  25. if (file < 0)
  26. file = open(NAME_DEV_RANDOM, O_RDONLY);
  27. #endif
  28. return true;
  29. }
  30. void Cryptography::ReleaseCryptographyProvider(void* provider)
  31. {
  32. }
  33. bool Cryptography::FillBufferWithRandomBytes(void* provider, intptr_t length, unsigned char* data)
  34. {
  35. int count = 0;
  36. ssize_t err;
  37. // Make sure the provider is correct, or we may end up reading from the wrong file.
  38. // If provider happens to be 0 we will read from stdin and hang!
  39. if ((int64_t)provider != file)
  40. return false;
  41. do
  42. {
  43. err = read((int)(size_t)provider, data + count, length - count);
  44. if (err < 0)
  45. {
  46. if (errno == EINTR)
  47. continue;
  48. break;
  49. }
  50. count += err;
  51. }
  52. while (count < length);
  53. return err >= 0;
  54. }
  55. }
  56. }
  57. #endif