libavutil/hmac.c
8ee288d2
 /*
  * Copyright (C) 2012 Martin Storsjo
  *
e7e0186e
  * This file is part of FFmpeg.
8ee288d2
  *
e7e0186e
  * FFmpeg is free software; you can redistribute it and/or
8ee288d2
  * 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.
  *
e7e0186e
  * FFmpeg is distributed in the hope that it will be useful,
8ee288d2
  * 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
e7e0186e
  * License along with FFmpeg; if not, write to the Free Software
8ee288d2
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include <string.h>
 
1fda184a
 #include "attributes.h"
8ee288d2
 #include "hmac.h"
 #include "md5.h"
 #include "sha.h"
82ef6701
 #include "sha512.h"
8ee288d2
 #include "mem.h"
 
82ef6701
 #define MAX_HASHLEN 64
 #define MAX_BLOCKLEN 128
8ee288d2
 
 struct AVHMAC {
     void *hash;
     int blocklen, hashlen;
     void (*final)(void*, uint8_t*);
     void (*update)(void*, const uint8_t*, int len);
     void (*init)(void*);
     uint8_t key[MAX_BLOCKLEN];
     int keylen;
 };
 
82ef6701
 #define DEFINE_SHA(bits)                           \
 static av_cold void sha ## bits ##_init(void *ctx) \
 {                                                  \
     av_sha_init(ctx, bits);                        \
 }
 
 #define DEFINE_SHA512(bits)                        \
 static av_cold void sha ## bits ##_init(void *ctx) \
 {                                                  \
     av_sha512_init(ctx, bits);                     \
8ee288d2
 }
 
82ef6701
 DEFINE_SHA(160)
 DEFINE_SHA(224)
 DEFINE_SHA(256)
 DEFINE_SHA512(384)
 DEFINE_SHA512(512)
 
8ee288d2
 AVHMAC *av_hmac_alloc(enum AVHMACType type)
 {
     AVHMAC *c = av_mallocz(sizeof(*c));
     if (!c)
         return NULL;
     switch (type) {
     case AV_HMAC_MD5:
         c->blocklen = 64;
         c->hashlen  = 16;
9c49d590
         c->init     = (void*)av_md5_init;
         c->update   = (void*)av_md5_update;
         c->final    = (void*)av_md5_final;
8ee288d2
         c->hash     = av_md5_alloc();
         break;
     case AV_HMAC_SHA1:
         c->blocklen = 64;
         c->hashlen  = 20;
82ef6701
         c->init     = sha160_init;
         c->update   = (void*)av_sha_update;
         c->final    = (void*)av_sha_final;
         c->hash     = av_sha_alloc();
         break;
     case AV_HMAC_SHA224:
         c->blocklen = 64;
         c->hashlen  = 28;
         c->init     = sha224_init;
         c->update   = (void*)av_sha_update;
         c->final    = (void*)av_sha_final;
         c->hash     = av_sha_alloc();
         break;
     case AV_HMAC_SHA256:
         c->blocklen = 64;
         c->hashlen  = 32;
         c->init     = sha256_init;
9c49d590
         c->update   = (void*)av_sha_update;
         c->final    = (void*)av_sha_final;
8ee288d2
         c->hash     = av_sha_alloc();
         break;
82ef6701
     case AV_HMAC_SHA384:
         c->blocklen = 128;
         c->hashlen  = 48;
         c->init     = sha384_init;
         c->update   = (void*)av_sha512_update;
         c->final    = (void*)av_sha512_final;
         c->hash     = av_sha512_alloc();
         break;
     case AV_HMAC_SHA512:
         c->blocklen = 128;
         c->hashlen  = 64;
         c->init     = sha512_init;
         c->update   = (void*)av_sha512_update;
         c->final    = (void*)av_sha512_final;
         c->hash     = av_sha512_alloc();
         break;
8ee288d2
     default:
         av_free(c);
         return NULL;
     }
     if (!c->hash) {
         av_free(c);
         return NULL;
     }
     return c;
 }
 
 void av_hmac_free(AVHMAC *c)
 {
     if (!c)
         return;
05e74ac2
     av_freep(&c->hash);
8ee288d2
     av_free(c);
 }
 
 void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
 {
     int i;
     uint8_t block[MAX_BLOCKLEN];
     if (keylen > c->blocklen) {
         c->init(c->hash);
         c->update(c->hash, key, keylen);
         c->final(c->hash, c->key);
         c->keylen = c->hashlen;
     } else {
         memcpy(c->key, key, keylen);
         c->keylen = keylen;
     }
     c->init(c->hash);
     for (i = 0; i < c->keylen; i++)
         block[i] = c->key[i] ^ 0x36;
     for (i = c->keylen; i < c->blocklen; i++)
         block[i] = 0x36;
     c->update(c->hash, block, c->blocklen);
 }
 
 void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
 {
     c->update(c->hash, data, len);
 }
 
 int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
 {
     uint8_t block[MAX_BLOCKLEN];
     int i;
     if (outlen < c->hashlen)
         return AVERROR(EINVAL);
     c->final(c->hash, out);
     c->init(c->hash);
     for (i = 0; i < c->keylen; i++)
         block[i] = c->key[i] ^ 0x5C;
     for (i = c->keylen; i < c->blocklen; i++)
         block[i] = 0x5C;
     c->update(c->hash, block, c->blocklen);
     c->update(c->hash, out, c->hashlen);
     c->final(c->hash, out);
     return c->hashlen;
 }
 
 int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
                  const uint8_t *key, unsigned int keylen,
                  uint8_t *out, unsigned int outlen)
 {
     av_hmac_init(c, key, keylen);
     av_hmac_update(c, data, len);
     return av_hmac_final(c, out, outlen);
 }