From patchwork Mon May 6 17:33:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 163421 Delivered-To: patch@linaro.org Received: by 2002:a05:6e02:81:0:0:0:0 with SMTP id l1csp254986ilm; Mon, 6 May 2019 10:37:05 -0700 (PDT) X-Google-Smtp-Source: APXvYqxJWAL115FXzPrrIi76+5Pm0o5S9LmDS18oxoTQwk4MoqxvitcwKEFuxPdhCQVMr7Wp1VgZ X-Received: by 2002:a1c:18d:: with SMTP id 135mr17257048wmb.14.1557164225472; Mon, 06 May 2019 10:37:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557164225; cv=none; d=google.com; s=arc-20160816; b=z/aa/BOOgur2Unib5IUdsTQudlIxey0wb+u3jkcl6P2AnzgSK308iBH+4o1IwM2hOK n1gkwzc9H6YHlyRQwcuXr6sunGV9ovWqGAHxTc6EENtCoG4/bCSalbbcRQZwoArO0oZj vQbiVfS4qptTvoCVZywDhKmRNysdEVbhMXhnUZ+qRxLqrYDT3rAB2N60aOTPMapT2MFx n5yiDwlWFmOLQEta19IpEAohT6caM9DoElIMhe3TKWt7eWufpWaJq+ts2FbR4m0wJU2h 2iHRzR2ENcGsVaaXek9F7Fxxxlyn8k6OGAIuYq+TbcnxCeCa+d+U3BM12cAp+J2zxZQ7 h8cQ== 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=O1PQbxI86I2bI4Y4HCTdo78SLn5JqRTP/j+lnThrZ4M=; b=fXGvdxT5Oy6Z19XgmYXosEUTnAO+hI/2FElZLtc4Fkg9h3AySp9VujIVp671VHDzXz j/8qr55awclo5xar3FTqlV7UffZRJs2ewNtRZNkuUWkimU7wCmXdhSpSRyuK5w6tD6sT Rmm6aH5iSCedqz5haBfDHENWNL3mywyeXkycXEHjr93bFGVoSk8EdhwK48pI1gfr09Hr 7iZFjgm5E2jyAyg9ReZ93Iew8ZKf254UoXaNak15Iq5tRdFxz7NQDAaFcDbFTGPnihBt ynImUVUEHHJUJAHOSGy1O5leJagUUcELwAtXZu5dVAIRtSnU57op8YDsTEOBGwGqYwWQ TQ5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=QlAFgO6b; 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 3si7150699wma.176.2019.05.06.10.37.05 for (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 06 May 2019 10:37:05 -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=QlAFgO6b; 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]:59936 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hNhY0-0004Hc-Ew for patch@linaro.org; Mon, 06 May 2019 13:37:04 -0400 Received: from eggs.gnu.org ([209.51.188.92]:39761) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hNhV9-00010o-3N for qemu-devel@nongnu.org; Mon, 06 May 2019 13:34:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hNhV5-0001Fm-2Y for qemu-devel@nongnu.org; Mon, 06 May 2019 13:34:05 -0400 Received: from mail-pl1-x644.google.com ([2607:f8b0:4864:20::644]:40440) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hNhV3-0001Dv-2O for qemu-devel@nongnu.org; Mon, 06 May 2019 13:34:02 -0400 Received: by mail-pl1-x644.google.com with SMTP id b3so6706946plr.7 for ; Mon, 06 May 2019 10:34:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=O1PQbxI86I2bI4Y4HCTdo78SLn5JqRTP/j+lnThrZ4M=; b=QlAFgO6bdhjojlKOog/QcrhXghb0rPMOgocTzc6WOYuB9M9c9XWFW5GG7hObkuqCht pBpqklm8CjGR79JFXs0OJlutcc1ViafuuU5LGZNKxVSjPlW+ow3qAZqePYbdOSXJ2LaZ oOy+Ig55KCJhAvOhbuVgWvjPrGhR+AxEe3ma0ockDfm3hgssTi3poJXuG385cSYsLZId +S5M0SuH0w7Gn2JnJmlBLliWl9P70nBbYTtmW4oZHCs5ivlfiXjK1D25tMjpKaJY+B6a iJBA3d2zv91M+nHBuTewjMNGnZTRY/GcCO9Gb1A6A0ToGiBTsr6LXRORaRqj6t5GmygW HyLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=O1PQbxI86I2bI4Y4HCTdo78SLn5JqRTP/j+lnThrZ4M=; b=n/0E/1oTSLfjKR4eoF3sf/KmCV4DtGqKxtrE+oqUvl2LDlmU39/eSWfGe0d9zv6UrQ NypLreU0eBKSnq1C+CJ1+1Cp+amMUk8bsc+iVxOCRmxY4xmxAxZSjkIMRnaHvkAEiH30 8I7fUdl5TGObSEvxxw0E5I0fUkWilgOWYhWlo80KNG/JoFLsUaqq4CkfrEnIw3CWZi1+ 58R9AQODRG/+9eBrqDWRyx84FV8OFIfxhUuGmUzkMAZSLFpHJgzqlS86aUzOHD5W7unS 6H4BzDsTUJUCz8HOXLY/Avd0z08MfRRspOWICp+Kly3D6bQHtP+srvvn2G1NWVG2Old2 F+Hg== X-Gm-Message-State: APjAAAUO/p7hwfgTHiuUSg+uq3BAMMbue03uvmuH1tYSPd933cjn7k+G OjHpk70Sdo82YoKIpUWE5mjmAieATys= X-Received: by 2002:a17:902:e612:: with SMTP id cm18mr10597325plb.255.1557164039170; Mon, 06 May 2019 10:33:59 -0700 (PDT) Received: from localhost.localdomain (97-113-189-189.tukw.qwest.net. [97.113.189.189]) by smtp.gmail.com with ESMTPSA id k9sm2268839pfa.180.2019.05.06.10.33.58 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 May 2019 10:33:58 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Mon, 6 May 2019 10:33:32 -0700 Message-Id: <20190506173353.32206-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190506173353.32206-1-richard.henderson@linaro.org> References: <20190506173353.32206-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::644 Subject: [Qemu-devel] [PATCH v4 03/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: , 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: 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 Reviewed-by: Laurent Vivier 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 }