From patchwork Thu Jul 24 04:57:29 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andy Lutomirski X-Patchwork-Id: 4614491 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id C022F9F37C for ; Thu, 24 Jul 2014 04:59:15 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D9CD8201DE for ; Thu, 24 Jul 2014 04:59:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E49B0201CE for ; Thu, 24 Jul 2014 04:59:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934142AbaGXE54 (ORCPT ); Thu, 24 Jul 2014 00:57:56 -0400 Received: from mail-pa0-f44.google.com ([209.85.220.44]:52571 "EHLO mail-pa0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934155AbaGXE5x (ORCPT ); Thu, 24 Jul 2014 00:57:53 -0400 Received: by mail-pa0-f44.google.com with SMTP id eu11so3137834pac.17 for ; Wed, 23 Jul 2014 21:57:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=/94JQzv6ktHb/viRNOskOaDazlXe+urSiVXMk2r4C34=; b=Voa3sYrcWxVZ4qEjwf3sf6XtkkH6Xks7FB6g8CBCbz35zB4icLq3Dhkvx2kb5xTxF6 lpWXAm3zGSchZ94G/6uuDvshezwhu8GxCsCDsBJS5JgFwQlESebBg+f0fXJoT5oYCn9O W3h4H7LSDt2lYE+a5CrVO5Z80ybs9WlKFzZtZwTHWeKZG9mjSr7ORrKhs2+wAkH3rEvj 0t6RphVO77U3LTXUzJTIyd86wXyxl1cw0lO6u+LrCBG3TghpuSzfnV0TWxY5Z9Uc3NdU rUzNIt8j6wPLmHoMq4CgmEoVc5oYy3LIA8QfA6ft30Grwc/1Syy+EbeshIViDsPLnY0H dJMQ== X-Gm-Message-State: ALoCoQkBFoGmYxiVATTX6AgYB+TbsAOXjk3QVGJlqRJ1VrlZkDRd+qBfNcpsUSncC1pSZ06DDM9I X-Received: by 10.66.65.193 with SMTP id z1mr276209pas.9.1406177872220; Wed, 23 Jul 2014 21:57:52 -0700 (PDT) Received: from localhost ([2001:5a8:4:83c0:fd15:6cb4:fa7d:1e89]) by mx.google.com with ESMTPSA id xc5sm705583pbc.66.2014.07.23.21.57.48 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 23 Jul 2014 21:57:51 -0700 (PDT) From: Andy Lutomirski To: kvm@vger.kernel.org, "H. Peter Anvin" , Theodore Ts'o , linux-kernel@vger.kernel.org, Kees Cook , x86@kernel.org Cc: Daniel Borkmann , Srivatsa Vaddagiri , Raghavendra K T , Gleb Natapov , Paolo Bonzini , bsd@redhat.com, Andrew Honig , Andy Lutomirski Subject: [PATCH v5 3/5] x86, random: Add an x86 implementation of arch_get_rng_seed Date: Wed, 23 Jul 2014 21:57:29 -0700 Message-Id: <66fd3430747ae8f7864ce2d0059a8cc881bb9d6a.1406177531.git.luto@amacapital.net> X-Mailer: git-send-email 1.9.3 In-Reply-To: References: In-Reply-To: References: Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This does the same thing as the generic implementation, except that it logs how many bits of each type it collected. I want to know whether the initial seeding is working and, if so, whether the RNG is fast enough. (I know that hpa assures me that the hardware RNG is more than fast enough, but I'd still like a direct way to verify this.) Arguably, arch_get_random_seed could be removed now: I'm having some trouble imagining a sensible non-architecture-specific use of it that wouldn't be better served by arch_get_rng_seed. Signed-off-by: Andy Lutomirski --- arch/x86/include/asm/archrandom.h | 6 +++++ arch/x86/kernel/Makefile | 2 ++ arch/x86/kernel/archrandom.c | 51 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 arch/x86/kernel/archrandom.c diff --git a/arch/x86/include/asm/archrandom.h b/arch/x86/include/asm/archrandom.h index 69f1366..88f9c5a 100644 --- a/arch/x86/include/asm/archrandom.h +++ b/arch/x86/include/asm/archrandom.h @@ -117,6 +117,12 @@ GET_SEED(arch_get_random_seed_int, unsigned int, RDSEED_INT, ASM_NOP4); #define arch_has_random() static_cpu_has(X86_FEATURE_RDRAND) #define arch_has_random_seed() static_cpu_has(X86_FEATURE_RDSEED) +#define __HAVE_ARCH_GET_RNG_SEED +extern void arch_get_rng_seed(void *ctx, + void (*seed)(void *ctx, u32 data), + int bits_per_source, + const char *log_prefix); + #else static inline int rdrand_long(unsigned long *v) diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index 047f9ff..0718bae 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -92,6 +92,8 @@ obj-$(CONFIG_PARAVIRT) += paravirt.o paravirt_patch_$(BITS).o obj-$(CONFIG_PARAVIRT_SPINLOCKS)+= paravirt-spinlocks.o obj-$(CONFIG_PARAVIRT_CLOCK) += pvclock.o +obj-$(CONFIG_ARCH_RANDOM) += archrandom.o + obj-$(CONFIG_PCSPKR_PLATFORM) += pcspeaker.o obj-$(CONFIG_X86_CHECK_BIOS_CORRUPTION) += check.o diff --git a/arch/x86/kernel/archrandom.c b/arch/x86/kernel/archrandom.c new file mode 100644 index 0000000..47d13b0 --- /dev/null +++ b/arch/x86/kernel/archrandom.c @@ -0,0 +1,51 @@ +/* + * This file is part of the Linux kernel. + * + * Copyright (c) 2014 Andy Lutomirski + * Authors: Andy Lutomirski + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + */ + +#include + +void arch_get_rng_seed(void *ctx, + void (*seed)(void *ctx, u32 data), + int bits_per_source, + const char *log_prefix) +{ + int i; + int rdseed_bits = 0, rdrand_bits = 0; + char buf[128] = ""; + char *msgptr = buf; + + for (i = 0; i < bits_per_source; i += 8 * sizeof(long)) { + unsigned long rv; + + if (arch_get_random_seed_long(&rv)) + rdseed_bits += 8 * sizeof(rv); + else if (arch_get_random_long(&rv)) + rdrand_bits += 8 * sizeof(rv); + else + continue; /* Don't waste time mixing. */ + + seed(ctx, (u32)rv); +#if BITS_PER_LONG > 32 + seed(ctx, (u32)(rv >> 32)); +#endif + } + + if (rdseed_bits) + msgptr += sprintf(msgptr, ", %d bits from RDSEED", rdseed_bits); + if (rdrand_bits) + msgptr += sprintf(msgptr, ", %d bits from RDRAND", rdrand_bits); + if (buf[0]) + pr_info("%s with %s\n", log_prefix, buf + 2); +}