From patchwork Wed May 22 18:42:04 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 164885 Delivered-To: patch@linaro.org Received: by 2002:a92:9e1a:0:0:0:0:0 with SMTP id q26csp1254164ili; Wed, 22 May 2019 12:28:43 -0700 (PDT) X-Google-Smtp-Source: APXvYqx+VE0JOEwxJWJg8BP79sQ2sC71pOoSfCczsT5NkryjKAWXdf9Lz3QyMgFQit6X8dJ2wiEE X-Received: by 2002:a50:b68b:: with SMTP id d11mr91772171ede.42.1558553323888; Wed, 22 May 2019 12:28:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558553323; cv=none; d=google.com; s=arc-20160816; b=hldqrFyFCeWrh/t+iH3fOsWgpna9Xx0Yhf5nvVeVOsf/3vg9LwDwW6kfbBJyMfrdQl GgpaZnQ4lyPZ8i+ItwTwjSjxJnR8UkYTMLHRvS+YNgA8b1iL5qPwjbf55xJ3yXAQo+IC cDs3f6XOYe94W9Dpgs7XyUn55ZdOk1anh2sYjzzJjDaO1TIgugF+BVKbIl3uGETH7bSH jysKjMevmta6lavHmFQFw7DcQ08PT6HViLLeflk2qrzgTsWKId7oqOoBNfpIZrFH44v2 FDLnAim5XsGlZfIRNvNY0/ns545qzWRmTKiqYp7A10Sk0HjZojqyDw40Z+cfPSbwDyw0 r/Qg== 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=bo735OSxFE828NpSjVcXceC5VQkMn/wo0pzFwWOQFT7qmMDQBQJqYpSdQorOKqnMxi pkYid4EvOhF0TXu5V+iATiOJk/nsKtOJTWbhdPaW3Id8tbBU1HhZw82Q4E9cCquPQ8m6 4hWBw6WDyQa278Ej3AgYQPx1d29qSYw8eFOWo9UQCoRRANcjfedssqnS/3SGu/Zx0593 A86SsIeSxai/7W2hPCpsifAQZYQrpQqtm3uTe09od8jIqvX46p83frqiNcSrh8rHhyWz smR+Upqo5hmcmATW4KYeIXZKb/4YMIDKbVe0cfaqprbX3HXpPP97H/uMFPlQhy8C8WaY 8k9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=WqoBSRUT; 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 n5si3172492edn.356.2019.05.22.12.28.43 for (version=TLS1 cipher=AES128-SHA bits=128/128); Wed, 22 May 2019 12:28:43 -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=WqoBSRUT; 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]:50142 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hTWuo-00046d-KG for patch@linaro.org; Wed, 22 May 2019 15:28:42 -0400 Received: from eggs.gnu.org ([209.51.188.92]:35240) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hTWOr-0008P8-BW for qemu-devel@nongnu.org; Wed, 22 May 2019 14:55:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hTWC8-00081S-Eh for qemu-devel@nongnu.org; Wed, 22 May 2019 14:42:33 -0400 Received: from mail-yw1-xc29.google.com ([2607:f8b0:4864:20::c29]:35728) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hTWC8-00081D-8o for qemu-devel@nongnu.org; Wed, 22 May 2019 14:42:32 -0400 Received: by mail-yw1-xc29.google.com with SMTP id k128so1247876ywf.2 for ; Wed, 22 May 2019 11:42:32 -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=WqoBSRUTTGnpLM+sdJxTl9/3hxQssulEWF+W79mNBvMYMqcWZuIYbo0rmAqiUMlwOG 79hIZG9TUAIpN6S3PVWHtrEuAmgmnNzdaQhfYBmURM2ZAtKtfEuf9bFu8DpK/MBNpiM5 d9nzvej5A+Dk/WKjDK2Us/E5OkQZZIaY5VcQ2R+m1O9rhH//DmRk10OBTXmEVFq/r/vI gTKDDOXRiSR85+1nRpqC324Zr1b92Umjv986mx52tcPnf6vV4khqYNyiyLuj8Ug8VglR zAFS1DrYn9xpmLn3zo1zua5BcjuCmbo5CJJuPhM/LfEHNZuDIIKUSl6b+CoYcrhcFYcj ED7Q== 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=TyYVEsMboPyieAIQNWz8uMynB7ltamqjOhoMbT8w1AXomvObdzIkX/IExSZfJyqn0h pBbgiW5s+t98WJqhincv2Rir2UsCdkMLteCjdAk5W25EtIpXtJNQKvN8EMhzrPbHiDcN 0uj4YXWupfjzpKS9lbK57x5WXr+umtPxRu6WKVhhtZ3Uy1ukH+lBK3tvXJIbN2WcwDmj wtGRgKTAm4g/ZFJLqnsd0A29wFUZb+Vd0Ylys1P/kOqGJJQJtWC0VVW3Ag55lbLNhvlJ n0azSefn4Sls15e4+52osfjuEhfDAReTARcOZiSh/aZ2ZiuqFyFeK9xdJ24I8oegM651 S3Mg== X-Gm-Message-State: APjAAAV6+wjbirf2bagGl2hmmEZ7sVO9dMDYk7w9bkMh1VLJCkk7kyEk uKVPSDd7A5IEADqgVb65XAkg/knQ2Cc= X-Received: by 2002:a81:b84:: with SMTP id 126mr41880660ywl.257.1558550551317; Wed, 22 May 2019 11:42:31 -0700 (PDT) Received: from localhost.localdomain ([71.46.56.17]) by smtp.gmail.com with ESMTPSA id 206sm1299445ywq.74.2019.05.22.11.42.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 May 2019 11:42:30 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Wed, 22 May 2019 14:42:04 -0400 Message-Id: <20190522184226.17871-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190522184226.17871-1-richard.henderson@linaro.org> References: <20190522184226.17871-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::c29 Subject: [Qemu-devel] [PULL 03/25] 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: peter.maydell@linaro.org 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 }