FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
hmac.c
Go to the documentation of this file.
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;
36  void (*final)(void*, uint8_t*);
37  void (*update)(void*, const uint8_t*, int len);
38  void (*init)(void*);
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 
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  c->blocklen = 64;
85  c->hashlen = 28;
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();
90  break;
91  case AV_HMAC_SHA256:
92  c->blocklen = 64;
93  c->hashlen = 32;
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();
98  break;
99  case AV_HMAC_SHA384:
100  c->blocklen = 128;
101  c->hashlen = 48;
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();
106  break;
107  case AV_HMAC_SHA512:
108  c->blocklen = 128;
109  c->hashlen = 64;
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();
114  break;
115  default:
116  av_free(c);
117  return NULL;
118  }
119  if (!c->hash) {
120  av_free(c);
121  return NULL;
122  }
123  return c;
124 }
125 
127 {
128  if (!c)
129  return;
130  av_freep(&c->hash);
131  av_free(c);
132 }
133 
134 void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
135 {
136  int i;
138  if (keylen > c->blocklen) {
139  c->init(c->hash);
140  c->update(c->hash, key, keylen);
141  c->final(c->hash, c->key);
142  c->keylen = c->hashlen;
143  } else {
144  memcpy(c->key, key, keylen);
145  c->keylen = keylen;
146  }
147  c->init(c->hash);
148  for (i = 0; i < c->keylen; i++)
149  block[i] = c->key[i] ^ 0x36;
150  for (i = c->keylen; i < c->blocklen; i++)
151  block[i] = 0x36;
152  c->update(c->hash, block, c->blocklen);
153 }
154 
155 void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
156 {
157  c->update(c->hash, data, len);
158 }
159 
160 int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
161 {
163  int i;
164  if (outlen < c->hashlen)
165  return AVERROR(EINVAL);
166  c->final(c->hash, out);
167  c->init(c->hash);
168  for (i = 0; i < c->keylen; i++)
169  block[i] = c->key[i] ^ 0x5C;
170  for (i = c->keylen; i < c->blocklen; i++)
171  block[i] = 0x5C;
172  c->update(c->hash, block, c->blocklen);
173  c->update(c->hash, out, c->hashlen);
174  c->final(c->hash, out);
175  return c->hashlen;
176 }
177 
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)
181 {
182  av_hmac_init(c, key, keylen);
183  av_hmac_update(c, data, len);
184  return av_hmac_final(c, out, outlen);
185 }
186 
187 #ifdef TEST
188 #include <stdio.h>
189 
190 static void test(AVHMAC *hmac, const uint8_t *key, int keylen,
191  const uint8_t *data, int datalen)
192 {
194  int out, i;
195  // Some of the test vectors are strings, where sizeof() includes the
196  // trailing null byte - remove that.
197  if (!key[keylen - 1])
198  keylen--;
199  if (!data[datalen - 1])
200  datalen--;
201  out = av_hmac_calc(hmac, data, datalen, key, keylen, buf, sizeof(buf));
202  for (i = 0; i < out; i++)
203  printf("%02x", buf[i]);
204  printf("\n");
205 }
206 
207 int main(void)
208 {
209  uint8_t key1[20], key3[131], data3[50];
210  enum AVHMACType i = AV_HMAC_SHA224;
211  static const uint8_t key2[] = "Jefe";
212  static const uint8_t data1[] = "Hi There";
213  static const uint8_t data2[] = "what do ya want for nothing?";
214  static const uint8_t data4[] = "Test Using Larger Than Block-Size Key - Hash Key First";
215  static const uint8_t data5[] = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";
216  static const uint8_t data6[] = "This is a test using a larger than block-size key and a larger "
217  "than block-size data. The key needs to be hashed before being used"
218  " by the HMAC algorithm.";
220  if (!hmac)
221  return 1;
222  memset(key1, 0x0b, sizeof(key1));
223  memset(key3, 0xaa, sizeof(key3));
224  memset(data3, 0xdd, sizeof(data3));
225  // RFC 2202 test vectors
226  test(hmac, key1, 16, data1, sizeof(data1));
227  test(hmac, key2, sizeof(key2), data2, sizeof(data2));
228  test(hmac, key3, 16, data3, sizeof(data3));
229  test(hmac, key3, 80, data4, sizeof(data4));
230  test(hmac, key3, 80, data5, sizeof(data5));
231  av_hmac_free(hmac);
232 
233  /* SHA-1 */
234  hmac = av_hmac_alloc(AV_HMAC_SHA1);
235  if (!hmac)
236  return 1;
237  // RFC 2202 test vectors
238  test(hmac, key1, sizeof(key1), data1, sizeof(data1));
239  test(hmac, key2, sizeof(key2), data2, sizeof(data2));
240  test(hmac, key3, 20, data3, sizeof(data3));
241  test(hmac, key3, 80, data4, sizeof(data4));
242  test(hmac, key3, 80, data5, sizeof(data5));
243  av_hmac_free(hmac);
244 
245  /* SHA-2 */
246  while (i <= AV_HMAC_SHA512) {
247  hmac = av_hmac_alloc(i);
248  if (!hmac)
249  return 1;
250  // RFC 4231 test vectors
251  test(hmac, key1, sizeof(key1), data1, sizeof(data1));
252  test(hmac, key2, sizeof(key2), data2, sizeof(data2));
253  test(hmac, key3, 20, data3, sizeof(data3));
254  test(hmac, key3, sizeof(key3), data4, sizeof(data4));
255  test(hmac, key3, sizeof(key3), data6, sizeof(data6));
256  av_hmac_free(hmac);
257  i++;
258  }
259  return 0;
260 }
261 #endif /* TEST */
void av_sha_final(AVSHA *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha.c:341
#define NULL
Definition: coverity.c:32
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)
Hash an array of data with a key.
Definition: hmac.c:178
void av_sha_update(AVSHA *ctx, const uint8_t *data, unsigned int len)
Update hash value.
Definition: sha.c:314
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
AVHMAC * av_hmac_alloc(enum AVHMACType type)
Allocate an AVHMAC context.
Definition: hmac.c:61
memory handling functions
AVHMACType
Definition: hmac.h:32
void av_md5_update(AVMD5 *ctx, const uint8_t *src, int len)
Update hash value.
Definition: md5.c:148
Macro definitions for various function/variable attributes.
void * hash
Definition: hmac.c:34
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
Definition: md5.c:47
uint8_t
void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
Hash data with the HMAC.
Definition: hmac.c:155
struct AVSHA512 * av_sha512_alloc(void)
Allocate an AVSHA512 context.
Definition: sha512.c:43
#define AVERROR(e)
Definition: error.h:43
int hashlen
Definition: hmac.c:35
int blocklen
Definition: hmac.c:35
static void test(const char *pattern, const char *host)
Definition: noproxy-test.c:23
Definition: hmac.c:33
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
Initialize an AVHMAC context with an authentication key.
Definition: hmac.c:134
uint8_t key[MAX_BLOCKLEN]
Definition: hmac.c:39
void(* final)(void *, uint8_t *)
Definition: hmac.c:36
void av_sha512_update(AVSHA512 *ctx, const uint8_t *data, unsigned int len)
Update hash value.
Definition: sha512.c:242
#define MAX_HASHLEN
Definition: hmac.c:30
struct AVSHA * av_sha_alloc(void)
Allocate an AVSHA context.
Definition: sha.c:45
void av_md5_init(AVMD5 *ctx)
Initialize MD5 hashing.
Definition: md5.c:138
void * buf
Definition: avisynth_c.h:553
GLint GLenum type
Definition: opengl_enc.c:105
#define MAX_BLOCKLEN
Definition: hmac.c:31
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Finish hashing and output digest value.
Definition: md5.c:183
int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
Finish hashing and output the HMAC digest.
Definition: hmac.c:160
void(* update)(void *, const uint8_t *, int len)
Definition: hmac.c:37
int keylen
Definition: hmac.c:40
#define DEFINE_SHA512(bits)
Definition: hmac.c:49
static double c[64]
void av_hmac_free(AVHMAC *c)
Free an AVHMAC context.
Definition: hmac.c:126
#define av_free(p)
int len
void av_sha512_final(AVSHA512 *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha512.c:269
void(* init)(void *)
Definition: hmac.c:38
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
#define av_freep(p)
#define DEFINE_SHA(bits)
Definition: hmac.c:43
int main(int argc, char **argv)
Definition: main.c:22
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:250
for(j=16;j >0;--j)
static int16_t block[64]
Definition: dct-test.c:110