ecc_platform_specific.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* uECC_platform_specific.c - Implementation of platform specific functions*/
  2. /* Copyright (c) 2014, Kenneth MacKay
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright notice,
  10. * this list of conditions and the following disclaimer in the documentation
  11. * and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. * POSSIBILITY OF SUCH DAMAGE.*/
  24. /*
  25. * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
  26. *
  27. * Redistribution and use in source and binary forms, with or without
  28. * modification, are permitted provided that the following conditions are met:
  29. *
  30. * - Redistributions of source code must retain the above copyright notice,
  31. * this list of conditions and the following disclaimer.
  32. *
  33. * - Redistributions in binary form must reproduce the above copyright
  34. * notice, this list of conditions and the following disclaimer in the
  35. * documentation and/or other materials provided with the distribution.
  36. *
  37. * - Neither the name of Intel Corporation nor the names of its contributors
  38. * may be used to endorse or promote products derived from this software
  39. * without specific prior written permission.
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  42. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  45. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  46. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  47. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  48. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  49. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  51. * POSSIBILITY OF SUCH DAMAGE.
  52. *
  53. * uECC_platform_specific.c -- Implementation of platform specific functions
  54. */
  55. #if defined(unix) || defined(__linux__) || defined(__unix__) || \
  56. defined(__unix) | (defined(__APPLE__) && defined(__MACH__)) || \
  57. defined(uECC_POSIX)
  58. /* Some POSIX-like system with /dev/urandom or /dev/random. */
  59. #include <sys/types.h>
  60. #include <fcntl.h>
  61. #include <unistd.h>
  62. #include <stdint.h>
  63. #ifndef O_CLOEXEC
  64. #define O_CLOEXEC 0
  65. #endif
  66. int default_CSPRNG(uint8_t *dest, unsigned int size) {
  67. /* input sanity check: */
  68. if (dest == (uint8_t *) 0 || (size <= 0))
  69. return 0;
  70. int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
  71. if (fd == -1) {
  72. fd = open("/dev/random", O_RDONLY | O_CLOEXEC);
  73. if (fd == -1) {
  74. return 0;
  75. }
  76. }
  77. char *ptr = (char *)dest;
  78. size_t left = (size_t) size;
  79. while (left > 0) {
  80. ssize_t bytes_read = read(fd, ptr, left);
  81. if (bytes_read <= 0) { // read failed
  82. close(fd);
  83. return 0;
  84. }
  85. left -= bytes_read;
  86. ptr += bytes_read;
  87. }
  88. close(fd);
  89. return 1;
  90. }
  91. #endif /* platform */