Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: op_get_random_values #2327

Merged
merged 4 commits into from May 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/BUILD.gn
Expand Up @@ -77,6 +77,7 @@ ts_sources = [
"../js/files.ts",
"../js/flatbuffers.ts",
"../js/form_data.ts",
"../js/get_random_values.ts",
"../js/globals.ts",
"../js/headers.ts",
"../js/io.ts",
Expand Down
1 change: 1 addition & 0 deletions cli/main.rs
Expand Up @@ -11,6 +11,7 @@ extern crate clap;
extern crate deno;
#[cfg(unix)]
extern crate nix;
extern crate rand;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not necessary IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My rustc demands it.


mod ansi;
pub mod compiler;
Expand Down
3 changes: 3 additions & 0 deletions cli/msg.fbs
Expand Up @@ -19,6 +19,7 @@ union Any {
FetchRes,
FormatError,
FormatErrorRes,
GetRandomValues,
GlobalTimer,
GlobalTimerRes,
GlobalTimerStop,
Expand Down Expand Up @@ -579,4 +580,6 @@ table Seek {
whence: uint;
}

table GetRandomValues {}

root_type Base;
13 changes: 13 additions & 0 deletions cli/ops.rs
Expand Up @@ -13,6 +13,7 @@ use crate::js_errors::apply_source_map;
use crate::js_errors::JSErrorColor;
use crate::msg;
use crate::msg_util;
use crate::rand;
use crate::repl;
use crate::resolve_addr::resolve_addr;
use crate::resources;
Expand All @@ -39,6 +40,7 @@ use futures::Sink;
use futures::Stream;
use hyper;
use hyper::rt::Future;
use rand::{thread_rng, Rng};
use remove_dir_all::remove_dir_all;
use std;
use std::convert::From;
Expand Down Expand Up @@ -195,6 +197,7 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<OpCreator> {
msg::Any::Exit => Some(op_exit),
msg::Any::Fetch => Some(op_fetch),
msg::Any::FormatError => Some(op_format_error),
msg::Any::GetRandomValues => Some(op_get_random_values),
msg::Any::GlobalTimer => Some(op_global_timer),
msg::Any::GlobalTimerStop => Some(op_global_timer_stop),
msg::Any::IsTTY => Some(op_is_tty),
Expand Down Expand Up @@ -2182,3 +2185,13 @@ fn op_host_post_message(
});
Box::new(op)
}

fn op_get_random_values(
_state: &ThreadSafeState,
_base: &msg::Base<'_>,
data: Option<PinnedBuf>
) -> Box<OpWithError> {
assert!(data.is_some());
thread_rng().fill(&mut data.unwrap()[..]);
Box::new(ok_future(empty_buf()))
}
1 change: 1 addition & 0 deletions js/deno.ts
Expand Up @@ -81,6 +81,7 @@ export {
export { inspect } from "./console";
export { build, platform, OperatingSystem, Arch } from "./build";
export { version } from "./version";
export { getRandomValues, getRandomValuesSync } from "./get_random_values";
ry marked this conversation as resolved.
Show resolved Hide resolved
export const args: string[] = [];

// These are internal Deno APIs. We are marking them as internal so they do not
Expand Down
36 changes: 36 additions & 0 deletions js/get_random_values.ts
@@ -0,0 +1,36 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/cli/msg_generated";
import * as flatbuffers from "./flatbuffers";
import * as dispatch from "./dispatch";
import { assert } from "./util";

function req(
typedArray: ArrayBufferView
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset, ArrayBufferView] {
assert(typedArray != null);
const builder = flatbuffers.createBuilder();
const inner = msg.GetRandomValues.createGetRandomValues(builder);
return [builder, msg.Any.GetRandomValues, inner, typedArray];
}

/** Collects cryptographically secure random values. Throws if given anything
* but an ArrayBufferView. The underlying CSPRNG in use is Rust's
* `rand::rngs::ThreadRng`.
*
* const arr = new Uint8Array(32);
* await Deno.getRandomValues(arr);
*/
export async function getRandomValues(typedArray: ArrayBufferView): Promise<void> {
await dispatch.sendAsync(...req(typedArray));
}

/** Synchronously collects cryptographically secure random values. Throws if
* given anything but an ArrayBufferView. The underlying CSPRNG in use is Rust's
* `rand::rngs::ThreadRng`.
*
* const arr = new Uint8Array(32);
* Deno.getRandomValuesSync(arr);
*/
export function getRandomValuesSync(typedArray: ArrayBufferView): void {
dispatch.sendSync(...req(typedArray));
ry marked this conversation as resolved.
Show resolved Hide resolved
}
33 changes: 33 additions & 0 deletions js/get_random_values_test.ts
@@ -0,0 +1,33 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, assertNotEquals } from "./test_util.ts";

test(async function csprngBytes(): Promise<void> {
const arr = new Uint8Array(32);
await Deno.getRandomValues(arr);
assertNotEquals(arr, new Uint8Array(32));
});

test(async function csprngValues(): Promise<void> {
const arr = new Int16Array(4);
await Deno.getRandomValues(arr);
assertNotEquals(arr, new Int16Array(4));
});

test(async function csprngBigints(): Promise<void> {
const arr = new BigInt64Array(4);
await Deno.getRandomValues(arr);
assertNotEquals(arr, new BigInt64Array(4));
});

test(async function csprngGivenADataView(): Promise<void> {
const arr = new Int32Array(4);
const view = new DataView(arr.buffer);
await Deno.getRandomValues(view);
assertNotEquals(arr, new Int32Array(4));
});

test(function csprngValuesSync(): void {
const arr = new Uint32Array(8);
Deno.getRandomValuesSync(arr);
assertNotEquals(arr, new Uint32Array(8));
});
1 change: 1 addition & 0 deletions js/unit_tests.ts
Expand Up @@ -19,6 +19,7 @@ import "./fetch_test.ts";
import "./file_test.ts";
import "./files_test.ts";
import "./form_data_test.ts";
import "./get_random_values_test.ts";
import "./globals_test.ts";
import "./headers_test.ts";
import "./link_test.ts";
Expand Down