Skip to content

Commit a7f36f8

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: move libbpf_sha256() implementation into libbpf_utils.c
Move sha256 implementation out of already large and unwieldy libbpf.c into libbpf_utils.c where we'll keep reusable helpers. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/r/20251001171326.3883055-5-andrii@kernel.org Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Eduard Zingerman <eddyz87@gmail.com>
1 parent c68b6fd commit a7f36f8

File tree

3 files changed

+100
-98
lines changed

3 files changed

+100
-98
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <linux/perf_event.h>
3636
#include <linux/bpf_perf_event.h>
3737
#include <linux/ring_buffer.h>
38-
#include <linux/unaligned.h>
3938
#include <sys/epoll.h>
4039
#include <sys/ioctl.h>
4140
#include <sys/mman.h>
@@ -14282,100 +14281,3 @@ void bpf_object__destroy_skeleton(struct bpf_object_skeleton *s)
1428214281
free(s->progs);
1428314282
free(s);
1428414283
}
14285-
14286-
static inline __u32 ror32(__u32 v, int bits)
14287-
{
14288-
return (v >> bits) | (v << (32 - bits));
14289-
}
14290-
14291-
#define SHA256_BLOCK_LENGTH 64
14292-
#define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
14293-
#define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
14294-
#define Sigma_0(x) (ror32((x), 2) ^ ror32((x), 13) ^ ror32((x), 22))
14295-
#define Sigma_1(x) (ror32((x), 6) ^ ror32((x), 11) ^ ror32((x), 25))
14296-
#define sigma_0(x) (ror32((x), 7) ^ ror32((x), 18) ^ ((x) >> 3))
14297-
#define sigma_1(x) (ror32((x), 17) ^ ror32((x), 19) ^ ((x) >> 10))
14298-
14299-
static const __u32 sha256_K[64] = {
14300-
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
14301-
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
14302-
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
14303-
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
14304-
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
14305-
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
14306-
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
14307-
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
14308-
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
14309-
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
14310-
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
14311-
};
14312-
14313-
#define SHA256_ROUND(i, a, b, c, d, e, f, g, h) \
14314-
{ \
14315-
__u32 tmp = h + Sigma_1(e) + Ch(e, f, g) + sha256_K[i] + w[i]; \
14316-
d += tmp; \
14317-
h = tmp + Sigma_0(a) + Maj(a, b, c); \
14318-
}
14319-
14320-
static void sha256_blocks(__u32 state[8], const __u8 *data, size_t nblocks)
14321-
{
14322-
while (nblocks--) {
14323-
__u32 a = state[0];
14324-
__u32 b = state[1];
14325-
__u32 c = state[2];
14326-
__u32 d = state[3];
14327-
__u32 e = state[4];
14328-
__u32 f = state[5];
14329-
__u32 g = state[6];
14330-
__u32 h = state[7];
14331-
__u32 w[64];
14332-
int i;
14333-
14334-
for (i = 0; i < 16; i++)
14335-
w[i] = get_unaligned_be32(&data[4 * i]);
14336-
for (; i < ARRAY_SIZE(w); i++)
14337-
w[i] = sigma_1(w[i - 2]) + w[i - 7] +
14338-
sigma_0(w[i - 15]) + w[i - 16];
14339-
for (i = 0; i < ARRAY_SIZE(w); i += 8) {
14340-
SHA256_ROUND(i + 0, a, b, c, d, e, f, g, h);
14341-
SHA256_ROUND(i + 1, h, a, b, c, d, e, f, g);
14342-
SHA256_ROUND(i + 2, g, h, a, b, c, d, e, f);
14343-
SHA256_ROUND(i + 3, f, g, h, a, b, c, d, e);
14344-
SHA256_ROUND(i + 4, e, f, g, h, a, b, c, d);
14345-
SHA256_ROUND(i + 5, d, e, f, g, h, a, b, c);
14346-
SHA256_ROUND(i + 6, c, d, e, f, g, h, a, b);
14347-
SHA256_ROUND(i + 7, b, c, d, e, f, g, h, a);
14348-
}
14349-
state[0] += a;
14350-
state[1] += b;
14351-
state[2] += c;
14352-
state[3] += d;
14353-
state[4] += e;
14354-
state[5] += f;
14355-
state[6] += g;
14356-
state[7] += h;
14357-
data += SHA256_BLOCK_LENGTH;
14358-
}
14359-
}
14360-
14361-
void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH])
14362-
{
14363-
__u32 state[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
14364-
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
14365-
const __be64 bitcount = cpu_to_be64((__u64)len * 8);
14366-
__u8 final_data[2 * SHA256_BLOCK_LENGTH] = { 0 };
14367-
size_t final_len = len % SHA256_BLOCK_LENGTH;
14368-
int i;
14369-
14370-
sha256_blocks(state, data, len / SHA256_BLOCK_LENGTH);
14371-
14372-
memcpy(final_data, data + len - final_len, final_len);
14373-
final_data[final_len] = 0x80;
14374-
final_len = round_up(final_len + 9, SHA256_BLOCK_LENGTH);
14375-
memcpy(&final_data[final_len - 8], &bitcount, 8);
14376-
14377-
sha256_blocks(state, final_data, final_len / SHA256_BLOCK_LENGTH);
14378-
14379-
for (i = 0; i < ARRAY_SIZE(state); i++)
14380-
put_unaligned_be32(state[i], &out[4 * i]);
14381-
}

tools/lib/bpf/libbpf_internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,11 @@ static inline bool is_pow_of_2(size_t x)
722722
return x && (x & (x - 1)) == 0;
723723
}
724724

