cipher-cfb.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* cipher-cfb.c - Generic CFB mode implementation
  2. * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
  3. * 2005, 2007, 2008, 2009, 2011 Free Software Foundation, Inc.
  4. *
  5. * This file is part of Libgcrypt.
  6. *
  7. * Libgcrypt is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * Libgcrypt is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include "g10lib.h"
  26. #include "cipher.h"
  27. #include "bufhelp.h"
  28. #include "./cipher-internal.h"
  29. gcry_err_code_t
  30. _gcry_cipher_cfb_encrypt (gcry_cipher_hd_t c,
  31. unsigned char *outbuf, size_t outbuflen,
  32. const unsigned char *inbuf, size_t inbuflen)
  33. {
  34. unsigned char *ivp;
  35. gcry_cipher_encrypt_t enc_fn = c->spec->encrypt;
  36. size_t blocksize_shift = _gcry_blocksize_shift(c);
  37. size_t blocksize = 1 << blocksize_shift;
  38. size_t blocksize_x_2 = blocksize + blocksize;
  39. unsigned int burn, nburn;
  40. if (outbuflen < inbuflen)
  41. return GPG_ERR_BUFFER_TOO_SHORT;
  42. if ( inbuflen <= c->unused )
  43. {
  44. /* Short enough to be encoded by the remaining XOR mask. */
  45. /* XOR the input with the IV and store input into IV. */
  46. ivp = c->u_iv.iv + blocksize - c->unused;
  47. buf_xor_2dst(outbuf, ivp, inbuf, inbuflen);
  48. c->unused -= inbuflen;
  49. return 0;
  50. }
  51. burn = 0;
  52. if ( c->unused )
  53. {
  54. /* XOR the input with the IV and store input into IV */
  55. inbuflen -= c->unused;
  56. ivp = c->u_iv.iv + blocksize - c->unused;
  57. buf_xor_2dst(outbuf, ivp, inbuf, c->unused);
  58. outbuf += c->unused;
  59. inbuf += c->unused;
  60. c->unused = 0;
  61. }
  62. /* Now we can process complete blocks. We use a loop as long as we
  63. have at least 2 blocks and use conditions for the rest. This
  64. also allows to use a bulk encryption function if available. */
  65. if (inbuflen >= blocksize_x_2 && c->bulk.cfb_enc)
  66. {
  67. size_t nblocks = inbuflen >> blocksize_shift;
  68. c->bulk.cfb_enc (&c->context.c, c->u_iv.iv, outbuf, inbuf, nblocks);
  69. outbuf += nblocks << blocksize_shift;
  70. inbuf += nblocks << blocksize_shift;
  71. inbuflen -= nblocks << blocksize_shift;
  72. }
  73. else
  74. {
  75. while ( inbuflen >= blocksize_x_2 )
  76. {
  77. /* Encrypt the IV. */
  78. nburn = enc_fn ( &c->context.c, c->u_iv.iv, c->u_iv.iv );
  79. burn = nburn > burn ? nburn : burn;
  80. /* XOR the input with the IV and store input into IV. */
  81. cipher_block_xor_2dst(outbuf, c->u_iv.iv, inbuf, blocksize);
  82. outbuf += blocksize;
  83. inbuf += blocksize;
  84. inbuflen -= blocksize;
  85. }
  86. }
  87. if ( inbuflen >= blocksize )
  88. {
  89. /* Save the current IV and then encrypt the IV. */
  90. cipher_block_cpy( c->lastiv, c->u_iv.iv, blocksize );
  91. nburn = enc_fn ( &c->context.c, c->u_iv.iv, c->u_iv.iv );
  92. burn = nburn > burn ? nburn : burn;
  93. /* XOR the input with the IV and store input into IV */
  94. cipher_block_xor_2dst(outbuf, c->u_iv.iv, inbuf, blocksize);
  95. outbuf += blocksize;
  96. inbuf += blocksize;
  97. inbuflen -= blocksize;
  98. }
  99. if ( inbuflen )
  100. {
  101. /* Save the current IV and then encrypt the IV. */
  102. cipher_block_cpy( c->lastiv, c->u_iv.iv, blocksize );
  103. nburn = enc_fn ( &c->context.c, c->u_iv.iv, c->u_iv.iv );
  104. burn = nburn > burn ? nburn : burn;
  105. c->unused = blocksize;
  106. /* Apply the XOR. */
  107. c->unused -= inbuflen;
  108. buf_xor_2dst(outbuf, c->u_iv.iv, inbuf, inbuflen);
  109. outbuf += inbuflen;
  110. inbuf += inbuflen;
  111. inbuflen = 0;
  112. }
  113. if (burn > 0)
  114. _gcry_burn_stack (burn + 4 * sizeof(void *));
  115. return 0;
  116. }
  117. gcry_err_code_t
  118. _gcry_cipher_cfb_decrypt (gcry_cipher_hd_t c,
  119. unsigned char *outbuf, size_t outbuflen,
  120. const unsigned char *inbuf, size_t inbuflen)
  121. {
  122. unsigned char *ivp;
  123. gcry_cipher_encrypt_t enc_fn = c->spec->encrypt;
  124. size_t blocksize_shift = _gcry_blocksize_shift(c);
  125. size_t blocksize = 1 << blocksize_shift;
  126. size_t blocksize_x_2 = blocksize + blocksize;
  127. unsigned int burn, nburn;
  128. if (outbuflen < inbuflen)
  129. return GPG_ERR_BUFFER_TOO_SHORT;
  130. if (inbuflen <= c->unused)
  131. {
  132. /* Short enough to be encoded by the remaining XOR mask. */
  133. /* XOR the input with the IV and store input into IV. */
  134. ivp = c->u_iv.iv + blocksize - c->unused;
  135. buf_xor_n_copy(outbuf, ivp, inbuf, inbuflen);
  136. c->unused -= inbuflen;
  137. return 0;
  138. }
  139. burn = 0;
  140. if (c->unused)
  141. {
  142. /* XOR the input with the IV and store input into IV. */
  143. inbuflen -= c->unused;
  144. ivp = c->u_iv.iv + blocksize - c->unused;
  145. buf_xor_n_copy(outbuf, ivp, inbuf, c->unused);
  146. outbuf += c->unused;
  147. inbuf += c->unused;
  148. c->unused = 0;
  149. }
  150. /* Now we can process complete blocks. We use a loop as long as we
  151. have at least 2 blocks and use conditions for the rest. This
  152. also allows to use a bulk encryption function if available. */
  153. if (inbuflen >= blocksize_x_2 && c->bulk.cfb_dec)
  154. {
  155. size_t nblocks = inbuflen >> blocksize_shift;
  156. c->bulk.cfb_dec (&c->context.c, c->u_iv.iv, outbuf, inbuf, nblocks);
  157. outbuf += nblocks << blocksize_shift;
  158. inbuf += nblocks << blocksize_shift;
  159. inbuflen -= nblocks << blocksize_shift;
  160. }
  161. else
  162. {
  163. while (inbuflen >= blocksize_x_2 )
  164. {
  165. /* Encrypt the IV. */
  166. nburn = enc_fn ( &c->context.c, c->u_iv.iv, c->u_iv.iv );
  167. burn = nburn > burn ? nburn : burn;
  168. /* XOR the input with the IV and store input into IV. */
  169. cipher_block_xor_n_copy(outbuf, c->u_iv.iv, inbuf, blocksize);
  170. outbuf += blocksize;
  171. inbuf += blocksize;
  172. inbuflen -= blocksize;
  173. }
  174. }
  175. if (inbuflen >= blocksize )
  176. {
  177. /* Save the current IV and then encrypt the IV. */
  178. cipher_block_cpy ( c->lastiv, c->u_iv.iv, blocksize);
  179. nburn = enc_fn ( &c->context.c, c->u_iv.iv, c->u_iv.iv );
  180. burn = nburn > burn ? nburn : burn;
  181. /* XOR the input with the IV and store input into IV */
  182. cipher_block_xor_n_copy(outbuf, c->u_iv.iv, inbuf, blocksize);
  183. outbuf += blocksize;
  184. inbuf += blocksize;
  185. inbuflen -= blocksize;
  186. }
  187. if (inbuflen)
  188. {
  189. /* Save the current IV and then encrypt the IV. */
  190. cipher_block_cpy ( c->lastiv, c->u_iv.iv, blocksize );
  191. nburn = enc_fn ( &c->context.c, c->u_iv.iv, c->u_iv.iv );
  192. burn = nburn > burn ? nburn : burn;
  193. c->unused = blocksize;
  194. /* Apply the XOR. */
  195. c->unused -= inbuflen;
  196. buf_xor_n_copy(outbuf, c->u_iv.iv, inbuf, inbuflen);
  197. outbuf += inbuflen;
  198. inbuf += inbuflen;
  199. inbuflen = 0;
  200. }
  201. if (burn > 0)
  202. _gcry_burn_stack (burn + 4 * sizeof(void *));
  203. return 0;
  204. }
  205. gcry_err_code_t
  206. _gcry_cipher_cfb8_encrypt (gcry_cipher_hd_t c,
  207. unsigned char *outbuf, size_t outbuflen,
  208. const unsigned char *inbuf, size_t inbuflen)
  209. {
  210. gcry_cipher_encrypt_t enc_fn = c->spec->encrypt;
  211. size_t blocksize = c->spec->blocksize;
  212. unsigned int burn, nburn;
  213. if (outbuflen < inbuflen)
  214. return GPG_ERR_BUFFER_TOO_SHORT;
  215. burn = 0;
  216. while ( inbuflen > 0)
  217. {
  218. int i;
  219. /* Encrypt the IV. */
  220. nburn = enc_fn ( &c->context.c, c->lastiv, c->u_iv.iv );
  221. burn = nburn > burn ? nburn : burn;
  222. outbuf[0] = c->lastiv[0] ^ inbuf[0];
  223. /* Bitshift iv by 8 bit to the left */
  224. for (i = 0; i < blocksize-1; i++)
  225. c->u_iv.iv[i] = c->u_iv.iv[i+1];
  226. /* append cipher text to iv */
  227. c->u_iv.iv[blocksize-1] = outbuf[0];
  228. outbuf += 1;
  229. inbuf += 1;
  230. inbuflen -= 1;
  231. }
  232. if (burn > 0)
  233. _gcry_burn_stack (burn + 4 * sizeof(void *));
  234. return 0;
  235. }
  236. gcry_err_code_t
  237. _gcry_cipher_cfb8_decrypt (gcry_cipher_hd_t c,
  238. unsigned char *outbuf, size_t outbuflen,
  239. const unsigned char *inbuf, size_t inbuflen)
  240. {
  241. gcry_cipher_encrypt_t enc_fn = c->spec->encrypt;
  242. size_t blocksize = c->spec->blocksize;
  243. unsigned int burn, nburn;
  244. unsigned char appendee;
  245. if (outbuflen < inbuflen)
  246. return GPG_ERR_BUFFER_TOO_SHORT;
  247. burn = 0;
  248. while (inbuflen > 0)
  249. {
  250. int i;
  251. /* Encrypt the IV. */
  252. nburn = enc_fn ( &c->context.c, c->lastiv, c->u_iv.iv );
  253. burn = nburn > burn ? nburn : burn;
  254. /* inbuf might == outbuf, make sure we keep the value
  255. so we can append it later */
  256. appendee = inbuf[0];
  257. outbuf[0] = inbuf[0] ^ c->lastiv[0];
  258. /* Bitshift iv by 8 bit to the left */
  259. for (i = 0; i < blocksize-1; i++)
  260. c->u_iv.iv[i] = c->u_iv.iv[i+1];
  261. c->u_iv.iv[blocksize-1] = appendee;
  262. outbuf += 1;
  263. inbuf += 1;
  264. inbuflen -= 1;
  265. }
  266. if (burn > 0)
  267. _gcry_burn_stack (burn + 4 * sizeof(void *));
  268. return 0;
  269. }