This repository has been archived on 2023-04-04. You can view files and clone it, but cannot push or open issues or pull requests.
webcrypto/src/mechs/ec/ec_dh.ts

50 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-01-25 10:43:13 +00:00
import * as core from "webcrypto-core";
import { CryptoKey } from "../../keys";
2020-03-13 15:20:02 +00:00
import { setCryptoKey, getCryptoKey } from "../storage";
2019-01-25 10:43:13 +00:00
import { EcCrypto } from "./crypto";
import { EcPrivateKey } from "./private_key";
import { EcPublicKey } from "./public_key";
export class EcdhProvider extends core.EcdhProvider {
2022-03-02 18:35:31 +00:00
public override namedCurves = core.EcCurves.names;
2021-10-26 09:39:51 +00:00
2022-03-02 18:35:31 +00:00
public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<CryptoKeyPair> {
2020-03-13 15:20:02 +00:00
const keys = await EcCrypto.generateKey(
2019-01-25 10:43:13 +00:00
{
...algorithm,
name: this.name,
},
extractable,
keyUsages);
2020-03-13 15:20:02 +00:00
return {
privateKey: setCryptoKey(keys.privateKey as CryptoKey),
publicKey: setCryptoKey(keys.publicKey as CryptoKey),
};
2019-01-25 10:43:13 +00:00
}
public async onExportKey(format: KeyFormat, key: CryptoKey): Promise<JsonWebKey | ArrayBuffer> {
2020-03-13 15:20:02 +00:00
return EcCrypto.exportKey(format, getCryptoKey(key));
2019-01-25 10:43:13 +00:00
}
2020-03-13 15:20:02 +00:00
public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: EcKeyImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise<core.CryptoKey> {
2019-01-25 10:43:13 +00:00
const key = await EcCrypto.importKey(format, keyData, {...algorithm, name: this.name}, extractable, keyUsages);
2020-03-13 15:20:02 +00:00
return setCryptoKey(key);
2019-01-25 10:43:13 +00:00
}
2022-03-02 18:35:31 +00:00
public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) {
2019-01-25 10:43:13 +00:00
super.checkCryptoKey(key, keyUsage);
2020-03-13 15:20:02 +00:00
const internalKey = getCryptoKey(key);
if (!(internalKey instanceof EcPrivateKey || internalKey instanceof EcPublicKey)) {
2019-01-25 10:43:13 +00:00
throw new TypeError("key: Is not EC CryptoKey");
}
}
public async onDeriveBits(algorithm: EcdhKeyDeriveParams, baseKey: CryptoKey, length: number): Promise<ArrayBuffer> {
2020-03-13 15:20:02 +00:00
const bits = await EcCrypto.deriveBits({...algorithm, public: getCryptoKey(algorithm.public)}, getCryptoKey(baseKey), length);
2019-01-25 10:43:13 +00:00
return bits;
}
}