poly1305.c 15 KB

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