Skip to content

Commit fa64158

Browse files
Eric BiggersKernel Patches Daemon
authored andcommitted
bpf: Use sha1() instead of sha1_transform() in bpf_prog_calc_tag()
Now that there's a proper SHA-1 library API, just use that instead of the low-level SHA-1 compression function. This eliminates the need for bpf_prog_calc_tag() to implement the SHA-1 padding itself. No functional change; the computed tags remain the same. Signed-off-by: Eric Biggers <[email protected]>
1 parent 71f5f86 commit fa64158

File tree

2 files changed

+9
-47
lines changed

2 files changed

+9
-47
lines changed

include/linux/filter.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -997,12 +997,6 @@ static inline u32 bpf_prog_insn_size(const struct bpf_prog *prog)
997997
return prog->len * sizeof(struct bpf_insn);
998998
}
999999

1000-
static inline u32 bpf_prog_tag_scratch_size(const struct bpf_prog *prog)
1001-
{
1002-
return round_up(bpf_prog_insn_size(prog) +
1003-
sizeof(__be64) + 1, SHA1_BLOCK_SIZE);
1004-
}
1005-
10061000
static inline unsigned int bpf_prog_size(unsigned int proglen)
10071001
{
10081002
return max(sizeof(struct bpf_prog),

kernel/bpf/core.c

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
#include <uapi/linux/btf.h>
21+
#include <crypto/sha1.h>
2122
#include <linux/filter.h>
2223
#include <linux/skbuff.h>
2324
#include <linux/vmalloc.h>
@@ -293,28 +294,19 @@ void __bpf_prog_free(struct bpf_prog *fp)
293294

294295
int bpf_prog_calc_tag(struct bpf_prog *fp)
295296
{
296-
const u32 bits_offset = SHA1_BLOCK_SIZE - sizeof(__be64);
297-
u32 raw_size = bpf_prog_tag_scratch_size(fp);
298-
u32 digest[SHA1_DIGEST_WORDS];
299-
u32 ws[SHA1_WORKSPACE_WORDS];
300-
u32 i, bsize, psize, blocks;
297+
size_t size = bpf_prog_insn_size(fp);
298+
u8 digest[SHA1_DIGEST_SIZE];
301299
struct bpf_insn *dst;
302300
bool was_ld_map;
303-
u8 *raw, *todo;
304-
__be32 *result;
305-
__be64 *bits;
301+
u32 i;
306302

307-
raw = vmalloc(raw_size);
308-
if (!raw)
303+
dst = vmalloc(size);
304+
if (!dst)
309305
return -ENOMEM;
310306

311-
sha1_init_raw(digest);
312-
memset(ws, 0, sizeof(ws));
313-
314307
/* We need to take out the map fd for the digest calculation
315308
* since they are unstable from user space side.
316309
*/
317-
dst = (void *)raw;
318310
for (i = 0, was_ld_map = false; i < fp->len; i++) {
319311
dst[i] = fp->insnsi[i];
320312
if (!was_ld_map &&
@@ -334,33 +326,9 @@ int bpf_prog_calc_tag(struct bpf_prog *fp)
334326
was_ld_map = false;
335327
}
336328
}
337-
338-
psize = bpf_prog_insn_size(fp);
339-
memset(&raw[psize], 0, raw_size - psize);
340-
raw[psize++] = 0x80;
341-
342-
bsize = round_up(psize, SHA1_BLOCK_SIZE);
343-
blocks = bsize / SHA1_BLOCK_SIZE;
344-
todo = raw;
345-
if (bsize - psize >= sizeof(__be64)) {
346-
bits = (__be64 *)(todo + bsize - sizeof(__be64));
347-
} else {
348-
bits = (__be64 *)(todo + bsize + bits_offset);
349-
blocks++;
350-
}
351-
*bits = cpu_to_be64((psize - 1) << 3);
352-
353-
while (blocks--) {
354-
sha1_transform(digest, todo, ws);
355-
todo += SHA1_BLOCK_SIZE;
356-
}
357-
358-
result = (__force __be32 *)digest;
359-
for (i = 0; i < SHA1_DIGEST_WORDS; i++)
360-
result[i] = cpu_to_be32(digest[i]);
361-
memcpy(fp->tag, result, sizeof(fp->tag));
362-
363-
vfree(raw);
329+
sha1((const u8 *)dst, size, digest);
330+
memcpy(fp->tag, digest, sizeof(fp->tag));
331+
vfree(dst);
364332
return 0;
365333
}
366334

0 commit comments

Comments
 (0)