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

Kostya kostya.shishkov
Thu Jun 11 07:10:03 CEST 2009


On Wed, Jun 10, 2009 at 07:41:59PM +0200, Michael Niedermayer wrote:
> On Wed, Jun 10, 2009 at 06:11:49PM +0300, Kostya wrote:
[...]
> > +/**
> > + * 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?

In essence, there are such differences from SHA-1:
* different digest size
* different initialization data
* different transform
* different digest size

Maybe I should rename existing files into sha.[ch] and extend it
instead:

/**
 * Initializes SHA hashing.
 *
 * @param context pointer to function context (allocate av_sha1_size
 * bytes of memory and pass here)
 * @param bits    number of bits in digest (SHA-1 - 160 bits, SHA-2 224
 * or 256 bits; 384 or 512 bits may be supported in the future)
 * @return 0 if initialization succeeded, -1 otherwise
 */

int av_sha_init(AVSHA* context, int bits) 
 
> [...]
> -- 
> Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB



More information about the ffmpeg-devel mailing list