aesgcm-sw.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Implementation of the GCM polynomial hash in pure software.
  3. *
  4. * I don't know of a faster way to do this in a side-channel safe
  5. * manner than by precomputing a giant table and iterating over the
  6. * whole thing.
  7. *
  8. * The original GCM reference suggests that you precompute the effects
  9. * of multiplying a 128-bit value by the fixed key, in the form of a
  10. * table indexed by some number of bits of the input value, so that
  11. * you end up computing something of the form
  12. *
  13. * table1[x & 0xFF] ^ table2[(x>>8) & 0xFF] ^ ... ^ table15[(x>>120) & 0xFF]
  14. *
  15. * But that was obviously written before cache and timing leaks were
  16. * known about. What's a time-safe approach?
  17. *
  18. * Well, the above technique isn't fixed to 8 bits of input per table.
  19. * You could trade off the number of tables against the size of each
  20. * table. At one extreme of this tradeoff, you have 128 tables each
  21. * indexed by a single input bit - which is to say, you have 128
  22. * values, each 128 bits wide, and you XOR together the subset of
  23. * those values corresponding to the input bits, which you can do by
  24. * making a bitmask out of each input bit using standard constant-
  25. * time-coding bit twiddling techniques.
  26. *
  27. * That's pretty unpleasant when GCM is supposed to be a fast
  28. * algorithm, but I don't know of a better approach that meets current
  29. * security standards! Suggestions welcome, if they can get through
  30. * testsc.
  31. */
  32. #include "ssh.h"
  33. #include "aesgcm.h"
  34. /*
  35. * Store a 128-bit value in the most convenient form standard C will
  36. * let us, namely two uint64_t giving its most and least significant
  37. * halves.
  38. */
  39. typedef struct {
  40. uint64_t hi, lo;
  41. } value128_t;
  42. typedef struct aesgcm_sw {
  43. AESGCM_COMMON_FIELDS;
  44. /* Accumulator for the current evaluation, and mask that will be
  45. * XORed in at the end. High */
  46. value128_t acc, mask;
  47. /*
  48. * Table of values to XOR in for each bit, representing the effect
  49. * of multiplying by the fixed key. The key itself doesn't need to
  50. * be stored separately, because it's never used. (However, it is
  51. * also the first entry in the table, so if you _do_ need it,
  52. * there it is.)
  53. *
  54. * Table is indexed from the low bit of the input upwards.
  55. */
  56. value128_t table[128];
  57. } aesgcm_sw;
  58. static bool aesgcm_sw_available(void)
  59. {
  60. return true; /* pure software implementation, always available */
  61. }
  62. static void aesgcm_sw_setkey_impl(aesgcm_sw *gcm, const unsigned char *var)
  63. {
  64. value128_t v;
  65. v.hi = GET_64BIT_MSB_FIRST(var);
  66. v.lo = GET_64BIT_MSB_FIRST(var + 8);
  67. /*
  68. * Prepare the table. This has to be done in reverse order, so
  69. * that the original value of the variable corresponds to
  70. * table[127], because AES-GCM works in the bit-reversal of its
  71. * logical specification so that's where the logical constant term
  72. * lives. (See more detailed comment in aesgcm-ref-poly.c.)
  73. */
  74. for (size_t i = 0; i < 128; i++) {
  75. gcm->table[127 - i] = v;
  76. /* Multiply v by x, which means shifting right (bit reversal
  77. * again) and then adding 0xE1 at the top if we shifted a 1 out. */
  78. uint64_t lobit = v.lo & 1;
  79. v.lo = (v.lo >> 1) ^ (v.hi << 63);
  80. v.hi = (v.hi >> 1) ^ (0xE100000000000000ULL & -lobit);
  81. }
  82. }
  83. static inline void aesgcm_sw_setup(aesgcm_sw *gcm, const unsigned char *mask)
  84. {
  85. gcm->mask.hi = GET_64BIT_MSB_FIRST(mask);
  86. gcm->mask.lo = GET_64BIT_MSB_FIRST(mask + 8);
  87. gcm->acc.hi = gcm->acc.lo = 0;
  88. }
  89. static inline void aesgcm_sw_coeff(aesgcm_sw *gcm, const unsigned char *coeff)
  90. {
  91. /* XOR in the new coefficient */
  92. gcm->acc.hi ^= GET_64BIT_MSB_FIRST(coeff);
  93. gcm->acc.lo ^= GET_64BIT_MSB_FIRST(coeff + 8);
  94. /* And now just loop over the bits of acc, making up a new value
  95. * by XORing together the entries of 'table' corresponding to set
  96. * bits. */
  97. value128_t out;
  98. out.lo = out.hi = 0;
  99. const value128_t *tableptr = gcm->table;
  100. for (size_t i = 0; i < 64; i++) {
  101. uint64_t bit = 1 & gcm->acc.lo;
  102. gcm->acc.lo >>= 1;
  103. uint64_t mask = -bit;
  104. out.hi ^= mask & tableptr->hi;
  105. out.lo ^= mask & tableptr->lo;
  106. tableptr++;
  107. }
  108. for (size_t i = 0; i < 64; i++) {
  109. uint64_t bit = 1 & gcm->acc.hi;
  110. gcm->acc.hi >>= 1;
  111. uint64_t mask = -bit;
  112. out.hi ^= mask & tableptr->hi;
  113. out.lo ^= mask & tableptr->lo;
  114. tableptr++;
  115. }
  116. gcm->acc = out;
  117. }
  118. static inline void aesgcm_sw_output(aesgcm_sw *gcm, unsigned char *output)
  119. {
  120. PUT_64BIT_MSB_FIRST(output, gcm->acc.hi ^ gcm->mask.hi);
  121. PUT_64BIT_MSB_FIRST(output + 8, gcm->acc.lo ^ gcm->mask.lo);
  122. smemclr(&gcm->acc, 16);
  123. smemclr(&gcm->mask, 16);
  124. }
  125. #define AESGCM_FLAVOUR sw
  126. #define AESGCM_NAME "unaccelerated"
  127. #include "aesgcm-footer.h"