X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=crypto%2Faes%2Faes_cbc.c;h=d2ba6bcdb4659d1ed9c6a01a898b08e167e0f2ef;hb=71fc9b37aedb0ee7e664fba53696a11d0b879dd7;hp=964a9780e4db459d5a97b21186b5096d1f89dd63;hpb=6f9079fd505c28961049a732e33e662530bdad21;p=oweals%2Fopenssl.git diff --git a/crypto/aes/aes_cbc.c b/crypto/aes/aes_cbc.c index 964a9780e4..d2ba6bcdb4 100644 --- a/crypto/aes/aes_cbc.c +++ b/crypto/aes/aes_cbc.c @@ -1,6 +1,6 @@ /* crypto/aes/aes_cbc.c -*- mode:C; c-file-style: "eay" -*- */ /* ==================================================================== - * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. + * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -49,7 +49,13 @@ * */ +#ifndef AES_DEBUG +# ifndef NDEBUG +# define NDEBUG +# endif +#endif #include + #include #include "aes_locl.h" @@ -57,33 +63,69 @@ void AES_cbc_encrypt(const unsigned char *in, unsigned char *out, const unsigned long length, const AES_KEY *key, unsigned char *ivec, const int enc) { - int n; + unsigned long n; unsigned long len = length; - unsigned char tmp[16]; + unsigned char tmp[AES_BLOCK_SIZE]; + const unsigned char *iv = ivec; assert(in && out && key && ivec); - assert(length % AES_BLOCK_SIZE == 0); assert((AES_ENCRYPT == enc)||(AES_DECRYPT == enc)); - if (AES_ENCRYPT == enc) - while (len > 0) { - for(n=0; n < 16; ++n) - tmp[n] = in[n] ^ ivec[n]; - AES_encrypt(tmp, out, key); - memcpy(ivec, out, 16); - len -= 16; - in += 16; - out += 16; + if (AES_ENCRYPT == enc) { + while (len >= AES_BLOCK_SIZE) { + for(n=0; n < AES_BLOCK_SIZE; ++n) + out[n] = in[n] ^ iv[n]; + AES_encrypt(out, out, key); + iv = out; + len -= AES_BLOCK_SIZE; + in += AES_BLOCK_SIZE; + out += AES_BLOCK_SIZE; + } + if (len) { + for(n=0; n < len; ++n) + out[n] = in[n] ^ iv[n]; + for(n=len; n < AES_BLOCK_SIZE; ++n) + out[n] = iv[n]; + AES_encrypt(out, out, key); + iv = out; } - else - while (len > 0) { - memcpy(tmp, in, 16); + memcpy(ivec,iv,AES_BLOCK_SIZE); + } else if (in != out) { + while (len >= AES_BLOCK_SIZE) { AES_decrypt(in, out, key); - for(n=0; n < 16; ++n) + for(n=0; n < AES_BLOCK_SIZE; ++n) + out[n] ^= iv[n]; + iv = in; + len -= AES_BLOCK_SIZE; + in += AES_BLOCK_SIZE; + out += AES_BLOCK_SIZE; + } + if (len) { + AES_decrypt(in,tmp,key); + for(n=0; n < len; ++n) + out[n] = tmp[n] ^ iv[n]; + iv = in; + } + memcpy(ivec,iv,AES_BLOCK_SIZE); + } else { + while (len >= AES_BLOCK_SIZE) { + memcpy(tmp, in, AES_BLOCK_SIZE); + AES_decrypt(in, out, key); + for(n=0; n < AES_BLOCK_SIZE; ++n) + out[n] ^= ivec[n]; + memcpy(ivec, tmp, AES_BLOCK_SIZE); + len -= AES_BLOCK_SIZE; + in += AES_BLOCK_SIZE; + out += AES_BLOCK_SIZE; + } + if (len) { + memcpy(tmp, in, AES_BLOCK_SIZE); + AES_decrypt(tmp, out, key); + for(n=0; n < len; ++n) out[n] ^= ivec[n]; - memcpy(ivec, tmp, 16); - len -= 16; - in += 16; - out += 16; + for(n=len; n < AES_BLOCK_SIZE; ++n) + out[n] = tmp[n]; + memcpy(ivec, tmp, AES_BLOCK_SIZE); } + } }