ctr128.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* ====================================================================
  2. * Copyright (c) 2008 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. */
  50. #include <openssl/crypto.h>
  51. #include "modes_lcl.h"
  52. #include <string.h>
  53. #ifndef MODES_DEBUG
  54. # ifndef NDEBUG
  55. # define NDEBUG
  56. # endif
  57. #endif
  58. #include <assert.h>
  59. /* NOTE: the IV/counter CTR mode is big-endian. The code itself
  60. * is endian-neutral. */
  61. /* increment counter (128-bit int) by 1 */
  62. static void ctr128_inc(unsigned char *counter) {
  63. u32 n=16;
  64. u8 c;
  65. do {
  66. --n;
  67. c = counter[n];
  68. ++c;
  69. counter[n] = c;
  70. if (c) return;
  71. } while (n);
  72. }
  73. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  74. static void ctr128_inc_aligned(unsigned char *counter) {
  75. size_t *data,c,n;
  76. const union { long one; char little; } is_endian = {1};
  77. if (is_endian.little) {
  78. ctr128_inc(counter);
  79. return;
  80. }
  81. data = (size_t *)counter;
  82. n = 16/sizeof(size_t);
  83. do {
  84. --n;
  85. c = data[n];
  86. ++c;
  87. data[n] = c;
  88. if (c) return;
  89. } while (n);
  90. }
  91. #endif
  92. /* The input encrypted as though 128bit counter mode is being
  93. * used. The extra state information to record how much of the
  94. * 128bit block we have used is contained in *num, and the
  95. * encrypted counter is kept in ecount_buf. Both *num and
  96. * ecount_buf must be initialised with zeros before the first
  97. * call to CRYPTO_ctr128_encrypt().
  98. *
  99. * This algorithm assumes that the counter is in the x lower bits
  100. * of the IV (ivec), and that the application has full control over
  101. * overflow and the rest of the IV. This implementation takes NO
  102. * responsability for checking that the counter doesn't overflow
  103. * into the rest of the IV when incremented.
  104. */
  105. void CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out,
  106. size_t len, const void *key,
  107. unsigned char ivec[16], unsigned char ecount_buf[16],
  108. unsigned int *num, block128_f block)
  109. {
  110. unsigned int n;
  111. size_t l=0;
  112. assert(in && out && key && ecount_buf && num);
  113. assert(*num < 16);
  114. n = *num;
  115. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  116. if (16%sizeof(size_t) == 0) do { /* always true actually */
  117. while (n && len) {
  118. *(out++) = *(in++) ^ ecount_buf[n];
  119. --len;
  120. n = (n+1) % 16;
  121. }
  122. #if defined(STRICT_ALIGNMENT)
  123. if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
  124. break;
  125. #endif
  126. while (len>=16) {
  127. (*block)(ivec, ecount_buf, key);
  128. ctr128_inc_aligned(ivec);
  129. for (; n<16; n+=sizeof(size_t))
  130. *(size_t *)(out+n) =
  131. *(size_t *)(in+n) ^ *(size_t *)(ecount_buf+n);
  132. len -= 16;
  133. out += 16;
  134. in += 16;
  135. n = 0;
  136. }
  137. if (len) {
  138. (*block)(ivec, ecount_buf, key);
  139. ctr128_inc_aligned(ivec);
  140. while (len--) {
  141. out[n] = in[n] ^ ecount_buf[n];
  142. ++n;
  143. }
  144. }
  145. *num = n;
  146. return;
  147. } while(0);
  148. /* the rest would be commonly eliminated by x86* compiler */
  149. #endif
  150. while (l<len) {
  151. if (n==0) {
  152. (*block)(ivec, ecount_buf, key);
  153. ctr128_inc(ivec);
  154. }
  155. out[l] = in[l] ^ ecount_buf[n];
  156. ++l;
  157. n = (n+1) % 16;
  158. }
  159. *num=n;
  160. }
  161. /* increment upper 96 bits of 128-bit counter by 1 */
  162. static void ctr96_inc(unsigned char *counter) {
  163. u32 n=12;
  164. u8 c;
  165. do {
  166. --n;
  167. c = counter[n];
  168. ++c;
  169. counter[n] = c;
  170. if (c) return;
  171. } while (n);
  172. }
  173. void CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out,
  174. size_t len, const void *key,
  175. unsigned char ivec[16], unsigned char ecount_buf[16],
  176. unsigned int *num, ctr128_f func)
  177. {
  178. unsigned int n,ctr32;
  179. assert(in && out && key && ecount_buf && num);
  180. assert(*num < 16);
  181. n = *num;
  182. while (n && len) {
  183. *(out++) = *(in++) ^ ecount_buf[n];
  184. --len;
  185. n = (n+1) % 16;
  186. }
  187. ctr32 = GETU32(ivec+12);
  188. while (len>=16) {
  189. size_t blocks = len/16;
  190. /*
  191. * 1<<28 is just a not-so-small yet not-so-large number...
  192. * Below condition is practically never met, but it has to
  193. * be checked for code correctness.
  194. */
  195. if (sizeof(size_t)>sizeof(unsigned int) && blocks>(1U<<28))
  196. blocks = (1U<<28);
  197. /*
  198. * As (*func) operates on 32-bit counter, caller
  199. * has to handle overflow. 'if' below detects the
  200. * overflow, which is then handled by limiting the
  201. * amount of blocks to the exact overflow point...
  202. */
  203. ctr32 += (u32)blocks;
  204. if (ctr32 < blocks) {
  205. blocks -= ctr32;
  206. ctr32 = 0;
  207. }
  208. (*func)(in,out,blocks,key,ivec);
  209. /* (*ctr) does not update ivec, caller does: */
  210. PUTU32(ivec+12,ctr32);
  211. /* ... overflow was detected, propogate carry. */
  212. if (ctr32 == 0) ctr96_inc(ivec);
  213. blocks *= 16;
  214. len -= blocks;
  215. out += blocks;
  216. in += blocks;
  217. }
  218. if (len) {
  219. memset(ecount_buf,0,16);
  220. (*func)(ecount_buf,ecount_buf,1,key,ivec);
  221. ++ctr32;
  222. PUTU32(ivec+12,ctr32);
  223. if (ctr32 == 0) ctr96_inc(ivec);
  224. while (len--) {
  225. out[n] = in[n] ^ ecount_buf[n];
  226. ++n;
  227. }
  228. }
  229. *num=n;
  230. }