• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 #if defined(BORINGSSL_FIPS_COUNTERS)
32 
FIPS_read_counter(enum fips_counter_t counter)33 size_t FIPS_read_counter(enum fips_counter_t counter) {
34   if (counter < 0 || counter > fips_counter_max) {
35     abort();
36   }
37 
38   const size_t *array =
39       CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_FIPS_COUNTERS);
40   if (!array) {
41     return 0;
42   }
43 
44   return array[counter];
45 }
46 
boringssl_fips_inc_counter(enum fips_counter_t counter)47 void boringssl_fips_inc_counter(enum fips_counter_t counter) {
48   if (counter < 0 || counter > fips_counter_max) {
49     abort();
50   }
51 
52   size_t *array =
53       CRYPTO_get_thread_local(OPENSSL_THREAD_LOCAL_FIPS_COUNTERS);
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);
62     if (!CRYPTO_set_thread_local(OPENSSL_THREAD_LOCAL_FIPS_COUNTERS, array,
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 
FIPS_read_counter(enum fips_counter_t counter)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
80