Libav
hmac.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Martin Storsjo
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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  * Libav 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 Libav; 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 "mem.h"
28 
29 #define MAX_HASHLEN 20
30 #define MAX_BLOCKLEN 64
31 
32 struct AVHMAC {
33  void *hash;
35  void (*final)(void*, uint8_t*);
36  void (*update)(void*, const uint8_t*, int len);
37  void (*init)(void*);
38  uint8_t key[MAX_BLOCKLEN];
39  int keylen;
40 };
41 
42 static av_cold void sha1_init(void *ctx)
43 {
44  av_sha_init(ctx, 160);
45 }
46 
48 {
49  AVHMAC *c = av_mallocz(sizeof(*c));
50  if (!c)
51  return NULL;
52  switch (type) {
53  case AV_HMAC_MD5:
54  c->blocklen = 64;
55  c->hashlen = 16;
56  c->init = av_md5_init;
57  c->update = av_md5_update;
58  c->final = av_md5_final;
59  c->hash = av_md5_alloc();
60  break;
61  case AV_HMAC_SHA1:
62  c->blocklen = 64;
63  c->hashlen = 20;
64  c->init = sha1_init;
65  c->update = av_sha_update;
66  c->final = av_sha_final;
67  c->hash = av_sha_alloc();
68  break;
69  default:
70  av_free(c);
71  return NULL;
72  }
73  if (!c->hash) {
74  av_free(c);
75  return NULL;
76  }
77  return c;
78 }
79 
81 {
82  if (!c)
83  return;
84  av_free(c->hash);
85  av_free(c);
86 }
87 
88 void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
89 {
90  int i;
92  if (keylen > c->blocklen) {
93  c->init(c->hash);
94  c->update(c->hash, key, keylen);
95  c->final(c->hash, c->key);
96  c->keylen = c->hashlen;
97  } else {
98  memcpy(c->key, key, keylen);
99  c->keylen = keylen;
100  }
101  c->init(c->hash);
102  for (i = 0; i < c->keylen; i++)
103  block[i] = c->key[i] ^ 0x36;
104  for (i = c->keylen; i < c->blocklen; i++)
105  block[i] = 0x36;
106  c->update(c->hash, block, c->blocklen);
107 }
108 
109 void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
110 {
111  c->update(c->hash, data, len);
112 }
113 
114 int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
115 {
117  int i;
118  if (outlen < c->hashlen)
119  return AVERROR(EINVAL);
120  c->final(c->hash, out);
121  c->init(c->hash);
122  for (i = 0; i < c->keylen; i++)
123  block[i] = c->key[i] ^ 0x5C;
124  for (i = c->keylen; i < c->blocklen; i++)
125  block[i] = 0x5C;
126  c->update(c->hash, block, c->blocklen);
127  c->update(c->hash, out, c->hashlen);
128  c->final(c->hash, out);
129  return c->hashlen;
130 }
131 
132 int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
133  const uint8_t *key, unsigned int keylen,
134  uint8_t *out, unsigned int outlen)
135 {
136  av_hmac_init(c, key, keylen);
137  av_hmac_update(c, data, len);
138  return av_hmac_final(c, out, outlen);
139 }
140 
141 #ifdef TEST
142 #include <stdio.h>
143 
144 static void test(AVHMAC *hmac, const uint8_t *key, int keylen,
145  const uint8_t *data, int datalen)
146 {
147  uint8_t buf[MAX_HASHLEN];
148  int out, i;
149  // Some of the test vectors are strings, where sizeof() includes the
150  // trailing null byte - remove that.
151  if (!key[keylen - 1])
152  keylen--;
153  if (!data[datalen - 1])
154  datalen--;
155  out = av_hmac_calc(hmac, data, datalen, key, keylen, buf, sizeof(buf));
156  for (i = 0; i < out; i++)
157  printf("%02x", buf[i]);
158  printf("\n");
159 }
160 
161 int main(void)
162 {
163  uint8_t key1[16], key3[16], data3[50], key4[63], key5[64], key6[65];
164  const uint8_t key2[] = "Jefe";
165  const uint8_t data1[] = "Hi There";
166  const uint8_t data2[] = "what do ya want for nothing?";
168  if (!hmac)
169  return 1;
170  memset(key1, 0x0b, sizeof(key1));
171  memset(key3, 0xaa, sizeof(key3));
172  memset(key4, 0x44, sizeof(key4));
173  memset(key5, 0x55, sizeof(key5));
174  memset(key6, 0x66, sizeof(key6));
175  memset(data3, 0xdd, sizeof(data3));
176  // RFC 2104 test vectors
177  test(hmac, key1, sizeof(key1), data1, sizeof(data1));
178  test(hmac, key2, sizeof(key2), data2, sizeof(data2));
179  test(hmac, key3, sizeof(key3), data3, sizeof(data3));
180  // Additional tests, to test cases where the key is too long
181  test(hmac, key4, sizeof(key4), data1, sizeof(data1));
182  test(hmac, key5, sizeof(key5), data2, sizeof(data2));
183  test(hmac, key6, sizeof(key6), data3, sizeof(data3));
184  av_hmac_free(hmac);
185  return 0;
186 }
187 #endif /* TEST */
void av_sha_final(AVSHA *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha.c:324
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:132
void av_sha_update(AVSHA *ctx, const uint8_t *data, unsigned int len)
Update hash value.
Definition: sha.c:297
AVHMAC * av_hmac_alloc(enum AVHMACType type)
Allocate an AVHMAC context.
Definition: hmac.c:47
av_cold int av_sha_init(AVSHA *ctx, int bits)
Initialize SHA-1 or SHA-2 hashing.
Definition: sha.c:256
memory handling functions
AVHMACType
Definition: hmac.h:32
Macro definitions for various function/variable attributes.
void * hash
Definition: hmac.c:33
struct AVMD5 * av_md5_alloc(void)
Definition: md5.c:49
uint8_t
#define av_cold
Definition: attributes.h:66
void(* final)(void *, uint8_t *)
Definition: hmac.c:35
const char data[16]
Definition: mxf.c:70
void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
Hash data with the HMAC.
Definition: hmac.c:109
void av_md5_update(AVMD5 *ctx, const uint8_t *src, const int len)
Definition: md5.c:144
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
void(* init)(void *)
Definition: hmac.c:37
#define AVERROR(e)
Definition: error.h:43
int hashlen
Definition: hmac.c:34
int main(int argc, char **argv)
Definition: avconv.c:2609
static av_cold void sha1_init(void *ctx)
Definition: hmac.c:42
int blocklen
Definition: hmac.c:34
static void test(const char *pattern, const char *host)
Definition: noproxy-test.c:23
Definition: hmac.c:32
void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
Initialize an AVHMAC context with an authentication key.
Definition: hmac.c:88
uint8_t key[MAX_BLOCKLEN]
Definition: hmac.c:38
NULL
Definition: eval.c:55
static void(WINAPI *cond_broadcast)(pthread_cond_t *cond)
#define MAX_HASHLEN
Definition: hmac.c:29
struct AVSHA * av_sha_alloc(void)
Allocate an AVSHA context.
Definition: sha.c:47
void av_md5_init(AVMD5 *ctx)
Definition: md5.c:134
#define MAX_BLOCKLEN
Definition: hmac.c:30
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Definition: md5.c:160
int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
Finish hashing and output the HMAC digest.
Definition: hmac.c:114
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_dlog(ac->avr,"%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
int keylen
Definition: hmac.c:39
void av_hmac_free(AVHMAC *c)
Free an AVHMAC context.
Definition: hmac.c:80
void(* update)(void *, const uint8_t *, int len)
Definition: hmac.c:36
int len
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:205
for(j=16;j >0;--j)
static int16_t block[64]
Definition: dct-test.c:88