vmac.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /*
  2. * VMAC: Message Authentication Code using Universal Hashing
  3. *
  4. * Reference: https://tools.ietf.org/html/draft-krovetz-vmac-01
  5. *
  6. * Copyright (c) 2009, Intel Corporation.
  7. * Copyright (c) 2018, Google Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. * Place - Suite 330, Boston, MA 02111-1307 USA.
  21. */
  22. /*
  23. * Derived from:
  24. * VMAC and VHASH Implementation by Ted Krovetz (tdk@acm.org) and Wei Dai.
  25. * This implementation is herby placed in the public domain.
  26. * The authors offers no warranty. Use at your own risk.
  27. * Last modified: 17 APR 08, 1700 PDT
  28. */
  29. #include <asm/unaligned.h>
  30. #include <linux/init.h>
  31. #include <linux/types.h>
  32. #include <linux/crypto.h>
  33. #include <linux/module.h>
  34. #include <linux/scatterlist.h>
  35. #include <asm/byteorder.h>
  36. #include <crypto/scatterwalk.h>
  37. #include <crypto/internal/hash.h>
  38. /*
  39. * User definable settings.
  40. */
  41. #define VMAC_TAG_LEN 64
  42. #define VMAC_KEY_SIZE 128/* Must be 128, 192 or 256 */
  43. #define VMAC_KEY_LEN (VMAC_KEY_SIZE/8)
  44. #define VMAC_NHBYTES 128/* Must 2^i for any 3 < i < 13 Standard = 128*/
  45. /* per-transform (per-key) context */
  46. struct vmac_tfm_ctx {
  47. struct crypto_cipher *cipher;
  48. u64 nhkey[(VMAC_NHBYTES/8)+2*(VMAC_TAG_LEN/64-1)];
  49. u64 polykey[2*VMAC_TAG_LEN/64];
  50. u64 l3key[2*VMAC_TAG_LEN/64];
  51. };
  52. /* per-request context */
  53. struct vmac_desc_ctx {
  54. union {
  55. u8 partial[VMAC_NHBYTES]; /* partial block */
  56. __le64 partial_words[VMAC_NHBYTES / 8];
  57. };
  58. unsigned int partial_size; /* size of the partial block */
  59. bool first_block_processed;
  60. u64 polytmp[2*VMAC_TAG_LEN/64]; /* running total of L2-hash */
  61. };
  62. /*
  63. * Constants and masks
  64. */
  65. #define UINT64_C(x) x##ULL
  66. static const u64 p64 = UINT64_C(0xfffffffffffffeff); /* 2^64 - 257 prime */
  67. static const u64 m62 = UINT64_C(0x3fffffffffffffff); /* 62-bit mask */
  68. static const u64 m63 = UINT64_C(0x7fffffffffffffff); /* 63-bit mask */
  69. static const u64 m64 = UINT64_C(0xffffffffffffffff); /* 64-bit mask */
  70. static const u64 mpoly = UINT64_C(0x1fffffff1fffffff); /* Poly key mask */
  71. #define pe64_to_cpup le64_to_cpup /* Prefer little endian */
  72. #ifdef __LITTLE_ENDIAN
  73. #define INDEX_HIGH 1
  74. #define INDEX_LOW 0
  75. #else
  76. #define INDEX_HIGH 0
  77. #define INDEX_LOW 1
  78. #endif
  79. /*
  80. * The following routines are used in this implementation. They are
  81. * written via macros to simulate zero-overhead call-by-reference.
  82. *
  83. * MUL64: 64x64->128-bit multiplication
  84. * PMUL64: assumes top bits cleared on inputs
  85. * ADD128: 128x128->128-bit addition
  86. */
  87. #define ADD128(rh, rl, ih, il) \
  88. do { \
  89. u64 _il = (il); \
  90. (rl) += (_il); \
  91. if ((rl) < (_il)) \
  92. (rh)++; \
  93. (rh) += (ih); \
  94. } while (0)
  95. #define MUL32(i1, i2) ((u64)(u32)(i1)*(u32)(i2))
  96. #define PMUL64(rh, rl, i1, i2) /* Assumes m doesn't overflow */ \
  97. do { \
  98. u64 _i1 = (i1), _i2 = (i2); \
  99. u64 m = MUL32(_i1, _i2>>32) + MUL32(_i1>>32, _i2); \
  100. rh = MUL32(_i1>>32, _i2>>32); \
  101. rl = MUL32(_i1, _i2); \
  102. ADD128(rh, rl, (m >> 32), (m << 32)); \
  103. } while (0)
  104. #define MUL64(rh, rl, i1, i2) \
  105. do { \
  106. u64 _i1 = (i1), _i2 = (i2); \
  107. u64 m1 = MUL32(_i1, _i2>>32); \
  108. u64 m2 = MUL32(_i1>>32, _i2); \
  109. rh = MUL32(_i1>>32, _i2>>32); \
  110. rl = MUL32(_i1, _i2); \
  111. ADD128(rh, rl, (m1 >> 32), (m1 << 32)); \
  112. ADD128(rh, rl, (m2 >> 32), (m2 << 32)); \
  113. } while (0)
  114. /*
  115. * For highest performance the L1 NH and L2 polynomial hashes should be
  116. * carefully implemented to take advantage of one's target architecture.
  117. * Here these two hash functions are defined multiple time; once for
  118. * 64-bit architectures, once for 32-bit SSE2 architectures, and once
  119. * for the rest (32-bit) architectures.
  120. * For each, nh_16 *must* be defined (works on multiples of 16 bytes).
  121. * Optionally, nh_vmac_nhbytes can be defined (for multiples of
  122. * VMAC_NHBYTES), and nh_16_2 and nh_vmac_nhbytes_2 (versions that do two
  123. * NH computations at once).
  124. */
  125. #ifdef CONFIG_64BIT
  126. #define nh_16(mp, kp, nw, rh, rl) \
  127. do { \
  128. int i; u64 th, tl; \
  129. rh = rl = 0; \
  130. for (i = 0; i < nw; i += 2) { \
  131. MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
  132. pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
  133. ADD128(rh, rl, th, tl); \
  134. } \
  135. } while (0)
  136. #define nh_16_2(mp, kp, nw, rh, rl, rh1, rl1) \
  137. do { \
  138. int i; u64 th, tl; \
  139. rh1 = rl1 = rh = rl = 0; \
  140. for (i = 0; i < nw; i += 2) { \
  141. MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
  142. pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
  143. ADD128(rh, rl, th, tl); \
  144. MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i+2], \
  145. pe64_to_cpup((mp)+i+1)+(kp)[i+3]); \
  146. ADD128(rh1, rl1, th, tl); \
  147. } \
  148. } while (0)
  149. #if (VMAC_NHBYTES >= 64) /* These versions do 64-bytes of message at a time */
  150. #define nh_vmac_nhbytes(mp, kp, nw, rh, rl) \
  151. do { \
  152. int i; u64 th, tl; \
  153. rh = rl = 0; \
  154. for (i = 0; i < nw; i += 8) { \
  155. MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
  156. pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
  157. ADD128(rh, rl, th, tl); \
  158. MUL64(th, tl, pe64_to_cpup((mp)+i+2)+(kp)[i+2], \
  159. pe64_to_cpup((mp)+i+3)+(kp)[i+3]); \
  160. ADD128(rh, rl, th, tl); \
  161. MUL64(th, tl, pe64_to_cpup((mp)+i+4)+(kp)[i+4], \
  162. pe64_to_cpup((mp)+i+5)+(kp)[i+5]); \
  163. ADD128(rh, rl, th, tl); \
  164. MUL64(th, tl, pe64_to_cpup((mp)+i+6)+(kp)[i+6], \
  165. pe64_to_cpup((mp)+i+7)+(kp)[i+7]); \
  166. ADD128(rh, rl, th, tl); \
  167. } \
  168. } while (0)
  169. #define nh_vmac_nhbytes_2(mp, kp, nw, rh, rl, rh1, rl1) \
  170. do { \
  171. int i; u64 th, tl; \
  172. rh1 = rl1 = rh = rl = 0; \
  173. for (i = 0; i < nw; i += 8) { \
  174. MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
  175. pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
  176. ADD128(rh, rl, th, tl); \
  177. MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i+2], \
  178. pe64_to_cpup((mp)+i+1)+(kp)[i+3]); \
  179. ADD128(rh1, rl1, th, tl); \
  180. MUL64(th, tl, pe64_to_cpup((mp)+i+2)+(kp)[i+2], \
  181. pe64_to_cpup((mp)+i+3)+(kp)[i+3]); \
  182. ADD128(rh, rl, th, tl); \
  183. MUL64(th, tl, pe64_to_cpup((mp)+i+2)+(kp)[i+4], \
  184. pe64_to_cpup((mp)+i+3)+(kp)[i+5]); \
  185. ADD128(rh1, rl1, th, tl); \
  186. MUL64(th, tl, pe64_to_cpup((mp)+i+4)+(kp)[i+4], \
  187. pe64_to_cpup((mp)+i+5)+(kp)[i+5]); \
  188. ADD128(rh, rl, th, tl); \
  189. MUL64(th, tl, pe64_to_cpup((mp)+i+4)+(kp)[i+6], \
  190. pe64_to_cpup((mp)+i+5)+(kp)[i+7]); \
  191. ADD128(rh1, rl1, th, tl); \
  192. MUL64(th, tl, pe64_to_cpup((mp)+i+6)+(kp)[i+6], \
  193. pe64_to_cpup((mp)+i+7)+(kp)[i+7]); \
  194. ADD128(rh, rl, th, tl); \
  195. MUL64(th, tl, pe64_to_cpup((mp)+i+6)+(kp)[i+8], \
  196. pe64_to_cpup((mp)+i+7)+(kp)[i+9]); \
  197. ADD128(rh1, rl1, th, tl); \
  198. } \
  199. } while (0)
  200. #endif
  201. #define poly_step(ah, al, kh, kl, mh, ml) \
  202. do { \
  203. u64 t1h, t1l, t2h, t2l, t3h, t3l, z = 0; \
  204. /* compute ab*cd, put bd into result registers */ \
  205. PMUL64(t3h, t3l, al, kh); \
  206. PMUL64(t2h, t2l, ah, kl); \
  207. PMUL64(t1h, t1l, ah, 2*kh); \
  208. PMUL64(ah, al, al, kl); \
  209. /* add 2 * ac to result */ \
  210. ADD128(ah, al, t1h, t1l); \
  211. /* add together ad + bc */ \
  212. ADD128(t2h, t2l, t3h, t3l); \
  213. /* now (ah,al), (t2l,2*t2h) need summing */ \
  214. /* first add the high registers, carrying into t2h */ \
  215. ADD128(t2h, ah, z, t2l); \
  216. /* double t2h and add top bit of ah */ \
  217. t2h = 2 * t2h + (ah >> 63); \
  218. ah &= m63; \
  219. /* now add the low registers */ \
  220. ADD128(ah, al, mh, ml); \
  221. ADD128(ah, al, z, t2h); \
  222. } while (0)
  223. #else /* ! CONFIG_64BIT */
  224. #ifndef nh_16
  225. #define nh_16(mp, kp, nw, rh, rl) \
  226. do { \
  227. u64 t1, t2, m1, m2, t; \
  228. int i; \
  229. rh = rl = t = 0; \
  230. for (i = 0; i < nw; i += 2) { \
  231. t1 = pe64_to_cpup(mp+i) + kp[i]; \
  232. t2 = pe64_to_cpup(mp+i+1) + kp[i+1]; \
  233. m2 = MUL32(t1 >> 32, t2); \
  234. m1 = MUL32(t1, t2 >> 32); \
  235. ADD128(rh, rl, MUL32(t1 >> 32, t2 >> 32), \
  236. MUL32(t1, t2)); \
  237. rh += (u64)(u32)(m1 >> 32) \
  238. + (u32)(m2 >> 32); \
  239. t += (u64)(u32)m1 + (u32)m2; \
  240. } \
  241. ADD128(rh, rl, (t >> 32), (t << 32)); \
  242. } while (0)
  243. #endif
  244. static void poly_step_func(u64 *ahi, u64 *alo,
  245. const u64 *kh, const u64 *kl,
  246. const u64 *mh, const u64 *ml)
  247. {
  248. #define a0 (*(((u32 *)alo)+INDEX_LOW))
  249. #define a1 (*(((u32 *)alo)+INDEX_HIGH))
  250. #define a2 (*(((u32 *)ahi)+INDEX_LOW))
  251. #define a3 (*(((u32 *)ahi)+INDEX_HIGH))
  252. #define k0 (*(((u32 *)kl)+INDEX_LOW))
  253. #define k1 (*(((u32 *)kl)+INDEX_HIGH))
  254. #define k2 (*(((u32 *)kh)+INDEX_LOW))
  255. #define k3 (*(((u32 *)kh)+INDEX_HIGH))
  256. u64 p, q, t;
  257. u32 t2;
  258. p = MUL32(a3, k3);
  259. p += p;
  260. p += *(u64 *)mh;
  261. p += MUL32(a0, k2);
  262. p += MUL32(a1, k1);
  263. p += MUL32(a2, k0);
  264. t = (u32)(p);
  265. p >>= 32;
  266. p += MUL32(a0, k3);
  267. p += MUL32(a1, k2);
  268. p += MUL32(a2, k1);
  269. p += MUL32(a3, k0);
  270. t |= ((u64)((u32)p & 0x7fffffff)) << 32;
  271. p >>= 31;
  272. p += (u64)(((u32 *)ml)[INDEX_LOW]);
  273. p += MUL32(a0, k0);
  274. q = MUL32(a1, k3);
  275. q += MUL32(a2, k2);
  276. q += MUL32(a3, k1);
  277. q += q;
  278. p += q;
  279. t2 = (u32)(p);
  280. p >>= 32;
  281. p += (u64)(((u32 *)ml)[INDEX_HIGH]);
  282. p += MUL32(a0, k1);
  283. p += MUL32(a1, k0);
  284. q = MUL32(a2, k3);
  285. q += MUL32(a3, k2);
  286. q += q;
  287. p += q;
  288. *(u64 *)(alo) = (p << 32) | t2;
  289. p >>= 32;
  290. *(u64 *)(ahi) = p + t;
  291. #undef a0
  292. #undef a1
  293. #undef a2
  294. #undef a3
  295. #undef k0
  296. #undef k1
  297. #undef k2
  298. #undef k3
  299. }
  300. #define poly_step(ah, al, kh, kl, mh, ml) \
  301. poly_step_func(&(ah), &(al), &(kh), &(kl), &(mh), &(ml))
  302. #endif /* end of specialized NH and poly definitions */
  303. /* At least nh_16 is defined. Defined others as needed here */
  304. #ifndef nh_16_2
  305. #define nh_16_2(mp, kp, nw, rh, rl, rh2, rl2) \
  306. do { \
  307. nh_16(mp, kp, nw, rh, rl); \
  308. nh_16(mp, ((kp)+2), nw, rh2, rl2); \
  309. } while (0)
  310. #endif
  311. #ifndef nh_vmac_nhbytes
  312. #define nh_vmac_nhbytes(mp, kp, nw, rh, rl) \
  313. nh_16(mp, kp, nw, rh, rl)
  314. #endif
  315. #ifndef nh_vmac_nhbytes_2
  316. #define nh_vmac_nhbytes_2(mp, kp, nw, rh, rl, rh2, rl2) \
  317. do { \
  318. nh_vmac_nhbytes(mp, kp, nw, rh, rl); \
  319. nh_vmac_nhbytes(mp, ((kp)+2), nw, rh2, rl2); \
  320. } while (0)
  321. #endif
  322. static u64 l3hash(u64 p1, u64 p2, u64 k1, u64 k2, u64 len)
  323. {
  324. u64 rh, rl, t, z = 0;
  325. /* fully reduce (p1,p2)+(len,0) mod p127 */
  326. t = p1 >> 63;
  327. p1 &= m63;
  328. ADD128(p1, p2, len, t);
  329. /* At this point, (p1,p2) is at most 2^127+(len<<64) */
  330. t = (p1 > m63) + ((p1 == m63) && (p2 == m64));
  331. ADD128(p1, p2, z, t);
  332. p1 &= m63;
  333. /* compute (p1,p2)/(2^64-2^32) and (p1,p2)%(2^64-2^32) */
  334. t = p1 + (p2 >> 32);
  335. t += (t >> 32);
  336. t += (u32)t > 0xfffffffeu;
  337. p1 += (t >> 32);
  338. p2 += (p1 << 32);
  339. /* compute (p1+k1)%p64 and (p2+k2)%p64 */
  340. p1 += k1;
  341. p1 += (0 - (p1 < k1)) & 257;
  342. p2 += k2;
  343. p2 += (0 - (p2 < k2)) & 257;
  344. /* compute (p1+k1)*(p2+k2)%p64 */
  345. MUL64(rh, rl, p1, p2);
  346. t = rh >> 56;
  347. ADD128(t, rl, z, rh);
  348. rh <<= 8;
  349. ADD128(t, rl, z, rh);
  350. t += t << 8;
  351. rl += t;
  352. rl += (0 - (rl < t)) & 257;
  353. rl += (0 - (rl > p64-1)) & 257;
  354. return rl;
  355. }
  356. /* L1 and L2-hash one or more VMAC_NHBYTES-byte blocks */
  357. static void vhash_blocks(const struct vmac_tfm_ctx *tctx,
  358. struct vmac_desc_ctx *dctx,
  359. const __le64 *mptr, unsigned int blocks)
  360. {
  361. const u64 *kptr = tctx->nhkey;
  362. const u64 pkh = tctx->polykey[0];
  363. const u64 pkl = tctx->polykey[1];
  364. u64 ch = dctx->polytmp[0];
  365. u64 cl = dctx->polytmp[1];
  366. u64 rh, rl;
  367. if (!dctx->first_block_processed) {
  368. dctx->first_block_processed = true;
  369. nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, rh, rl);
  370. rh &= m62;
  371. ADD128(ch, cl, rh, rl);
  372. mptr += (VMAC_NHBYTES/sizeof(u64));
  373. blocks--;
  374. }
  375. while (blocks--) {
  376. nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, rh, rl);
  377. rh &= m62;
  378. poly_step(ch, cl, pkh, pkl, rh, rl);
  379. mptr += (VMAC_NHBYTES/sizeof(u64));
  380. }
  381. dctx->polytmp[0] = ch;
  382. dctx->polytmp[1] = cl;
  383. }
  384. static int vmac_setkey(struct crypto_shash *tfm,
  385. const u8 *key, unsigned int keylen)
  386. {
  387. struct vmac_tfm_ctx *tctx = crypto_shash_ctx(tfm);
  388. __be64 out[2];
  389. u8 in[16] = { 0 };
  390. unsigned int i;
  391. int err;
  392. if (keylen != VMAC_KEY_LEN) {
  393. crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  394. return -EINVAL;
  395. }
  396. err = crypto_cipher_setkey(tctx->cipher, key, keylen);
  397. if (err)
  398. return err;
  399. /* Fill nh key */
  400. in[0] = 0x80;
  401. for (i = 0; i < ARRAY_SIZE(tctx->nhkey); i += 2) {
  402. crypto_cipher_encrypt_one(tctx->cipher, (u8 *)out, in);
  403. tctx->nhkey[i] = be64_to_cpu(out[0]);
  404. tctx->nhkey[i+1] = be64_to_cpu(out[1]);
  405. in[15]++;
  406. }
  407. /* Fill poly key */
  408. in[0] = 0xC0;
  409. in[15] = 0;
  410. for (i = 0; i < ARRAY_SIZE(tctx->polykey); i += 2) {
  411. crypto_cipher_encrypt_one(tctx->cipher, (u8 *)out, in);
  412. tctx->polykey[i] = be64_to_cpu(out[0]) & mpoly;
  413. tctx->polykey[i+1] = be64_to_cpu(out[1]) & mpoly;
  414. in[15]++;
  415. }
  416. /* Fill ip key */
  417. in[0] = 0xE0;
  418. in[15] = 0;
  419. for (i = 0; i < ARRAY_SIZE(tctx->l3key); i += 2) {
  420. do {
  421. crypto_cipher_encrypt_one(tctx->cipher, (u8 *)out, in);
  422. tctx->l3key[i] = be64_to_cpu(out[0]);
  423. tctx->l3key[i+1] = be64_to_cpu(out[1]);
  424. in[15]++;
  425. } while (tctx->l3key[i] >= p64 || tctx->l3key[i+1] >= p64);
  426. }
  427. return 0;
  428. }
  429. static int vmac_init(struct shash_desc *desc)
  430. {
  431. const struct vmac_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
  432. struct vmac_desc_ctx *dctx = shash_desc_ctx(desc);
  433. dctx->partial_size = 0;
  434. dctx->first_block_processed = false;
  435. memcpy(dctx->polytmp, tctx->polykey, sizeof(dctx->polytmp));
  436. return 0;
  437. }
  438. static int vmac_update(struct shash_desc *desc, const u8 *p, unsigned int len)
  439. {
  440. const struct vmac_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
  441. struct vmac_desc_ctx *dctx = shash_desc_ctx(desc);
  442. unsigned int n;
  443. if (dctx->partial_size) {
  444. n = min(len, VMAC_NHBYTES - dctx->partial_size);
  445. memcpy(&dctx->partial[dctx->partial_size], p, n);
  446. dctx->partial_size += n;
  447. p += n;
  448. len -= n;
  449. if (dctx->partial_size == VMAC_NHBYTES) {
  450. vhash_blocks(tctx, dctx, dctx->partial_words, 1);
  451. dctx->partial_size = 0;
  452. }
  453. }
  454. if (len >= VMAC_NHBYTES) {
  455. n = round_down(len, VMAC_NHBYTES);
  456. /* TODO: 'p' may be misaligned here */
  457. vhash_blocks(tctx, dctx, (const __le64 *)p, n / VMAC_NHBYTES);
  458. p += n;
  459. len -= n;
  460. }
  461. if (len) {
  462. memcpy(dctx->partial, p, len);
  463. dctx->partial_size = len;
  464. }
  465. return 0;
  466. }
  467. static u64 vhash_final(const struct vmac_tfm_ctx *tctx,
  468. struct vmac_desc_ctx *dctx)
  469. {
  470. unsigned int partial = dctx->partial_size;
  471. u64 ch = dctx->polytmp[0];
  472. u64 cl = dctx->polytmp[1];
  473. /* L1 and L2-hash the final block if needed */
  474. if (partial) {
  475. /* Zero-pad to next 128-bit boundary */
  476. unsigned int n = round_up(partial, 16);
  477. u64 rh, rl;
  478. memset(&dctx->partial[partial], 0, n - partial);
  479. nh_16(dctx->partial_words, tctx->nhkey, n / 8, rh, rl);
  480. rh &= m62;
  481. if (dctx->first_block_processed)
  482. poly_step(ch, cl, tctx->polykey[0], tctx->polykey[1],
  483. rh, rl);
  484. else
  485. ADD128(ch, cl, rh, rl);
  486. }
  487. /* L3-hash the 128-bit output of L2-hash */
  488. return l3hash(ch, cl, tctx->l3key[0], tctx->l3key[1], partial * 8);
  489. }
  490. static int vmac_final(struct shash_desc *desc, u8 *out)
  491. {
  492. const struct vmac_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
  493. struct vmac_desc_ctx *dctx = shash_desc_ctx(desc);
  494. static const u8 nonce[16] = {}; /* TODO: this is insecure */
  495. union {
  496. u8 bytes[16];
  497. __be64 pads[2];
  498. } block;
  499. int index;
  500. u64 hash, pad;
  501. /* Finish calculating the VHASH of the message */
  502. hash = vhash_final(tctx, dctx);
  503. /* Generate pseudorandom pad by encrypting the nonce */
  504. memcpy(&block, nonce, 16);
  505. index = block.bytes[15] & 1;
  506. block.bytes[15] &= ~1;
  507. crypto_cipher_encrypt_one(tctx->cipher, block.bytes, block.bytes);
  508. pad = be64_to_cpu(block.pads[index]);
  509. /* The VMAC is the sum of VHASH and the pseudorandom pad */
  510. put_unaligned_le64(hash + pad, out);
  511. return 0;
  512. }
  513. static int vmac_init_tfm(struct crypto_tfm *tfm)
  514. {
  515. struct crypto_instance *inst = crypto_tfm_alg_instance(tfm);
  516. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  517. struct vmac_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
  518. struct crypto_cipher *cipher;
  519. cipher = crypto_spawn_cipher(spawn);
  520. if (IS_ERR(cipher))
  521. return PTR_ERR(cipher);
  522. tctx->cipher = cipher;
  523. return 0;
  524. }
  525. static void vmac_exit_tfm(struct crypto_tfm *tfm)
  526. {
  527. struct vmac_tfm_ctx *tctx = crypto_tfm_ctx(tfm);
  528. crypto_free_cipher(tctx->cipher);
  529. }
  530. static int vmac_create(struct crypto_template *tmpl, struct rtattr **tb)
  531. {
  532. struct shash_instance *inst;
  533. struct crypto_alg *alg;
  534. int err;
  535. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
  536. if (err)
  537. return err;
  538. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
  539. CRYPTO_ALG_TYPE_MASK);
  540. if (IS_ERR(alg))
  541. return PTR_ERR(alg);
  542. err = -EINVAL;
  543. if (alg->cra_blocksize != 16)
  544. goto out_put_alg;
  545. inst = shash_alloc_instance("vmac", alg);
  546. err = PTR_ERR(inst);
  547. if (IS_ERR(inst))
  548. goto out_put_alg;
  549. err = crypto_init_spawn(shash_instance_ctx(inst), alg,
  550. shash_crypto_instance(inst),
  551. CRYPTO_ALG_TYPE_MASK);
  552. if (err)
  553. goto out_free_inst;
  554. inst->alg.base.cra_priority = alg->cra_priority;
  555. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  556. inst->alg.base.cra_alignmask = alg->cra_alignmask;
  557. inst->alg.base.cra_ctxsize = sizeof(struct vmac_tfm_ctx);
  558. inst->alg.base.cra_init = vmac_init_tfm;
  559. inst->alg.base.cra_exit = vmac_exit_tfm;
  560. inst->alg.descsize = sizeof(struct vmac_desc_ctx);
  561. inst->alg.digestsize = VMAC_TAG_LEN / 8;
  562. inst->alg.init = vmac_init;
  563. inst->alg.update = vmac_update;
  564. inst->alg.final = vmac_final;
  565. inst->alg.setkey = vmac_setkey;
  566. err = shash_register_instance(tmpl, inst);
  567. if (err) {
  568. out_free_inst:
  569. shash_free_instance(shash_crypto_instance(inst));
  570. }
  571. out_put_alg:
  572. crypto_mod_put(alg);
  573. return err;
  574. }
  575. static struct crypto_template vmac_tmpl = {
  576. .name = "vmac",
  577. .create = vmac_create,
  578. .free = shash_free_instance,
  579. .module = THIS_MODULE,
  580. };
  581. static int __init vmac_module_init(void)
  582. {
  583. return crypto_register_template(&vmac_tmpl);
  584. }
  585. static void __exit vmac_module_exit(void)
  586. {
  587. crypto_unregister_template(&vmac_tmpl);
  588. }
  589. module_init(vmac_module_init);
  590. module_exit(vmac_module_exit);
  591. MODULE_LICENSE("GPL");
  592. MODULE_DESCRIPTION("VMAC hash algorithm");
  593. MODULE_ALIAS_CRYPTO("vmac");