Poly1305.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include "Poly1305.hpp"
  28. namespace ZeroTier {
  29. //////////////////////////////////////////////////////////////////////////////
  30. //////////////////////////////////////////////////////////////////////////////
  31. /*
  32. 20080912
  33. D. J. Bernstein
  34. Public domain.
  35. */
  36. static void add(unsigned int h[17],const unsigned int c[17])
  37. {
  38. unsigned int j;
  39. unsigned int u;
  40. u = 0;
  41. for (j = 0;j < 17;++j) { u += h[j] + c[j]; h[j] = u & 255; u >>= 8; }
  42. }
  43. static void squeeze(unsigned int h[17])
  44. {
  45. unsigned int j;
  46. unsigned int u;
  47. u = 0;
  48. for (j = 0;j < 16;++j) { u += h[j]; h[j] = u & 255; u >>= 8; }
  49. u += h[16]; h[16] = u & 3;
  50. u = 5 * (u >> 2);
  51. for (j = 0;j < 16;++j) { u += h[j]; h[j] = u & 255; u >>= 8; }
  52. u += h[16]; h[16] = u;
  53. }
  54. static const unsigned int minusp[17] = {
  55. 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 252
  56. } ;
  57. static void freeze(unsigned int h[17])
  58. {
  59. unsigned int horig[17];
  60. unsigned int j;
  61. unsigned int negative;
  62. for (j = 0;j < 17;++j) horig[j] = h[j];
  63. add(h,minusp);
  64. negative = -(h[16] >> 7);
  65. for (j = 0;j < 17;++j) h[j] ^= negative & (horig[j] ^ h[j]);
  66. }
  67. static void mulmod(unsigned int h[17],const unsigned int r[17])
  68. {
  69. unsigned int hr[17];
  70. unsigned int i;
  71. unsigned int j;
  72. unsigned int u;
  73. for (i = 0;i < 17;++i) {
  74. u = 0;
  75. for (j = 0;j <= i;++j) u += h[j] * r[i - j];
  76. for (j = i + 1;j < 17;++j) u += 320 * h[j] * r[i + 17 - j];
  77. hr[i] = u;
  78. }
  79. for (i = 0;i < 17;++i) h[i] = hr[i];
  80. squeeze(h);
  81. }
  82. static inline int crypto_onetimeauth(unsigned char *out,const unsigned char *in,unsigned long long inlen,const unsigned char *k)
  83. {
  84. unsigned int j;
  85. unsigned int r[17];
  86. unsigned int h[17];
  87. unsigned int c[17];
  88. r[0] = k[0];
  89. r[1] = k[1];
  90. r[2] = k[2];
  91. r[3] = k[3] & 15;
  92. r[4] = k[4] & 252;
  93. r[5] = k[5];
  94. r[6] = k[6];
  95. r[7] = k[7] & 15;
  96. r[8] = k[8] & 252;
  97. r[9] = k[9];
  98. r[10] = k[10];
  99. r[11] = k[11] & 15;
  100. r[12] = k[12] & 252;
  101. r[13] = k[13];
  102. r[14] = k[14];
  103. r[15] = k[15] & 15;
  104. r[16] = 0;
  105. for (j = 0;j < 17;++j) h[j] = 0;
  106. while (inlen > 0) {
  107. for (j = 0;j < 17;++j) c[j] = 0;
  108. for (j = 0;(j < 16) && (j < inlen);++j) c[j] = in[j];
  109. c[j] = 1;
  110. in += j; inlen -= j;
  111. add(h,c);
  112. mulmod(h,r);
  113. }
  114. freeze(h);
  115. for (j = 0;j < 16;++j) c[j] = k[j + 16];
  116. c[16] = 0;
  117. add(h,c);
  118. for (j = 0;j < 16;++j) out[j] = h[j];
  119. return 0;
  120. }
  121. //////////////////////////////////////////////////////////////////////////////
  122. //////////////////////////////////////////////////////////////////////////////
  123. void Poly1305::compute(void *auth,const void *data,unsigned int len,const void *key)
  124. throw()
  125. {
  126. crypto_onetimeauth((unsigned char *)auth,(const unsigned char *)data,len,(const unsigned char *)key);
  127. }
  128. } // namespace ZeroTier