mac-poly1305.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* mac-poly1305.c - Poly1305 based MACs
  2. * Copyright (C) 2014 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  3. *
  4. * This file is part of Libgcrypt.
  5. *
  6. * Libgcrypt is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * Libgcrypt is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include "g10lib.h"
  25. #include "mac-internal.h"
  26. #include "poly1305-internal.h"
  27. struct poly1305mac_context_s {
  28. poly1305_context_t ctx;
  29. gcry_cipher_hd_t hd;
  30. struct {
  31. unsigned int key_set:1;
  32. unsigned int nonce_set:1;
  33. unsigned int tag:1;
  34. } marks;
  35. byte tag[POLY1305_TAGLEN];
  36. byte key[POLY1305_KEYLEN];
  37. };
  38. static gcry_err_code_t
  39. poly1305mac_open (gcry_mac_hd_t h)
  40. {
  41. struct poly1305mac_context_s *mac_ctx;
  42. int secure = (h->magic == CTX_MAC_MAGIC_SECURE);
  43. unsigned int flags = (secure ? GCRY_CIPHER_SECURE : 0);
  44. gcry_err_code_t err;
  45. int cipher_algo;
  46. if (secure)
  47. mac_ctx = xtrycalloc_secure (1, sizeof(*mac_ctx));
  48. else
  49. mac_ctx = xtrycalloc (1, sizeof(*mac_ctx));
  50. if (!mac_ctx)
  51. return gpg_err_code_from_syserror ();
  52. h->u.poly1305mac.ctx = mac_ctx;
  53. switch (h->spec->algo)
  54. {
  55. default:
  56. /* already checked. */
  57. case GCRY_MAC_POLY1305:
  58. /* plain Poly1305. */
  59. cipher_algo = -1;
  60. return 0;
  61. case GCRY_MAC_POLY1305_AES:
  62. cipher_algo = GCRY_CIPHER_AES;
  63. break;
  64. case GCRY_MAC_POLY1305_CAMELLIA:
  65. cipher_algo = GCRY_CIPHER_CAMELLIA128;
  66. break;
  67. case GCRY_MAC_POLY1305_TWOFISH:
  68. cipher_algo = GCRY_CIPHER_TWOFISH;
  69. break;
  70. case GCRY_MAC_POLY1305_SERPENT:
  71. cipher_algo = GCRY_CIPHER_SERPENT128;
  72. break;
  73. case GCRY_MAC_POLY1305_SEED:
  74. cipher_algo = GCRY_CIPHER_SEED;
  75. break;
  76. case GCRY_MAC_POLY1305_SM4:
  77. cipher_algo = GCRY_CIPHER_SM4;
  78. break;
  79. case GCRY_MAC_POLY1305_ARIA:
  80. cipher_algo = GCRY_CIPHER_ARIA128;
  81. break;
  82. }
  83. err = _gcry_cipher_open_internal (&mac_ctx->hd, cipher_algo,
  84. GCRY_CIPHER_MODE_ECB, flags);
  85. if (err)
  86. goto err_free;
  87. return 0;
  88. err_free:
  89. xfree(h->u.poly1305mac.ctx);
  90. return err;
  91. }
  92. static void
  93. poly1305mac_close (gcry_mac_hd_t h)
  94. {
  95. struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
  96. if (h->spec->algo != GCRY_MAC_POLY1305)
  97. _gcry_cipher_close (mac_ctx->hd);
  98. xfree(mac_ctx);
  99. }
  100. static gcry_err_code_t
  101. poly1305mac_prepare_key (gcry_mac_hd_t h, const unsigned char *key, size_t keylen)
  102. {
  103. struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
  104. size_t block_keylen = keylen - 16;
  105. /* Need at least 16 + 1 byte key. */
  106. if (keylen <= 16)
  107. return GPG_ERR_INV_KEYLEN;
  108. /* For Poly1305-AES, first part of key is passed to Poly1305 as is. */
  109. memcpy (mac_ctx->key, key + block_keylen, 16);
  110. /* Remaining part is used as key for the block cipher. */
  111. return _gcry_cipher_setkey (mac_ctx->hd, key, block_keylen);
  112. }
  113. static gcry_err_code_t
  114. poly1305mac_setkey (gcry_mac_hd_t h, const unsigned char *key, size_t keylen)
  115. {
  116. struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
  117. gcry_err_code_t err;
  118. memset(&mac_ctx->ctx, 0, sizeof(mac_ctx->ctx));
  119. memset(&mac_ctx->tag, 0, sizeof(mac_ctx->tag));
  120. memset(&mac_ctx->key, 0, sizeof(mac_ctx->key));
  121. mac_ctx->marks.key_set = 0;
  122. mac_ctx->marks.nonce_set = 0;
  123. mac_ctx->marks.tag = 0;
  124. if (h->spec->algo != GCRY_MAC_POLY1305)
  125. {
  126. err = poly1305mac_prepare_key (h, key, keylen);
  127. if (err)
  128. return err;
  129. /* Poly1305-AES/etc also need nonce. */
  130. mac_ctx->marks.key_set = 1;
  131. mac_ctx->marks.nonce_set = 0;
  132. }
  133. else
  134. {
  135. /* For plain Poly1305, key is the nonce and setup is complete now. */
  136. if (keylen != POLY1305_KEYLEN)
  137. return GPG_ERR_INV_KEYLEN;
  138. memcpy (mac_ctx->key, key, keylen);
  139. err = _gcry_poly1305_init (&mac_ctx->ctx, mac_ctx->key, POLY1305_KEYLEN);
  140. if (err)
  141. {
  142. memset(&mac_ctx->key, 0, sizeof(mac_ctx->key));
  143. return err;
  144. }
  145. mac_ctx->marks.key_set = 1;
  146. mac_ctx->marks.nonce_set = 1;
  147. }
  148. return 0;
  149. }
  150. static gcry_err_code_t
  151. poly1305mac_setiv (gcry_mac_hd_t h, const unsigned char *iv, size_t ivlen)
  152. {
  153. struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
  154. gcry_err_code_t err;
  155. if (h->spec->algo == GCRY_MAC_POLY1305)
  156. return GPG_ERR_INV_ARG;
  157. if (ivlen != 16)
  158. return GPG_ERR_INV_ARG;
  159. if (!mac_ctx->marks.key_set)
  160. return 0;
  161. memset(&mac_ctx->ctx, 0, sizeof(mac_ctx->ctx));
  162. memset(&mac_ctx->tag, 0, sizeof(mac_ctx->tag));
  163. mac_ctx->marks.nonce_set = 0;
  164. mac_ctx->marks.tag = 0;
  165. /* Prepare second part of the poly1305 key. */
  166. err = _gcry_cipher_encrypt (mac_ctx->hd, mac_ctx->key + 16, 16, iv, 16);
  167. if (err)
  168. return err;
  169. err = _gcry_poly1305_init (&mac_ctx->ctx, mac_ctx->key, POLY1305_KEYLEN);
  170. if (err)
  171. return err;
  172. mac_ctx->marks.nonce_set = 1;
  173. return 0;
  174. }
  175. static gcry_err_code_t
  176. poly1305mac_reset (gcry_mac_hd_t h)
  177. {
  178. struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
  179. if (!mac_ctx->marks.key_set || !mac_ctx->marks.nonce_set)
  180. return GPG_ERR_INV_STATE;
  181. memset(&mac_ctx->ctx, 0, sizeof(mac_ctx->ctx));
  182. memset(&mac_ctx->tag, 0, sizeof(mac_ctx->tag));
  183. mac_ctx->marks.key_set = 1;
  184. mac_ctx->marks.nonce_set = 1;
  185. mac_ctx->marks.tag = 0;
  186. return _gcry_poly1305_init (&mac_ctx->ctx, mac_ctx->key, POLY1305_KEYLEN);
  187. }
  188. static gcry_err_code_t
  189. poly1305mac_write (gcry_mac_hd_t h, const unsigned char *buf, size_t buflen)
  190. {
  191. struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
  192. if (!mac_ctx->marks.key_set || !mac_ctx->marks.nonce_set ||
  193. mac_ctx->marks.tag)
  194. return GPG_ERR_INV_STATE;
  195. _gcry_poly1305_update (&mac_ctx->ctx, buf, buflen);
  196. return 0;
  197. }
  198. static gcry_err_code_t
  199. poly1305mac_read (gcry_mac_hd_t h, unsigned char *outbuf, size_t *outlen)
  200. {
  201. struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
  202. if (!mac_ctx->marks.key_set || !mac_ctx->marks.nonce_set)
  203. return GPG_ERR_INV_STATE;
  204. if (!mac_ctx->marks.tag)
  205. {
  206. _gcry_poly1305_finish(&mac_ctx->ctx, mac_ctx->tag);
  207. memset(&mac_ctx->ctx, 0, sizeof(mac_ctx->ctx));
  208. mac_ctx->marks.tag = 1;
  209. }
  210. if (*outlen == 0)
  211. return 0;
  212. if (*outlen <= POLY1305_TAGLEN)
  213. buf_cpy (outbuf, mac_ctx->tag, *outlen);
  214. else
  215. {
  216. buf_cpy (outbuf, mac_ctx->tag, POLY1305_TAGLEN);
  217. *outlen = POLY1305_TAGLEN;
  218. }
  219. return 0;
  220. }
  221. static gcry_err_code_t
  222. poly1305mac_verify (gcry_mac_hd_t h, const unsigned char *buf, size_t buflen)
  223. {
  224. struct poly1305mac_context_s *mac_ctx = h->u.poly1305mac.ctx;
  225. gcry_err_code_t err;
  226. size_t outlen = 0;
  227. /* Check and finalize tag. */
  228. err = poly1305mac_read(h, NULL, &outlen);
  229. if (err)
  230. return err;
  231. if (buflen > POLY1305_TAGLEN)
  232. return GPG_ERR_INV_LENGTH;
  233. return buf_eq_const (buf, mac_ctx->tag, buflen) ? 0 : GPG_ERR_CHECKSUM;
  234. }
  235. static unsigned int
  236. poly1305mac_get_maclen (int algo)
  237. {
  238. (void)algo;
  239. return POLY1305_TAGLEN;
  240. }
  241. static unsigned int
  242. poly1305mac_get_keylen (int algo)
  243. {
  244. (void)algo;
  245. return POLY1305_KEYLEN;
  246. }
  247. static gcry_mac_spec_ops_t poly1305mac_ops = {
  248. poly1305mac_open,
  249. poly1305mac_close,
  250. poly1305mac_setkey,
  251. poly1305mac_setiv,
  252. poly1305mac_reset,
  253. poly1305mac_write,
  254. poly1305mac_read,
  255. poly1305mac_verify,
  256. poly1305mac_get_maclen,
  257. poly1305mac_get_keylen,
  258. NULL,
  259. NULL,
  260. };
  261. const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac = {
  262. GCRY_MAC_POLY1305, {0, 0}, "POLY1305",
  263. &poly1305mac_ops
  264. };
  265. #if USE_AES
  266. const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_aes = {
  267. GCRY_MAC_POLY1305_AES, {0, 0}, "POLY1305_AES",
  268. &poly1305mac_ops
  269. };
  270. #endif
  271. #if USE_CAMELLIA
  272. const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_camellia = {
  273. GCRY_MAC_POLY1305_CAMELLIA, {0, 0}, "POLY1305_CAMELLIA",
  274. &poly1305mac_ops
  275. };
  276. #endif
  277. #if USE_TWOFISH
  278. const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_twofish = {
  279. GCRY_MAC_POLY1305_TWOFISH, {0, 0}, "POLY1305_TWOFISH",
  280. &poly1305mac_ops
  281. };
  282. #endif
  283. #if USE_SERPENT
  284. const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_serpent = {
  285. GCRY_MAC_POLY1305_SERPENT, {0, 0}, "POLY1305_SERPENT",
  286. &poly1305mac_ops
  287. };
  288. #endif
  289. #if USE_SEED
  290. const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_seed = {
  291. GCRY_MAC_POLY1305_SEED, {0, 0}, "POLY1305_SEED",
  292. &poly1305mac_ops
  293. };
  294. #endif
  295. #if USE_SM4
  296. const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_sm4 = {
  297. GCRY_MAC_POLY1305_SM4, {0, 0}, "POLY1305_SM4",
  298. &poly1305mac_ops
  299. };
  300. #endif
  301. #if USE_ARIA
  302. const gcry_mac_spec_t _gcry_mac_type_spec_poly1305mac_aria = {
  303. GCRY_MAC_POLY1305_ARIA, {0, 0}, "POLY1305_ARIA",
  304. &poly1305mac_ops
  305. };
  306. #endif