word.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright 2014 Cryptography Research, Inc.
  4. *
  5. * Licensed under the Apache License 2.0 (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 Mike Hamburg
  11. */
  12. #ifndef OSSL_CRYPTO_EC_CURVE448_WORD_H
  13. # define OSSL_CRYPTO_EC_CURVE448_WORD_H
  14. # include <string.h>
  15. # include <assert.h>
  16. # include <stdlib.h>
  17. # include <openssl/e_os2.h>
  18. # include "curve448utils.h"
  19. # ifdef INT128_MAX
  20. # include "arch_64/arch_intrinsics.h"
  21. # else
  22. # include "arch_32/arch_intrinsics.h"
  23. # endif
  24. # if (ARCH_WORD_BITS == 64)
  25. typedef uint64_t word_t, mask_t;
  26. typedef uint128_t dword_t;
  27. typedef int32_t hsword_t;
  28. typedef int64_t sword_t;
  29. typedef int128_t dsword_t;
  30. # elif (ARCH_WORD_BITS == 32)
  31. typedef uint32_t word_t, mask_t;
  32. typedef uint64_t dword_t;
  33. typedef int16_t hsword_t;
  34. typedef int32_t sword_t;
  35. typedef int64_t dsword_t;
  36. # else
  37. # error "For now, we only support 32- and 64-bit architectures."
  38. # endif
  39. /*
  40. * Scalar limbs are keyed off of the API word size instead of the arch word
  41. * size.
  42. */
  43. # if C448_WORD_BITS == 64
  44. # define SC_LIMB(x) (x)
  45. # elif C448_WORD_BITS == 32
  46. # define SC_LIMB(x) ((uint32_t)(x)),((x) >> 32)
  47. # else
  48. # error "For now we only support 32- and 64-bit architectures."
  49. # endif
  50. /*
  51. * The plan on booleans: The external interface uses c448_bool_t, but this
  52. * might be a different size than our particular arch's word_t (and thus
  53. * mask_t). Also, the caller isn't guaranteed to pass it as nonzero. So
  54. * bool_to_mask converts word sizes and checks nonzero. On the flip side,
  55. * mask_t is always -1 or 0, but it might be a different size than
  56. * c448_bool_t. On the third hand, we have success vs boolean types, but
  57. * that's handled in common.h: it converts between c448_bool_t and
  58. * c448_error_t.
  59. */
  60. static ossl_inline c448_bool_t mask_to_bool(mask_t m)
  61. {
  62. return (c448_sword_t)(sword_t)m;
  63. }
  64. static ossl_inline mask_t bool_to_mask(c448_bool_t m)
  65. {
  66. /* On most arches this will be optimized to a simple cast. */
  67. mask_t ret = 0;
  68. unsigned int i;
  69. unsigned int limit = sizeof(c448_bool_t) / sizeof(mask_t);
  70. if (limit < 1)
  71. limit = 1;
  72. for (i = 0; i < limit; i++)
  73. ret |= ~word_is_zero(m >> (i * 8 * sizeof(word_t)));
  74. return ret;
  75. }
  76. #endif /* OSSL_CRYPTO_EC_CURVE448_WORD_H */