/** * * Copyright 2024 Algorand Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */ /** * DeterministicP256 - a class that generates deterministic P-256 keypairs from a BIP39 phrase and a * domain-specific origin and userHandle. * * For generating passkeys intended for FIDO2-based authentication to web services, in a * deterministic manner that allows a user to regenerate the same keypair on different devices. * * 1) Start by generating a derived main key from a BIP39 phrase using PBKDF2-HMAC-SHA512 with 210k * iterations. This should only be run once per device, and the derived main key should be stored * securely. The mnemonic phrase should only be inputed once and then be discarded by the device. * * 2) Generate a domain-specific keypair from the derived main key, origin, and userHandle. The * origin is the domain of the service, and the userHandle is the user's unique identifier on that * service. A counter can also be set in case it is pertinent to generate multiple passkeys for a * service. * * 3) Sign a payload with the domain-specific keypair. The keypairs can be stored and retreived from * storage using some secure storage mechanism. */ export declare class DeterministicP256 { /** * Generates a derived main key using a BIP39 mnemonic phrase. * * This function validates the provided BIP39 mnemonic phrase and then derives a main key using the specified parameters. * The derived key is generated using the PBKDF2 key derivation function with HMAC-SHA512. * * @param phrase - A BIP39 mnemonic phrase used as the entropy source for key derivation. * @param salt - An optional salt value used in the key derivation process. Defaults to the UTF-8 encoded string "liquid". * @param iterationCount - The number of iterations to perform in the key derivation process. Defaults to 210,000. * @param keyLength - The desired length of the derived key in bits. Defaults to 512 bits. * * @returns A `Uint8Array` containing the derived key. * * @throws An error if the mnemonic phrase is invalid or if the key derivation process fails. * * @note The `keyLength` parameter is integer divided by 8 internally to convert the length from bits to full bytes. */ genDerivedMainKeyWithBIP39(phrase: string, salt?: Uint8Array, iterationCount?: number, keyLength?: number): Promise; /** * Generates a derived key using the PBKDF2 key derivation function with HMAC-SHA512. * * This function derives a key from the provided entropy and salt using the specified iteration count and key length. * The derived key is generated using the PBKDF2 key derivation function with HMAC-SHA512. * * @param entropy - A Uint8Array representing the entropy source for key derivation. * @param salt - A Uint8Array representing the salt value used in the key derivation process. * @param iterationCount - The number of iterations to perform in the key derivation process. * @param keyLengthBytes - The desired length of the derived key in bytes. * * @returns A Promise resolving to a `Uint8Array` containing the derived key. * * @throws An error if the key derivation process fails. * * @note The `keyLengthBytes` parameter specifies the length of the derived key in bytes. */ genDerivedMainKey(entropy: Uint8Array, salt: Uint8Array, iterationCount: number, keyLengthBytes: number): Promise; /** * Generates a domain-specific key pair using a derived main key, origin, user ID, and an optional counter. * * This function concatenates the provided derived main key, origin, user ID, and counter to create a unique input. * It then calculates the SHA-512 hash of this input and uses the first 32 bytes of the hash as the seed to * generate a P256 private key. The origin and userHandle are meant to correspond to their WebAuthn counterparts * but they can be any strings that help to uniquely identify the key pair. * * @param derivedMainKey - A `Uint8Array` representing the derived main key. * @param origin - A `string` representing the origin or domain for which the key pair is being generated. * @param userHandle - A `string` representing the user ID. * @param counter - An optional `number` counter to ensure uniqueness. Defaults to 0. * * @returns A `Uint8Array` representing the generated private key. * * @throws An error if the key generation fails. * * @note The SHA-512 hash is calculated using the Web Crypto API, and only the first 32 bytes of the hash * are used as the seed for the private key, similar to BC in the Kotlin implementation. Certain java.security providers * accept 40 bytes of seed but we explicitly ensure it is 32 bytes. */ genDomainSpecificKeyPair(derivedMainKey: Uint8Array, origin: string, userHandle: string, counter?: number): Promise; /** * Signs a payload using a domain-specific private key. * * This function takes a P256 private key and a payload, and generates an ECDSA signature for the * payload using the private key. * * @param privateKey - A `Uint8Array` representing the domain-specific private key. * @param payload - A `Uint8Array` representing the payload to be signed. * * @returns A `Uint8Array` representing the generated signature in compact format. * * @throws An error if the signing process fails. * * @note The signature is generated using the ECDSA algorithm with the P256 curve. */ signWithDomainSpecificKeyPair(privateKey: Uint8Array, payload: Uint8Array): Uint8Array; /** * Retrieves the raw public key bytes from a P256 private key. * * This method provides API parity with the Kotlin and Swift implementations. * * In the current TypeScript implementation using @noble/curves, the public key is simple to retrieve * in raw representation. This method returns the exact same bytes as the equivalent `getPurePKBytes` * methods in the Kotlin and Swift implementations. * * @param privateKey - A `Uint8Array` representing the private key from which to extract the public key bytes. * * @returns A `Uint8Array` containing the raw public key bytes. */ getPurePKBytes(privateKey: Uint8Array): Uint8Array; /** * Validates if a byte array represents a valid P256 private key. * A valid private key must be in the range [1, n-1] where n is the curve order. * * @param key - The potential private key bytes * @returns true if the key is valid, false otherwise */ private isValidPrivateKey; /** * Converts a raw ECDSA signature (64 bytes: 32 bytes r + 32 bytes s) to DER format. * * @param rawSignature - 64-byte raw signature (32 bytes r + 32 bytes s) * @returns DER-encoded signature */ rawToDER(rawSignature: Uint8Array): Uint8Array; /** * Converts a DER-encoded ECDSA signature to raw format (64 bytes: 32 bytes r + 32 bytes s). * * @param derSignature - DER-encoded signature * @returns 64-byte raw signature (32 bytes r + 32 bytes s) */ derToRaw(derSignature: Uint8Array): Uint8Array; private encodeDERInteger; private encodeDERSequence; private decodeDERLength; private getDERLengthSize; private padTo32Bytes; }