1 /* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 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 #ifndef OPENSSL_HEADER_SERVICE_INDICATOR_H 16 #define OPENSSL_HEADER_SERVICE_INDICATOR_H 17 18 #include <openssl/base.h> 19 20 #if defined(__cplusplus) 21 extern "C" { 22 #endif 23 24 // FIPS_service_indicator_before_call and |FIPS_service_indicator_after_call| 25 // both currently return the same local thread counter which is slowly 26 // incremented whenever approved services are called. The 27 // |CALL_SERVICE_AND_CHECK_APPROVED| macro is strongly recommended over calling 28 // these functions directly. 29 // 30 // |FIPS_service_indicator_before_call| is intended to be called immediately 31 // before an approved service, while |FIPS_service_indicator_after_call| should 32 // be called immediately after. If the values returned from these two functions 33 // are not equal, this means that the service called inbetween is deemed to be 34 // approved. If the values are still the same, this means the counter has not 35 // been incremented, and the service called is not approved for FIPS. 36 // 37 // In non-FIPS builds, |FIPS_service_indicator_before_call| always returns zero 38 // and |FIPS_service_indicator_after_call| always returns one. Thus calls always 39 // appear to be approved. This is intended to simplify testing. 40 OPENSSL_EXPORT uint64_t FIPS_service_indicator_before_call(void); 41 OPENSSL_EXPORT uint64_t FIPS_service_indicator_after_call(void); 42 43 #if defined(__cplusplus) 44 } 45 46 #if !defined(BORINGSSL_NO_CXX) 47 48 extern "C++" { 49 50 // CALL_SERVICE_AND_CHECK_APPROVED runs |func| and sets |approved| to one of the 51 // |FIPSStatus*| values, above, depending on whether |func| invoked an 52 // approved service. The result of |func| becomes the result of this macro. 53 #define CALL_SERVICE_AND_CHECK_APPROVED(approved, func) \ 54 [&] { \ 55 bssl::FIPSIndicatorHelper fips_indicator_helper(&approved); \ 56 return func; \ 57 }() 58 59 namespace bssl { 60 61 enum class FIPSStatus { 62 NOT_APPROVED = 0, 63 APPROVED = 1, 64 }; 65 66 // FIPSIndicatorHelper records whether the service indicator counter advanced 67 // during its lifetime. 68 class FIPSIndicatorHelper { 69 public: FIPSIndicatorHelper(FIPSStatus * result)70 FIPSIndicatorHelper(FIPSStatus *result) 71 : result_(result), before_(FIPS_service_indicator_before_call()) { 72 *result_ = FIPSStatus::NOT_APPROVED; 73 } 74 ~FIPSIndicatorHelper()75 ~FIPSIndicatorHelper() { 76 uint64_t after = FIPS_service_indicator_after_call(); 77 if (after != before_) { 78 *result_ = FIPSStatus::APPROVED; 79 } 80 } 81 82 FIPSIndicatorHelper(const FIPSIndicatorHelper&) = delete; 83 FIPSIndicatorHelper &operator=(const FIPSIndicatorHelper &) = delete; 84 85 private: 86 FIPSStatus *const result_; 87 const uint64_t before_; 88 }; 89 90 } // namespace bssl 91 } // extern "C++" 92 93 #endif // !BORINGSSL_NO_CXX 94 #endif // __cplusplus 95 96 #endif // OPENSSL_HEADER_SERVICE_INDICATOR_H 97