aes_ige.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/cryptlib.h"
  10. #include <openssl/aes.h>
  11. #include "aes_local.h"
  12. /* XXX: probably some better way to do this */
  13. #if defined(__i386__) || defined(__x86_64__)
  14. # define UNALIGNED_MEMOPS_ARE_FAST 1
  15. #else
  16. # define UNALIGNED_MEMOPS_ARE_FAST 0
  17. #endif
  18. #define N_WORDS (AES_BLOCK_SIZE / sizeof(unsigned long))
  19. typedef struct {
  20. unsigned long data[N_WORDS];
  21. #if defined(__GNUC__) && UNALIGNED_MEMOPS_ARE_FAST
  22. } aes_block_t __attribute((__aligned__(1)));
  23. #else
  24. } aes_block_t;
  25. #endif
  26. #if UNALIGNED_MEMOPS_ARE_FAST
  27. # define load_block(d, s) (d) = *(const aes_block_t *)(s)
  28. # define store_block(d, s) *(aes_block_t *)(d) = (s)
  29. #else
  30. # define load_block(d, s) memcpy((d).data, (s), AES_BLOCK_SIZE)
  31. # define store_block(d, s) memcpy((d), (s).data, AES_BLOCK_SIZE)
  32. #endif
  33. /* N.B. The IV for this mode is _twice_ the block size */
  34. void AES_ige_encrypt(const unsigned char *in, unsigned char *out,
  35. size_t length, const AES_KEY *key,
  36. unsigned char *ivec, const int enc)
  37. {
  38. size_t n;
  39. size_t len = length;
  40. if (length == 0)
  41. return;
  42. OPENSSL_assert(in && out && key && ivec);
  43. OPENSSL_assert((AES_ENCRYPT == enc) || (AES_DECRYPT == enc));
  44. OPENSSL_assert((length % AES_BLOCK_SIZE) == 0);
  45. len = length / AES_BLOCK_SIZE;
  46. if (AES_ENCRYPT == enc) {
  47. if (in != out &&
  48. (UNALIGNED_MEMOPS_ARE_FAST
  49. || ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(long) ==
  50. 0)) {
  51. aes_block_t *ivp = (aes_block_t *) ivec;
  52. aes_block_t *iv2p = (aes_block_t *) (ivec + AES_BLOCK_SIZE);
  53. while (len) {
  54. aes_block_t *inp = (aes_block_t *) in;
  55. aes_block_t *outp = (aes_block_t *) out;
  56. for (n = 0; n < N_WORDS; ++n)
  57. outp->data[n] = inp->data[n] ^ ivp->data[n];
  58. AES_encrypt((unsigned char *)outp->data,
  59. (unsigned char *)outp->data, key);
  60. for (n = 0; n < N_WORDS; ++n)
  61. outp->data[n] ^= iv2p->data[n];
  62. ivp = outp;
  63. iv2p = inp;
  64. --len;
  65. in += AES_BLOCK_SIZE;
  66. out += AES_BLOCK_SIZE;
  67. }
  68. memcpy(ivec, ivp->data, AES_BLOCK_SIZE);
  69. memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE);
  70. } else {
  71. aes_block_t tmp, tmp2;
  72. aes_block_t iv;
  73. aes_block_t iv2;
  74. load_block(iv, ivec);
  75. load_block(iv2, ivec + AES_BLOCK_SIZE);
  76. while (len) {
  77. load_block(tmp, in);
  78. for (n = 0; n < N_WORDS; ++n)
  79. tmp2.data[n] = tmp.data[n] ^ iv.data[n];
  80. AES_encrypt((unsigned char *)tmp2.data,
  81. (unsigned char *)tmp2.data, key);
  82. for (n = 0; n < N_WORDS; ++n)
  83. tmp2.data[n] ^= iv2.data[n];
  84. store_block(out, tmp2);
  85. iv = tmp2;
  86. iv2 = tmp;
  87. --len;
  88. in += AES_BLOCK_SIZE;
  89. out += AES_BLOCK_SIZE;
  90. }
  91. memcpy(ivec, iv.data, AES_BLOCK_SIZE);
  92. memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE);
  93. }
  94. } else {
  95. if (in != out &&
  96. (UNALIGNED_MEMOPS_ARE_FAST
  97. || ((size_t)in | (size_t)out | (size_t)ivec) % sizeof(long) ==
  98. 0)) {
  99. aes_block_t *ivp = (aes_block_t *) ivec;
  100. aes_block_t *iv2p = (aes_block_t *) (ivec + AES_BLOCK_SIZE);
  101. while (len) {
  102. aes_block_t tmp;
  103. aes_block_t *inp = (aes_block_t *) in;
  104. aes_block_t *outp = (aes_block_t *) out;
  105. for (n = 0; n < N_WORDS; ++n)
  106. tmp.data[n] = inp->data[n] ^ iv2p->data[n];
  107. AES_decrypt((unsigned char *)tmp.data,
  108. (unsigned char *)outp->data, key);
  109. for (n = 0; n < N_WORDS; ++n)
  110. outp->data[n] ^= ivp->data[n];
  111. ivp = inp;
  112. iv2p = outp;
  113. --len;
  114. in += AES_BLOCK_SIZE;
  115. out += AES_BLOCK_SIZE;
  116. }
  117. memcpy(ivec, ivp->data, AES_BLOCK_SIZE);
  118. memcpy(ivec + AES_BLOCK_SIZE, iv2p->data, AES_BLOCK_SIZE);
  119. } else {
  120. aes_block_t tmp, tmp2;
  121. aes_block_t iv;
  122. aes_block_t iv2;
  123. load_block(iv, ivec);
  124. load_block(iv2, ivec + AES_BLOCK_SIZE);
  125. while (len) {
  126. load_block(tmp, in);
  127. tmp2 = tmp;
  128. for (n = 0; n < N_WORDS; ++n)
  129. tmp.data[n] ^= iv2.data[n];
  130. AES_decrypt((unsigned char *)tmp.data,
  131. (unsigned char *)tmp.data, key);
  132. for (n = 0; n < N_WORDS; ++n)
  133. tmp.data[n] ^= iv.data[n];
  134. store_block(out, tmp);
  135. iv = tmp2;
  136. iv2 = tmp;
  137. --len;
  138. in += AES_BLOCK_SIZE;
  139. out += AES_BLOCK_SIZE;
  140. }
  141. memcpy(ivec, iv.data, AES_BLOCK_SIZE);
  142. memcpy(ivec + AES_BLOCK_SIZE, iv2.data, AES_BLOCK_SIZE);
  143. }
  144. }
  145. }
  146. /*
  147. * Note that its effectively impossible to do biIGE in anything other
  148. * than a single pass, so no provision is made for chaining.
  149. */
  150. /* N.B. The IV for this mode is _four times_ the block size */
  151. void AES_bi_ige_encrypt(const unsigned char *in, unsigned char *out,
  152. size_t length, const AES_KEY *key,
  153. const AES_KEY *key2, const unsigned char *ivec,
  154. const int enc)
  155. {
  156. size_t n;
  157. size_t len = length;
  158. unsigned char tmp[AES_BLOCK_SIZE];
  159. unsigned char tmp2[AES_BLOCK_SIZE];
  160. unsigned char tmp3[AES_BLOCK_SIZE];
  161. unsigned char prev[AES_BLOCK_SIZE];
  162. const unsigned char *iv;
  163. const unsigned char *iv2;
  164. OPENSSL_assert(in && out && key && ivec);
  165. OPENSSL_assert((AES_ENCRYPT == enc) || (AES_DECRYPT == enc));
  166. OPENSSL_assert((length % AES_BLOCK_SIZE) == 0);
  167. if (AES_ENCRYPT == enc) {
  168. /*
  169. * XXX: Do a separate case for when in != out (strictly should check
  170. * for overlap, too)
  171. */
  172. /* First the forward pass */
  173. iv = ivec;
  174. iv2 = ivec + AES_BLOCK_SIZE;
  175. while (len >= AES_BLOCK_SIZE) {
  176. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  177. out[n] = in[n] ^ iv[n];
  178. AES_encrypt(out, out, key);
  179. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  180. out[n] ^= iv2[n];
  181. iv = out;
  182. memcpy(prev, in, AES_BLOCK_SIZE);
  183. iv2 = prev;
  184. len -= AES_BLOCK_SIZE;
  185. in += AES_BLOCK_SIZE;
  186. out += AES_BLOCK_SIZE;
  187. }
  188. /* And now backwards */
  189. iv = ivec + AES_BLOCK_SIZE * 2;
  190. iv2 = ivec + AES_BLOCK_SIZE * 3;
  191. len = length;
  192. while (len >= AES_BLOCK_SIZE) {
  193. out -= AES_BLOCK_SIZE;
  194. /*
  195. * XXX: reduce copies by alternating between buffers
  196. */
  197. memcpy(tmp, out, AES_BLOCK_SIZE);
  198. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  199. out[n] ^= iv[n];
  200. /*
  201. * hexdump(stdout, "out ^ iv", out, AES_BLOCK_SIZE);
  202. */
  203. AES_encrypt(out, out, key);
  204. /*
  205. * hexdump(stdout,"enc", out, AES_BLOCK_SIZE);
  206. */
  207. /*
  208. * hexdump(stdout,"iv2", iv2, AES_BLOCK_SIZE);
  209. */
  210. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  211. out[n] ^= iv2[n];
  212. /*
  213. * hexdump(stdout,"out", out, AES_BLOCK_SIZE);
  214. */
  215. iv = out;
  216. memcpy(prev, tmp, AES_BLOCK_SIZE);
  217. iv2 = prev;
  218. len -= AES_BLOCK_SIZE;
  219. }
  220. } else {
  221. /* First backwards */
  222. iv = ivec + AES_BLOCK_SIZE * 2;
  223. iv2 = ivec + AES_BLOCK_SIZE * 3;
  224. in += length;
  225. out += length;
  226. while (len >= AES_BLOCK_SIZE) {
  227. in -= AES_BLOCK_SIZE;
  228. out -= AES_BLOCK_SIZE;
  229. memcpy(tmp, in, AES_BLOCK_SIZE);
  230. memcpy(tmp2, in, AES_BLOCK_SIZE);
  231. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  232. tmp[n] ^= iv2[n];
  233. AES_decrypt(tmp, out, key);
  234. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  235. out[n] ^= iv[n];
  236. memcpy(tmp3, tmp2, AES_BLOCK_SIZE);
  237. iv = tmp3;
  238. iv2 = out;
  239. len -= AES_BLOCK_SIZE;
  240. }
  241. /* And now forwards */
  242. iv = ivec;
  243. iv2 = ivec + AES_BLOCK_SIZE;
  244. len = length;
  245. while (len >= AES_BLOCK_SIZE) {
  246. memcpy(tmp, out, AES_BLOCK_SIZE);
  247. memcpy(tmp2, out, AES_BLOCK_SIZE);
  248. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  249. tmp[n] ^= iv2[n];
  250. AES_decrypt(tmp, out, key);
  251. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  252. out[n] ^= iv[n];
  253. memcpy(tmp3, tmp2, AES_BLOCK_SIZE);
  254. iv = tmp3;
  255. iv2 = out;
  256. len -= AES_BLOCK_SIZE;
  257. in += AES_BLOCK_SIZE;
  258. out += AES_BLOCK_SIZE;
  259. }
  260. }
  261. }