poly1305-donna-8.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. poly1305 implementation using 8 bit * 8 bit = 16 bit multiplication and 32 bit addition
  3. based on the public domain reference version in supercop by djb
  4. */
  5. #if defined(_MSC_VER)
  6. #define POLY1305_NOINLINE __declspec(noinline)
  7. #elif defined(__GNUC__)
  8. #define POLY1305_NOINLINE __attribute__((noinline))
  9. #else
  10. #define POLY1305_NOINLINE
  11. #endif
  12. #define poly1305_block_size 16
  13. /* 17 + sizeof(size_t) + 51*sizeof(unsigned char) */
  14. typedef struct poly1305_state_internal_t {
  15. unsigned char buffer[poly1305_block_size];
  16. size_t leftover;
  17. unsigned char h[17];
  18. unsigned char r[17];
  19. unsigned char pad[17];
  20. unsigned char final;
  21. } poly1305_state_internal_t;
  22. void
  23. poly1305_init(poly1305_context *ctx, const unsigned char key[32]) {
  24. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  25. size_t i;
  26. st->leftover = 0;
  27. /* h = 0 */
  28. for (i = 0; i < 17; i++)
  29. st->h[i] = 0;
  30. /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  31. st->r[ 0] = key[ 0] & 0xff;
  32. st->r[ 1] = key[ 1] & 0xff;
  33. st->r[ 2] = key[ 2] & 0xff;
  34. st->r[ 3] = key[ 3] & 0x0f;
  35. st->r[ 4] = key[ 4] & 0xfc;
  36. st->r[ 5] = key[ 5] & 0xff;
  37. st->r[ 6] = key[ 6] & 0xff;
  38. st->r[ 7] = key[ 7] & 0x0f;
  39. st->r[ 8] = key[ 8] & 0xfc;
  40. st->r[ 9] = key[ 9] & 0xff;
  41. st->r[10] = key[10] & 0xff;
  42. st->r[11] = key[11] & 0x0f;
  43. st->r[12] = key[12] & 0xfc;
  44. st->r[13] = key[13] & 0xff;
  45. st->r[14] = key[14] & 0xff;
  46. st->r[15] = key[15] & 0x0f;
  47. st->r[16] = 0;
  48. /* save pad for later */
  49. for (i = 0; i < 16; i++)
  50. st->pad[i] = key[i + 16];
  51. st->pad[16] = 0;
  52. st->final = 0;
  53. }
  54. static void
  55. poly1305_add(unsigned char h[17], const unsigned char c[17]) {
  56. unsigned short u;
  57. unsigned int i;
  58. for (u = 0, i = 0; i < 17; i++) {
  59. u += (unsigned short)h[i] + (unsigned short)c[i];
  60. h[i] = (unsigned char)u & 0xff;
  61. u >>= 8;
  62. }
  63. }
  64. static void
  65. poly1305_squeeze(unsigned char h[17], unsigned long hr[17]) {
  66. unsigned long u;
  67. unsigned int i;
  68. u = 0;
  69. for (i = 0; i < 16; i++) {
  70. u += hr[i];
  71. h[i] = (unsigned char)u & 0xff;
  72. u >>= 8;
  73. }
  74. u += hr[16];
  75. h[16] = (unsigned char)u & 0x03;
  76. u >>= 2;
  77. u += (u << 2); /* u *= 5; */
  78. for (i = 0; i < 16; i++) {
  79. u += h[i];
  80. h[i] = (unsigned char)u & 0xff;
  81. u >>= 8;
  82. }
  83. h[16] += (unsigned char)u;
  84. }
  85. static void
  86. poly1305_freeze(unsigned char h[17]) {
  87. static const unsigned char minusp[17] = {
  88. 0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  89. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  90. 0xfc
  91. };
  92. unsigned char horig[17], negative;
  93. unsigned int i;
  94. /* compute h + -p */
  95. for (i = 0; i < 17; i++)
  96. horig[i] = h[i];
  97. poly1305_add(h, minusp);
  98. /* select h if h < p, or h + -p if h >= p */
  99. negative = -(h[16] >> 7);
  100. for (i = 0; i < 17; i++)
  101. h[i] ^= negative & (horig[i] ^ h[i]);
  102. }
  103. static void
  104. poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) {
  105. const unsigned char hibit = st->final ^ 1; /* 1 << 128 */
  106. while (bytes >= poly1305_block_size) {
  107. unsigned long hr[17], u;
  108. unsigned char c[17];
  109. unsigned int i, j;
  110. /* h += m */
  111. for (i = 0; i < 16; i++)
  112. c[i] = m[i];
  113. c[16] = hibit;
  114. poly1305_add(st->h, c);
  115. /* h *= r */
  116. for (i = 0; i < 17; i++) {
  117. u = 0;
  118. for (j = 0; j <= i ; j++) {
  119. u += (unsigned short)st->h[j] * st->r[i - j];
  120. }
  121. for (j = i + 1; j < 17; j++) {
  122. unsigned long v = (unsigned short)st->h[j] * st->r[i + 17 - j];
  123. v = ((v << 8) + (v << 6)); /* v *= (5 << 6); */
  124. u += v;
  125. }
  126. hr[i] = u;
  127. }
  128. /* (partial) h %= p */
  129. poly1305_squeeze(st->h, hr);
  130. m += poly1305_block_size;
  131. bytes -= poly1305_block_size;
  132. }
  133. }
  134. POLY1305_NOINLINE void
  135. poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) {
  136. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  137. size_t i;
  138. /* process the remaining block */
  139. if (st->leftover) {
  140. size_t i = st->leftover;
  141. st->buffer[i++] = 1;
  142. for (; i < poly1305_block_size; i++)
  143. st->buffer[i] = 0;
  144. st->final = 1;
  145. poly1305_blocks(st, st->buffer, poly1305_block_size);
  146. }
  147. /* fully reduce h */
  148. poly1305_freeze(st->h);
  149. /* h = (h + pad) % (1 << 128) */
  150. poly1305_add(st->h, st->pad);
  151. for (i = 0; i < 16; i++)
  152. mac[i] = st->h[i];
  153. /* zero out the state */
  154. for (i = 0; i < 17; i++)
  155. st->h[i] = 0;
  156. for (i = 0; i < 17; i++)
  157. st->r[i] = 0;
  158. for (i = 0; i < 17; i++)
  159. st->pad[i] = 0;
  160. }