Repository navigation
Add initial Web Crypto support and QuotaExceededError #45
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
Open
karthik2804
wants to merge
1
commit into
bytecodealliance:main
Choose a base branch
from
karthik2804:crypto
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,950
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| [package] | ||
| name = "web-crypto" | ||
| version.workspace = true | ||
| repository.workspace = true | ||
| license.workspace = true | ||
| authors.workspace = true | ||
| edition.workspace = true | ||
|
|
||
| [features] | ||
| debugmozjs = ["js/debugmozjs"] | ||
|
|
||
| [lib] | ||
| # No unit tests. This skips building the (SpiderMonkey-sized) lib test harness binary. | ||
| test = false | ||
|
|
||
| [dependencies] | ||
| core-runtime.workspace = true | ||
| getrandom.workspace = true | ||
| js.workspace = true | ||
| sha1.workspace = true | ||
| sha2.workspace = true | ||
| web-globals.workspace = true | ||
|
|
||
| [lints] | ||
| workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,126 @@ | ||||||||||||||||||||||
| // SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| //! Algorithm identifiers and | ||||||||||||||||||||||
| //! <https://w3c.github.io/webcrypto/#algorithm-normalization-normalize-an-algorithm>, for the | ||||||||||||||||||||||
| //! "digest" operation, the only one implemented so far. (Numbers like "Step 2.5" count the | ||||||||||||||||||||||
| //! items of the spec's nested list in order.) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| use core_runtime::webidl_union; | ||||||||||||||||||||||
| use js::conversion::FromJSVal; | ||||||||||||||||||||||
| use js::error::{throw_type_error, ExnThrown}; | ||||||||||||||||||||||
| use js::gc::scope::Scope; | ||||||||||||||||||||||
| use js::Object; | ||||||||||||||||||||||
| use sha1::Sha1; | ||||||||||||||||||||||
| use sha2::{Digest, Sha256, Sha384, Sha512}; | ||||||||||||||||||||||
| use web_globals::dom_exception::throw_dom_exception; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// <https://w3c.github.io/webcrypto/#dfn-AlgorithmIdentifier> | ||||||||||||||||||||||
| /// | ||||||||||||||||||||||
| /// `(object or DOMString)`. An object stands for an `Algorithm` dictionary, whose only member is | ||||||||||||||||||||||
| /// the required `name`. | ||||||||||||||||||||||
| #[webidl_union] | ||||||||||||||||||||||
| pub enum AlgorithmIdentifier<'a> { | ||||||||||||||||||||||
| Object(Object<'a>), | ||||||||||||||||||||||
| Name(String), | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// The hash functions `digest()` supports. | ||||||||||||||||||||||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||||||||||||||||||||||
| pub enum HashAlgorithm { | ||||||||||||||||||||||
| Sha1, | ||||||||||||||||||||||
| Sha256, | ||||||||||||||||||||||
| Sha384, | ||||||||||||||||||||||
| Sha512, | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| impl HashAlgorithm { | ||||||||||||||||||||||
| /// The `supportedAlgorithms` entries for "digest", looked up by case-insensitive match. | ||||||||||||||||||||||
| fn from_name(name: &str) -> Option<Self> { | ||||||||||||||||||||||
| match name.to_ascii_uppercase().as_str() { | ||||||||||||||||||||||
| "SHA-1" => Some(Self::Sha1), | ||||||||||||||||||||||
| "SHA-256" => Some(Self::Sha256), | ||||||||||||||||||||||
| "SHA-384" => Some(Self::Sha384), | ||||||||||||||||||||||
| "SHA-512" => Some(Self::Sha512), | ||||||||||||||||||||||
| _ => None, | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// The digest operation's result for each hash function: the message digest of _data_. | ||||||||||||||||||||||
| pub fn digest(self, data: &[u8]) -> Vec<u8> { | ||||||||||||||||||||||
| match self { | ||||||||||||||||||||||
| Self::Sha1 => Sha1::digest(data).to_vec(), | ||||||||||||||||||||||
| Self::Sha256 => Sha256::digest(data).to_vec(), | ||||||||||||||||||||||
| Self::Sha384 => Sha384::digest(data).to_vec(), | ||||||||||||||||||||||
| Self::Sha512 => Sha512::digest(data).to_vec(), | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /// <https://w3c.github.io/webcrypto/#algorithm-normalization-normalize-an-algorithm>, with _op_ | ||||||||||||||||||||||
| /// fixed to "digest", whose `supportedAlgorithms` are the four SHA functions. None of them has | ||||||||||||||||||||||
| /// members beyond `name`, so their _desiredType_ is the plain `Algorithm` dictionary. | ||||||||||||||||||||||
| pub fn normalize_digest_algorithm( | ||||||||||||||||||||||
| scope: &Scope<'_>, | ||||||||||||||||||||||
| identifier: AlgorithmIdentifier<'_>, | ||||||||||||||||||||||
| ) -> Result<HashAlgorithm, ExnThrown> { | ||||||||||||||||||||||
| // Step 1: If _alg_ is an instance of a DOMString: Return the result of running the | ||||||||||||||||||||||
| // `normalize an algorithm` algorithm, with _alg_ set to a new `Algorithm` | ||||||||||||||||||||||
| // dictionary whose `name` attribute is _alg_, and with _op_ set to _op_. | ||||||||||||||||||||||
| // (For that new dictionary, steps 2.1-2.4 just yield _alg_ as _algName_, so this | ||||||||||||||||||||||
| // skips straight to step 2.5.) | ||||||||||||||||||||||
| let alg_name = match identifier { | ||||||||||||||||||||||
| AlgorithmIdentifier::Name(name) => name, | ||||||||||||||||||||||
| // Step 2: If _alg_ is an object: | ||||||||||||||||||||||
| AlgorithmIdentifier::Object(object) => { | ||||||||||||||||||||||
| // Step 2.1: Let _registeredAlgorithms_ be the associative container stored at the | ||||||||||||||||||||||
| // _op_ key of `supportedAlgorithms`. | ||||||||||||||||||||||
| // (That is `HashAlgorithm::from_name`.) | ||||||||||||||||||||||
| // Step 2.2: Let _initialAlg_ be the result of converting the ECMAScript object | ||||||||||||||||||||||
| // represented by _alg_ to the IDL dictionary type `Algorithm`. | ||||||||||||||||||||||
| // Step 2.3: If an error occurred, return the error and terminate this algorithm. | ||||||||||||||||||||||
| // (`name` is a required member, so a missing one is a `TypeError`.) | ||||||||||||||||||||||
| let value = object.get_property(scope, c"name")?; | ||||||||||||||||||||||
| if value.is_undefined() { | ||||||||||||||||||||||
| return Err(throw_type_error( | ||||||||||||||||||||||
| scope, | ||||||||||||||||||||||
| c"Algorithm: Missing required property 'name'", | ||||||||||||||||||||||
| )); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| // Step 2.4: Let _algName_ be the value of the `name` attribute of _initialAlg_. | ||||||||||||||||||||||
| String::from_jsval_throwing(scope, value, Default::default())? | ||||||||||||||||||||||
|
Comment on lines
+82
to
+90
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be made a little bit nicer by introducing an /// <https://w3c.github.io/webcrypto/#dfn-Algorithm>
#[webidl_dictionary]
pub struct Algorithm {
pub name: String,
}With this code then just becoming
Suggested change
|
||||||||||||||||||||||
| } | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
| // Step 2.5: If _registeredAlgorithms_ contains a key that is a `case-insensitive` string | ||||||||||||||||||||||
| // match for _algName_: set _algName_ to the value of the matching key and let | ||||||||||||||||||||||
| // _desiredType_ be the IDL dictionary type stored at _algName_ in | ||||||||||||||||||||||
| // _registeredAlgorithms_. Otherwise: return a new `NotSupportedError` and | ||||||||||||||||||||||
| // terminate this algorithm. | ||||||||||||||||||||||
| let Some(hash) = HashAlgorithm::from_name(&alg_name) else { | ||||||||||||||||||||||
| return Err(throw_dom_exception( | ||||||||||||||||||||||
| scope, | ||||||||||||||||||||||
| "NotSupportedError", | ||||||||||||||||||||||
| &format!("Unrecognized algorithm name: {alg_name}"), | ||||||||||||||||||||||
| )); | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
| // Step 2.6: Let _normalizedAlgorithm_ be the result of converting the ECMAScript object | ||||||||||||||||||||||
| // represented by _alg_ to the IDL dictionary type _desiredType_. | ||||||||||||||||||||||
| // Step 2.7: Set the `name` attribute of _normalizedAlgorithm_ to _algName_. | ||||||||||||||||||||||
| // Step 2.8: If an error occurred, return the error and terminate this algorithm. | ||||||||||||||||||||||
| // (_desiredType_ is `Algorithm` itself, already converted by steps 2.2-2.3. The | ||||||||||||||||||||||
| // returned `HashAlgorithm` stands for the matched key.) | ||||||||||||||||||||||
| // Step 2.9: Let _dictionaries_ be a list consisting of the IDL dictionary type _desiredType_ | ||||||||||||||||||||||
| // and all of _desiredType_'s inherited dictionaries, in order from least to most | ||||||||||||||||||||||
| // derived. | ||||||||||||||||||||||
| // Step 2.10: For each dictionary _dictionary_ in _dictionaries_, for each dictionary member | ||||||||||||||||||||||
| // _member_ declared on _dictionary_, in order: if _member_ is of the type | ||||||||||||||||||||||
| // `BufferSource` and is present, replace its value with the result of `getting a | ||||||||||||||||||||||
| // copy of the bytes held by` it; if it is of the type `HashAlgorithmIdentifier`, | ||||||||||||||||||||||
| // replace its value with the result of `normalizing an algorithm` with _op_ set to | ||||||||||||||||||||||
| // "digest"; if it is of the type `AlgorithmIdentifier`, replace its value with the | ||||||||||||||||||||||
| // result of `normalizing an algorithm` with _op_ set to the operation defined by | ||||||||||||||||||||||
| // the specification that defines the algorithm identified by _algName_. | ||||||||||||||||||||||
| // (`Algorithm` has only `name`, so there is nothing to do.) | ||||||||||||||||||||||
| // Step 2.11: If an error occurred, return the error and terminate this algorithm. | ||||||||||||||||||||||
| // Step 2.12: Return _normalizedAlgorithm_. | ||||||||||||||||||||||
| Ok(hash) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please always make sure you're using the latest versions of dependencies. In this case,
0.11is available for both of these.