|
| 1 | +/* eslint-disable @typescript-eslint/naming-convention */ |
| 2 | +import { AsymmetricKeySpec, CryptoHash, KeyPairHandle, KeyPairSpec, Provider } from "@nmshd/rs-crypto-types"; |
| 3 | +import { defaults } from "lodash"; |
| 4 | +import { CoreBuffer } from "../CoreBuffer"; |
| 5 | +import { CryptoError } from "../CryptoError"; |
| 6 | +import { CryptoErrorCode } from "../CryptoErrorCode"; |
| 7 | +import { CryptoExchangeAlgorithm } from "../exchange/CryptoExchange"; |
| 8 | +import { CryptoHashAlgorithm } from "../hash/CryptoHash"; |
| 9 | +import { CryptoSignatureAlgorithm } from "../signature/CryptoSignatureAlgorithm"; |
| 10 | +import { getProviderOrThrow, ProviderIdentifier } from "./CryptoLayerProviders"; |
| 11 | + |
| 12 | +export const DEFAULT_KEY_PAIR_SPEC: KeyPairSpec = { |
| 13 | + asym_spec: "P256", |
| 14 | + cipher: "AesGcm256", |
| 15 | + signing_hash: "Sha2_512", |
| 16 | + ephemeral: false, |
| 17 | + non_exportable: false |
| 18 | +}; |
| 19 | + |
| 20 | +export function asymSpecFromCryptoAlgorithm( |
| 21 | + algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm |
| 22 | +): AsymmetricKeySpec { |
| 23 | + switch (algorithm) { |
| 24 | + case CryptoExchangeAlgorithm.ECDH_P256: |
| 25 | + return "P256"; |
| 26 | + case CryptoExchangeAlgorithm.ECDH_P521: |
| 27 | + return "P521"; |
| 28 | + case CryptoExchangeAlgorithm.ECDH_X25519: |
| 29 | + return "Curve25519"; |
| 30 | + case CryptoSignatureAlgorithm.ECDSA_P256: |
| 31 | + return "P256"; |
| 32 | + case CryptoSignatureAlgorithm.ECDSA_P521: |
| 33 | + return "P521"; |
| 34 | + case CryptoSignatureAlgorithm.ECDSA_ED25519: |
| 35 | + return "Curve25519"; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export function cryptoHashFromCryptoHashAlgorithm(algorithm: CryptoHashAlgorithm): CryptoHash { |
| 40 | + switch (algorithm) { |
| 41 | + case CryptoHashAlgorithm.SHA256: |
| 42 | + return "Sha2_256"; |
| 43 | + case CryptoHashAlgorithm.SHA512: |
| 44 | + return "Sha2_512"; |
| 45 | + case CryptoHashAlgorithm.BLAKE2B: |
| 46 | + throw new CryptoError(CryptoErrorCode.CalUnsupportedAlgorithm); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +export class CryptoLayerUtils { |
| 51 | + /** |
| 52 | + * Returns a provider and a key pair handle from default key spec and a raw private key. |
| 53 | + * |
| 54 | + * @param providerIdent An identifier of an initialized provider. |
| 55 | + * @param privateKeyBuffer The raw private key. |
| 56 | + * @param specOverride Override default key pair spec. |
| 57 | + * @returns Tuple with provider and handle to key pair. |
| 58 | + * |
| 59 | + * @throws `CryptoErrorCode.WrongParameters` if providerIdent does not match any initialized providers or if provider cannot import key pair. |
| 60 | + */ |
| 61 | + public static async providerAndKeyPairFromPrivateBuffer( |
| 62 | + providerIdent: ProviderIdentifier, |
| 63 | + privateKeyBuffer: CoreBuffer, |
| 64 | + specOverride: Partial<KeyPairSpec> |
| 65 | + ): Promise<[Provider, KeyPairHandle]> { |
| 66 | + const provider = getProviderOrThrow(providerIdent); |
| 67 | + const spec = defaults(specOverride, DEFAULT_KEY_PAIR_SPEC); |
| 68 | + const keyPair = await provider.importKeyPair(spec, new Uint8Array(0), privateKeyBuffer.buffer); |
| 69 | + return [provider, keyPair]; |
| 70 | + } |
| 71 | + |
| 72 | + public static async providerAndKeyPairFromPrivateBufferWithAlgorithm( |
| 73 | + providerIdent: ProviderIdentifier, |
| 74 | + privateKeyBuffer: CoreBuffer, |
| 75 | + asymmetricAlgorithm?: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm, |
| 76 | + hashAlgorithm?: CryptoHashAlgorithm |
| 77 | + ): Promise<[Provider, KeyPairHandle]> { |
| 78 | + const override: Partial<KeyPairSpec> = { |
| 79 | + asym_spec: asymmetricAlgorithm ? asymSpecFromCryptoAlgorithm(asymmetricAlgorithm) : undefined, |
| 80 | + signing_hash: hashAlgorithm ? cryptoHashFromCryptoHashAlgorithm(hashAlgorithm) : undefined |
| 81 | + }; |
| 82 | + return await this.providerAndKeyPairFromPrivateBuffer(providerIdent, privateKeyBuffer, override); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns a provider and a key pair handle from default key spec and a raw public key. |
| 87 | + * |
| 88 | + * @param providerIdent An identifier of an initialized provider. |
| 89 | + * @param privateKeyBuffer The raw public key. |
| 90 | + * @param specOverride Override default key pair spec. |
| 91 | + * @returns Tuple with provider and handle to key pair. |
| 92 | + * |
| 93 | + * @throws `CryptoErrorCode.WrongParameters` if providerIdent does not match any initialized providers or if provider cannot import key pair. |
| 94 | + */ |
| 95 | + public static async providerAndKeyPairFromPublicBuffer( |
| 96 | + providerIdent: ProviderIdentifier, |
| 97 | + publicKeyBuffer: CoreBuffer, |
| 98 | + specOverride: Partial<KeyPairSpec> |
| 99 | + ): Promise<[Provider, KeyPairHandle]> { |
| 100 | + const provider = getProviderOrThrow(providerIdent); |
| 101 | + const spec = defaults(specOverride, DEFAULT_KEY_PAIR_SPEC); |
| 102 | + const keyPair = await provider.importPublicKey(spec, publicKeyBuffer.buffer); |
| 103 | + return [provider, keyPair]; |
| 104 | + } |
| 105 | + |
| 106 | + public static async providerAndKeyPairFromPublicBufferWithAlgorithm( |
| 107 | + providerIdent: ProviderIdentifier, |
| 108 | + privateKeyBuffer: CoreBuffer, |
| 109 | + asymmetricAlgorithm?: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm, |
| 110 | + hashAlgorithm?: CryptoHashAlgorithm |
| 111 | + ): Promise<CryptoLayerUtils> { |
| 112 | + const override: Partial<KeyPairSpec> = { |
| 113 | + asym_spec: asymmetricAlgorithm ? asymSpecFromCryptoAlgorithm(asymmetricAlgorithm) : undefined, |
| 114 | + signing_hash: hashAlgorithm ? cryptoHashFromCryptoHashAlgorithm(hashAlgorithm) : undefined |
| 115 | + }; |
| 116 | + return await this.providerAndKeyPairFromPublicBuffer(providerIdent, privateKeyBuffer, override); |
| 117 | + } |
| 118 | +} |
0 commit comments