-
Notifications
You must be signed in to change notification settings - Fork 6
chore(sdk): Break up the crypto service implementation into smaller more managable files #888
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
Merged
elizabethhealy
merged 3 commits into
main
from
dspx-2415-break-up-default-crypto-service-for-clarity
Mar 20, 2026
Merged
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
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,118 @@ | ||
| import { | ||
| type ECCurve, | ||
| type HkdfParams, | ||
| type KeyAlgorithm, | ||
| type KeyPair, | ||
| type PrivateKey, | ||
| type PublicKey, | ||
| type SymmetricKey, | ||
| } from '../declarations.js'; | ||
| import { ConfigurationError } from '../../../../src/errors.js'; | ||
| import { unwrapKey, wrapPrivateKey, wrapPublicKey, wrapSymmetricKey } from './keys.js'; | ||
|
|
||
| /** | ||
| * Map ECCurve to Web Crypto named curve. | ||
| */ | ||
| function curveToNamedCurve(curve: ECCurve): string { | ||
| switch (curve) { | ||
| case 'P-256': | ||
| return 'P-256'; | ||
| case 'P-384': | ||
| return 'P-384'; | ||
| case 'P-521': | ||
| return 'P-521'; | ||
| default: | ||
| throw new ConfigurationError(`Unsupported curve: ${curve}`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Generate an EC key pair for ECDH key agreement. | ||
| */ | ||
| export async function generateECKeyPair(curve: ECCurve = 'P-256'): Promise<KeyPair> { | ||
| const namedCurve = curveToNamedCurve(curve); | ||
|
|
||
| // Generate key pair for ECDH key agreement | ||
| const keyPair = await crypto.subtle.generateKey({ name: 'ECDH', namedCurve }, true, [ | ||
| 'deriveBits', | ||
| ]); | ||
|
|
||
| // Map to KeyAlgorithm literal type | ||
| let algorithm: KeyAlgorithm; | ||
| switch (namedCurve) { | ||
| case 'P-256': | ||
| algorithm = 'ec:secp256r1'; | ||
| break; | ||
| case 'P-384': | ||
| algorithm = 'ec:secp384r1'; | ||
| break; | ||
| case 'P-521': | ||
| algorithm = 'ec:secp521r1'; | ||
| break; | ||
| default: | ||
| throw new ConfigurationError(`Unsupported curve: ${namedCurve}`); | ||
| } | ||
|
|
||
| return { | ||
| publicKey: wrapPublicKey(keyPair.publicKey, algorithm), | ||
| privateKey: wrapPrivateKey(keyPair.privateKey, algorithm), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Perform ECDH key agreement followed by HKDF key derivation. | ||
| * Returns opaque symmetric key for symmetric encryption. | ||
| */ | ||
| export async function deriveKeyFromECDH( | ||
| privateKey: PrivateKey, | ||
| publicKey: PublicKey, | ||
| hkdfParams: HkdfParams | ||
| ): Promise<SymmetricKey> { | ||
| // Unwrap the internal CryptoKeys | ||
| const privateKeyCrypto = unwrapKey(privateKey); | ||
| const publicKeyCrypto = unwrapKey(publicKey); | ||
|
|
||
| // Get curve from key metadata | ||
| const curve = publicKey.curve; | ||
| if (!curve) { | ||
| throw new ConfigurationError('EC curve not found on public key'); | ||
| } | ||
|
|
||
| // Determine bits based on curve | ||
| const curveBits: Record<ECCurve, number> = { | ||
| 'P-256': 256, | ||
| 'P-384': 384, | ||
| // P-521 derives 528 bits (66 bytes) | ||
| 'P-521': 528, | ||
| }; | ||
| const bits = curveBits[curve]; | ||
|
|
||
| // Perform ECDH to get shared secret | ||
| const sharedSecret = await crypto.subtle.deriveBits( | ||
| { name: 'ECDH', public: publicKeyCrypto }, | ||
| privateKeyCrypto, | ||
| bits | ||
| ); | ||
|
|
||
| // Import shared secret as HKDF key material | ||
| const hkdfKey = await crypto.subtle.importKey('raw', sharedSecret, 'HKDF', false, ['deriveKey']); | ||
|
|
||
| // Derive the final key using HKDF | ||
| const keyLength = hkdfParams.keyLength ?? 256; | ||
| const derivedKey = await crypto.subtle.deriveKey( | ||
| { | ||
| name: 'HKDF', | ||
| hash: hkdfParams.hash, | ||
| salt: hkdfParams.salt, | ||
| info: hkdfParams.info ?? new Uint8Array(0), | ||
| }, | ||
| hkdfKey, | ||
| { name: 'AES-GCM', length: keyLength }, | ||
| true, | ||
| ['encrypt', 'decrypt'] | ||
| ); | ||
|
|
||
| // Export the derived key as raw bytes and wrap as SymmetricKey | ||
| const keyBytes = await crypto.subtle.exportKey('raw', derivedKey); | ||
| return wrapSymmetricKey(new Uint8Array(keyBytes)); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.