2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-01-13 02:51:32 -05:00
|
|
|
|
2022-09-27 08:13:42 -04:00
|
|
|
use curve25519_dalek::montgomery::MontgomeryPoint;
|
2023-10-30 11:25:12 -04:00
|
|
|
use deno_core::error::custom_error;
|
2022-10-04 02:06:25 -04:00
|
|
|
use deno_core::error::AnyError;
|
2023-09-13 11:54:19 -04:00
|
|
|
use deno_core::op2;
|
2023-06-22 17:37:56 -04:00
|
|
|
use deno_core::ToJsBuffer;
|
2022-09-27 08:13:42 -04:00
|
|
|
use elliptic_curve::pkcs8::PrivateKeyInfo;
|
|
|
|
use elliptic_curve::subtle::ConstantTimeEq;
|
|
|
|
use rand::rngs::OsRng;
|
|
|
|
use rand::RngCore;
|
2023-11-04 00:29:25 -04:00
|
|
|
use spki::der::asn1::BitString;
|
2022-09-27 08:13:42 -04:00
|
|
|
use spki::der::Decode;
|
2022-10-04 02:06:25 -04:00
|
|
|
use spki::der::Encode;
|
2022-09-27 08:13:42 -04:00
|
|
|
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
pub fn op_crypto_generate_x25519_keypair(
|
|
|
|
#[buffer] pkey: &mut [u8],
|
|
|
|
#[buffer] pubkey: &mut [u8],
|
|
|
|
) {
|
2022-09-27 08:13:42 -04:00
|
|
|
// u-coordinate of the base point.
|
|
|
|
const X25519_BASEPOINT_BYTES: [u8; 32] = [
|
|
|
|
9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0,
|
|
|
|
];
|
|
|
|
let mut rng = OsRng;
|
|
|
|
rng.fill_bytes(pkey);
|
|
|
|
// https://www.rfc-editor.org/rfc/rfc7748#section-6.1
|
|
|
|
// pubkey = x25519(a, 9) which is constant-time Montgomery ladder.
|
|
|
|
// https://eprint.iacr.org/2014/140.pdf page 4
|
|
|
|
// https://eprint.iacr.org/2017/212.pdf algorithm 8
|
|
|
|
// pubkey is in LE order.
|
|
|
|
let pkey: [u8; 32] = pkey.try_into().expect("Expected byteLength 32");
|
|
|
|
pubkey.copy_from_slice(&x25519_dalek::x25519(pkey, X25519_BASEPOINT_BYTES));
|
|
|
|
}
|
|
|
|
|
|
|
|
const MONTGOMERY_IDENTITY: MontgomeryPoint = MontgomeryPoint([0; 32]);
|
|
|
|
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2(fast)]
|
2023-05-08 17:07:45 -04:00
|
|
|
pub fn op_crypto_derive_bits_x25519(
|
2023-09-13 11:54:19 -04:00
|
|
|
#[buffer] k: &[u8],
|
|
|
|
#[buffer] u: &[u8],
|
|
|
|
#[buffer] secret: &mut [u8],
|
2023-05-08 17:07:45 -04:00
|
|
|
) -> bool {
|
2022-09-27 08:13:42 -04:00
|
|
|
let k: [u8; 32] = k.try_into().expect("Expected byteLength 32");
|
|
|
|
let u: [u8; 32] = u.try_into().expect("Expected byteLength 32");
|
|
|
|
let sh_sec = x25519_dalek::x25519(k, u);
|
|
|
|
let point = MontgomeryPoint(sh_sec);
|
|
|
|
if point.ct_eq(&MONTGOMERY_IDENTITY).unwrap_u8() == 1 {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
secret.copy_from_slice(&sh_sec);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
// id-X25519 OBJECT IDENTIFIER ::= { 1 3 101 110 }
|
|
|
|
pub const X25519_OID: const_oid::ObjectIdentifier =
|
|
|
|
const_oid::ObjectIdentifier::new_unwrap("1.3.101.110");
|
|
|
|
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
pub fn op_crypto_import_spki_x25519(
|
|
|
|
#[buffer] key_data: &[u8],
|
|
|
|
#[buffer] out: &mut [u8],
|
|
|
|
) -> bool {
|
2022-09-27 08:13:42 -04:00
|
|
|
// 2-3.
|
2023-11-04 00:29:25 -04:00
|
|
|
let pk_info = match spki::SubjectPublicKeyInfoRef::try_from(key_data) {
|
2022-09-27 08:13:42 -04:00
|
|
|
Ok(pk_info) => pk_info,
|
|
|
|
Err(_) => return false,
|
|
|
|
};
|
|
|
|
// 4.
|
|
|
|
let alg = pk_info.algorithm.oid;
|
|
|
|
if alg != X25519_OID {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 5.
|
|
|
|
if pk_info.algorithm.parameters.is_some() {
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-04 00:29:25 -04:00
|
|
|
out.copy_from_slice(pk_info.subject_public_key.raw_bytes());
|
2022-09-27 08:13:42 -04:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
pub fn op_crypto_import_pkcs8_x25519(
|
|
|
|
#[buffer] key_data: &[u8],
|
|
|
|
#[buffer] out: &mut [u8],
|
|
|
|
) -> bool {
|
2022-09-27 08:13:42 -04:00
|
|
|
// 2-3.
|
2022-10-04 02:06:25 -04:00
|
|
|
// This should probably use OneAsymmetricKey instead
|
2022-09-27 08:13:42 -04:00
|
|
|
let pk_info = match PrivateKeyInfo::from_der(key_data) {
|
|
|
|
Ok(pk_info) => pk_info,
|
|
|
|
Err(_) => return false,
|
|
|
|
};
|
|
|
|
// 4.
|
|
|
|
let alg = pk_info.algorithm.oid;
|
|
|
|
if alg != X25519_OID {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 5.
|
|
|
|
if pk_info.algorithm.parameters.is_some() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// 6.
|
|
|
|
// CurvePrivateKey ::= OCTET STRING
|
2022-10-04 02:06:25 -04:00
|
|
|
if pk_info.private_key.len() != 34 {
|
2022-09-27 08:13:42 -04:00
|
|
|
return false;
|
|
|
|
}
|
2022-10-04 02:06:25 -04:00
|
|
|
out.copy_from_slice(&pk_info.private_key[2..]);
|
2022-09-27 08:13:42 -04:00
|
|
|
true
|
|
|
|
}
|
2022-10-04 02:06:25 -04:00
|
|
|
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2023-05-08 17:07:45 -04:00
|
|
|
pub fn op_crypto_export_spki_x25519(
|
2023-09-13 11:54:19 -04:00
|
|
|
#[buffer] pubkey: &[u8],
|
2023-06-22 17:37:56 -04:00
|
|
|
) -> Result<ToJsBuffer, AnyError> {
|
2022-10-04 02:06:25 -04:00
|
|
|
let key_info = spki::SubjectPublicKeyInfo {
|
2023-11-04 00:29:25 -04:00
|
|
|
algorithm: spki::AlgorithmIdentifierRef {
|
2022-10-04 02:06:25 -04:00
|
|
|
// id-X25519
|
|
|
|
oid: X25519_OID,
|
|
|
|
parameters: None,
|
|
|
|
},
|
2023-11-04 00:29:25 -04:00
|
|
|
subject_public_key: BitString::from_bytes(pubkey)?,
|
2022-10-04 02:06:25 -04:00
|
|
|
};
|
2023-10-30 11:25:12 -04:00
|
|
|
Ok(
|
|
|
|
key_info
|
2023-11-04 00:29:25 -04:00
|
|
|
.to_der()
|
2023-10-30 11:25:12 -04:00
|
|
|
.map_err(|_| {
|
|
|
|
custom_error("DOMExceptionOperationError", "Failed to export key")
|
|
|
|
})?
|
|
|
|
.into(),
|
|
|
|
)
|
2022-10-04 02:06:25 -04:00
|
|
|
}
|
|
|
|
|
2023-09-13 11:54:19 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2023-05-08 17:07:45 -04:00
|
|
|
pub fn op_crypto_export_pkcs8_x25519(
|
2023-09-13 11:54:19 -04:00
|
|
|
#[buffer] pkey: &[u8],
|
2023-06-22 17:37:56 -04:00
|
|
|
) -> Result<ToJsBuffer, AnyError> {
|
2023-10-30 11:25:12 -04:00
|
|
|
use rsa::pkcs1::der::Encode;
|
|
|
|
|
2022-10-04 02:06:25 -04:00
|
|
|
// This should probably use OneAsymmetricKey instead
|
|
|
|
let pk_info = rsa::pkcs8::PrivateKeyInfo {
|
|
|
|
public_key: None,
|
2023-10-30 11:25:12 -04:00
|
|
|
algorithm: rsa::pkcs8::AlgorithmIdentifierRef {
|
2022-10-04 02:06:25 -04:00
|
|
|
// id-X25519
|
|
|
|
oid: X25519_OID,
|
|
|
|
parameters: None,
|
|
|
|
},
|
|
|
|
private_key: pkey, // OCTET STRING
|
|
|
|
};
|
|
|
|
|
2023-10-30 11:25:12 -04:00
|
|
|
let mut buf = Vec::new();
|
|
|
|
pk_info.encode_to_vec(&mut buf)?;
|
|
|
|
Ok(buf.into())
|
2022-10-04 02:06:25 -04:00
|
|
|
}
|