glue_helper.c 8.0 KB

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