From patchwork Tue May 14 19:16:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 164237 Delivered-To: patch@linaro.org Received: by 2002:a92:9e1a:0:0:0:0:0 with SMTP id q26csp3006199ili; Tue, 14 May 2019 12:23:59 -0700 (PDT) X-Google-Smtp-Source: APXvYqyxdLJ/VRUHUMBWn0UVUteU5XTaUJffY0zfVU8Bfwix3MgfRND8r2EVFfLvlKvpbdUpUTJa X-Received: by 2002:a17:906:a28d:: with SMTP id i13mr29919718ejz.148.1557861839061; Tue, 14 May 2019 12:23:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557861839; cv=none; d=google.com; s=arc-20160816; b=Kus/Po2dPczdgUrCWTEM69MD4g30zTjkOJ4/WzZii4LqnSCqeowrQSMNziVDX4R3nC RwjvOw1e9w9uCajOI/B0dHwJz/2NP/OLrWk6uR9i25dEg9wMKwDm5qAk7VYgcNxOx5KH xywNLXIeqG23gUQGuO7Tm114n6KS/fc1ks9GdE+dH7YMcLNvBeZxDmP+2BXp9Rksp7Nk BiYfdxOptna10FW9KRYUZbzeImrfzWXxQok2i6h8Y1HoeOlZuNxLjBcbkwwgo9FcMCO7 w/RoE2mqGAJa2PaRwHi8gYYaLo8S+9VISvxBWj0HpuBkjaSkW0BgKX2ZKeqav0TJz+O1 e2KQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=WcaPjSSW8mCEGy+vfZffpT+ukMC7uteGhxOw5c2wJXo=; b=MoDmwqqZS42MFNoDo+y2dA0Cnr8/Agpz1oVsc3N8fJ3BeqxYBCHCjpQU1RPI8lJWJh ib3UmTnRfJO0i21OL6TEBSyktDIzS4mcVipe+Rf5Y3QjvN+6tjCinkPh8R3W+vOa1F4+ XudMuKmnUMShEKT8KpOMo8Ytrmd1odUhnThGyGGX5fcbesVlwrwBw4WmZZayAz9RpL4N UVAlS/81daOWtiF5VDroZbAcl3v/1ap5qoo3b0GR+L/HBch5g1wK6JWgMH3kj68H63HE JRQhcGdDhupqyiPuvxRCPZQn8HeHsbbZEhTcGVYDafAW46zHx0YmnqDcdqO5XWAoYTlc sYcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=Gb+xsA4U; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id e22si3454150eda.365.2019.05.14.12.23.58 for (version=TLS1 cipher=AES128-SHA bits=128/128); Tue, 14 May 2019 12:23:59 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=Gb+xsA4U; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([127.0.0.1]:52998 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hQd1p-0008Bc-V0 for patch@linaro.org; Tue, 14 May 2019 15:23:58 -0400 Received: from eggs.gnu.org ([209.51.188.92]:48718) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hQcv7-0002RV-MW for qemu-devel@nongnu.org; Tue, 14 May 2019 15:17:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hQcv6-0006bA-Ad for qemu-devel@nongnu.org; Tue, 14 May 2019 15:17:01 -0400 Received: from mail-pg1-x544.google.com ([2607:f8b0:4864:20::544]:36047) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hQcv6-0006ZU-4y for qemu-devel@nongnu.org; Tue, 14 May 2019 15:17:00 -0400 Received: by mail-pg1-x544.google.com with SMTP id a3so51477pgb.3 for ; Tue, 14 May 2019 12:17:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WcaPjSSW8mCEGy+vfZffpT+ukMC7uteGhxOw5c2wJXo=; b=Gb+xsA4UIYZS7cwRNKCo6SpGCgSreXYfWByMU7/mA9nPjkwQxadgEYrs+xeJJRwj+p HNGJuwCiKp31mt8+sW8yHU3VPhI5m+VM82GA98KMPCb4ScXgK1tBtG9DHPTLHYD4X5Yg chW0MGy7d14uUDyqsFTiaw7u88J484/7B9XqeWCmTHy+CRRUWMr58lhX5iBb9rGnQwO5 w+Wm0woCLXjLkAKmbjoQBM9WdUZkxHLT9GhYgU76BEFh77r/GXhg5D4QF6aNKjKRKIXJ HKiOsM235FPdDUcc6A1ni0U7RYdBTUtl7k+cIFhmXPNa5kIwpDn3itHIeRHOoGtUlLKs 2MTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WcaPjSSW8mCEGy+vfZffpT+ukMC7uteGhxOw5c2wJXo=; b=kQkkUAQS157FF6LY1RAtGze/FA5gRRXp9LjUgvYYY8WHyFVnS4EmxR1llQmkWi/7pV 7f41sTShSKfJWkHXw1XR8PaxPvzxy12X3oFJCaSIZFeLkD1wNEv3qOjen2+yxJ+KtOoV mWPPe5vmk3pNWKdBhco4wIcB/fgzOsjY8mPj1smiFRQHQQqxqOe2U+zV0QPm6DWtyICS IFxcp69kqam20wU1gry5TI/blW8Lo7BqKLz+SYhCVqLF7poVSgqSl/DtJ7p0BIZd7WIm G/l/iOaqrnp+9qSFH+bUTvob5Nw+H6jLiXIrAy8EX8VxrWBW+tZpxfJBtSje/YFsvuk/ 7WxQ== X-Gm-Message-State: APjAAAVmrKVD5spIm+g0W/F4i3dguH2S0dL5ouVOEs1YPYfht1/YnA8T uzF5RAaxegonUMSj9rGcFwmHLDD8i8Y= X-Received: by 2002:a62:160b:: with SMTP id 11mr43761787pfw.88.1557861418821; Tue, 14 May 2019 12:16:58 -0700 (PDT) Received: from localhost.localdomain (97-113-13-231.tukw.qwest.net. [97.113.13.231]) by smtp.gmail.com with ESMTPSA id o6sm18682982pfa.88.2019.05.14.12.16.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 May 2019 12:16:57 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Tue, 14 May 2019 12:16:31 -0700 Message-Id: <20190514191653.31488-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190514191653.31488-1-richard.henderson@linaro.org> References: <20190514191653.31488-1-richard.henderson@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::544 Subject: [Qemu-devel] [PATCH v7 02/24] crypto: Reverse code blocks in random-platform.c X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: lvivier@redhat.com, berrange@redhat.com, armbru@redhat.com Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Use #ifdef _WIN32 instead of #ifndef _WIN32. This will make other tests easier to sequence. Reviewed-by: Laurent Vivier Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Daniel P. Berrangé Signed-off-by: Richard Henderson --- crypto/random-platform.c | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) -- 2.17.1 diff --git a/crypto/random-platform.c b/crypto/random-platform.c index 7541b4cae7..f995fc0ef1 100644 --- a/crypto/random-platform.c +++ b/crypto/random-platform.c @@ -32,7 +32,14 @@ static int fd; /* a file handle to either /dev/urandom or /dev/random */ int qcrypto_random_init(Error **errp) { -#ifndef _WIN32 +#ifdef _WIN32 + if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, + CRYPT_SILENT | CRYPT_VERIFYCONTEXT)) { + error_setg_win32(errp, GetLastError(), + "Unable to create cryptographic provider"); + return -1; + } +#else /* TBD perhaps also add support for BSD getentropy / Linux * getrandom syscalls directly */ fd = open("/dev/urandom", O_RDONLY); @@ -44,15 +51,7 @@ int qcrypto_random_init(Error **errp) error_setg(errp, "No /dev/urandom or /dev/random found"); return -1; } -#else - if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, - CRYPT_SILENT | CRYPT_VERIFYCONTEXT)) { - error_setg_win32(errp, GetLastError(), - "Unable to create cryptographic provider"); - return -1; - } #endif - return 0; } @@ -60,7 +59,15 @@ int qcrypto_random_bytes(uint8_t *buf G_GNUC_UNUSED, size_t buflen G_GNUC_UNUSED, Error **errp) { -#ifndef _WIN32 +#ifdef _WIN32 + if (!CryptGenRandom(hCryptProv, buflen, buf)) { + error_setg_win32(errp, GetLastError(), + "Unable to read random bytes"); + return -1; + } + + return 0; +#else int ret = -1; int got; @@ -82,13 +89,5 @@ int qcrypto_random_bytes(uint8_t *buf G_GNUC_UNUSED, ret = 0; cleanup: return ret; -#else - if (!CryptGenRandom(hCryptProv, buflen, buf)) { - error_setg_win32(errp, GetLastError(), - "Unable to read random bytes"); - return -1; - } - - return 0; #endif }