|  | 
| 35 | 35 | #include <linux/perf_event.h> | 
| 36 | 36 | #include <linux/bpf_perf_event.h> | 
| 37 | 37 | #include <linux/ring_buffer.h> | 
|  | 38 | +#include <linux/unaligned.h> | 
| 38 | 39 | #include <sys/epoll.h> | 
| 39 | 40 | #include <sys/ioctl.h> | 
| 40 | 41 | #include <sys/mman.h> | 
|  | 
| 43 | 44 | #include <sys/vfs.h> | 
| 44 | 45 | #include <sys/utsname.h> | 
| 45 | 46 | #include <sys/resource.h> | 
| 46 |  | -#include <sys/socket.h> | 
| 47 |  | -#include <linux/if_alg.h> | 
| 48 |  | -#include <linux/socket.h> | 
| 49 | 47 | #include <libelf.h> | 
| 50 | 48 | #include <gelf.h> | 
| 51 | 49 | #include <zlib.h> | 
| @@ -4491,7 +4489,7 @@ bpf_object__section_to_libbpf_map_type(const struct bpf_object *obj, int shndx) | 
| 4491 | 4489 | static int bpf_prog_compute_hash(struct bpf_program *prog) | 
| 4492 | 4490 | { | 
| 4493 | 4491 | 	struct bpf_insn *purged; | 
| 4494 |  | -	int i, err; | 
|  | 4492 | +	int i, err = 0; | 
| 4495 | 4493 | 
 | 
| 4496 | 4494 | 	purged = calloc(prog->insns_cnt, BPF_INSN_SZ); | 
| 4497 | 4495 | 	if (!purged) | 
| @@ -4519,8 +4517,8 @@ static int bpf_prog_compute_hash(struct bpf_program *prog) | 
| 4519 | 4517 | 			purged[i].imm = 0; | 
| 4520 | 4518 | 		} | 
| 4521 | 4519 | 	} | 
| 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); | 
| 4524 | 4522 | out: | 
| 4525 | 4523 | 	free(purged); | 
| 4526 | 4524 | 	return err; | 
| @@ -14288,58 +14286,99 @@ void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s) | 
| 14288 | 14286 | 	free(s); | 
| 14289 | 14287 | } | 
| 14290 | 14288 | 
 | 
| 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) | 
| 14292 | 14290 | { | 
| 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 | +} | 
| 14301 | 14293 | 
 | 
| 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)) | 
| 14306 | 14301 | 
 | 
| 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 | +}; | 
| 14313 | 14315 | 
 | 
| 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; | 
| 14319 | 14372 | 
 | 
| 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); | 
| 14326 | 14374 | 
 | 
| 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); | 
| 14332 | 14379 | 
 | 
| 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); | 
| 14338 | 14381 | 
 | 
| 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]); | 
| 14345 | 14384 | } | 
0 commit comments