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/aes/aes_gcm.ts

79 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-01-25 10:43:13 +00:00
import * as core from "webcrypto-core";
import { AesCrypto } from "./crypto";
import { AesCryptoKey } from "./key";
2023-03-21 18:50:09 +00:00
import { setCryptoKey, getCryptoKey } from "../storage";
2019-01-25 10:43:13 +00:00
export class AesGcmProvider extends core.AesGcmProvider {
2023-04-04 10:20:37 +00:00
public async onGenerateKey(
algorithm: AesKeyGenParams,
extractable: boolean,
keyUsages: KeyUsage[]
): Promise<CryptoKey> {
2019-01-25 10:43:13 +00:00
const key = await AesCrypto.generateKey(
{
name: this.name,
length: algorithm.length,
},
extractable,
2023-04-04 10:20:37 +00:00
keyUsages
);
2019-01-25 10:43:13 +00:00
2020-03-13 15:20:02 +00:00
return setCryptoKey(key);
2019-01-25 10:43:13 +00:00
}
2023-04-04 10:20:37 +00:00
public async onEncrypt(
algorithm: AesGcmParams,
key: AesCryptoKey,
data: ArrayBuffer
): Promise<ArrayBuffer> {
return AesCrypto.encrypt(
algorithm,
getCryptoKey(key) as AesCryptoKey,
new Uint8Array(data)
);
2019-01-25 10:43:13 +00:00
}
2023-04-04 10:20:37 +00:00
public async onDecrypt(
algorithm: AesGcmParams,
key: AesCryptoKey,
data: ArrayBuffer
): Promise<ArrayBuffer> {
return AesCrypto.decrypt(
algorithm,
getCryptoKey(key) as AesCryptoKey,
new Uint8Array(data)
);
2019-01-25 10:43:13 +00:00
}
2023-04-04 10:20:37 +00:00
public async onExportKey(
format: KeyFormat,
key: AesCryptoKey
): Promise<JsonWebKey | ArrayBuffer> {
2020-03-13 15:20:02 +00:00
return AesCrypto.exportKey(format, getCryptoKey(key) as AesCryptoKey);
2019-01-25 10:43:13 +00:00
}
2023-04-04 10:20:37 +00:00
public async onImportKey(
format: KeyFormat,
keyData: JsonWebKey | ArrayBuffer,
algorithm: Algorithm,
extractable: boolean,
keyUsages: KeyUsage[]
): Promise<CryptoKey> {
const res = await AesCrypto.importKey(
format,
keyData,
{ name: algorithm.name },
extractable,
keyUsages
);
2020-03-13 15:20:02 +00:00
return setCryptoKey(res);
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
if (!(getCryptoKey(key) instanceof AesCryptoKey)) {
2019-01-25 10:43:13 +00:00
throw new TypeError("key: Is not a AesCryptoKey");
}
}
}