Change in dart/sdk[master]: Use BCryptGenRandom for Crypto::GetRandomBytes on Windows

13 views
Skip to first unread message

包布丁 (Gerrit)

unread,
May 17, 2021, 12:32:16 AM5/17/21
to rev...@dartlang.org, vm-...@dartlang.org

包布丁 has uploaded this change for review.

View Change

Use BCryptGenRandom for Crypto::GetRandomBytes on Windows

The current implementation of Crypto::GetRandomBytes on Windows calls
`rand_s` repeatedly until the buffer is completely filled. However,
`BCryptGenRandom` already provides the similar functionality to fill the
whole buffer at once and thus there is no need to maintain a handcrafted
implementation any more.

Change-Id: I52d990b01b59be872d825f2aa0e30b500a6d3e36
---
M runtime/bin/BUILD.gn
M runtime/bin/crypto_win.cc
2 files changed, 6 insertions(+), 16 deletions(-)

diff --git a/runtime/bin/BUILD.gn b/runtime/bin/BUILD.gn
index 1bc8225..8ab6fca 100644
--- a/runtime/bin/BUILD.gn
+++ b/runtime/bin/BUILD.gn
@@ -214,6 +214,7 @@
"Rpcrt4.lib",
"shlwapi.lib",
"winmm.lib",
+ "bcrypt.lib",
]
if (target_os != "winuwp") {
libs += [ "psapi.lib" ]
@@ -784,6 +785,7 @@
"Rpcrt4.lib",
"shlwapi.lib",
"winmm.lib",
+ "bcrypt.lib",
]
}
}
@@ -958,6 +960,7 @@
"Rpcrt4.lib",
"shlwapi.lib",
"winmm.lib",
+ "bcrypt.lib",
]
}
}
diff --git a/runtime/bin/crypto_win.cc b/runtime/bin/crypto_win.cc
index fd68802..23d52ef 100644
--- a/runtime/bin/crypto_win.cc
+++ b/runtime/bin/crypto_win.cc
@@ -2,31 +2,18 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

-#ifndef _CRT_RAND_S
-#define _CRT_RAND_S
-#endif
-
#include "platform/globals.h"
#if defined(HOST_OS_WINDOWS)

+#include <bcrypt.h>
#include "bin/crypto.h"

namespace dart {
namespace bin {

bool Crypto::GetRandomBytes(intptr_t count, uint8_t* buffer) {
- uint32_t num;
- intptr_t read = 0;
- while (read < count) {
- if (rand_s(&num) != 0) {
- return false;
- }
- for (int i = 0; i < 4 && read < count; i++) {
- buffer[read] = num >> (i * 8);
- read++;
- }
- }
- return true;
+ return SUCCEEDED(BCryptGenRandom(NULL, buffer, (ULONG)count,
+ BCRYPT_USE_SYSTEM_PREFERRED_RNG));
}

} // namespace bin

To view, visit change 200160. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: sdk
Gerrit-Branch: master
Gerrit-Change-Id: I52d990b01b59be872d825f2aa0e30b500a6d3e36
Gerrit-Change-Number: 200160
Gerrit-PatchSet: 1
Gerrit-Owner: 包布丁 <bdba...@163.com>
Gerrit-MessageType: newchange

Dart CI (Gerrit)

unread,
May 24, 2021, 12:40:28 PM5/24/21
to 包布丁, rev...@dartlang.org, vm-...@dartlang.org, commi...@chromium.org, Alexander Aprelev

Attention is currently required from: 包布丁.

go/dart-cbuild result: FAILURE (REGRESSIONS DETECTED)

Details: https://goto.google.com/dart-cbuild/find/4f86cd9176625d9883b4315367c6bc7e2fca0f40
Bugs: go/dart-cbuild-bug/4f86cd9176625d9883b4315367c6bc7e2fca0f40

View Change

    To view, visit change 200160. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: sdk
    Gerrit-Branch: master
    Gerrit-Change-Id: I52d990b01b59be872d825f2aa0e30b500a6d3e36
    Gerrit-Change-Number: 200160
    Gerrit-PatchSet: 1
    Gerrit-Owner: 包布丁 <bdba...@163.com>
    Gerrit-Reviewer: Alexander Aprelev <a...@google.com>
    Gerrit-Attention: 包布丁 <bdba...@163.com>
    Gerrit-Comment-Date: Mon, 24 May 2021 16:40:23 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Alexander Aprelev (Gerrit)

    unread,
    May 25, 2021, 12:50:48 PM5/25/21
    to Alexander Aprelev, 包布丁, rev...@dartlang.org, vm-...@dartlang.org, Dart CI, commi...@chromium.org

    Attention is currently required from: 包布丁.

    View Change

    1 comment:

    To view, visit change 200160. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: sdk
    Gerrit-Branch: master
    Gerrit-Change-Id: I52d990b01b59be872d825f2aa0e30b500a6d3e36
    Gerrit-Change-Number: 200160
    Gerrit-PatchSet: 2
    Gerrit-Owner: 包布丁 <bdba...@163.com>
    Gerrit-Reviewer: Alexander Aprelev <a...@google.com>
    Gerrit-Attention: 包布丁 <bdba...@163.com>
    Gerrit-Comment-Date: Tue, 25 May 2021 16:50:43 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    包布丁 (Gerrit)

