rand_apple.c 948 B

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <gmssl/rand.h>
  13. #include <gmssl/error.h>
  14. #include <Security/Security.h> // clang -framework Security
  15. int rand_bytes(uint8_t *buf, size_t len)
  16. {
  17. int errCode;
  18. if ((errCode = SecRandomCopyBytes(kSecRandomDefault, len, buf)) != errSecSuccess) {
  19. error_print();
  20. fprintf(stderr, "%s:%d: SecRandomCopyBytes() return OSStatus = %d\n", __FILE__, __LINE__, errCode);
  21. /*
  22. CFStringRef errStr;
  23. errStr = SecCopyErrorMessageString(errCode, NULL);
  24. fprintf(stderr, "error: %s\n", CFStringGetCStringPtr(errStr, kCFStringEncodingMacRoman));
  25. CFRelease(errStr); // -framework CoreFoundation
  26. */
  27. return -1;
  28. }
  29. return 1;
  30. }