On Sun, Jul 01, 2012 at 12:45:35PM +0200, Samuel Pitoiset wrote:
> This encryption will be needed for implementing RTMPE type 8.
> I still have to write a test.
> ---
> libavutil/Makefile | 3 ++
> libavutil/avutil.h | 2 +-
> libavutil/xtea.c | 68
> ++++++++++++++++++++++++++++++++++++++++++++++
> libavutil/xtea.h | 44 +++++++++++++++++++++++++++++
> tests/fate/libavutil.mak | 4 +++
> 5 files changed, 120 insertions(+), 1 deletions(-)
> create mode 100644 libavutil/xtea.c
> create mode 100644 libavutil/xtea.h
>
> diff --git a/libavutil/Makefile b/libavutil/Makefile
> index 6fe174b..aac18ad 100644
> --- a/libavutil/Makefile
> +++ b/libavutil/Makefile
> @@ -37,6 +37,7 @@ HEADERS = adler32.h
> \
> samplefmt.h \
> sha.h \
> time.h \
> + xtea.h \
>
> ARCH_HEADERS = bswap.h \
> intmath.h \
> @@ -81,6 +82,7 @@ OBJS = adler32.o
> \
> time.o \
> tree.o \
> utils.o \
> + xtea.o \
>
> TESTPROGS = adler32 \
> aes \
> @@ -98,3 +100,4 @@ TESTPROGS = adler32
> \
> parseutils \
> sha \
> tree \
> + xtea \
> diff --git a/libavutil/avutil.h b/libavutil/avutil.h
> index 351e827..2aae161 100644
> --- a/libavutil/avutil.h
> +++ b/libavutil/avutil.h
> @@ -152,7 +152,7 @@
> */
>
> #define LIBAVUTIL_VERSION_MAJOR 51
> -#define LIBAVUTIL_VERSION_MINOR 34
> +#define LIBAVUTIL_VERSION_MINOR 0
> #define LIBAVUTIL_VERSION_MICRO 0
>
> #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
> diff --git a/libavutil/xtea.c b/libavutil/xtea.c
> new file mode 100644
> index 0000000..356c865
> --- /dev/null
> +++ b/libavutil/xtea.c
> @@ -0,0 +1,68 @@
> +/*
> + * XTEA algorithm
> + * Copyright (c) 2012 Samuel Pitoiset
> + *
> + * loosely based on the implementation of David Wheeler and Roger Needham
> + *
> + * This file is part of Libav.
> + *
> + * Libav is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * Libav is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with Libav; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
> USA
> + */
> +
> +#include "avutil.h"
> +#include "common.h"
> +#include "xtea.h"
> +
> +void av_xtea_encrypt(uint32_t num_rounds, uint32_t *data, uint32_t *key)
> +{
> + uint32_t v0 = data[0], v1 = data[1], i;
> + uint32_t sum = 0, delta = 0x9E3779B9;
> +
> + for (i = 0; i < num_rounds; i++) {
> + v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + key[sum & 3]);
> + sum += delta;
> + v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + key[sum >> 11 & 3]);
I'd rather add some parentheses, now it's a bit hard to understand what it
does here. And the notion about accepting bytes instead of uint32_t's
applies here as well.
> + }
> +
> + data[0] = v0;
> + data[1] = v1;
> +}
> +
> +void av_xtea_decrypt(uint32_t num_rounds, uint32_t *data, uint32_t *key)
> +{
> + uint32_t v0 = data[0], v1 = data[1], i;
> + uint32_t delta = 0x9E3779B9, sum = delta * num_rounds;
> +
> + for (i = 0; i < num_rounds; i++) {
> + v1 -= ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + key[sum >> 11 & 3]);
> + sum -= delta;
> + v0 -= ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + key[sum & 3]);
> + }
> +
> + data[0] = v0;
> + data[1] = v1;
> +}
> +
> +#ifdef TEST
> +#include <stdio.h>
> +#undef printf
> +
> +int main(void)
> +{
> + // TODO: Write tests.
> + return 0;
> +}
> +
> +#endif
> diff --git a/libavutil/xtea.h b/libavutil/xtea.h
> new file mode 100644
> index 0000000..30da631
> --- /dev/null
> +++ b/libavutil/xtea.h
> @@ -0,0 +1,44 @@
> +/*
> + * XTEA algorithm
> + *
> + * This file is part of Libav.
> + *
> + * Libav is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * Libav is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with Libav; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
> USA
> + */
> +
> +#ifndef AVUTIL_XTEA_H
> +#define AVUTIL_XTEA_H
> +
> +#include <stdint.h>
> +
> +/**
> + * @brief Encrypts using the XTEA algorithm.
You fail to mention that it encrypts only eight bytes. It should be more
generic or you should explain why two uint32_t's are enough for everyone.
> + *
> + * @param num_rounds number of rounds
> + * @param data data to encrypt
> + * @param key a key
> + */
> +void av_xtea_encrypt(uint32_t num_rounds, uint32_t *data, uint32_t *key);
> +
> +/**
> + * @brief Decrypts using the XTEA algorithm.
> + *
> + * @param num_rounds number of rounds
> + * @param data data to decrypt
> + * @param key a key
> + */
> +void av_xtea_decrypt(uint32_t num_rounds, uint32_t *data, uint32_t *key);
> +
> +#endif /* AVUTIL_XTEA_H */
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel