[FFmpeg-devel] [PATCH] lavu: Add RIPEMD hashing
Michael Niedermayer
michaelni at gmx.at
Sat Jun 15 11:21:50 CEST 2013
On Thu, Jun 06, 2013 at 06:46:05PM -0300, James Almer wrote:
> Includes RIPEMD-128, RIPEMD-160, RIPEMD-256 and RIPEMD-320
>
> Signed-off-by: James Almer <jamrial at gmail.com>
> ---
> doc/APIchanges | 7 +
> libavutil/Makefile | 3 +
> libavutil/ripemd.c | 437 +++++++++++++++++++++++++++++++++++++++++++++++
> libavutil/ripemd.h | 75 ++++++++
> libavutil/version.h | 2 +-
> tests/fate/libavutil.mak | 4 +
> tests/ref/fate/ripemd | 28 +++
> 7 files changed, 555 insertions(+), 1 deletion(-)
> create mode 100644 libavutil/ripemd.c
> create mode 100644 libavutil/ripemd.h
> create mode 100644 tests/ref/fate/ripemd
>
> diff --git a/doc/APIchanges b/doc/APIchanges
> index 1ab17c2..ffdb8a2 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -16,6 +16,13 @@ libavutil: 2012-10-22
> API changes, most recent first:
>
>
> +2013-06-xx - xxxxxxx - lavu 52.36.100
> + Add AVRIPEMD:
> + av_ripemd_alloc()
> + av_ripemd_init()
> + av_ripemd_update()
> + av_ripemd_final()
> +
> 2013-06-05 - fc962d4 - lavu 52.13.0 - mem.h
> Add av_realloc_array and av_reallocp_array
>
> diff --git a/libavutil/Makefile b/libavutil/Makefile
> index f1ff9c6..21746f0 100644
> --- a/libavutil/Makefile
> +++ b/libavutil/Makefile
> @@ -43,6 +43,7 @@ HEADERS = adler32.h \
> pixfmt.h \
> random_seed.h \
> rational.h \
> + ripemd.h \
> samplefmt.h \
> sha.h \
> sha512.h \
> @@ -102,6 +103,7 @@ OBJS = adler32.o \
> random_seed.o \
> rational.o \
> rc4.o \
> + ripemd.o \
> samplefmt.o \
> sha.o \
> sha512.o \
> @@ -148,6 +150,7 @@ TESTPROGS = adler32 \
> parseutils \
> random_seed \
> rational \
> + ripemd \
> sha \
> sha512 \
> tree \
> diff --git a/libavutil/ripemd.c b/libavutil/ripemd.c
> new file mode 100644
> index 0000000..f73073c
> --- /dev/null
> +++ b/libavutil/ripemd.c
> @@ -0,0 +1,437 @@
> +/*
> + * Copyright (C) 2007 Michael Niedermayer <michaelni at gmx.at>
> + * Copyright (C) 2013 James Almer
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg 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.
> + *
> + * FFmpeg 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 FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include <string.h>
> +
> +#include "attributes.h"
> +#include "avutil.h"
> +#include "bswap.h"
> +#include "intreadwrite.h"
> +#include "ripemd.h"
> +#include "mem.h"
> +
> +/** hash context */
> +typedef struct AVRIPEMD {
> + uint8_t digest_len; ///< digest length in 32-bit words
> + uint64_t count; ///< number of bytes in buffer
> + uint8_t buffer[64]; ///< 512-bit buffer of input values used in hash updating
> + uint32_t state[10]; ///< current hash value
> + uint8_t ext; ///< extension (0 for 128 and 160, 1 for 256 and 320)
> + /** function used to update hash for 512-bit input block */
> + void (*transform)(uint32_t *state, const uint8_t buffer[64], int ext);
> +} AVRIPEMD;
> +
> +const int av_ripemd_size = sizeof(AVRIPEMD);
> +
> +struct AVRIPEMD *av_ripemd_alloc(void)
> +{
> + return av_mallocz(sizeof(struct AVRIPEMD));
> +}
> +
> +static const uint32_t KA[4] = {
> + 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e
> +};
> +
> +static const uint32_t KB[4] = {
> + 0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9
> +};
> +
> +static const int ROTA[80] = {
> + 11, 14, 15, 12, 5, 8, 7 , 9, 11, 13, 14, 15, 6, 7, 9, 8,
> + 7 , 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
> + 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
> + 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
> + 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
> +};
> +
> +static const int ROTB[80] = {
> + 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
> + 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
> + 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
> + 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
> + 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
> +};
> +
> +static const int WA[80] = {
> + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
> + 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
> + 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
> + 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
> + 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
> +};
> +
> +static const int WB[80] = {
> + 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
> + 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
> + 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
> + 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
> + 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
> +};
> +
> +static uint32_t rol(uint32_t value, uint32_t bits) {
> + return (value << bits) | (value >> (32 - bits));
> +}
should be always_inline as its just 1 instruction on many architectures
also, if you plan to maintain this in the future you should add
yourself to MAINTAINERS for this
[...]
> +#define ROUND160_0_TO_15(a,b,c,d,e,f,g,h,i,j) \
> + a = rol(a + (( b ^ c ^ d) + block[WA[n]]), ROTA[n]) + e; \
> + f = rol(f + (((~i | h) ^ g) + block[WB[n]] + KB[0]), ROTB[n]) + j; \
> + ROTATE(c,h)
> +
> +#define ROUND160_16_TO_31(a,b,c,d,e,f,g,h,i,j) \
> + a = rol(a + ((((c ^ d) & b) ^ d) + block[WA[n]] + KA[0]), ROTA[n]) + e; \
> + f = rol(f + ((((g ^ h) & i) ^ h) + block[WB[n]] + KB[1]), ROTB[n]) + j; \
> + ROTATE(c,h)
> +
> +#define ROUND160_32_TO_47(a,b,c,d,e,f,g,h,i,j) \
> + a = rol(a + (((~c | b) ^ d) + block[WA[n]] + KA[1]), ROTA[n]) + e; \
> + f = rol(f + (((~h | g) ^ i) + block[WB[n]] + KB[2]), ROTB[n]) + j; \
> + ROTATE(c,h)
> +
> +#define ROUND160_48_TO_63(a,b,c,d,e,f,g,h,i,j) \
> + a = rol(a + ((((b ^ c) & d) ^ c) + block[WA[n]] + KA[2]), ROTA[n]) + e; \
> + f = rol(f + ((((h ^ i) & g) ^ i) + block[WB[n]] + KB[3]), ROTB[n]) + j; \
> + ROTATE(c,h)
> +
> +#define ROUND160_64_TO_79(a,b,c,d,e,f,g,h,i,j) \
> + a = rol(a + (((~d | c) ^ b) + block[WA[n]] + KA[3]), ROTA[n]) + e; \
> + f = rol(f + (( g ^ h ^ i) + block[WB[n]]), ROTB[n]) + j; \
> + ROTATE(c,h)
> +
> +static void ripemd160_transform(uint32_t *state, const uint8_t buffer[64], int ext)
> +{
> + uint32_t a, b, c, d, e, f, g, h, i, j;
> + uint32_t block[16];
> + int n;
> +
> + if (ext) {
> + a = state[0]; b = state[1]; c = state[2]; d = state[3]; e = state[4];
> + f = state[5]; g = state[6]; h = state[7]; i = state[8]; j = state[9];
> + } else {
> + a = f = state[0];
> + b = g = state[1];
> + c = h = state[2];
> + d = i = state[3];
> + e = j = state[4];
> + }
> +
> + for (n = 0; n < 16; n++)
> + block[n] = AV_RL32(buffer + 4 * n);
> +
> + for (n = 0; n < 16 - 4;) {
> + ROUND160_0_TO_15(a,b,c,d,e,f,g,h,i,j);
> + ROUND160_0_TO_15(e,a,b,c,d,j,f,g,h,i);
> + ROUND160_0_TO_15(d,e,a,b,c,i,j,f,g,h);
> + ROUND160_0_TO_15(c,d,e,a,b,h,i,j,f,g);
> + ROUND160_0_TO_15(b,c,d,e,a,g,h,i,j,f);
> + }
> + ROUND160_0_TO_15(a,b,c,d,e,f,g,h,i,j);
> + SWAP(a,f)
> +
> + for (; n < 32 - 4;) {
> + ROUND160_16_TO_31(e,a,b,c,d,j,f,g,h,i);
> + ROUND160_16_TO_31(d,e,a,b,c,i,j,f,g,h);
> + ROUND160_16_TO_31(c,d,e,a,b,h,i,j,f,g);
> + ROUND160_16_TO_31(b,c,d,e,a,g,h,i,j,f);
> + ROUND160_16_TO_31(a,b,c,d,e,f,g,h,i,j);
> + }
> + ROUND160_16_TO_31(e,a,b,c,d,j,f,g,h,i);
> + SWAP(b,g)
> +
> + for (; n < 48 - 4;) {
> + ROUND160_32_TO_47(d,e,a,b,c,i,j,f,g,h);
> + ROUND160_32_TO_47(c,d,e,a,b,h,i,j,f,g);
> + ROUND160_32_TO_47(b,c,d,e,a,g,h,i,j,f);
> + ROUND160_32_TO_47(a,b,c,d,e,f,g,h,i,j);
> + ROUND160_32_TO_47(e,a,b,c,d,j,f,g,h,i);
> + }
> + ROUND160_32_TO_47(d,e,a,b,c,i,j,f,g,h);
> + SWAP(c,h)
> +
> + for (; n < 64 - 4;) {
> + ROUND160_48_TO_63(c,d,e,a,b,h,i,j,f,g);
> + ROUND160_48_TO_63(b,c,d,e,a,g,h,i,j,f);
> + ROUND160_48_TO_63(a,b,c,d,e,f,g,h,i,j);
> + ROUND160_48_TO_63(e,a,b,c,d,j,f,g,h,i);
> + ROUND160_48_TO_63(d,e,a,b,c,i,j,f,g,h);
> + }
> + ROUND160_48_TO_63(c,d,e,a,b,h,i,j,f,g);
> + SWAP(d,i)
> +
> + for (; n < 80 - 4;) {
> + ROUND160_64_TO_79(b,c,d,e,a,g,h,i,j,f);
> + ROUND160_64_TO_79(a,b,c,d,e,f,g,h,i,j);
> + ROUND160_64_TO_79(e,a,b,c,d,j,f,g,h,i);
> + ROUND160_64_TO_79(d,e,a,b,c,i,j,f,g,h);
> + ROUND160_64_TO_79(c,d,e,a,b,h,i,j,f,g);
> + }
It would be interresting to see what speed & size effect completely
unrolling these loops has.
but thats probably more for the future / after this patch is applied
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
I know you won't believe me, but the highest form of Human Excellence is
to question oneself and others. -- Socrates
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20130615/a036e047/attachment.asc>
More information about the ffmpeg-devel
mailing list