aesgcm-sw.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. { // WINSCP
  75. size_t i;
  76. for (i = 0; i < 128; i++) {
  77. gcm->table[127 - i] = v;
  78. /* Multiply v by x, which means shifting right (bit reversal
  79. * again) and then adding 0xE1 at the top if we shifted a 1 out. */
  80. { // WINSCP
  81. uint64_t lobit = v.lo & 1;
  82. v.lo = (v.lo >> 1) ^ (v.hi << 63);
  83. v.hi = (v.hi >> 1) ^ (0xE100000000000000ULL & -lobit);
  84. } // WINSCP
  85. }
  86. } // WINSCP
  87. }
  88. static inline void aesgcm_sw_setup(aesgcm_sw *gcm, const unsigned char *mask)
  89. {
  90. gcm->mask.hi = GET_64BIT_MSB_FIRST(mask);
  91. gcm->mask.lo = GET_64BIT_MSB_FIRST(mask + 8);
  92. gcm->acc.hi = gcm->acc.lo = 0;
  93. }
  94. static inline void aesgcm_sw_coeff(aesgcm_sw *gcm, const unsigned char *coeff)
  95. {
  96. /* XOR in the new coefficient */
  97. gcm->acc.hi ^= GET_64BIT_MSB_FIRST(coeff);
  98. gcm->acc.lo ^= GET_64BIT_MSB_FIRST(coeff + 8);
  99. /* And now just loop over the bits of acc, making up a new value
  100. * by XORing together the entries of 'table' corresponding to set
  101. * bits. */
  102. { // WINSCP
  103. value128_t out;
  104. out.lo = out.hi = 0;
  105. { // WINSCP
  106. const value128_t *tableptr = gcm->table;
  107. size_t i;
  108. for (i = 0; i < 64; i++) {
  109. uint64_t bit = 1 & gcm->acc.lo;
  110. gcm->acc.lo >>= 1;
  111. { // WINSCP
  112. uint64_t mask = -bit;
  113. out.hi ^= mask & tableptr->hi;
  114. out.lo ^= mask & tableptr->lo;
  115. tableptr++;
  116. } // WINSCP
  117. }
  118. for (i = 0; i < 64; i++) {
  119. uint64_t bit = 1 & gcm->acc.hi;
  120. gcm->acc.hi >>= 1;
  121. { // WINSCP
  122. uint64_t mask = -bit;
  123. out.hi ^= mask & tableptr->hi;
  124. out.lo ^= mask & tableptr->lo;
  125. tableptr++;
  126. } // WINSCP
  127. }
  128. gcm->acc = out;
  129. } // WINSCP
  130. } // WINSCP
  131. }
  132. static inline void aesgcm_sw_output(aesgcm_sw *gcm, unsigned char *output)
  133. {
  134. PUT_64BIT_MSB_FIRST(output, gcm->acc.hi ^ gcm->mask.hi);
  135. PUT_64BIT_MSB_FIRST(output + 8, gcm->acc.lo ^ gcm->mask.lo);
  136. smemclr(&gcm->acc, 16);
  137. smemclr(&gcm->mask, 16);
  138. }
  139. #define AESGCM_FLAVOUR sw
  140. #define AESGCM_NAME "unaccelerated"
  141. #include "aesgcm-footer.h"