info.c 8.0 KB

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