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