    unread,
    May 25, 2021, 8:58:57 PM5/25/21
    to Alexander Aprelev, rev...@dartlang.org, vm-...@dartlang.org, Dart CI, commi...@chromium.org

    Attention is currently required from: Alexander Aprelev.

    View Change

    1 comment:

    • Patchset:

      • Patch Set #2:

        Thank you for putting together the change! […]

        Yes, I have already signed CLA.

        May I know why CI failed? I could not log in to the website that Dart CI posted for details.

    To view, visit change 200160. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: sdk
    Gerrit-Branch: master
    Gerrit-Change-Id: I52d990b01b59be872d825f2aa0e30b500a6d3e36
    Gerrit-Change-Number: 200160
    Gerrit-PatchSet: 2
    Gerrit-Owner: 包布丁 <bdba...@163.com>
    Gerrit-Reviewer: Alexander Aprelev <a...@google.com>
    Gerrit-Attention: Alexander Aprelev <a...@google.com>
    Gerrit-Comment-Date: Wed, 26 May 2021 00:58:51 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Alexander Aprelev <a...@google.com>
    Gerrit-MessageType: comment

    Alexander Aprelev (Gerrit)

    unread,
    May 25, 2021, 9:04:46 PM5/25/21
    to Alexander Aprelev, 包布丁, rev...@dartlang.org, vm-...@dartlang.org, Dart CI, commi...@chromium.org

    Attention is currently required from: 包布丁.

    Patch set 2:Commit-Queue +2

    View Change

    1 comment:

    • Patchset:

      • Patch Set #2:

        Yes, I have already signed CLA. […]

        Great! The failed CI can be ignored - there was an unrelated problem with the run.

    To view, visit change 200160. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: sdk
    Gerrit-Branch: master
    Gerrit-Change-Id: I52d990b01b59be872d825f2aa0e30b500a6d3e36
    Gerrit-Change-Number: 200160
    Gerrit-PatchSet: 2
    Gerrit-Owner: 包布丁 <bdba...@163.com>
    Gerrit-Reviewer: Alexander Aprelev <a...@google.com>
    Gerrit-Attention: 包布丁 <bdba...@163.com>
    Gerrit-Comment-Date: Wed, 26 May 2021 01:04:43 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Comment-In-Reply-To: Alexander Aprelev <a...@google.com>
    Comment-In-Reply-To: 包布丁 <bdba...@163.com>
    Gerrit-MessageType: comment

    commit-bot@chromium.org (Gerrit)

    unread,
    May 25, 2021, 9:04:56 PM5/25/21
    to 包布丁, rev...@dartlang.org, vm-...@dartlang.org, Alexander Aprelev, Dart CI

    Attention is currently required from: 包布丁.

    CL dart-review.googlesource.com/200160 must be approved before triggering CQ
    CL is missing the approving votes on Code-Review, Commit-Message-has-TEST labels.
    If you think this is not correct, then your Gerrit project may be misconfigured: check `refs/meta/config`:`project.cfg` or contact maintainers of your project's infrastructure

    View Change

      To view, visit change 200160. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: sdk
      Gerrit-Branch: master
      Gerrit-Change-Id: I52d990b01b59be872d825f2aa0e30b500a6d3e36
      Gerrit-Change-Number: 200160
      Gerrit-PatchSet: 2
      Gerrit-Owner: 包布丁 <bdba...@163.com>
      Gerrit-Reviewer: Alexander Aprelev <a...@google.com>
      Gerrit-Attention: 包布丁 <bdba...@163.com>
      Gerrit-Comment-Date: Wed, 26 May 2021 01:04:53 +0000

      Alexander Aprelev (Gerrit)

      unread,
      May 25, 2021, 9:05:40 PM5/25/21
      to Alexander Aprelev, 包布丁, rev...@dartlang.org, vm-...@dartlang.org

      Attention is currently required from: 包布丁.

      Alexander Aprelev uploaded patch set #3 to the change originally created by 包布丁.

      View Change

      Use BCryptGenRandom for Crypto::GetRandomBytes on Windows

      The current implementation of Crypto::GetRandomBytes on Windows calls
      `rand_s` repeatedly until the buffer is completely filled. However,
      `BCryptGenRandom` already provides the similar functionality to fill the
      whole buffer at once and thus there is no need to maintain a handcrafted
      implementation any more.

      TEST=ci


      Change-Id: I52d990b01b59be872d825f2aa0e30b500a6d3e36
      ---
      M runtime/bin/BUILD.gn
      M runtime/bin/crypto_win.cc
      2 files changed, 6 insertions(+), 16 deletions(-)

