aes_ige.c 11 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. const unsigned long length, const AES_KEY *key,
  74. unsigned char *ivec, const int enc)
  75. {
  76. unsigned long n;
  77. unsigned long len;
  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. if (in != out &&
  84. (UNALIGNED_MEMOPS_ARE_FAST
  85. || ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(long) ==
  86. 0)) {
  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. aes_block_t *inp = (aes_block_t *) in;
  91. aes_block_t *outp = (aes_block_t *) out;
  92. for (n = 0; n < N_WORDS; ++n)
  93. outp->data[n] = inp->data[n] ^ ivp->data[n];
  94. AES_encrypt((unsigned char *)outp->data,
  95. (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. } else {
  107. aes_block_t tmp, tmp2;
  108. aes_block_t iv;
  109. aes_block_t iv2;
  110. load_block(iv, ivec);
  111. load_block(iv2, ivec + AES_BLOCK_SIZE);
  112. while (len) {
  113. load_block(tmp, in);
  114. for (n = 0; n < N_WORDS; ++n)
  115. tmp2.data[n] = tmp.data[n] ^ iv.data[n];
  116. AES_encrypt((unsigned char *)tmp2.data,
  117. (unsigned char *)tmp2.data, key);
  118. for (n = 0; n < N_WORDS; ++n)
  119. tmp2.data[n] ^= iv2.data[n];
  120. store_block(out, tmp2);
  121. iv = tmp2;
  122. iv2 = tmp;
  123. --len;
  124. in += AES_BLOCK_SIZE;
  125. out += AES_BLOCK_SIZE;
  126. }
  127. memcpy(ivec, iv.data, AES_BLOCK_SIZE);
  128. memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE);
  129. }
  130. } else {
  131. if (in != out &&
  132. (UNALIGNED_MEMOPS_ARE_FAST
  133. || ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(long) ==
  134. 0)) {
  135. aes_block_t *ivp = (aes_block_t *) ivec;
  136. aes_block_t *iv2p = (aes_block_t *) (ivec + AES_BLOCK_SIZE);
  137. while (len) {
  138. aes_block_t tmp;
  139. aes_block_t *inp = (aes_block_t *) in;
  140. aes_block_t *outp = (aes_block_t *) out;
  141. for (n = 0; n < N_WORDS; ++n)
  142. tmp.data[n] = inp->data[n] ^ iv2p->data[n];
  143. AES_decrypt((unsigned char *)tmp.data,
  144. (unsigned char *)outp->data, key);
  145. for (n = 0; n < N_WORDS; ++n)
  146. outp->data[n] ^= ivp->data[n];
  147. ivp = inp;
  148. iv2p = outp;
  149. --len;
  150. in += AES_BLOCK_SIZE;
  151. out += AES_BLOCK_SIZE;
  152. }
  153. memcpy(ivec, ivp->data, AES_BLOCK_SIZE);
  154. memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE);
  155. } else {
  156. aes_block_t tmp, tmp2;
  157. aes_block_t iv;
  158. aes_block_t iv2;
  159. load_block(iv, ivec);
  160. load_block(iv2, ivec + AES_BLOCK_SIZE);
  161. while (len) {
  162. load_block(tmp, in);
  163. tmp2 = tmp;
  164. for (n = 0; n < N_WORDS; ++n)
  165. tmp.data[n] ^= iv2.data[n];
  166. AES_decrypt((unsigned char *)tmp.data,
  167. (unsigned char *)tmp.data, key);
  168. for (n = 0; n < N_WORDS; ++n)
  169. tmp.data[n] ^= iv.data[n];
  170. store_block(out, tmp);
  171. iv = tmp2;
  172. iv2 = tmp;
  173. --len;
  174. in += AES_BLOCK_SIZE;
  175. out += AES_BLOCK_SIZE;
  176. }
  177. memcpy(ivec, iv.data, AES_BLOCK_SIZE);
  178. memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE);
  179. }
  180. }
  181. }
  182. /*
  183. * Note that its effectively impossible to do biIGE in anything other
  184. * than a single pass, so no provision is made for chaining.
  185. */
  186. /* N.B. The IV for this mode is _four times_ the block size */
  187. void AES_bi_ige_encrypt(const unsigned char *in, unsigned char *out,
  188. const unsigned long length, const AES_KEY *key,
  189. const AES_KEY *key2, const unsigned char *ivec,
  190. const int enc)
  191. {
  192. unsigned long n;
  193. unsigned long len = length;
  194. unsigned char tmp[AES_BLOCK_SIZE];
  195. unsigned char tmp2[AES_BLOCK_SIZE];
  196. unsigned char tmp3[AES_BLOCK_SIZE];
  197. unsigned char prev[AES_BLOCK_SIZE];
  198. const unsigned char *iv;
  199. const unsigned char *iv2;
  200. OPENSSL_assert(in && out && key && ivec);
  201. OPENSSL_assert((AES_ENCRYPT == enc) || (AES_DECRYPT == enc));
  202. OPENSSL_assert((length % AES_BLOCK_SIZE) == 0);
  203. if (AES_ENCRYPT == enc) {
  204. /*
  205. * XXX: Do a separate case for when in != out (strictly should check
  206. * for overlap, too)
  207. */
  208. /* First the forward pass */
  209. iv = ivec;
  210. iv2 = ivec + AES_BLOCK_SIZE;
  211. while (len >= AES_BLOCK_SIZE) {
  212. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  213. out[n] = in[n] ^ iv[n];
  214. AES_encrypt(out, out, key);
  215. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  216. out[n] ^= iv2[n];
  217. iv = out;
  218. memcpy(prev, in, AES_BLOCK_SIZE);
  219. iv2 = prev;
  220. len -= AES_BLOCK_SIZE;
  221. in += AES_BLOCK_SIZE;
  222. out += AES_BLOCK_SIZE;
  223. }
  224. /* And now backwards */
  225. iv = ivec + AES_BLOCK_SIZE * 2;
  226. iv2 = ivec + AES_BLOCK_SIZE * 3;
  227. len = length;
  228. while (len >= AES_BLOCK_SIZE) {
  229. out -= AES_BLOCK_SIZE;
  230. /*
  231. * XXX: reduce copies by alternating between buffers
  232. */
  233. memcpy(tmp, out, AES_BLOCK_SIZE);
  234. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  235. out[n] ^= iv[n];
  236. /*
  237. * hexdump(stdout, "out ^ iv", out, AES_BLOCK_SIZE);
  238. */
  239. AES_encrypt(out, out, key);
  240. /*
  241. * hexdump(stdout,"enc", out, AES_BLOCK_SIZE);
  242. */
  243. /*
  244. * hexdump(stdout,"iv2", iv2, AES_BLOCK_SIZE);
  245. */
  246. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  247. out[n] ^= iv2[n];
  248. /*
  249. * hexdump(stdout,"out", out, AES_BLOCK_SIZE);
  250. */
  251. iv = out;
  252. memcpy(prev, tmp, AES_BLOCK_SIZE);
  253. iv2 = prev;
  254. len -= AES_BLOCK_SIZE;
  255. }
  256. } else {
  257. /* First backwards */
  258. iv = ivec + AES_BLOCK_SIZE * 2;
  259. iv2 = ivec + AES_BLOCK_SIZE * 3;
  260. in += length;
  261. out += length;
  262. while (len >= AES_BLOCK_SIZE) {
  263. in -= AES_BLOCK_SIZE;
  264. out -= AES_BLOCK_SIZE;
  265. memcpy(tmp, in, AES_BLOCK_SIZE);
  266. memcpy(tmp2, in, AES_BLOCK_SIZE);
  267. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  268. tmp[n] ^= iv2[n];
  269. AES_decrypt(tmp, out, key);
  270. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  271. out[n] ^= iv[n];
  272. memcpy(tmp3, tmp2, AES_BLOCK_SIZE);
  273. iv = tmp3;
  274. iv2 = out;
  275. len -= AES_BLOCK_SIZE;
  276. }
  277. /* And now forwards */
  278. iv = ivec;
  279. iv2 = ivec + AES_BLOCK_SIZE;
  280. len = length;
  281. while (len >= AES_BLOCK_SIZE) {
  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. }