|
43 | 43 | #include <sys/vfs.h>
|
44 | 44 | #include <sys/utsname.h>
|
45 | 45 | #include <sys/resource.h>
|
| 46 | +#include <sys/socket.h> |
| 47 | +#include <linux/if_alg.h> |
| 48 | +#include <linux/socket.h> |
46 | 49 | #include <libelf.h>
|
47 | 50 | #include <gelf.h>
|
48 | 51 | #include <zlib.h>
|
@@ -14207,3 +14210,59 @@ void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
|
14207 | 14210 | free(s->progs);
|
14208 | 14211 | free(s);
|
14209 | 14212 | }
|
| 14213 | + |
| 14214 | +int libbpf_sha256(const void *data, size_t data_sz, void *sha_out, size_t sha_out_sz) |
| 14215 | +{ |
| 14216 | + struct sockaddr_alg sa = { |
| 14217 | + .salg_family = AF_ALG, |
| 14218 | + .salg_type = "hash", |
| 14219 | + .salg_name = "sha256" |
| 14220 | + }; |
| 14221 | + int sock_fd = -1; |
| 14222 | + int op_fd = -1; |
| 14223 | + int err = 0; |
| 14224 | + |
| 14225 | + if (sha_out_sz != SHA256_DIGEST_LENGTH) { |
| 14226 | + pr_warn("sha_out_sz should be exactly 32 bytes for a SHA256 digest"); |
| 14227 | + return libbpf_err(-EINVAL); |
| 14228 | + } |
| 14229 | + |
| 14230 | + sock_fd = socket(AF_ALG, SOCK_SEQPACKET, 0); |
| 14231 | + if (sock_fd < 0) { |
| 14232 | + err = -errno; |
| 14233 | + pr_warn("failed to create AF_ALG socket for SHA256: %s\n", errstr(err)); |
| 14234 | + return libbpf_err(err); |
| 14235 | + } |
| 14236 | + |
| 14237 | + if (bind(sock_fd, (struct sockaddr *)&sa, sizeof(sa)) < 0) { |
| 14238 | + err = -errno; |
| 14239 | + pr_warn("failed to bind to AF_ALG socket for SHA256: %s\n", errstr(err)); |
| 14240 | + goto out; |
| 14241 | + } |
| 14242 | + |
| 14243 | + op_fd = accept(sock_fd, NULL, 0); |
| 14244 | + if (op_fd < 0) { |
| 14245 | + err = -errno; |
| 14246 | + pr_warn("failed to accept from AF_ALG socket for SHA256: %s\n", errstr(err)); |
| 14247 | + goto out; |
| 14248 | + } |
| 14249 | + |
| 14250 | + if (write(op_fd, data, data_sz) != data_sz) { |
| 14251 | + err = -errno; |
| 14252 | + pr_warn("failed to write data to AF_ALG socket for SHA256: %s\n", errstr(err)); |
| 14253 | + goto out; |
| 14254 | + } |
| 14255 | + |
| 14256 | + if (read(op_fd, sha_out, SHA256_DIGEST_LENGTH) != SHA256_DIGEST_LENGTH) { |
| 14257 | + err = -errno; |
| 14258 | + pr_warn("failed to read SHA256 from AF_ALG socket: %s\n", errstr(err)); |
| 14259 | + goto out; |
| 14260 | + } |
| 14261 | + |
| 14262 | +out: |
| 14263 | + if (op_fd >= 0) |
| 14264 | + close(op_fd); |
| 14265 | + if (sock_fd >= 0) |
| 14266 | + close(sock_fd); |
| 14267 | + return libbpf_err(err); |
| 14268 | +} |
0 commit comments