fips.c
Go to the documentation of this file.
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 
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 
29 int FIPS_mode_set(int on) { return on == FIPS_mode(); }
30 
31 #if defined(BORINGSSL_FIPS_COUNTERS)
32 
34  if (counter < 0 || counter > fips_counter_max) {
35  abort();
36  }
37 
38  const size_t *array =
40  if (!array) {
41  return 0;
42  }
43 
44  return array[counter];
45 }
46 
48  if (counter < 0 || counter > fips_counter_max) {
49  abort();
50  }
51 
52  size_t *array =
54  if (!array) {
55  const size_t num_bytes = sizeof(size_t) * (fips_counter_max + 1);
56  array = OPENSSL_malloc(num_bytes);
57  if (!array) {
58  return;
59  }
60 
61  OPENSSL_memset(array, 0, num_bytes);
63  OPENSSL_free)) {
64  // |OPENSSL_free| has already been called by |CRYPTO_set_thread_local|.
65  return;
66  }
67  }
68 
69  array[counter]++;
70 }
71 
72 #else
73 
74 size_t FIPS_read_counter(enum fips_counter_t counter) { return 0; }
75 
76 // boringssl_fips_inc_counter is a no-op, inline function in internal.h in this
77 // case. That should let the compiler optimise away the callsites.
78 
79 #endif
boringssl_fips_inc_counter
OPENSSL_INLINE void boringssl_fips_inc_counter(enum fips_counter_t counter)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:952
FIPS_mode_set
int FIPS_mode_set(int on)
Definition: fips.c:29
CRYPTO_get_thread_local
#define CRYPTO_get_thread_local
Definition: boringssl_prefix_symbols.h:1169
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
CRYPTO_set_thread_local
#define CRYPTO_set_thread_local
Definition: boringssl_prefix_symbols.h:1199
array
Definition: undname.c:101
fips_counter_t
fips_counter_t
Definition: crypto.h:80
fips_counter_max
@ fips_counter_max
Definition: crypto.h:86
counter
static int counter
Definition: abseil-cpp/absl/flags/reflection_test.cc:131
crypto.h
FIPS_read_counter
size_t FIPS_read_counter(enum fips_counter_t counter)
Definition: fips.c:74
FIPS_mode
int FIPS_mode(void)
Definition: fips.c:21
OPENSSL_THREAD_LOCAL_FIPS_COUNTERS
@ OPENSSL_THREAD_LOCAL_FIPS_COUNTERS
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:638
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:20