Skip to content

Commit 4ef77dd

Browse files
Eric BiggersAlexei Starovoitov
authored andcommitted
libbpf: Replace AF_ALG with open coded SHA-256
Reimplement libbpf_sha256() using some basic SHA-256 C code. This eliminates the newly-added dependency on AF_ALG, which is a problematic UAPI that is not supported by all kernels. Make libbpf_sha256() return void, since it can no longer fail. This simplifies some callers. Also drop the unnecessary 'sha_out_sz' parameter. Finally, also fix the typo in "compute_sha_udpate_offsets". Fixes: c297fe3 ("libbpf: Implement SHA256 internal helper") Signed-off-by: Eric Biggers <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 15cf392 commit 4ef77dd

File tree

3 files changed

+98
-67
lines changed

3 files changed

+98
-67
lines changed

tools/lib/bpf/gen_loader.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ static void emit_sys_close_blob(struct bpf_gen *gen, int blob_off)
371371
__emit_sys_close(gen);
372372
}
373373

374-
static int compute_sha_udpate_offsets(struct bpf_gen *gen);
374+
static void compute_sha_update_offsets(struct bpf_gen *gen);
375375

376376
int bpf_gen__finish(struct bpf_gen *gen, int nr_progs, int nr_maps)
377377
{
@@ -399,11 +399,8 @@ int bpf_gen__finish(struct bpf_gen *gen, int nr_progs, int nr_maps)
399399
blob_fd_array_off(gen, i));
400400
emit(gen, BPF_MOV64_IMM(BPF_REG_0, 0));
401401
emit(gen, BPF_EXIT_INSN());
402-
if (OPTS_GET(gen->opts, gen_hash, false)) {
403-
gen->error = compute_sha_udpate_offsets(gen);
404-
if (gen->error)
405-
return gen->error;
406-
}
402+
if (OPTS_GET(gen->opts, gen_hash, false))
403+
compute_sha_update_offsets(gen);
407404

408405
pr_debug("gen: finish %s\n", errstr(gen->error));
409406
if (!gen->error) {
@@ -457,25 +454,20 @@ void bpf_gen__free(struct bpf_gen *gen)
457454
_val; \
458455
})
459456

460-
static int compute_sha_udpate_offsets(struct bpf_gen *gen)
457+
static void compute_sha_update_offsets(struct bpf_gen *gen)
461458
{
462459
__u64 sha[SHA256_DWORD_SIZE];
463460
__u64 sha_dw;
464-
int i, err;
461+
int i;
465462

466-
err = libbpf_sha256(gen->data_start, gen->data_cur - gen->data_start, sha, SHA256_DIGEST_LENGTH);
467-
if (err < 0) {
468-
pr_warn("sha256 computation of the metadata failed");
469-
return err;
470-
}
463+
libbpf_sha256(gen->data_start, gen->data_cur - gen->data_start, (__u8 *)sha);
471464
for (i = 0; i < SHA256_DWORD_SIZE; i++) {
472465
struct bpf_insn *insn =
473466
(struct bpf_insn *)(gen->insn_start + gen->hash_insn_offset[i]);
474467
sha_dw = tgt_endian(sha[i]);
475468
insn[0].imm = (__u32)sha_dw;
476469
insn[1].imm = sha_dw >> 32;
477470
}
478-
return 0;
479471
}
480472

