From patchwork Mon Jul 10 10:22:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 701964 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C36DCEB64D9 for ; Mon, 10 Jul 2023 10:23:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232798AbjGJKXf (ORCPT ); Mon, 10 Jul 2023 06:23:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231824AbjGJKX3 (ORCPT ); Mon, 10 Jul 2023 06:23:29 -0400 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D937B93; Mon, 10 Jul 2023 03:23:24 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-682eef7d752so438378b3a.0; Mon, 10 Jul 2023 03:23:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1688984603; x=1689589403; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eCSMJahyXmCvgvDzVjTHJgX6FPvVAG1dRHxwcI+b1M4=; b=Jm+G2W/vSOFtBrwWvqTPkva20krEpuoen+Jo5jFHmpYfh9RGcWNYXy/p6YHJ27G3NM NFiwZ8YY2dIecN6twcLi44rwS1onWTHpwueTMJQGEGTmYDI1bdJifHbDmlYyqB8xHdQN uiwdQagHJNFlQeUqDjiXUJAih2Yz1gNIdpqUMwGeoKBPXbmY98sTifpzcYRUZrpt6Qty xO9WQmtfmLVIEMJvOFO8SbB28De24yZLEeshbGfLN/1bTW8I752XJYc3BqWQN0s4ZlvT 0fHEvtFbm8WtehwG8wJrOWckdTP0gPTiD0i0NR9Yjl8tEir7VyDcnxKSx8U6ELzzEWpg 43EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688984603; x=1689589403; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eCSMJahyXmCvgvDzVjTHJgX6FPvVAG1dRHxwcI+b1M4=; b=mCT1Mo/efNGt307bqZE9PuxDdB4o0owuFNp68w4dL5bNgMRD1/kxhvCivr0N+FHk0q qXORo36oDerRWWrR0+OP2vXwX/CSRIFFuTQn7yC3f+BjF4iQ9K3gFi0JZEQjpm+p8MSz xW+/lzKYdYwDmvyo4h92ihTxrsVtBUYPoOxXh4qZSBUoXgn79+yvbNG50lD4GU0H3I9r 18MjPX/6KRsUO2VLopo5IwNh+vHZeFMgBBlfnuoRE2zi4SP0Mb/KLvWXBXu7fenqsbLo VBV/ZSvZHvjZp5o1GB4280IDzOp55hfZM2k6CJF91JAoCnAMa04NZ1qpFLeK/tEgPWqb b8LA== X-Gm-Message-State: ABy/qLYxl+oVVHUcCbGgNYS/RGYLSLU6oujOS1jX66tVLQ+9vx69ONnN QcFHzzp/0osHve+csevAmTT3IZY19uxe8GcM X-Google-Smtp-Source: APBJJlExBeLEy2uKwhHfSJufN+HVdZCqOEozIFPrNAwHf9n8zDFvR21ATY8bGkw/rPI0UBVpPg109A== X-Received: by 2002:a05:6a20:8e2a:b0:125:6443:4eb8 with SMTP id y42-20020a056a208e2a00b0012564434eb8mr16472763pzj.5.1688984602120; Mon, 10 Jul 2023 03:23:22 -0700 (PDT) Received: from ip-172-30-47-114.us-west-2.compute.internal (ec2-54-68-170-188.us-west-2.compute.amazonaws.com. [54.68.170.188]) by smtp.gmail.com with ESMTPSA id d5-20020a170902cec500b001b5656b0bf9sm7901984plg.286.2023.07.10.03.23.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jul 2023 03:23:21 -0700 (PDT) From: FUJITA Tomonori To: rust-for-linux@vger.kernel.org, linux-crypto@vger.kernel.org Cc: alex.gaynor@gmail.com, herbert@gondor.apana.org.au, ebiggers@kernel.org, benno.lossin@proton.me Subject: [PATCH v2 1/3] rust: crypto abstractions for synchronous message digest API Date: Mon, 10 Jul 2023 19:22:23 +0900 Message-Id: <20230710102225.155019-2-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230710102225.155019-1-fujita.tomonori@gmail.com> References: <20230710102225.155019-1-fujita.tomonori@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org This patch adds basic abstractions for synchronous message digest API, wrapping crypto_shash and shash_desc structures. Signed-off-by: FUJITA Tomonori --- rust/bindings/bindings_helper.h | 1 + rust/helpers.c | 26 +++++++ rust/kernel/crypto.rs | 5 ++ rust/kernel/crypto/hash.rs | 128 ++++++++++++++++++++++++++++++++ rust/kernel/lib.rs | 2 + 5 files changed, 162 insertions(+) create mode 100644 rust/kernel/crypto.rs create mode 100644 rust/kernel/crypto/hash.rs diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 3e601ce2548d..2f198c6d5de5 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -6,6 +6,7 @@ * Sorted alphabetically. */ +#include #include #include #include diff --git a/rust/helpers.c b/rust/helpers.c index bb594da56137..7966902ed8eb 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -18,6 +18,7 @@ * accidentally exposed. */ +#include #include #include #include @@ -28,6 +29,31 @@ #include #include +#ifdef CONFIG_CRYPTO +void rust_helper_crypto_free_shash(struct crypto_shash *tfm) +{ + crypto_free_shash(tfm); +} +EXPORT_SYMBOL_GPL(rust_helper_crypto_free_shash); + +unsigned int rust_helper_crypto_shash_digestsize(struct crypto_shash *tfm) +{ + return crypto_shash_digestsize(tfm); +} +EXPORT_SYMBOL_GPL(rust_helper_crypto_shash_digestsize); + +unsigned int rust_helper_crypto_shash_descsize(struct crypto_shash *tfm) +{ + return crypto_shash_descsize(tfm); +} +EXPORT_SYMBOL_GPL(rust_helper_crypto_shash_descsize); + +int rust_helper_crypto_shash_init(struct shash_desc *desc) { + return crypto_shash_init(desc); +} +EXPORT_SYMBOL_GPL(rust_helper_crypto_shash_init); +#endif + __noreturn void rust_helper_BUG(void) { BUG(); diff --git a/rust/kernel/crypto.rs b/rust/kernel/crypto.rs new file mode 100644 index 000000000000..f80dd7bd3381 --- /dev/null +++ b/rust/kernel/crypto.rs @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Cryptography. + +pub mod hash; diff --git a/rust/kernel/crypto/hash.rs b/rust/kernel/crypto/hash.rs new file mode 100644 index 000000000000..cdbc8e70e8f5 --- /dev/null +++ b/rust/kernel/crypto/hash.rs @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Cryptographic Hash operations. +//! +//! C headers: [`include/crypto/hash.h`](../../../../include/crypto/hash.h) + +use crate::{ + error::{ + code::{EINVAL, ENOMEM}, + from_err_ptr, to_result, Result, + }, + str::CStr, +}; +use alloc::alloc::{alloc, dealloc}; +use core::alloc::Layout; + +/// Corresponds to the kernel's `struct crypto_shash`. +/// +/// # Invariants +/// +/// The pointer is valid. +pub struct Shash(*mut bindings::crypto_shash); + +impl Drop for Shash { + fn drop(&mut self) { + // SAFETY: The type invariant guarantees that the pointer is valid. + unsafe { bindings::crypto_free_shash(self.0) } + } +} + +impl Shash { + /// Creates a [`Shash`] object for a message digest handle. + pub fn new(name: &CStr, t: u32, mask: u32) -> Result { + // SAFETY: There are no safety requirements for this FFI call. + let ptr = + unsafe { from_err_ptr(bindings::crypto_alloc_shash(name.as_char_ptr(), t, mask)) }?; + // INVARIANT: `ptr` is valid and non-null since `crypto_alloc_shash` + // returned a valid pointer which was null-checked. + Ok(Self(ptr)) + } + + /// Sets optional key used by the hashing algorithm. + pub fn setkey(&mut self, data: &[u8]) -> Result { + // SAFETY: The type invariant guarantees that the pointer is valid. + to_result(unsafe { + bindings::crypto_shash_setkey(self.0, data.as_ptr(), data.len() as u32) + }) + } + + /// Returns the size of the result of the transformation. + pub fn digestsize(&self) -> u32 { + // SAFETY: The type invariant guarantees that the pointer is valid. + unsafe { bindings::crypto_shash_digestsize(self.0) } + } +} + +/// Corresponds to the kernel's `struct shash_desc`. +/// +/// # Invariants +/// +/// The field `ptr` is valid. +pub struct ShashDesc<'a> { + ptr: *mut bindings::shash_desc, + tfm: &'a Shash, + size: usize, +} + +impl Drop for ShashDesc<'_> { + fn drop(&mut self) { + // SAFETY: The type invariant guarantees that the pointer is valid. + unsafe { + dealloc( + self.ptr.cast(), + Layout::from_size_align(self.size, 2).unwrap(), + ); + } + } +} + +impl<'a> ShashDesc<'a> { + /// Creates a [`ShashDesc`] object for a request data structure for message digest. + pub fn new(tfm: &'a Shash) -> Result { + // SAFETY: The type invariant guarantees that `tfm.0` pointer is valid. + let size = core::mem::size_of::() + + unsafe { bindings::crypto_shash_descsize(tfm.0) } as usize; + let layout = Layout::from_size_align(size, 2)?; + // SAFETY: It's safe because layout has non-zero size. + let ptr = unsafe { alloc(layout) } as *mut bindings::shash_desc; + if ptr.is_null() { + return Err(ENOMEM); + } + // INVARIANT: `ptr` is valid and non-null since `alloc` + // returned a valid pointer which was null-checked. + let mut desc = ShashDesc { ptr, tfm, size }; + // SAFETY: `desc.ptr` is valid and non-null since `alloc` + // returned a valid pointer which was null-checked. + // Additionally, The type invariant guarantees that `tfm.0` is valid. + unsafe { (*desc.ptr).tfm = desc.tfm.0 }; + desc.reset()?; + Ok(desc) + } + + /// Re-initializes message digest. + pub fn reset(&mut self) -> Result { + // SAFETY: The type invariant guarantees that the pointer is valid. + to_result(unsafe { bindings::crypto_shash_init(self.ptr) }) + } + + /// Adds data to message digest for processing. + pub fn update(&mut self, data: &[u8]) -> Result { + if data.len() > u32::MAX as usize { + return Err(EINVAL); + } + // SAFETY: The type invariant guarantees that the pointer is valid. + to_result(unsafe { + bindings::crypto_shash_update(self.ptr, data.as_ptr(), data.len() as u32) + }) + } + + /// Calculates message digest. + pub fn finalize(&mut self, output: &mut [u8]) -> Result { + if self.tfm.digestsize() as usize > output.len() { + return Err(EINVAL); + } + // SAFETY: The type invariant guarantees that the pointer is valid. + to_result(unsafe { bindings::crypto_shash_final(self.ptr, output.as_mut_ptr()) }) + } +} diff --git a/rust/kernel/lib.rs b/rust/kernel/lib.rs index 85b261209977..3cb8bd8a17d9 100644 --- a/rust/kernel/lib.rs +++ b/rust/kernel/lib.rs @@ -31,6 +31,8 @@ #[cfg(not(testlib))] mod allocator; mod build_assert; +#[cfg(CONFIG_CRYPTO)] +pub mod crypto; pub mod error; pub mod init; pub mod ioctl; From patchwork Mon Jul 10 10:22:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 701291 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E8A2FC001DF for ; Mon, 10 Jul 2023 10:23:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231637AbjGJKX3 (ORCPT ); Mon, 10 Jul 2023 06:23:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230345AbjGJKX2 (ORCPT ); Mon, 10 Jul 2023 06:23:28 -0400 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9948A0; Mon, 10 Jul 2023 03:23:25 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1b9d9cbcc70so1918345ad.0; Mon, 10 Jul 2023 03:23:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1688984604; x=1689589404; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fDDML1x+MMF/uMtkdte/RNq+k0jZQz0suwas+90hCNw=; b=i/kva2Tak8H5rfnw0Uqy+oIWWb41reuOkAhnRN2xav0y+7O340keD8JjfrIQBgfnHe ahSqmyKxsM2lgJveYCslnlhpA8PViJIM7UnCMhJkcdVAFVauyTHcS21OitY5QRGFxnvL 1NZlJ3voNwZHUn0XaZ9f4xjMrO8GyI5ceDkg0j8sXrF8nS1RDDGOHB2me1BhQYQPXQ8N mJ16E9HlzNOTQr8RLKGy40kXpKLLIgu53KluIoj0o5QkKrxoYJU9JNACjRwAMRTXrpWJ 7V9ce9W3tNFj6S6XRUSliab7xaP006ko4TZYV7S8fW6WHM+EorRHPQdCmvzLoM0Lg3Fz c19w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688984604; x=1689589404; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fDDML1x+MMF/uMtkdte/RNq+k0jZQz0suwas+90hCNw=; b=Rh1hyGLqH1dMfRmDPUHurbPeS6BBYsUK5CJeT1QZ/spN2A3n3UKW6o4bR2+uwInN98 uvSR/xTOQwErlhm0mEWXz7Rd8phSmO0mlQWGWauIWq8yyyVKFwmmuzOzZyyu53URw5bd EMN5mJl8ovQ3StTRuEUP3A+UwphZ08AkHd1Avb2+0kLkhpBra0ZykobtfqJU/rw870ZP kgPk0JbF2D+zf0cVkVWNmDjPM3qVT76y2oJR07/u8rCnBV2POsq9aPHnglviAmmeBIzu 9GYcgP4/xo/S+clMn+J8EE1JqXg1ZF8BC8XLwSvIPZz6jy7dizD9ByLmKIyELpJwNarX eE3A== X-Gm-Message-State: ABy/qLYI5XMJoKhAloLlXH1Uj242bwzP9HctqdB8on2XELlY5kTNYfLa WbCgQzFXcTjFVaq9M96zSXDFFcPC2NFkp/by X-Google-Smtp-Source: APBJJlGkdEjDl0j/ds/nznUVO/o5zJ0h1QZm/ARL0SqmBQd1HJohPk7rJICbuKtni/H2p87yYE0o6Q== X-Received: by 2002:a17:902:ec88:b0:1ae:4567:2737 with SMTP id x8-20020a170902ec8800b001ae45672737mr15777953plg.2.1688984604096; Mon, 10 Jul 2023 03:23:24 -0700 (PDT) Received: from ip-172-30-47-114.us-west-2.compute.internal (ec2-54-68-170-188.us-west-2.compute.amazonaws.com. [54.68.170.188]) by smtp.gmail.com with ESMTPSA id d5-20020a170902cec500b001b5656b0bf9sm7901984plg.286.2023.07.10.03.23.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jul 2023 03:23:23 -0700 (PDT) From: FUJITA Tomonori To: rust-for-linux@vger.kernel.org, linux-crypto@vger.kernel.org Cc: alex.gaynor@gmail.com, herbert@gondor.apana.org.au, ebiggers@kernel.org, benno.lossin@proton.me Subject: [PATCH v2 2/3] rust: crypto abstractions for random number generator API Date: Mon, 10 Jul 2023 19:22:24 +0900 Message-Id: <20230710102225.155019-3-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230710102225.155019-1-fujita.tomonori@gmail.com> References: <20230710102225.155019-1-fujita.tomonori@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org This patch adds basic abstractions for random number generator API, wrapping crypto_rng structure. Signed-off-by: FUJITA Tomonori --- rust/bindings/bindings_helper.h | 1 + rust/helpers.c | 12 ++++ rust/kernel/crypto.rs | 1 + rust/kernel/crypto/rng.rs | 101 ++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 rust/kernel/crypto/rng.rs diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h index 2f198c6d5de5..089ac38c6461 100644 --- a/rust/bindings/bindings_helper.h +++ b/rust/bindings/bindings_helper.h @@ -7,6 +7,7 @@ */ #include +#include #include #include #include diff --git a/rust/helpers.c b/rust/helpers.c index 7966902ed8eb..e4dcd611738f 100644 --- a/rust/helpers.c +++ b/rust/helpers.c @@ -19,6 +19,7 @@ */ #include +#include #include #include #include @@ -52,6 +53,17 @@ int rust_helper_crypto_shash_init(struct shash_desc *desc) { return crypto_shash_init(desc); } EXPORT_SYMBOL_GPL(rust_helper_crypto_shash_init); + +void rust_helper_crypto_free_rng(struct crypto_rng *tfm) { + crypto_free_rng(tfm); +} +EXPORT_SYMBOL_GPL(rust_helper_crypto_free_rng); + +int rust_helper_crypto_rng_generate(struct crypto_rng *tfm, const u8 *src, + unsigned int slen, u8 *dst, unsigned int dlen) { + return crypto_rng_generate(tfm, src, slen, dst, dlen); +} +EXPORT_SYMBOL_GPL(rust_helper_crypto_rng_generate); #endif __noreturn void rust_helper_BUG(void) diff --git a/rust/kernel/crypto.rs b/rust/kernel/crypto.rs index f80dd7bd3381..a1995e6c85d4 100644 --- a/rust/kernel/crypto.rs +++ b/rust/kernel/crypto.rs @@ -3,3 +3,4 @@ //! Cryptography. pub mod hash; +pub mod rng; diff --git a/rust/kernel/crypto/rng.rs b/rust/kernel/crypto/rng.rs new file mode 100644 index 000000000000..683f5ee464ce --- /dev/null +++ b/rust/kernel/crypto/rng.rs @@ -0,0 +1,101 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Random number generator. +//! +//! C headers: [`include/crypto/rng.h`](../../../../include/crypto/rng.h) + +use crate::{ + error::{code::EINVAL, from_err_ptr, to_result, Result}, + str::CStr, +}; + +/// Type of Random number generator. +/// +/// # Invariants +/// +/// The pointer is valid. +enum RngType { + /// Uses `crypto_default_rng` + // We don't need to keep an pointer for the default but simpler. + Default(*mut bindings::crypto_rng), + + /// Allocated via `crypto_alloc_rng. + Allocated(*mut bindings::crypto_rng), +} + +/// Corresponds to the kernel's `struct crypto_rng`. +pub struct Rng(RngType); + +impl Drop for Rng { + fn drop(&mut self) { + match self.0 { + RngType::Default(_) => { + // SAFETY: it's safe because `crypto_get_default_rng()` was called during + // the initialization. + unsafe { + bindings::crypto_put_default_rng(); + } + } + RngType::Allocated(ptr) => { + // SAFETY: The type invariants of `RngType` guarantees that the pointer is valid. + unsafe { bindings::crypto_free_rng(ptr) }; + } + } + } +} + +impl Rng { + /// Creates a [`Rng`] instance. + pub fn new(name: &CStr, t: u32, mask: u32) -> Result { + // SAFETY: There are no safety requirements for this FFI call. + let ptr = unsafe { from_err_ptr(bindings::crypto_alloc_rng(name.as_char_ptr(), t, mask)) }?; + // INVARIANT: `ptr` is valid and non-null since `crypto_alloc_rng` + // returned a valid pointer which was null-checked. + Ok(Self(RngType::Allocated(ptr))) + } + + /// Creates a [`Rng`] instance with a default algorithm. + pub fn new_with_default() -> Result { + // SAFETY: There are no safety requirements for this FFI call. + to_result(unsafe { bindings::crypto_get_default_rng() })?; + // INVARIANT: The C API guarantees that `crypto_default_rng` is valid until + // `crypto_put_default_rng` is called. + Ok(Self(RngType::Default(unsafe { + bindings::crypto_default_rng + }))) + } + + /// Get a random number. + pub fn generate(&mut self, src: &[u8], dst: &mut [u8]) -> Result { + if src.len() > u32::MAX as usize || dst.len() > u32::MAX as usize { + return Err(EINVAL); + } + let ptr = match self.0 { + RngType::Default(ptr) => ptr, + RngType::Allocated(ptr) => ptr, + }; + // SAFETY: The type invariants of `RngType' guarantees that the pointer is valid. + to_result(unsafe { + bindings::crypto_rng_generate( + ptr, + src.as_ptr(), + src.len() as u32, + dst.as_mut_ptr(), + dst.len() as u32, + ) + }) + } + + /// Re-initializes the [`Rng`] instance. + pub fn reset(&mut self, seed: &[u8]) -> Result { + if seed.len() > u32::MAX as usize { + return Err(EINVAL); + } + let ptr = match self.0 { + RngType::Default(ptr) => ptr, + RngType::Allocated(ptr) => ptr, + }; + // SAFETY: The type invariants of `RngType' guarantees that the pointer is valid. + to_result(unsafe { bindings::crypto_rng_reset(ptr, seed.as_ptr(), seed.len() as u32) }) + } +} From patchwork Mon Jul 10 10:22:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: FUJITA Tomonori X-Patchwork-Id: 701965 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 16588EB64DA for ; Mon, 10 Jul 2023 10:23:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231542AbjGJKX2 (ORCPT ); Mon, 10 Jul 2023 06:23:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36914 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230169AbjGJKX1 (ORCPT ); Mon, 10 Jul 2023 06:23:27 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7FC0AA4; Mon, 10 Jul 2023 03:23:26 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1b867f9198dso7642255ad.0; Mon, 10 Jul 2023 03:23:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1688984605; x=1691576605; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/bES5zK8LfFOykRC97jWvEVozpQHjO1n1TwlTrs0pDo=; b=XJd1AdkP5i26E0jRIx43SZEhT8iTIXZSdJDgQ2egpeJTMEz0Fp7BEVXrVgOmyfQUaS h6050GANKD90idk2a1dKLzG0l6HFqJ6UoYINEDdwEcSuJQsiVxR9impwkrjOH/j/FhYS VY7HYHkeWVmWws6SMulYGty2qLAtbcyQhieMQ3H11JqFvabvwWEMo9c4yX8rT13LJFF3 /jfHFjIfHcch8FWvDh9vzn34KaBb+cGH/7I2x/kpazrANXaqjPnoFTE3vr6hZWgduCLy e4PUgsckjol7nyaAx5anIslsHJt4Pad+6rRruNBYbDPQy86F2kSeynGI6HwvuBob7gXi PXJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688984605; x=1691576605; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/bES5zK8LfFOykRC97jWvEVozpQHjO1n1TwlTrs0pDo=; b=KPFhQpgxKtpfPur1kig1oP+Yq9OHmaHlVX61pG9Whn9XOrU5XnDsgKFSg2EGPcGO+k KgnsbkHZC5sNBvkxy68MqZfyHl5aquVodxITtVl1LxMIUIyHAHzj6Vgx5MwUhJ6kPIG7 HNXhPU/UAeOB+yJ+LWcj0BaNYzs6sKWWd0wT3axQb8+OTAOOhTFxN7uPz14VcIIYe8wH teTTb6CuwGAvH+j37w0rl4clfmCq42RJXXhgQAonja0QhSYiCE2oEzL63sWDABHsVrno NXhvl0AgxQwXqEt2XVwtc9y+LInxvOy6kTgas7Pu7QeitpcQ04bxdodoa9R25rmfSoZp cXxg== X-Gm-Message-State: ABy/qLZM53CpcJUTr28fwsNjnjejhJjtWKCfvCO4P7pX9CLqu+isAF48 DfeF24zWk3dT9EJbqO/lJykKMx9u/YRetvY5 X-Google-Smtp-Source: APBJJlF8mNENavwfmHPm4sg9nfRrfTMpQA0coeANubDmtepTsYFki89FZdm9IZeY8EqIWZvm2KOMYg== X-Received: by 2002:a17:902:e809:b0:1b8:1591:9f81 with SMTP id u9-20020a170902e80900b001b815919f81mr16160703plg.4.1688984605206; Mon, 10 Jul 2023 03:23:25 -0700 (PDT) Received: from ip-172-30-47-114.us-west-2.compute.internal (ec2-54-68-170-188.us-west-2.compute.amazonaws.com. [54.68.170.188]) by smtp.gmail.com with ESMTPSA id d5-20020a170902cec500b001b5656b0bf9sm7901984plg.286.2023.07.10.03.23.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jul 2023 03:23:24 -0700 (PDT) From: FUJITA Tomonori To: rust-for-linux@vger.kernel.org, linux-crypto@vger.kernel.org Cc: alex.gaynor@gmail.com, herbert@gondor.apana.org.au, ebiggers@kernel.org, benno.lossin@proton.me Subject: [PATCH v2 3/3] MAINTAINERS: add Rust crypto abstractions files to the CRYPTO API entry Date: Mon, 10 Jul 2023 19:22:25 +0900 Message-Id: <20230710102225.155019-4-fujita.tomonori@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230710102225.155019-1-fujita.tomonori@gmail.com> References: <20230710102225.155019-1-fujita.tomonori@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The files are placed at rust/kernel/ directory for now but the files are likely to be moved to crypto/ directory if things go well. Signed-off-by: FUJITA Tomonori --- MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 250518fc70ff..3dd33850ff7e 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5446,6 +5446,8 @@ F: drivers/crypto/ F: include/crypto/ F: include/linux/crypto* F: lib/crypto/ +F: rust/kernel/crypto.rs +F: rust/kernel/crypto/ CRYPTOGRAPHIC RANDOM NUMBER GENERATOR M: Neil Horman