poly1305.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /**
  2. * \file poly1305.c
  3. *
  4. * \brief Poly1305 authentication algorithm.
  5. *
  6. * Copyright The Mbed TLS Contributors
  7. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  8. */
  9. #include "common.h"
  10. #if defined(MBEDTLS_POLY1305_C)
  11. #include "mbedtls/poly1305.h"
  12. #include "mbedtls/platform_util.h"
  13. #include "mbedtls/error.h"
  14. #include <string.h>
  15. #include "mbedtls/platform.h"
  16. #if !defined(MBEDTLS_POLY1305_ALT)
  17. /* Parameter validation macros */
  18. #define POLY1305_VALIDATE_RET(cond) \
  19. MBEDTLS_INTERNAL_VALIDATE_RET(cond, MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA)
  20. #define POLY1305_VALIDATE(cond) \
  21. MBEDTLS_INTERNAL_VALIDATE(cond)
  22. #define POLY1305_BLOCK_SIZE_BYTES (16U)
  23. /*
  24. * Our implementation is tuned for 32-bit platforms with a 64-bit multiplier.
  25. * However we provided an alternative for platforms without such a multiplier.
  26. */
  27. #if defined(MBEDTLS_NO_64BIT_MULTIPLICATION)
  28. static uint64_t mul64(uint32_t a, uint32_t b)
  29. {
  30. /* a = al + 2**16 ah, b = bl + 2**16 bh */
  31. const uint16_t al = (uint16_t) a;
  32. const uint16_t bl = (uint16_t) b;
  33. const uint16_t ah = a >> 16;
  34. const uint16_t bh = b >> 16;
  35. /* ab = al*bl + 2**16 (ah*bl + bl*bh) + 2**32 ah*bh */
  36. const uint32_t lo = (uint32_t) al * bl;
  37. const uint64_t me = (uint64_t) ((uint32_t) ah * bl) + (uint32_t) al * bh;
  38. const uint32_t hi = (uint32_t) ah * bh;
  39. return lo + (me << 16) + ((uint64_t) hi << 32);
  40. }
  41. #else
  42. static inline uint64_t mul64(uint32_t a, uint32_t b)
  43. {
  44. return (uint64_t) a * b;
  45. }
  46. #endif
  47. /**
  48. * \brief Process blocks with Poly1305.
  49. *
  50. * \param ctx The Poly1305 context.
  51. * \param nblocks Number of blocks to process. Note that this
  52. * function only processes full blocks.
  53. * \param input Buffer containing the input block(s).
  54. * \param needs_padding Set to 0 if the padding bit has already been
  55. * applied to the input data before calling this
  56. * function. Otherwise, set this parameter to 1.
  57. */
  58. static void poly1305_process(mbedtls_poly1305_context *ctx,
  59. size_t nblocks,
  60. const unsigned char *input,
  61. uint32_t needs_padding)
  62. {
  63. uint64_t d0, d1, d2, d3;
  64. uint32_t acc0, acc1, acc2, acc3, acc4;
  65. uint32_t r0, r1, r2, r3;
  66. uint32_t rs1, rs2, rs3;
  67. size_t offset = 0U;
  68. size_t i;
  69. r0 = ctx->r[0];
  70. r1 = ctx->r[1];
  71. r2 = ctx->r[2];
  72. r3 = ctx->r[3];
  73. rs1 = r1 + (r1 >> 2U);
  74. rs2 = r2 + (r2 >> 2U);
  75. rs3 = r3 + (r3 >> 2U);
  76. acc0 = ctx->acc[0];
  77. acc1 = ctx->acc[1];
  78. acc2 = ctx->acc[2];
  79. acc3 = ctx->acc[3];
  80. acc4 = ctx->acc[4];
  81. /* Process full blocks */
  82. for (i = 0U; i < nblocks; i++) {
  83. /* The input block is treated as a 128-bit little-endian integer */
  84. d0 = MBEDTLS_GET_UINT32_LE(input, offset + 0);
  85. d1 = MBEDTLS_GET_UINT32_LE(input, offset + 4);
  86. d2 = MBEDTLS_GET_UINT32_LE(input, offset + 8);
  87. d3 = MBEDTLS_GET_UINT32_LE(input, offset + 12);
  88. /* Compute: acc += (padded) block as a 130-bit integer */
  89. d0 += (uint64_t) acc0;
  90. d1 += (uint64_t) acc1 + (d0 >> 32U);
  91. d2 += (uint64_t) acc2 + (d1 >> 32U);
  92. d3 += (uint64_t) acc3 + (d2 >> 32U);
  93. acc0 = (uint32_t) d0;
  94. acc1 = (uint32_t) d1;
  95. acc2 = (uint32_t) d2;
  96. acc3 = (uint32_t) d3;
  97. acc4 += (uint32_t) (d3 >> 32U) + needs_padding;
  98. /* Compute: acc *= r */
  99. d0 = mul64(acc0, r0) +
  100. mul64(acc1, rs3) +
  101. mul64(acc2, rs2) +
  102. mul64(acc3, rs1);
  103. d1 = mul64(acc0, r1) +
  104. mul64(acc1, r0) +
  105. mul64(acc2, rs3) +
  106. mul64(acc3, rs2) +
  107. mul64(acc4, rs1);
  108. d2 = mul64(acc0, r2) +
  109. mul64(acc1, r1) +
  110. mul64(acc2, r0) +
  111. mul64(acc3, rs3) +
  112. mul64(acc4, rs2);
  113. d3 = mul64(acc0, r3) +
  114. mul64(acc1, r2) +
  115. mul64(acc2, r1) +
  116. mul64(acc3, r0) +
  117. mul64(acc4, rs3);
  118. acc4 *= r0;
  119. /* Compute: acc %= (2^130 - 5) (partial remainder) */
  120. d1 += (d0 >> 32);
  121. d2 += (d1 >> 32);
  122. d3 += (d2 >> 32);
  123. acc0 = (uint32_t) d0;
  124. acc1 = (uint32_t) d1;
  125. acc2 = (uint32_t) d2;
  126. acc3 = (uint32_t) d3;
  127. acc4 = (uint32_t) (d3 >> 32) + acc4;
  128. d0 = (uint64_t) acc0 + (acc4 >> 2) + (acc4 & 0xFFFFFFFCU);
  129. acc4 &= 3U;
  130. acc0 = (uint32_t) d0;
  131. d0 = (uint64_t) acc1 + (d0 >> 32U);
  132. acc1 = (uint32_t) d0;
  133. d0 = (uint64_t) acc2 + (d0 >> 32U);
  134. acc2 = (uint32_t) d0;
  135. d0 = (uint64_t) acc3 + (d0 >> 32U);
  136. acc3 = (uint32_t) d0;
  137. d0 = (uint64_t) acc4 + (d0 >> 32U);
  138. acc4 = (uint32_t) d0;
  139. offset += POLY1305_BLOCK_SIZE_BYTES;
  140. }
  141. ctx->acc[0] = acc0;
  142. ctx->acc[1] = acc1;
  143. ctx->acc[2] = acc2;
  144. ctx->acc[3] = acc3;
  145. ctx->acc[4] = acc4;
  146. }
  147. /**
  148. * \brief Compute the Poly1305 MAC
  149. *
  150. * \param ctx The Poly1305 context.
  151. * \param mac The buffer to where the MAC is written. Must be
  152. * big enough to contain the 16-byte MAC.
  153. */
  154. static void poly1305_compute_mac(const mbedtls_poly1305_context *ctx,
  155. unsigned char mac[16])
  156. {
  157. uint64_t d;
  158. uint32_t g0, g1, g2, g3, g4;
  159. uint32_t acc0, acc1, acc2, acc3, acc4;
  160. uint32_t mask;
  161. uint32_t mask_inv;
  162. acc0 = ctx->acc[0];
  163. acc1 = ctx->acc[1];
  164. acc2 = ctx->acc[2];
  165. acc3 = ctx->acc[3];
  166. acc4 = ctx->acc[4];
  167. /* Before adding 's' we ensure that the accumulator is mod 2^130 - 5.
  168. * We do this by calculating acc - (2^130 - 5), then checking if
  169. * the 131st bit is set. If it is, then reduce: acc -= (2^130 - 5)
  170. */
  171. /* Calculate acc + -(2^130 - 5) */
  172. d = ((uint64_t) acc0 + 5U);
  173. g0 = (uint32_t) d;
  174. d = ((uint64_t) acc1 + (d >> 32));
  175. g1 = (uint32_t) d;
  176. d = ((uint64_t) acc2 + (d >> 32));
  177. g2 = (uint32_t) d;
  178. d = ((uint64_t) acc3 + (d >> 32));
  179. g3 = (uint32_t) d;
  180. g4 = acc4 + (uint32_t) (d >> 32U);
  181. /* mask == 0xFFFFFFFF if 131st bit is set, otherwise mask == 0 */
  182. mask = (uint32_t) 0U - (g4 >> 2U);
  183. mask_inv = ~mask;
  184. /* If 131st bit is set then acc=g, otherwise, acc is unmodified */
  185. acc0 = (acc0 & mask_inv) | (g0 & mask);
  186. acc1 = (acc1 & mask_inv) | (g1 & mask);
  187. acc2 = (acc2 & mask_inv) | (g2 & mask);
  188. acc3 = (acc3 & mask_inv) | (g3 & mask);
  189. /* Add 's' */
  190. d = (uint64_t) acc0 + ctx->s[0];
  191. acc0 = (uint32_t) d;
  192. d = (uint64_t) acc1 + ctx->s[1] + (d >> 32U);
  193. acc1 = (uint32_t) d;
  194. d = (uint64_t) acc2 + ctx->s[2] + (d >> 32U);
  195. acc2 = (uint32_t) d;
  196. acc3 += ctx->s[3] + (uint32_t) (d >> 32U);
  197. /* Compute MAC (128 least significant bits of the accumulator) */
  198. MBEDTLS_PUT_UINT32_LE(acc0, mac, 0);
  199. MBEDTLS_PUT_UINT32_LE(acc1, mac, 4);
  200. MBEDTLS_PUT_UINT32_LE(acc2, mac, 8);
  201. MBEDTLS_PUT_UINT32_LE(acc3, mac, 12);
  202. }
  203. void mbedtls_poly1305_init(mbedtls_poly1305_context *ctx)
  204. {
  205. POLY1305_VALIDATE(ctx != NULL);
  206. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_poly1305_context));
  207. }
  208. void mbedtls_poly1305_free(mbedtls_poly1305_context *ctx)
  209. {
  210. if (ctx == NULL) {
  211. return;
  212. }
  213. mbedtls_platform_zeroize(ctx, sizeof(mbedtls_poly1305_context));
  214. }
  215. int mbedtls_poly1305_starts(mbedtls_poly1305_context *ctx,
  216. const unsigned char key[32])
  217. {
  218. POLY1305_VALIDATE_RET(ctx != NULL);
  219. POLY1305_VALIDATE_RET(key != NULL);
  220. /* r &= 0x0ffffffc0ffffffc0ffffffc0fffffff */
  221. ctx->r[0] = MBEDTLS_GET_UINT32_LE(key, 0) & 0x0FFFFFFFU;
  222. ctx->r[1] = MBEDTLS_GET_UINT32_LE(key, 4) & 0x0FFFFFFCU;
  223. ctx->r[2] = MBEDTLS_GET_UINT32_LE(key, 8) & 0x0FFFFFFCU;
  224. ctx->r[3] = MBEDTLS_GET_UINT32_LE(key, 12) & 0x0FFFFFFCU;
  225. ctx->s[0] = MBEDTLS_GET_UINT32_LE(key, 16);
  226. ctx->s[1] = MBEDTLS_GET_UINT32_LE(key, 20);
  227. ctx->s[2] = MBEDTLS_GET_UINT32_LE(key, 24);
  228. ctx->s[3] = MBEDTLS_GET_UINT32_LE(key, 28);
  229. /* Initial accumulator state */
  230. ctx->acc[0] = 0U;
  231. ctx->acc[1] = 0U;
  232. ctx->acc[2] = 0U;
  233. ctx->acc[3] = 0U;
  234. ctx->acc[4] = 0U;
  235. /* Queue initially empty */
  236. mbedtls_platform_zeroize(ctx->queue, sizeof(ctx->queue));
  237. ctx->queue_len = 0U;
  238. return 0;
  239. }
  240. int mbedtls_poly1305_update(mbedtls_poly1305_context *ctx,
  241. const unsigned char *input,
  242. size_t ilen)
  243. {
  244. size_t offset = 0U;
  245. size_t remaining = ilen;
  246. size_t queue_free_len;
  247. size_t nblocks;
  248. POLY1305_VALIDATE_RET(ctx != NULL);
  249. POLY1305_VALIDATE_RET(ilen == 0 || input != NULL);
  250. if ((remaining > 0U) && (ctx->queue_len > 0U)) {
  251. queue_free_len = (POLY1305_BLOCK_SIZE_BYTES - ctx->queue_len);
  252. if (ilen < queue_free_len) {
  253. /* Not enough data to complete the block.
  254. * Store this data with the other leftovers.
  255. */
  256. memcpy(&ctx->queue[ctx->queue_len],
  257. input,
  258. ilen);
  259. ctx->queue_len += ilen;
  260. remaining = 0U;
  261. } else {
  262. /* Enough data to produce a complete block */
  263. memcpy(&ctx->queue[ctx->queue_len],
  264. input,
  265. queue_free_len);
  266. ctx->queue_len = 0U;
  267. poly1305_process(ctx, 1U, ctx->queue, 1U); /* add padding bit */
  268. offset += queue_free_len;
  269. remaining -= queue_free_len;
  270. }
  271. }
  272. if (remaining >= POLY1305_BLOCK_SIZE_BYTES) {
  273. nblocks = remaining / POLY1305_BLOCK_SIZE_BYTES;
  274. poly1305_process(ctx, nblocks, &input[offset], 1U);
  275. offset += nblocks * POLY1305_BLOCK_SIZE_BYTES;
  276. remaining %= POLY1305_BLOCK_SIZE_BYTES;
  277. }
  278. if (remaining > 0U) {
  279. /* Store partial block */
  280. ctx->queue_len = remaining;
  281. memcpy(ctx->queue, &input[offset], remaining);
  282. }
  283. return 0;
  284. }
  285. int mbedtls_poly1305_finish(mbedtls_poly1305_context *ctx,
  286. unsigned char mac[16])
  287. {
  288. POLY1305_VALIDATE_RET(ctx != NULL);
  289. POLY1305_VALIDATE_RET(mac != NULL);
  290. /* Process any leftover data */
  291. if (ctx->queue_len > 0U) {
  292. /* Add padding bit */
  293. ctx->queue[ctx->queue_len] = 1U;
  294. ctx->queue_len++;
  295. /* Pad with zeroes */
  296. memset(&ctx->queue[ctx->queue_len],
  297. 0,
  298. POLY1305_BLOCK_SIZE_BYTES - ctx->queue_len);
  299. poly1305_process(ctx, 1U, /* Process 1 block */
  300. ctx->queue, 0U); /* Already padded above */
  301. }
  302. poly1305_compute_mac(ctx, mac);
  303. return 0;
  304. }
  305. int mbedtls_poly1305_mac(const unsigned char key[32],
  306. const unsigned char *input,
  307. size_t ilen,
  308. unsigned char mac[16])
  309. {
  310. mbedtls_poly1305_context ctx;
  311. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  312. POLY1305_VALIDATE_RET(key != NULL);
  313. POLY1305_VALIDATE_RET(mac != NULL);
  314. POLY1305_VALIDATE_RET(ilen == 0 || input != NULL);
  315. mbedtls_poly1305_init(&ctx);
  316. ret = mbedtls_poly1305_starts(&ctx, key);
  317. if (ret != 0) {
  318. goto cleanup;
  319. }
  320. ret = mbedtls_poly1305_update(&ctx, input, ilen);
  321. if (ret != 0) {
  322. goto cleanup;
  323. }
  324. ret = mbedtls_poly1305_finish(&ctx, mac);
  325. cleanup:
  326. mbedtls_poly1305_free(&ctx);
  327. return ret;
  328. }
  329. #endif /* MBEDTLS_POLY1305_ALT */
  330. #if defined(MBEDTLS_SELF_TEST)
  331. static const unsigned char test_keys[2][32] =
  332. {
  333. {
  334. 0x85, 0xd6, 0xbe, 0x78, 0x57, 0x55, 0x6d, 0x33,
  335. 0x7f, 0x44, 0x52, 0xfe, 0x42, 0xd5, 0x06, 0xa8,
  336. 0x01, 0x03, 0x80, 0x8a, 0xfb, 0x0d, 0xb2, 0xfd,
  337. 0x4a, 0xbf, 0xf6, 0xaf, 0x41, 0x49, 0xf5, 0x1b
  338. },
  339. {
  340. 0x1c, 0x92, 0x40, 0xa5, 0xeb, 0x55, 0xd3, 0x8a,
  341. 0xf3, 0x33, 0x88, 0x86, 0x04, 0xf6, 0xb5, 0xf0,
  342. 0x47, 0x39, 0x17, 0xc1, 0x40, 0x2b, 0x80, 0x09,
  343. 0x9d, 0xca, 0x5c, 0xbc, 0x20, 0x70, 0x75, 0xc0
  344. }
  345. };
  346. static const unsigned char test_data[2][127] =
  347. {
  348. {
  349. 0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x67, 0x72,
  350. 0x61, 0x70, 0x68, 0x69, 0x63, 0x20, 0x46, 0x6f,
  351. 0x72, 0x75, 0x6d, 0x20, 0x52, 0x65, 0x73, 0x65,
  352. 0x61, 0x72, 0x63, 0x68, 0x20, 0x47, 0x72, 0x6f,
  353. 0x75, 0x70
  354. },
  355. {
  356. 0x27, 0x54, 0x77, 0x61, 0x73, 0x20, 0x62, 0x72,
  357. 0x69, 0x6c, 0x6c, 0x69, 0x67, 0x2c, 0x20, 0x61,
  358. 0x6e, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x73,
  359. 0x6c, 0x69, 0x74, 0x68, 0x79, 0x20, 0x74, 0x6f,
  360. 0x76, 0x65, 0x73, 0x0a, 0x44, 0x69, 0x64, 0x20,
  361. 0x67, 0x79, 0x72, 0x65, 0x20, 0x61, 0x6e, 0x64,
  362. 0x20, 0x67, 0x69, 0x6d, 0x62, 0x6c, 0x65, 0x20,
  363. 0x69, 0x6e, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77,
  364. 0x61, 0x62, 0x65, 0x3a, 0x0a, 0x41, 0x6c, 0x6c,
  365. 0x20, 0x6d, 0x69, 0x6d, 0x73, 0x79, 0x20, 0x77,
  366. 0x65, 0x72, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20,
  367. 0x62, 0x6f, 0x72, 0x6f, 0x67, 0x6f, 0x76, 0x65,
  368. 0x73, 0x2c, 0x0a, 0x41, 0x6e, 0x64, 0x20, 0x74,
  369. 0x68, 0x65, 0x20, 0x6d, 0x6f, 0x6d, 0x65, 0x20,
  370. 0x72, 0x61, 0x74, 0x68, 0x73, 0x20, 0x6f, 0x75,
  371. 0x74, 0x67, 0x72, 0x61, 0x62, 0x65, 0x2e
  372. }
  373. };
  374. static const size_t test_data_len[2] =
  375. {
  376. 34U,
  377. 127U
  378. };
  379. static const unsigned char test_mac[2][16] =
  380. {
  381. {
  382. 0xa8, 0x06, 0x1d, 0xc1, 0x30, 0x51, 0x36, 0xc6,
  383. 0xc2, 0x2b, 0x8b, 0xaf, 0x0c, 0x01, 0x27, 0xa9
  384. },
  385. {
  386. 0x45, 0x41, 0x66, 0x9a, 0x7e, 0xaa, 0xee, 0x61,
  387. 0xe7, 0x08, 0xdc, 0x7c, 0xbc, 0xc5, 0xeb, 0x62
  388. }
  389. };
  390. /* Make sure no other definition is already present. */
  391. #undef ASSERT
  392. #define ASSERT(cond, args) \
  393. do \
  394. { \
  395. if (!(cond)) \
  396. { \
  397. if (verbose != 0) \
  398. mbedtls_printf args; \
  399. \
  400. return -1; \
  401. } \
  402. } \
  403. while (0)
  404. int mbedtls_poly1305_self_test(int verbose)
  405. {
  406. unsigned char mac[16];
  407. unsigned i;
  408. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  409. for (i = 0U; i < 2U; i++) {
  410. if (verbose != 0) {
  411. mbedtls_printf(" Poly1305 test %u ", i);
  412. }
  413. ret = mbedtls_poly1305_mac(test_keys[i],
  414. test_data[i],
  415. test_data_len[i],
  416. mac);
  417. ASSERT(0 == ret, ("error code: %i\n", ret));
  418. ASSERT(0 == memcmp(mac, test_mac[i], 16U), ("failed (mac)\n"));
  419. if (verbose != 0) {
  420. mbedtls_printf("passed\n");
  421. }
  422. }
  423. if (verbose != 0) {
  424. mbedtls_printf("\n");
  425. }
  426. return 0;
  427. }
  428. #endif /* MBEDTLS_SELF_TEST */
  429. #endif /* MBEDTLS_POLY1305_C */