FFmpeg
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 <stddef.h>
22 #include <stdint.h>
23 #include <string.h>
24 
25 #include "attributes.h"
26 #include "error.h"
27 #include "hmac.h"
28 #include "md5.h"
29 #include "sha.h"
30 #include "sha512.h"
31 #include "mem.h"
32 
33 #define MAX_HASHLEN 64
34 #define MAX_BLOCKLEN 128
35 
36 typedef void (*hmac_final)(void *ctx, uint8_t *dst);
37 typedef void (*hmac_update)(void *ctx, const uint8_t *src, size_t len);
38 typedef void (*hmac_init)(void *ctx);
39 
40 struct AVHMAC {
41  void *hash;
43  hmac_final final;
46  uint8_t key[MAX_BLOCKLEN];
47  int keylen;
48 };
49 
50 #define DEFINE_SHA(bits) \
51 static av_cold void sha ## bits ##_init(void *ctx) \
52 { \
53  av_sha_init(ctx, bits); \
54 }
55 
56 #define DEFINE_SHA512(bits) \
57 static av_cold void sha ## bits ##_init(void *ctx) \
58 { \
59  av_sha512_init(ctx, bits); \
60 }
61 
62 DEFINE_SHA(160)
63 DEFINE_SHA(224)
64 DEFINE_SHA(256)
65 DEFINE_SHA512(384)
66 DEFINE_SHA512(512)
67 
69 {
70  AVHMAC *c = av_mallocz(sizeof(*c));
71  if (!c)
72  return NULL;
73  switch (type) {
74  case AV_HMAC_MD5:
75  c->blocklen = 64;
76  c->hashlen = 16;
77  c->init = (hmac_init) av_md5_init;
78  c->update = (hmac_update) av_md5_update;
79  c->final = (hmac_final) av_md5_final;
80  c->hash = av_md5_alloc();
81  break;
82  case AV_HMAC_SHA1:
83  c->blocklen = 64;
84  c->hashlen = 20;
85  c->init = sha160_init;
86  c->update = (hmac_update) av_sha_update;
87  c->final = (hmac_final) av_sha_final;
88  c->hash = av_sha_alloc();
89  break;
90  case AV_HMAC_SHA224:
91  c->blocklen = 64;
92  c->hashlen = 28;
93  c->init = sha224_init;
94  c->update = (hmac_update) av_sha_update;
95  c->final = (hmac_final) av_sha_final;
96  c->hash = av_sha_alloc();
97  break;
98  case AV_HMAC_SHA256:
99  c->blocklen = 64;
100  c->hashlen = 32;
101  c->init = sha256_init;
102  c->update = (hmac_update) av_sha_update;
103  c->final = (hmac_final) av_sha_final;
104  c->hash = av_sha_alloc();
105  break;
106  case AV_HMAC_SHA384:
107  c->blocklen = 128;
108  c->hashlen = 48;
109  c->init = sha384_init;
110  c->update = (hmac_update) av_sha512_update;
111  c->final = (hmac_final) av_sha512_final;
112  c->hash = av_sha512_alloc();
113  break;
114  case AV_HMAC_SHA512:
115  c->blocklen = 128;
116  c->hashlen = 64;
117  c->init = sha512_init;
118  c->update = (hmac_update) av_sha512_update;
119  c->final = (hmac_final) av_sha512_final;
120  c->hash = av_sha512_alloc();
121  break;
122  default:
123  av_free(c);
124  return NULL;
125  }
126  if (!c->hash) {
127  av_free(c);
128  return NULL;
129  }
130  return c;
131 }
132 
134 {
135  if (!c)
136  return;
137  av_freep(&c->hash);
138  av_free(c);
139 }
140 
141 void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
142 {
143  int i;
144  uint8_t block[MAX_BLOCKLEN];
145  if (keylen > c->blocklen) {
146  c->init(c->hash);
147  c->update(c->hash, key, keylen);
148  c->final(c->hash, c->key);
149  c->keylen = c->hashlen;
150  } else {
151  memcpy(c->key, key, keylen);
152  c->keylen = keylen;
153  }
154  c->init(c->hash);
155  for (i = 0; i < c->keylen; i++)
156  block[i] = c->key[i] ^ 0x36;
157  for (i = c->keylen; i < c->blocklen; i++)
158  block[i] = 0x36;
159  c->update(c->hash, block, c->blocklen);
160 }
161 
162 void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
163 {
164  c->update(c->hash, data, len);
165 }
166 
167 int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
168 {
169  uint8_t block[MAX_BLOCKLEN];
170  int i;
171  if (outlen < c->hashlen)
172  return AVERROR(EINVAL);
173  c->final(c->hash, out);
174  c->init(c->hash);
175  for (i = 0; i < c->keylen; i++)
176  block[i] = c->key[i] ^ 0x5C;
177  for (i = c->keylen; i < c->blocklen; i++)
178  block[i] = 0x5C;
179  c->update(c->hash, block, c->blocklen);
180  c->update(c->hash, out, c->hashlen);
181  c->final(c->hash, out);
182  return c->hashlen;
183 }
184 
185 int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
186  const uint8_t *key, unsigned int keylen,
187  uint8_t *out, unsigned int outlen)
188 {
189  av_hmac_init(c, key, keylen);
191  return av_hmac_final(c, out, outlen);
192 }
AVHMAC
Definition: hmac.c:40
DEFINE_SHA512
#define DEFINE_SHA512(bits)
Definition: hmac.c:56
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
av_sha512_final
void av_sha512_final(AVSHA512 *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha512.c:275
out
FILE * out
Definition: movenc.c:55
MAX_BLOCKLEN
#define MAX_BLOCKLEN
Definition: hmac.c:34
AV_HMAC_MD5
@ AV_HMAC_MD5
Definition: hmac.h:33
data
const char data[16]
Definition: mxf.c:148
av_sha512_alloc
struct AVSHA512 * av_sha512_alloc(void)
Allocate an AVSHA512 context.
Definition: sha512.c:44
hmac_update
void(* hmac_update)(void *ctx, const uint8_t *src, size_t len)
Definition: hmac.c:37
sha512.h
av_hmac_calc
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:185
av_hmac_final
int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
Finish hashing and output the HMAC digest.
Definition: hmac.c:167
AVHMAC::key
uint8_t key[MAX_BLOCKLEN]
Definition: hmac.c:46
hmac_final
void(* hmac_final)(void *ctx, uint8_t *dst)
Definition: hmac.c:36
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
DEFINE_SHA
#define DEFINE_SHA(bits)
Definition: hmac.c:50
ctx
AVFormatContext * ctx
Definition: movenc.c:49
key
const char * key
Definition: hwcontext_opencl.c:189
AVHMAC::update
hmac_update update
Definition: hmac.c:44
av_sha_final
void av_sha_final(AVSHA *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha.c:347
AV_HMAC_SHA256
@ AV_HMAC_SHA256
Definition: hmac.h:36
NULL
#define NULL
Definition: coverity.c:32
sha.h
av_hmac_update
void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
Hash data with the HMAC.
Definition: hmac.c:162
AVHMAC::hash
void * hash
Definition: hmac.c:41
AVHMAC::keylen
int keylen
Definition: hmac.c:47
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
AV_HMAC_SHA1
@ AV_HMAC_SHA1
Definition: hmac.h:34
error.h
AV_HMAC_SHA384
@ AV_HMAC_SHA384
Definition: hmac.h:37
AV_HMAC_SHA512
@ AV_HMAC_SHA512
Definition: hmac.h:38
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
av_sha_update
void av_sha_update(struct AVSHA *ctx, const uint8_t *data, size_t len)
Update hash value.
Definition: sha.c:315
attributes.h
av_hmac_alloc
AVHMAC * av_hmac_alloc(enum AVHMACType type)
Allocate an AVHMAC context.
Definition: hmac.c:68
av_hmac_free
void av_hmac_free(AVHMAC *c)
Free an AVHMAC context.
Definition: hmac.c:133
av_sha_alloc
struct AVSHA * av_sha_alloc(void)
Allocate an AVSHA context.
Definition: sha.c:46
av_md5_init
void av_md5_init(AVMD5 *ctx)
Initialize MD5 hashing.
Definition: md5.c:143
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_hmac_init
void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
Initialize an AVHMAC context with an authentication key.
Definition: hmac.c:141
md5.h
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
len
int len
Definition: vorbis_enc_data.h:426
av_md5_final
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Finish hashing and output digest value.
Definition: md5.c:188
AVHMAC::blocklen
int blocklen
Definition: hmac.c:42
AVHMACType
AVHMACType
Definition: hmac.h:32
av_md5_alloc
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
Definition: md5.c:50
hmac_init
void(* hmac_init)(void *ctx)
Definition: hmac.c:38
av_md5_update
void av_md5_update(AVMD5 *ctx, const uint8_t *src, size_t len)
Update hash value.
Definition: md5.c:153
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVHMAC::hashlen
int hashlen
Definition: hmac.c:42
AV_HMAC_SHA224
@ AV_HMAC_SHA224
Definition: hmac.h:35
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
AVHMAC::init
hmac_init init
Definition: hmac.c:45
av_sha512_update
void av_sha512_update(AVSHA512 *ctx, const uint8_t *data, size_t len)
Update hash value.
Definition: sha512.c:243
hmac.h