Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/key/generate_key_pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface GenerateKeyPairOptions {
* The EC "crv" (Curve) or OKP "crv" (Subtype of Key Pair) value to generate. The curve must be
* both supported on the runtime as well as applicable for the given JWA algorithm identifier.
*/
crv?: string
crv?: `P-${'256' | '384' | '521'}` | 'X25519'

/**
* A hint for RSA algorithms to generate an RSA key of a given `modulusLength` (Key size in bits).
Expand All @@ -47,6 +47,26 @@ export interface GenerateKeyPairOptions {
extractable?: boolean
}

/**
* JWA Algorithm Identifier for asymmetric key pair generation.
*
* The {@link https://github.com/panva/jose/issues/114 Algorithm Selection Guide} should be consulted
* as a quick reference if you're having trouble selecting an appropriate algorithm for your needs.
*
* See {@link https://github.com/panva/jose/issues/210 Algorithm Key Requirements} for usage support
* details.
*/
export type KeyPairAlgorithm =
| `PS${'256' | '384' | '512'}`
| `RS${'256' | '384' | '512'}`
| `RSA-OAEP`
| `RSA-OAEP-${'256' | '384' | '512'}`
| `ES${'256' | '384' | '512'}`
| `Ed${'25519' | 'DSA'}`
| `ML-DSA-${'44' | '65' | '87'}`
| `ECDH-ES`
| `ECDH-ES+A${'128' | '192' | '256'}KW`

function getModulusLengthOption(options?: GenerateKeyPairOptions) {
const modulusLength = options?.modulusLength ?? 2048
if (typeof modulusLength !== 'number' || modulusLength < 2048) {
Expand Down Expand Up @@ -81,7 +101,7 @@ function getModulusLengthOption(options?: GenerateKeyPairOptions) {
* @param options Additional options passed down to the key pair generation.
*/
export async function generateKeyPair(
alg: string,
alg: KeyPairAlgorithm,
options?: GenerateKeyPairOptions,
): Promise<GenerateKeyPairResult> {
let algorithm: RsaHashedKeyGenParams | EcKeyGenParams | KeyAlgorithm
Expand Down
18 changes: 17 additions & 1 deletion src/key/generate_secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ export interface GenerateSecretOptions {
extractable?: boolean
}

/**
* JWA Algorithm Identifier for symmetric secret generation.
*
* The {@link https://github.com/panva/jose/issues/114 Algorithm Selection Guide} should be consulted
* as a quick reference if you're having trouble selecting an appropriate algorithm for your needs.
*
* See {@link https://github.com/panva/jose/issues/210 Algorithm Key Requirements} for usage support
* details.
*/
export type SecretKeyAlgorithm =
| `HS${'256' | '384' | '512'}`
| `A${'128' | '192' | '256'}${'GCM' | 'KW' | 'GCMKW'}`
| `A128CBC-HS256`
| `A192CBC-HS384`
| `A256CBC-HS512`

/**
* Generates a symmetric secret key for a given JWA algorithm identifier.
*
Expand All @@ -45,7 +61,7 @@ export interface GenerateSecretOptions {
* @param options Additional options passed down to the secret generation.
*/
export async function generateSecret(
alg: string,
alg: SecretKeyAlgorithm,
options?: GenerateSecretOptions,
): Promise<types.CryptoKey | Uint8Array> {
let length: number
Expand Down