History

avutil/hmac: avoid calling functions through pointer of invalid type

Add type removed function wrappers to resolve UB of calling function through pointer to incorrect function type.

Kacper Michajłow committed 1 year ago

Commit ce048771 parent 6f88b901 file · +41 −27Browse files

Unified · Split

libavutil/hmac.c+41−27
@@ -47,23 +47,37 @@ struct AVHMAC { · 46 unmodified lines
4747 int keylen;
4848 };
4949
50−#define DEFINE_SHA(bits) \
51−static av_cold void sha ## bits ##_init(void *ctx) \
50+#define DEFINE_ALGO_BITS_INIT(prefix, bits) \
51+static av_cold void prefix##bits##_init(void *ctx) \
5252 { \
53− av_sha_init(ctx, bits); \
53+ av_##prefix##_init(ctx, bits); \
5454 }
5555
56−#define DEFINE_SHA512(bits) \
57−static av_cold void sha ## bits ##_init(void *ctx) \
58−{ \
59− av_sha512_init(ctx, bits); \
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); \
6070 }
6171
62−DEFINE_SHA(160)
63−DEFINE_SHA(224)
64−DEFINE_SHA(256)
65−DEFINE_SHA512(384)
66−DEFINE_SHA512(512)
72+DEFINE_ALGO_INIT(md5)
73+DEFINE_ALGO_BITS_INIT(sha, 160)
74+DEFINE_ALGO_BITS_INIT(sha, 224)
75+DEFINE_ALGO_BITS_INIT(sha, 256)
76+DEFINE_ALGO_BITS_INIT(sha512, 384)
77+DEFINE_ALGO_BITS_INIT(sha512, 512)
78+DEFINE_ALGO(md5)
79+DEFINE_ALGO(sha)
80+DEFINE_ALGO(sha512)
6781
6882 AVHMAC *av_hmac_alloc(enum AVHMACType type)
6983 {
@@ -74,49 +88,49 @@ AVHMAC *av_hmac_alloc(enum AVHMACType type) · 4 unmodified lines
7488 case AV_HMAC_MD5:
7589 c->blocklen = 64;
7690 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;
91+ c->init = md5_init;
92+ c->update = md5_update;
93+ c->final = md5_final;
8094 c->hash = av_md5_alloc();
8195 break;
8296 case AV_HMAC_SHA1:
8397 c->blocklen = 64;
8498 c->hashlen = 20;
8599 c->init = sha160_init;
86− c->update = (hmac_update) av_sha_update;
87− c->final = (hmac_final) av_sha_final;
100+ c->update = sha_update;
101+ c->final = sha_final;
88102 c->hash = av_sha_alloc();
89103 break;
90104 case AV_HMAC_SHA224:
91105 c->blocklen = 64;
92106 c->hashlen = 28;
93107 c->init = sha224_init;
94− c->update = (hmac_update) av_sha_update;
95− c->final = (hmac_final) av_sha_final;
108+ c->update = sha_update;
109+ c->final = sha_final;
96110 c->hash = av_sha_alloc();
97111 break;
98112 case AV_HMAC_SHA256:
99113 c->blocklen = 64;
100114 c->hashlen = 32;
101115 c->init = sha256_init;
102− c->update = (hmac_update) av_sha_update;
103− c->final = (hmac_final) av_sha_final;
116+ c->update = sha_update;
117+ c->final = sha_final;
104118 c->hash = av_sha_alloc();
105119 break;
106120 case AV_HMAC_SHA384:
107121 c->blocklen = 128;
108122 c->hashlen = 48;
109− c->init = sha384_init;
110− c->update = (hmac_update) av_sha512_update;
111− c->final = (hmac_final) av_sha512_final;
123+ c->init = sha512384_init;
124+ c->update = sha512_update;
125+ c->final = sha512_final;
112126 c->hash = av_sha512_alloc();
113127 break;
114128 case AV_HMAC_SHA512:
115129 c->blocklen = 128;
116130 c->hashlen = 64;
117− c->init = sha512_init;
118− c->update = (hmac_update) av_sha512_update;
119− c->final = (hmac_final) av_sha512_final;
131+ c->init = sha512512_init;
132+ c->update = sha512_update;
133+ c->final = sha512_final;
120134 c->hash = av_sha512_alloc();
121135 break;
122136 default:

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t