cfb128.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2008-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <openssl/crypto.h>
  10. #include "modes_local.h"
  11. #include <string.h>
  12. #if defined(__GNUC__) && !defined(STRICT_ALIGNMENT)
  13. typedef size_t size_t_aX __attribute((__aligned__(1)));
  14. #else
  15. typedef size_t size_t_aX;
  16. #endif
  17. /*
  18. * The input and output encrypted as though 128bit cfb mode is being used.
  19. * The extra state information to record how much of the 128bit block we have
  20. * used is contained in *num;
  21. */
  22. void CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out,
  23. size_t len, const void *key,
  24. unsigned char ivec[16], int *num,
  25. int enc, block128_f block)
  26. {
  27. unsigned int n;
  28. size_t l = 0;
  29. n = *num;
  30. if (enc) {
  31. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  32. if (16 % sizeof(size_t) == 0) { /* always true actually */
  33. do {
  34. while (n && len) {
  35. *(out++) = ivec[n] ^= *(in++);
  36. --len;
  37. n = (n + 1) % 16;
  38. }
  39. # if defined(STRICT_ALIGNMENT)
  40. if (((size_t)in | (size_t)out | (size_t)ivec) %
  41. sizeof(size_t) != 0)
  42. break;
  43. # endif
  44. while (len >= 16) {
  45. (*block) (ivec, ivec, key);
  46. for (; n < 16; n += sizeof(size_t)) {
  47. *(size_t_aX *)(out + n) =
  48. *(size_t_aX *)(ivec + n)
  49. ^= *(size_t_aX *)(in + n);
  50. }
  51. len -= 16;
  52. out += 16;
  53. in += 16;
  54. n = 0;
  55. }
  56. if (len) {
  57. (*block) (ivec, ivec, key);
  58. while (len--) {
  59. out[n] = ivec[n] ^= in[n];
  60. ++n;
  61. }
  62. }
  63. *num = n;
  64. return;
  65. } while (0);
  66. }
  67. /* the rest would be commonly eliminated by x86* compiler */
  68. #endif
  69. while (l < len) {
  70. if (n == 0) {
  71. (*block) (ivec, ivec, key);
  72. }
  73. out[l] = ivec[n] ^= in[l];
  74. ++l;
  75. n = (n + 1) % 16;
  76. }
  77. *num = n;
  78. } else {
  79. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  80. if (16 % sizeof(size_t) == 0) { /* always true actually */
  81. do {
  82. while (n && len) {
  83. unsigned char c;
  84. *(out++) = ivec[n] ^ (c = *(in++));
  85. ivec[n] = c;
  86. --len;
  87. n = (n + 1) % 16;
  88. }
  89. # if defined(STRICT_ALIGNMENT)
  90. if (((size_t)in | (size_t)out | (size_t)ivec) %
  91. sizeof(size_t) != 0)
  92. break;
  93. # endif
  94. while (len >= 16) {
  95. (*block) (ivec, ivec, key);
  96. for (; n < 16; n += sizeof(size_t)) {
  97. size_t t = *(size_t_aX *)(in + n);
  98. *(size_t_aX *)(out + n)
  99. = *(size_t_aX *)(ivec + n) ^ t;
  100. *(size_t_aX *)(ivec + n) = t;
  101. }
  102. len -= 16;
  103. out += 16;
  104. in += 16;
  105. n = 0;
  106. }
  107. if (len) {
  108. (*block) (ivec, ivec, key);
  109. while (len--) {
  110. unsigned char c;
  111. out[n] = ivec[n] ^ (c = in[n]);
  112. ivec[n] = c;
  113. ++n;
  114. }
  115. }
  116. *num = n;
  117. return;
  118. } while (0);
  119. }
  120. /* the rest would be commonly eliminated by x86* compiler */
  121. #endif
  122. while (l < len) {
  123. unsigned char c;
  124. if (n == 0) {
  125. (*block) (ivec, ivec, key);
  126. }
  127. out[l] = ivec[n] ^ (c = in[l]);
  128. ivec[n] = c;
  129. ++l;
  130. n = (n + 1) % 16;
  131. }
  132. *num = n;
  133. }
  134. }
  135. /*
  136. * This expects a single block of size nbits for both in and out. Note that
  137. * it corrupts any extra bits in the last byte of out
  138. */
  139. static void cfbr_encrypt_block(const unsigned char *in, unsigned char *out,
  140. int nbits, const void *key,
  141. unsigned char ivec[16], int enc,
  142. block128_f block)
  143. {
  144. int n, rem, num;
  145. unsigned char ovec[16 * 2 + 1]; /* +1 because we dereference (but don't
  146. * use) one byte off the end */
  147. if (nbits <= 0 || nbits > 128)
  148. return;
  149. /* fill in the first half of the new IV with the current IV */
  150. memcpy(ovec, ivec, 16);
  151. /* construct the new IV */
  152. (*block) (ivec, ivec, key);
  153. num = (nbits + 7) / 8;
  154. if (enc) /* encrypt the input */
  155. for (n = 0; n < num; ++n)
  156. out[n] = (ovec[16 + n] = in[n] ^ ivec[n]);
  157. else /* decrypt the input */
  158. for (n = 0; n < num; ++n)
  159. out[n] = (ovec[16 + n] = in[n]) ^ ivec[n];
  160. /* shift ovec left... */
  161. rem = nbits % 8;
  162. num = nbits / 8;
  163. if (rem == 0)
  164. memcpy(ivec, ovec + num, 16);
  165. else
  166. for (n = 0; n < 16; ++n)
  167. ivec[n] = ovec[n + num] << rem | ovec[n + num + 1] >> (8 - rem);
  168. /* it is not necessary to cleanse ovec, since the IV is not secret */
  169. }
  170. /* N.B. This expects the input to be packed, MS bit first */
  171. void CRYPTO_cfb128_1_encrypt(const unsigned char *in, unsigned char *out,
  172. size_t bits, const void *key,
  173. unsigned char ivec[16], int *num,
  174. int enc, block128_f block)
  175. {
  176. size_t n;
  177. unsigned char c[1], d[1];
  178. for (n = 0; n < bits; ++n) {
  179. c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
  180. cfbr_encrypt_block(c, d, 1, key, ivec, enc, block);
  181. out[n / 8] = (out[n / 8] & ~(1 << (unsigned int)(7 - n % 8))) |
  182. ((d[0] & 0x80) >> (unsigned int)(n % 8));
  183. }
  184. }
  185. void CRYPTO_cfb128_8_encrypt(const unsigned char *in, unsigned char *out,
  186. size_t length, const void *key,
  187. unsigned char ivec[16], int *num,
  188. int enc, block128_f block)
  189. {
  190. size_t n;
  191. for (n = 0; n < length; ++n)
  192. cfbr_encrypt_block(&in[n], &out[n], 8, key, ivec, enc, block);
  193. }