s2n_pq.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include "s2n_pq.h"
  16. #include "crypto/s2n_openssl.h"
  17. #include "s2n_kyber_evp.h"
  18. static bool kyber512r3_avx2_bmi2_enabled = false;
  19. #if defined(S2N_CPUID_AVAILABLE)
  20. /* https://en.wikipedia.org/wiki/CPUID */
  21. #include <cpuid.h>
  22. #define PROCESSOR_INFO_AND_FEATURES 1
  23. #define EXTENDED_FEATURES_LEAF 7
  24. #define EXTENDED_FEATURES_SUBLEAF_ZERO 0
  25. /* The cpuid.h header included with older versions of gcc and
  26. * clang doesn't include definitions for bit_ADX, bit_BMI2, or
  27. * __get_cpuid_count(). */
  28. #if !defined(bit_BMI2)
  29. #define bit_BMI2 (1 << 8)
  30. #endif
  31. #define EBX_BIT_AVX2 (1 << 5)
  32. bool s2n_get_cpuid_count(uint32_t leaf, uint32_t sub_leaf, uint32_t *eax, uint32_t *ebx, uint32_t *ecx, uint32_t *edx)
  33. {
  34. /* 0x80000000 probes for extended cpuid info */
  35. uint32_t max_level = __get_cpuid_max(leaf & 0x80000000, 0);
  36. if (max_level == 0 || max_level < leaf) {
  37. return false;
  38. }
  39. __cpuid_count(leaf, sub_leaf, *eax, *ebx, *ecx, *edx);
  40. return true;
  41. }
  42. /* https://en.wikipedia.org/wiki/Bit_manipulation_instruction_set#BMI2_(Bit_Manipulation_Instruction_Set_2) */
  43. bool s2n_cpu_supports_bmi2()
  44. {
  45. uint32_t eax, ebx, ecx, edx;
  46. if (!s2n_get_cpuid_count(EXTENDED_FEATURES_LEAF, EXTENDED_FEATURES_SUBLEAF_ZERO, &eax, &ebx, &ecx, &edx)) {
  47. return false;
  48. }
  49. return (ebx & bit_BMI2);
  50. }
  51. bool s2n_cpu_supports_avx2()
  52. {
  53. uint32_t eax, ebx, ecx, edx;
  54. if (!s2n_get_cpuid_count(EXTENDED_FEATURES_LEAF, EXTENDED_FEATURES_SUBLEAF_ZERO, &eax, &ebx, &ecx, &edx)) {
  55. return false;
  56. }
  57. return (ebx & EBX_BIT_AVX2);
  58. }
  59. bool s2n_cpu_supports_kyber512r3_avx2_bmi2()
  60. {
  61. #if defined(S2N_KYBER512R3_AVX2_BMI2)
  62. return s2n_cpu_supports_bmi2() && s2n_cpu_supports_avx2();
  63. #else
  64. return false;
  65. #endif
  66. }
  67. #else /* defined(S2N_CPUID_AVAILABLE) */
  68. /* If CPUID is not available, we cannot perform necessary run-time checks. */
  69. bool s2n_cpu_supports_kyber512r3_avx2_bmi2()
  70. {
  71. return false;
  72. }
  73. #endif /* defined(S2N_CPUID_AVAILABLE) */
  74. bool s2n_kyber512r3_is_avx2_bmi2_enabled()
  75. {
  76. return kyber512r3_avx2_bmi2_enabled;
  77. }
  78. bool s2n_pq_is_enabled()
  79. {
  80. #if defined(S2N_NO_PQ)
  81. return false;
  82. #else
  83. /* aws-lc is currently the only supported FIPS library known to support PQ. */
  84. return s2n_libcrypto_is_awslc() || (!s2n_is_in_fips_mode());
  85. #endif
  86. }
  87. bool s2n_libcrypto_supports_kyber()
  88. {
  89. #if defined(S2N_LIBCRYPTO_SUPPORTS_KYBER)
  90. return true;
  91. #else
  92. return false;
  93. #endif
  94. }
  95. S2N_RESULT s2n_disable_kyber512r3_opt_avx2_bmi2()
  96. {
  97. kyber512r3_avx2_bmi2_enabled = false;
  98. return S2N_RESULT_OK;
  99. }
  100. S2N_RESULT s2n_try_enable_kyber512r3_opt_avx2_bmi2()
  101. {
  102. if (s2n_pq_is_enabled() && s2n_cpu_supports_kyber512r3_avx2_bmi2()) {
  103. kyber512r3_avx2_bmi2_enabled = true;
  104. }
  105. return S2N_RESULT_OK;
  106. }
  107. S2N_RESULT s2n_pq_init()
  108. {
  109. RESULT_ENSURE_OK(s2n_try_enable_kyber512r3_opt_avx2_bmi2(), S2N_ERR_SAFETY);
  110. return S2N_RESULT_OK;
  111. }