FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/hmac.c
Date: 2025-07-28 20:30:09
Exec Total Coverage
Lines: 101 109 92.7%
Functions: 18 18 100.0%
Branches: 20 25 80.0%

Line Branch Exec Source
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;
42 int blocklen, hashlen;
43 hmac_final final;
44 hmac_update update;
45 hmac_init init;
46 uint8_t key[MAX_BLOCKLEN];
47 int keylen;
48 };
49
50 #define DEFINE_ALGO_BITS_INIT(prefix, bits) \
51 static av_cold void prefix##bits##_init(void *ctx) \
52 { \
53 av_##prefix##_init(ctx, bits); \
54 }
55
56 #define DEFINE_ALGO_INIT(prefix) \
57 static av_cold void prefix##_init(void *ctx) \
58 { \
59 av_##prefix##_init(ctx); \
60 }
61
62 #define DEFINE_ALGO(prefix) \
63 static void prefix##_update(void *ctx, const uint8_t *src, size_t len) \
64 { \
65 av_##prefix##_update(ctx, src, len); \
66 } \
67 static void prefix##_final(void *ctx, uint8_t *dst) \
68 { \
69 av_##prefix##_final(ctx, dst); \
70 }
71
72 12 DEFINE_ALGO_INIT(md5)
73 48 DEFINE_ALGO_BITS_INIT(sha, 160)
74 12 DEFINE_ALGO_BITS_INIT(sha, 224)
75 12 DEFINE_ALGO_BITS_INIT(sha, 256)
76 12 DEFINE_ALGO_BITS_INIT(sha512, 384)
77 12 DEFINE_ALGO_BITS_INIT(sha512, 512)
78 68 DEFINE_ALGO(md5)
79 438 DEFINE_ALGO(sha)
80 136 DEFINE_ALGO(sha512)
81
82 21 AVHMAC *av_hmac_alloc(enum AVHMACType type)
83 {
84 21 AVHMAC *c = av_mallocz(sizeof(*c));
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!c)
86 return NULL;
87
6/7
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
21 switch (type) {
88 1 case AV_HMAC_MD5:
89 1 c->blocklen = 64;
90 1 c->hashlen = 16;
91 1 c->init = md5_init;
92 1 c->update = md5_update;
93 1 c->final = md5_final;
94 1 c->hash = av_md5_alloc();
95 1 break;
96 16 case AV_HMAC_SHA1:
97 16 c->blocklen = 64;
98 16 c->hashlen = 20;
99 16 c->init = sha160_init;
100 16 c->update = sha_update;
101 16 c->final = sha_final;
102 16 c->hash = av_sha_alloc();
103 16 break;
104 1 case AV_HMAC_SHA224:
105 1 c->blocklen = 64;
106 1 c->hashlen = 28;
107 1 c->init = sha224_init;
108 1 c->update = sha_update;
109 1 c->final = sha_final;
110 1 c->hash = av_sha_alloc();
111 1 break;
112 1 case AV_HMAC_SHA256:
113 1 c->blocklen = 64;
114 1 c->hashlen = 32;
115 1 c->init = sha256_init;
116 1 c->update = sha_update;
117 1 c->final = sha_final;
118 1 c->hash = av_sha_alloc();
119 1 break;
120 1 case AV_HMAC_SHA384:
121 1 c->blocklen = 128;
122 1 c->hashlen = 48;
123 1 c->init = sha512384_init;
124 1 c->update = sha512_update;
125 1 c->final = sha512_final;
126 1 c->hash = av_sha512_alloc();
127 1 break;
128 1 case AV_HMAC_SHA512:
129 1 c->blocklen = 128;
130 1 c->hashlen = 64;
131 1 c->init = sha512512_init;
132 1 c->update = sha512_update;
133 1 c->final = sha512_final;
134 1 c->hash = av_sha512_alloc();
135 1 break;
136 default:
137 av_free(c);
138 return NULL;
139 }
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!c->hash) {
141 av_free(c);
142 return NULL;
143 }
144 21 return c;
145 }
146
147 21 void av_hmac_free(AVHMAC *c)
148 {
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!c)
150 return;
151 21 av_freep(&c->hash);
152 21 av_free(c);
153 }
154
155 48 void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
156 {
157 int i;
158 uint8_t block[MAX_BLOCKLEN];
159
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 36 times.
48 if (keylen > c->blocklen) {
160 12 c->init(c->hash);
161 12 c->update(c->hash, key, keylen);
162 12 c->final(c->hash, c->key);
163 12 c->keylen = c->hashlen;
164 } else {
165 36 memcpy(c->key, key, keylen);
166 36 c->keylen = keylen;
167 }
168 48 c->init(c->hash);
169
2/2
✓ Branch 0 taken 1032 times.
✓ Branch 1 taken 48 times.
1080 for (i = 0; i < c->keylen; i++)
170 1032 block[i] = c->key[i] ^ 0x36;
171
2/2
✓ Branch 0 taken 2680 times.
✓ Branch 1 taken 48 times.
2728 for (i = c->keylen; i < c->blocklen; i++)
172 2680 block[i] = 0x36;
173 48 c->update(c->hash, block, c->blocklen);
174 48 }
175
176 57 void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
177 {
178 57 c->update(c->hash, data, len);
179 57 }
180
181 48 int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
182 {
183 uint8_t block[MAX_BLOCKLEN];
184 int i;
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (outlen < c->hashlen)
186 return AVERROR(EINVAL);
187 48 c->final(c->hash, out);
188 48 c->init(c->hash);
189
2/2
✓ Branch 0 taken 1032 times.
✓ Branch 1 taken 48 times.
1080 for (i = 0; i < c->keylen; i++)
190 1032 block[i] = c->key[i] ^ 0x5C;
191
2/2
✓ Branch 0 taken 2680 times.
✓ Branch 1 taken 48 times.
2728 for (i = c->keylen; i < c->blocklen; i++)
192 2680 block[i] = 0x5C;
193 48 c->update(c->hash, block, c->blocklen);
194 48 c->update(c->hash, out, c->hashlen);
195 48 c->final(c->hash, out);
196 48 return c->hashlen;
197 }
198
199 30 int av_hmac_calc(AVHMAC *c, const uint8_t *data, unsigned int len,
200 const uint8_t *key, unsigned int keylen,
201 uint8_t *out, unsigned int outlen)
202 {
203 30 av_hmac_init(c, key, keylen);
204 30 av_hmac_update(c, data, len);
205 30 return av_hmac_final(c, out, outlen);
206 }
207