aes_cfb.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* crypto/aes/aes_cfb.c -*- mode:C; c-file-style: "eay" -*- */
  2. /* ====================================================================
  3. * Copyright (c) 2002-2006 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. */
  51. #ifndef AES_DEBUG
  52. # ifndef NDEBUG
  53. # define NDEBUG
  54. # endif
  55. #endif
  56. #include <assert.h>
  57. #include <openssl/aes.h>
  58. #include "aes_locl.h"
  59. #include "e_os.h"
  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. # undef STRICT_ALIGNMENT
  65. #endif
  66. /* The input and output encrypted as though 128bit cfb mode is being
  67. * used. The extra state information to record how much of the
  68. * 128bit block we have used is contained in *num;
  69. */
  70. void AES_cfb128_encrypt(const unsigned char *in, unsigned char *out,
  71. size_t length, const AES_KEY *key,
  72. unsigned char *ivec, int *num, const int enc) {
  73. unsigned int n;
  74. size_t l = 0;
  75. assert(in && out && key && ivec && num);
  76. n = *num;
  77. #if !defined(OPENSSL_SMALL_FOOTPRINT)
  78. if (AES_BLOCK_SIZE%sizeof(size_t) == 0) { /* always true actually */
  79. if (enc) {
  80. if (n) {
  81. while (length) {
  82. *(out++) = ivec[n] ^= *(in++);
  83. length--;
  84. if(!(n = (n + 1) % AES_BLOCK_SIZE))
  85. break;
  86. }
  87. }
  88. #if defined(STRICT_ALIGNMENT)
  89. if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
  90. goto enc_unaligned;
  91. #endif
  92. while ((l + AES_BLOCK_SIZE) <= length) {
  93. unsigned int i;
  94. AES_encrypt(ivec, ivec, key);
  95. for (i=0;i<AES_BLOCK_SIZE;i+=sizeof(size_t)) {
  96. *(size_t*)(out+l+i) =
  97. *(size_t*)(ivec+i) ^= *(size_t*)(in+l+i);
  98. }
  99. l += AES_BLOCK_SIZE;
  100. }
  101. if (l < length) {
  102. AES_encrypt(ivec, ivec, key);
  103. do { out[l] = ivec[n] ^= in[l];
  104. l++; n++;
  105. } while (l < length);
  106. }
  107. } else {
  108. if (n) {
  109. while (length) {
  110. unsigned char c;
  111. *(out++) = ivec[n] ^ (c = *(in++)); ivec[n] = c;
  112. length--;
  113. if(!(n = (n + 1) % AES_BLOCK_SIZE))
  114. break;
  115. }
  116. }
  117. #if defined(STRICT_ALIGNMENT)
  118. if (((size_t)in|(size_t)out|(size_t)ivec)%sizeof(size_t) != 0)
  119. goto dec_unaligned;
  120. #endif
  121. while (l + AES_BLOCK_SIZE <= length) {
  122. unsigned int i;
  123. AES_encrypt(ivec, ivec, key);
  124. for (i=0;i<AES_BLOCK_SIZE;i+=sizeof(size_t)) {
  125. size_t t = *(size_t*)(in+l+i);
  126. *(size_t*)(out+l+i) = *(size_t*)(ivec+i) ^ t;
  127. *(size_t*)(ivec+i) = t;
  128. }
  129. l += AES_BLOCK_SIZE;
  130. }
  131. if (l < length) {
  132. AES_encrypt(ivec, ivec, key);
  133. do { unsigned char c;
  134. out[l] = ivec[n] ^ (c = in[l]); ivec[n] = c;
  135. l++; n++;
  136. } while (l < length);
  137. }
  138. }
  139. *num = n;
  140. return;
  141. }
  142. #endif
  143. /* this code would be commonly eliminated by x86* compiler */
  144. if (enc) {
  145. #if defined(STRICT_ALIGNMENT) && !defined(OPENSSL_SMALL_FOOTPRINT)
  146. enc_unaligned:
  147. #endif
  148. while (l<length) {
  149. if (n == 0) {
  150. AES_encrypt(ivec, ivec, key);
  151. }
  152. out[l] = ivec[n] ^= in[l];
  153. l++;
  154. n = (n+1) % AES_BLOCK_SIZE;
  155. }
  156. } else {
  157. #if defined(STRICT_ALIGNMENT) && !defined(OPENSSL_SMALL_FOOTPRINT)
  158. dec_unaligned:
  159. #endif
  160. while (l<length) {
  161. unsigned char c;
  162. if (n == 0) {
  163. AES_encrypt(ivec, ivec, key);
  164. }
  165. out[l] = ivec[n] ^ (c = in[l]); ivec[n] = c;
  166. l++;
  167. n = (n+1) % AES_BLOCK_SIZE;
  168. }
  169. }
  170. *num=n;
  171. }
  172. /* This expects a single block of size nbits for both in and out. Note that
  173. it corrupts any extra bits in the last byte of out */
  174. void AES_cfbr_encrypt_block(const unsigned char *in,unsigned char *out,
  175. const int nbits,const AES_KEY *key,
  176. unsigned char *ivec,const int enc)
  177. {
  178. int n,rem,num;
  179. unsigned char ovec[AES_BLOCK_SIZE*2 + 1]; /* +1 because we dererefence (but don't use) one byte off the end */
  180. if (nbits<=0 || nbits>128) return;
  181. /* fill in the first half of the new IV with the current IV */
  182. memcpy(ovec,ivec,AES_BLOCK_SIZE);
  183. /* construct the new IV */
  184. AES_encrypt(ivec,ivec,key);
  185. num = (nbits+7)/8;
  186. if (enc) /* encrypt the input */
  187. for(n=0 ; n < num ; ++n)
  188. out[n] = (ovec[AES_BLOCK_SIZE+n] = in[n] ^ ivec[n]);
  189. else /* decrypt the input */
  190. for(n=0 ; n < num ; ++n)
  191. out[n] = (ovec[AES_BLOCK_SIZE+n] = in[n]) ^ ivec[n];
  192. /* shift ovec left... */
  193. rem = nbits%8;
  194. num = nbits/8;
  195. if(rem==0)
  196. memcpy(ivec,ovec+num,AES_BLOCK_SIZE);
  197. else
  198. for(n=0 ; n < AES_BLOCK_SIZE ; ++n)
  199. ivec[n] = ovec[n+num]<<rem | ovec[n+num+1]>>(8-rem);
  200. /* it is not necessary to cleanse ovec, since the IV is not secret */
  201. }
  202. /* N.B. This expects the input to be packed, MS bit first */
  203. void AES_cfb1_encrypt(const unsigned char *in, unsigned char *out,
  204. size_t length, const AES_KEY *key,
  205. unsigned char *ivec, int *num, const int enc)
  206. {
  207. size_t n;
  208. unsigned char c[1],d[1];
  209. assert(in && out && key && ivec && num);
  210. assert(*num == 0);
  211. memset(out,0,(length+7)/8);
  212. for(n=0 ; n < length ; ++n)
  213. {
  214. c[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0;
  215. AES_cfbr_encrypt_block(c,d,1,key,ivec,enc);
  216. out[n/8]=(out[n/8]&~(1 << (7-n%8)))|((d[0]&0x80) >> (n%8));
  217. }
  218. }
  219. void AES_cfb8_encrypt(const unsigned char *in, unsigned char *out,
  220. size_t length, const AES_KEY *key,
  221. unsigned char *ivec, int *num, const int enc)
  222. {
  223. size_t n;
  224. assert(in && out && key && ivec && num);
  225. assert(*num == 0);
  226. for(n=0 ; n < length ; ++n)
  227. AES_cfbr_encrypt_block(&in[n],&out[n],8,key,ivec,enc);
  228. }