725+
static inline __u32 ror32(__u32 v, int bits)
726+
{
727+
return (v >> bits) | (v << (32 - bits));
728+
}
729+
725730
#define PROG_LOAD_ATTEMPTS 5
726731
int sys_bpf_prog_load(union bpf_attr *attr, unsigned int size, int attempts);
727732

tools/lib/bpf/libbpf_utils.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#include <stdio.h>
1212
#include <string.h>
1313
#include <errno.h>
14+
#include <inttypes.h>
15+
#include <linux/kernel.h>
16+
#include <linux/unaligned.h>
1417

1518
#include "libbpf.h"
1619
#include "libbpf_internal.h"
@@ -145,3 +148,95 @@ const char *libbpf_errstr(int err)
145148
return buf;
146149
}
147150
}
151+
152+
#define SHA256_BLOCK_LENGTH 64
153+
#define Ch(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
154+
#define Maj(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
155+
#define Sigma_0(x) (ror32((x), 2) ^ ror32((x), 13) ^ ror32((x), 22))
156+
#define Sigma_1(x) (ror32((x), 6) ^ ror32((x), 11) ^ ror32((x), 25))
157+
#define sigma_0(x) (ror32((x), 7) ^ ror32((x), 18) ^ ((x) >> 3))
158+
#define sigma_1(x) (ror32((x), 17) ^ ror32((x), 19) ^ ((x) >> 10))
159+
160+
static const __u32 sha256_K[64] = {
161+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
162+
0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
163+
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
164+
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
165+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
166+
0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
167+
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
168+
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
169+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
170+
0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
171+
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
172+
};
173+
174+
#define SHA256_ROUND(i, a, b, c, d, e, f, g, h) \
175+
{ \
176+
__u32 tmp = h + Sigma_1(e) + Ch(e, f, g) + sha256_K[i] + w[i]; \
177+
d += tmp; \
178+
h = tmp + Sigma_0(a) + Maj(a, b, c); \
179+
}
180+
181+
static void sha256_blocks(__u32 state[8], const __u8 *data, size_t nblocks)
182+
{
183+
while (nblocks--) {
184+
__u32 a = state[0];
185+
__u32 b = state[1];
186+
__u32 c = state[2];
187+
__u32 d = state[3];
188+
__u32 e = state[4];
189+
__u32 f = state[5];
190+
__u32 g = state[6];
191+
__u32 h = state[7];
192+
__u32 w[64];
193+
int i;
194+
195+
for (i = 0; i < 16; i++)
196+
w[i] = get_unaligned_be32(&data[4 * i]);
197+
for (; i < ARRAY_SIZE(w); i++)
198+
w[i] = sigma_1(w[i - 2]) + w[i - 7] +
199+
sigma_0(w[i - 15]) + w[i - 16];
200+
for (i = 0; i < ARRAY_SIZE(w); i += 8) {
201+
SHA256_ROUND(i + 0, a, b, c, d, e, f, g, h);
202+
SHA256_ROUND(i + 1, h, a, b, c, d, e, f, g);
203+
SHA256_ROUND(i + 2, g, h, a, b, c, d, e, f);
204+
SHA256_ROUND(i + 3, f, g, h, a, b, c, d, e);
205+
SHA256_ROUND(i + 4, e, f, g, h, a, b, c, d);
206+
SHA256_ROUND(i + 5, d, e, f, g, h, a, b, c);
207+
SHA256_ROUND(i + 6, c, d, e, f, g, h, a, b);
208+
SHA256_ROUND(i + 7, b, c, d, e, f, g, h, a);
209+
}
210+
state[0] += a;
211+
state[1] += b;
212+
state[2] += c;
213+
state[3] += d;
214+
state[4] += e;
215+
state[5] += f;
216+
state[6] += g;
217+
state[7] += h;
218+
data += SHA256_BLOCK_LENGTH;
219+
}
220+
}
221+
222+
void libbpf_sha256(const void *data, size_t len, __u8 out[SHA256_DIGEST_LENGTH])
223+
{
224+
__u32 state[8] = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
225+
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
226+
const __be64 bitcount = cpu_to_be64((__u64)len * 8);
227+
__u8 final_data[2 * SHA256_BLOCK_LENGTH] = { 0 };
228+
size_t final_len = len % SHA256_BLOCK_LENGTH;
229+
int i;
230+
231+
sha256_blocks(state, data, len / SHA256_BLOCK_LENGTH);
232+
233+
memcpy(final_data, data + len - final_len, final_len);
234+
final_data[final_len] = 0x80;
235+
final_len = round_up(final_len + 9, SHA256_BLOCK_LENGTH);
236+
memcpy(&final_data[final_len - 8], &bitcount, 8);
237+
238+
sha256_blocks(state, final_data, final_len / SHA256_BLOCK_LENGTH);
239+
240+
for (i = 0; i < ARRAY_SIZE(state); i++)
241+
put_unaligned_be32(state[i], &out[4 * i]);
242+
}

0 commit comments

Comments
 (0)