glue_helper.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/lrw.h>
  30. #include <crypto/xts.h>
  31. #include <asm/crypto/glue_helper.h>
  32. #include <crypto/scatterwalk.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. /* for implementations implementing faster XTS IV generator */
  283. int glue_xts_crypt_128bit(const struct common_glue_ctx *gctx,
  284. struct blkcipher_desc *desc, struct scatterlist *dst,
  285. struct scatterlist *src, unsigned int nbytes,
  286. void (*tweak_fn)(void *ctx, u8 *dst, const u8 *src),
  287. void *tweak_ctx, void *crypt_ctx)
  288. {
  289. const unsigned int bsize = 128 / 8;
  290. bool fpu_enabled = false;
  291. struct blkcipher_walk walk;
  292. int err;
  293. blkcipher_walk_init(&walk, dst, src, nbytes);
  294. err = blkcipher_walk_virt(desc, &walk);
  295. nbytes = walk.nbytes;
  296. if (!nbytes)
  297. return err;
  298. /* set minimum length to bsize, for tweak_fn */
  299. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  300. desc, fpu_enabled,
  301. nbytes < bsize ? bsize : nbytes);
  302. /* calculate first value of T */
  303. tweak_fn(tweak_ctx, walk.iv, walk.iv);
  304. while (nbytes) {
  305. nbytes = __glue_xts_crypt_128bit(gctx, crypt_ctx, desc, &walk);
  306. err = blkcipher_walk_done(desc, &walk, nbytes);
  307. nbytes = walk.nbytes;
  308. }
  309. glue_fpu_end(fpu_enabled);
  310. return err;
  311. }
  312. EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit);
  313. void glue_xts_crypt_128bit_one(void *ctx, u128 *dst, const u128 *src, le128 *iv,
  314. common_glue_func_t fn)
  315. {
  316. le128 ivblk = *iv;
  317. /* generate next IV */
  318. le128_gf128mul_x_ble(iv, &ivblk);
  319. /* CC <- T xor C */
  320. u128_xor(dst, src, (u128 *)&ivblk);
  321. /* PP <- D(Key2,CC) */
  322. fn(ctx, (u8 *)dst, (u8 *)dst);
  323. /* P <- T xor PP */
  324. u128_xor(dst, dst, (u128 *)&ivblk);
  325. }
  326. EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit_one);
  327. MODULE_LICENSE("GPL");