ctr128.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "modes.h"
  51. #include <string.h>
  52. #ifndef MODES_DEBUG
  53. # ifndef NDEBUG
  54. # define NDEBUG
  55. # endif
  56. #endif
  57. #include <assert.h>
  58. typedef unsigned int u32;
  59. typedef unsigned char u8;
  60. #define STRICT_ALIGNMENT
  61. #if defined(__i386) || defined(__i386__) || \
  62. defined(__x86_64) || defined(__x86_64__) || \
  63. defined(_M_IX86) || defined(_M_AMD64) || defined(_M_X64) || \
  64. defined(__s390__) || defined(__s390x__)
  65. # undef STRICT_ALIGNMENT
  66. #endif
  67. /*
  68. * NOTE: the IV/counter CTR mode is big-endian. The code itself is
  69. * endian-neutral.
  70. */
  71. /* increment counter (128-bit int) by 1 */
  72. static void ctr128_inc(unsigned char *counter)
  73. {
  74. u32 n = 16;
  75. u8 c;
  76. do {
  77. --n;
  78. c = counter[n];
  79. ++c;
  80. counter[n] = c;
  81. if (c)
  82. return;
  83. } while (n);
  84. }
  85. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  86. static void ctr128_inc_aligned(unsigned char *counter)
  87. {
  88. size_t *data, c, n;
  89. const union {
  90. long one;
  91. char little;
  92. } is_endian = {
  93. 1
  94. };
  95. if (is_endian.little) {
  96. ctr128_inc(counter);
  97. return;
  98. }
  99. data = (size_t *)counter;
  100. n = 16 / sizeof(size_t);
  101. do {
  102. --n;
  103. c = data[n];
  104. ++c;
  105. data[n] = c;
  106. if (c)
  107. return;
  108. } while (n);
  109. }
  110. #endif
  111. /*
  112. * The input encrypted as though 128bit counter mode is being used. The
  113. * extra state information to record how much of the 128bit block we have
  114. * used is contained in *num, and the encrypted counter is kept in
  115. * ecount_buf. Both *num and ecount_buf must be initialised with zeros
  116. * before the first call to CRYPTO_ctr128_encrypt(). This algorithm assumes
  117. * that the counter is in the x lower bits of the IV (ivec), and that the
  118. * application has full control over overflow and the rest of the IV. This
  119. * implementation takes NO responsability for checking that the counter
  120. * doesn't overflow into the rest of the IV when incremented.
  121. */
  122. void CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out,
  123. size_t len, const void *key,
  124. unsigned char ivec[16],
  125. unsigned char ecount_buf[16], unsigned int *num,
  126. block128_f block)
  127. {
  128. unsigned int n;
  129. size_t l = 0;
  130. assert(in && out && key && ecount_buf && num);
  131. assert(*num < 16);
  132. n = *num;
  133. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  134. if (16 % sizeof(size_t) == 0) { /* always true actually */
  135. do {
  136. while (n && len) {
  137. *(out++) = *(in++) ^ ecount_buf[n];
  138. --len;
  139. n = (n + 1) % 16;
  140. }
  141. # if defined(STRICT_ALIGNMENT)
  142. if (((size_t)in | (size_t)out | (size_t)ivec) % sizeof(size_t) !=
  143. 0)
  144. break;
  145. # endif
  146. while (len >= 16) {
  147. (*block) (ivec, ecount_buf, key);
  148. ctr128_inc_aligned(ivec);
  149. for (; n < 16; n += sizeof(size_t))
  150. *(size_t *)(out + n) =
  151. *(size_t *)(in + n) ^ *(size_t *)(ecount_buf + n);
  152. len -= 16;
  153. out += 16;
  154. in += 16;
  155. n = 0;
  156. }
  157. if (len) {
  158. (*block) (ivec, ecount_buf, key);
  159. ctr128_inc_aligned(ivec);
  160. while (len--) {
  161. out[n] = in[n] ^ ecount_buf[n];
  162. ++n;
  163. }
  164. }
  165. *num = n;
  166. return;
  167. } while (0);
  168. }
  169. /* the rest would be commonly eliminated by x86* compiler */
  170. #endif
  171. while (l < len) {
  172. if (n == 0) {
  173. (*block) (ivec, ecount_buf, key);
  174. ctr128_inc(ivec);
  175. }
  176. out[l] = in[l] ^ ecount_buf[n];
  177. ++l;
  178. n = (n + 1) % 16;
  179. }
  180. *num = n;
  181. }