2
0

aes_ige.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. #if OPENSSL_API_3
  11. NON_EMPTY_TRANSLATION_UNIT
  12. #else
  13. #include <openssl/aes.h>
  14. #include "aes_locl.h"
  15. #define N_WORDS (AES_BLOCK_SIZE / sizeof(unsigned long))
  16. typedef struct {
  17. unsigned long data[N_WORDS];
  18. } aes_block_t;
  19. /* XXX: probably some better way to do this */
  20. #if defined(__i386__) || defined(__x86_64__)
  21. # define UNALIGNED_MEMOPS_ARE_FAST 1
  22. #else
  23. # define UNALIGNED_MEMOPS_ARE_FAST 0
  24. #endif
  25. #if UNALIGNED_MEMOPS_ARE_FAST
  26. # define load_block(d, s) (d) = *(const aes_block_t *)(s)
  27. # define store_block(d, s) *(aes_block_t *)(d) = (s)
  28. #else
  29. # define load_block(d, s) memcpy((d).data, (s), AES_BLOCK_SIZE)
  30. # define store_block(d, s) memcpy((d), (s).data, AES_BLOCK_SIZE)
  31. #endif
  32. /* N.B. The IV for this mode is _twice_ the block size */
  33. /* Use of this function is deprecated. */
  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. * NB: The implementation of AES_bi_ige_encrypt has a bug. It is supposed to use
  151. * 2 AES keys, but in fact only one is ever used. This bug has been present
  152. * since this code was first implemented. It is believed to have minimal
  153. * security impact in practice and has therefore not been fixed for backwards
  154. * compatibility reasons.
  155. *
  156. * Use of this function is deprecated.
  157. */
  158. /* N.B. The IV for this mode is _four times_ the block size */
  159. void AES_bi_ige_encrypt(const unsigned char *in, unsigned char *out,
  160. size_t length, const AES_KEY *key,
  161. const AES_KEY *key2, const unsigned char *ivec,
  162. const int enc)
  163. {
  164. size_t n;
  165. size_t len = length;
  166. unsigned char tmp[AES_BLOCK_SIZE];
  167. unsigned char tmp2[AES_BLOCK_SIZE];
  168. unsigned char tmp3[AES_BLOCK_SIZE];
  169. unsigned char prev[AES_BLOCK_SIZE];
  170. const unsigned char *iv;
  171. const unsigned char *iv2;
  172. OPENSSL_assert(in && out && key && ivec);
  173. OPENSSL_assert((AES_ENCRYPT == enc) || (AES_DECRYPT == enc));
  174. OPENSSL_assert((length % AES_BLOCK_SIZE) == 0);
  175. if (AES_ENCRYPT == enc) {
  176. /*
  177. * XXX: Do a separate case for when in != out (strictly should check
  178. * for overlap, too)
  179. */
  180. /* First the forward pass */
  181. iv = ivec;
  182. iv2 = ivec + AES_BLOCK_SIZE;
  183. while (len >= AES_BLOCK_SIZE) {
  184. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  185. out[n] = in[n] ^ iv[n];
  186. AES_encrypt(out, out, key);
  187. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  188. out[n] ^= iv2[n];
  189. iv = out;
  190. memcpy(prev, in, AES_BLOCK_SIZE);
  191. iv2 = prev;
  192. len -= AES_BLOCK_SIZE;
  193. in += AES_BLOCK_SIZE;
  194. out += AES_BLOCK_SIZE;
  195. }
  196. /* And now backwards */
  197. iv = ivec + AES_BLOCK_SIZE * 2;
  198. iv2 = ivec + AES_BLOCK_SIZE * 3;
  199. len = length;
  200. while (len >= AES_BLOCK_SIZE) {
  201. out -= AES_BLOCK_SIZE;
  202. /*
  203. * XXX: reduce copies by alternating between buffers
  204. */
  205. memcpy(tmp, out, AES_BLOCK_SIZE);
  206. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  207. out[n] ^= iv[n];
  208. /*
  209. * hexdump(stdout, "out ^ iv", out, AES_BLOCK_SIZE);
  210. */
  211. AES_encrypt(out, out, key);
  212. /*
  213. * hexdump(stdout,"enc", out, AES_BLOCK_SIZE);
  214. */
  215. /*
  216. * hexdump(stdout,"iv2", iv2, AES_BLOCK_SIZE);
  217. */
  218. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  219. out[n] ^= iv2[n];
  220. /*
  221. * hexdump(stdout,"out", out, AES_BLOCK_SIZE);
  222. */
  223. iv = out;
  224. memcpy(prev, tmp, AES_BLOCK_SIZE);
  225. iv2 = prev;
  226. len -= AES_BLOCK_SIZE;
  227. }
  228. } else {
  229. /* First backwards */
  230. iv = ivec + AES_BLOCK_SIZE * 2;
  231. iv2 = ivec + AES_BLOCK_SIZE * 3;
  232. in += length;
  233. out += length;
  234. while (len >= AES_BLOCK_SIZE) {
  235. in -= AES_BLOCK_SIZE;
  236. out -= AES_BLOCK_SIZE;
  237. memcpy(tmp, in, AES_BLOCK_SIZE);
  238. memcpy(tmp2, in, AES_BLOCK_SIZE);
  239. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  240. tmp[n] ^= iv2[n];
  241. AES_decrypt(tmp, out, key);
  242. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  243. out[n] ^= iv[n];
  244. memcpy(tmp3, tmp2, AES_BLOCK_SIZE);
  245. iv = tmp3;
  246. iv2 = out;
  247. len -= AES_BLOCK_SIZE;
  248. }
  249. /* And now forwards */
  250. iv = ivec;
  251. iv2 = ivec + AES_BLOCK_SIZE;
  252. len = length;
  253. while (len >= AES_BLOCK_SIZE) {
  254. memcpy(tmp, out, AES_BLOCK_SIZE);
  255. memcpy(tmp2, out, AES_BLOCK_SIZE);
  256. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  257. tmp[n] ^= iv2[n];
  258. AES_decrypt(tmp, out, key);
  259. for (n = 0; n < AES_BLOCK_SIZE; ++n)
  260. out[n] ^= iv[n];
  261. memcpy(tmp3, tmp2, AES_BLOCK_SIZE);
  262. iv = tmp3;
  263. iv2 = out;
  264. len -= AES_BLOCK_SIZE;
  265. in += AES_BLOCK_SIZE;
  266. out += AES_BLOCK_SIZE;
  267. }
  268. }
  269. }
  270. #endif