ctr128.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. * [email protected].
  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. /*
  60. * NOTE: the IV/counter CTR mode is big-endian. The code itself is
  61. * endian-neutral.
  62. */
  63. /* increment counter (128-bit int) by 1 */
  64. static void ctr128_inc(unsigned char *counter)
  65. {
  66. u32 n = 16;
  67. u8 c;
  68. do {
  69. --n;
  70. c = counter[n];
  71. ++c;
  72. counter[n] = c;
  73. if (c)
  74. return;
  75. } while (n);
  76. }
  77. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  78. static void ctr128_inc_aligned(unsigned char *counter)
  79. {
  80. size_t *data, c, n;
  81. const union {
  82. long one;
  83. char little;
  84. } is_endian = {
  85. 1
  86. };
  87. if (is_endian.little) {
  88. ctr128_inc(counter);
  89. return;
  90. }
  91. data = (size_t *)counter;
  92. n = 16 / sizeof(size_t);
  93. do {
  94. --n;
  95. c = data[n];
  96. ++c;
  97. data[n] = c;
  98. if (c)
  99. return;
  100. } while (n);
  101. }
  102. #endif
  103. /*
  104. * The input encrypted as though 128bit counter mode is being used. The
  105. * extra state information to record how much of the 128bit block we have
  106. * used is contained in *num, and the encrypted counter is kept in
  107. * ecount_buf. Both *num and ecount_buf must be initialised with zeros
  108. * before the first call to CRYPTO_ctr128_encrypt(). This algorithm assumes
  109. * that the counter is in the x lower bits of the IV (ivec), and that the
  110. * application has full control over overflow and the rest of the IV. This
  111. * implementation takes NO responsability for checking that the counter
  112. * doesn't overflow into the rest of the IV when incremented.
  113. */
  114. void CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out,
  115. size_t len, const void *key,
  116. unsigned char ivec[16],
  117. unsigned char ecount_buf[16], unsigned int *num,
  118. block128_f block)
  119. {
  120. unsigned int n;
  121. size_t l = 0;
  122. assert(in && out && key && ecount_buf && num);
  123. assert(*num < 16);
  124. n = *num;
  125. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  126. if (16 % sizeof(size_t) == 0) { /* always true actually */
  127. do {
  128. while (n && len) {
  129. *(out++) = *(in++) ^ ecount_buf[n];
  130. --len;
  131. n = (n + 1) % 16;
  132. }
  133. # if defined(STRICT_ALIGNMENT)
  134. if (((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) !=
  135. 0)
  136. break;
  137. # endif
  138. while (len >= 16) {
  139. (*block) (ivec, ecount_buf, key);
  140. ctr128_inc_aligned(ivec);
  141. for (; n < 16; n += sizeof(size_t))
  142. *(size_t *)(out + n) =
  143. *(size_t *)(in + n) ^ *(size_t *)(ecount_buf + n);
  144. len -= 16;
  145. out += 16;
  146. in += 16;
  147. n = 0;
  148. }
  149. if (len) {
  150. (*block) (ivec, ecount_buf, key);
  151. ctr128_inc_aligned(ivec);
  152. while (len--) {
  153. out[n] = in[n] ^ ecount_buf[n];
  154. ++n;
  155. }
  156. }
  157. *num = n;
  158. return;
  159. } while (0);
  160. }
  161. /* the rest would be commonly eliminated by x86* compiler */
  162. #endif
  163. while (l < len) {
  164. if (n == 0) {
  165. (*block) (ivec, ecount_buf, key);
  166. ctr128_inc(ivec);
  167. }
  168. out[l] = in[l] ^ ecount_buf[n];
  169. ++l;
  170. n = (n + 1) % 16;
  171. }
  172. *num = n;
  173. }
  174. /* increment upper 96 bits of 128-bit counter by 1 */
  175. static void ctr96_inc(unsigned char *counter)
  176. {
  177. u32 n = 12;
  178. u8 c;
  179. do {
  180. --n;
  181. c = counter[n];
  182. ++c;
  183. counter[n] = c;
  184. if (c)
  185. return;
  186. } while (n);
  187. }
  188. void CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out,
  189. size_t len, const void *key,
  190. unsigned char ivec[16],
  191. unsigned char ecount_buf[16],
  192. unsigned int *num, ctr128_f func)
  193. {
  194. unsigned int n, ctr32;
  195. assert(in && out && key && ecount_buf && num);
  196. assert(*num < 16);
  197. n = *num;
  198. while (n && len) {
  199. *(out++) = *(in++) ^ ecount_buf[n];
  200. --len;
  201. n = (n + 1) % 16;
  202. }
  203. ctr32 = GETU32(ivec + 12);
  204. while (len >= 16) {
  205. size_t blocks = len / 16;
  206. /*
  207. * 1<<28 is just a not-so-small yet not-so-large number...
  208. * Below condition is practically never met, but it has to
  209. * be checked for code correctness.
  210. */
  211. if (sizeof(size_t) > sizeof(unsigned int) && blocks > (1U << 28))
  212. blocks = (1U << 28);
  213. /*
  214. * As (*func) operates on 32-bit counter, caller
  215. * has to handle overflow. 'if' below detects the
  216. * overflow, which is then handled by limiting the
  217. * amount of blocks to the exact overflow point...
  218. */
  219. ctr32 += (u32)blocks;
  220. if (ctr32 < blocks) {
  221. blocks -= ctr32;
  222. ctr32 = 0;
  223. }
  224. (*func) (in, out, blocks, key, ivec);
  225. /* (*ctr) does not update ivec, caller does: */
  226. PUTU32(ivec + 12, ctr32);
  227. /* ... overflow was detected, propogate carry. */
  228. if (ctr32 == 0)
  229. ctr96_inc(ivec);
  230. blocks *= 16;
  231. len -= blocks;
  232. out += blocks;
  233. in += blocks;
  234. }
  235. if (len) {
  236. memset(ecount_buf, 0, 16);
  237. (*func) (ecount_buf, ecount_buf, 1, key, ivec);
  238. ++ctr32;
  239. PUTU32(ivec + 12, ctr32);
  240. if (ctr32 == 0)
  241. ctr96_inc(ivec);
  242. while (len--) {
  243. out[n] = in[n] ^ ecount_buf[n];
  244. ++n;
  245. }
  246. }
  247. *num = n;
  248. }