2023-01-02 16:00:42 -05:00
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
2020-09-06 02:34:02 +02:00
2022-01-11 01:44:47 -03:00
use aes_kw ::KekAes128 ;
use aes_kw ::KekAes192 ;
use aes_kw ::KekAes256 ;
2021-07-06 17:46:04 +05:30
use deno_core ::error ::custom_error ;
use deno_core ::error ::not_supported ;
use deno_core ::error ::type_error ;
2020-09-14 18:48:57 +02:00
use deno_core ::error ::AnyError ;
2022-03-14 23:14:15 +05:30
use deno_core ::op ;
2023-06-22 23:37:56 +02:00
use deno_core ::ToJsBuffer ;
2022-03-14 23:14:15 +05:30
2023-05-14 15:40:01 -06:00
use deno_core ::task ::spawn_blocking ;
2023-06-22 23:37:56 +02:00
use deno_core ::JsBuffer ;
2020-09-10 09:57:45 -04:00
use deno_core ::OpState ;
2021-07-06 17:46:04 +05:30
use serde ::Deserialize ;
2022-01-11 01:44:47 -03:00
use shared ::operation_error ;
2021-07-06 17:46:04 +05:30
2021-12-16 13:28:43 -03:00
use p256 ::elliptic_curve ::sec1 ::FromEncodedPoint ;
2022-06-20 16:53:57 +05:30
use p256 ::pkcs8 ::DecodePrivateKey ;
2021-07-06 17:46:04 +05:30
use rand ::rngs ::OsRng ;
2020-09-18 20:39:47 +02:00
use rand ::rngs ::StdRng ;
2019-08-14 17:03:02 +02:00
use rand ::thread_rng ;
use rand ::Rng ;
2021-04-28 18:41:50 +02:00
use rand ::SeedableRng ;
2021-06-06 18:57:10 +08:00
use ring ::digest ;
2021-09-12 02:24:03 +05:30
use ring ::hkdf ;
2021-07-06 17:46:04 +05:30
use ring ::hmac ::Algorithm as HmacAlgorithm ;
use ring ::hmac ::Key as HmacKey ;
2021-08-26 16:18:07 +05:30
use ring ::pbkdf2 ;
2021-07-06 17:46:04 +05:30
use ring ::rand as RingRand ;
use ring ::signature ::EcdsaKeyPair ;
use ring ::signature ::EcdsaSigningAlgorithm ;
2021-09-12 02:19:53 +05:30
use ring ::signature ::EcdsaVerificationAlgorithm ;
use ring ::signature ::KeyPair ;
2022-06-20 16:53:57 +05:30
use rsa ::pkcs1 ::DecodeRsaPrivateKey ;
use rsa ::pkcs1 ::DecodeRsaPublicKey ;
2021-08-06 13:40:50 +05:30
use rsa ::RsaPrivateKey ;
use rsa ::RsaPublicKey ;
2021-07-06 17:46:04 +05:30
use sha1 ::Sha1 ;
use sha2 ::Sha256 ;
use sha2 ::Sha384 ;
use sha2 ::Sha512 ;
2023-01-18 09:18:41 -06:00
use signature ::RandomizedSigner ;
use signature ::Signer ;
use signature ::Verifier ;
2022-01-11 01:44:47 -03:00
use std ::convert ::TryFrom ;
2022-06-20 16:53:57 +05:30
use std ::num ::NonZeroU32 ;
2022-03-16 20:25:44 -04:00
use std ::path ::PathBuf ;
2019-08-14 17:03:02 +02:00
2020-11-14 02:31:57 +05:30
pub use rand ; // Re-export rand
2021-12-20 20:37:36 +05:30
mod decrypt ;
2022-09-27 17:43:42 +05:30
mod ed25519 ;
2021-12-15 22:18:26 +01:00
mod encrypt ;
2021-12-13 13:22:03 +01:00
mod export_key ;
2021-12-13 18:45:08 +01:00
mod generate_key ;
2021-12-10 15:06:03 +01:00
mod import_key ;
2021-07-06 17:46:04 +05:30
mod key ;
2021-12-10 15:06:03 +01:00
mod shared ;
2022-09-27 17:43:42 +05:30
mod x25519 ;
2021-07-06 17:46:04 +05:30
2021-12-20 20:37:36 +05:30
pub use crate ::decrypt ::op_crypto_decrypt ;
2021-12-15 22:18:26 +01:00
pub use crate ::encrypt ::op_crypto_encrypt ;
2021-12-13 18:45:08 +01:00
pub use crate ::export_key ::op_crypto_export_key ;
pub use crate ::generate_key ::op_crypto_generate_key ;
pub use crate ::import_key ::op_crypto_import_key ;
2021-07-06 17:46:04 +05:30
use crate ::key ::Algorithm ;
use crate ::key ::CryptoHash ;
use crate ::key ::CryptoNamedCurve ;
2021-09-12 02:24:03 +05:30
use crate ::key ::HkdfOutput ;
2023-06-22 23:37:56 +02:00
use crate ::shared ::V8RawKeyData ;
2021-07-06 17:46:04 +05:30
2023-03-17 12:22:15 -06:00
deno_core ::extension! ( deno_crypto ,
deps = [ deno_webidl , deno_web ] ,
ops = [
op_crypto_get_random_values ,
op_crypto_generate_key ,
op_crypto_sign_key ,
op_crypto_verify_key ,
op_crypto_derive_bits ,
op_crypto_import_key ,
op_crypto_export_key ,
op_crypto_encrypt ,
op_crypto_decrypt ,
op_crypto_subtle_digest ,
op_crypto_random_uuid ,
op_crypto_wrap_key ,
op_crypto_unwrap_key ,
op_crypto_base64url_decode ,
op_crypto_base64url_encode ,
2023-05-08 23:07:45 +02:00
x25519 ::op_crypto_generate_x25519_keypair ,
x25519 ::op_crypto_derive_bits_x25519 ,
x25519 ::op_crypto_import_spki_x25519 ,
x25519 ::op_crypto_import_pkcs8_x25519 ,
ed25519 ::op_crypto_generate_ed25519_keypair ,
ed25519 ::op_crypto_import_spki_ed25519 ,
ed25519 ::op_crypto_import_pkcs8_ed25519 ,
ed25519 ::op_crypto_sign_ed25519 ,
ed25519 ::op_crypto_verify_ed25519 ,
ed25519 ::op_crypto_export_spki_ed25519 ,
ed25519 ::op_crypto_export_pkcs8_ed25519 ,
ed25519 ::op_crypto_jwk_x_ed25519 ,
x25519 ::op_crypto_export_spki_x25519 ,
x25519 ::op_crypto_export_pkcs8_x25519 ,
2023-03-17 12:22:15 -06:00
] ,
2023-05-09 12:37:13 +02:00
esm = [ " 00_crypto.js " ] ,
2023-03-17 16:15:27 -06:00
options = {
2023-03-17 12:22:15 -06:00
maybe_seed : Option < u64 > ,
} ,
2023-03-17 16:15:27 -06:00
state = | state , options | {
if let Some ( seed ) = options . maybe_seed {
2023-03-17 12:22:15 -06:00
state . put ( StdRng ::seed_from_u64 ( seed ) ) ;
}
} ,
) ;
2019-10-11 11:41:54 -07:00
2022-09-27 17:43:42 +05:30
#[ op ]
2023-06-05 14:52:02 +02:00
pub fn op_crypto_base64url_decode (
data : String ,
2023-06-22 23:37:56 +02:00
) -> Result < ToJsBuffer , AnyError > {
2023-06-05 14:52:02 +02:00
let data : Vec < u8 > = base64 ::decode_config ( data , base64 ::URL_SAFE_NO_PAD ) ? ;
Ok ( data . into ( ) )
2022-09-27 17:43:42 +05:30
}
2022-10-04 08:06:25 +02:00
#[ op ]
2023-06-22 23:37:56 +02:00
pub fn op_crypto_base64url_encode ( data : JsBuffer ) -> String {
2022-10-04 08:06:25 +02:00
let data : String = base64 ::encode_config ( data , base64 ::URL_SAFE_NO_PAD ) ;
data
}
2022-10-09 03:08:38 -07:00
#[ op(fast) ]
2021-01-15 01:24:38 +01:00
pub fn op_crypto_get_random_values (
2020-09-10 09:57:45 -04:00
state : & mut OpState ,
2022-10-09 03:08:38 -07:00
out : & mut [ u8 ] ,
2021-04-05 18:40:24 +02:00
) -> Result < ( ) , AnyError > {
2022-10-09 03:08:38 -07:00
if out . len ( ) > 65536 {
2021-06-05 19:30:20 +02:00
return Err (
2022-10-09 03:08:38 -07:00
deno_web ::DomExceptionQuotaExceededError ::new ( & format! ( " The ArrayBufferView's byte length ( {} ) exceeds the number of bytes of entropy available via this API (65536) " , out . len ( ) ) )
2021-06-05 19:30:20 +02:00
. into ( ) ,
) ;
}
2020-09-18 20:39:47 +02:00
let maybe_seeded_rng = state . try_borrow_mut ::< StdRng > ( ) ;
if let Some ( seeded_rng ) = maybe_seeded_rng {
2022-10-09 03:08:38 -07:00
seeded_rng . fill ( out ) ;
2019-08-14 17:03:02 +02:00
} else {
let mut rng = thread_rng ( ) ;
2022-10-09 03:08:38 -07:00
rng . fill ( out ) ;
2019-08-14 17:03:02 +02:00
}
2021-04-05 18:40:24 +02:00
Ok ( ( ) )
2019-08-14 17:03:02 +02:00
}
2021-02-26 22:36:26 +05:30
2021-07-06 17:46:04 +05:30
#[ derive(Deserialize) ]
#[ serde(rename_all = " lowercase " ) ]
pub enum KeyFormat {
Raw ,
Pkcs8 ,
2021-10-06 14:48:12 +05:30
Spki ,
2021-07-06 17:46:04 +05:30
}
2021-12-09 20:32:55 +01:00
#[ derive(Deserialize) ]
#[ serde(rename_all = " lowercase " ) ]
pub enum KeyType {
Secret ,
Private ,
Public ,
}
2021-07-06 17:46:04 +05:30
#[ derive(Deserialize) ]
#[ serde(rename_all = " lowercase " ) ]
pub struct KeyData {
2021-12-09 20:32:55 +01:00
r#type : KeyType ,
2023-06-22 23:37:56 +02:00
data : JsBuffer ,
2021-07-06 17:46:04 +05:30
}
#[ derive(Deserialize) ]
#[ serde(rename_all = " camelCase " ) ]
pub struct SignArg {
key : KeyData ,
algorithm : Algorithm ,
salt_length : Option < u32 > ,
hash : Option < CryptoHash > ,
named_curve : Option < CryptoNamedCurve > ,
}
2022-03-14 23:14:15 +05:30
#[ op ]
2021-07-06 17:46:04 +05:30
pub async fn op_crypto_sign_key (
args : SignArg ,
2023-06-22 23:37:56 +02:00
zero_copy : JsBuffer ,
) -> Result < ToJsBuffer , AnyError > {
2021-07-06 17:46:04 +05:30
let data = & * zero_copy ;
let algorithm = args . algorithm ;
let signature = match algorithm {
Algorithm ::RsassaPkcs1v15 = > {
2023-01-18 09:18:41 -06:00
use rsa ::pkcs1v15 ::SigningKey ;
2022-11-17 22:59:10 -03:00
let private_key = RsaPrivateKey ::from_pkcs1_der ( & args . key . data ) ? ;
2023-01-18 09:18:41 -06:00
match args
2021-07-06 17:46:04 +05:30
. hash
. ok_or_else ( | | type_error ( " Missing argument hash " . to_string ( ) ) ) ?
{
2021-07-07 20:03:58 +05:30
CryptoHash ::Sha1 = > {
2023-01-18 09:18:41 -06:00
let signing_key = SigningKey ::< Sha1 > ::new_with_prefix ( private_key ) ;
signing_key . sign ( data )
2021-07-07 20:03:58 +05:30
}
CryptoHash ::Sha256 = > {
2023-01-18 09:18:41 -06:00
let signing_key = SigningKey ::< Sha256 > ::new_with_prefix ( private_key ) ;
signing_key . sign ( data )
2021-07-07 20:03:58 +05:30
}
CryptoHash ::Sha384 = > {
2023-01-18 09:18:41 -06:00
let signing_key = SigningKey ::< Sha384 > ::new_with_prefix ( private_key ) ;
signing_key . sign ( data )
2021-07-07 20:03:58 +05:30
}
CryptoHash ::Sha512 = > {
2023-01-18 09:18:41 -06:00
let signing_key = SigningKey ::< Sha512 > ::new_with_prefix ( private_key ) ;
signing_key . sign ( data )
2021-07-07 20:03:58 +05:30
}
2023-01-18 09:18:41 -06:00
}
. to_vec ( )
2021-07-06 17:46:04 +05:30
}
Algorithm ::RsaPss = > {
2023-01-18 09:18:41 -06:00
use rsa ::pss ::SigningKey ;
2022-11-17 22:59:10 -03:00
let private_key = RsaPrivateKey ::from_pkcs1_der ( & args . key . data ) ? ;
2021-07-06 17:46:04 +05:30
let salt_len = args
. salt_length
. ok_or_else ( | | type_error ( " Missing argument saltLength " . to_string ( ) ) ) ?
as usize ;
let rng = OsRng ;
2023-01-18 09:18:41 -06:00
match args
2021-07-06 17:46:04 +05:30
. hash
. ok_or_else ( | | type_error ( " Missing argument hash " . to_string ( ) ) ) ?
{
CryptoHash ::Sha1 = > {
2023-01-18 09:18:41 -06:00
let signing_key =
SigningKey ::< Sha1 > ::new_with_salt_len ( private_key , salt_len ) ;
signing_key . sign_with_rng ( rng , data )
2021-07-06 17:46:04 +05:30
}
CryptoHash ::Sha256 = > {
2023-01-18 09:18:41 -06:00
let signing_key =
SigningKey ::< Sha256 > ::new_with_salt_len ( private_key , salt_len ) ;
signing_key . sign_with_rng ( rng , data )
2021-07-06 17:46:04 +05:30
}
CryptoHash ::Sha384 = > {
2023-01-18 09:18:41 -06:00
let signing_key =
SigningKey ::< Sha384 > ::new_with_salt_len ( private_key , salt_len ) ;
signing_key . sign_with_rng ( rng , data )
2021-07-06 17:46:04 +05:30
}
CryptoHash ::Sha512 = > {
2023-01-18 09:18:41 -06:00
let signing_key =
SigningKey ::< Sha512 > ::new_with_salt_len ( private_key , salt_len ) ;
signing_key . sign_with_rng ( rng , data )
2021-07-06 17:46:04 +05:30
}
2023-01-18 09:18:41 -06:00
}
. to_vec ( )
2021-07-06 17:46:04 +05:30
}
Algorithm ::Ecdsa = > {
let curve : & EcdsaSigningAlgorithm =
args . named_curve . ok_or_else ( not_supported ) ? . try_into ( ) ? ;
2022-11-17 22:59:10 -03:00
let key_pair = EcdsaKeyPair ::from_pkcs8 ( curve , & args . key . data ) ? ;
2021-07-06 17:46:04 +05:30
// We only support P256-SHA256 & P384-SHA384. These are recommended signature pairs.
// https://briansmith.org/rustdoc/ring/signature/index.html#statics
if let Some ( hash ) = args . hash {
match hash {
CryptoHash ::Sha256 | CryptoHash ::Sha384 = > ( ) ,
_ = > return Err ( type_error ( " Unsupported algorithm " ) ) ,
}
} ;
let rng = RingRand ::SystemRandom ::new ( ) ;
2021-07-30 22:03:41 +09:00
let signature = key_pair . sign ( & rng , data ) ? ;
2021-07-06 17:46:04 +05:30
// Signature data as buffer.
signature . as_ref ( ) . to_vec ( )
}
Algorithm ::Hmac = > {
let hash : HmacAlgorithm = args . hash . ok_or_else ( not_supported ) ? . into ( ) ;
2022-11-17 22:59:10 -03:00
let key = HmacKey ::new ( hash , & args . key . data ) ;
2021-07-06 17:46:04 +05:30
2021-07-30 22:03:41 +09:00
let signature = ring ::hmac ::sign ( & key , data ) ;
2021-07-06 17:46:04 +05:30
signature . as_ref ( ) . to_vec ( )
}
_ = > return Err ( type_error ( " Unsupported algorithm " . to_string ( ) ) ) ,
} ;
Ok ( signature . into ( ) )
}
2021-07-12 18:15:36 +05:30
#[ derive(Deserialize) ]
#[ serde(rename_all = " camelCase " ) ]
pub struct VerifyArg {
key : KeyData ,
algorithm : Algorithm ,
hash : Option < CryptoHash > ,
2023-06-22 23:37:56 +02:00
signature : JsBuffer ,
2021-09-12 02:19:53 +05:30
named_curve : Option < CryptoNamedCurve > ,
2021-07-12 18:15:36 +05:30
}
2022-03-14 23:14:15 +05:30
#[ op ]
2021-07-12 18:15:36 +05:30
pub async fn op_crypto_verify_key (
args : VerifyArg ,
2023-06-22 23:37:56 +02:00
zero_copy : JsBuffer ,
2021-07-12 18:15:36 +05:30
) -> Result < bool , AnyError > {
let data = & * zero_copy ;
let algorithm = args . algorithm ;
let verification = match algorithm {
Algorithm ::RsassaPkcs1v15 = > {
2023-01-18 09:18:41 -06:00
use rsa ::pkcs1v15 ::Signature ;
use rsa ::pkcs1v15 ::VerifyingKey ;
2021-12-09 20:32:55 +01:00
let public_key = read_rsa_public_key ( args . key ) ? ;
2023-01-18 09:18:41 -06:00
let signature : Signature = args . signature . to_vec ( ) . into ( ) ;
match args
2021-07-12 18:15:36 +05:30
. hash
. ok_or_else ( | | type_error ( " Missing argument hash " . to_string ( ) ) ) ?
{
CryptoHash ::Sha1 = > {
2023-01-18 09:18:41 -06:00
let verifying_key = VerifyingKey ::< Sha1 > ::new_with_prefix ( public_key ) ;
verifying_key . verify ( data , & signature ) . is_ok ( )
2021-07-12 18:15:36 +05:30
}
CryptoHash ::Sha256 = > {
2023-01-18 09:18:41 -06:00
let verifying_key =
VerifyingKey ::< Sha256 > ::new_with_prefix ( public_key ) ;
verifying_key . verify ( data , & signature ) . is_ok ( )
2021-07-12 18:15:36 +05:30
}
CryptoHash ::Sha384 = > {
2023-01-18 09:18:41 -06:00
let verifying_key =
VerifyingKey ::< Sha384 > ::new_with_prefix ( public_key ) ;
verifying_key . verify ( data , & signature ) . is_ok ( )
2021-07-12 18:15:36 +05:30
}
CryptoHash ::Sha512 = > {
2023-01-18 09:18:41 -06:00
let verifying_key =
VerifyingKey ::< Sha512 > ::new_with_prefix ( public_key ) ;
verifying_key . verify ( data , & signature ) . is_ok ( )
2021-07-12 18:15:36 +05:30
}
2023-01-18 09:18:41 -06:00
}
2021-07-12 18:15:36 +05:30
}
Algorithm ::RsaPss = > {
2023-01-18 09:18:41 -06:00
use rsa ::pss ::Signature ;
use rsa ::pss ::VerifyingKey ;
2021-12-09 20:32:55 +01:00
let public_key = read_rsa_public_key ( args . key ) ? ;
2023-01-18 09:18:41 -06:00
let signature : Signature = args . signature . to_vec ( ) . into ( ) ;
2021-07-12 18:15:36 +05:30
2023-01-18 09:18:41 -06:00
match args
2021-07-12 18:15:36 +05:30
. hash
. ok_or_else ( | | type_error ( " Missing argument hash " . to_string ( ) ) ) ?
{
CryptoHash ::Sha1 = > {
2023-01-18 09:18:41 -06:00
let verifying_key : VerifyingKey < Sha1 > = public_key . into ( ) ;
verifying_key . verify ( data , & signature ) . is_ok ( )
2021-07-12 18:15:36 +05:30
}
CryptoHash ::Sha256 = > {
2023-01-18 09:18:41 -06:00
let verifying_key : VerifyingKey < Sha256 > = public_key . into ( ) ;
verifying_key . verify ( data , & signature ) . is_ok ( )
2021-07-12 18:15:36 +05:30
}
CryptoHash ::Sha384 = > {
2023-01-18 09:18:41 -06:00
let verifying_key : VerifyingKey < Sha384 > = public_key . into ( ) ;
verifying_key . verify ( data , & signature ) . is_ok ( )
2021-07-12 18:15:36 +05:30
}
CryptoHash ::Sha512 = > {
2023-01-18 09:18:41 -06:00
let verifying_key : VerifyingKey < Sha512 > = public_key . into ( ) ;
verifying_key . verify ( data , & signature ) . is_ok ( )
2021-07-12 18:15:36 +05:30
}
2023-01-18 09:18:41 -06:00
}
2021-07-12 18:15:36 +05:30
}
2021-08-05 01:19:27 +05:30
Algorithm ::Hmac = > {
let hash : HmacAlgorithm = args . hash . ok_or_else ( not_supported ) ? . into ( ) ;
2022-11-17 22:59:10 -03:00
let key = HmacKey ::new ( hash , & args . key . data ) ;
ring ::hmac ::verify ( & key , data , & args . signature ) . is_ok ( )
2021-08-05 01:19:27 +05:30
}
2021-09-12 02:19:53 +05:30
Algorithm ::Ecdsa = > {
let signing_alg : & EcdsaSigningAlgorithm =
args . named_curve . ok_or_else ( not_supported ) ? . try_into ( ) ? ;
let verify_alg : & EcdsaVerificationAlgorithm =
args . named_curve . ok_or_else ( not_supported ) ? . try_into ( ) ? ;
2021-12-16 13:28:43 -03:00
let private_key ;
let public_key_bytes = match args . key . r#type {
KeyType ::Private = > {
2022-11-17 22:59:10 -03:00
private_key = EcdsaKeyPair ::from_pkcs8 ( signing_alg , & args . key . data ) ? ;
2021-12-16 13:28:43 -03:00
private_key . public_key ( ) . as_ref ( )
}
KeyType ::Public = > & * args . key . data ,
_ = > return Err ( type_error ( " Invalid Key format " . to_string ( ) ) ) ,
} ;
2021-09-12 02:19:53 +05:30
let public_key =
ring ::signature ::UnparsedPublicKey ::new ( verify_alg , public_key_bytes ) ;
2022-11-17 22:59:10 -03:00
public_key . verify ( data , & args . signature ) . is_ok ( )
2021-09-12 02:19:53 +05:30
}
2021-07-12 18:15:36 +05:30
_ = > return Err ( type_error ( " Unsupported algorithm " . to_string ( ) ) ) ,
} ;
Ok ( verification )
}
2021-08-26 16:18:07 +05:30
#[ derive(Deserialize) ]
#[ serde(rename_all = " camelCase " ) ]
pub struct DeriveKeyArg {
key : KeyData ,
algorithm : Algorithm ,
hash : Option < CryptoHash > ,
length : usize ,
iterations : Option < u32 > ,
2021-10-08 20:59:36 +05:30
// ECDH
public_key : Option < KeyData > ,
named_curve : Option < CryptoNamedCurve > ,
// HKDF
2023-06-22 23:37:56 +02:00
info : Option < JsBuffer > ,
2021-08-26 16:18:07 +05:30
}
2022-03-14 23:14:15 +05:30
#[ op ]
2021-08-26 16:18:07 +05:30
pub async fn op_crypto_derive_bits (
args : DeriveKeyArg ,
2023-06-22 23:37:56 +02:00
zero_copy : Option < JsBuffer > ,
) -> Result < ToJsBuffer , AnyError > {
2021-08-26 16:18:07 +05:30
let algorithm = args . algorithm ;
match algorithm {
Algorithm ::Pbkdf2 = > {
2021-10-08 20:59:36 +05:30
let zero_copy = zero_copy . ok_or_else ( not_supported ) ? ;
let salt = & * zero_copy ;
2021-08-26 16:18:07 +05:30
// The caller must validate these cases.
assert! ( args . length > 0 ) ;
assert! ( args . length % 8 = = 0 ) ;
let algorithm = match args . hash . ok_or_else ( not_supported ) ? {
CryptoHash ::Sha1 = > pbkdf2 ::PBKDF2_HMAC_SHA1 ,
CryptoHash ::Sha256 = > pbkdf2 ::PBKDF2_HMAC_SHA256 ,
CryptoHash ::Sha384 = > pbkdf2 ::PBKDF2_HMAC_SHA384 ,
CryptoHash ::Sha512 = > pbkdf2 ::PBKDF2_HMAC_SHA512 ,
} ;
// This will never panic. We have already checked length earlier.
let iterations =
NonZeroU32 ::new ( args . iterations . ok_or_else ( not_supported ) ? ) . unwrap ( ) ;
let secret = args . key . data ;
let mut out = vec! [ 0 ; args . length / 8 ] ;
pbkdf2 ::derive ( algorithm , iterations , salt , & secret , & mut out ) ;
Ok ( out . into ( ) )
}
2021-10-08 20:59:36 +05:30
Algorithm ::Ecdh = > {
let named_curve = args
. named_curve
. ok_or_else ( | | type_error ( " Missing argument namedCurve " . to_string ( ) ) ) ? ;
let public_key = args
. public_key
2021-12-16 13:28:43 -03:00
. ok_or_else ( | | type_error ( " Missing argument publicKey " ) ) ? ;
2021-10-08 20:59:36 +05:30
match named_curve {
CryptoNamedCurve ::P256 = > {
2021-12-16 13:28:43 -03:00
let secret_key = p256 ::SecretKey ::from_pkcs8_der ( & args . key . data )
. map_err ( | _ | type_error ( " Unexpected error decoding private key " ) ) ? ;
let public_key = match public_key . r#type {
KeyType ::Private = > {
p256 ::SecretKey ::from_pkcs8_der ( & public_key . data )
. map_err ( | _ | {
type_error ( " Unexpected error decoding private key " )
} ) ?
. public_key ( )
}
KeyType ::Public = > {
let point = p256 ::EncodedPoint ::from_bytes ( public_key . data )
. map_err ( | _ | {
type_error ( " Unexpected error decoding private key " )
} ) ? ;
2022-06-20 16:53:57 +05:30
let pk = p256 ::PublicKey ::from_encoded_point ( & point ) ;
// pk is a constant time Option.
if pk . is_some ( ) . into ( ) {
pk . unwrap ( )
2021-12-16 13:28:43 -03:00
} else {
return Err ( type_error (
" Unexpected error decoding private key " ,
) ) ;
}
}
_ = > unreachable! ( ) ,
} ;
2021-10-08 20:59:36 +05:30
let shared_secret = p256 ::elliptic_curve ::ecdh ::diffie_hellman (
2022-06-20 16:53:57 +05:30
secret_key . to_nonzero_scalar ( ) ,
2021-10-08 20:59:36 +05:30
public_key . as_affine ( ) ,
) ;
2022-06-20 16:53:57 +05:30
// raw serialized x-coordinate of the computed point
Ok ( shared_secret . raw_secret_bytes ( ) . to_vec ( ) . into ( ) )
2021-10-08 20:59:36 +05:30
}
2022-07-24 03:04:37 +10:00
CryptoNamedCurve ::P384 = > {
let secret_key = p384 ::SecretKey ::from_pkcs8_der ( & args . key . data )
. map_err ( | _ | type_error ( " Unexpected error decoding private key " ) ) ? ;
let public_key = match public_key . r#type {
KeyType ::Private = > {
p384 ::SecretKey ::from_pkcs8_der ( & public_key . data )
. map_err ( | _ | {
type_error ( " Unexpected error decoding private key " )
} ) ?
. public_key ( )
}
KeyType ::Public = > {
let point = p384 ::EncodedPoint ::from_bytes ( public_key . data )
. map_err ( | _ | {
type_error ( " Unexpected error decoding private key " )
} ) ? ;
let pk = p384 ::PublicKey ::from_encoded_point ( & point ) ;
// pk is a constant time Option.
if pk . is_some ( ) . into ( ) {
pk . unwrap ( )
} else {
return Err ( type_error (
" Unexpected error decoding private key " ,
) ) ;
}
}
_ = > unreachable! ( ) ,
} ;
let shared_secret = p384 ::elliptic_curve ::ecdh ::diffie_hellman (
secret_key . to_nonzero_scalar ( ) ,
public_key . as_affine ( ) ,
) ;
// raw serialized x-coordinate of the computed point
Ok ( shared_secret . raw_secret_bytes ( ) . to_vec ( ) . into ( ) )
}
2021-10-08 20:59:36 +05:30
}
}
2021-09-12 02:24:03 +05:30
Algorithm ::Hkdf = > {
2021-10-08 20:59:36 +05:30
let zero_copy = zero_copy . ok_or_else ( not_supported ) ? ;
let salt = & * zero_copy ;
2021-09-12 02:24:03 +05:30
let algorithm = match args . hash . ok_or_else ( not_supported ) ? {
CryptoHash ::Sha1 = > hkdf ::HKDF_SHA1_FOR_LEGACY_USE_ONLY ,
CryptoHash ::Sha256 = > hkdf ::HKDF_SHA256 ,
CryptoHash ::Sha384 = > hkdf ::HKDF_SHA384 ,
CryptoHash ::Sha512 = > hkdf ::HKDF_SHA512 ,
} ;
let info = args
. info
. ok_or_else ( | | type_error ( " Missing argument info " . to_string ( ) ) ) ? ;
// IKM
let secret = args . key . data ;
// L
let length = args . length / 8 ;
let salt = hkdf ::Salt ::new ( algorithm , salt ) ;
let prk = salt . extract ( & secret ) ;
let info = & [ & * info ] ;
2021-11-11 14:34:17 +05:30
let okm = prk . expand ( info , HkdfOutput ( length ) ) . map_err ( | _e | {
custom_error (
" DOMExceptionOperationError " ,
" The length provided for HKDF is too large " ,
)
} ) ? ;
2021-09-12 02:24:03 +05:30
let mut r = vec! [ 0 u8 ; length ] ;
okm . fill ( & mut r ) ? ;
Ok ( r . into ( ) )
}
2021-08-26 16:18:07 +05:30
_ = > Err ( type_error ( " Unsupported algorithm " . to_string ( ) ) ) ,
}
}
2021-12-09 20:32:55 +01:00
fn read_rsa_public_key ( key_data : KeyData ) -> Result < RsaPublicKey , AnyError > {
let public_key = match key_data . r#type {
KeyType ::Private = > {
2022-11-17 22:59:10 -03:00
RsaPrivateKey ::from_pkcs1_der ( & key_data . data ) ? . to_public_key ( )
2021-12-09 20:32:55 +01:00
}
2022-11-17 22:59:10 -03:00
KeyType ::Public = > RsaPublicKey ::from_pkcs1_der ( & key_data . data ) ? ,
2021-12-09 20:32:55 +01:00
KeyType ::Secret = > unreachable! ( " unexpected KeyType::Secret " ) ,
} ;
Ok ( public_key )
}
2022-03-14 23:14:15 +05:30
#[ op ]
2022-03-14 23:38:53 +01:00
pub fn op_crypto_random_uuid ( state : & mut OpState ) -> Result < String , AnyError > {
2021-06-05 14:46:24 +02:00
let maybe_seeded_rng = state . try_borrow_mut ::< StdRng > ( ) ;
let uuid = if let Some ( seeded_rng ) = maybe_seeded_rng {
let mut bytes = [ 0 u8 ; 16 ] ;
seeded_rng . fill ( & mut bytes ) ;
uuid ::Builder ::from_bytes ( bytes )
2022-05-05 12:41:59 +02:00
. with_version ( uuid ::Version ::Random )
. into_uuid ( )
2021-06-05 14:46:24 +02:00
} else {
uuid ::Uuid ::new_v4 ( )
} ;
Ok ( uuid . to_string ( ) )
}
2022-03-14 23:14:15 +05:30
#[ op ]
2021-06-06 18:57:10 +08:00
pub async fn op_crypto_subtle_digest (
2021-07-08 00:04:02 +05:30
algorithm : CryptoHash ,
2023-06-22 23:37:56 +02:00
data : JsBuffer ,
) -> Result < ToJsBuffer , AnyError > {
2023-05-14 15:40:01 -06:00
let output = spawn_blocking ( move | | {
2021-10-05 22:38:27 +02:00
digest ::digest ( algorithm . into ( ) , & data )
2021-07-08 00:04:02 +05:30
. as_ref ( )
. to_vec ( )
. into ( )
2021-06-06 18:57:10 +08:00
} )
. await ? ;
Ok ( output )
}
2022-01-11 01:44:47 -03:00
#[ derive(Deserialize) ]
#[ serde(rename_all = " camelCase " ) ]
pub struct WrapUnwrapKeyArg {
2023-06-22 23:37:56 +02:00
key : V8RawKeyData ,
2022-01-11 01:44:47 -03:00
algorithm : Algorithm ,
}
2022-03-14 23:14:15 +05:30
#[ op ]
2022-01-11 01:44:47 -03:00
pub fn op_crypto_wrap_key (
args : WrapUnwrapKeyArg ,
2023-06-22 23:37:56 +02:00
data : JsBuffer ,
) -> Result < ToJsBuffer , AnyError > {
2022-01-11 01:44:47 -03:00
let algorithm = args . algorithm ;
match algorithm {
Algorithm ::AesKw = > {
let key = args . key . as_secret_key ( ) ? ;
if data . len ( ) % 8 ! = 0 {
return Err ( type_error ( " Data must be multiple of 8 bytes " ) ) ;
}
let wrapped_key = match key . len ( ) {
16 = > KekAes128 ::new ( key . into ( ) ) . wrap_vec ( & data ) ,
24 = > KekAes192 ::new ( key . into ( ) ) . wrap_vec ( & data ) ,
32 = > KekAes256 ::new ( key . into ( ) ) . wrap_vec ( & data ) ,
_ = > return Err ( type_error ( " Invalid key length " ) ) ,
}
. map_err ( | _ | operation_error ( " encryption error " ) ) ? ;
Ok ( wrapped_key . into ( ) )
}
_ = > Err ( type_error ( " Unsupported algorithm " ) ) ,
}
}
2022-03-14 23:14:15 +05:30
#[ op ]
2022-01-11 01:44:47 -03:00
pub fn op_crypto_unwrap_key (
args : WrapUnwrapKeyArg ,
2023-06-22 23:37:56 +02:00
data : JsBuffer ,
) -> Result < ToJsBuffer , AnyError > {
2022-01-11 01:44:47 -03:00
let algorithm = args . algorithm ;
match algorithm {
Algorithm ::AesKw = > {
let key = args . key . as_secret_key ( ) ? ;
if data . len ( ) % 8 ! = 0 {
return Err ( type_error ( " Data must be multiple of 8 bytes " ) ) ;
}
let unwrapped_key = match key . len ( ) {
16 = > KekAes128 ::new ( key . into ( ) ) . unwrap_vec ( & data ) ,
24 = > KekAes192 ::new ( key . into ( ) ) . unwrap_vec ( & data ) ,
32 = > KekAes256 ::new ( key . into ( ) ) . unwrap_vec ( & data ) ,
_ = > return Err ( type_error ( " Invalid key length " ) ) ,
}
. map_err ( | _ | {
operation_error ( " decryption error - integrity check failed " )
} ) ? ;
Ok ( unwrapped_key . into ( ) )
}
_ = > Err ( type_error ( " Unsupported algorithm " ) ) ,
}
}
2022-03-16 20:25:44 -04:00
pub fn get_declaration ( ) -> PathBuf {
PathBuf ::from ( env! ( " CARGO_MANIFEST_DIR " ) ) . join ( " lib.deno_crypto.d.ts " )
}