diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2022-02-17 03:29:55 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2022-02-17 03:29:55 +0000 |
commit | 4655b801bbb4f31c2040f4ca2728a82913bf171b (patch) | |
tree | ce60cbf9c98add0e1123950b82f3bc0affe9f212 | |
parent | 0dfdad1cdfb2facc5136ee740a75ab4ad1210498 (diff) | |
parent | a09868dd917bedc021c22d599c098acbf631a39b (diff) | |
download | getrandom-4655b801bbb4f31c2040f4ca2728a82913bf171b.tar.gz |
Snap for 8191477 from a09868dd917bedc021c22d599c098acbf631a39b to tm-frc-permission-release
Change-Id: I24f245166263e0c0e383f3b919f95cda0d6ed02c
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | Android.bp | 76 | ||||
-rw-r--r-- | CHANGELOG.md | 10 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | Cargo.toml.orig | 2 | ||||
-rw-r--r-- | METADATA | 8 | ||||
-rw-r--r-- | TEST_MAPPING | 104 | ||||
-rw-r--r-- | build.rs | 14 | ||||
-rw-r--r-- | cargo2android.json | 11 | ||||
-rw-r--r-- | src/dragonfly.rs | 26 | ||||
-rw-r--r-- | src/error.rs | 2 | ||||
-rw-r--r-- | src/js.rs | 81 | ||||
-rw-r--r-- | src/lib.rs | 27 | ||||
-rw-r--r-- | src/windows.rs | 1 |
14 files changed, 243 insertions, 123 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 64fafa1..d80d39e 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "d79de0c95c01860268e071bcb6b0d019e18cd608" + "sha1": "de51e2961d05c7af3f19c0c78f9a414cdcb83b67" } } @@ -1,4 +1,4 @@ -// This file is generated by cargo2android.py --device --run --dependencies --tests --features=std. +// This file is generated by cargo2android.py --config cargo2android.json. // Do not modify this file as changes will be overridden on upgrade. package { @@ -37,12 +37,18 @@ license { ], } -rust_defaults { - name: "getrandom_defaults", +rust_test { + name: "getrandom_test_src_lib", + host_supported: true, crate_name: "getrandom", + cargo_env_compat: true, + cargo_pkg_version: "0.2.3", srcs: ["src/lib.rs"], test_suites: ["general-tests"], auto_gen_config: true, + test_options: { + unit_test: true, + }, edition: "2018", features: ["std"], rustlibs: [ @@ -51,22 +57,11 @@ rust_defaults { ], } -rust_test_host { - name: "getrandom_host_test_src_lib", - defaults: ["getrandom_defaults"], - test_options: { - unit_test: true, - }, -} - -rust_test { - name: "getrandom_device_test_src_lib", - defaults: ["getrandom_defaults"], -} - rust_defaults { - name: "getrandom_defaults_getrandom", + name: "getrandom_test_defaults", crate_name: "getrandom", + cargo_env_compat: true, + cargo_pkg_version: "0.2.3", test_suites: ["general-tests"], auto_gen_config: true, edition: "2018", @@ -78,24 +73,10 @@ rust_defaults { ], } -rust_test_host { - name: "getrandom_host_test_tests_custom", - defaults: ["getrandom_defaults_getrandom"], - srcs: ["tests/custom.rs"], - test_options: { - unit_test: true, - }, -} - rust_test { - name: "getrandom_device_test_tests_custom", - defaults: ["getrandom_defaults_getrandom"], - srcs: ["tests/custom.rs"], -} - -rust_test_host { - name: "getrandom_host_test_tests_normal", - defaults: ["getrandom_defaults_getrandom"], + name: "getrandom_test_tests_normal", + defaults: ["getrandom_test_defaults"], + host_supported: true, srcs: ["tests/normal.rs"], test_options: { unit_test: true, @@ -103,30 +84,21 @@ rust_test_host { } rust_test { - name: "getrandom_device_test_tests_normal", - defaults: ["getrandom_defaults_getrandom"], - srcs: ["tests/normal.rs"], -} - -rust_test_host { - name: "getrandom_host_test_tests_rdrand", - defaults: ["getrandom_defaults_getrandom"], + name: "getrandom_test_tests_rdrand", + defaults: ["getrandom_test_defaults"], + host_supported: true, srcs: ["tests/rdrand.rs"], test_options: { unit_test: true, }, } -rust_test { - name: "getrandom_device_test_tests_rdrand", - defaults: ["getrandom_defaults_getrandom"], - srcs: ["tests/rdrand.rs"], -} - rust_library { name: "libgetrandom", host_supported: true, crate_name: "getrandom", + cargo_env_compat: true, + cargo_pkg_version: "0.2.3", srcs: ["src/lib.rs"], edition: "2018", features: ["std"], @@ -134,8 +106,8 @@ rust_library { "libcfg_if", "liblibc", ], + apex_available: [ + "//apex_available:platform", + "com.android.virt", + ], } - -// dependent_library ["feature_list"] -// cfg-if-1.0.0 -// libc-0.2.87 diff --git a/CHANGELOG.md b/CHANGELOG.md index c3ca728..0ae351a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.2.3] - 2021-04-10 +### Changed +- Replace build.rs with link attributes. [#205] +- Add support for getrandom syscall on DragonFly BSD. [#210] +- Improve Node.js detection. [#215] + +[#205]: https://github.com/rust-random/getrandom/pull/205 +[#210]: https://github.com/rust-random/getrandom/pull/210 +[#215]: https://github.com/rust-random/getrandom/pull/215 + ## [0.2.2] - 2021-01-19 ### Changed - Forward `rustc-dep-of-std` to dependencies. [#198] @@ -13,7 +13,7 @@ [package] edition = "2018" name = "getrandom" -version = "0.2.2" +version = "0.2.3" authors = ["The Rand Project Developers"] exclude = [".*"] description = "A small cross-platform library for retrieving random data from system source" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index dabf016..4ab9ad6 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "getrandom" -version = "0.2.2" # Also update html_root_url in lib.rs when bumping this +version = "0.2.3" # Also update html_root_url in lib.rs when bumping this edition = "2018" authors = ["The Rand Project Developers"] license = "MIT OR Apache-2.0" @@ -7,13 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/getrandom/getrandom-0.2.2.crate" + value: "https://static.crates.io/crates/getrandom/getrandom-0.2.3.crate" } - version: "0.2.2" + version: "0.2.3" license_type: NOTICE last_upgrade_date { year: 2021 - month: 3 - day: 3 + month: 6 + day: 21 } } diff --git a/TEST_MAPPING b/TEST_MAPPING index efa80d7..42e04b4 100644 --- a/TEST_MAPPING +++ b/TEST_MAPPING @@ -1,32 +1,120 @@ // Generated by update_crate_tests.py for tests that depend on this crate. { + "imports": [ + { + "path": "external/rust/crates/base64" + }, + { + "path": "external/rust/crates/cast" + }, + { + "path": "external/rust/crates/crc32fast" + }, + { + "path": "external/rust/crates/crossbeam-deque" + }, + { + "path": "external/rust/crates/crossbeam-epoch" + }, + { + "path": "external/rust/crates/crossbeam-queue" + }, + { + "path": "external/rust/crates/crossbeam-utils" + }, + { + "path": "external/rust/crates/mio" + }, + { + "path": "external/rust/crates/quickcheck" + }, + { + "path": "external/rust/crates/rand_chacha" + }, + { + "path": "external/rust/crates/rand_core" + }, + { + "path": "external/rust/crates/rand_xorshift" + }, + { + "path": "external/rust/crates/regex" + }, + { + "path": "external/rust/crates/ryu" + }, + { + "path": "external/rust/crates/tokio" + } + ], "presubmit": [ { + "name": "ZipFuseTest" + }, + { + "name": "apkdmverity.test" + }, + { + "name": "authfs_device_test_src_lib" + }, + { + "name": "getrandom_test_src_lib" + }, + { + "name": "getrandom_test_tests_normal" + }, + { + "name": "getrandom_test_tests_rdrand" + }, + { "name": "keystore2_test" }, { - "name": "getrandom_device_test_tests_custom" + "name": "keystore2_test_utils_test" + }, + { + "name": "legacykeystore_test" + }, + { + "name": "microdroid_manager_test" + }, + { + "name": "virtualizationservice_device_test" + } + ], + "presubmit-rust": [ + { + "name": "ZipFuseTest" + }, + { + "name": "apkdmverity.test" + }, + { + "name": "authfs_device_test_src_lib" }, { - "name": "rand_xorshift_device_test_tests_mod" + "name": "getrandom_test_src_lib" }, { - "name": "getrandom_device_test_tests_normal" + "name": "getrandom_test_tests_normal" }, { - "name": "rand_xorshift_device_test_src_lib" + "name": "getrandom_test_tests_rdrand" + }, + { + "name": "keystore2_test" }, { - "name": "getrandom_device_test_tests_rdrand" + "name": "keystore2_test_utils_test" }, { - "name": "rand_core_device_test_src_lib" + "name": "legacykeystore_test" }, { - "name": "getrandom_device_test_src_lib" + "name": "microdroid_manager_test" }, { - "name": "vpnprofilestore_test" + "name": "virtualizationservice_device_test" } ] } diff --git a/build.rs b/build.rs deleted file mode 100644 index 95f4b90..0000000 --- a/build.rs +++ /dev/null @@ -1,14 +0,0 @@ -#![deny(warnings)] - -use std::env; - -fn main() { - let target = env::var("TARGET").expect("TARGET was not set"); - if target.contains("windows") { - // for BCryptGenRandom - println!("cargo:rustc-link-lib=bcrypt"); - } else if target.contains("apple-ios") { - // for SecRandomCopyBytes and kSecRandomDefault - println!("cargo:rustc-link-lib=framework=Security"); - } -} diff --git a/cargo2android.json b/cargo2android.json new file mode 100644 index 0000000..b73c7b4 --- /dev/null +++ b/cargo2android.json @@ -0,0 +1,11 @@ +{ + "apex-available": [ + "//apex_available:platform", + "com.android.virt" + ], + "dependencies": true, + "device": true, + "features": "std", + "run": true, + "tests": true +}
\ No newline at end of file diff --git a/src/dragonfly.rs b/src/dragonfly.rs new file mode 100644 index 0000000..f27e906 --- /dev/null +++ b/src/dragonfly.rs @@ -0,0 +1,26 @@ +// Copyright 2021 Developers of the Rand project. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Implementation for DragonFly BSD +use crate::{ + use_file, + util_libc::{sys_fill_exact, Weak}, + Error, +}; + +pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { + static GETRANDOM: Weak = unsafe { Weak::new("getrandom\0") }; + type GetRandomFn = unsafe extern "C" fn(*mut u8, libc::size_t, libc::c_uint) -> libc::ssize_t; + + if let Some(fptr) = GETRANDOM.ptr() { + let func: GetRandomFn = unsafe { core::mem::transmute(fptr) }; + return sys_fill_exact(dest, |buf| unsafe { func(buf.as_mut_ptr(), buf.len(), 0) }); + } else { + use_file::getrandom_inner(dest) + } +} diff --git a/src/error.rs b/src/error.rs index 48abdc1..0f52186 100644 --- a/src/error.rs +++ b/src/error.rs @@ -162,7 +162,7 @@ fn internal_desc(error: Error) -> Option<&'static str> { Error::WINDOWS_RTL_GEN_RANDOM => Some("RtlGenRandom: Windows system function failure"), Error::FAILED_RDRAND => Some("RDRAND: failed multiple times: CPU issue likely"), Error::NO_RDRAND => Some("RDRAND: instruction not supported"), - Error::WEB_CRYPTO => Some("Web API self.crypto is unavailable"), + Error::WEB_CRYPTO => Some("Web Crypto API is unavailable"), Error::WEB_GET_RANDOM_VALUES => Some("Web API crypto.getRandomValues is unavailable"), Error::VXWORKS_RAND_SECURE => Some("randSecure: VxWorks RNG module is not initialized"), Error::NODE_CRYPTO => Some("Node.js crypto module is unavailable"), @@ -11,7 +11,7 @@ extern crate std; use std::thread_local; use js_sys::Uint8Array; -use wasm_bindgen::prelude::*; +use wasm_bindgen::{prelude::*, JsCast}; // Maximum is 65536 bytes see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues const BROWSER_CRYPTO_BUFFER_SIZE: usize = 256; @@ -57,50 +57,65 @@ pub(crate) fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> { } fn getrandom_init() -> Result<RngSource, Error> { - if let Ok(self_) = Global::get_self() { - // If `self` is defined then we're in a browser somehow (main window - // or web worker). We get `self.crypto` (called `msCrypto` on IE), so we - // can call `crypto.getRandomValues`. If `crypto` isn't defined, we - // assume we're in an older web browser and the OS RNG isn't available. - - let crypto: BrowserCrypto = match (self_.crypto(), self_.ms_crypto()) { - (crypto, _) if !crypto.is_undefined() => crypto, - (_, crypto) if !crypto.is_undefined() => crypto, - _ => return Err(Error::WEB_CRYPTO), - }; - - let buf = Uint8Array::new_with_length(BROWSER_CRYPTO_BUFFER_SIZE as u32); - return Ok(RngSource::Browser(crypto, buf)); + let global: Global = js_sys::global().unchecked_into(); + if is_node(&global) { + let crypto = require("crypto").map_err(|_| Error::NODE_CRYPTO)?; + return Ok(RngSource::Node(crypto)); } - let crypto = MODULE.require("crypto").map_err(|_| Error::NODE_CRYPTO)?; - Ok(RngSource::Node(crypto)) + // Assume we are in some Web environment (browser or web worker). We get + // `self.crypto` (called `msCrypto` on IE), so we can call + // `crypto.getRandomValues`. If `crypto` isn't defined, we assume that + // we are in an older web browser and the OS RNG isn't available. + let crypto = match (global.crypto(), global.ms_crypto()) { + (c, _) if c.is_object() => c, + (_, c) if c.is_object() => c, + _ => return Err(Error::WEB_CRYPTO), + }; + + let buf = Uint8Array::new_with_length(BROWSER_CRYPTO_BUFFER_SIZE as u32); + Ok(RngSource::Browser(crypto, buf)) +} + +// Taken from https://www.npmjs.com/package/browser-or-node +fn is_node(global: &Global) -> bool { + let process = global.process(); + if process.is_object() { + let versions = process.versions(); + if versions.is_object() { + return versions.node().is_string(); + } + } + false } #[wasm_bindgen] extern "C" { - type Global; - #[wasm_bindgen(getter, catch, static_method_of = Global, js_class = self, js_name = self)] - fn get_self() -> Result<Self_, JsValue>; + type Global; // Return type of js_sys::global() - type Self_; + // Web Crypto API (https://www.w3.org/TR/WebCryptoAPI/) #[wasm_bindgen(method, getter, js_name = "msCrypto")] - fn ms_crypto(me: &Self_) -> BrowserCrypto; + fn ms_crypto(this: &Global) -> BrowserCrypto; #[wasm_bindgen(method, getter)] - fn crypto(me: &Self_) -> BrowserCrypto; - + fn crypto(this: &Global) -> BrowserCrypto; type BrowserCrypto; #[wasm_bindgen(method, js_name = getRandomValues, catch)] - fn get_random_values(me: &BrowserCrypto, buf: &Uint8Array) -> Result<(), JsValue>; - - #[wasm_bindgen(js_name = module)] - static MODULE: NodeModule; - - type NodeModule; - #[wasm_bindgen(method, catch)] - fn require(this: &NodeModule, s: &str) -> Result<NodeCrypto, JsValue>; + fn get_random_values(this: &BrowserCrypto, buf: &Uint8Array) -> Result<(), JsValue>; + // Node JS crypto module (https://nodejs.org/api/crypto.html) + #[wasm_bindgen(catch, js_name = "module.require")] + fn require(s: &str) -> Result<NodeCrypto, JsValue>; type NodeCrypto; #[wasm_bindgen(method, js_name = randomFillSync, catch)] - fn random_fill_sync(crypto: &NodeCrypto, buf: &mut [u8]) -> Result<(), JsValue>; + fn random_fill_sync(this: &NodeCrypto, buf: &mut [u8]) -> Result<(), JsValue>; + + // Node JS process Object (https://nodejs.org/api/process.html) + #[wasm_bindgen(method, getter)] + fn process(this: &Global) -> Process; + type Process; + #[wasm_bindgen(method, getter)] + fn versions(this: &Process) -> Versions; + type Versions; + #[wasm_bindgen(method, getter)] + fn node(this: &Versions) -> JsValue; } @@ -19,7 +19,7 @@ //! | FreeBSD | `*‑freebsd` | [`getrandom()`][21] if available, otherwise [`kern.arandom`][5] //! | OpenBSD | `*‑openbsd` | [`getentropy`][6] //! | NetBSD | `*‑netbsd` | [`kern.arandom`][7] -//! | Dragonfly BSD | `*‑dragonfly` | [`/dev/random`][8] +//! | Dragonfly BSD | `*‑dragonfly` | [`getrandom()`][22] if available, otherwise [`/dev/random`][8] //! | Solaris, illumos | `*‑solaris`, `*‑illumos` | [`getrandom()`][9] if available, otherwise [`/dev/random`][10] //! | Fuchsia OS | `*‑fuchsia` | [`cprng_draw`][11] //! | Redox | `*‑redox` | [`rand:`][12] @@ -27,7 +27,7 @@ //! | SGX | `x86_64‑*‑sgx` | [RDRAND][18] //! | VxWorks | `*‑wrs‑vxworks‑*` | `randABytes` after checking entropy pool initialization with `randSecure` //! | Emscripten | `*‑emscripten` | `/dev/random` (identical to `/dev/urandom`) -//! | WASI | `wasm32‑wasi` | [`__wasi_random_get`][17] +//! | WASI | `wasm32‑wasi` | [`random_get`][17] //! | Web Browser | `wasm32‑*‑unknown` | [`Crypto.getRandomValues()`][14], see [WebAssembly support][16] //! | Node.js | `wasm32‑*‑unknown` | [`crypto.randomBytes`][15], see [WebAssembly support][16] //! @@ -59,7 +59,8 @@ //! This crate fully supports the //! [`wasm32-wasi`](https://github.com/CraneStation/wasi) and //! [`wasm32-unknown-emscripten`](https://www.hellorust.com/setup/emscripten/) -//! targets. However, the `wasm32-unknown-unknown` target is not automatically +//! targets. However, the `wasm32-unknown-unknown` target (i.e. the target used +//! by `wasm-pack`) is not automatically //! supported since, from the target name alone, we cannot deduce which //! JavaScript interface is in use (or if JavaScript is available at all). //! @@ -124,7 +125,7 @@ //! [4]: https://developer.apple.com/documentation/security/1399291-secrandomcopybytes?language=objc //! [5]: https://www.freebsd.org/cgi/man.cgi?query=random&sektion=4 //! [6]: https://man.openbsd.org/getentropy.2 -//! [7]: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7+NetBSD-8.0 +//! [7]: https://man.netbsd.org/sysctl.7 //! [8]: https://leaf.dragonflybsd.org/cgi/web-man?command=random§ion=4 //! [9]: https://docs.oracle.com/cd/E88353_01/html/E37841/getrandom-2.html //! [10]: https://docs.oracle.com/cd/E86824_01/html/E54777/random-7d.html @@ -133,16 +134,17 @@ //! [14]: https://www.w3.org/TR/WebCryptoAPI/#Crypto-method-getRandomValues //! [15]: https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback //! [16]: #webassembly-support -//! [17]: https://github.com/WebAssembly/WASI/blob/master/design/WASI-core.md#__wasi_random_get +//! [17]: https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#-random_getbuf-pointeru8-buf_len-size---errno //! [18]: https://software.intel.com/en-us/articles/intel-digital-random-number-generator-drng-software-implementation-guide //! [19]: https://www.unix.com/man-page/mojave/2/getentropy/ //! [20]: https://www.unix.com/man-page/mojave/4/random/ //! [21]: https://www.freebsd.org/cgi/man.cgi?query=getrandom&manpath=FreeBSD+12.0-stable +//! [22]: https://leaf.dragonflybsd.org/cgi/web-man?command=getrandom #![doc( html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png", html_favicon_url = "https://www.rust-lang.org/favicon.ico", - html_root_url = "https://docs.rs/getrandom/0.2.2" + html_root_url = "https://docs.rs/getrandom/0.2.3" )] #![no_std] #![warn(rust_2018_idioms, unused_lifetimes, missing_docs)] @@ -166,8 +168,8 @@ pub use crate::error::Error; // // These should all provide getrandom_inner with the same signature as getrandom. cfg_if! { - if #[cfg(any(target_os = "dragonfly", target_os = "emscripten", - target_os = "haiku", target_os = "redox"))] { + if #[cfg(any(target_os = "emscripten", target_os = "haiku", + target_os = "redox"))] { mod util_libc; #[path = "use_file.rs"] mod imp; } else if #[cfg(any(target_os = "android", target_os = "linux"))] { @@ -181,6 +183,10 @@ cfg_if! { } else if #[cfg(any(target_os = "freebsd", target_os = "netbsd"))] { mod util_libc; #[path = "bsd_arandom.rs"] mod imp; + } else if #[cfg(target_os = "dragonfly")] { + mod util_libc; + mod use_file; + #[path = "dragonfly.rs"] mod imp; } else if #[cfg(target_os = "fuchsia")] { #[path = "fuchsia.rs"] mod imp; } else if #[cfg(target_os = "ios")] { @@ -209,6 +215,11 @@ cfg_if! { #[path = "js.rs"] mod imp; } else if #[cfg(feature = "custom")] { use custom as imp; + } else if #[cfg(all(target_arch = "wasm32", target_os = "unknown"))] { + compile_error!("the wasm32-unknown-unknown target is not supported by \ + default, you may need to enable the \"js\" feature. \ + For more information see: \ + https://docs.rs/getrandom/#webassembly-support"); } else { compile_error!("target is not supported, for more information see: \ https://docs.rs/getrandom/#unsupported-targets"); diff --git a/src/windows.rs b/src/windows.rs index 56b3d07..643badd 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -11,6 +11,7 @@ use core::{ffi::c_void, num::NonZeroU32, ptr}; const BCRYPT_USE_SYSTEM_PREFERRED_RNG: u32 = 0x00000002; +#[link(name = "bcrypt")] extern "system" { fn BCryptGenRandom( hAlgorithm: *mut c_void, |