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 <stddef.h>
22 #include <stdint.h>
23 #include <string.h>
24 
25 #include "attributes.h"
26 #include "hmac.h"
27 #include "md5.h"
28 #include "sha.h"
29 #include "sha512.h"
30 #include "mem.h"
31 #include "version.h"
32 
33 #define MAX_HASHLEN 64
34 #define MAX_BLOCKLEN 128
35 
36 typedef void (*hmac_final)(void *ctx, uint8_t *dst);
37 #if FF_API_CRYPTO_SIZE_T
38 typedef void (*hmac_update)(void *ctx, const uint8_t *src, int len);
39 #else
40 typedef void (*hmac_update)(void *ctx, const uint8_t *src, size_t len);
41 #endif
42 typedef void (*hmac_init)(void *ctx);
43 
44 struct AVHMAC {
45  void *hash;
47  hmac_final final;
51  int keylen;
52 };
53 
54 #define DEFINE_SHA(bits) \
55 static av_cold void sha ## bits ##_init(void *ctx) \
56 { \
57  av_sha_init(ctx, bits); \
58 }
59 
60 #define DEFINE_SHA512(bits) \
61 static av_cold void sha ## bits ##_init(void *ctx) \
62 { \
63  av_sha512_init(ctx, bits); \
64 }
65 
66 DEFINE_SHA(160)
67 DEFINE_SHA(224)
68 DEFINE_SHA(256)
69 DEFINE_SHA512(384)
70 DEFINE_SHA512(512)
71 
73 {
74  AVHMAC *c = av_mallocz(sizeof(*c));
75  if (!c)
76  return NULL;
77  switch (type) {
78  case AV_HMAC_MD5:
79  c->blocklen = 64;
80  c->hashlen = 16;
81  c->init = (hmac_init) av_md5_init;
84  c->hash = av_md5_alloc();
85  break;
86  case AV_HMAC_SHA1:
87  c->blocklen = 64;
88  c->hashlen = 20;
89  c->init = sha160_init;
92  c->hash = av_sha_alloc();
93  break;
94  case AV_HMAC_SHA224:
95  c->blocklen = 64;
96  c->hashlen = 28;
97  c->init = sha224_init;
100  c->hash = av_sha_alloc();
101  break;
102  case AV_HMAC_SHA256:
103  c->blocklen = 64;
104  c->hashlen = 32;
105  c->init = sha256_init;
108  c->hash = av_sha_alloc();
109  break;
110  case AV_HMAC_SHA384:
111  c->blocklen = 128;
112  c->hashlen = 48;
113  c->init = sha384_init;
116  c->hash = av_sha512_alloc();
117  break;
118  case AV_HMAC_SHA512:
119  c->blocklen = 128;
120  c->hashlen = 64;
121  c->init = sha512_init;
124  c->hash = av_sha512_alloc();
125  break;
126  default:
127  av_free(c);
128  return NULL;
129  }
130  if (!c->hash) {
131  av_free(c);
132  return NULL;
133  }
134  return c;
135 }
136 
138 {
139  if (!c)
140  return;
141  av_freep(&c->hash);
142  av_free(c);
143 }
144 
145 void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
146 {
147  int i;
149  if (keylen > c->blocklen) {
150  c->init(c->hash);
151  c->update(c->hash, key, keylen);
152  c->final(c->hash, c->key);
153  c->keylen = c->hashlen;
154  } else {
155  memcpy(c->key, key, keylen);
156  c->keylen = keylen;
157  }
158  c->init(c->hash);
159  for (i = 0; i < c->keylen; i++)
160  block[i] = c->key[i] ^ 0x36;
161  for (i = c->keylen; i < c->blocklen; i++)
162  block[i] = 0x36;
163  c->update(c->hash, block, c->blocklen);
164 }
165 
166 void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
167 {
168  c->update(c->hash, data, len);
169 }
170 
171 int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
172 {
174  int i;
175  if (outlen < c->hashlen)
176  return AVERROR(EINVAL);
177  c->final(c->hash, out);
178  c->init(c->hash);
179  for (i = 0; i < c->keylen; i++)
180  block[i] = c->key[i] ^ 0x5C;
181  for (i = c->keylen; i < c->blocklen; i++)
182  block[i] = 0x5C;
183  c->update(c->hash, block, c->blocklen);
184  c->update(c->hash, out, c->hashlen);
185  c->final(c->hash, out);
186  return c->hashlen;
187 }
188 
189 int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
190  const uint8_t *key, unsigned int keylen,
191  uint8_t *out, unsigned int outlen)
192 {
193  av_hmac_init(c, key, keylen);
194  av_hmac_update(c, data, len);
195  return av_hmac_final(c, out, outlen);
196 }
void av_sha_final(AVSHA *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha.c:345
#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:189
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
hmac_init init
Definition: hmac.c:49
AVHMAC * av_hmac_alloc(enum AVHMACType type)
Allocate an AVHMAC context.
Definition: hmac.c:72
Memory handling functions.
const char * key
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
#define src
Definition: vp8dsp.c:254
AVHMACType
Definition: hmac.h:33
void av_md5_update(AVMD5 *ctx, const uint8_t *src, int len)
Update hash value.
Definition: md5.c:154
Macro definitions for various function/variable attributes.
void * hash
Definition: hmac.c:45
static int16_t block[64]
Definition: dct.c:115
struct AVMD5 * av_md5_alloc(void)
Allocate an AVMD5 context.
Definition: md5.c:48
uint8_t
void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
Hash data with the HMAC.
Definition: hmac.c:166
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:46
Libavutil version macros.
int blocklen
Definition: hmac.c:46
void(* hmac_final)(void *ctx, uint8_t *dst)
Definition: hmac.c:36
Definition: hmac.c:44
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
void av_sha_update(struct AVSHA *ctx, const uint8_t *data, unsigned int len)
Update hash value.
Definition: sha.c:315
void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
Initialize an AVHMAC context with an authentication key.
Definition: hmac.c:145
AVFormatContext * ctx
Definition: movenc.c:48
uint8_t key[MAX_BLOCKLEN]
Definition: hmac.c:50
void av_sha512_update(AVSHA512 *ctx, const uint8_t *data, unsigned int len)
Update hash value.
Definition: sha512.c:243
Public header for SHA-1 & SHA-256 hash function implementations.
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:143
GLint GLenum type
Definition: opengl_enc.c:105
#define MAX_BLOCKLEN
Definition: hmac.c:34
void av_md5_final(AVMD5 *ctx, uint8_t *dst)
Finish hashing and output digest value.
Definition: md5.c:192
int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
Finish hashing and output the HMAC digest.
Definition: hmac.c:171
hmac_final final
Definition: hmac.c:47
void(* hmac_update)(void *ctx, const uint8_t *src, int len)
Definition: hmac.c:38
Public header for SHA-512 implementation.
int keylen
Definition: hmac.c:51
#define DEFINE_SHA512(bits)
Definition: hmac.c:60
static double c[64]
void av_hmac_free(AVHMAC *c)
Free an AVHMAC context.
Definition: hmac.c:137
#define av_free(p)
int len
void av_sha512_final(AVSHA512 *ctx, uint8_t *digest)
Finish hashing and output digest value.
Definition: sha512.c:273
void(* hmac_init)(void *ctx)
Definition: hmac.c:42
hmac_update update
Definition: hmac.c:48
FILE * out
Definition: movenc.c:54
Public header for MD5 hash function implementation.
#define av_freep(p)
#define DEFINE_SHA(bits)
Definition: hmac.c:54
for(j=16;j >0;--j)