      To view, visit change 200160. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: sdk
      Gerrit-Branch: master
      Gerrit-Change-Id: I52d990b01b59be872d825f2aa0e30b500a6d3e36
      Gerrit-Change-Number: 200160
      Gerrit-PatchSet: 3
      Gerrit-Owner: 包布丁 <bdba...@163.com>
      Gerrit-Reviewer: Alexander Aprelev <a...@google.com>
      Gerrit-Attention: 包布丁 <bdba...@163.com>
      Gerrit-MessageType: newpatchset

      Alexander Aprelev (Gerrit)

      unread,
      May 25, 2021, 9:06:03 PM5/25/21
      to Alexander Aprelev, 包布丁, rev...@dartlang.org, vm-...@dartlang.org, Dart CI, commi...@chromium.org

      Attention is currently required from: 包布丁.

      Patch set 3:Code-Review +1Commit-Queue +2

      View Change

      1 comment:

      To view, visit change 200160. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: sdk
      Gerrit-Branch: master
      Gerrit-Change-Id: I52d990b01b59be872d825f2aa0e30b500a6d3e36
      Gerrit-Change-Number: 200160
      Gerrit-PatchSet: 3
      Gerrit-Owner: 包布丁 <bdba...@163.com>
      Gerrit-Reviewer: Alexander Aprelev <a...@google.com>
      Gerrit-Attention: 包布丁 <bdba...@163.com>
      Gerrit-Comment-Date: Wed, 26 May 2021 01:06:00 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: Yes
      Gerrit-MessageType: comment

      Alexander Aprelev (Gerrit)

      unread,
      May 25, 2021, 9:06:57 PM5/25/21
      to Alexander Aprelev, 包布丁, rev...@dartlang.org, vm-...@dartlang.org, Dart CI, commi...@chromium.org

      Attention is currently required from: 包布丁.

      Patch set 2:Code-Review +1

      View Change

        To view, visit change 200160. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: sdk
        Gerrit-Branch: master
        Gerrit-Change-Id: I52d990b01b59be872d825f2aa0e30b500a6d3e36
        Gerrit-Change-Number: 200160
        Gerrit-PatchSet: 2
        Gerrit-Owner: 包布丁 <bdba...@163.com>
        Gerrit-Reviewer: Alexander Aprelev <a...@google.com>
        Gerrit-Attention: 包布丁 <bdba...@163.com>
        Gerrit-Comment-Date: Wed, 26 May 2021 01:05:22 +0000
        Gerrit-HasComments: No
        Gerrit-Has-Labels: Yes
        Gerrit-MessageType: comment

        commit-bot@chromium.org (Gerrit)

        unread,
        May 25, 2021, 9:55:15 PM5/25/21
        to 包布丁, rev...@dartlang.org, vm-...@dartlang.org, Alexander Aprelev, Dart CI

        commi...@chromium.org submitted this change.

        View Change

        Approvals: Alexander Aprelev: Looks good to me, approved; Commit
        Use BCryptGenRandom for Crypto::GetRandomBytes on Windows

        The current implementation of Crypto::GetRandomBytes on Windows calls
        `rand_s` repeatedly until the buffer is completely filled. However,
        `BCryptGenRandom` already provides the similar functionality to fill the
        whole buffer at once and thus there is no need to maintain a handcrafted
        implementation any more.

        TEST=ci

        Change-Id: I52d990b01b59be872d825f2aa0e30b500a6d3e36
        Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/200160
        Reviewed-by: Alexander Aprelev <a...@google.com>
        Commit-Queue: Alexander Aprelev <a...@google.com>

        ---
        M runtime/bin/BUILD.gn
        M runtime/bin/crypto_win.cc
        2 files changed, 6 insertions(+), 16 deletions(-)

        To view, visit change 200160. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: sdk
        Gerrit-Branch: master
        Gerrit-Change-Id: I52d990b01b59be872d825f2aa0e30b500a6d3e36
        Gerrit-Change-Number: 200160
        Gerrit-PatchSet: 4
        Gerrit-Owner: 包布丁 <bdba...@163.com>
        Gerrit-Reviewer: Alexander Aprelev <a...@google.com>
        Gerrit-MessageType: merged

        Dart CI (Gerrit)

        unread,
        May 25, 2021, 10:22:08 PM5/25/21
        to commi...@chromium.org, 包布丁, rev...@dartlang.org, vm-...@dartlang.org, Alexander Aprelev

        go/dart-cbuild result: SUCCESS

        Details: https://goto.google.com/dart-cbuild/find/1731050b29097b7b40d208ec1658f6f20737d276

        View Change

          To view, visit change 200160. To unsubscribe, or for help writing mail filters, visit settings.

          Gerrit-Project: sdk
          Gerrit-Branch: master
          Gerrit-Change-Id: I52d990b01b59be872d825f2aa0e30b500a6d3e36
          Gerrit-Change-Number: 200160
          Gerrit-PatchSet: 4
          Gerrit-Owner: 包布丁 <bdba...@163.com>
          Gerrit-Reviewer: Alexander Aprelev <a...@google.com>
          Gerrit-Comment-Date: Wed, 26 May 2021 02:22:04 +0000
          Reply all
          Reply to author
          Forward
          0 new messages