cfb128.c 6.8 KB

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