glue_helper.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Shared glue code for 128bit block ciphers
  3. *
  4. * Copyright © 2012-2013 Jussi Kivilinna <jussi.kivilinna@iki.fi>
  5. *
  6. * CBC & ECB parts based on code (crypto/cbc.c,ecb.c) by:
  7. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  8. * CTR part based on code (crypto/ctr.c) by:
  9. * (C) Copyright IBM Corp. 2007 - Joy Latten <latten@us.ibm.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  24. * USA
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <crypto/b128ops.h>
  29. #include <crypto/internal/skcipher.h>
  30. #include <crypto/lrw.h>
  31. #include <crypto/xts.h>
  32. #include <asm/crypto/glue_helper.h>
  33. static int __glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  34. struct blkcipher_desc *desc,
  35. struct blkcipher_walk *walk)
  36. {
  37. void *ctx = crypto_blkcipher_ctx(desc->tfm);
  38. const unsigned int bsize = 128 / 8;
  39. unsigned int nbytes, i, func_bytes;
  40. bool fpu_enabled = false;
  41. int err;
  42. err = blkcipher_walk_virt(desc, walk);
  43. while ((nbytes = walk->nbytes)) {
  44. u8 *wsrc = walk->src.virt.addr;
  45. u8 *wdst = walk->dst.virt.addr;
  46. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  47. desc, fpu_enabled, nbytes);
  48. for (i = 0; i < gctx->num_funcs; i++) {
  49. func_bytes = bsize * gctx->funcs[i].num_blocks;
  50. /* Process multi-block batch */
  51. if (nbytes >= func_bytes) {
  52. do {
  53. gctx->funcs[i].fn_u.ecb(ctx, wdst,
  54. wsrc);
  55. wsrc += func_bytes;
  56. wdst += func_bytes;
  57. nbytes -= func_bytes;
  58. } while (nbytes >= func_bytes);
  59. if (nbytes < bsize)
  60. goto done;
  61. }
  62. }
  63. done:
  64. err = blkcipher_walk_done(desc, walk, nbytes);
  65. }
  66. glue_fpu_end(fpu_enabled);
  67. return err;
  68. }
  69. int glue_ecb_crypt_128bit(const struct common_glue_ctx *gctx,
  70. struct blkcipher_desc *desc, struct scatterlist *dst,
  71. struct scatterlist *src, unsigned int nbytes)
  72. {
  73. struct blkcipher_walk walk;
  74. blkcipher_walk_init(&walk, dst, src, nbytes);
  75. return __glue_ecb_crypt_128bit(gctx, desc, &walk);
  76. }
  77. EXPORT_SYMBOL_GPL(glue_ecb_crypt_128bit);
  78. static unsigned int __glue_cbc_encrypt_128bit(const common_glue_func_t fn,
  79. struct blkcipher_desc *desc,
  80. struct blkcipher_walk *walk)
  81. {
  82. void *ctx = crypto_blkcipher_ctx(desc->tfm);
  83. const unsigned int bsize = 128 / 8;
  84. unsigned int nbytes = walk->nbytes;
  85. u128 *src = (u128 *)walk->src.virt.addr;
  86. u128 *dst = (u128 *)walk->dst.virt.addr;
  87. u128 *iv = (u128 *)walk->iv;
  88. do {
  89. u128_xor(dst, src, iv);
  90. fn(ctx, (u8 *)dst, (u8 *)dst);
  91. iv = dst;
  92. src += 1;
  93. dst += 1;
  94. nbytes -= bsize;
  95. } while (nbytes >= bsize);
  96. *(u128 *)walk->iv = *iv;
  97. return nbytes;
  98. }
  99. int glue_cbc_encrypt_128bit(const common_glue_func_t fn,
  100. struct blkcipher_desc *desc,
  101. struct scatterlist *dst,
  102. struct scatterlist *src, unsigned int nbytes)
  103. {
  104. struct blkcipher_walk walk;
  105. int err;
  106. blkcipher_walk_init(&walk, dst, src, nbytes);
  107. err = blkcipher_walk_virt(desc, &walk);
  108. while ((nbytes = walk.nbytes)) {
  109. nbytes = __glue_cbc_encrypt_128bit(fn, desc, &walk);
  110. err = blkcipher_walk_done(desc, &walk, nbytes);
  111. }
  112. return err;
  113. }
  114. EXPORT_SYMBOL_GPL(glue_cbc_encrypt_128bit);
  115. static unsigned int
  116. __glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
  117. struct blkcipher_desc *desc,
  118. struct blkcipher_walk *walk)
  119. {
  120. void *ctx = crypto_blkcipher_ctx(desc->tfm);
  121. const unsigned int bsize = 128 / 8;
  122. unsigned int nbytes = walk->nbytes;
  123. u128 *src = (u128 *)walk->src.virt.addr;
  124. u128 *dst = (u128 *)walk->dst.virt.addr;
  125. u128 last_iv;
  126. unsigned int num_blocks, func_bytes;
  127. unsigned int i;
  128. /* Start of the last block. */
  129. src += nbytes / bsize - 1;
  130. dst += nbytes / bsize - 1;
  131. last_iv = *src;
  132. for (i = 0; i < gctx->num_funcs; i++) {
  133. num_blocks = gctx->funcs[i].num_blocks;
  134. func_bytes = bsize * num_blocks;
  135. /* Process multi-block batch */
  136. if (nbytes >= func_bytes) {
  137. do {
  138. nbytes -= func_bytes - bsize;
  139. src -= num_blocks - 1;
  140. dst -= num_blocks - 1;
  141. gctx->funcs[i].fn_u.cbc(ctx, dst, src);
  142. nbytes -= bsize;
  143. if (nbytes < bsize)
  144. goto done;
  145. u128_xor(dst, dst, src - 1);
  146. src -= 1;
  147. dst -= 1;
  148. } while (nbytes >= func_bytes);
  149. if (nbytes < bsize)
  150. goto done;
  151. }
  152. }
  153. done:
  154. u128_xor(dst, dst, (u128 *)walk->iv);
  155. *(u128 *)walk->iv = last_iv;
  156. return nbytes;
  157. }
  158. int glue_cbc_decrypt_128bit(const struct common_glue_ctx *gctx,
  159. struct blkcipher_desc *desc,
  160. struct scatterlist *dst,
  161. struct scatterlist *src, unsigned int nbytes)
  162. {
  163. const unsigned int bsize = 128 / 8;
  164. bool fpu_enabled = false;
  165. struct blkcipher_walk walk;
  166. int err;
  167. blkcipher_walk_init(&walk, dst, src, nbytes);
  168. err = blkcipher_walk_virt(desc, &walk);
  169. while ((nbytes = walk.nbytes)) {
  170. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  171. desc, fpu_enabled, nbytes);
  172. nbytes = __glue_cbc_decrypt_128bit(gctx, desc, &walk);
  173. err = blkcipher_walk_done(desc, &walk, nbytes);
  174. }
  175. glue_fpu_end(fpu_enabled);
  176. return err;
  177. }
  178. EXPORT_SYMBOL_GPL(glue_cbc_decrypt_128bit);
  179. static void glue_ctr_crypt_final_128bit(const common_glue_ctr_func_t fn_ctr,
  180. struct blkcipher_desc *desc,
  181. struct blkcipher_walk *walk)
  182. {
  183. void *ctx = crypto_blkcipher_ctx(desc->tfm);
  184. u8 *src = (u8 *)walk->src.virt.addr;
  185. u8 *dst = (u8 *)walk->dst.virt.addr;
  186. unsigned int nbytes = walk->nbytes;
  187. le128 ctrblk;
  188. u128 tmp;
  189. be128_to_le128(&ctrblk, (be128 *)walk->iv);
  190. memcpy(&tmp, src, nbytes);
  191. fn_ctr(ctx, &tmp, &tmp, &ctrblk);
  192. memcpy(dst, &tmp, nbytes);
  193. le128_to_be128((be128 *)walk->iv, &ctrblk);
  194. }
  195. static unsigned int __glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
  196. struct blkcipher_desc *desc,
  197. struct blkcipher_walk *walk)
  198. {
  199. const unsigned int bsize = 128 / 8;
  200. void *ctx = crypto_blkcipher_ctx(desc->tfm);
  201. unsigned int nbytes = walk->nbytes;
  202. u128 *src = (u128 *)walk->src.virt.addr;
  203. u128 *dst = (u128 *)walk->dst.virt.addr;
  204. le128 ctrblk;
  205. unsigned int num_blocks, func_bytes;
  206. unsigned int i;
  207. be128_to_le128(&ctrblk, (be128 *)walk->iv);
  208. /* Process multi-block batch */
  209. for (i = 0; i < gctx->num_funcs; i++) {
  210. num_blocks = gctx->funcs[i].num_blocks;
  211. func_bytes = bsize * num_blocks;
  212. if (nbytes >= func_bytes) {
  213. do {
  214. gctx->funcs[i].fn_u.ctr(ctx, dst, src, &ctrblk);
  215. src += num_blocks;
  216. dst += num_blocks;
  217. nbytes -= func_bytes;
  218. } while (nbytes >= func_bytes);
  219. if (nbytes < bsize)
  220. goto done;
  221. }
  222. }
  223. done:
  224. le128_to_be128((be128 *)walk->iv, &ctrblk);
  225. return nbytes;
  226. }
  227. int glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
  228. struct blkcipher_desc *desc, struct scatterlist *dst,
  229. struct scatterlist *src, unsigned int nbytes)
  230. {
  231. const unsigned int bsize = 128 / 8;
  232. bool fpu_enabled = false;
  233. struct blkcipher_walk walk;
  234. int err;
  235. blkcipher_walk_init(&walk, dst, src, nbytes);
  236. err = blkcipher_walk_virt_block(desc, &walk, bsize);
  237. while ((nbytes = walk.nbytes) >= bsize) {
  238. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  239. desc, fpu_enabled, nbytes);
  240. nbytes = __glue_ctr_crypt_128bit(gctx, desc, &walk);
  241. err = blkcipher_walk_done(desc, &walk, nbytes);
  242. }
  243. glue_fpu_end(fpu_enabled);
  244. if (walk.nbytes) {
  245. glue_ctr_crypt_final_128bit(
  246. gctx->funcs[gctx->num_funcs - 1].fn_u.ctr, desc, &walk);
  247. err = blkcipher_walk_done(desc, &walk, 0);
  248. }
  249. return err;
  250. }
  251. EXPORT_SYMBOL_GPL(glue_ctr_crypt_128bit);
  252. static unsigned int __glue_xts_crypt_128bit(const struct common_glue_ctx *gctx,
  253. void *ctx,
  254. struct blkcipher_desc *desc,
  255. struct blkcipher_walk *walk)
  256. {
  257. const unsigned int bsize = 128 / 8;
  258. unsigned int nbytes = walk->nbytes;
  259. u128 *src = (u128 *)walk->src.virt.addr;
  260. u128 *dst = (u128 *)walk->dst.virt.addr;
  261. unsigned int num_blocks, func_bytes;
  262. unsigned int i;
  263. /* Process multi-block batch */
  264. for (i = 0; i < gctx->num_funcs; i++) {
  265. num_blocks = gctx->funcs[i].num_blocks;
  266. func_bytes = bsize * num_blocks;
  267. if (nbytes >= func_bytes) {
  268. do {
  269. gctx->funcs[i].fn_u.xts(ctx, dst, src,
  270. (le128 *)walk->iv);
  271. src += num_blocks;
  272. dst += num_blocks;
  273. nbytes -= func_bytes;
  274. } while (nbytes >= func_bytes);
  275. if (nbytes < bsize)
  276. goto done;
  277. }
  278. }
  279. done:
  280. return nbytes;
  281. }
  282. static unsigned int __glue_xts_req_128bit(const struct common_glue_ctx *gctx,
  283. void *ctx,
  284. struct skcipher_walk *walk)
  285. {
  286. const unsigned int bsize = 128 / 8;
  287. unsigned int nbytes = walk->nbytes;
  288. u128 *src = walk->src.virt.addr;
  289. u128 *dst = walk->dst.virt.addr;
  290. unsigned int num_blocks, func_bytes;
  291. unsigned int i;
  292. /* Process multi-block batch */
  293. for (i = 0; i < gctx->num_funcs; i++) {
  294. num_blocks = gctx->funcs[i].num_blocks;
  295. func_bytes = bsize * num_blocks;
  296. if (nbytes >= func_bytes) {
  297. do {
  298. gctx->funcs[i].fn_u.xts(ctx, dst, src,
  299. walk->iv);
  300. src += num_blocks;
  301. dst += num_blocks;
  302. nbytes -= func_bytes;
  303. } while (nbytes >= func_bytes);
  304. if (nbytes < bsize)
  305. goto done;
  306. }
  307. }
  308. done:
  309. return nbytes;
  310. }
  311. /* for implementations implementing faster XTS IV generator */
  312. int glue_xts_crypt_128bit(const struct common_glue_ctx *gctx,
  313. struct blkcipher_desc *desc, struct scatterlist *dst,
  314. struct scatterlist *src, unsigned int nbytes,
  315. void (*tweak_fn)(void *ctx, u8 *dst, const u8 *src),
  316. void *tweak_ctx, void *crypt_ctx)
  317. {
  318. const unsigned int bsize = 128 / 8;
  319. bool fpu_enabled = false;
  320. struct blkcipher_walk walk;
  321. int err;
  322. blkcipher_walk_init(&walk, dst, src, nbytes);
  323. err = blkcipher_walk_virt(desc, &walk);
  324. nbytes = walk.nbytes;
  325. if (!nbytes)
  326. return err;
  327. /* set minimum length to bsize, for tweak_fn */
  328. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  329. desc, fpu_enabled,
  330. nbytes < bsize ? bsize : nbytes);
  331. /* calculate first value of T */
  332. tweak_fn(tweak_ctx, walk.iv, walk.iv);
  333. while (nbytes) {
  334. nbytes = __glue_xts_crypt_128bit(gctx, crypt_ctx, desc, &walk);
  335. err = blkcipher_walk_done(desc, &walk, nbytes);
  336. nbytes = walk.nbytes;
  337. }
  338. glue_fpu_end(fpu_enabled);
  339. return err;
  340. }
  341. EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit);
  342. int glue_xts_req_128bit(const struct common_glue_ctx *gctx,
  343. struct skcipher_request *req,
  344. common_glue_func_t tweak_fn, void *tweak_ctx,
  345. void *crypt_ctx)
  346. {
  347. const unsigned int bsize = 128 / 8;
  348. struct skcipher_walk walk;
  349. bool fpu_enabled = false;
  350. unsigned int nbytes;
  351. int err;
  352. err = skcipher_walk_virt(&walk, req, false);
  353. nbytes = walk.nbytes;
  354. if (!nbytes)
  355. return err;
  356. /* set minimum length to bsize, for tweak_fn */
  357. fpu_enabled = glue_skwalk_fpu_begin(bsize, gctx->fpu_blocks_limit,
  358. &walk, fpu_enabled,
  359. nbytes < bsize ? bsize : nbytes);
  360. /* calculate first value of T */
  361. tweak_fn(tweak_ctx, walk.iv, walk.iv);
  362. while (nbytes) {
  363. nbytes = __glue_xts_req_128bit(gctx, crypt_ctx, &walk);
  364. err = skcipher_walk_done(&walk, nbytes);
  365. nbytes = walk.nbytes;
  366. }
  367. glue_fpu_end(fpu_enabled);
  368. return err;
  369. }
  370. EXPORT_SYMBOL_GPL(glue_xts_req_128bit);
  371. void glue_xts_crypt_128bit_one(void *ctx, u128 *dst, const u128 *src, le128 *iv,
  372. common_glue_func_t fn)
  373. {
  374. le128 ivblk = *iv;
  375. /* generate next IV */
  376. le128_gf128mul_x_ble(iv, &ivblk);
  377. /* CC <- T xor C */
  378. u128_xor(dst, src, (u128 *)&ivblk);
  379. /* PP <- D(Key2,CC) */
  380. fn(ctx, (u8 *)dst, (u8 *)dst);
  381. /* P <- T xor PP */
  382. u128_xor(dst, dst, (u128 *)&ivblk);
  383. }
  384. EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit_one);
  385. MODULE_LICENSE("GPL");