aes_ige.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* crypto/aes/aes_ige.c -*- mode:C; c-file-style: "eay" -*- */
  2. /* ====================================================================
  3. * Copyright (c) 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. #include "cryptlib.h"
  52. #include <openssl/aes.h>
  53. #include "aes_locl.h"
  54. #define N_WORDS (AES_BLOCK_SIZE / sizeof(unsigned long))
  55. typedef struct {
  56. unsigned long data[N_WORDS];
  57. } aes_block_t;
  58. /* XXX: probably some better way to do this */
  59. #if defined(__i386__) || defined(__x86_64__)
  60. #define UNALIGNED_MEMOPS_ARE_FAST 1
  61. #else
  62. #define UNALIGNED_MEMOPS_ARE_FAST 0
  63. #endif
  64. #if UNALIGNED_MEMOPS_ARE_FAST
  65. #define load_block(d, s) (d) = *(const aes_block_t *)(s)
  66. #define store_block(d, s) *(aes_block_t *)(d) = (s)
  67. #else
  68. #define load_block(d, s) memcpy((d).data, (s), AES_BLOCK_SIZE)
  69. #define store_block(d, s) memcpy((d), (s).data, AES_BLOCK_SIZE)
  70. #endif
  71. /* N.B. The IV for this mode is _twice_ the block size */
  72. void AES_ige_encrypt(const unsigned char *in, unsigned char *out,
  73. size_t length, const AES_KEY *key,
  74. unsigned char *ivec, const int enc)
  75. {
  76. size_t n;
  77. size_t len = length;
  78. OPENSSL_assert(in && out && key && ivec);
  79. OPENSSL_assert((AES_ENCRYPT == enc)||(AES_DECRYPT == enc));
  80. OPENSSL_assert((length%AES_BLOCK_SIZE) == 0);
  81. len = length / AES_BLOCK_SIZE;
  82. if (AES_ENCRYPT == enc)
  83. {
  84. if (in != out &&
  85. (UNALIGNED_MEMOPS_ARE_FAST || ((size_t)in|(size_t)out|(size_t)ivec)%sizeof(long)==0))
  86. {
  87. aes_block_t *ivp = (aes_block_t *)ivec;
  88. aes_block_t *iv2p = (aes_block_t *)(ivec + AES_BLOCK_SIZE);
  89. while (len)
  90. {
  91. aes_block_t *inp = (aes_block_t *)in;
  92. aes_block_t *outp = (aes_block_t *)out;
  93. for(n=0 ; n < N_WORDS; ++n)
  94. outp->data[n] = inp->data[n] ^ ivp->data[n];
  95. AES_encrypt((unsigned char *)outp->data, (unsigned char *)outp->data, key);
  96. for(n=0 ; n < N_WORDS; ++n)
  97. outp->data[n] ^= iv2p->data[n];
  98. ivp = outp;
  99. iv2p = inp;
  100. --len;
  101. in += AES_BLOCK_SIZE;
  102. out += AES_BLOCK_SIZE;
  103. }
  104. memcpy(ivec, ivp->data, AES_BLOCK_SIZE);
  105. memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE);
  106. }
  107. else
  108. {
  109. aes_block_t tmp, tmp2;
  110. aes_block_t iv;
  111. aes_block_t iv2;
  112. load_block(iv, ivec);
  113. load_block(iv2, ivec + AES_BLOCK_SIZE);
  114. while (len)
  115. {
  116. load_block(tmp, in);
  117. for(n=0 ; n < N_WORDS; ++n)
  118. tmp2.data[n] = tmp.data[n] ^ iv.data[n];
  119. AES_encrypt((unsigned char *)tmp2.data, (unsigned char *)tmp2.data, key);
  120. for(n=0 ; n < N_WORDS; ++n)
  121. tmp2.data[n] ^= iv2.data[n];
  122. store_block(out, tmp2);
  123. iv = tmp2;
  124. iv2 = tmp;
  125. --len;
  126. in += AES_BLOCK_SIZE;
  127. out += AES_BLOCK_SIZE;
  128. }
  129. memcpy(ivec, iv.data, AES_BLOCK_SIZE);
  130. memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE);
  131. }
  132. }
  133. else
  134. {
  135. if (in != out &&
  136. (UNALIGNED_MEMOPS_ARE_FAST || ((size_t)in|(size_t)out|(size_t)ivec)%sizeof(long)==0))
  137. {
  138. aes_block_t *ivp = (aes_block_t *)ivec;
  139. aes_block_t *iv2p = (aes_block_t *)(ivec + AES_BLOCK_SIZE);
  140. while (len)
  141. {
  142. aes_block_t tmp;
  143. aes_block_t *inp = (aes_block_t *)in;
  144. aes_block_t *outp = (aes_block_t *)out;
  145. for(n=0 ; n < N_WORDS; ++n)
  146. tmp.data[n] = inp->data[n] ^ iv2p->data[n];
  147. AES_decrypt((unsigned char *)tmp.data, (unsigned char *)outp->data, key);
  148. for(n=0 ; n < N_WORDS; ++n)
  149. outp->data[n] ^= ivp->data[n];
  150. ivp = inp;
  151. iv2p = outp;
  152. --len;
  153. in += AES_BLOCK_SIZE;
  154. out += AES_BLOCK_SIZE;
  155. }
  156. memcpy(ivec, ivp->data, AES_BLOCK_SIZE);
  157. memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE);
  158. }
  159. else
  160. {
  161. aes_block_t tmp, tmp2;
  162. aes_block_t iv;
  163. aes_block_t iv2;
  164. load_block(iv, ivec);
  165. load_block(iv2, ivec + AES_BLOCK_SIZE);
  166. while (len)
  167. {
  168. load_block(tmp, in);
  169. tmp2 = tmp;
  170. for(n=0 ; n < N_WORDS; ++n)
  171. tmp.data[n] ^= iv2.data[n];
  172. AES_decrypt((unsigned char *)tmp.data, (unsigned char *)tmp.data, key);
  173. for(n=0 ; n < N_WORDS; ++n)
  174. tmp.data[n] ^= iv.data[n];
  175. store_block(out, tmp);
  176. iv = tmp2;
  177. iv2 = tmp;
  178. --len;
  179. in += AES_BLOCK_SIZE;
  180. out += AES_BLOCK_SIZE;
  181. }
  182. memcpy(ivec, iv.data, AES_BLOCK_SIZE);
  183. memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE);
  184. }
  185. }
  186. }
  187. /*
  188. * Note that its effectively impossible to do biIGE in anything other
  189. * than a single pass, so no provision is made for chaining.
  190. */
  191. /* N.B. The IV for this mode is _four times_ the block size */
  192. void AES_bi_ige_encrypt(const unsigned char *in, unsigned char *out,
  193. size_t length, const AES_KEY *key,
  194. const AES_KEY *key2, const unsigned char *ivec,
  195. const int enc)
  196. {
  197. size_t n;
  198. size_t len = length;
  199. unsigned char tmp[AES_BLOCK_SIZE];
  200. unsigned char tmp2[AES_BLOCK_SIZE];
  201. unsigned char tmp3[AES_BLOCK_SIZE];
  202. unsigned char prev[AES_BLOCK_SIZE];
  203. const unsigned char *iv;
  204. const unsigned char *iv2;
  205. OPENSSL_assert(in && out && key && ivec);
  206. OPENSSL_assert((AES_ENCRYPT == enc)||(AES_DECRYPT == enc));
  207. OPENSSL_assert((length%AES_BLOCK_SIZE) == 0);
  208. if (AES_ENCRYPT == enc)
  209. {
  210. /* XXX: Do a separate case for when in != out (strictly should
  211. check for overlap, too) */
  212. /* First the forward pass */
  213. iv = ivec;
  214. iv2 = ivec + AES_BLOCK_SIZE;
  215. while (len >= AES_BLOCK_SIZE)
  216. {
  217. for(n=0 ; n < AES_BLOCK_SIZE ; ++n)
  218. out[n] = in[n] ^ iv[n];
  219. AES_encrypt(out, out, key);
  220. for(n=0 ; n < AES_BLOCK_SIZE ; ++n)
  221. out[n] ^= iv2[n];
  222. iv = out;
  223. memcpy(prev, in, AES_BLOCK_SIZE);
  224. iv2 = prev;
  225. len -= AES_BLOCK_SIZE;
  226. in += AES_BLOCK_SIZE;
  227. out += AES_BLOCK_SIZE;
  228. }
  229. /* And now backwards */
  230. iv = ivec + AES_BLOCK_SIZE*2;
  231. iv2 = ivec + AES_BLOCK_SIZE*3;
  232. len = length;
  233. while(len >= AES_BLOCK_SIZE)
  234. {
  235. out -= AES_BLOCK_SIZE;
  236. /* XXX: reduce copies by alternating between buffers */
  237. memcpy(tmp, out, AES_BLOCK_SIZE);
  238. for(n=0 ; n < AES_BLOCK_SIZE ; ++n)
  239. out[n] ^= iv[n];
  240. /* hexdump(stdout, "out ^ iv", out, AES_BLOCK_SIZE); */
  241. AES_encrypt(out, out, key);
  242. /* hexdump(stdout,"enc", out, AES_BLOCK_SIZE); */
  243. /* hexdump(stdout,"iv2", iv2, AES_BLOCK_SIZE); */
  244. for(n=0 ; n < AES_BLOCK_SIZE ; ++n)
  245. out[n] ^= iv2[n];
  246. /* hexdump(stdout,"out", out, AES_BLOCK_SIZE); */
  247. iv = out;
  248. memcpy(prev, tmp, AES_BLOCK_SIZE);
  249. iv2 = prev;
  250. len -= AES_BLOCK_SIZE;
  251. }
  252. }
  253. else
  254. {
  255. /* First backwards */
  256. iv = ivec + AES_BLOCK_SIZE*2;
  257. iv2 = ivec + AES_BLOCK_SIZE*3;
  258. in += length;
  259. out += length;
  260. while (len >= AES_BLOCK_SIZE)
  261. {
  262. in -= AES_BLOCK_SIZE;
  263. out -= AES_BLOCK_SIZE;
  264. memcpy(tmp, in, AES_BLOCK_SIZE);
  265. memcpy(tmp2, in, AES_BLOCK_SIZE);
  266. for(n=0 ; n < AES_BLOCK_SIZE ; ++n)
  267. tmp[n] ^= iv2[n];
  268. AES_decrypt(tmp, out, key);
  269. for(n=0 ; n < AES_BLOCK_SIZE ; ++n)
  270. out[n] ^= iv[n];
  271. memcpy(tmp3, tmp2, AES_BLOCK_SIZE);
  272. iv = tmp3;
  273. iv2 = out;
  274. len -= AES_BLOCK_SIZE;
  275. }
  276. /* And now forwards */
  277. iv = ivec;
  278. iv2 = ivec + AES_BLOCK_SIZE;
  279. len = length;
  280. while (len >= AES_BLOCK_SIZE)
  281. {
  282. memcpy(tmp, out, AES_BLOCK_SIZE);
  283. memcpy(tmp2, out, AES_BLOCK_SIZE);
  284. for(n=0 ; n < AES_BLOCK_SIZE ; ++n)
  285. tmp[n] ^= iv2[n];
  286. AES_decrypt(tmp, out, key);
  287. for(n=0 ; n < AES_BLOCK_SIZE ; ++n)
  288. out[n] ^= iv[n];
  289. memcpy(tmp3, tmp2, AES_BLOCK_SIZE);
  290. iv = tmp3;
  291. iv2 = out;
  292. len -= AES_BLOCK_SIZE;
  293. in += AES_BLOCK_SIZE;
  294. out += AES_BLOCK_SIZE;
  295. }
  296. }
  297. }