481473
void bpf_gen__load_btf(struct bpf_gen *gen, const void *btf_raw_data,

tools/lib/bpf/libbpf.c

Lines changed: 91 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <linux/perf_event.h>
3636
#include <linux/bpf_perf_event.h>
3737
#include <linux/ring_buffer.h>
38+
#include <linux/unaligned.h>
3839
#include <sys/epoll.h>
3940
#include <sys/ioctl.h>
4041
#include <sys/mman.h>
@@ -43,9 +44,6 @@
4344
#include <sys/vfs.h>
4445
#include <sys/utsname.h>
4546
#include <sys/resource.h>
46-
#include <sys/socket.h>
47-
#include <linux/if_alg.h>
48-
#include <linux/socket.h>
4947
#include <libelf.h>
5048
#include <gelf.h>
5149
#include <zlib.h>
@@ -4491,7 +4489,7 @@ bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx)
44914489
static int bpf_prog_compute_hash(struct bpf_program *prog)
44924490
{
44934491
struct bpf_insn *purged;
4494-
int i, err;
4492+
int i, err = 0;
44954493

44964494
purged = calloc(prog->insns_cnt, BPF_INSN_SZ);
44974495
if (!purged)
@@ -4519,8 +4517,8 @@ static int bpf_prog_compute_hash(struct bpf_program *prog)
45194517
purged[i].imm = 0;
45204518
}
45214519
}
4522-
err = libbpf_sha256(purged, prog->insns_cnt * sizeof(struct bpf_insn),
4523-
prog->hash, SHA256_DIGEST_LENGTH);
4520+
libbpf_sha256(purged, prog->insns_cnt * sizeof(struct bpf_insn),
4521+
prog->hash);
45244522
out:
45254523
free(purged);
45264524
return err;
@@ -14288,58 +14286,99 @@ void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
1428814286
free(s);
1428914287
}
1429014288

14291-
int libbpf_sha256(const void *data, size_t data_sz, void *sha_out, size_t sha_out_sz)
14289+
static inline __u32 ror32(__u32 v, int bits)
1429214290
{
14293-
struct sockaddr_alg sa = {
14294-
.salg_family = AF_ALG,
14295-
.salg_type = "hash",
14296-
.salg_name = "sha256"
14297-
};
14298-
int sock_fd = -1;
14299-
int op_fd = -1;
14300-
int err = 0;
14291+
return (v >> bits) | (v << (32 - bits));
14292+
}
1430114293

14302-
if (sha_out_sz != SHA256_DIGEST_LENGTH) {
14303-
pr_warn("sha_out_sz should be exactly 32 bytes for a SHA256 digest");
14304-
return -EINVAL;
14305-
}
14294+
#define SHA256_BLOCK_LENGTH 64
14295+
#define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
14296+
#define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
14297+
#define Sigma_0(x) (ror32((x), 2) ^ ror32((x), 13) ^ ror32((x), 22))
14298+
#define Sigma_1(x) (ror32((x), 6) ^ ror32((x), 11) ^ ror32((x), 25))
14299+
#define sigma_0(x) (ror32((x), 7) ^ ror32((x), 18) ^ ((x) >> 3))
14300+
#define sigma_1(x) (ror32((x), 17) ^ ror32((x), 19) ^ ((x) >> 10))
1430614301

14307-
sock_fd = socket(AF_ALG, SOCK_SEQPACKET, 0);
14308-
if (sock_fd < 0) {
14309-
err = -errno;
14310-
pr_warn("failed to create AF_ALG socket for SHA256: %s\n", errstr(err));
14311-
return err;
14312-
}
14302+
static const __u32 sha256_K[64] = {
14303+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
14304+
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
14305+
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
14306+
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
14307+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
14308+
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
14309+
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
14310+
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
14311+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
14312+
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
14313+
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
14314+
};
1431314315

