From patchwork Wed Jan 16 11:07:34 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephan Mueller X-Patchwork-Id: 10765763 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D9F9E13BF for ; Wed, 16 Jan 2019 11:12:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C97532C440 for ; Wed, 16 Jan 2019 11:12:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BD9C82D4BE; Wed, 16 Jan 2019 11:12:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4FC7F2C279 for ; Wed, 16 Jan 2019 11:12:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392401AbfAPLMY (ORCPT ); Wed, 16 Jan 2019 06:12:24 -0500 Received: from mo4-p03-ob.smtp.rzone.de ([85.215.255.104]:26740 "EHLO mo4-p03-ob.smtp.rzone.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389696AbfAPLMC (ORCPT ); Wed, 16 Jan 2019 06:12:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1547637119; s=strato-dkim-0002; d=chronox.de; h=References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: X-RZG-CLASS-ID:X-RZG-AUTH:From:Subject:Sender; bh=pSfduHW8di1A56/Fcff8gBYrw/e7p9TbH/mFZ8R2KMQ=; b=P/TRbXXWA4LEkLQ0T188OtZ1APiZ4lc6V0l7tgCR/oP9igZvP0eCy3F0/I/s+oRFHu DpMoNhUo9xSnGQal7JKvF4tnFtUfHUHkAm1DBgHcBS9fyWCd0WLbBEM0/c17P5E/zc7t Hu+j3rxQnSplrFXmRpkasddFEF17lxwRZ54dKgG2ZmkGys93dVseJHhLvOAvVBoms8ms TGuTmH6jpskSxaERLroDx1vC57HSHiEECPDOD7Mu16Z4yYN10v39crpU0tAAfzIqOvL0 nk5gqpwUhHTv5FlQsxJbroHYMHod/VUAi85TlT/IM74tm0Hf54vwUMMgkjpKhu6QHXXp I6nQ== X-RZG-AUTH: ":P2ERcEykfu11Y98lp/T7+hdri+uKZK8TKWEqNyiHySGSa9k9yWgdNs16dfA/c7fW145n" X-RZG-CLASS-ID: mo00 Received: from positron.chronox.de by smtp.strato.de (RZmta 44.9 AUTH) with ESMTPSA id 309bcfv0GBBg4Ne (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (curve secp521r1 with 521 ECDH bits, eq. 15360 bits RSA)) (Client did not present a certificate); Wed, 16 Jan 2019 12:11:42 +0100 (CET) From: Stephan =?iso-8859-1?q?M=FCller?= To: Herbert Xu Cc: Eric Biggers , James Bottomley , Andy Lutomirski , "Lee, Chun-Yi" , "Rafael J . Wysocki" , Pavel Machek , linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, keyrings@vger.kernel.org, "Rafael J. Wysocki" , Chen Yu , Oliver Neukum , Ryan Chen , David Howells , Giovanni Gherdovich , Randy Dunlap , Jann Horn , Andy Lutomirski , linux-crypto@vger.kernel.org Subject: [PATCH v2 1/6] crypto: add template handling for RNGs Date: Wed, 16 Jan 2019 12:07:34 +0100 Message-ID: <61907509.O7QvflLhaU@positron.chronox.de> In-Reply-To: <2082192.jPI8ve1O8G@positron.chronox.de> References: <20190103143227.9138-1-jlee@suse.com> <9733066.Vrs4h5eWcW@positron.chronox.de> <2082192.jPI8ve1O8G@positron.chronox.de> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Adds ability to register templates for pseudo random number generators (PRNG). PRNGs are "meta" mechanisms using raw cipher primitives. Thus, PRNGs can now be implemented as templates to allow the complete flexibility the kernel crypto API provides. The RNG API provides access to the PRNG algorithms without an entropy management. Signed-off-by: Stephan Mueller --- crypto/rng.c | 44 +++++++++++++++++++++++++++++++++++ include/crypto/internal/rng.h | 26 +++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/crypto/rng.c b/crypto/rng.c index 33c38a72bff5..da4fd03c0acd 100644 --- a/crypto/rng.c +++ b/crypto/rng.c @@ -64,6 +64,25 @@ static int crypto_rng_init_tfm(struct crypto_tfm *tfm) return 0; } +static inline struct rng_alg *__crypto_rng_alg(struct crypto_alg *alg) +{ + return container_of(alg, struct rng_alg, base); +} + +static inline struct rng_instance *rng_instance( + struct crypto_instance *inst) +{ + return container_of(__crypto_rng_alg(&inst->alg), + struct rng_instance, alg); +} + +static void crypto_rng_free_instance(struct crypto_instance *inst) +{ + struct rng_instance *rng = rng_instance(inst); + + rng->free(rng); +} + static unsigned int seedsize(struct crypto_alg *alg) { struct rng_alg *ralg = container_of(alg, struct rng_alg, base); @@ -102,6 +121,7 @@ static void crypto_rng_show(struct seq_file *m, struct crypto_alg *alg) static const struct crypto_type crypto_rng_type = { .extsize = crypto_alg_extsize, .init_tfm = crypto_rng_init_tfm, + .free = crypto_rng_free_instance, #ifdef CONFIG_PROC_FS .show = crypto_rng_show, #endif @@ -229,5 +249,29 @@ void crypto_unregister_rngs(struct rng_alg *algs, int count) } EXPORT_SYMBOL_GPL(crypto_unregister_rngs); +static int rng_prepare_alg(struct rng_alg *alg) +{ + struct crypto_alg *base = &alg->base; + + base->cra_type = &crypto_rng_type; + base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK; + base->cra_flags |= CRYPTO_ALG_TYPE_RNG; + + return 0; +} + +int rng_register_instance(struct crypto_template *tmpl, + struct rng_instance *inst) +{ + int err; + + err = rng_prepare_alg(&inst->alg); + if (err) + return err; + + return crypto_register_instance(tmpl, rng_crypto_instance(inst)); +} +EXPORT_SYMBOL_GPL(rng_register_instance); + MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Random Number Generator"); diff --git a/include/crypto/internal/rng.h b/include/crypto/internal/rng.h index a52ef3483dd7..bfe4482ad336 100644 --- a/include/crypto/internal/rng.h +++ b/include/crypto/internal/rng.h @@ -42,4 +42,30 @@ static inline void crypto_rng_set_entropy(struct crypto_rng *tfm, crypto_rng_alg(tfm)->set_ent(tfm, data, len); } +struct rng_instance { + void (*free)(struct rng_instance *inst); + struct rng_alg alg; +}; + +static inline struct rng_instance *rng_alloc_instance( + const char *name, struct crypto_alg *alg) +{ + return crypto_alloc_instance(name, alg, + sizeof(struct rng_instance) - sizeof(*alg)); +} + +static inline struct crypto_instance *rng_crypto_instance( + struct rng_instance *inst) +{ + return container_of(&inst->alg.base, struct crypto_instance, alg); +} + +static inline void *rng_instance_ctx(struct rng_instance *inst) +{ + return crypto_instance_ctx(rng_crypto_instance(inst)); +} + +int rng_register_instance(struct crypto_template *tmpl, + struct rng_instance *inst); + #endif