Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp463113pxb; Thu, 5 Nov 2020 05:01:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJz9OcbvsVLQzXRu5tMC4nuH4zfvUaKGYq9B8orITCqZoRsZhn0qSXOVGxzaeasG1fpYDgdV X-Received: by 2002:a50:d8ce:: with SMTP id y14mr2509603edj.90.1604581263012; Thu, 05 Nov 2020 05:01:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604581263; cv=none; d=google.com; s=arc-20160816; b=hQu96LzUYNk2xlEy/TpYiqnm1KxaSlb8SvR4XAXFHTzLvXtXR4FIZ7Dk51bfuvssAu Un8z5KjYxSpd1ZUARE2iZyOCJhoJIMULkvatNT9RYfCyAfs5G3EaNyWvlSVOV1WWmGio tvL7/lYtr9BdxESyNtT18xrwoznK7f8w5pOayZAVu0730nc0um6xT+08t6Ij3Kr35d4g 8HDzQ+nx1eBcI/WxCXyiGM9b3gLvXpttaWPuEnm1OTPjg7RlnOKLSCGG9JNETyOqnxnM fkvCp1Y5aNazwtLsHV+RsJwtMxYB5bNIaUXKaPudj7rj099vUN7NI6jWj/4z9mks+d5r zdXA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=oDIkrEooxToWiczeElVPULQyRRfwVrg6rQmXRuL4PKI=; b=aVBlZi8X6CSZUJs6S5O0whYJ+0LihDxjfzjVJhcDZ7nCavV0nujXGOV6/RQdD3SjkW q7GyZuYib8ZV0G/BOtd8YEuN7lkE8VrU1feubqEkoxWtWvkjiTHPsRgx0QTuDr4TXFse C4enfyVz4Zm2oq1/Enbgoo0A37kbOETpelilJS3QOFqvjpPOc0muyF3ogqbiqCt04mT0 OJ/6fBCsCoRN4GMbYFw5TaPsUn5LJr3WQIqhLaIXqHiGeb3Ld/FO9ERMcRDPH/ZMtEq+ +1m0MD1EgMlLc27wkyvQaaovjQoUsoCq7vc055pBm3+urYlLcfInWGPHjP7xzyPiPERh EKjw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v23si1138927ejo.131.2020.11.05.05.00.40; Thu, 05 Nov 2020 05:01:03 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-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-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730979AbgKEM5a (ORCPT + 99 others); Thu, 5 Nov 2020 07:57:30 -0500 Received: from foss.arm.com ([217.140.110.172]:60126 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730938AbgKEM5L (ORCPT ); Thu, 5 Nov 2020 07:57:11 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 13DF11529; Thu, 5 Nov 2020 04:57:11 -0800 (PST) Received: from donnerap.arm.com (donnerap.cambridge.arm.com [10.1.195.35]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 6A19B3F719; Thu, 5 Nov 2020 04:57:09 -0800 (PST) From: Andre Przywara To: Will Deacon , Catalin Marinas , Ard Biesheuvel , Russell King Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, Sudeep Holla , Mark Rutland , Mark Brown , Lorenzo Pieralisi , Linus Walleij Subject: [PATCH v2 3/5] ARM: implement support for SMCCC TRNG entropy source Date: Thu, 5 Nov 2020 12:56:54 +0000 Message-Id: <20201105125656.25259-4-andre.przywara@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201105125656.25259-1-andre.przywara@arm.com> References: <20201105125656.25259-1-andre.przywara@arm.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ard Biesheuvel Implement arch_get_random_seed_*() for ARM based on the firmware or hypervisor provided entropy source described in ARM DEN0098. This will make the kernel's random number generator consume entropy provided by this interface, at early boot, and periodically at runtime when reseeding. Cc: Linus Walleij Cc: Russell King Signed-off-by: Ard Biesheuvel [Andre: rework to be initialised by the SMCCC firmware driver] Signed-off-by: Andre Przywara --- arch/arm/Kconfig | 4 ++ arch/arm/include/asm/archrandom.h | 64 +++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index fe2f17eb2b50..06fda4f954fd 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -1667,6 +1667,10 @@ config STACKPROTECTOR_PER_TASK Enable this option to switch to a different method that uses a different canary value for each task. +config ARCH_RANDOM + def_bool y + depends on HAVE_ARM_SMCCC + endmenu menu "Boot options" diff --git a/arch/arm/include/asm/archrandom.h b/arch/arm/include/asm/archrandom.h index a8e84ca5c2ee..f3e96a5b65f8 100644 --- a/arch/arm/include/asm/archrandom.h +++ b/arch/arm/include/asm/archrandom.h @@ -2,9 +2,73 @@ #ifndef _ASM_ARCHRANDOM_H #define _ASM_ARCHRANDOM_H +#ifdef CONFIG_ARCH_RANDOM + +#include +#include + +#define ARM_SMCCC_TRNG_MIN_VERSION 0x10000UL + +extern bool smccc_trng_available; + +static inline bool __init smccc_probe_trng(void) +{ + struct arm_smccc_res res; + + arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_VERSION, &res); + if ((s32)res.a0 < 0) + return false; + if (res.a0 >= ARM_SMCCC_TRNG_MIN_VERSION) { + /* double check that the 32-bit flavor is available */ + arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_FEATURES, + ARM_SMCCC_TRNG_RND32, + &res); + if ((s32)res.a0 >= 0) + return true; + } + + return false; +} + +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) +{ + struct arm_smccc_res res; + + if (smccc_trng_available) { + arm_smccc_1_1_invoke(ARM_SMCCC_TRNG_RND32, 8 * sizeof(*v), &res); + + if (res.a0 != 0) + return false; + + *v = res.a3; + return true; + } + + return false; +} + +static inline bool __must_check arch_get_random_seed_int(unsigned int *v) +{ + return arch_get_random_seed_long((unsigned long *)v); +} + + +#else /* !CONFIG_ARCH_RANDOM */ + static inline bool __init smccc_probe_trng(void) { return false; } +#endif /* CONFIG_ARCH_RANDOM */ #endif /* _ASM_ARCHRANDOM_H */ -- 2.17.1