glue_helper.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Shared glue code for 128bit block ciphers
  3. *
  4. * Copyright (c) 2012 Jussi Kivilinna <jussi.kivilinna@mbnet.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_xor((u128 *)walk->iv, (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. u128 ctrblk;
  188. u128 tmp;
  189. be128_to_u128(&ctrblk, (be128 *)walk->iv);
  190. memcpy(&tmp, src, nbytes);
  191. fn_ctr(ctx, &tmp, &tmp, &ctrblk);
  192. memcpy(dst, &tmp, nbytes);
  193. u128_to_be128((be128 *)walk->iv, &ctrblk);
  194. }
  195. EXPORT_SYMBOL_GPL(glue_ctr_crypt_final_128bit);
  196. static unsigned int __glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
  197. struct blkcipher_desc *desc,
  198. struct blkcipher_walk *walk)
  199. {
  200. const unsigned int bsize = 128 / 8;
  201. void *ctx = crypto_blkcipher_ctx(desc->tfm);
  202. unsigned int nbytes = walk->nbytes;
  203. u128 *src = (u128 *)walk->src.virt.addr;
  204. u128 *dst = (u128 *)walk->dst.virt.addr;
  205. u128 ctrblk;
  206. unsigned int num_blocks, func_bytes;
  207. unsigned int i;
  208. be128_to_u128(&ctrblk, (be128 *)walk->iv);
  209. /* Process multi-block batch */
  210. for (i = 0; i < gctx->num_funcs; i++) {
  211. num_blocks = gctx->funcs[i].num_blocks;
  212. func_bytes = bsize * num_blocks;
  213. if (nbytes >= func_bytes) {
  214. do {
  215. gctx->funcs[i].fn_u.ctr(ctx, dst, src, &ctrblk);
  216. src += num_blocks;
  217. dst += num_blocks;
  218. nbytes -= func_bytes;
  219. } while (nbytes >= func_bytes);
  220. if (nbytes < bsize)
  221. goto done;
  222. }
  223. }
  224. done:
  225. u128_to_be128((be128 *)walk->iv, &ctrblk);
  226. return nbytes;
  227. }
  228. int glue_ctr_crypt_128bit(const struct common_glue_ctx *gctx,
  229. struct blkcipher_desc *desc, struct scatterlist *dst,
  230. struct scatterlist *src, unsigned int nbytes)
  231. {
  232. const unsigned int bsize = 128 / 8;
  233. bool fpu_enabled = false;
  234. struct blkcipher_walk walk;
  235. int err;
  236. blkcipher_walk_init(&walk, dst, src, nbytes);
  237. err = blkcipher_walk_virt_block(desc, &walk, bsize);
  238. while ((nbytes = walk.nbytes) >= bsize) {
  239. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  240. desc, fpu_enabled, nbytes);
  241. nbytes = __glue_ctr_crypt_128bit(gctx, desc, &walk);
  242. err = blkcipher_walk_done(desc, &walk, nbytes);
  243. }
  244. glue_fpu_end(fpu_enabled);
  245. if (walk.nbytes) {
  246. glue_ctr_crypt_final_128bit(
  247. gctx->funcs[gctx->num_funcs - 1].fn_u.ctr, desc, &walk);
  248. err = blkcipher_walk_done(desc, &walk, 0);
  249. }
  250. return err;
  251. }
  252. EXPORT_SYMBOL_GPL(glue_ctr_crypt_128bit);
  253. MODULE_LICENSE("GPL");