| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #if !defined(_GNU_SOURCE) |
| 16 | #define _GNU_SOURCE // needed for syscall() on Linux. |
| 17 | #endif |
| 18 |
| 19 | #include <openssl/crypto.h> |
| 20 |
| 21 | #include <stdlib.h> |
| 22 | #if defined(BORINGSSL_FIPS) |
| 23 | #include <sys/mman.h> |
| 24 | #include <unistd.h> |
| 25 | #endif |
| 26 |
| 27 | #include <openssl/digest.h> |
| 28 | #include <openssl/hmac.h> |
| 29 | #include <openssl/sha.h> |
| 30 |
| 31 | #include "../bcm_support.h" |
| 32 | #include "../internal.h" |
| 33 | #include "bcm_interface.h" |
| 34 |
| 35 | // TODO(crbug.com/362530616): When delocate is removed, build these files as |
| 36 | // separate compilation units again. |
| 37 | #include "aes/aes.cc.inc" |
| 38 | #include "aes/aes_nohw.cc.inc" |
| 39 | #include "aes/cbc.cc.inc" |
| 40 | #include "aes/cfb.cc.inc" |
| 41 | #include "aes/ctr.cc.inc" |
| 42 | #include "aes/gcm.cc.inc" |
| 43 | #include "aes/gcm_nohw.cc.inc" |
| 44 | #include "aes/key_wrap.cc.inc" |
| 45 | #include "aes/mode_wrappers.cc.inc" |
| 46 | #include "aes/ofb.cc.inc" |
| 47 | #include "bn/add.cc.inc" |
| 48 | #include "bn/asm/x86_64-gcc.cc.inc" |
| 49 | #include "bn/bn.cc.inc" |
| 50 | #include "bn/bytes.cc.inc" |
| 51 | #include "bn/cmp.cc.inc" |
| 52 | #include "bn/ctx.cc.inc" |
| 53 | #include "bn/div.cc.inc" |
| 54 | #include "bn/div_extra.cc.inc" |
| 55 | #include "bn/exponentiation.cc.inc" |
| 56 | #include "bn/gcd.cc.inc" |
| 57 | #include "bn/gcd_extra.cc.inc" |
| 58 | #include "bn/generic.cc.inc" |
| 59 | #include "bn/jacobi.cc.inc" |
| 60 | #include "bn/montgomery.cc.inc" |
| 61 | #include "bn/montgomery_inv.cc.inc" |
| 62 | #include "bn/mul.cc.inc" |
| 63 | #include "bn/prime.cc.inc" |
| 64 | #include "bn/random.cc.inc" |
| 65 | #include "bn/rsaz_exp.cc.inc" |
| 66 | #include "bn/shift.cc.inc" |
| 67 | #include "bn/sqrt.cc.inc" |
| 68 | #include "cipher/aead.cc.inc" |
| 69 | #include "cipher/cipher.cc.inc" |
| 70 | #include "cipher/e_aes.cc.inc" |
| 71 | #include "cipher/e_aesccm.cc.inc" |
| 72 | #include "cmac/cmac.cc.inc" |
| 73 | #include "dh/check.cc.inc" |
| 74 | #include "dh/dh.cc.inc" |
| 75 | #include "digest/digest.cc.inc" |
| 76 | #include "digest/digests.cc.inc" |
| 77 | #include "digestsign/digestsign.cc.inc" |
| 78 | #include "ec/ec.cc.inc" |
| 79 | #include "ec/ec_key.cc.inc" |
| 80 | #include "ec/ec_montgomery.cc.inc" |
| 81 | #include "ec/felem.cc.inc" |
| 82 | #include "ec/oct.cc.inc" |
| 83 | #include "ec/p224-64.cc.inc" |
| 84 | #include "ec/p256-nistz.cc.inc" |
| 85 | #include "ec/p256.cc.inc" |
| 86 | #include "ec/scalar.cc.inc" |
| 87 | #include "ec/simple.cc.inc" |
| 88 | #include "ec/simple_mul.cc.inc" |
| 89 | #include "ec/util.cc.inc" |
| 90 | #include "ec/wnaf.cc.inc" |
| 91 | #include "ecdh/ecdh.cc.inc" |
| 92 | #include "ecdsa/ecdsa.cc.inc" |
| 93 | #include "hkdf/hkdf.cc.inc" |
| 94 | #include "hmac/hmac.cc.inc" |
| 95 | #include "keccak/keccak.cc.inc" |
| 96 | #include "mldsa/mldsa.cc.inc" |
| 97 | #include "mlkem/mlkem.cc.inc" |
| 98 | #include "rand/ctrdrbg.cc.inc" |
| 99 | #include "rand/rand.cc.inc" |
| 100 | #include "rsa/blinding.cc.inc" |
| 101 | #include "rsa/padding.cc.inc" |
| 102 | #include "rsa/rsa.cc.inc" |
| 103 | #include "rsa/rsa_impl.cc.inc" |
| 104 | #include "self_check/fips.cc.inc" |
| 105 | #include "self_check/self_check.cc.inc" |
| 106 | #include "service_indicator/service_indicator.cc.inc" |
| 107 | #include "sha/sha1.cc.inc" |
| 108 | #include "sha/sha256.cc.inc" |
| 109 | #include "sha/sha512.cc.inc" |
| 110 | #include "slhdsa/fors.cc.inc" |
| 111 | #include "slhdsa/merkle.cc.inc" |
| 112 | #include "slhdsa/slhdsa.cc.inc" |
| 113 | #include "slhdsa/thash.cc.inc" |
| 114 | #include "slhdsa/wots.cc.inc" |
| 115 | #include "tls/kdf.cc.inc" |
| 116 |
| 117 |
| 118 | #if defined(BORINGSSL_FIPS) |
| 119 |
| 120 | #if !defined(OPENSSL_ASAN) |
| 121 |
| 122 | // These symbols are filled in by delocate.go (in static builds) or a linker |
| 123 | // script (in shared builds). They point to the start and end of the module, and |
| 124 | // the location of the integrity hash, respectively. |
| 125 | extern const uint8_t BORINGSSL_bcm_text_start[]; |
| 126 | extern const uint8_t BORINGSSL_bcm_text_end[]; |
| 127 | extern const uint8_t BORINGSSL_bcm_text_hash[]; |
| 128 | #if defined(BORINGSSL_SHARED_LIBRARY) |
| 129 | extern const uint8_t BORINGSSL_bcm_rodata_start[]; |
| 130 | extern const uint8_t BORINGSSL_bcm_rodata_end[]; |
| 131 | #endif |
| 132 |
| 133 | // assert_within is used to sanity check that certain symbols are within the |
| 134 | // bounds of the integrity check. It checks that start <= symbol < end and |
| 135 | // aborts otherwise. |
| 136 | static void assert_within(const void *start, const void *symbol, |
| 137 | const void *end) { |
| 138 | const uintptr_t start_val = (uintptr_t)start; |
| 139 | const uintptr_t symbol_val = (uintptr_t)symbol; |
| 140 | const uintptr_t end_val = (uintptr_t)end; |
| 141 |
| 142 | if (start_val <= symbol_val && symbol_val < end_val) { |
| 143 | return; |
| 144 | } |
| 145 |
| 146 | fprintf(CRYPTO_get_stderr(), |
| 147 | "FIPS module doesn't span expected symbol. Expected %p <= %p < %p\n", |
| 148 | start, symbol, end); |
| 149 | BORINGSSL_FIPS_abort(); |
| 150 | } |
| 151 |
| 152 | #if defined(OPENSSL_ANDROID) && defined(OPENSSL_AARCH64) |
| 153 | static void BORINGSSL_maybe_set_module_text_permissions(int permission) { |
| 154 | // Android may be compiled in execute-only-memory mode, in which case the |
| 155 | // .text segment cannot be read. That conflicts with the need for a FIPS |
| 156 | // module to hash its own contents, therefore |mprotect| is used to make |
| 157 | // the module's .text readable for the duration of the hashing process. In |
| 158 | // other build configurations this is a no-op. |
| 159 | const uintptr_t page_size = getpagesize(); |
| 160 | const uintptr_t page_start = |
| 161 | ((uintptr_t)BORINGSSL_bcm_text_start) & ~(page_size - 1); |
| 162 |
| 163 | if (mprotect((void *)page_start, |
| 164 | ((uintptr_t)BORINGSSL_bcm_text_end) - page_start, |
| 165 | permission) != 0) { |
| 166 | perror("BoringSSL: mprotect"); |
| 167 | } |
| 168 | } |
| 169 | #else |
| 170 | static void BORINGSSL_maybe_set_module_text_permissions(int permission) {} |
| 171 | #endif // !ANDROID |
| 172 |
| 173 | #endif // !ASAN |
| 174 |
| 175 | static void __attribute__((constructor)) |
| 176 | BORINGSSL_bcm_power_on_self_test(void) { |
| 177 | #if !defined(OPENSSL_ASAN) |
| 178 | // Integrity tests cannot run under ASAN because it involves reading the full |
| 179 | // .text section, which triggers the global-buffer overflow detection. |
| 180 | if (!BORINGSSL_integrity_test()) { |
| 181 | goto err; |
| 182 | } |
| 183 | #endif // OPENSSL_ASAN |
| 184 |
| 185 | if (!boringssl_self_test_startup()) { |
| 186 | goto err; |
| 187 | } |
| 188 |
| 189 | return; |
| 190 |
| 191 | err: |
| 192 | BORINGSSL_FIPS_abort(); |
| 193 | } |
| 194 |
| 195 | #if !defined(OPENSSL_ASAN) |
| 196 | int BORINGSSL_integrity_test(void) { |
| 197 | const uint8_t *const start = BORINGSSL_bcm_text_start; |
| 198 | const uint8_t *const end = BORINGSSL_bcm_text_end; |
| 199 |
| 200 | assert_within(start, reinterpret_cast<const void *>(BCM_aes_encrypt), end); |
| 201 | assert_within(start, reinterpret_cast<const void *>(RSA_sign), end); |
| 202 | assert_within(start, reinterpret_cast<const void *>(BCM_rand_bytes), end); |
| 203 | assert_within(start, reinterpret_cast<const void *>(EC_GROUP_cmp), end); |
| 204 | assert_within(start, reinterpret_cast<const void *>(BCM_sha256_update), end); |
| 205 | assert_within(start, reinterpret_cast<const void *>(ecdsa_verify_fixed), end); |
| 206 | assert_within(start, reinterpret_cast<const void *>(EVP_AEAD_CTX_seal), end); |
| 207 |
| 208 | #if defined(BORINGSSL_SHARED_LIBRARY) |
| 209 | const uint8_t *const rodata_start = BORINGSSL_bcm_rodata_start; |
| 210 | const uint8_t *const rodata_end = BORINGSSL_bcm_rodata_end; |
| 211 | #else |
| 212 | // In the static build, read-only data is placed within the .text segment. |
| 213 | const uint8_t *const rodata_start = BORINGSSL_bcm_text_start; |
| 214 | const uint8_t *const rodata_end = BORINGSSL_bcm_text_end; |
| 215 | #endif |
| 216 |
| 217 | assert_within(rodata_start, kPrimes, rodata_end); |
| 218 | assert_within(rodata_start, kP256Field, rodata_end); |
| 219 | assert_within(rodata_start, kPKCS1SigPrefixes, rodata_end); |
| 220 |
| 221 | uint8_t result[SHA256_DIGEST_LENGTH]; |
| 222 | const EVP_MD *const kHashFunction = EVP_sha256(); |
| 223 | if (!boringssl_self_test_sha256() || !boringssl_self_test_hmac_sha256()) { |
| 224 | return 0; |
| 225 | } |
| 226 |
| 227 | static const uint8_t kHMACKey[64] = {0}; |
| 228 | unsigned result_len; |
| 229 | HMAC_CTX hmac_ctx; |
| 230 | HMAC_CTX_init(&hmac_ctx); |
| 231 | if (!HMAC_Init_ex(&hmac_ctx, kHMACKey, sizeof(kHMACKey), kHashFunction, |
| 232 | NULL /* no ENGINE */)) { |
| 233 | fprintf(CRYPTO_get_stderr(), "HMAC_Init_ex failed.\n"); |
| 234 | return 0; |
| 235 | } |
| 236 |
| 237 | BORINGSSL_maybe_set_module_text_permissions(PROT_READ | PROT_EXEC); |
| 238 | #if defined(BORINGSSL_SHARED_LIBRARY) |
| 239 | uint64_t length = end - start; |
| 240 | HMAC_Update(&hmac_ctx, (const uint8_t *)&length, sizeof(length)); |
| 241 | HMAC_Update(&hmac_ctx, start, length); |
| 242 |
| 243 | length = rodata_end - rodata_start; |
| 244 | HMAC_Update(&hmac_ctx, (const uint8_t *)&length, sizeof(length)); |
| 245 | HMAC_Update(&hmac_ctx, rodata_start, length); |
| 246 | #else |
| 247 | HMAC_Update(&hmac_ctx, start, end - start); |
| 248 | #endif |
| 249 | BORINGSSL_maybe_set_module_text_permissions(PROT_EXEC); |
| 250 |
| 251 | if (!HMAC_Final(&hmac_ctx, result, &result_len) || |
| 252 | result_len != sizeof(result)) { |
| 253 | fprintf(CRYPTO_get_stderr(), "HMAC failed.\n"); |
| 254 | return 0; |
| 255 | } |
| 256 | HMAC_CTX_cleanse(&hmac_ctx); // FIPS 140-3, AS05.10. |
| 257 |
| 258 | const uint8_t *expected = BORINGSSL_bcm_text_hash; |
| 259 |
| 260 | if (!BORINGSSL_check_test(expected, result, sizeof(result), |
| 261 | "FIPS integrity test")) { |
| 262 | #if !defined(BORINGSSL_FIPS_BREAK_TESTS) |
| 263 | return 0; |
| 264 | #endif |
| 265 | } |
| 266 |
| 267 | OPENSSL_cleanse(result, sizeof(result)); // FIPS 140-3, AS05.10. |
| 268 | return 1; |
| 269 | } |
| 270 |
| 271 | const uint8_t *FIPS_module_hash(void) { return BORINGSSL_bcm_text_hash; } |
| 272 |
| 273 | #endif // OPENSSL_ASAN |
| 274 |
| 275 | void BORINGSSL_FIPS_abort(void) { |
| 276 | for (;;) { |
| 277 | abort(); |
| 278 | exit(1); |
| 279 | } |
| 280 | } |
| 281 |
| 282 | #endif // BORINGSSL_FIPS |
| 283 |