summaryrefslogtreecommitdiff
path: root/libavutil/xtea.c
diff options
context:
space:
mode:
authorMichael Niedermayer <michaelni@gmx.at>2012-09-30 17:46:37 +0200
committerMartin Storsjö <martin@martin.st>2012-10-03 19:19:49 +0300
commitb4345004319eee35e697520ca305eb95db0669e9 (patch)
tree8d0934fe7557a9204e0391cd5ba5b58665c4049f /libavutil/xtea.c
parentca074cc3134cb81c086f54b441f0bbde4d4249e7 (diff)
xtea: Fix CBC decryption when src==dst
Signed-off-by: Martin Storsjö <martin@martin.st>
Diffstat (limited to 'libavutil/xtea.c')
-rw-r--r--libavutil/xtea.c19
1 files changed, 9 insertions, 10 deletions
diff --git a/libavutil/xtea.c b/libavutil/xtea.c
index 11ae2664bb..cf63670dbe 100644
--- a/libavutil/xtea.c
+++ b/libavutil/xtea.c
@@ -36,7 +36,7 @@ void av_xtea_init(AVXTEA *ctx, const uint8_t key[16])
}
static void xtea_crypt_ecb(AVXTEA *ctx, uint8_t *dst, const uint8_t *src,
- int decrypt)
+ int decrypt, uint8_t *iv)
{
uint32_t v0, v1;
int i;
@@ -52,6 +52,11 @@ static void xtea_crypt_ecb(AVXTEA *ctx, uint8_t *dst, const uint8_t *src,
sum -= delta;
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
}
+ if (iv) {
+ v0 ^= AV_RB32(iv);
+ v1 ^= AV_RB32(iv + 4);
+ memcpy(iv, src, 8);
+ }
} else {
uint32_t sum = 0, delta = 0x9E3779B9;
@@ -73,13 +78,7 @@ void av_xtea_crypt(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, int count,
if (decrypt) {
while (count--) {
- xtea_crypt_ecb(ctx, dst, src, decrypt);
-
- if (iv) {
- for (i = 0; i < 8; i++)
- dst[i] = dst[i] ^ iv[i];
- memcpy(iv, src, 8);
- }
+ xtea_crypt_ecb(ctx, dst, src, decrypt, iv);
src += 8;
dst += 8;
@@ -89,10 +88,10 @@ void av_xtea_crypt(AVXTEA *ctx, uint8_t *dst, const uint8_t *src, int count,
if (iv) {
for (i = 0; i < 8; i++)
dst[i] = src[i] ^ iv[i];
- xtea_crypt_ecb(ctx, dst, dst, decrypt);
+ xtea_crypt_ecb(ctx, dst, dst, decrypt, NULL);
memcpy(iv, dst, 8);
} else {
- xtea_crypt_ecb(ctx, dst, src, decrypt);
+ xtea_crypt_ecb(ctx, dst, src, decrypt, NULL);
}
src += 8;
dst += 8;