cipher-ctr.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* cipher-ctr.c - Generic CTR 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_ctr_encrypt_ctx (gcry_cipher_hd_t c,
  31. unsigned char *outbuf, size_t outbuflen,
  32. const unsigned char *inbuf, size_t inbuflen,
  33. void *algo_context)
  34. {
  35. size_t n;
  36. int i;
  37. gcry_cipher_encrypt_t enc_fn = c->spec->encrypt;
  38. size_t blocksize_shift = _gcry_blocksize_shift(c);
  39. size_t blocksize = 1 << blocksize_shift;
  40. size_t nblocks;
  41. unsigned int burn, nburn;
  42. if (outbuflen < inbuflen)
  43. return GPG_ERR_BUFFER_TOO_SHORT;
  44. burn = 0;
  45. /* First process a left over encrypted counter. */
  46. if (c->unused)
  47. {
  48. gcry_assert (c->unused < blocksize);
  49. i = blocksize - c->unused;
  50. n = c->unused > inbuflen ? inbuflen : c->unused;
  51. buf_xor(outbuf, inbuf, &c->lastiv[i], n);
  52. c->unused -= n;
  53. inbuf += n;
  54. outbuf += n;
  55. inbuflen -= n;
  56. }
  57. /* Use a bulk method if available. */
  58. nblocks = inbuflen >> blocksize_shift;
  59. if (nblocks && c->bulk.ctr_enc)
  60. {
  61. c->bulk.ctr_enc (algo_context, c->u_ctr.ctr, outbuf, inbuf, nblocks);
  62. inbuf += nblocks << blocksize_shift;
  63. outbuf += nblocks << blocksize_shift;
  64. inbuflen -= nblocks << blocksize_shift;
  65. }
  66. /* If we don't have a bulk method use the standard method. We also
  67. use this method for the a remaining partial block. */
  68. if (inbuflen)
  69. {
  70. unsigned char tmp[MAX_BLOCKSIZE];
  71. n = blocksize;
  72. do
  73. {
  74. nburn = enc_fn (algo_context, tmp, c->u_ctr.ctr);
  75. burn = nburn > burn ? nburn : burn;
  76. cipher_block_add(c->u_ctr.ctr, 1, blocksize);
  77. if (inbuflen < blocksize)
  78. break;
  79. cipher_block_xor(outbuf, inbuf, tmp, blocksize);
  80. inbuflen -= n;
  81. outbuf += n;
  82. inbuf += n;
  83. }
  84. while (inbuflen);
  85. if (inbuflen)
  86. {
  87. n = inbuflen;
  88. buf_xor(outbuf, inbuf, tmp, inbuflen);
  89. inbuflen -= n;
  90. outbuf += n;
  91. inbuf += n;
  92. }
  93. /* Save the unused bytes of the counter. */
  94. c->unused = blocksize - n;
  95. if (c->unused)
  96. buf_cpy (c->lastiv+n, tmp+n, c->unused);
  97. wipememory (tmp, sizeof tmp);
  98. }
  99. if (burn > 0)
  100. _gcry_burn_stack (burn + 4 * sizeof(void *));
  101. return 0;
  102. }
  103. gcry_err_code_t
  104. _gcry_cipher_ctr_encrypt (gcry_cipher_hd_t c,
  105. unsigned char *outbuf, size_t outbuflen,
  106. const unsigned char *inbuf, size_t inbuflen)
  107. {
  108. return _gcry_cipher_ctr_encrypt_ctx (c, outbuf, outbuflen, inbuf, inbuflen,
  109. &c->context.c);
  110. }