Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2950961pxk; Mon, 21 Sep 2020 01:01:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwLfr7HAB5Z31QYB9KHH/CPItLC5agiOnOL1PmPFsZ0BmVjJaTiX0DJcKA12s5nUJmiAh70 X-Received: by 2002:aa7:d04d:: with SMTP id n13mr52660122edo.354.1600675271151; Mon, 21 Sep 2020 01:01:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600675271; cv=none; d=google.com; s=arc-20160816; b=AyfnzMVLSqDFQM+PE0p6wF024ypl/PBcOdwI9ruoLlUWq1xWIocEXSp0CpTuLrlXl+ pQ0uEdV7jAvva0oJaqMHK59yG2I8Cc2Ffg5WnpgBKh7jHCr7JXpbPGzZm7AmPE2DS179 eFLeFgCZ2bCnXDVWZAXIIfVXflf/FttKEV5JvrzPveauj3Tmz3V115YIEnmYx/amZHnN Yx6S+EpHGnTrXlCrbKGDwBz0ocDppQAvpxuaJCAlrqyycNil0XdE77bz6TAV8YZD8O4h /KNdwlrWjj80hSR5vas8peVYIFtnJwE4ml5JHXzT2L+N50TGHZfW6qmIrS6tRLV8DrY5 vTAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=BNsY2S12mKOL8P5O2VnAvfZqYQvwHtCdppSnqmRTMg4=; b=fFKSus8rC2i9Fi4m8VAjzFB0N70R+sAlbKlJDWOdAnyYrP0XffBp7R3UkBigInrEQj 2xN2HbvRAiEqr9NroEbCuOIEnIa88S2khzUgaYcO+2bac9jPDK6wAbToTd3Q9YdyJJfZ 5ZaXx3Ndjumy8uS6x7wk6RVaQ8GgZAhOJWyKDY2rX+1kseCgRuQGdZ2PPl0AGwkKWcy3 5Ha5/dUw046dTcW3ub+9pGzV8SC2YQKq9F/8vyqz/YcgsqMcLe6b4kjWiRoBnTB26Q5F BxlnnnNRSecpRYUl4vtXRypP9wB2DRQ3VCyuPWLiSaYM1CuXAnfGVU8YQS7DENYKVs/b FPOg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i21si7589549edv.439.2020.09.21.01.00.47; Mon, 21 Sep 2020 01:01:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726620AbgIUH7u (ORCPT + 99 others); Mon, 21 Sep 2020 03:59:50 -0400 Received: from mx2.suse.de ([195.135.220.15]:57622 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726524AbgIUH71 (ORCPT ); Mon, 21 Sep 2020 03:59:27 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 2A891B510; Mon, 21 Sep 2020 08:00:00 +0000 (UTC) From: Nicolai Stange To: "Theodore Y. Ts'o" Cc: linux-crypto@vger.kernel.org, LKML , Arnd Bergmann , Greg Kroah-Hartman , "Eric W. Biederman" , "Alexander E. Patrakov" , "Ahmed S. Darwish" , Willy Tarreau , Matthew Garrett , Vito Caputo , Andreas Dilger , Jan Kara , Ray Strode , William Jon McCann , zhangjs , Andy Lutomirski , Florian Weimer , Lennart Poettering , Peter Matthias , Marcelo Henrique Cerri , Roman Drahtmueller , Neil Horman , Randy Dunlap , Julia Lawall , Dan Carpenter , Andy Lavr , Eric Biggers , "Jason A. Donenfeld" , =?UTF-8?q?Stephan=20M=C3=BCller?= , Torsten Duwe , Petr Tesarik , Nicolai Stange Subject: [RFC PATCH 19/41] random: reintroduce arch_has_random() + arch_has_random_seed() Date: Mon, 21 Sep 2020 09:58:35 +0200 Message-Id: <20200921075857.4424-20-nstange@suse.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200921075857.4424-1-nstange@suse.de> References: <20200921075857.4424-1-nstange@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org A future patch will introduce support for making up for a certain amount of lacking entropy in crng_reseed() by means of arch_get_random_long() or arch_get_random_seed_long() respectively. However, before even the tiniest bit of precious entropy is withdrawn from the input_pool, it should be checked if whether the current arch even has support for these. Reintroduce arch_has_random() + arch_has_random_seed() and implement them for arm64, powerpc, s390 and x86 as appropriate (yeah, I know this should go in separate commits, but this is part of a RFC series). Note that this more or less reverts commits 647f50d5d9d9 ("linux/random.h: Remove arch_has_random, arch_has_random_seed") cbac004995a0 ("powerpc: Remove arch_has_random, arch_has_random_seed") 5e054c820f59 ("s390: Remove arch_has_random, arch_has_random_seed") 5f2ed7f5b99b ("x86: Remove arch_has_random, arch_has_random_seed") Signed-off-by: Nicolai Stange --- arch/arm64/include/asm/archrandom.h | 25 ++++++++++++++++++------- arch/powerpc/include/asm/archrandom.h | 12 +++++++++++- arch/s390/include/asm/archrandom.h | 14 ++++++++++++-- arch/x86/include/asm/archrandom.h | 18 ++++++++++++++---- include/linux/random.h | 8 ++++++++ 5 files changed, 63 insertions(+), 14 deletions(-) diff --git a/arch/arm64/include/asm/archrandom.h b/arch/arm64/include/asm/archrandom.h index 44209f6146aa..055d18713db7 100644 --- a/arch/arm64/include/asm/archrandom.h +++ b/arch/arm64/include/asm/archrandom.h @@ -26,17 +26,13 @@ static inline bool __arm64_rndr(unsigned long *v) return ok; } -static inline bool __must_check arch_get_random_long(unsigned long *v) -{ - return false; -} -static inline bool __must_check arch_get_random_int(unsigned int *v) +static inline bool arch_has_random(void) { return false; } -static inline bool __must_check arch_get_random_seed_long(unsigned long *v) +static inline bool arch_has_random_seed(void) { /* * Only support the generic interface after we have detected @@ -44,7 +40,22 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v) * cpufeature code and with potential scheduling between CPUs * with and without the feature. */ - if (!cpus_have_const_cap(ARM64_HAS_RNG)) + return cpus_have_const_cap(ARM64_HAS_RNG); +} + +static inline bool __must_check arch_get_random_long(unsigned long *v) +{ + return false; +} + +static inline bool __must_check arch_get_random_int(unsigned int *v) +{ + return false; +} + +static inline bool __must_check arch_get_random_seed_long(unsigned long *v) +{ + if (!arch_has_random_seed()) return false; return __arm64_rndr(v); diff --git a/arch/powerpc/include/asm/archrandom.h b/arch/powerpc/include/asm/archrandom.h index 9a53e29680f4..47c2d74e7244 100644 --- a/arch/powerpc/include/asm/archrandom.h +++ b/arch/powerpc/include/asm/archrandom.h @@ -6,6 +6,16 @@ #include +static inline bool arch_has_random(void) +{ + return false; +} + +static inline bool arch_has_random_seed(void) +{ + return ppc_md.get_random_seed; +} + static inline bool __must_check arch_get_random_long(unsigned long *v) { return false; @@ -18,7 +28,7 @@ static inline bool __must_check arch_get_random_int(unsigned int *v) static inline bool __must_check arch_get_random_seed_long(unsigned long *v) { - if (ppc_md.get_random_seed) + if (arch_has_random_seed()) return ppc_md.get_random_seed(v); return false; diff --git a/arch/s390/include/asm/archrandom.h b/arch/s390/include/asm/archrandom.h index de61ce562052..18973845634c 100644 --- a/arch/s390/include/asm/archrandom.h +++ b/arch/s390/include/asm/archrandom.h @@ -21,6 +21,16 @@ extern atomic64_t s390_arch_random_counter; bool s390_arch_random_generate(u8 *buf, unsigned int nbytes); +static inline bool arch_has_random(void) +{ + return false; +} + +static inline bool arch_has_random_seed(void) +{ + return static_branch_likely(&s390_arch_random_available); +} + static inline bool __must_check arch_get_random_long(unsigned long *v) { return false; @@ -33,7 +43,7 @@ static inline bool __must_check arch_get_random_int(unsigned int *v) static inline bool __must_check arch_get_random_seed_long(unsigned long *v) { - if (static_branch_likely(&s390_arch_random_available)) { + if (arch_has_random_seed()) { return s390_arch_random_generate((u8 *)v, sizeof(*v)); } return false; @@ -41,7 +51,7 @@ static inline bool __must_check arch_get_random_seed_long(unsigned long *v) static inline bool __must_check arch_get_random_seed_int(unsigned int *v) { - if (static_branch_likely(&s390_arch_random_available)) { + if (arch_has_random_seed()) { return s390_arch_random_generate((u8 *)v, sizeof(*v)); } return false; diff --git a/arch/x86/include/asm/archrandom.h b/arch/x86/include/asm/archrandom.h index ebc248e49549..030f46c9e310 100644 --- a/arch/x86/include/asm/archrandom.h +++ b/arch/x86/include/asm/archrandom.h @@ -70,24 +70,34 @@ static inline bool __must_check rdseed_int(unsigned int *v) */ #ifdef CONFIG_ARCH_RANDOM +static inline bool arch_has_random(void) +{ + return static_cpu_has(X86_FEATURE_RDRAND); +} + +static inline bool arch_has_random_seed(void) +{ + return static_cpu_has(X86_FEATURE_RDSEED); +} + static inline bool __must_check arch_get_random_long(unsigned long *v) { - return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_long(v) : false; + return arch_has_random() ? rdrand_long(v) : false; } static inline bool __must_check arch_get_random_int(unsigned int *v) { - return static_cpu_has(X86_FEATURE_RDRAND) ? rdrand_int(v) : false; + return arch_has_random() ? rdrand_int(v) : false; } static inline bool __must_check arch_get_random_seed_long(unsigned long *v) { - return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_long(v) : false; + return arch_has_random_seed() ? rdseed_long(v) : false; } static inline bool __must_check arch_get_random_seed_int(unsigned int *v) { - return static_cpu_has(X86_FEATURE_RDSEED) ? rdseed_int(v) : false; + return arch_has_random_seed() ? rdseed_int(v) : false; } extern void x86_init_rdrand(struct cpuinfo_x86 *c); diff --git a/include/linux/random.h b/include/linux/random.h index f45b8be3e3c4..d4653422a0c7 100644 --- a/include/linux/random.h +++ b/include/linux/random.h @@ -120,6 +120,14 @@ unsigned long randomize_page(unsigned long start, unsigned long range); #ifdef CONFIG_ARCH_RANDOM # include #else +static inline bool arch_has_random(void) +{ + return false; +} +static inline bool arch_has_random_seed(void) +{ + return false; +} static inline bool __must_check arch_get_random_long(unsigned long *v) { return false; -- 2.26.2