vmac.c 19 KB

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