gfmult.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*-
  2. * Copyright (c) 2014 The FreeBSD Foundation
  3. * All rights reserved.
  4. *
  5. * This software was developed by John-Mark Gurney under
  6. * the sponsorship of the FreeBSD Foundation and
  7. * Rubicon Communications, LLC (Netgate).
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * $FreeBSD$
  30. *
  31. */
  32. #ifndef _GFMULT_H_
  33. #define _GFMULT_H_
  34. #ifdef __APPLE__
  35. #define __aligned(x) __attribute__((__aligned__(x)))
  36. #define be64dec(buf) __builtin_bswap64(*(uint64_t *)buf)
  37. #define be64enc(buf, x) (*(uint64_t *)buf = __builtin_bswap64(x))
  38. #else
  39. #include <sys/endian.h>
  40. #endif
  41. #ifdef _KERNEL
  42. #include <sys/types.h>
  43. #else
  44. #include <stdint.h>
  45. #include <strings.h>
  46. #endif
  47. #define REQ_ALIGN (16 * 4)
  48. /*
  49. * The rows are striped across cache lines. Note that the indexes
  50. * are bit reversed to make accesses quicker.
  51. */
  52. struct gf128table {
  53. uint32_t a[16] __aligned(REQ_ALIGN); /* bits 0 - 31 */
  54. uint32_t b[16] __aligned(REQ_ALIGN); /* bits 63 - 32 */
  55. uint32_t c[16] __aligned(REQ_ALIGN); /* bits 95 - 64 */
  56. uint32_t d[16] __aligned(REQ_ALIGN); /* bits 127 - 96 */
  57. } __aligned(REQ_ALIGN);
  58. /*
  59. * A set of tables that contain h, h^2, h^3, h^4. To be used w/ gf128_mul4.
  60. */
  61. struct gf128table4 {
  62. struct gf128table tbls[4];
  63. };
  64. /*
  65. * GCM per spec is bit reversed in memory. So byte 0 is really bit reversed
  66. * and contains bits 0-7. We can deal w/ this by using right shifts and
  67. * related math instead of having to bit reverse everything. This means that
  68. * the low bits are in v[0] (bits 0-63) and reverse order, while the high
  69. * bits are in v[1] (bits 64-127) and reverse order. The high bit of v[0] is
  70. * bit 0, and the low bit of v[1] is bit 127.
  71. */
  72. struct gf128 {
  73. uint64_t v[2];
  74. };
  75. /* Note that we don't bit reverse in MAKE_GF128. */
  76. #define MAKE_GF128(a, b) ((struct gf128){.v = { (a), (b) } })
  77. #define GF128_EQ(a, b) ((((a).v[0] ^ (b).v[0]) | \
  78. ((a).v[1] ^ (b).v[1])) == 0)
  79. static inline struct gf128
  80. gf128_read(const uint8_t *buf)
  81. {
  82. struct gf128 r;
  83. r.v[0] = be64dec(buf);
  84. buf += sizeof(uint64_t);
  85. r.v[1] = be64dec(buf);
  86. return r;
  87. }
  88. static inline void
  89. gf128_write(struct gf128 v, uint8_t *buf)
  90. {
  91. uint64_t tmp;
  92. be64enc(buf, v.v[0]);
  93. buf += sizeof tmp;
  94. be64enc(buf, v.v[1]);
  95. }
  96. static inline struct gf128 __pure /* XXX - __pure2 instead */
  97. gf128_add(struct gf128 a, struct gf128 b)
  98. {
  99. a.v[0] ^= b.v[0];
  100. a.v[1] ^= b.v[1];
  101. return a;
  102. }
  103. void gf128_genmultable(struct gf128 h, struct gf128table *t);
  104. void gf128_genmultable4(struct gf128 h, struct gf128table4 *t);
  105. struct gf128 gf128_mul(struct gf128 v, struct gf128table *tbl);
  106. struct gf128 gf128_mul4(struct gf128 a, struct gf128 b, struct gf128 c,
  107. struct gf128 d, struct gf128table4 *tbl);
  108. struct gf128 gf128_mul4b(struct gf128 r, const uint8_t *v,
  109. struct gf128table4 *tbl);
  110. #endif /* _GFMULT_H_ */