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
23 #include "attributes.h"
30 #define MAX_HASHLEN 64
31 #define MAX_BLOCKLEN 128
35 int blocklen, hashlen;
36 void (*final)(void*, uint8_t*);
37 void (*update)(void*, const uint8_t*, int len);
39 uint8_t key[MAX_BLOCKLEN];
43 #define DEFINE_SHA(bits) \
44 static av_cold void sha ## bits ##_init(void *ctx) \
46 av_sha_init(ctx, bits); \
49 #define DEFINE_SHA512(bits) \
50 static av_cold void sha ## bits ##_init(void *ctx) \
52 av_sha512_init(ctx, bits); \
61 AVHMAC *av_hmac_alloc(enum AVHMACType type)
63 AVHMAC *c = av_mallocz(sizeof(*c));
70 c->init = (void*)av_md5_init;
71 c->update = (void*)av_md5_update;
72 c->final = (void*)av_md5_final;
73 c->hash = av_md5_alloc();
78 c->init = sha160_init;
79 c->update = (void*)av_sha_update;
80 c->final = (void*)av_sha_final;
81 c->hash = av_sha_alloc();
86 c->init = sha224_init;
87 c->update = (void*)av_sha_update;
88 c->final = (void*)av_sha_final;
89 c->hash = av_sha_alloc();
94 c->init = sha256_init;
95 c->update = (void*)av_sha_update;
96 c->final = (void*)av_sha_final;
97 c->hash = av_sha_alloc();
102 c->init = sha384_init;
103 c->update = (void*)av_sha512_update;
104 c->final = (void*)av_sha512_final;
105 c->hash = av_sha512_alloc();
110 c->init = sha512_init;
111 c->update = (void*)av_sha512_update;
112 c->final = (void*)av_sha512_final;
113 c->hash = av_sha512_alloc();
126 void av_hmac_free(AVHMAC *c)
134 void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
137 uint8_t block[MAX_BLOCKLEN];
138 if (keylen > c->blocklen) {
140 c->update(c->hash, key, keylen);
141 c->final(c->hash, c->key);
142 c->keylen = c->hashlen;
144 memcpy(c->key, key, keylen);
148 for (i = 0; i < c->keylen; i++)
149 block[i] = c->key[i] ^ 0x36;
150 for (i = c->keylen; i < c->blocklen; i++)
152 c->update(c->hash, block, c->blocklen);
155 void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
157 c->update(c->hash, data, len);
160 int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
162 uint8_t block[MAX_BLOCKLEN];
164 if (outlen < c->hashlen)
165 return AVERROR(EINVAL);
166 c->final(c->hash, out);
168 for (i = 0; i < c->keylen; i++)
169 block[i] = c->key[i] ^ 0x5C;
170 for (i = c->keylen; i < c->blocklen; i++)
172 c->update(c->hash, block, c->blocklen);
173 c->update(c->hash, out, c->hashlen);
174 c->final(c->hash, out);
178 int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
179 const uint8_t *key, unsigned int keylen,
180 uint8_t *out, unsigned int outlen)
182 av_hmac_init(c, key, keylen);
183 av_hmac_update(c, data, len);
184 return av_hmac_final(c, out, outlen);