cfb128.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <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. * The input and output encrypted as though 128bit cfb mode is being used.
  61. * The extra state information to record how much of the 128bit block we have
  62. * used is contained in *num;
  63. */
  64. void CRYPTO_cfb128_encrypt(const unsigned char *in, unsigned char *out,
  65. size_t len, const void *key,
  66. unsigned char ivec[16], int *num,
  67. int enc, block128_f block)
  68. {
  69. unsigned int n;
  70. size_t l = 0;
  71. assert(in && out && key && ivec && num);
  72. n = *num;
  73. if (enc) {
  74. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  75. if (16 % sizeof(size_t) == 0) { /* always true actually */
  76. do {
  77. while (n && len) {
  78. *(out++) = ivec[n] ^= *(in++);
  79. --len;
  80. n = (n + 1) % 16;
  81. }
  82. # if defined(STRICT_ALIGNMENT)
  83. if (((size_t)in | (size_t)out | (size_t)ivec) %
  84. sizeof(size_t) != 0)
  85. break;
  86. # endif
  87. while (len >= 16) {
  88. (*block) (ivec, ivec, key);
  89. for (; n < 16; n += sizeof(size_t)) {
  90. *(size_t *)(out + n) =
  91. *(size_t *)(ivec + n) ^= *(size_t *)(in + n);
  92. }
  93. len -= 16;
  94. out += 16;
  95. in += 16;
  96. n = 0;
  97. }
  98. if (len) {
  99. (*block) (ivec, ivec, key);
  100. while (len--) {
  101. out[n] = ivec[n] ^= in[n];
  102. ++n;
  103. }
  104. }
  105. *num = n;
  106. return;
  107. } while (0);
  108. }
  109. /* the rest would be commonly eliminated by x86* compiler */
  110. #endif
  111. while (l < len) {
  112. if (n == 0) {
  113. (*block) (ivec, ivec, key);
  114. }
  115. out[l] = ivec[n] ^= in[l];
  116. ++l;
  117. n = (n + 1) % 16;
  118. }
  119. *num = n;
  120. } else {
  121. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  122. if (16 % sizeof(size_t) == 0) { /* always true actually */
  123. do {
  124. while (n && len) {
  125. unsigned char c;
  126. *(out++) = ivec[n] ^ (c = *(in++));
  127. ivec[n] = c;
  128. --len;
  129. n = (n + 1) % 16;
  130. }
  131. # if defined(STRICT_ALIGNMENT)
  132. if (((size_t)in | (size_t)out | (size_t)ivec) %
  133. sizeof(size_t) != 0)
  134. break;
  135. # endif
  136. while (len >= 16) {
  137. (*block) (ivec, ivec, key);
  138. for (; n < 16; n += sizeof(size_t)) {
  139. size_t t = *(size_t *)(in + n);
  140. *(size_t *)(out + n) = *(size_t *)(ivec + n) ^ t;
  141. *(size_t *)(ivec + n) = t;
  142. }
  143. len -= 16;
  144. out += 16;
  145. in += 16;
  146. n = 0;
  147. }
  148. if (len) {
  149. (*block) (ivec, ivec, key);
  150. while (len--) {
  151. unsigned char c;
  152. out[n] = ivec[n] ^ (c = in[n]);
  153. ivec[n] = c;
  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. unsigned char c;
  165. if (n == 0) {
  166. (*block) (ivec, ivec, key);
  167. }
  168. out[l] = ivec[n] ^ (c = in[l]);
  169. ivec[n] = c;
  170. ++l;
  171. n = (n + 1) % 16;
  172. }
  173. *num = n;
  174. }
  175. }
  176. /*
  177. * This expects a single block of size nbits for both in and out. Note that
  178. * it corrupts any extra bits in the last byte of out
  179. */
  180. static void cfbr_encrypt_block(const unsigned char *in, unsigned char *out,
  181. int nbits, const void *key,
  182. unsigned char ivec[16], int enc,
  183. block128_f block)
  184. {
  185. int n, rem, num;
  186. unsigned char ovec[16 * 2 + 1]; /* +1 because we dererefence (but don't
  187. * use) one byte off the end */
  188. if (nbits <= 0 || nbits > 128)
  189. return;
  190. /* fill in the first half of the new IV with the current IV */
  191. memcpy(ovec, ivec, 16);
  192. /* construct the new IV */
  193. (*block) (ivec, ivec, key);
  194. num = (nbits + 7) / 8;
  195. if (enc) /* encrypt the input */
  196. for (n = 0; n < num; ++n)
  197. out[n] = (ovec[16 + n] = in[n] ^ ivec[n]);
  198. else /* decrypt the input */
  199. for (n = 0; n < num; ++n)
  200. out[n] = (ovec[16 + n] = in[n]) ^ ivec[n];
  201. /* shift ovec left... */
  202. rem = nbits % 8;
  203. num = nbits / 8;
  204. if (rem == 0)
  205. memcpy(ivec, ovec + num, 16);
  206. else
  207. for (n = 0; n < 16; ++n)
  208. ivec[n] = ovec[n + num] << rem | ovec[n + num + 1] >> (8 - rem);
  209. /* it is not necessary to cleanse ovec, since the IV is not secret */
  210. }
  211. /* N.B. This expects the input to be packed, MS bit first */
  212. void CRYPTO_cfb128_1_encrypt(const unsigned char *in, unsigned char *out,
  213. size_t bits, const void *key,
  214. unsigned char ivec[16], int *num,
  215. int enc, block128_f block)
  216. {
  217. size_t n;
  218. unsigned char c[1], d[1];
  219. assert(in && out && key && ivec && num);
  220. assert(*num == 0);
  221. for (n = 0; n < bits; ++n) {
  222. c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
  223. cfbr_encrypt_block(c, d, 1, key, ivec, enc, block);
  224. out[n / 8] = (out[n / 8] & ~(1 << (unsigned int)(7 - n % 8))) |
  225. ((d[0] & 0x80) >> (unsigned int)(n % 8));
  226. }
  227. }
  228. void CRYPTO_cfb128_8_encrypt(const unsigned char *in, unsigned char *out,
  229. size_t length, const void *key,
  230. unsigned char ivec[16], int *num,
  231. int enc, block128_f block)
  232. {
  233. size_t n;
  234. assert(in && out && key && ivec && num);
  235. assert(*num == 0);
  236. for (n = 0; n < length; ++n)
  237. cfbr_encrypt_block(&in[n], &out[n], 8, key, ivec, enc, block);
  238. }