2 * Copyright (C) 2012 Martin Storsjo
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "attributes.h"
33 #define MAX_HASHLEN 64
34 #define MAX_BLOCKLEN 128
36 typedef void (*hmac_final)(void *ctx, uint8_t *dst);
37 #if FF_API_CRYPTO_SIZE_T
38 typedef void (*hmac_update)(void *ctx, const uint8_t *src, int len);
40 typedef void (*hmac_update)(void *ctx, const uint8_t *src, size_t len);
42 typedef void (*hmac_init)(void *ctx);
46 int blocklen, hashlen;
50 uint8_t key[MAX_BLOCKLEN];
54 #define DEFINE_SHA(bits) \
55 static av_cold void sha ## bits ##_init(void *ctx) \
57 av_sha_init(ctx, bits); \
60 #define DEFINE_SHA512(bits) \
61 static av_cold void sha ## bits ##_init(void *ctx) \
63 av_sha512_init(ctx, bits); \
72 AVHMAC *av_hmac_alloc(enum AVHMACType type)
74 AVHMAC *c = av_mallocz(sizeof(*c));
81 c->init = (hmac_init) av_md5_init;
82 c->update = (hmac_update) av_md5_update;
83 c->final = (hmac_final) av_md5_final;
84 c->hash = av_md5_alloc();
89 c->init = sha160_init;
90 c->update = (hmac_update) av_sha_update;
91 c->final = (hmac_final) av_sha_final;
92 c->hash = av_sha_alloc();
97 c->init = sha224_init;
98 c->update = (hmac_update) av_sha_update;
99 c->final = (hmac_final) av_sha_final;
100 c->hash = av_sha_alloc();
105 c->init = sha256_init;
106 c->update = (hmac_update) av_sha_update;
107 c->final = (hmac_final) av_sha_final;
108 c->hash = av_sha_alloc();
113 c->init = sha384_init;
114 c->update = (hmac_update) av_sha512_update;
115 c->final = (hmac_final) av_sha512_final;
116 c->hash = av_sha512_alloc();
121 c->init = sha512_init;
122 c->update = (hmac_update) av_sha512_update;
123 c->final = (hmac_final) av_sha512_final;
124 c->hash = av_sha512_alloc();
137 void av_hmac_free(AVHMAC *c)
145 void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
148 uint8_t block[MAX_BLOCKLEN];
149 if (keylen > c->blocklen) {
151 c->update(c->hash, key, keylen);
152 c->final(c->hash, c->key);
153 c->keylen = c->hashlen;
155 memcpy(c->key, key, keylen);
159 for (i = 0; i < c->keylen; i++)
160 block[i] = c->key[i] ^ 0x36;
161 for (i = c->keylen; i < c->blocklen; i++)
163 c->update(c->hash, block, c->blocklen);
166 void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
168 c->update(c->hash, data, len);
171 int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
173 uint8_t block[MAX_BLOCKLEN];
175 if (outlen < c->hashlen)
176 return AVERROR(EINVAL);
177 c->final(c->hash, out);
179 for (i = 0; i < c->keylen; i++)
180 block[i] = c->key[i] ^ 0x5C;
181 for (i = c->keylen; i < c->blocklen; i++)
183 c->update(c->hash, block, c->blocklen);
184 c->update(c->hash, out, c->hashlen);
185 c->final(c->hash, out);
189 int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
190 const uint8_t *key, unsigned int keylen,
191 uint8_t *out, unsigned int outlen)
193 av_hmac_init(c, key, keylen);
194 av_hmac_update(c, data, len);
195 return av_hmac_final(c, out, outlen);