vmac.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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/scatterlist.h>
  29. #include <asm/byteorder.h>
  30. #include <crypto/scatterwalk.h>
  31. #include <crypto/vmac.h>
  32. #include <crypto/internal/hash.h>
  33. /*
  34. * Constants and masks
  35. */
  36. #define UINT64_C(x) x##ULL
  37. const u64 p64 = UINT64_C(0xfffffffffffffeff); /* 2^64 - 257 prime */
  38. const u64 m62 = UINT64_C(0x3fffffffffffffff); /* 62-bit mask */
  39. const u64 m63 = UINT64_C(0x7fffffffffffffff); /* 63-bit mask */
  40. const u64 m64 = UINT64_C(0xffffffffffffffff); /* 64-bit mask */
  41. const u64 mpoly = UINT64_C(0x1fffffff1fffffff); /* Poly key mask */
  42. #define pe64_to_cpup le64_to_cpup /* Prefer little endian */
  43. #ifdef __LITTLE_ENDIAN
  44. #define INDEX_HIGH 1
  45. #define INDEX_LOW 0
  46. #else
  47. #define INDEX_HIGH 0
  48. #define INDEX_LOW 1
  49. #endif
  50. /*
  51. * The following routines are used in this implementation. They are
  52. * written via macros to simulate zero-overhead call-by-reference.
  53. *
  54. * MUL64: 64x64->128-bit multiplication
  55. * PMUL64: assumes top bits cleared on inputs
  56. * ADD128: 128x128->128-bit addition
  57. */
  58. #define ADD128(rh, rl, ih, il) \
  59. do { \
  60. u64 _il = (il); \
  61. (rl) += (_il); \
  62. if ((rl) < (_il)) \
  63. (rh)++; \
  64. (rh) += (ih); \
  65. } while (0)
  66. #define MUL32(i1, i2) ((u64)(u32)(i1)*(u32)(i2))
  67. #define PMUL64(rh, rl, i1, i2) /* Assumes m doesn't overflow */ \
  68. do { \
  69. u64 _i1 = (i1), _i2 = (i2); \
  70. u64 m = MUL32(_i1, _i2>>32) + MUL32(_i1>>32, _i2); \
  71. rh = MUL32(_i1>>32, _i2>>32); \
  72. rl = MUL32(_i1, _i2); \
  73. ADD128(rh, rl, (m >> 32), (m << 32)); \
  74. } while (0)
  75. #define MUL64(rh, rl, i1, i2) \
  76. do { \
  77. u64 _i1 = (i1), _i2 = (i2); \
  78. u64 m1 = MUL32(_i1, _i2>>32); \
  79. u64 m2 = MUL32(_i1>>32, _i2); \
  80. rh = MUL32(_i1>>32, _i2>>32); \
  81. rl = MUL32(_i1, _i2); \
  82. ADD128(rh, rl, (m1 >> 32), (m1 << 32)); \
  83. ADD128(rh, rl, (m2 >> 32), (m2 << 32)); \
  84. } while (0)
  85. /*
  86. * For highest performance the L1 NH and L2 polynomial hashes should be
  87. * carefully implemented to take advantage of one's target architecture.
  88. * Here these two hash functions are defined multiple time; once for
  89. * 64-bit architectures, once for 32-bit SSE2 architectures, and once
  90. * for the rest (32-bit) architectures.
  91. * For each, nh_16 *must* be defined (works on multiples of 16 bytes).
  92. * Optionally, nh_vmac_nhbytes can be defined (for multiples of
  93. * VMAC_NHBYTES), and nh_16_2 and nh_vmac_nhbytes_2 (versions that do two
  94. * NH computations at once).
  95. */
  96. #ifdef CONFIG_64BIT
  97. #define nh_16(mp, kp, nw, rh, rl) \
  98. do { \
  99. int i; u64 th, tl; \
  100. rh = rl = 0; \
  101. for (i = 0; i < nw; i += 2) { \
  102. MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
  103. pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
  104. ADD128(rh, rl, th, tl); \
  105. } \
  106. } while (0)
  107. #define nh_16_2(mp, kp, nw, rh, rl, rh1, rl1) \
  108. do { \
  109. int i; u64 th, tl; \
  110. rh1 = rl1 = rh = rl = 0; \
  111. for (i = 0; i < nw; i += 2) { \
  112. MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
  113. pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
  114. ADD128(rh, rl, th, tl); \
  115. MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i+2], \
  116. pe64_to_cpup((mp)+i+1)+(kp)[i+3]); \
  117. ADD128(rh1, rl1, th, tl); \
  118. } \
  119. } while (0)
  120. #if (VMAC_NHBYTES >= 64) /* These versions do 64-bytes of message at a time */
  121. #define nh_vmac_nhbytes(mp, kp, nw, rh, rl) \
  122. do { \
  123. int i; u64 th, tl; \
  124. rh = rl = 0; \
  125. for (i = 0; i < nw; i += 8) { \
  126. MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
  127. pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
  128. ADD128(rh, rl, th, tl); \
  129. MUL64(th, tl, pe64_to_cpup((mp)+i+2)+(kp)[i+2], \
  130. pe64_to_cpup((mp)+i+3)+(kp)[i+3]); \
  131. ADD128(rh, rl, th, tl); \
  132. MUL64(th, tl, pe64_to_cpup((mp)+i+4)+(kp)[i+4], \
  133. pe64_to_cpup((mp)+i+5)+(kp)[i+5]); \
  134. ADD128(rh, rl, th, tl); \
  135. MUL64(th, tl, pe64_to_cpup((mp)+i+6)+(kp)[i+6], \
  136. pe64_to_cpup((mp)+i+7)+(kp)[i+7]); \
  137. ADD128(rh, rl, th, tl); \
  138. } \
  139. } while (0)
  140. #define nh_vmac_nhbytes_2(mp, kp, nw, rh, rl, rh1, rl1) \
  141. do { \
  142. int i; u64 th, tl; \
  143. rh1 = rl1 = rh = rl = 0; \
  144. for (i = 0; i < nw; i += 8) { \
  145. MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i], \
  146. pe64_to_cpup((mp)+i+1)+(kp)[i+1]); \
  147. ADD128(rh, rl, th, tl); \
  148. MUL64(th, tl, pe64_to_cpup((mp)+i)+(kp)[i+2], \
  149. pe64_to_cpup((mp)+i+1)+(kp)[i+3]); \
  150. ADD128(rh1, rl1, th, tl); \
  151. MUL64(th, tl, pe64_to_cpup((mp)+i+2)+(kp)[i+2], \
  152. pe64_to_cpup((mp)+i+3)+(kp)[i+3]); \
  153. ADD128(rh, rl, th, tl); \
  154. MUL64(th, tl, pe64_to_cpup((mp)+i+2)+(kp)[i+4], \
  155. pe64_to_cpup((mp)+i+3)+(kp)[i+5]); \
  156. ADD128(rh1, rl1, th, tl); \
  157. MUL64(th, tl, pe64_to_cpup((mp)+i+4)+(kp)[i+4], \
  158. pe64_to_cpup((mp)+i+5)+(kp)[i+5]); \
  159. ADD128(rh, rl, th, tl); \
  160. MUL64(th, tl, pe64_to_cpup((mp)+i+4)+(kp)[i+6], \
  161. pe64_to_cpup((mp)+i+5)+(kp)[i+7]); \
  162. ADD128(rh1, rl1, th, tl); \
  163. MUL64(th, tl, pe64_to_cpup((mp)+i+6)+(kp)[i+6], \
  164. pe64_to_cpup((mp)+i+7)+(kp)[i+7]); \
  165. ADD128(rh, rl, th, tl); \
  166. MUL64(th, tl, pe64_to_cpup((mp)+i+6)+(kp)[i+8], \
  167. pe64_to_cpup((mp)+i+7)+(kp)[i+9]); \
  168. ADD128(rh1, rl1, th, tl); \
  169. } \
  170. } while (0)
  171. #endif
  172. #define poly_step(ah, al, kh, kl, mh, ml) \
  173. do { \
  174. u64 t1h, t1l, t2h, t2l, t3h, t3l, z = 0; \
  175. /* compute ab*cd, put bd into result registers */ \
  176. PMUL64(t3h, t3l, al, kh); \
  177. PMUL64(t2h, t2l, ah, kl); \
  178. PMUL64(t1h, t1l, ah, 2*kh); \
  179. PMUL64(ah, al, al, kl); \
  180. /* add 2 * ac to result */ \
  181. ADD128(ah, al, t1h, t1l); \
  182. /* add together ad + bc */ \
  183. ADD128(t2h, t2l, t3h, t3l); \
  184. /* now (ah,al), (t2l,2*t2h) need summing */ \
  185. /* first add the high registers, carrying into t2h */ \
  186. ADD128(t2h, ah, z, t2l); \
  187. /* double t2h and add top bit of ah */ \
  188. t2h = 2 * t2h + (ah >> 63); \
  189. ah &= m63; \
  190. /* now add the low registers */ \
  191. ADD128(ah, al, mh, ml); \
  192. ADD128(ah, al, z, t2h); \
  193. } while (0)
  194. #else /* ! CONFIG_64BIT */
  195. #ifndef nh_16
  196. #define nh_16(mp, kp, nw, rh, rl) \
  197. do { \
  198. u64 t1, t2, m1, m2, t; \
  199. int i; \
  200. rh = rl = t = 0; \
  201. for (i = 0; i < nw; i += 2) { \
  202. t1 = pe64_to_cpup(mp+i) + kp[i]; \
  203. t2 = pe64_to_cpup(mp+i+1) + kp[i+1]; \
  204. m2 = MUL32(t1 >> 32, t2); \
  205. m1 = MUL32(t1, t2 >> 32); \
  206. ADD128(rh, rl, MUL32(t1 >> 32, t2 >> 32), \
  207. MUL32(t1, t2)); \
  208. rh += (u64)(u32)(m1 >> 32) \
  209. + (u32)(m2 >> 32); \
  210. t += (u64)(u32)m1 + (u32)m2; \
  211. } \
  212. ADD128(rh, rl, (t >> 32), (t << 32)); \
  213. } while (0)
  214. #endif
  215. static void poly_step_func(u64 *ahi, u64 *alo,
  216. const u64 *kh, const u64 *kl,
  217. const u64 *mh, const u64 *ml)
  218. {
  219. #define a0 (*(((u32 *)alo)+INDEX_LOW))
  220. #define a1 (*(((u32 *)alo)+INDEX_HIGH))
  221. #define a2 (*(((u32 *)ahi)+INDEX_LOW))
  222. #define a3 (*(((u32 *)ahi)+INDEX_HIGH))
  223. #define k0 (*(((u32 *)kl)+INDEX_LOW))
  224. #define k1 (*(((u32 *)kl)+INDEX_HIGH))
  225. #define k2 (*(((u32 *)kh)+INDEX_LOW))
  226. #define k3 (*(((u32 *)kh)+INDEX_HIGH))
  227. u64 p, q, t;
  228. u32 t2;
  229. p = MUL32(a3, k3);
  230. p += p;
  231. p += *(u64 *)mh;
  232. p += MUL32(a0, k2);
  233. p += MUL32(a1, k1);
  234. p += MUL32(a2, k0);
  235. t = (u32)(p);
  236. p >>= 32;
  237. p += MUL32(a0, k3);
  238. p += MUL32(a1, k2);
  239. p += MUL32(a2, k1);
  240. p += MUL32(a3, k0);
  241. t |= ((u64)((u32)p & 0x7fffffff)) << 32;
  242. p >>= 31;
  243. p += (u64)(((u32 *)ml)[INDEX_LOW]);
  244. p += MUL32(a0, k0);
  245. q = MUL32(a1, k3);
  246. q += MUL32(a2, k2);
  247. q += MUL32(a3, k1);
  248. q += q;
  249. p += q;
  250. t2 = (u32)(p);
  251. p >>= 32;
  252. p += (u64)(((u32 *)ml)[INDEX_HIGH]);
  253. p += MUL32(a0, k1);
  254. p += MUL32(a1, k0);
  255. q = MUL32(a2, k3);
  256. q += MUL32(a3, k2);
  257. q += q;
  258. p += q;
  259. *(u64 *)(alo) = (p << 32) | t2;
  260. p >>= 32;
  261. *(u64 *)(ahi) = p + t;
  262. #undef a0
  263. #undef a1
  264. #undef a2
  265. #undef a3
  266. #undef k0
  267. #undef k1
  268. #undef k2
  269. #undef k3
  270. }
  271. #define poly_step(ah, al, kh, kl, mh, ml) \
  272. poly_step_func(&(ah), &(al), &(kh), &(kl), &(mh), &(ml))
  273. #endif /* end of specialized NH and poly definitions */
  274. /* At least nh_16 is defined. Defined others as needed here */
  275. #ifndef nh_16_2
  276. #define nh_16_2(mp, kp, nw, rh, rl, rh2, rl2) \
  277. do { \
  278. nh_16(mp, kp, nw, rh, rl); \
  279. nh_16(mp, ((kp)+2), nw, rh2, rl2); \
  280. } while (0)
  281. #endif
  282. #ifndef nh_vmac_nhbytes
  283. #define nh_vmac_nhbytes(mp, kp, nw, rh, rl) \
  284. nh_16(mp, kp, nw, rh, rl)
  285. #endif
  286. #ifndef nh_vmac_nhbytes_2
  287. #define nh_vmac_nhbytes_2(mp, kp, nw, rh, rl, rh2, rl2) \
  288. do { \
  289. nh_vmac_nhbytes(mp, kp, nw, rh, rl); \
  290. nh_vmac_nhbytes(mp, ((kp)+2), nw, rh2, rl2); \
  291. } while (0)
  292. #endif
  293. static void vhash_abort(struct vmac_ctx *ctx)
  294. {
  295. ctx->polytmp[0] = ctx->polykey[0] ;
  296. ctx->polytmp[1] = ctx->polykey[1] ;
  297. ctx->first_block_processed = 0;
  298. }
  299. static u64 l3hash(u64 p1, u64 p2, u64 k1, u64 k2, u64 len)
  300. {
  301. u64 rh, rl, t, z = 0;
  302. /* fully reduce (p1,p2)+(len,0) mod p127 */
  303. t = p1 >> 63;
  304. p1 &= m63;
  305. ADD128(p1, p2, len, t);
  306. /* At this point, (p1,p2) is at most 2^127+(len<<64) */
  307. t = (p1 > m63) + ((p1 == m63) && (p2 == m64));
  308. ADD128(p1, p2, z, t);
  309. p1 &= m63;
  310. /* compute (p1,p2)/(2^64-2^32) and (p1,p2)%(2^64-2^32) */
  311. t = p1 + (p2 >> 32);
  312. t += (t >> 32);
  313. t += (u32)t > 0xfffffffeu;
  314. p1 += (t >> 32);
  315. p2 += (p1 << 32);
  316. /* compute (p1+k1)%p64 and (p2+k2)%p64 */
  317. p1 += k1;
  318. p1 += (0 - (p1 < k1)) & 257;
  319. p2 += k2;
  320. p2 += (0 - (p2 < k2)) & 257;
  321. /* compute (p1+k1)*(p2+k2)%p64 */
  322. MUL64(rh, rl, p1, p2);
  323. t = rh >> 56;
  324. ADD128(t, rl, z, rh);
  325. rh <<= 8;
  326. ADD128(t, rl, z, rh);
  327. t += t << 8;
  328. rl += t;
  329. rl += (0 - (rl < t)) & 257;
  330. rl += (0 - (rl > p64-1)) & 257;
  331. return rl;
  332. }
  333. static void vhash_update(const unsigned char *m,
  334. unsigned int mbytes, /* Pos multiple of VMAC_NHBYTES */
  335. struct vmac_ctx *ctx)
  336. {
  337. u64 rh, rl, *mptr;
  338. const u64 *kptr = (u64 *)ctx->nhkey;
  339. int i;
  340. u64 ch, cl;
  341. u64 pkh = ctx->polykey[0];
  342. u64 pkl = ctx->polykey[1];
  343. mptr = (u64 *)m;
  344. i = mbytes / VMAC_NHBYTES; /* Must be non-zero */
  345. ch = ctx->polytmp[0];
  346. cl = ctx->polytmp[1];
  347. if (!ctx->first_block_processed) {
  348. ctx->first_block_processed = 1;
  349. nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, rh, rl);
  350. rh &= m62;
  351. ADD128(ch, cl, rh, rl);
  352. mptr += (VMAC_NHBYTES/sizeof(u64));
  353. i--;
  354. }
  355. while (i--) {
  356. nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, rh, rl);
  357. rh &= m62;
  358. poly_step(ch, cl, pkh, pkl, rh, rl);
  359. mptr += (VMAC_NHBYTES/sizeof(u64));
  360. }
  361. ctx->polytmp[0] = ch;
  362. ctx->polytmp[1] = cl;
  363. }
  364. static u64 vhash(unsigned char m[], unsigned int mbytes,
  365. u64 *tagl, struct vmac_ctx *ctx)
  366. {
  367. u64 rh, rl, *mptr;
  368. const u64 *kptr = (u64 *)ctx->nhkey;
  369. int i, remaining;
  370. u64 ch, cl;
  371. u64 pkh = ctx->polykey[0];
  372. u64 pkl = ctx->polykey[1];
  373. mptr = (u64 *)m;
  374. i = mbytes / VMAC_NHBYTES;
  375. remaining = mbytes % VMAC_NHBYTES;
  376. if (ctx->first_block_processed) {
  377. ch = ctx->polytmp[0];
  378. cl = ctx->polytmp[1];
  379. } else if (i) {
  380. nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, ch, cl);
  381. ch &= m62;
  382. ADD128(ch, cl, pkh, pkl);
  383. mptr += (VMAC_NHBYTES/sizeof(u64));
  384. i--;
  385. } else if (remaining) {
  386. nh_16(mptr, kptr, 2*((remaining+15)/16), ch, cl);
  387. ch &= m62;
  388. ADD128(ch, cl, pkh, pkl);
  389. mptr += (VMAC_NHBYTES/sizeof(u64));
  390. goto do_l3;
  391. } else {/* Empty String */
  392. ch = pkh; cl = pkl;
  393. goto do_l3;
  394. }
  395. while (i--) {
  396. nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, rh, rl);
  397. rh &= m62;
  398. poly_step(ch, cl, pkh, pkl, rh, rl);
  399. mptr += (VMAC_NHBYTES/sizeof(u64));
  400. }
  401. if (remaining) {
  402. nh_16(mptr, kptr, 2*((remaining+15)/16), rh, rl);
  403. rh &= m62;
  404. poly_step(ch, cl, pkh, pkl, rh, rl);
  405. }
  406. do_l3:
  407. vhash_abort(ctx);
  408. remaining *= 8;
  409. return l3hash(ch, cl, ctx->l3key[0], ctx->l3key[1], remaining);
  410. }
  411. static u64 vmac(unsigned char m[], unsigned int mbytes,
  412. unsigned char n[16], u64 *tagl,
  413. struct vmac_ctx_t *ctx)
  414. {
  415. u64 *in_n, *out_p;
  416. u64 p, h;
  417. int i;
  418. in_n = ctx->__vmac_ctx.cached_nonce;
  419. out_p = ctx->__vmac_ctx.cached_aes;
  420. i = n[15] & 1;
  421. if ((*(u64 *)(n+8) != in_n[1]) || (*(u64 *)(n) != in_n[0])) {
  422. in_n[0] = *(u64 *)(n);
  423. in_n[1] = *(u64 *)(n+8);
  424. ((unsigned char *)in_n)[15] &= 0xFE;
  425. crypto_cipher_encrypt_one(ctx->child,
  426. (unsigned char *)out_p, (unsigned char *)in_n);
  427. ((unsigned char *)in_n)[15] |= (unsigned char)(1-i);
  428. }
  429. p = be64_to_cpup(out_p + i);
  430. h = vhash(m, mbytes, (u64 *)0, &ctx->__vmac_ctx);
  431. return le64_to_cpu(p + h);
  432. }
  433. static int vmac_set_key(unsigned char user_key[], struct vmac_ctx_t *ctx)
  434. {
  435. u64 in[2] = {0}, out[2];
  436. unsigned i;
  437. int err = 0;
  438. err = crypto_cipher_setkey(ctx->child, user_key, VMAC_KEY_LEN);
  439. if (err)
  440. return err;
  441. /* Fill nh key */
  442. ((unsigned char *)in)[0] = 0x80;
  443. for (i = 0; i < sizeof(ctx->__vmac_ctx.nhkey)/8; i += 2) {
  444. crypto_cipher_encrypt_one(ctx->child,
  445. (unsigned char *)out, (unsigned char *)in);
  446. ctx->__vmac_ctx.nhkey[i] = be64_to_cpup(out);
  447. ctx->__vmac_ctx.nhkey[i+1] = be64_to_cpup(out+1);
  448. ((unsigned char *)in)[15] += 1;
  449. }
  450. /* Fill poly key */
  451. ((unsigned char *)in)[0] = 0xC0;
  452. in[1] = 0;
  453. for (i = 0; i < sizeof(ctx->__vmac_ctx.polykey)/8; i += 2) {
  454. crypto_cipher_encrypt_one(ctx->child,
  455. (unsigned char *)out, (unsigned char *)in);
  456. ctx->__vmac_ctx.polytmp[i] =
  457. ctx->__vmac_ctx.polykey[i] =
  458. be64_to_cpup(out) & mpoly;
  459. ctx->__vmac_ctx.polytmp[i+1] =
  460. ctx->__vmac_ctx.polykey[i+1] =
  461. be64_to_cpup(out+1) & mpoly;
  462. ((unsigned char *)in)[15] += 1;
  463. }
  464. /* Fill ip key */
  465. ((unsigned char *)in)[0] = 0xE0;
  466. in[1] = 0;
  467. for (i = 0; i < sizeof(ctx->__vmac_ctx.l3key)/8; i += 2) {
  468. do {
  469. crypto_cipher_encrypt_one(ctx->child,
  470. (unsigned char *)out, (unsigned char *)in);
  471. ctx->__vmac_ctx.l3key[i] = be64_to_cpup(out);
  472. ctx->__vmac_ctx.l3key[i+1] = be64_to_cpup(out+1);
  473. ((unsigned char *)in)[15] += 1;
  474. } while (ctx->__vmac_ctx.l3key[i] >= p64
  475. || ctx->__vmac_ctx.l3key[i+1] >= p64);
  476. }
  477. /* Invalidate nonce/aes cache and reset other elements */
  478. ctx->__vmac_ctx.cached_nonce[0] = (u64)-1; /* Ensure illegal nonce */
  479. ctx->__vmac_ctx.cached_nonce[1] = (u64)0; /* Ensure illegal nonce */
  480. ctx->__vmac_ctx.first_block_processed = 0;
  481. return err;
  482. }
  483. static int vmac_setkey(struct crypto_shash *parent,
  484. const u8 *key, unsigned int keylen)
  485. {
  486. struct vmac_ctx_t *ctx = crypto_shash_ctx(parent);
  487. if (keylen != VMAC_KEY_LEN) {
  488. crypto_shash_set_flags(parent, CRYPTO_TFM_RES_BAD_KEY_LEN);
  489. return -EINVAL;
  490. }
  491. return vmac_set_key((u8 *)key, ctx);
  492. }
  493. static int vmac_init(struct shash_desc *pdesc)
  494. {
  495. return 0;
  496. }
  497. static int vmac_update(struct shash_desc *pdesc, const u8 *p,
  498. unsigned int len)
  499. {
  500. struct crypto_shash *parent = pdesc->tfm;
  501. struct vmac_ctx_t *ctx = crypto_shash_ctx(parent);
  502. vhash_update(p, len, &ctx->__vmac_ctx);
  503. return 0;
  504. }
  505. static int vmac_final(struct shash_desc *pdesc, u8 *out)
  506. {
  507. struct crypto_shash *parent = pdesc->tfm;
  508. struct vmac_ctx_t *ctx = crypto_shash_ctx(parent);
  509. vmac_t mac;
  510. u8 nonce[16] = {};
  511. mac = vmac(NULL, 0, nonce, NULL, ctx);
  512. memcpy(out, &mac, sizeof(vmac_t));
  513. memset(&mac, 0, sizeof(vmac_t));
  514. memset(&ctx->__vmac_ctx, 0, sizeof(struct vmac_ctx));
  515. return 0;
  516. }
  517. static int vmac_init_tfm(struct crypto_tfm *tfm)
  518. {
  519. struct crypto_cipher *cipher;
  520. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  521. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  522. struct vmac_ctx_t *ctx = crypto_tfm_ctx(tfm);
  523. cipher = crypto_spawn_cipher(spawn);
  524. if (IS_ERR(cipher))
  525. return PTR_ERR(cipher);
  526. ctx->child = cipher;
  527. return 0;
  528. }
  529. static void vmac_exit_tfm(struct crypto_tfm *tfm)
  530. {
  531. struct vmac_ctx_t *ctx = crypto_tfm_ctx(tfm);
  532. crypto_free_cipher(ctx->child);
  533. }
  534. static int vmac_create(struct crypto_template *tmpl, struct rtattr **tb)
  535. {
  536. struct shash_instance *inst;
  537. struct crypto_alg *alg;
  538. int err;
  539. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
  540. if (err)
  541. return err;
  542. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
  543. CRYPTO_ALG_TYPE_MASK);
  544. if (IS_ERR(alg))
  545. return PTR_ERR(alg);
  546. inst = shash_alloc_instance("vmac", alg);
  547. err = PTR_ERR(inst);
  548. if (IS_ERR(inst))
  549. goto out_put_alg;
  550. err = crypto_init_spawn(shash_instance_ctx(inst), alg,
  551. shash_crypto_instance(inst),
  552. CRYPTO_ALG_TYPE_MASK);
  553. if (err)
  554. goto out_free_inst;
  555. inst->alg.base.cra_priority = alg->cra_priority;
  556. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  557. inst->alg.base.cra_alignmask = alg->cra_alignmask;
  558. inst->alg.digestsize = sizeof(vmac_t);
  559. inst->alg.base.cra_ctxsize = sizeof(struct vmac_ctx_t);
  560. inst->alg.base.cra_init = vmac_init_tfm;
  561. inst->alg.base.cra_exit = vmac_exit_tfm;
  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");