word.h 2.4 KB

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