Libav
hmac.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "libavutil/hmac.c"
20 
21 #include <stdio.h>
22 #include <string.h>
23 
24 static void test(AVHMAC *hmac, const uint8_t *key, int keylen,
25  const uint8_t *data, int datalen)
26 {
27  uint8_t buf[MAX_HASHLEN];
28  int out, i;
29  // Some of the test vectors are strings, where sizeof() includes the
30  // trailing null byte - remove that.
31  if (!key[keylen - 1])
32  keylen--;
33  if (!data[datalen - 1])
34  datalen--;
35  out = av_hmac_calc(hmac, data, datalen, key, keylen, buf, sizeof(buf));
36  for (i = 0; i < out; i++)
37  printf("%02x", buf[i]);
38  printf("\n");
39 }
40 
41 int main(void)
42 {
43  uint8_t key1[20], key3[131], data3[50];
44  enum AVHMACType i = AV_HMAC_SHA224;
45  static const uint8_t key2[] = "Jefe";
46  static const uint8_t data1[] = "Hi There";
47  static const uint8_t data2[] = "what do ya want for nothing?";
48  static const uint8_t data4[] = "Test Using Larger Than Block-Size Key - Hash Key First";
49  static const uint8_t data5[] = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";
50  static const uint8_t data6[] = "This is a test using a larger than block-size key and a larger "
51  "than block-size data. The key needs to be hashed before being used"
52  " by the HMAC algorithm.";
54  if (!hmac)
55  return 1;
56  memset(key1, 0x0b, sizeof(key1));
57  memset(key3, 0xaa, sizeof(key3));
58  memset(data3, 0xdd, sizeof(data3));
59  // RFC 2202 test vectors
60  test(hmac, key1, 16, data1, sizeof(data1));
61  test(hmac, key2, sizeof(key2), data2, sizeof(data2));
62  test(hmac, key3, 16, data3, sizeof(data3));
63  test(hmac, key3, 80, data4, sizeof(data4));
64  test(hmac, key3, 80, data5, sizeof(data5));
65  av_hmac_free(hmac);
66 
67  /* SHA-1 */
69  if (!hmac)
70  return 1;
71  // RFC 2202 test vectors
72  test(hmac, key1, sizeof(key1), data1, sizeof(data1));
73  test(hmac, key2, sizeof(key2), data2, sizeof(data2));
74  test(hmac, key3, 20, data3, sizeof(data3));
75  test(hmac, key3, 80, data4, sizeof(data4));
76  test(hmac, key3, 80, data5, sizeof(data5));
77  av_hmac_free(hmac);
78 
79  /* SHA-2 */
80  while (i <= AV_HMAC_SHA256) {
81  hmac = av_hmac_alloc(i);
82  // RFC 4231 test vectors
83  test(hmac, key1, sizeof(key1), data1, sizeof(data1));
84  test(hmac, key2, sizeof(key2), data2, sizeof(data2));
85  test(hmac, key3, 20, data3, sizeof(data3));
86  test(hmac, key3, sizeof(key3), data4, sizeof(data4));
87  test(hmac, key3, sizeof(key3), data6, sizeof(data6));
88  av_hmac_free(hmac);
89  i++;
90  }
91  return 0;
92 }
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:153
int main(void)
Definition: hmac.c:41
AVHMAC * av_hmac_alloc(enum AVHMACType type)
Allocate an AVHMAC context.
Definition: hmac.c:52
AVHMACType
Definition: hmac.h:32
uint8_t
static void test(AVHMAC *hmac, const uint8_t *key, int keylen, const uint8_t *data, int datalen)
Definition: hmac.c:24
const char data[16]
Definition: mxf.c:70
Definition: hmac.c:32
#define MAX_HASHLEN
Definition: hmac.c:29
void av_hmac_free(AVHMAC *c)
Free an AVHMAC context.
Definition: hmac.c:101
FILE * out
Definition: movenc.c:54