vmac.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. const u64 p64 = UINT64_C(0xfffffffffffffeff); /* 2^64 - 257 prime */
  39. const u64 m62 = UINT64_C(0x3fffffffffffffff); /* 62-bit mask */
  40. const u64 m63 = UINT64_C(0x7fffffffffffffff); /* 63-bit mask */
  41. const u64 m64 = UINT64_C(0xffffffffffffffff); /* 64-bit mask */
  42. 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. mptr = (u64 *)m;
  345. i = mbytes / VMAC_NHBYTES; /* Must be non-zero */
  346. ch = ctx->polytmp[0];
  347. cl = ctx->polytmp[1];
  348. if (!ctx->first_block_processed) {
  349. ctx->first_block_processed = 1;
  350. nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, rh, rl);
  351. rh &= m62;
  352. ADD128(ch, cl, rh, rl);
  353. mptr += (VMAC_NHBYTES/sizeof(u64));
  354. i--;
  355. }
  356. while (i--) {
  357. nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, rh, rl);
  358. rh &= m62;
  359. poly_step(ch, cl, pkh, pkl, rh, rl);
  360. mptr += (VMAC_NHBYTES/sizeof(u64));
  361. }
  362. ctx->polytmp[0] = ch;
  363. ctx->polytmp[1] = cl;
  364. }
  365. static u64 vhash(unsigned char m[], unsigned int mbytes,
  366. u64 *tagl, struct vmac_ctx *ctx)
  367. {
  368. u64 rh, rl, *mptr;
  369. const u64 *kptr = (u64 *)ctx->nhkey;
  370. int i, remaining;
  371. u64 ch, cl;
  372. u64 pkh = ctx->polykey[0];
  373. u64 pkl = ctx->polykey[1];
  374. mptr = (u64 *)m;
  375. i = mbytes / VMAC_NHBYTES;
  376. remaining = mbytes % VMAC_NHBYTES;
  377. if (ctx->first_block_processed) {
  378. ch = ctx->polytmp[0];
  379. cl = ctx->polytmp[1];
  380. } else if (i) {
  381. nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, ch, cl);
  382. ch &= m62;
  383. ADD128(ch, cl, pkh, pkl);
  384. mptr += (VMAC_NHBYTES/sizeof(u64));
  385. i--;
  386. } else if (remaining) {
  387. nh_16(mptr, kptr, 2*((remaining+15)/16), ch, cl);
  388. ch &= m62;
  389. ADD128(ch, cl, pkh, pkl);
  390. mptr += (VMAC_NHBYTES/sizeof(u64));
  391. goto do_l3;
  392. } else {/* Empty String */
  393. ch = pkh; cl = pkl;
  394. goto do_l3;
  395. }
  396. while (i--) {
  397. nh_vmac_nhbytes(mptr, kptr, VMAC_NHBYTES/8, rh, rl);
  398. rh &= m62;
  399. poly_step(ch, cl, pkh, pkl, rh, rl);
  400. mptr += (VMAC_NHBYTES/sizeof(u64));
  401. }
  402. if (remaining) {
  403. nh_16(mptr, kptr, 2*((remaining+15)/16), rh, rl);
  404. rh &= m62;
  405. poly_step(ch, cl, pkh, pkl, rh, rl);
  406. }
  407. do_l3:
  408. vhash_abort(ctx);
  409. remaining *= 8;
  410. return l3hash(ch, cl, ctx->l3key[0], ctx->l3key[1], remaining);
  411. }
  412. static u64 vmac(unsigned char m[], unsigned int mbytes,
  413. unsigned char n[16], u64 *tagl,
  414. struct vmac_ctx_t *ctx)
  415. {
  416. u64 *in_n, *out_p;
  417. u64 p, h;
  418. int i;
  419. in_n = ctx->__vmac_ctx.cached_nonce;
  420. out_p = ctx->__vmac_ctx.cached_aes;
  421. i = n[15] & 1;
  422. if ((*(u64 *)(n+8) != in_n[1]) || (*(u64 *)(n) != in_n[0])) {
  423. in_n[0] = *(u64 *)(n);
  424. in_n[1] = *(u64 *)(n+8);
  425. ((unsigned char *)in_n)[15] &= 0xFE;
  426. crypto_cipher_encrypt_one(ctx->child,
  427. (unsigned char *)out_p, (unsigned char *)in_n);
  428. ((unsigned char *)in_n)[15] |= (unsigned char)(1-i);
  429. }
  430. p = be64_to_cpup(out_p + i);
  431. h = vhash(m, mbytes, (u64 *)0, &ctx->__vmac_ctx);
  432. return le64_to_cpu(p + h);
  433. }
  434. static int vmac_set_key(unsigned char user_key[], struct vmac_ctx_t *ctx)
  435. {
  436. u64 in[2] = {0}, out[2];
  437. unsigned i;
  438. int err = 0;
  439. err = crypto_cipher_setkey(ctx->child, user_key, VMAC_KEY_LEN);
  440. if (err)
  441. return err;
  442. /* Fill nh key */
  443. ((unsigned char *)in)[0] = 0x80;
  444. for (i = 0; i < sizeof(ctx->__vmac_ctx.nhkey)/8; i += 2) {
  445. crypto_cipher_encrypt_one(ctx->child,
  446. (unsigned char *)out, (unsigned char *)in);
  447. ctx->__vmac_ctx.nhkey[i] = be64_to_cpup(out);
  448. ctx->__vmac_ctx.nhkey[i+1] = be64_to_cpup(out+1);
  449. ((unsigned char *)in)[15] += 1;
  450. }
  451. /* Fill poly key */
  452. ((unsigned char *)in)[0] = 0xC0;
  453. in[1] = 0;
  454. for (i = 0; i < sizeof(ctx->__vmac_ctx.polykey)/8; i += 2) {
  455. crypto_cipher_encrypt_one(ctx->child,
  456. (unsigned char *)out, (unsigned char *)in);
  457. ctx->__vmac_ctx.polytmp[i] =
  458. ctx->__vmac_ctx.polykey[i] =
  459. be64_to_cpup(out) & mpoly;
  460. ctx->__vmac_ctx.polytmp[i+1] =
  461. ctx->__vmac_ctx.polykey[i+1] =
  462. be64_to_cpup(out+1) & mpoly;
  463. ((unsigned char *)in)[15] += 1;
  464. }
  465. /* Fill ip key */
  466. ((unsigned char *)in)[0] = 0xE0;
  467. in[1] = 0;
  468. for (i = 0; i < sizeof(ctx->__vmac_ctx.l3key)/8; i += 2) {
  469. do {
  470. crypto_cipher_encrypt_one(ctx->child,
  471. (unsigned char *)out, (unsigned char *)in);
  472. ctx->__vmac_ctx.l3key[i] = be64_to_cpup(out);
  473. ctx->__vmac_ctx.l3key[i+1] = be64_to_cpup(out+1);
  474. ((unsigned char *)in)[15] += 1;
  475. } while (ctx->__vmac_ctx.l3key[i] >= p64
  476. || ctx->__vmac_ctx.l3key[i+1] >= p64);
  477. }
  478. /* Invalidate nonce/aes cache and reset other elements */
  479. ctx->__vmac_ctx.cached_nonce[0] = (u64)-1; /* Ensure illegal nonce */
  480. ctx->__vmac_ctx.cached_nonce[1] = (u64)0; /* Ensure illegal nonce */
  481. ctx->__vmac_ctx.first_block_processed = 0;
  482. return err;
  483. }
  484. static int vmac_setkey(struct crypto_shash *parent,
  485. const u8 *key, unsigned int keylen)
  486. {
  487. struct vmac_ctx_t *ctx = crypto_shash_ctx(parent);
  488. if (keylen != VMAC_KEY_LEN) {
  489. crypto_shash_set_flags(parent, CRYPTO_TFM_RES_BAD_KEY_LEN);
  490. return -EINVAL;
  491. }
  492. return vmac_set_key((u8 *)key, ctx);
  493. }
  494. static int vmac_init(struct shash_desc *pdesc)
  495. {
  496. return 0;
  497. }
  498. static int vmac_update(struct shash_desc *pdesc, const u8 *p,
  499. unsigned int len)
  500. {
  501. struct crypto_shash *parent = pdesc->tfm;
  502. struct vmac_ctx_t *ctx = crypto_shash_ctx(parent);
  503. vhash_update(p, len, &ctx->__vmac_ctx);
  504. return 0;
  505. }
  506. static int vmac_final(struct shash_desc *pdesc, u8 *out)
  507. {
  508. struct crypto_shash *parent = pdesc->tfm;
  509. struct vmac_ctx_t *ctx = crypto_shash_ctx(parent);
  510. vmac_t mac;
  511. u8 nonce[16] = {};
  512. mac = vmac(NULL, 0, nonce, NULL, ctx);
  513. memcpy(out, &mac, sizeof(vmac_t));
  514. memset(&mac, 0, sizeof(vmac_t));
  515. memset(&ctx->__vmac_ctx, 0, sizeof(struct vmac_ctx));
  516. return 0;
  517. }
  518. static int vmac_init_tfm(struct crypto_tfm *tfm)
  519. {
  520. struct crypto_cipher *cipher;
  521. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  522. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  523. struct vmac_ctx_t *ctx = crypto_tfm_ctx(tfm);
  524. cipher = crypto_spawn_cipher(spawn);
  525. if (IS_ERR(cipher))
  526. return PTR_ERR(cipher);
  527. ctx->child = cipher;
  528. return 0;
  529. }
  530. static void vmac_exit_tfm(struct crypto_tfm *tfm)
  531. {
  532. struct vmac_ctx_t *ctx = crypto_tfm_ctx(tfm);
  533. crypto_free_cipher(ctx->child);
  534. }
  535. static int vmac_create(struct crypto_template *tmpl, struct rtattr **tb)
  536. {
  537. struct shash_instance *inst;
  538. struct crypto_alg *alg;
  539. int err;
  540. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SHASH);
  541. if (err)
  542. return err;
  543. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
  544. CRYPTO_ALG_TYPE_MASK);
  545. if (IS_ERR(alg))
  546. return PTR_ERR(alg);
  547. inst = shash_alloc_instance("vmac", alg);
  548. err = PTR_ERR(inst);
  549. if (IS_ERR(inst))
  550. goto out_put_alg;
  551. err = crypto_init_spawn(shash_instance_ctx(inst), alg,
  552. shash_crypto_instance(inst),
  553. CRYPTO_ALG_TYPE_MASK);
  554. if (err)
  555. goto out_free_inst;
  556. inst->alg.base.cra_priority = alg->cra_priority;
  557. inst->alg.base.cra_blocksize = alg->cra_blocksize;
  558. inst->alg.base.cra_alignmask = alg->cra_alignmask;
  559. inst->alg.digestsize = sizeof(vmac_t);
  560. inst->alg.base.cra_ctxsize = sizeof(struct vmac_ctx_t);
  561. inst->alg.base.cra_init = vmac_init_tfm;
  562. inst->alg.base.cra_exit = vmac_exit_tfm;
  563. inst->alg.init = vmac_init;
  564. inst->alg.update = vmac_update;
  565. inst->alg.final = vmac_final;
  566. inst->alg.setkey = vmac_setkey;
  567. err = shash_register_instance(tmpl, inst);
  568. if (err) {
  569. out_free_inst:
  570. shash_free_instance(shash_crypto_instance(inst));
  571. }
  572. out_put_alg:
  573. crypto_mod_put(alg);
  574. return err;
  575. }
  576. static struct crypto_template vmac_tmpl = {
  577. .name = "vmac",
  578. .create = vmac_create,
  579. .free = shash_free_instance,
  580. .module = THIS_MODULE,
  581. };
  582. static int __init vmac_module_init(void)
  583. {
  584. return crypto_register_template(&vmac_tmpl);
  585. }
  586. static void __exit vmac_module_exit(void)
  587. {
  588. crypto_unregister_template(&vmac_tmpl);
  589. }
  590. module_init(vmac_module_init);
  591. module_exit(vmac_module_exit);
  592. MODULE_LICENSE("GPL");
  593. MODULE_DESCRIPTION("VMAC hash algorithm");