aes_ige.c 11 KB

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