glue_helper.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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/gf128mul.h>
  30. #include <crypto/internal/skcipher.h>
  31. #include <crypto/xts.h>
  32. #include <asm/crypto/glue_helper.h>
  33. int glue_ecb_req_128bit(const struct common_glue_ctx *gctx,
  34. struct skcipher_request *req)
  35. {
  36. void *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
  37. const unsigned int bsize = 128 / 8;
  38. struct skcipher_walk walk;
  39. bool fpu_enabled = false;
  40. unsigned int nbytes;
  41. int err;
  42. err = skcipher_walk_virt(&walk, req, false);
  43. while ((nbytes = walk.nbytes)) {
  44. const u8 *src = walk.src.virt.addr;
  45. u8 *dst = walk.dst.virt.addr;
  46. unsigned int func_bytes;
  47. unsigned int i;
  48. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  49. &walk, fpu_enabled, nbytes);
  50. for (i = 0; i < gctx->num_funcs; i++) {
  51. func_bytes = bsize * gctx->funcs[i].num_blocks;
  52. if (nbytes < func_bytes)
  53. continue;
  54. /* Process multi-block batch */
  55. do {
  56. gctx->funcs[i].fn_u.ecb(ctx, dst, src);
  57. src += func_bytes;
  58. dst += func_bytes;
  59. nbytes -= func_bytes;
  60. } while (nbytes >= func_bytes);
  61. if (nbytes < bsize)
  62. break;
  63. }
  64. err = skcipher_walk_done(&walk, nbytes);
  65. }
  66. glue_fpu_end(fpu_enabled);
  67. return err;
  68. }
  69. EXPORT_SYMBOL_GPL(glue_ecb_req_128bit);
  70. int glue_cbc_encrypt_req_128bit(const common_glue_func_t fn,
  71. struct skcipher_request *req)
  72. {
  73. void *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
  74. const unsigned int bsize = 128 / 8;
  75. struct skcipher_walk walk;
  76. unsigned int nbytes;
  77. int err;
  78. err = skcipher_walk_virt(&walk, req, false);
  79. while ((nbytes = walk.nbytes)) {
  80. const u128 *src = (u128 *)walk.src.virt.addr;
  81. u128 *dst = (u128 *)walk.dst.virt.addr;
  82. u128 *iv = (u128 *)walk.iv;
  83. do {
  84. u128_xor(dst, src, iv);
  85. fn(ctx, (u8 *)dst, (u8 *)dst);
  86. iv = dst;
  87. src++;
  88. dst++;
  89. nbytes -= bsize;
  90. } while (nbytes >= bsize);
  91. *(u128 *)walk.iv = *iv;
  92. err = skcipher_walk_done(&walk, nbytes);
  93. }
  94. return err;
  95. }
  96. EXPORT_SYMBOL_GPL(glue_cbc_encrypt_req_128bit);
  97. int glue_cbc_decrypt_req_128bit(const struct common_glue_ctx *gctx,
  98. struct skcipher_request *req)
  99. {
  100. void *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
  101. const unsigned int bsize = 128 / 8;
  102. struct skcipher_walk walk;
  103. bool fpu_enabled = false;
  104. unsigned int nbytes;
  105. int err;
  106. err = skcipher_walk_virt(&walk, req, false);
  107. while ((nbytes = walk.nbytes)) {
  108. const u128 *src = walk.src.virt.addr;
  109. u128 *dst = walk.dst.virt.addr;
  110. unsigned int func_bytes, num_blocks;
  111. unsigned int i;
  112. u128 last_iv;
  113. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  114. &walk, fpu_enabled, nbytes);
  115. /* Start of the last block. */
  116. src += nbytes / bsize - 1;
  117. dst += nbytes / bsize - 1;
  118. last_iv = *src;
  119. for (i = 0; i < gctx->num_funcs; i++) {
  120. num_blocks = gctx->funcs[i].num_blocks;
  121. func_bytes = bsize * num_blocks;
  122. if (nbytes < func_bytes)
  123. continue;
  124. /* Process multi-block batch */
  125. do {
  126. src -= num_blocks - 1;
  127. dst -= num_blocks - 1;
  128. gctx->funcs[i].fn_u.cbc(ctx, dst, src);
  129. nbytes -= func_bytes;
  130. if (nbytes < bsize)
  131. goto done;
  132. u128_xor(dst, dst, --src);
  133. dst--;
  134. } while (nbytes >= func_bytes);
  135. }
  136. done:
  137. u128_xor(dst, dst, (u128 *)walk.iv);
  138. *(u128 *)walk.iv = last_iv;
  139. err = skcipher_walk_done(&walk, nbytes);
  140. }
  141. glue_fpu_end(fpu_enabled);
  142. return err;
  143. }
  144. EXPORT_SYMBOL_GPL(glue_cbc_decrypt_req_128bit);
  145. int glue_ctr_req_128bit(const struct common_glue_ctx *gctx,
  146. struct skcipher_request *req)
  147. {
  148. void *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
  149. const unsigned int bsize = 128 / 8;
  150. struct skcipher_walk walk;
  151. bool fpu_enabled = false;
  152. unsigned int nbytes;
  153. int err;
  154. err = skcipher_walk_virt(&walk, req, false);
  155. while ((nbytes = walk.nbytes) >= bsize) {
  156. const u128 *src = walk.src.virt.addr;
  157. u128 *dst = walk.dst.virt.addr;
  158. unsigned int func_bytes, num_blocks;
  159. unsigned int i;
  160. le128 ctrblk;
  161. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  162. &walk, fpu_enabled, nbytes);
  163. be128_to_le128(&ctrblk, (be128 *)walk.iv);
  164. for (i = 0; i < gctx->num_funcs; i++) {
  165. num_blocks = gctx->funcs[i].num_blocks;
  166. func_bytes = bsize * num_blocks;
  167. if (nbytes < func_bytes)
  168. continue;
  169. /* Process multi-block batch */
  170. do {
  171. gctx->funcs[i].fn_u.ctr(ctx, dst, src, &ctrblk);
  172. src += num_blocks;
  173. dst += num_blocks;
  174. nbytes -= func_bytes;
  175. } while (nbytes >= func_bytes);
  176. if (nbytes < bsize)
  177. break;
  178. }
  179. le128_to_be128((be128 *)walk.iv, &ctrblk);
  180. err = skcipher_walk_done(&walk, nbytes);
  181. }
  182. glue_fpu_end(fpu_enabled);
  183. if (nbytes) {
  184. le128 ctrblk;
  185. u128 tmp;
  186. be128_to_le128(&ctrblk, (be128 *)walk.iv);
  187. memcpy(&tmp, walk.src.virt.addr, nbytes);
  188. gctx->funcs[gctx->num_funcs - 1].fn_u.ctr(ctx, &tmp, &tmp,
  189. &ctrblk);
  190. memcpy(walk.dst.virt.addr, &tmp, nbytes);
  191. le128_to_be128((be128 *)walk.iv, &ctrblk);
  192. err = skcipher_walk_done(&walk, 0);
  193. }
  194. return err;
  195. }
  196. EXPORT_SYMBOL_GPL(glue_ctr_req_128bit);
  197. static unsigned int __glue_xts_req_128bit(const struct common_glue_ctx *gctx,
  198. void *ctx,
  199. struct skcipher_walk *walk)
  200. {
  201. const unsigned int bsize = 128 / 8;
  202. unsigned int nbytes = walk->nbytes;
  203. u128 *src = walk->src.virt.addr;
  204. u128 *dst = walk->dst.virt.addr;
  205. unsigned int num_blocks, func_bytes;
  206. unsigned int i;
  207. /* Process multi-block batch */
  208. for (i = 0; i < gctx->num_funcs; i++) {
  209. num_blocks = gctx->funcs[i].num_blocks;
  210. func_bytes = bsize * num_blocks;
  211. if (nbytes >= func_bytes) {
  212. do {
  213. gctx->funcs[i].fn_u.xts(ctx, dst, src,
  214. walk->iv);
  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. return nbytes;
  225. }
  226. int glue_xts_req_128bit(const struct common_glue_ctx *gctx,
  227. struct skcipher_request *req,
  228. common_glue_func_t tweak_fn, void *tweak_ctx,
  229. void *crypt_ctx)
  230. {
  231. const unsigned int bsize = 128 / 8;
  232. struct skcipher_walk walk;
  233. bool fpu_enabled = false;
  234. unsigned int nbytes;
  235. int err;
  236. err = skcipher_walk_virt(&walk, req, false);
  237. nbytes = walk.nbytes;
  238. if (!nbytes)
  239. return err;
  240. /* set minimum length to bsize, for tweak_fn */
  241. fpu_enabled = glue_fpu_begin(bsize, gctx->fpu_blocks_limit,
  242. &walk, fpu_enabled,
  243. nbytes < bsize ? bsize : nbytes);
  244. /* calculate first value of T */
  245. tweak_fn(tweak_ctx, walk.iv, walk.iv);
  246. while (nbytes) {
  247. nbytes = __glue_xts_req_128bit(gctx, crypt_ctx, &walk);
  248. err = skcipher_walk_done(&walk, nbytes);
  249. nbytes = walk.nbytes;
  250. }
  251. glue_fpu_end(fpu_enabled);
  252. return err;
  253. }
  254. EXPORT_SYMBOL_GPL(glue_xts_req_128bit);
  255. void glue_xts_crypt_128bit_one(void *ctx, u128 *dst, const u128 *src, le128 *iv,
  256. common_glue_func_t fn)
  257. {
  258. le128 ivblk = *iv;
  259. /* generate next IV */
  260. gf128mul_x_ble(iv, &ivblk);
  261. /* CC <- T xor C */
  262. u128_xor(dst, src, (u128 *)&ivblk);
  263. /* PP <- D(Key2,CC) */
  264. fn(ctx, (u8 *)dst, (u8 *)dst);
  265. /* P <- T xor PP */
  266. u128_xor(dst, dst, (u128 *)&ivblk);
  267. }
  268. EXPORT_SYMBOL_GPL(glue_xts_crypt_128bit_one);
  269. MODULE_LICENSE("GPL");