vmac.c 19 KB

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