vmac.c 19 KB

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