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/des/des_ede3_cbc.ts

89 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-01-25 10:43:13 +00:00
import * as core from "webcrypto-core";
import { DesCrypto } from "./crypto";
import { DesCryptoKey } from "./key";
2023-03-21 18:50:09 +00:00
import { CryptoKey } from "../../keys";
import { setCryptoKey, getCryptoKey } from "../storage";
2019-01-25 10:43:13 +00:00
export type DesEde3CbcParams = core.DesParams;
export class DesEde3CbcProvider extends core.DesProvider {
public keySizeBits = 192;
public ivSize = 8;
public name = "DES-EDE3-CBC";
2023-04-04 10:20:37 +00:00
public async onGenerateKey(
algorithm: core.DesKeyGenParams,
extractable: boolean,
keyUsages: KeyUsage[]
): Promise<core.CryptoKey> {
2019-01-25 10:43:13 +00:00
const key = await DesCrypto.generateKey(
{
name: this.name,
length: this.keySizeBits,
},
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: DesEde3CbcParams,
key: DesCryptoKey,
data: ArrayBuffer
): Promise<ArrayBuffer> {
return DesCrypto.encrypt(
algorithm,
getCryptoKey(key) as DesCryptoKey,
new Uint8Array(data)
);
2019-01-25 10:43:13 +00:00
}
2023-04-04 10:20:37 +00:00
public async onDecrypt(
algorithm: DesEde3CbcParams,
key: DesCryptoKey,
data: ArrayBuffer
): Promise<ArrayBuffer> {
return DesCrypto.decrypt(
algorithm,
getCryptoKey(key) as DesCryptoKey,
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: CryptoKey
): Promise<JsonWebKey | ArrayBuffer> {
2020-03-13 15:20:02 +00:00
return DesCrypto.exportKey(format, getCryptoKey(key) as DesCryptoKey);
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<core.CryptoKey> {
const key = await DesCrypto.importKey(
format,
keyData,
{ name: this.name, length: this.keySizeBits },
extractable,
keyUsages
);
if (key.data.length !== this.keySizeBits >> 3) {
2019-01-25 10:43:13 +00:00
throw new core.OperationError("keyData: Wrong key size");
}
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
if (!(getCryptoKey(key) instanceof DesCryptoKey)) {
2019-01-25 10:43:13 +00:00
throw new TypeError("key: Is not a DesCryptoKey");
}
}
}