From patchwork Thu Mar 14 04:55:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 160289 Delivered-To: patch@linaro.org Received: by 2002:a02:5cc1:0:0:0:0:0 with SMTP id w62csp15075238jad; Wed, 13 Mar 2019 22:01:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqy560fqABFcFmtn+W92oQZxyZ43ayg7po0qRcdq3Aatut/yW+g30HYvM8D7WSuzM3WyU+Y/ X-Received: by 2002:a0d:ffc5:: with SMTP id p188mr27301279ywf.40.1552539701884; Wed, 13 Mar 2019 22:01:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552539701; cv=none; d=google.com; s=arc-20160816; b=JWTUJHhWuY49C6IyNCwbdBRfNrAIEi8j+ChRxIWnfeSTO00GkTRVmEwXRyvwmyLxoa QjP++tpnyMz9VDs5RZCa3MTWvDr+IEWzJ2bcpxqoE1ULcDamIu/ByLOq3bT2bBszwMlq oVn/2ICnZtR8ukltUSzH5i/8mmZ5yqDufJcmUCMhRkoIRUm8PmnIRdfjvQJAFml67toe Cs8MsXY1qgTkqh5rkfH8hPCSBpbLSDLE3YjyHSFqJH/n1hQVyAf8DYFf1V/N4Aika0OX 60fBsUfXKkJEuPLTtkidQBv1ZiSTimjB5GCq4lp91zkyKUcMUFkH6zwanjqDK+RCNNIF gqcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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=+ojpjwybhRjnluxW5GelrvS+BsZOt4ZCH9VZc7CqjKY=; b=zJSjKaJwA6GHbMyr9ypxWh4fckxbehn4+6o2sUkN4WcgpHrnKdInQIe1HzkaIdmX64 0/+LseqkEXH3ioSQ6RqXLXiW5o7ckPdoy1hqTVKJc072xe7Fiqwh4ScXMFrzU6bJQR3N KPHy4gJoGl81IHNZvGSleCp1rw7v/r53T25anHWR4aunfhbarnYeTg/w5eJwVuBij0na UZGu1raS38M4d1jP3abva5WvmEjg3DB03zyoTuqPmeYt5eZFoN2WAy0fYjv6kFqb8l2K tBeMNyTF6/PT5cYdKemql2ZhNh5Tfih7X0hDtYVxUXF3Kg7qrV/i30eGSB86oMgkBgEO eBSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=haMytNTz; 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 d65si7664976ywa.142.2019.03.13.22.01.41 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 13 Mar 2019 22:01:41 -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=haMytNTz; 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]:56924 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4IUv-0000qn-Cc for patch@linaro.org; Thu, 14 Mar 2019 01:01:41 -0400 Received: from eggs.gnu.org ([209.51.188.92]:56107) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h4IP0-0004T9-GA for qemu-devel@nongnu.org; Thu, 14 Mar 2019 00:55:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h4IOz-0007MC-HS for qemu-devel@nongnu.org; Thu, 14 Mar 2019 00:55:34 -0400 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]:43537) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h4IOz-0007LY-A7 for qemu-devel@nongnu.org; Thu, 14 Mar 2019 00:55:33 -0400 Received: by mail-pf1-x42c.google.com with SMTP id q17so2992547pfh.10 for ; Wed, 13 Mar 2019 21:55:33 -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=+ojpjwybhRjnluxW5GelrvS+BsZOt4ZCH9VZc7CqjKY=; b=haMytNTzrA4eecoTkONX/2FM+ki13f/+RQNYDtUCAwDAOj+dNzsNYqP5od6nlBk0SZ d87QVrepShFEiQaSlL/pcQx1kPdGkTAbKKAMDXe78wo9m11ElHWjlMFh71rb8rFHwOWy ryRowXkqF7DdUuEImcF7XizVbl/kKkdrG9uERvOQca9gaWaQlHqjhVJ6UdY3f/WgtxDC vDcd2AXn8MjzKrTrevLGfGDedMHHW86i/kRr5/7UzDv0Q3JxbYBc1kOPsQmqCcTU7aTf RHWdEEHU183/VPG/DtOmDhRAjGoCGYa4VAGRFkAe8xOl/zclmy3FcO8r74omZ7dWWMuw XQKA== 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=+ojpjwybhRjnluxW5GelrvS+BsZOt4ZCH9VZc7CqjKY=; b=W1pUseQu3xrBZ2zjh4J36JxglYZWxV/2RYNmzscIPsBVQmOgTt/9HuWCVFRh+NzxB/ x8UUnGVjFc9bfiztoh5BKuhawJp2/yAHgNdY1ufZ9WiqGVqIpuoCggmw8qghUc09r5w/ IFbdakb4tUUNmy5c4X+QiHin064SHBI5PnzYXcwE6zHG2IukSTtnmbz2+ZZSQdo3/7CQ qhP52FHyDj+34PKZgodyi8Nw6hteTHX554jyz8tHYGV19LSs2RPhG3ROOUIAApg8AGjI HLYae3Dqhu88A0vbH8aXTixxFlllr7N6Ye8OGJrduFKxhu6rI8koxGTWuIdlUsaAmFZ9 fJ8w== X-Gm-Message-State: APjAAAXo93M+McynPy6nDHFWlXlHIRawLxLi9Mmx15wpmDBgkkneCGEU g3fx5D8OeMwoL6LxKoHoIbJsRDGgf9w= X-Received: by 2002:a17:902:7592:: with SMTP id j18mr23278850pll.300.1552539331785; Wed, 13 Mar 2019 21:55:31 -0700 (PDT) Received: from localhost.localdomain (97-113-188-82.tukw.qwest.net. [97.113.188.82]) by smtp.gmail.com with ESMTPSA id d9sm16185027pgq.6.2019.03.13.21.55.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Mar 2019 21:55:30 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 13 Mar 2019 21:55:14 -0700 Message-Id: <20190314045526.13342-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190314045526.13342-1-richard.henderson@linaro.org> References: <20190314045526.13342-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::42c Subject: [Qemu-devel] [PATCH v2 02/14] 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: , 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. Cc: Daniel P. Berrangé Signed-off-by: Richard Henderson --- crypto/random-platform.c | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) -- 2.17.1 Reviewed-by: Daniel P. Berrangé Reviewed-by: Philippe Mathieu-Daudé 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 }