poly1305-donna.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "poly1305-donna.h"
  2. #include "poly1305-donna-32.h"
  3. void
  4. poly1305_update(poly1305_context *ctx, const unsigned char *m, size_t bytes) {
  5. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  6. size_t i = 0;
  7. /* handle leftover */
  8. if (st->leftover) {
  9. size_t want = (poly1305_block_size - st->leftover);
  10. if (want > bytes)
  11. want = bytes;
  12. for (i = 0; i < want; i++)
  13. st->buffer[st->leftover + i] = m[i];
  14. bytes -= want;
  15. m += want;
  16. st->leftover += want;
  17. if (st->leftover < poly1305_block_size)
  18. return;
  19. poly1305_blocks(st, st->buffer, poly1305_block_size);
  20. st->leftover = 0;
  21. }
  22. /* process full blocks */
  23. if (bytes >= poly1305_block_size) {
  24. size_t want = (bytes & ~(poly1305_block_size - 1));
  25. poly1305_blocks(st, m, want);
  26. m += want;
  27. bytes -= want;
  28. }
  29. /* store leftover */
  30. if (bytes) {
  31. for (i = 0; i < bytes; i++)
  32. st->buffer[st->leftover + i] = m[i];
  33. st->leftover += bytes;
  34. }
  35. }
  36. void
  37. poly1305_auth(unsigned char mac[16], const unsigned char *m, size_t bytes, const unsigned char key[32]) {
  38. poly1305_context ctx = {0};
  39. poly1305_init(&ctx, key);
  40. poly1305_update(&ctx, m, bytes);
  41. poly1305_finish(&ctx, mac);
  42. }
  43. int
  44. poly1305_verify(const unsigned char mac1[16], const unsigned char mac2[16]) {
  45. size_t i = 0;
  46. unsigned int dif = 0;
  47. for (i = 0; i < 16; i++)
  48. dif |= (mac1[i] ^ mac2[i]);
  49. dif = (dif - 1) >> ((sizeof(unsigned int) * 8) - 1);
  50. return (dif & 1);
  51. }
  52. /* test a few basic operations */
  53. int
  54. poly1305_power_on_self_test(void) {
  55. /* example from nacl */
  56. static const unsigned char nacl_key[32] = {
  57. 0xee,0xa6,0xa7,0x25,0x1c,0x1e,0x72,0x91,
  58. 0x6d,0x11,0xc2,0xcb,0x21,0x4d,0x3c,0x25,
  59. 0x25,0x39,0x12,0x1d,0x8e,0x23,0x4e,0x65,
  60. 0x2d,0x65,0x1f,0xa4,0xc8,0xcf,0xf8,0x80,
  61. };
  62. static const unsigned char nacl_msg[131] = {
  63. 0x8e,0x99,0x3b,0x9f,0x48,0x68,0x12,0x73,
  64. 0xc2,0x96,0x50,0xba,0x32,0xfc,0x76,0xce,
  65. 0x48,0x33,0x2e,0xa7,0x16,0x4d,0x96,0xa4,
  66. 0x47,0x6f,0xb8,0xc5,0x31,0xa1,0x18,0x6a,
  67. 0xc0,0xdf,0xc1,0x7c,0x98,0xdc,0xe8,0x7b,
  68. 0x4d,0xa7,0xf0,0x11,0xec,0x48,0xc9,0x72,
  69. 0x71,0xd2,0xc2,0x0f,0x9b,0x92,0x8f,0xe2,
  70. 0x27,0x0d,0x6f,0xb8,0x63,0xd5,0x17,0x38,
  71. 0xb4,0x8e,0xee,0xe3,0x14,0xa7,0xcc,0x8a,
  72. 0xb9,0x32,0x16,0x45,0x48,0xe5,0x26,0xae,
  73. 0x90,0x22,0x43,0x68,0x51,0x7a,0xcf,0xea,
  74. 0xbd,0x6b,0xb3,0x73,0x2b,0xc0,0xe9,0xda,
  75. 0x99,0x83,0x2b,0x61,0xca,0x01,0xb6,0xde,
  76. 0x56,0x24,0x4a,0x9e,0x88,0xd5,0xf9,0xb3,
  77. 0x79,0x73,0xf6,0x22,0xa4,0x3d,0x14,0xa6,
  78. 0x59,0x9b,0x1f,0x65,0x4c,0xb4,0x5a,0x74,
  79. 0xe3,0x55,0xa5
  80. };
  81. static const unsigned char nacl_mac[16] = {
  82. 0xf3,0xff,0xc7,0x70,0x3f,0x94,0x00,0xe5,
  83. 0x2a,0x7d,0xfb,0x4b,0x3d,0x33,0x05,0xd9
  84. };
  85. /* generates a final value of (2^130 - 2) == 3 */
  86. static const unsigned char wrap_key[32] = {
  87. 0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  88. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  89. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  90. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  91. };
  92. static const unsigned char wrap_msg[16] = {
  93. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  94. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
  95. };
  96. static const unsigned char wrap_mac[16] = {
  97. 0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  98. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  99. };
  100. /*
  101. mac of the macs of messages of length 0 to 256, where the key and messages
  102. have all their values set to the length
  103. */
  104. static const unsigned char total_key[32] = {
  105. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  106. 0xff,0xfe,0xfd,0xfc,0xfb,0xfa,0xf9,
  107. 0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  108. 0xff,0xff,0xff,0xff,0xff,0xff,0xff
  109. };
  110. static const unsigned char total_mac[16] = {
  111. 0x64,0xaf,0xe2,0xe8,0xd6,0xad,0x7b,0xbd,
  112. 0xd2,0x87,0xf9,0x7c,0x44,0x62,0x3d,0x39
  113. };
  114. poly1305_context ctx = {0};
  115. poly1305_context total_ctx = {0};
  116. unsigned char all_key[32] = {0};
  117. unsigned char all_msg[256] = {0};
  118. unsigned char mac[16] = {0};
  119. size_t i = 0, j = 0;
  120. int result = 1;
  121. for (i = 0; i < sizeof(mac); i++)
  122. mac[i] = 0;
  123. poly1305_auth(mac, nacl_msg, sizeof(nacl_msg), nacl_key);
  124. result &= poly1305_verify(nacl_mac, mac);
  125. for (i = 0; i < sizeof(mac); i++)
  126. mac[i] = 0;
  127. poly1305_init(&ctx, nacl_key);
  128. poly1305_update(&ctx, nacl_msg + 0, 32);
  129. poly1305_update(&ctx, nacl_msg + 32, 64);
  130. poly1305_update(&ctx, nacl_msg + 96, 16);
  131. poly1305_update(&ctx, nacl_msg + 112, 8);
  132. poly1305_update(&ctx, nacl_msg + 120, 4);
  133. poly1305_update(&ctx, nacl_msg + 124, 2);
  134. poly1305_update(&ctx, nacl_msg + 126, 1);
  135. poly1305_update(&ctx, nacl_msg + 127, 1);
  136. poly1305_update(&ctx, nacl_msg + 128, 1);
  137. poly1305_update(&ctx, nacl_msg + 129, 1);
  138. poly1305_update(&ctx, nacl_msg + 130, 1);
  139. poly1305_finish(&ctx, mac);
  140. result &= poly1305_verify(nacl_mac, mac);
  141. for (i = 0; i < sizeof(mac); i++)
  142. mac[i] = 0;
  143. poly1305_auth(mac, wrap_msg, sizeof(wrap_msg), wrap_key);
  144. result &= poly1305_verify(wrap_mac, mac);
  145. poly1305_init(&total_ctx, total_key);
  146. for (i = 0; i < 256; i++) {
  147. /* set key and message to 'i,i,i..' */
  148. for (j = 0; j < sizeof(all_key); j++)
  149. all_key[j] = i;
  150. for (j = 0; j < i; j++)
  151. all_msg[j] = i;
  152. poly1305_auth(mac, all_msg, i, all_key);
  153. poly1305_update(&total_ctx, mac, 16);
  154. }
  155. poly1305_finish(&total_ctx, mac);
  156. result &= poly1305_verify(total_mac, mac);
  157. return result;
  158. }