14314-
if (bind(sock_fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
14315-
err = -errno;
14316-
pr_warn("failed to bind to AF_ALG socket for SHA256: %s\n", errstr(err));
14317-
goto out;
14318-
}
14316+
#define SHA256_ROUND(i, a, b, c, d, e, f, g, h) \
14317+
{ \
14318+
__u32 tmp = h + Sigma_1(e) + Ch(e, f, g) + sha256_K[i] + w[i]; \
14319+
d += tmp; \
14320+
h = tmp + Sigma_0(a) + Maj(a, b, c); \
14321+
}
14322+
14323+
static void sha256_blocks(__u32 state[8], const __u8 *data, size_t nblocks)
14324+
{
14325+
while (nblocks--) {
14326+
__u32 a = state[0];
14327+
__u32 b = state[1];
14328+
__u32 c = state[2];
14329+
__u32 d = state[3];
14330+
__u32 e = state[4];
14331+
__u32 f = state[5];
14332+
__u32 g = state[6];
14333+
__u32 h = state[7];
14334+
__u32 w[64];
14335+
int i;
14336+
14337+
for (i = 0; i < 16; i++)
14338+
w[i] = get_unaligned_be32(&data[4 * i]);
14339+
for (; i < ARRAY_SIZE(w); i++)
14340+
w[i] = sigma_1(w[i - 2]) + w[i - 7] +
14341+
sigma_0(w[i - 15]) + w[i - 16];
14342+
for (i = 0; i < ARRAY_SIZE(w); i += 8) {
14343+
SHA256_ROUND(i + 0, a, b, c, d, e, f, g, h);
14344+
SHA256_ROUND(i + 1, h, a, b, c, d, e, f, g);
14345+
SHA256_ROUND(i + 2, g, h, a, b, c, d, e, f);
14346+
SHA256_ROUND(i + 3, f, g, h, a, b, c, d, e);
14347+
SHA256_ROUND(i + 4, e, f, g, h, a, b, c, d);
14348+
SHA256_ROUND(i + 5, d, e, f, g, h, a, b, c);
14349+
SHA256_ROUND(i + 6, c, d, e, f, g, h, a, b);
14350+
SHA256_ROUND(i + 7, b, c, d, e, f, g, h, a);
14351+
}
14352+
state[0] += a;
14353+
state[1] += b;
14354+
state[2] += c;
14355+
state[3] += d;
14356+
state[4] += e;
14357+
state[5] += f;
14358+
state[6] += g;
14359+
state[7] += h;
14360+
data += SHA256_BLOCK_LENGTH;
14361+
}
14362+
}
14363+
14364+
void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH])
14365+
{
14366+
__u32 state[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
14367+
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
14368+
const __be64 bitcount = cpu_to_be64((__u64)len * 8);
14369+
__u8 final_data[2 * SHA256_BLOCK_LENGTH] = { 0 };
14370+
size_t final_len = len % SHA256_BLOCK_LENGTH;
14371+
int i;
1431914372

14320-
op_fd = accept(sock_fd, NULL, 0);
14321-
if (op_fd < 0) {
14322-
err = -errno;
14323-
pr_warn("failed to accept from AF_ALG socket for SHA256: %s\n", errstr(err));
14324-
goto out;
14325-
}
14373+
sha256_blocks(state, data, len / SHA256_BLOCK_LENGTH);
1432614374

14327-
if (write(op_fd, data, data_sz) != data_sz) {
14328-
err = -errno;
14329-
pr_warn("failed to write data to AF_ALG socket for SHA256: %s\n", errstr(err));
14330-
goto out;
14331-
}
14375+
memcpy(final_data, data + len - final_len, final_len);
14376+
final_data[final_len] = 0x80;
14377+
final_len = round_up(final_len + 9, SHA256_BLOCK_LENGTH);
14378+
memcpy(&final_data[final_len - 8], &bitcount, 8);
1433214379

14333-
if (read(op_fd, sha_out, SHA256_DIGEST_LENGTH) != SHA256_DIGEST_LENGTH) {
14334-
err = -errno;
14335-
pr_warn("failed to read SHA256 from AF_ALG socket: %s\n", errstr(err));
14336-
goto out;
14337-
}
14380+
sha256_blocks(state, final_data, final_len / SHA256_BLOCK_LENGTH);
1433814381

14339-
out:
14340-
if (op_fd >= 0)
14341-
close(op_fd);
14342-
if (sock_fd >= 0)
14343-
close(sock_fd);
14344-
return err;
14382+
for (i = 0; i < ARRAY_SIZE(state); i++)
14383+
put_unaligned_be32(state[i], &out[4 * i]);
1434514384
}

tools/lib/bpf/libbpf_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,5 +739,5 @@ int probe_fd(int fd);
739739
#define SHA256_DIGEST_LENGTH 32
740740
#define SHA256_DWORD_SIZE SHA256_DIGEST_LENGTH / sizeof(__u64)
741741

742-
int libbpf_sha256(const void *data, size_t data_sz, void *sha_out, size_t sha_out_sz);
742+
void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH]);
743743
#endif /* __LIBBPF_LIBBPF_INTERNAL_H */

0 commit comments

Comments
 (0)