Move random-related defines to "crypto/rand.h"
[openssl.git] / crypto / info.c
1 /*
2  * Copyright 2019 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
10 #include <openssl/crypto.h>
11 #include "crypto/rand.h"
12 #include "crypto/dso_conf.h"
13 #include "internal/thread_once.h"
14 #include "internal/cryptlib.h"
15 #include "e_os.h"
16 #include "buildinf.h"
17
18 #if defined(__arm__) || defined(__arm) || defined(__aarch64__)
19 # include "arm_arch.h"
20 #endif
21
22 /* extern declaration to avoid warning */
23 extern char ossl_cpu_info_str[];
24
25 static char *seed_sources = NULL;
26
27 char ossl_cpu_info_str[128] = "";
28 #define CPUINFO_PREFIX "CPUINFO: "
29
30 static CRYPTO_ONCE init_info = CRYPTO_ONCE_STATIC_INIT;
31
32 DEFINE_RUN_ONCE_STATIC(init_info_strings)
33 {
34 #if defined(OPENSSL_CPUID_OBJ)
35 # if defined(__i386)   || defined(__i386__)   || defined(_M_IX86) || \
36      defined(__x86_64) || defined(__x86_64__) || \
37      defined(_M_AMD64) || defined(_M_X64)
38     const char *env;
39
40     BIO_snprintf(ossl_cpu_info_str, sizeof(ossl_cpu_info_str),
41                  CPUINFO_PREFIX "OPENSSL_ia32cap=0x%llx:0x%llx",
42                  (long long)OPENSSL_ia32cap_P[0] |
43                  (long long)OPENSSL_ia32cap_P[1] << 32,
44                  (long long)OPENSSL_ia32cap_P[2] |
45                  (long long)OPENSSL_ia32cap_P[3] << 32);
46     if ((env = getenv("OPENSSL_ia32cap")) != NULL)
47         BIO_snprintf(ossl_cpu_info_str + strlen(ossl_cpu_info_str),
48                      sizeof(ossl_cpu_info_str) - strlen(ossl_cpu_info_str),
49                      " env:%s", env);
50 # elif defined(__arm__) || defined(__arm) || defined(__aarch64__)
51     const char *env;
52
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 # endif
60 #endif
61
62     {
63         static char seeds[512] = "";
64
65 #define add_seeds_string(str)                                           \
66         do {                                                            \
67             if (seeds[0] != '\0')                                       \
68                 OPENSSL_strlcat(seeds, " ", sizeof(seeds));             \
69             OPENSSL_strlcat(seeds, str, sizeof(seeds));                 \
70         } while (0)
71 #define add_seeds_stringlist(label, strlist)                            \
72         do {                                                            \
73             add_seeds_string(label "(");                                \
74             {                                                           \
75                 const char *dev[] = strlist;                            \
76                 int first = 1;                                          \
77                                                                         \
78                 for (; *dev != NULL; dev++) {                           \
79                     if (!first)                                         \
80                         OPENSSL_strlcat(seeds, " ", sizeof(seeds));     \
81                     first = 0;                                          \
82                     OPENSSL_strlcat(seeds, *dev, sizeof(seeds));        \
83                 }                                                       \
84             }                                                           \
85             OPENSSL_strlcat(seeds, ")", sizeof(seeds));                 \
86         } while (0)
87
88 #ifdef OPENSSL_RAND_SEED_NONE
89         add_seeds_string("none");
90 #endif
91 #ifdef OPENSSL_RAND_SEED_RTDSC
92         add_seeds_string("stdsc");
93 #endif
94 #ifdef OPENSSL_RAND_SEED_RDCPU
95         add_seeds_string("rdrand ( rdseed rdrand )");
96 #endif
97 #ifdef OPENSSL_RAND_SEED_LIBRANDOM
98         add_seeds_string("C-library-random");
99 #endif
100 #ifdef OPENSSL_RAND_SEED_GETRANDOM
101         add_seeds_string("getrandom-syscall");
102 #endif
103 #ifdef OPENSSL_RAND_SEED_DEVRANDOM
104         add_seeds_stringlist("random-device", { DEVRANDOM, NULL });
105 #endif
106 #ifdef OPENSSL_RAND_SEED_EGD
107         add_seeds_stringlist("EGD", { DEVRANDOM_EGD, NULL });
108 #endif
109 #ifdef OPENSSL_RAND_SEED_OS
110         add_seeds_string("os-specific");
111 #endif
112         seed_sources = seeds;
113     }
114     return 1;
115 }
116
117 const char *OPENSSL_info(int t)
118 {
119     /*
120      * We don't care about the result.  Worst case scenario, the strings
121      * won't be initialised, i.e. remain NULL, which means that the info
122      * isn't available anyway...
123      */
124     (void)RUN_ONCE(&init_info, init_info_strings);
125
126     switch (t) {
127     case OPENSSL_INFO_CONFIG_DIR:
128         return OPENSSLDIR;
129     case OPENSSL_INFO_ENGINES_DIR:
130         return ENGINESDIR;
131     case OPENSSL_INFO_MODULES_DIR:
132         return MODULESDIR;
133     case OPENSSL_INFO_DSO_EXTENSION:
134         return DSO_EXTENSION;
135     case OPENSSL_INFO_DIR_FILENAME_SEPARATOR:
136 #if defined(_WIN32)
137         return "\\";
138 #elif defined(__VMS)
139         return "";
140 #else  /* Assume POSIX */
141         return "/";
142 #endif
143     case OPENSSL_INFO_LIST_SEPARATOR:
144         {
145             static const char list_sep[] = { LIST_SEPARATOR_CHAR, '\0' };
146             return list_sep;
147         }
148     case OPENSSL_INFO_SEED_SOURCE:
149         return seed_sources;
150     case OPENSSL_INFO_CPU_SETTINGS:
151         /*
152          * If successfully initialized, ossl_cpu_info_str will start
153          * with CPUINFO_PREFIX, if failed it will be an empty string.
154          * Strip away the CPUINFO_PREFIX which we don't need here.
155          */
156         if (ossl_cpu_info_str[0] != '\0')
157             return ossl_cpu_info_str + strlen(CPUINFO_PREFIX);
158         break;
159     default:
160         break;
161     }
162     /* Not an error */
163     return NULL;
164 }