1 /* Copyright (c) 2017, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include <openssl/crypto.h>
16
17 #include "../../internal.h"
18 #include "../delocate.h"
19
20
FIPS_mode(void)21 int FIPS_mode(void) {
22 #if defined(BORINGSSL_FIPS) && !defined(OPENSSL_ASAN)
23 return 1;
24 #else
25 return 0;
26 #endif
27 }
28
FIPS_mode_set(int on)29 int FIPS_mode_set(int on) { return on == FIPS_mode(); }
30
FIPS_module_name(void)31 const char *FIPS_module_name(void) { return "BoringCrypto"; }
32
FIPS_version(void)33 uint32_t FIPS_version(void) {
34 return 20240805;
35 }
36
FIPS_query_algorithm_status(const char * algorithm)37 int FIPS_query_algorithm_status(const char *algorithm) {
38 #if defined(BORINGSSL_FIPS)
39 static const char kApprovedAlgorithms[][13] = {
40 "AES-CBC",
41 "AES-CCM",
42 "AES-CTR",
43 "AES-ECB",
44 "AES-GCM",
45 "AES-KW",
46 "AES-KWP",
47 "ctrDRBG",
48 "ECC-SSC",
49 "ECDSA-sign",
50 "ECDSA-verify",
51 "FFC-SSC",
52 "HMAC",
53 "RSA-sign",
54 "RSA-verify",
55 "SHA-1",
56 "SHA2-224",
57 "SHA2-256",
58 "SHA2-384",
59 "SHA2-512",
60 "SHA2-512/256",
61 };
62 for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kApprovedAlgorithms); i++) {
63 if (strcmp(algorithm, kApprovedAlgorithms[i]) == 0) {
64 return 1;
65 }
66 }
67 #endif // BORINGSSL_FIPS
68
69 return 0;
70 }
71
72 #if defined(BORINGSSL_FIPS_COUNTERS)
73
FIPS_read_counter(enum fips_counter_t counter)74 size_t FIPS_read_counter(enum fips_counter_t counter) {
75 size_t index = (size_t)counter;
76 if (index > fips_counter_max) {
77 abort();
78 }
79
80 const size_t *array =
81 CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_FIPS_COUNTERS);
82 if (!array) {
83 return 0;
84 }
85
86 return array[index];
87 }
88
boringssl_fips_inc_counter(enum fips_counter_t counter)89 void boringssl_fips_inc_counter(enum fips_counter_t counter) {
90 size_t index = (size_t)counter;
91 if (index > fips_counter_max) {
92 abort();
93 }
94
95 size_t *array =
96 CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_FIPS_COUNTERS);
97 if (!array) {
98 const size_t num_bytes = sizeof(size_t) * (fips_counter_max + 1);
99 array = OPENSSL_zalloc(num_bytes);
100 if (!array) {
101 return;
102 }
103
104 if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_FIPS_COUNTERS, array,
105 OPENSSL_free)) {
106 // |OPENSSL_free| has already been called by |CRYPTO_set_thread_local|.
107 return;
108 }
109 }
110
111 array[index]++;
112 }
113
114 #else
115
FIPS_read_counter(enum fips_counter_t counter)116 size_t FIPS_read_counter(enum fips_counter_t counter) { return 0; }
117
118 // boringssl_fips_inc_counter is a no-op, inline function in internal.h in this
119 // case. That should let the compiler optimise away the callsites.
120
121 #endif
122