gf128.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * cifra - embedded cryptography library
  3. * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
  4. *
  5. * To the extent possible under law, the author(s) have dedicated all
  6. * copyright and related and neighboring rights to this software to the
  7. * public domain worldwide. This software is distributed without any
  8. * warranty.
  9. *
  10. * You should have received a copy of the CC0 Public Domain Dedication
  11. * along with this software. If not, see
  12. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  13. */
  14. #ifndef GF128_H
  15. #define GF128_H
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. /**
  19. * @brief Operations in GF(2^128).
  20. *
  21. * These implementations are constant time, but relatively slow.
  22. */
  23. typedef uint32_t cf_gf128[4];
  24. /* Unpack from big-endian bytes into out. */
  25. void cf_gf128_frombytes_be(const uint8_t in[16], cf_gf128 out);
  26. /* Pack in big-endian order into out. */
  27. void cf_gf128_tobytes_be(const cf_gf128 in, uint8_t out[16]);
  28. /* out = 2 * in. Arguments may not alias. */
  29. void cf_gf128_double(const cf_gf128 in, cf_gf128 out);
  30. /* out = 2 * in. Arguments may not alias.
  31. * This differs from cf_gf128_double because it interprets the
  32. * block in little endian: the lsb is the msb of the
  33. * first element, the msb is the lsb of the last element.
  34. *
  35. * GCM uses this convention. */
  36. void cf_gf128_double_le(const cf_gf128 in, cf_gf128 out);
  37. /* out = x + y. Arguments may alias. */
  38. void cf_gf128_add(const cf_gf128 x, const cf_gf128 y, cf_gf128 out);
  39. /* out = xy. Arguments may alias.
  40. *
  41. * This uses cf_gf128_double_le internally, and is suitable for
  42. * GCM. */
  43. void cf_gf128_mul(const cf_gf128 x, const cf_gf128 y, cf_gf128 out);
  44. #endif