[FFmpeg-devel] [PATCH] SHA-256 support

Michael Niedermayer michaelni
Wed Jun 10 19:41:59 CEST 2009


On Wed, Jun 10, 2009 at 06:11:49PM +0300, Kostya wrote:
> On Wed, Jun 10, 2009 at 11:56:37AM +0200, Michael Niedermayer wrote:
> > On Wed, Jun 10, 2009 at 08:35:39AM +0300, Kostya wrote:
> > > On Tue, Jun 09, 2009 at 09:16:10PM +0200, Michael Niedermayer wrote:
> > > > On Tue, Jun 09, 2009 at 09:32:24PM +0300, Kostya wrote:
> > > > > On Tue, Jun 09, 2009 at 04:47:09PM +0200, Michael Niedermayer wrote:
> > > > > > On Tue, Jun 09, 2009 at 08:13:40AM +0300, Kostya wrote:
> > > > > [...]
> > > > > > > /* Hash a single 512-bit block. This is the core of the algorithm. */
> > > > > > > 
> > > > > > > static void transform(uint32_t state[5], const uint8_t buffer[64])
> > > > > > 
> > > > > > doxy?
> > > > >  
> > > > > no
> > > > 
> > > > make it a yes please
> > >  
> > > you know, in this phrase punctiation can radically alter its meaning
> > 
> > please read and follow the patch checklist, developer guide/policy as well
> > as running our never tired patcheck script over your patches.
> > Also please document functions and fields in a doxygen compatible way.
>  
> I did
> BTW, looks like your patcheck behaviour is rather strange on non-GNU
> systems. At least running it on my MacOS X results in strange hangups
> and grep errors.

bugreport is welcome but i must admit that i made no attempt to make it
portable or "posix only"


[...]
>  Makefile |    4 
>  avutil.h |    2 
>  sha2.c   |  267 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  sha2.h   |   55 ++++++++++++
>  4 files changed, 326 insertions(+), 2 deletions(-)
> 38f68ed88e9c34221b476b3570a49a1d579eda03  sha2.patch
> Index: libavutil/sha2.h
> ===================================================================
> --- libavutil/sha2.h	(revision 0)
> +++ libavutil/sha2.h	(revision 0)
> @@ -0,0 +1,55 @@
> +/*
> + * Copyright (C) 2009 Konstantin Shishkov
> + *
> + * 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
> + */
> +
> +#ifndef AVUTIL_SHA2_H
> +#define AVUTIL_SHA2_H
> +
> +#include <stdint.h>
> +
> +extern const int av_sha256_size;
> +
> +struct AVSHA256;
> +
> +/**
> + * initialize SHA-224/256 hashing
> + *
> + * @param context    hash function context
> + * @param use_sha224 selects mode of operation - zero for SHA-256, SHA-224 in all other cases
> + */
> +void av_sha256_init(struct AVSHA256* ctx, int use_sha224);

please pass the number of bits as argument not some funny flag that noone
will remember without checking the doxy
also please document from where ctx comes from, the idea is that the
user can use the functions without having to read the code but just the
doxy


[...]
> +void av_sha256_update(AVSHA256* ctx, const uint8_t* data, unsigned int len)
> +{
> +    unsigned int i, j;
> +
> +    j = ctx->count & 63;
> +    ctx->count += len;
> +#if CONFIG_SMALL
> +    for (i = 0; i < len; i++) {
> +        ctx->buffer[j++] = data[i];
> +        if (j == 64) {
> +            transform(ctx->state, ctx->buffer);
> +            j = 0;
> +        }
> +    }
> +#else
> +    if ((j + len) > 63) {
> +        memcpy(&ctx->buffer[j], data, (i = 64-j));
> +        transform(ctx->state, ctx->buffer);
> +        for (; i + 63 < len; i += 64) {
> +            transform(ctx->state, &data[i]);
> +        }
> +        j=0;
> +    }
> +    else i = 0;
> +    memcpy(&ctx->buffer[j], &data[i], len - i);
> +#endif
> +}
> +
> +void av_sha256_final(AVSHA256* ctx, uint8_t *digest)
> +{
> +    int i;
> +    uint64_t finalcount = be2me_64(ctx->count<<3);
> +
> +    av_sha256_update(ctx, "\200", 1);
> +    while ((ctx->count & 63) != 56)
> +        av_sha256_update(ctx, "", 1);
> +    av_sha256_update(ctx, (uint8_t *)&finalcount, 8); /* Should cause a transform() */
> +    for (i = 0; i < 8 - ctx->is_sha224; i++)
> +        ((uint32_t*)digest)[i] = be2me_32(ctx->state[i]);
> +}

these functions look very similar to sha1
cant they be shared?

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Opposition brings concord. Out of discord comes the fairest harmony.
-- Heraclitus
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20090610/5b6a1620/attachment.pgp>



More information about the ffmpeg-devel mailing list