rsaz_exp.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2013-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2012, Intel Corporation. All Rights Reserved.
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. *
  10. * Originally written by Shay Gueron (1, 2), and Vlad Krasnov (1)
  11. * (1) Intel Corporation, Israel Development Center, Haifa, Israel
  12. * (2) University of Haifa, Israel
  13. */
  14. #ifndef OSSL_CRYPTO_BN_RSAZ_EXP_H
  15. # define OSSL_CRYPTO_BN_RSAZ_EXP_H
  16. # undef RSAZ_ENABLED
  17. # if defined(OPENSSL_BN_ASM_MONT) && \
  18. (defined(__x86_64) || defined(__x86_64__) || \
  19. defined(_M_AMD64) || defined(_M_X64))
  20. # define RSAZ_ENABLED
  21. # include <openssl/bn.h>
  22. # include "internal/constant_time.h"
  23. # include "bn_local.h"
  24. void RSAZ_1024_mod_exp_avx2(BN_ULONG result[16],
  25. const BN_ULONG base_norm[16],
  26. const BN_ULONG exponent[16],
  27. const BN_ULONG m_norm[16], const BN_ULONG RR[16],
  28. BN_ULONG k0);
  29. int rsaz_avx2_eligible(void);
  30. void RSAZ_512_mod_exp(BN_ULONG result[8],
  31. const BN_ULONG base_norm[8], const BN_ULONG exponent[8],
  32. const BN_ULONG m_norm[8], BN_ULONG k0,
  33. const BN_ULONG RR[8]);
  34. static ossl_inline void bn_select_words(BN_ULONG *r, BN_ULONG mask,
  35. const BN_ULONG *a,
  36. const BN_ULONG *b, size_t num)
  37. {
  38. size_t i;
  39. for (i = 0; i < num; i++) {
  40. r[i] = constant_time_select_64(mask, a[i], b[i]);
  41. }
  42. }
  43. static ossl_inline BN_ULONG bn_reduce_once_in_place(BN_ULONG *r,
  44. BN_ULONG carry,
  45. const BN_ULONG *m,
  46. BN_ULONG *tmp, size_t num)
  47. {
  48. carry -= bn_sub_words(tmp, r, m, num);
  49. bn_select_words(r, carry, r /* tmp < 0 */, tmp /* tmp >= 0 */, num);
  50. return carry;
  51. }
  52. # endif
  53. #endif