|  | 
| 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> | 
| 39 | 38 | #include <sys/epoll.h> | 
| 40 | 39 | #include <sys/ioctl.h> | 
| 41 | 40 | #include <sys/mman.h> | 
|  | 
| 51 | 50 | #include "libbpf.h" | 
| 52 | 51 | #include "bpf.h" | 
| 53 | 52 | #include "btf.h" | 
| 54 |  | -#include "str_error.h" | 
| 55 | 53 | #include "libbpf_internal.h" | 
| 56 | 54 | #include "hashmap.h" | 
| 57 | 55 | #include "bpf_gen_internal.h" | 
| @@ -319,8 +317,6 @@ static void pr_perm_msg(int err) | 
| 319 | 317 | 		buf); | 
| 320 | 318 | } | 
| 321 | 319 | 
 | 
| 322 |  | -#define STRERR_BUFSIZE  128 | 
| 323 |  | - | 
| 324 | 320 | /* Copied from tools/perf/util/util.h */ | 
| 325 | 321 | #ifndef zfree | 
| 326 | 322 | # define zfree(ptr) ({ free(*ptr); *ptr = NULL; }) | 
| @@ -14285,100 +14281,3 @@ void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s) | 
| 14285 | 14281 | 	free(s->progs); | 
| 14286 | 14282 | 	free(s); | 
| 14287 | 14283 | } | 
| 14288 |  | - | 
| 14289 |  | -static inline __u32 ror32(__u32 v, int bits) | 
| 14290 |  | -{ | 
| 14291 |  | -	return (v >> bits) | (v << (32 - bits)); | 
| 14292 |  | -} | 
| 14293 |  | - | 
| 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)) | 
| 14301 |  | - | 
| 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 |  | -}; | 
| 14315 |  | - | 
| 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; | 
| 14372 |  | - | 
| 14373 |  | -	sha256_blocks(state, data, len / SHA256_BLOCK_LENGTH); | 
| 14374 |  | - | 
| 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); | 
| 14379 |  | - | 
| 14380 |  | -	sha256_blocks(state, final_data, final_len / SHA256_BLOCK_LENGTH); | 
| 14381 |  | - | 
| 14382 |  | -	for (i = 0; i < ARRAY_SIZE(state); i++) | 
| 14383 |  | -		put_unaligned_be32(state[i], &out[4 * i]); | 
| 14384 |  | -} | 
0 commit comments