ctr128.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright 2008-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/crypto.h>
  11. #include "crypto/modes.h"
  12. /*
  13. * NOTE: the IV/counter CTR mode is big-endian. The code itself is
  14. * endian-neutral.
  15. */
  16. /* increment counter (128-bit int) by 1 */
  17. static void ctr128_inc(unsigned char *counter)
  18. {
  19. u32 n = 16, c = 1;
  20. do {
  21. --n;
  22. c += counter[n];
  23. counter[n] = (u8)c;
  24. c >>= 8;
  25. } while (n);
  26. }
  27. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  28. static void ctr128_inc_aligned(unsigned char *counter)
  29. {
  30. size_t *data, c, d, n;
  31. const union {
  32. long one;
  33. char little;
  34. } is_endian = {
  35. 1
  36. };
  37. if (is_endian.little || ((size_t)counter % sizeof(size_t)) != 0) {
  38. ctr128_inc(counter);
  39. return;
  40. }
  41. data = (size_t *)counter;
  42. c = 1;
  43. n = 16 / sizeof(size_t);
  44. do {
  45. --n;
  46. d = data[n] += c;
  47. /* did addition carry? */
  48. c = ((d - c) & ~d) >> (sizeof(size_t) * 8 - 1);
  49. } while (n);
  50. }
  51. #endif
  52. /*
  53. * The input encrypted as though 128bit counter mode is being used. The
  54. * extra state information to record how much of the 128bit block we have
  55. * used is contained in *num, and the encrypted counter is kept in
  56. * ecount_buf. Both *num and ecount_buf must be initialised with zeros
  57. * before the first call to CRYPTO_ctr128_encrypt(). This algorithm assumes
  58. * that the counter is in the x lower bits of the IV (ivec), and that the
  59. * application has full control over overflow and the rest of the IV. This
  60. * implementation takes NO responsibility for checking that the counter
  61. * doesn't overflow into the rest of the IV when incremented.
  62. */
  63. void CRYPTO_ctr128_encrypt(const unsigned char *in, unsigned char *out,
  64. size_t len, const void *key,
  65. unsigned char ivec[16],
  66. unsigned char ecount_buf[16], unsigned int *num,
  67. block128_f block)
  68. {
  69. unsigned int n;
  70. size_t l = 0;
  71. n = *num;
  72. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  73. if (16 % sizeof(size_t) == 0) { /* always true actually */
  74. do {
  75. while (n && len) {
  76. *(out++) = *(in++) ^ ecount_buf[n];
  77. --len;
  78. n = (n + 1) % 16;
  79. }
  80. # if defined(STRICT_ALIGNMENT)
  81. if (((size_t)in | (size_t)out | (size_t)ecount_buf)
  82. % sizeof(size_t) != 0)
  83. break;
  84. # endif
  85. while (len >= 16) {
  86. (*block) (ivec, ecount_buf, key);
  87. ctr128_inc_aligned(ivec);
  88. for (n = 0; n < 16; n += sizeof(size_t))
  89. *(size_t *)(out + n) =
  90. *(size_t *)(in + n) ^ *(size_t *)(ecount_buf + n);
  91. len -= 16;
  92. out += 16;
  93. in += 16;
  94. n = 0;
  95. }
  96. if (len) {
  97. (*block) (ivec, ecount_buf, key);
  98. ctr128_inc_aligned(ivec);
  99. while (len--) {
  100. out[n] = in[n] ^ ecount_buf[n];
  101. ++n;
  102. }
  103. }
  104. *num = n;
  105. return;
  106. } while (0);
  107. }
  108. /* the rest would be commonly eliminated by x86* compiler */
  109. #endif
  110. while (l < len) {
  111. if (n == 0) {
  112. (*block) (ivec, ecount_buf, key);
  113. ctr128_inc(ivec);
  114. }
  115. out[l] = in[l] ^ ecount_buf[n];
  116. ++l;
  117. n = (n + 1) % 16;
  118. }
  119. *num = n;
  120. }
  121. /* increment upper 96 bits of 128-bit counter by 1 */
  122. static void ctr96_inc(unsigned char *counter)
  123. {
  124. u32 n = 12, c = 1;
  125. do {
  126. --n;
  127. c += counter[n];
  128. counter[n] = (u8)c;
  129. c >>= 8;
  130. } while (n);
  131. }
  132. void CRYPTO_ctr128_encrypt_ctr32(const unsigned char *in, unsigned char *out,
  133. size_t len, const void *key,
  134. unsigned char ivec[16],
  135. unsigned char ecount_buf[16],
  136. unsigned int *num, ctr128_f func)
  137. {
  138. unsigned int n, ctr32;
  139. n = *num;
  140. while (n && len) {
  141. *(out++) = *(in++) ^ ecount_buf[n];
  142. --len;
  143. n = (n + 1) % 16;
  144. }
  145. ctr32 = GETU32(ivec + 12);
  146. while (len >= 16) {
  147. size_t blocks = len / 16;
  148. /*
  149. * 1<<28 is just a not-so-small yet not-so-large number...
  150. * Below condition is practically never met, but it has to
  151. * be checked for code correctness.
  152. */
  153. if (sizeof(size_t) > sizeof(unsigned int) && blocks > (1U << 28))
  154. blocks = (1U << 28);
  155. /*
  156. * As (*func) operates on 32-bit counter, caller
  157. * has to handle overflow. 'if' below detects the
  158. * overflow, which is then handled by limiting the
  159. * amount of blocks to the exact overflow point...
  160. */
  161. ctr32 += (u32)blocks;
  162. if (ctr32 < blocks) {
  163. blocks -= ctr32;
  164. ctr32 = 0;
  165. }
  166. (*func) (in, out, blocks, key, ivec);
  167. /* (*ctr) does not update ivec, caller does: */
  168. PUTU32(ivec + 12, ctr32);
  169. /* ... overflow was detected, propagate carry. */
  170. if (ctr32 == 0)
  171. ctr96_inc(ivec);
  172. blocks *= 16;
  173. len -= blocks;
  174. out += blocks;
  175. in += blocks;
  176. }
  177. if (len) {
  178. memset(ecount_buf, 0, 16);
  179. (*func) (ecount_buf, ecount_buf, 1, key, ivec);
  180. ++ctr32;
  181. PUTU32(ivec + 12, ctr32);
  182. if (ctr32 == 0)
  183. ctr96_inc(ivec);
  184. while (len--) {
  185. out[n] = in[n] ^ ecount_buf[n];
  186. ++n;
  187. }
  188. }
  189. *num = n;
  190. }