]> git.sesse.net Git - ffmpeg/blob - libavutil/hmac.c
avcodec/smacker: Check init_get_bits8() for failure
[ffmpeg] / libavutil / hmac.c
1 /*
2  * Copyright (C) 2012 Martin Storsjo
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <string.h>
22
23 #include "attributes.h"
24 #include "hmac.h"
25 #include "md5.h"
26 #include "sha.h"
27 #include "sha512.h"
28 #include "mem.h"
29
30 #define MAX_HASHLEN 64
31 #define MAX_BLOCKLEN 128
32
33 struct AVHMAC {
34     void *hash;
35     int blocklen, hashlen;
36     void (*final)(void*, uint8_t*);
37     void (*update)(void*, const uint8_t*, int len);
38     void (*init)(void*);
39     uint8_t key[MAX_BLOCKLEN];
40     int keylen;
41 };
42
43 #define DEFINE_SHA(bits)                           \
44 static av_cold void sha ## bits ##_init(void *ctx) \
45 {                                                  \
46     av_sha_init(ctx, bits);                        \
47 }
48
49 #define DEFINE_SHA512(bits)                        \
50 static av_cold void sha ## bits ##_init(void *ctx) \
51 {                                                  \
52     av_sha512_init(ctx, bits);                     \
53 }
54
55 DEFINE_SHA(160)
56 DEFINE_SHA(224)
57 DEFINE_SHA(256)
58 DEFINE_SHA512(384)
59 DEFINE_SHA512(512)
60
61 AVHMAC *av_hmac_alloc(enum AVHMACType type)
62 {
63     AVHMAC *c = av_mallocz(sizeof(*c));
64     if (!c)
65         return NULL;
66     switch (type) {
67     case AV_HMAC_MD5:
68         c->blocklen = 64;
69         c->hashlen  = 16;
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();
74         break;
75     case AV_HMAC_SHA1:
76         c->blocklen = 64;
77         c->hashlen  = 20;
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();
82         break;
83     case AV_HMAC_SHA224:
84 #if FF_API_HMAC
85     case AV_HMAC_SHA224_DEPRECATED:
86 #endif
87         c->blocklen = 64;
88         c->hashlen  = 28;
89         c->init     = sha224_init;
90         c->update   = (void*)av_sha_update;
91         c->final    = (void*)av_sha_final;
92         c->hash     = av_sha_alloc();
93         break;
94     case AV_HMAC_SHA256:
95 #if FF_API_HMAC
96     case AV_HMAC_SHA256_DEPRECATED:
97 #endif
98         c->blocklen = 64;
99         c->hashlen  = 32;
100         c->init     = sha256_init;
101         c->update   = (void*)av_sha_update;
102         c->final    = (void*)av_sha_final;
103         c->hash     = av_sha_alloc();
104         break;
105     case AV_HMAC_SHA384:
106         c->blocklen = 128;
107         c->hashlen  = 48;
108         c->init     = sha384_init;
109         c->update   = (void*)av_sha512_update;
110         c->final    = (void*)av_sha512_final;
111         c->hash     = av_sha512_alloc();
112         break;
113     case AV_HMAC_SHA512:
114         c->blocklen = 128;
115         c->hashlen  = 64;
116         c->init     = sha512_init;
117         c->update   = (void*)av_sha512_update;
118         c->final    = (void*)av_sha512_final;
119         c->hash     = av_sha512_alloc();
120         break;
121     default:
122         av_free(c);
123         return NULL;
124     }
125     if (!c->hash) {
126         av_free(c);
127         return NULL;
128     }
129     return c;
130 }
131
132 void av_hmac_free(AVHMAC *c)
133 {
134     if (!c)
135         return;
136     av_freep(&c->hash);
137     av_free(c);
138 }
139
140 void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
141 {
142     int i;
143     uint8_t block[MAX_BLOCKLEN];
144     if (keylen > c->blocklen) {
145         c->init(c->hash);
146         c->update(c->hash, key, keylen);
147         c->final(c->hash, c->key);
148         c->keylen = c->hashlen;
149     } else {
150         memcpy(c->key, key, keylen);
151         c->keylen = keylen;
152     }
153     c->init(c->hash);
154     for (i = 0; i < c->keylen; i++)
155         block[i] = c->key[i] ^ 0x36;
156     for (i = c->keylen; i < c->blocklen; i++)
157         block[i] = 0x36;
158     c->update(c->hash, block, c->blocklen);
159 }
160
161 void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
162 {
163     c->update(c->hash, data, len);
164 }
165
166 int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
167 {
168     uint8_t block[MAX_BLOCKLEN];
169     int i;
170     if (outlen < c->hashlen)
171         return AVERROR(EINVAL);
172     c->final(c->hash, out);
173     c->init(c->hash);
174     for (i = 0; i < c->keylen; i++)
175         block[i] = c->key[i] ^ 0x5C;
176     for (i = c->keylen; i < c->blocklen; i++)
177         block[i] = 0x5C;
178     c->update(c->hash, block, c->blocklen);
179     c->update(c->hash, out, c->hashlen);
180     c->final(c->hash, out);
181     return c->hashlen;
182 }
183
184 int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
185                  const uint8_t *key, unsigned int keylen,
186                  uint8_t *out, unsigned int outlen)
187 {
188     av_hmac_init(c, key, keylen);
189     av_hmac_update(c, data, len);
190     return av_hmac_final(c, out, outlen);
191 }
192
193 #ifdef TEST
194 #include <stdio.h>
195
196 static void test(AVHMAC *hmac, const uint8_t *key, int keylen,
197                  const uint8_t *data, int datalen)
198 {
199     uint8_t buf[MAX_HASHLEN];
200     int out, i;
201     // Some of the test vectors are strings, where sizeof() includes the
202     // trailing null byte - remove that.
203     if (!key[keylen - 1])
204         keylen--;
205     if (!data[datalen - 1])
206         datalen--;
207     out = av_hmac_calc(hmac, data, datalen, key, keylen, buf, sizeof(buf));
208     for (i = 0; i < out; i++)
209         printf("%02x", buf[i]);
210     printf("\n");
211 }
212
213 int main(void)
214 {
215     uint8_t key1[20], key3[131], data3[50];
216     AVHMAC *hmac;
217     enum AVHMACType i;
218     static const uint8_t key2[]  = "Jefe";
219     static const uint8_t data1[] = "Hi There";
220     static const uint8_t data2[] = "what do ya want for nothing?";
221     static const uint8_t data4[] = "Test Using Larger Than Block-Size Key - Hash Key First";
222     static const uint8_t data5[] = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";
223     static const uint8_t data6[] = "This is a test using a larger than block-size key and a larger "
224                             "than block-size data. The key needs to be hashed before being used"
225                             " by the HMAC algorithm.";
226     memset(key1, 0x0b, sizeof(key1));
227     memset(key3, 0xaa, sizeof(key3));
228     memset(data3, 0xdd, sizeof(data3));
229
230     /* MD5, SHA-1 */
231     for (i = AV_HMAC_MD5; i <= AV_HMAC_SHA1; i++) {
232         hmac = av_hmac_alloc(i);
233         if (!hmac)
234             return 1;
235         // RFC 2202 test vectors
236         test(hmac, key1, hmac->hashlen, data1, sizeof(data1));
237         test(hmac, key2, sizeof(key2),  data2, sizeof(data2));
238         test(hmac, key3, hmac->hashlen, data3, sizeof(data3));
239         test(hmac, key3, 80,            data4, sizeof(data4));
240         test(hmac, key3, 80,            data5, sizeof(data5));
241         av_hmac_free(hmac);
242     }
243
244     /* SHA-2 */
245     for (i = AV_HMAC_SHA224; i <= AV_HMAC_SHA256; i++) {
246         hmac = av_hmac_alloc(i);
247         if (!hmac)
248             return 1;
249         // RFC 4231 test vectors
250         test(hmac, key1, sizeof(key1), data1, sizeof(data1));
251         test(hmac, key2, sizeof(key2), data2, sizeof(data2));
252         test(hmac, key3, 20,           data3, sizeof(data3));
253         test(hmac, key3, sizeof(key3), data4, sizeof(data4));
254         test(hmac, key3, sizeof(key3), data6, sizeof(data6));
255         av_hmac_free(hmac);
256     }
257
258     for (i = AV_HMAC_SHA384; i <= AV_HMAC_SHA512; i++) {
259         hmac = av_hmac_alloc(i);
260         if (!hmac)
261             return 1;
262         // RFC 4231 test vectors
263         test(hmac, key1, sizeof(key1), data1, sizeof(data1));
264         test(hmac, key2, sizeof(key2), data2, sizeof(data2));
265         test(hmac, key3, 20, data3, sizeof(data3));
266         test(hmac, key3, sizeof(key3), data4, sizeof(data4));
267         test(hmac, key3, sizeof(key3), data6, sizeof(data6));
268         av_hmac_free(hmac);
269     }
270     return 0;
271 }
272 #endif /* TEST */