diff mbox series

[v2,02/14] crypto: Reverse code blocks in random-platform.c

Message ID 20190314045526.13342-3-richard.henderson@linaro.org
State New
Headers show
Series Add qemu_getrandom and ARMv8.5-RNG | expand

Commit Message

Richard Henderson March 14, 2019, 4:55 a.m. UTC
Use #ifdef _WIN32 instead of #ifndef _WIN32.
This will make other tests easier to sequence.

Cc: Daniel P. Berrangé <berrange@redhat.com>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 crypto/random-platform.c | 35 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 18 deletions(-)

Comments

Daniel P. Berrangé March 14, 2019, 3:30 p.m. UTC | #1
On Wed, Mar 13, 2019 at 09:55:14PM -0700, Richard Henderson wrote:
> Use #ifdef _WIN32 instead of #ifndef _WIN32.
> This will make other tests easier to sequence.
> 
> Cc: Daniel P. Berrangé <berrange@redhat.com>
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>  crypto/random-platform.c | 35 +++++++++++++++++------------------
>  1 file changed, 17 insertions(+), 18 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>


Regards,
Daniel
Philippe Mathieu-Daudé March 14, 2019, 9:38 p.m. UTC | #2
On 3/14/19 5:55 AM, Richard Henderson wrote:
> Use #ifdef _WIN32 instead of #ifndef _WIN32.
> This will make other tests easier to sequence.
> 
> Cc: Daniel P. Berrangé <berrange@redhat.com>
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>

> ---
>  crypto/random-platform.c | 35 +++++++++++++++++------------------
>  1 file changed, 17 insertions(+), 18 deletions(-)
> 
> 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
>  }
>
diff mbox series

Patch

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
 }