1
0

info.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/crypto.h>
  10. #include "crypto/rand.h"
  11. #include "crypto/dso_conf.h"
  12. #include "internal/thread_once.h"
  13. #include "internal/cryptlib.h"
  14. #include "internal/e_os.h"
  15. #include "buildinf.h"
  16. #if defined(__arm__) || defined(__arm) || defined(__aarch64__)
  17. # include "arm_arch.h"
  18. # define CPU_INFO_STR_LEN 128
  19. #elif defined(__powerpc__) || defined(__POWERPC__) || defined(_ARCH_PPC)
  20. # include "crypto/ppc_arch.h"
  21. # define CPU_INFO_STR_LEN 128
  22. #elif defined(__s390__) || defined(__s390x__)
  23. # include "s390x_arch.h"
  24. # define CPU_INFO_STR_LEN 2048
  25. #else
  26. # define CPU_INFO_STR_LEN 128
  27. #endif
  28. /* extern declaration to avoid warning */
  29. extern char ossl_cpu_info_str[];
  30. static char *seed_sources = NULL;
  31. char ossl_cpu_info_str[CPU_INFO_STR_LEN] = "";
  32. #define CPUINFO_PREFIX "CPUINFO: "
  33. static CRYPTO_ONCE init_info = CRYPTO_ONCE_STATIC_INIT;
  34. DEFINE_RUN_ONCE_STATIC(init_info_strings)
  35. {
  36. #if defined(OPENSSL_CPUID_OBJ)
  37. # if defined(__i386) || defined(__i386__) || defined(_M_IX86) || \
  38. defined(__x86_64) || defined(__x86_64__) || \
  39. defined(_M_AMD64) || defined(_M_X64)
  40. const char *env;
  41. BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
  42. CPUINFO_PREFIX "OPENSSL_ia32cap=0x%llx:0x%llx",
  43. (unsigned long long)OPENSSL_ia32cap_P[0] |
  44. (unsigned long long)OPENSSL_ia32cap_P[1] << 32,
  45. (unsigned long long)OPENSSL_ia32cap_P[2] |
  46. (unsigned long long)OPENSSL_ia32cap_P[3] << 32);
  47. if ((env = getenv("OPENSSL_ia32cap")) != NULL)
  48. BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
  49. sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
  50. " env:%s", env);
  51. # elif defined(__arm__) || defined(__arm) || defined(__aarch64__)
  52. const char *env;
  53. BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
  54. CPUINFO_PREFIX "OPENSSL_armcap=0x%x", OPENSSL_armcap_P);
  55. if ((env = getenv("OPENSSL_armcap")) != NULL)
  56. BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
  57. sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
  58. " env:%s", env);
  59. # elif defined(__powerpc__) || defined(__POWERPC__) || defined(_ARCH_PPC)
  60. const char *env;
  61. BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
  62. CPUINFO_PREFIX "OPENSSL_ppccap=0x%x", OPENSSL_ppccap_P);
  63. if ((env = getenv("OPENSSL_ppccap")) != NULL)
  64. BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
  65. sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
  66. " env:%s", env);
  67. # elif defined(__s390__) || defined(__s390x__)
  68. const char *env;
  69. BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
  70. CPUINFO_PREFIX "OPENSSL_s390xcap="
  71. "stfle:0x%llx:0x%llx:0x%llx:0x%llx:"
  72. "kimd:0x%llx:0x%llx:"
  73. "klmd:0x%llx:0x%llx:"
  74. "km:0x%llx:0x%llx:"
  75. "kmc:0x%llx:0x%llx:"
  76. "kmac:0x%llx:0x%llx:"
  77. "kmctr:0x%llx:0x%llx:"
  78. "kmo:0x%llx:0x%llx:"
  79. "kmf:0x%llx:0x%llx:"
  80. "prno:0x%llx:0x%llx:"
  81. "kma:0x%llx:0x%llx:"
  82. "pcc:0x%llx:0x%llx:"
  83. "kdsa:0x%llx:0x%llx",
  84. OPENSSL_s390xcap_P.stfle[0], OPENSSL_s390xcap_P.stfle[1],
  85. OPENSSL_s390xcap_P.stfle[2], OPENSSL_s390xcap_P.stfle[3],
  86. OPENSSL_s390xcap_P.kimd[0], OPENSSL_s390xcap_P.kimd[1],
  87. OPENSSL_s390xcap_P.klmd[0], OPENSSL_s390xcap_P.klmd[1],
  88. OPENSSL_s390xcap_P.km[0], OPENSSL_s390xcap_P.km[1],
  89. OPENSSL_s390xcap_P.kmc[0], OPENSSL_s390xcap_P.kmc[1],
  90. OPENSSL_s390xcap_P.kmac[0], OPENSSL_s390xcap_P.kmac[1],
  91. OPENSSL_s390xcap_P.kmctr[0], OPENSSL_s390xcap_P.kmctr[1],
  92. OPENSSL_s390xcap_P.kmo[0], OPENSSL_s390xcap_P.kmo[1],
  93. OPENSSL_s390xcap_P.kmf[0], OPENSSL_s390xcap_P.kmf[1],
  94. OPENSSL_s390xcap_P.prno[0], OPENSSL_s390xcap_P.prno[1],
  95. OPENSSL_s390xcap_P.kma[0], OPENSSL_s390xcap_P.kma[1],
  96. OPENSSL_s390xcap_P.pcc[0], OPENSSL_s390xcap_P.pcc[1],
  97. OPENSSL_s390xcap_P.kdsa[0], OPENSSL_s390xcap_P.kdsa[1]);
  98. if ((env = getenv("OPENSSL_s390xcap")) != NULL)
  99. BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
  100. sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
  101. " env:%s", env);
  102. # endif
  103. #endif
  104. {
  105. static char seeds[512] = "";
  106. #define add_seeds_string(str) \
  107. do { \
  108. if (seeds[0] != '\0') \
  109. OPENSSL_strlcat(seeds, " ", sizeof(seeds)); \
  110. OPENSSL_strlcat(seeds, str, sizeof(seeds)); \
  111. } while (0)
  112. #define add_seeds_stringlist(label, strlist) \
  113. do { \
  114. add_seeds_string(label "("); \
  115. { \
  116. const char *dev[] = { strlist, NULL }; \
  117. const char **p; \
  118. int first = 1; \
  119. \
  120. for (p = dev; *p != NULL; p++) { \
  121. if (!first) \
  122. OPENSSL_strlcat(seeds, " ", sizeof(seeds)); \
  123. first = 0; \
  124. OPENSSL_strlcat(seeds, *p, sizeof(seeds)); \
  125. } \
  126. } \
  127. OPENSSL_strlcat(seeds, ")", sizeof(seeds)); \
  128. } while (0)
  129. #ifdef OPENSSL_RAND_SEED_NONE
  130. add_seeds_string("none");
  131. #endif
  132. #ifdef OPENSSL_RAND_SEED_RDTSC
  133. add_seeds_string("rdtsc");
  134. #endif
  135. #ifdef OPENSSL_RAND_SEED_RDCPU
  136. # ifdef __aarch64__
  137. add_seeds_string("rndr ( rndrrs rndr )");
  138. # else
  139. add_seeds_string("rdrand ( rdseed rdrand )");
  140. # endif
  141. #endif
  142. #ifdef OPENSSL_RAND_SEED_LIBRANDOM
  143. add_seeds_string("C-library-random");
  144. #endif
  145. #ifdef OPENSSL_RAND_SEED_GETRANDOM
  146. add_seeds_string("getrandom-syscall");
  147. #endif
  148. #ifdef OPENSSL_RAND_SEED_DEVRANDOM
  149. add_seeds_stringlist("random-device", DEVRANDOM);
  150. #endif
  151. #ifdef OPENSSL_RAND_SEED_EGD
  152. add_seeds_stringlist("EGD", DEVRANDOM_EGD);
  153. #endif
  154. #ifdef OPENSSL_RAND_SEED_OS
  155. add_seeds_string("os-specific");
  156. #endif
  157. seed_sources = seeds;
  158. }
  159. return 1;
  160. }
  161. const char *OPENSSL_info(int t)
  162. {
  163. /*
  164. * We don't care about the result. Worst case scenario, the strings
  165. * won't be initialised, i.e. remain NULL, which means that the info
  166. * isn't available anyway...
  167. */
  168. (void)RUN_ONCE(&init_info, init_info_strings);
  169. switch (t) {
  170. case OPENSSL_INFO_CONFIG_DIR:
  171. return OPENSSLDIR;
  172. case OPENSSL_INFO_ENGINES_DIR:
  173. return ENGINESDIR;
  174. case OPENSSL_INFO_MODULES_DIR:
  175. return MODULESDIR;
  176. case OPENSSL_INFO_DSO_EXTENSION:
  177. return DSO_EXTENSION;
  178. case OPENSSL_INFO_DIR_FILENAME_SEPARATOR:
  179. #if defined(_WIN32)
  180. return "\\";
  181. #elif defined(__VMS)
  182. return "";
  183. #else /* Assume POSIX */
  184. return "/";
  185. #endif
  186. case OPENSSL_INFO_LIST_SEPARATOR:
  187. {
  188. static const char list_sep[] = { LIST_SEPARATOR_CHAR, '\0' };
  189. return list_sep;
  190. }
  191. case OPENSSL_INFO_SEED_SOURCE:
  192. return seed_sources;
  193. case OPENSSL_INFO_CPU_SETTINGS:
  194. /*
  195. * If successfully initialized, ossl_cpu_info_str will start
  196. * with CPUINFO_PREFIX, if failed it will be an empty string.
  197. * Strip away the CPUINFO_PREFIX which we don't need here.
  198. */
  199. if (ossl_cpu_info_str[0] != '\0')
  200. return ossl_cpu_info_str + strlen(CPUINFO_PREFIX);
  201. break;
  202. default:
  203. break;
  204. }
  205. /* Not an error */
  206. return NULL;
  207. }