From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-184.mta0.migadu.com (out-184.mta0.migadu.com [91.218.175.184]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C95E434F46F for ; Tue, 18 Nov 2025 12:13:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.184 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763468037; cv=none; b=G9fTU1sVqr7KW5oqaR6kNIveN9ftzHuVjuMQd8LQAsEPrTGKlF6t7wgbpUl5d7ImzWPWAxCJAFAY3kRfP5KTjqrOKE0uS5dmNCmlJR8bYsxYFKxHM7yW8BWwcN+fYWp0fotOP/02nx5kseg9Htqp11Qy+iN6178Fg46DBB6pfWQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763468037; c=relaxed/simple; bh=cldJh+D1bjbxu/2MTDXVRGp6C7nKPCHXwJINkvJZQcc=; h=Message-ID:Date:MIME-Version:Subject:To:References:From: In-Reply-To:Content-Type; b=SrIrl1nZ0H1791AusIgvhMED12D+PgApBl2SPjBcfhpIxvuWoDIr9gSIiAj6DEhKDaGXLwJmasTvl1wEIOaWpl5kY5ffrHlinpbZQ1+eqgoFbCt1SlwfnNO/fwPK6rld8XH6qascGT+CFqiziVPxItgkxTHHoJ2oWEnljWpofAI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=Kaa0j4bq; arc=none smtp.client-ip=91.218.175.184 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="Kaa0j4bq" Message-ID: <1f313f86-c0be-4d29-aa90-5c95afa92827@linux.dev> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1763468023; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DdzhoZH3NR8Ep51OQAMEdgwgttikfFQ5UBUROZBxzNg=; b=Kaa0j4bqQxsgRk3yOVkVUBromUSJlGF27wX7cBEMeDIIHSFMWkhrXPfqo1rZ4qmVnXHOvA +oGea/z+ucEjfGGyb5KYDGr0qJ6ocXNgk54dKbAo5HrB7WufagsOgMSRpnyt508UFe4HCd sbOXK+MEF1SvB/cuIVrxTT5CGlt3Zk8= Date: Tue, 18 Nov 2025 12:13:39 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Subject: Re: [PATCH bpf-next 1/4] bpf: Add SHA hash kfuncs for cryptographic hashing To: Daniel Hodges , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , "open list:BPF [CRYPTO]" , open list References: <20251117211413.1394-1-git@danielhodges.dev> <20251117211413.1394-2-git@danielhodges.dev> Content-Language: en-US X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Vadim Fedorenko In-Reply-To: <20251117211413.1394-2-git@danielhodges.dev> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_OUT On 17/11/2025 21:13, Daniel Hodges wrote: > Add three new kfuncs for computing cryptographic hashes in BPF programs: > - bpf_sha256_hash(): Computes SHA-256 hash (32-byte output) > - bpf_sha384_hash(): Computes SHA-384 hash (48-byte output) > - bpf_sha512_hash(): Computes SHA-512 hash (64-byte output) > > These kfuncs leverage the kernel's existing crypto library (sha256/sha384/ > sha512 functions) and use bpf_dynptr for safe memory access without risk > of page faults. The functions validate input parameters including checking > for read-only output buffers and ensuring sufficient buffer sizes. > > This enables BPF programs to compute cryptographic hashes for use cases > such as content verification, integrity checking, and data authentication. > > Signed-off-by: Daniel Hodges [...] > +#if IS_ENABLED(CONFIG_CRYPTO_LIB_SHA256) > +/** > + * bpf_sha256_hash() - Compute SHA-256 hash using kernel crypto library > + * @data: bpf_dynptr to the input data to hash. Must be a trusted pointer. > + * @out: bpf_dynptr to the output buffer (must be at least 32 bytes). Must be a trusted pointer. > + * > + * Computes SHA-256 hash of the input data. Uses bpf_dynptr to ensure safe memory access > + * without risk of page faults. > + */ > +__bpf_kfunc int bpf_sha256_hash(const struct bpf_dynptr *data, const struct bpf_dynptr *out) > +{ > + const struct bpf_dynptr_kern *data_kern = (struct bpf_dynptr_kern *)data; > + const struct bpf_dynptr_kern *out_kern = (struct bpf_dynptr_kern *)out; > + u32 data_len, out_len; > + const u8 *data_ptr; > + u8 *out_ptr; > + > + if (__bpf_dynptr_is_rdonly(out_kern)) > + return -EINVAL; __bpf_dynptr_data_rw() contains __bpf_dynptr_is_rdonly() check, no need to do it again explicitly. This applies to all helpers > + > + data_len = __bpf_dynptr_size(data_kern); > + out_len = __bpf_dynptr_size(out_kern); > + > + if (data_len == 0 || out_len < 32) > + return -EINVAL; > + > + data_ptr = __bpf_dynptr_data(data_kern, data_len); > + if (!data_ptr) > + return -EINVAL; > + > + out_ptr = __bpf_dynptr_data_rw(out_kern, out_len); > + if (!out_ptr) > + return -EINVAL; > + > + sha256(data_ptr, data_len, out_ptr); > + > + return 0; > +}