FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/hmac.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 97 105 92.4%
Functions: 11 11 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_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 48 DEFINE_SHA(160)
63 12 DEFINE_SHA(224)
64 12 DEFINE_SHA(256)
65 12 DEFINE_SHA512(384)
66 12 DEFINE_SHA512(512)
67
68 21 AVHMAC *av_hmac_alloc(enum AVHMACType type)
69 {
70 21 AVHMAC *c = av_mallocz(sizeof(*c));
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!c)
72 return NULL;
73
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) {
74 1 case AV_HMAC_MD5:
75 1 c->blocklen = 64;
76 1 c->hashlen = 16;
77 1 c->init = (hmac_init) av_md5_init;
78 1 c->update = (hmac_update) av_md5_update;
79 1 c->final = (hmac_final) av_md5_final;
80 1 c->hash = av_md5_alloc();
81 1 break;
82 16 case AV_HMAC_SHA1:
83 16 c->blocklen = 64;
84 16 c->hashlen = 20;
85 16 c->init = sha160_init;
86 16 c->update = (hmac_update) av_sha_update;
87 16 c->final = (hmac_final) av_sha_final;
88 16 c->hash = av_sha_alloc();
89 16 break;
90 1 case AV_HMAC_SHA224:
91 1 c->blocklen = 64;
92 1 c->hashlen = 28;
93 1 c->init = sha224_init;
94 1 c->update = (hmac_update) av_sha_update;
95 1 c->final = (hmac_final) av_sha_final;
96 1 c->hash = av_sha_alloc();
97 1 break;
98 1 case AV_HMAC_SHA256:
99 1 c->blocklen = 64;
100 1 c->hashlen = 32;
101 1 c->init = sha256_init;
102 1 c->update = (hmac_update) av_sha_update;
103 1 c->final = (hmac_final) av_sha_final;
104 1 c->hash = av_sha_alloc();
105 1 break;
106 1 case AV_HMAC_SHA384:
107 1 c->blocklen = 128;
108 1 c->hashlen = 48;
109 1 c->init = sha384_init;
110 1 c->update = (hmac_update) av_sha512_update;
111 1 c->final = (hmac_final) av_sha512_final;
112 1 c->hash = av_sha512_alloc();
113 1 break;
114 1 case AV_HMAC_SHA512:
115 1 c->blocklen = 128;
116 1 c->hashlen = 64;
117 1 c->init = sha512_init;
118 1 c->update = (hmac_update) av_sha512_update;
119 1 c->final = (hmac_final) av_sha512_final;
120 1 c->hash = av_sha512_alloc();
121 1 break;
122 default:
123 av_free(c);
124 return NULL;
125 }
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!c->hash) {
127 av_free(c);
128 return NULL;
129 }
130 21 return c;
131 }
132
133 21 void av_hmac_free(AVHMAC *c)
134 {
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!c)
136 return;
137 21 av_freep(&c->hash);
138 21 av_free(c);
139 }
140
141 48 void av_hmac_init(AVHMAC *c, const uint8_t *key, unsigned int keylen)
142 {
143 int i;
144 uint8_t block[MAX_BLOCKLEN];
145
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 36 times.
48 if (keylen > c->blocklen) {
146 12 c->init(c->hash);
147 12 c->update(c->hash, key, keylen);
148 12 c->final(c->hash, c->key);
149 12 c->keylen = c->hashlen;
150 } else {
151 36 memcpy(c->key, key, keylen);
152 36 c->keylen = keylen;
153 }
154 48 c->init(c->hash);
155
2/2
✓ Branch 0 taken 1032 times.
✓ Branch 1 taken 48 times.
1080 for (i = 0; i < c->keylen; i++)
156 1032 block[i] = c->key[i] ^ 0x36;
157
2/2
✓ Branch 0 taken 2680 times.
✓ Branch 1 taken 48 times.
2728 for (i = c->keylen; i < c->blocklen; i++)
158 2680 block[i] = 0x36;
159 48 c->update(c->hash, block, c->blocklen);
160 48 }
161
162 57 void av_hmac_update(AVHMAC *c, const uint8_t *data, unsigned int len)
163 {
164 57 c->update(c->hash, data, len);
165 57 }
166
167 48 int av_hmac_final(AVHMAC *c, uint8_t *out, unsigned int outlen)
168 {
169 uint8_t block[MAX_BLOCKLEN];
170 int i;
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (outlen < c->hashlen)
172 return AVERROR(EINVAL);
173 48 c->final(c->hash, out);
174 48 c->init(c->hash);
175
2/2
✓ Branch 0 taken 1032 times.
✓ Branch 1 taken 48 times.
1080 for (i = 0; i < c->keylen; i++)
176 1032 block[i] = c->key[i] ^ 0x5C;
177
2/2
✓ Branch 0 taken 2680 times.
✓ Branch 1 taken 48 times.
2728 for (i = c->keylen; i < c->blocklen; i++)
178 2680 block[i] = 0x5C;
179 48 c->update(c->hash, block, c->blocklen);
180 48 c->update(c->hash, out, c->hashlen);
181 48 c->final(c->hash, out);
182 48 return c->hashlen;
183 }
184
185 30 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 30 av_hmac_init(c, key, keylen);
190 30 av_hmac_update(c, data, len);
191 30 return av_hmac_final(c, out, outlen);
192 }
193