diff --git a/src/keys/asymmetric.ts b/src/keys/asymmetric.ts index d2c83a9..565ba7c 100644 --- a/src/keys/asymmetric.ts +++ b/src/keys/asymmetric.ts @@ -1,8 +1,6 @@ import { CryptoKey } from "./key"; export abstract class AsymmetricKey extends CryptoKey { - public abstract override type: "public" | "private"; public pem?: string; - } diff --git a/src/keys/symmetric.ts b/src/keys/symmetric.ts index 7dfc742..e139e92 100644 --- a/src/keys/symmetric.ts +++ b/src/keys/symmetric.ts @@ -1,8 +1,6 @@ import { CryptoKey } from "./key"; export class SymmetricKey extends CryptoKey { - public override readonly kty = "oct"; public override readonly type = "secret" as const; - } diff --git a/src/mechs/aes/aes_cbc.ts b/src/mechs/aes/aes_cbc.ts index c5d7eb5..bc63c73 100644 --- a/src/mechs/aes/aes_cbc.ts +++ b/src/mechs/aes/aes_cbc.ts @@ -4,33 +4,68 @@ import { AesCryptoKey } from "./key"; import { getCryptoKey, setCryptoKey } from "../storage"; export class AesCbcProvider extends core.AesCbcProvider { - - public async onGenerateKey(algorithm: AesKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { + public async onGenerateKey( + algorithm: AesKeyGenParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { const key = await AesCrypto.generateKey( { name: this.name, length: algorithm.length, }, extractable, - keyUsages); + keyUsages + ); return setCryptoKey(key); } - public async onEncrypt(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer): Promise { - return AesCrypto.encrypt(algorithm, getCryptoKey(key) as AesCryptoKey, new Uint8Array(data)); + public async onEncrypt( + algorithm: Algorithm, + key: AesCryptoKey, + data: ArrayBuffer + ): Promise { + return AesCrypto.encrypt( + algorithm, + getCryptoKey(key) as AesCryptoKey, + new Uint8Array(data) + ); } - public async onDecrypt(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer): Promise { - return AesCrypto.decrypt(algorithm, getCryptoKey(key) as AesCryptoKey, new Uint8Array(data)); + public async onDecrypt( + algorithm: Algorithm, + key: AesCryptoKey, + data: ArrayBuffer + ): Promise { + return AesCrypto.decrypt( + algorithm, + getCryptoKey(key) as AesCryptoKey, + new Uint8Array(data) + ); } - public async onExportKey(format: KeyFormat, key: AesCryptoKey): Promise { + public async onExportKey( + format: KeyFormat, + key: AesCryptoKey + ): Promise { return AesCrypto.exportKey(format, getCryptoKey(key) as AesCryptoKey); } - public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise { - const key = await AesCrypto.importKey(format, keyData, { name: algorithm.name }, extractable, keyUsages); + public async onImportKey( + format: KeyFormat, + keyData: JsonWebKey | ArrayBuffer, + algorithm: Algorithm, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { + const key = await AesCrypto.importKey( + format, + keyData, + { name: algorithm.name }, + extractable, + keyUsages + ); return setCryptoKey(key); } diff --git a/src/mechs/aes/aes_ctr.ts b/src/mechs/aes/aes_ctr.ts index f341774..bbe985c 100644 --- a/src/mechs/aes/aes_ctr.ts +++ b/src/mechs/aes/aes_ctr.ts @@ -4,33 +4,68 @@ import { AesCryptoKey } from "./key"; import { setCryptoKey, getCryptoKey } from "../storage"; export class AesCtrProvider extends core.AesCtrProvider { - - public async onGenerateKey(algorithm: AesKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { + public async onGenerateKey( + algorithm: AesKeyGenParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { const key = await AesCrypto.generateKey( { name: this.name, length: algorithm.length, }, extractable, - keyUsages); + keyUsages + ); return setCryptoKey(key); } - public async onEncrypt(algorithm: AesCtrParams, key: AesCryptoKey, data: ArrayBuffer): Promise { - return AesCrypto.encrypt(algorithm, getCryptoKey(key) as AesCryptoKey, new Uint8Array(data)); + public async onEncrypt( + algorithm: AesCtrParams, + key: AesCryptoKey, + data: ArrayBuffer + ): Promise { + return AesCrypto.encrypt( + algorithm, + getCryptoKey(key) as AesCryptoKey, + new Uint8Array(data) + ); } - public async onDecrypt(algorithm: AesCtrParams, key: AesCryptoKey, data: ArrayBuffer): Promise { - return AesCrypto.decrypt(algorithm, getCryptoKey(key) as AesCryptoKey, new Uint8Array(data)); + public async onDecrypt( + algorithm: AesCtrParams, + key: AesCryptoKey, + data: ArrayBuffer + ): Promise { + return AesCrypto.decrypt( + algorithm, + getCryptoKey(key) as AesCryptoKey, + new Uint8Array(data) + ); } - public async onExportKey(format: KeyFormat, key: AesCryptoKey): Promise { + public async onExportKey( + format: KeyFormat, + key: AesCryptoKey + ): Promise { return AesCrypto.exportKey(format, getCryptoKey(key) as AesCryptoKey); } - public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise { - const res = await AesCrypto.importKey(format, keyData, { name: algorithm.name }, extractable, keyUsages); + public async onImportKey( + format: KeyFormat, + keyData: JsonWebKey | ArrayBuffer, + algorithm: Algorithm, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { + const res = await AesCrypto.importKey( + format, + keyData, + { name: algorithm.name }, + extractable, + keyUsages + ); return setCryptoKey(res); } diff --git a/src/mechs/aes/aes_ecb.ts b/src/mechs/aes/aes_ecb.ts index 1280a3f..9a84183 100644 --- a/src/mechs/aes/aes_ecb.ts +++ b/src/mechs/aes/aes_ecb.ts @@ -4,33 +4,68 @@ import { AesCryptoKey } from "./key"; import { setCryptoKey, getCryptoKey } from "../storage"; export class AesEcbProvider extends core.AesEcbProvider { - - public async onGenerateKey(algorithm: AesKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { + public async onGenerateKey( + algorithm: AesKeyGenParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { const key = await AesCrypto.generateKey( { name: this.name, length: algorithm.length, }, extractable, - keyUsages); + keyUsages + ); return setCryptoKey(key); } - public async onEncrypt(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer): Promise { - return AesCrypto.encrypt(algorithm, getCryptoKey(key) as AesCryptoKey, new Uint8Array(data)); + public async onEncrypt( + algorithm: Algorithm, + key: AesCryptoKey, + data: ArrayBuffer + ): Promise { + return AesCrypto.encrypt( + algorithm, + getCryptoKey(key) as AesCryptoKey, + new Uint8Array(data) + ); } - public async onDecrypt(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer): Promise { - return AesCrypto.decrypt(algorithm, getCryptoKey(key) as AesCryptoKey, new Uint8Array(data)); + public async onDecrypt( + algorithm: Algorithm, + key: AesCryptoKey, + data: ArrayBuffer + ): Promise { + return AesCrypto.decrypt( + algorithm, + getCryptoKey(key) as AesCryptoKey, + new Uint8Array(data) + ); } - public async onExportKey(format: KeyFormat, key: AesCryptoKey): Promise { + public async onExportKey( + format: KeyFormat, + key: AesCryptoKey + ): Promise { return AesCrypto.exportKey(format, getCryptoKey(key) as AesCryptoKey); } - public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise { - const res = await AesCrypto.importKey(format, keyData, { name: algorithm.name }, extractable, keyUsages); + public async onImportKey( + format: KeyFormat, + keyData: JsonWebKey | ArrayBuffer, + algorithm: Algorithm, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { + const res = await AesCrypto.importKey( + format, + keyData, + { name: algorithm.name }, + extractable, + keyUsages + ); return setCryptoKey(res); } diff --git a/src/mechs/aes/aes_gcm.ts b/src/mechs/aes/aes_gcm.ts index 71b406e..f168a67 100644 --- a/src/mechs/aes/aes_gcm.ts +++ b/src/mechs/aes/aes_gcm.ts @@ -4,33 +4,68 @@ import { AesCryptoKey } from "./key"; import { setCryptoKey, getCryptoKey } from "../storage"; export class AesGcmProvider extends core.AesGcmProvider { - - public async onGenerateKey(algorithm: AesKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { + public async onGenerateKey( + algorithm: AesKeyGenParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { const key = await AesCrypto.generateKey( { name: this.name, length: algorithm.length, }, extractable, - keyUsages); + keyUsages + ); return setCryptoKey(key); } - public async onEncrypt(algorithm: AesGcmParams, key: AesCryptoKey, data: ArrayBuffer): Promise { - return AesCrypto.encrypt(algorithm, getCryptoKey(key) as AesCryptoKey, new Uint8Array(data)); + public async onEncrypt( + algorithm: AesGcmParams, + key: AesCryptoKey, + data: ArrayBuffer + ): Promise { + return AesCrypto.encrypt( + algorithm, + getCryptoKey(key) as AesCryptoKey, + new Uint8Array(data) + ); } - public async onDecrypt(algorithm: AesGcmParams, key: AesCryptoKey, data: ArrayBuffer): Promise { - return AesCrypto.decrypt(algorithm, getCryptoKey(key) as AesCryptoKey, new Uint8Array(data)); + public async onDecrypt( + algorithm: AesGcmParams, + key: AesCryptoKey, + data: ArrayBuffer + ): Promise { + return AesCrypto.decrypt( + algorithm, + getCryptoKey(key) as AesCryptoKey, + new Uint8Array(data) + ); } - public async onExportKey(format: KeyFormat, key: AesCryptoKey): Promise { + public async onExportKey( + format: KeyFormat, + key: AesCryptoKey + ): Promise { return AesCrypto.exportKey(format, getCryptoKey(key) as AesCryptoKey); } - public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise { - const res = await AesCrypto.importKey(format, keyData, { name: algorithm.name }, extractable, keyUsages); + public async onImportKey( + format: KeyFormat, + keyData: JsonWebKey | ArrayBuffer, + algorithm: Algorithm, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { + const res = await AesCrypto.importKey( + format, + keyData, + { name: algorithm.name }, + extractable, + keyUsages + ); return setCryptoKey(res); } diff --git a/src/mechs/aes/aes_kw.ts b/src/mechs/aes/aes_kw.ts index 161200a..e55dcd3 100644 --- a/src/mechs/aes/aes_kw.ts +++ b/src/mechs/aes/aes_kw.ts @@ -4,34 +4,68 @@ import { AesCryptoKey } from "./key"; import { setCryptoKey, getCryptoKey } from "../storage"; export class AesKwProvider extends core.AesKwProvider { - - public async onGenerateKey(algorithm: AesKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { + public async onGenerateKey( + algorithm: AesKeyGenParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { const res = await AesCrypto.generateKey( { name: this.name, length: algorithm.length, }, extractable, - keyUsages, + keyUsages ); return setCryptoKey(res); } - public async onExportKey(format: KeyFormat, key: AesCryptoKey): Promise { + public async onExportKey( + format: KeyFormat, + key: AesCryptoKey + ): Promise { return AesCrypto.exportKey(format, getCryptoKey(key) as AesCryptoKey); } - public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise { - const res = await AesCrypto.importKey(format, keyData, { name: algorithm.name }, extractable, keyUsages); + public async onImportKey( + format: KeyFormat, + keyData: JsonWebKey | ArrayBuffer, + algorithm: Algorithm, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { + const res = await AesCrypto.importKey( + format, + keyData, + { name: algorithm.name }, + extractable, + keyUsages + ); return setCryptoKey(res); } - public override async onEncrypt(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer): Promise { - return AesCrypto.encrypt(algorithm, getCryptoKey(key) as AesCryptoKey, new Uint8Array(data)); + public override async onEncrypt( + algorithm: Algorithm, + key: AesCryptoKey, + data: ArrayBuffer + ): Promise { + return AesCrypto.encrypt( + algorithm, + getCryptoKey(key) as AesCryptoKey, + new Uint8Array(data) + ); } - public override async onDecrypt(algorithm: Algorithm, key: AesCryptoKey, data: ArrayBuffer): Promise { - return AesCrypto.decrypt(algorithm, getCryptoKey(key) as AesCryptoKey, new Uint8Array(data)); + public override async onDecrypt( + algorithm: Algorithm, + key: AesCryptoKey, + data: ArrayBuffer + ): Promise { + return AesCrypto.decrypt( + algorithm, + getCryptoKey(key) as AesCryptoKey, + new Uint8Array(data) + ); } public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { diff --git a/src/mechs/aes/key.ts b/src/mechs/aes/key.ts index d652a29..47815d0 100644 --- a/src/mechs/aes/key.ts +++ b/src/mechs/aes/key.ts @@ -4,10 +4,9 @@ import { JsonBase64UrlConverter } from "../../converters"; import { SymmetricKey } from "../../keys"; export class AesCryptoKey extends SymmetricKey { - public override algorithm!: AesKeyAlgorithm; - @JsonProp({name: "k", converter: JsonBase64UrlConverter}) + @JsonProp({ name: "k", converter: JsonBase64UrlConverter }) public override data!: Buffer; // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -34,5 +33,4 @@ export class AesCryptoKey extends SymmetricKey { public override set alg(value: string) { // nothing, cause set is needed for json-schema, but is not used by module } - } diff --git a/src/mechs/des/des_cbc.ts b/src/mechs/des/des_cbc.ts index bdf5ae3..fbc2e85 100644 --- a/src/mechs/des/des_cbc.ts +++ b/src/mechs/des/des_cbc.ts @@ -7,38 +7,73 @@ import { setCryptoKey, getCryptoKey } from "../storage"; export type DesCbcParams = core.DesParams; export class DesCbcProvider extends core.DesProvider { - public keySizeBits = 64; public ivSize = 8; public name = "DES-CBC"; - public async onGenerateKey(algorithm: core.DesKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { + public async onGenerateKey( + algorithm: core.DesKeyGenParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { const key = await DesCrypto.generateKey( { name: this.name, length: this.keySizeBits, }, extractable, - keyUsages); + keyUsages + ); return setCryptoKey(key); } - public async onEncrypt(algorithm: DesCbcParams, key: DesCryptoKey, data: ArrayBuffer): Promise { - return DesCrypto.encrypt(algorithm, getCryptoKey(key) as DesCryptoKey, new Uint8Array(data)); + public async onEncrypt( + algorithm: DesCbcParams, + key: DesCryptoKey, + data: ArrayBuffer + ): Promise { + return DesCrypto.encrypt( + algorithm, + getCryptoKey(key) as DesCryptoKey, + new Uint8Array(data) + ); } - public async onDecrypt(algorithm: DesCbcParams, key: DesCryptoKey, data: ArrayBuffer): Promise { - return DesCrypto.decrypt(algorithm, getCryptoKey(key) as DesCryptoKey, new Uint8Array(data)); + public async onDecrypt( + algorithm: DesCbcParams, + key: DesCryptoKey, + data: ArrayBuffer + ): Promise { + return DesCrypto.decrypt( + algorithm, + getCryptoKey(key) as DesCryptoKey, + new Uint8Array(data) + ); } - public async onExportKey(format: KeyFormat, key: CryptoKey): Promise { + public async onExportKey( + format: KeyFormat, + key: CryptoKey + ): Promise { return DesCrypto.exportKey(format, getCryptoKey(key) as DesCryptoKey); } - public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise { - const key = await DesCrypto.importKey(format, keyData, { name: this.name, length: this.keySizeBits }, extractable, keyUsages); - if (key.data.length !== (this.keySizeBits >> 3)) { + public async onImportKey( + format: KeyFormat, + keyData: JsonWebKey | ArrayBuffer, + algorithm: Algorithm, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { + const key = await DesCrypto.importKey( + format, + keyData, + { name: this.name, length: this.keySizeBits }, + extractable, + keyUsages + ); + if (key.data.length !== this.keySizeBits >> 3) { throw new core.OperationError("keyData: Wrong key size"); } return setCryptoKey(key); @@ -50,5 +85,4 @@ export class DesCbcProvider extends core.DesProvider { throw new TypeError("key: Is not a DesCryptoKey"); } } - } diff --git a/src/mechs/des/des_ede3_cbc.ts b/src/mechs/des/des_ede3_cbc.ts index b3c02e3..8636497 100644 --- a/src/mechs/des/des_ede3_cbc.ts +++ b/src/mechs/des/des_ede3_cbc.ts @@ -7,38 +7,73 @@ import { setCryptoKey, getCryptoKey } from "../storage"; export type DesEde3CbcParams = core.DesParams; export class DesEde3CbcProvider extends core.DesProvider { - public keySizeBits = 192; public ivSize = 8; public name = "DES-EDE3-CBC"; - public async onGenerateKey(algorithm: core.DesKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { + public async onGenerateKey( + algorithm: core.DesKeyGenParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { const key = await DesCrypto.generateKey( { name: this.name, length: this.keySizeBits, }, extractable, - keyUsages); + keyUsages + ); return setCryptoKey(key); } - public async onEncrypt(algorithm: DesEde3CbcParams, key: DesCryptoKey, data: ArrayBuffer): Promise { - return DesCrypto.encrypt(algorithm, getCryptoKey(key) as DesCryptoKey, new Uint8Array(data)); + public async onEncrypt( + algorithm: DesEde3CbcParams, + key: DesCryptoKey, + data: ArrayBuffer + ): Promise { + return DesCrypto.encrypt( + algorithm, + getCryptoKey(key) as DesCryptoKey, + new Uint8Array(data) + ); } - public async onDecrypt(algorithm: DesEde3CbcParams, key: DesCryptoKey, data: ArrayBuffer): Promise { - return DesCrypto.decrypt(algorithm, getCryptoKey(key) as DesCryptoKey, new Uint8Array(data)); + public async onDecrypt( + algorithm: DesEde3CbcParams, + key: DesCryptoKey, + data: ArrayBuffer + ): Promise { + return DesCrypto.decrypt( + algorithm, + getCryptoKey(key) as DesCryptoKey, + new Uint8Array(data) + ); } - public async onExportKey(format: KeyFormat, key: CryptoKey): Promise { + public async onExportKey( + format: KeyFormat, + key: CryptoKey + ): Promise { return DesCrypto.exportKey(format, getCryptoKey(key) as DesCryptoKey); } - public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[]): Promise { - const key = await DesCrypto.importKey(format, keyData, { name: this.name, length: this.keySizeBits }, extractable, keyUsages); - if (key.data.length !== (this.keySizeBits >> 3)) { + public async onImportKey( + format: KeyFormat, + keyData: JsonWebKey | ArrayBuffer, + algorithm: Algorithm, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { + const key = await DesCrypto.importKey( + format, + keyData, + { name: this.name, length: this.keySizeBits }, + extractable, + keyUsages + ); + if (key.data.length !== this.keySizeBits >> 3) { throw new core.OperationError("keyData: Wrong key size"); } return setCryptoKey(key); @@ -50,5 +85,4 @@ export class DesEde3CbcProvider extends core.DesProvider { throw new TypeError("key: Is not a DesCryptoKey"); } } - } diff --git a/src/mechs/des/key.ts b/src/mechs/des/key.ts index 65910da..198cd5c 100644 --- a/src/mechs/des/key.ts +++ b/src/mechs/des/key.ts @@ -4,10 +4,9 @@ import { JsonBase64UrlConverter } from "../../converters"; import { SymmetricKey } from "../../keys"; export class DesCryptoKey extends SymmetricKey { - public override algorithm!: core.DesKeyAlgorithm; - @JsonProp({name: "k", converter: JsonBase64UrlConverter}) + @JsonProp({ name: "k", converter: JsonBase64UrlConverter }) public override data!: Buffer; // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -26,5 +25,4 @@ export class DesCryptoKey extends SymmetricKey { public override set alg(value: string) { // nothing, cause set is needed for json-schema, but is not used by module } - } diff --git a/src/mechs/ec/ec_dh.ts b/src/mechs/ec/ec_dh.ts index 21d412e..c1f5532 100644 --- a/src/mechs/ec/ec_dh.ts +++ b/src/mechs/ec/ec_dh.ts @@ -6,17 +6,21 @@ import { CryptoKey } from "../../keys"; import { setCryptoKey, getCryptoKey } from "../storage"; export class EcdhProvider extends core.EcdhProvider { - public override namedCurves = core.EcCurves.names; - public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { + public async onGenerateKey( + algorithm: EcKeyGenParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { const keys = await EcCrypto.generateKey( { ...algorithm, name: this.name, }, extractable, - keyUsages); + keyUsages + ); return { privateKey: setCryptoKey(keys.privateKey as CryptoKey), @@ -24,26 +28,53 @@ export class EcdhProvider extends core.EcdhProvider { }; } - public async onExportKey(format: KeyFormat, key: CryptoKey): Promise { + public async onExportKey( + format: KeyFormat, + key: CryptoKey + ): Promise { return EcCrypto.exportKey(format, getCryptoKey(key)); } - public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: EcKeyImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { - const key = await EcCrypto.importKey(format, keyData, {...algorithm, name: this.name}, extractable, keyUsages); + public async onImportKey( + format: KeyFormat, + keyData: JsonWebKey | ArrayBuffer, + algorithm: EcKeyImportParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { + const key = await EcCrypto.importKey( + format, + keyData, + { ...algorithm, name: this.name }, + extractable, + keyUsages + ); return setCryptoKey(key); } public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { super.checkCryptoKey(key, keyUsage); const internalKey = getCryptoKey(key); - if (!(internalKey instanceof EcPrivateKey || internalKey instanceof EcPublicKey)) { + if ( + !( + internalKey instanceof EcPrivateKey || + internalKey instanceof EcPublicKey + ) + ) { throw new TypeError("key: Is not EC CryptoKey"); } } - public async onDeriveBits(algorithm: EcdhKeyDeriveParams, baseKey: CryptoKey, length: number): Promise { - const bits = await EcCrypto.deriveBits({...algorithm, public: getCryptoKey(algorithm.public)}, getCryptoKey(baseKey), length); + public async onDeriveBits( + algorithm: EcdhKeyDeriveParams, + baseKey: CryptoKey, + length: number + ): Promise { + const bits = await EcCrypto.deriveBits( + { ...algorithm, public: getCryptoKey(algorithm.public) }, + getCryptoKey(baseKey), + length + ); return bits; } - } diff --git a/src/mechs/ec/ec_dsa.ts b/src/mechs/ec/ec_dsa.ts index c3988c1..6640bbf 100644 --- a/src/mechs/ec/ec_dsa.ts +++ b/src/mechs/ec/ec_dsa.ts @@ -5,22 +5,33 @@ import { EcPublicKey } from "./public_key"; import { setCryptoKey, getCryptoKey } from "../storage"; export class EcdsaProvider extends core.EcdsaProvider { - public override namedCurves = core.EcCurves.names; public override hashAlgorithms = [ - "SHA-1", "SHA-256", "SHA-384", "SHA-512", - "shake128", "shake256", - "SHA3-256", "SHA3-384", "SHA3-512"]; + "SHA-1", + "SHA-256", + "SHA-384", + "SHA-512", + "shake128", + "shake256", + "SHA3-256", + "SHA3-384", + "SHA3-512", + ]; - public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { + public async onGenerateKey( + algorithm: EcKeyGenParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { const keys = await EcCrypto.generateKey( { ...algorithm, name: this.name, }, extractable, - keyUsages); + keyUsages + ); return { privateKey: setCryptoKey(keys.privateKey as EcPrivateKey), @@ -28,29 +39,66 @@ export class EcdsaProvider extends core.EcdsaProvider { }; } - public async onSign(algorithm: EcdsaParams, key: EcPrivateKey, data: ArrayBuffer): Promise { - return EcCrypto.sign(algorithm, getCryptoKey(key) as EcPrivateKey, new Uint8Array(data)); + public async onSign( + algorithm: EcdsaParams, + key: EcPrivateKey, + data: ArrayBuffer + ): Promise { + return EcCrypto.sign( + algorithm, + getCryptoKey(key) as EcPrivateKey, + new Uint8Array(data) + ); } - public async onVerify(algorithm: EcdsaParams, key: EcPublicKey, signature: ArrayBuffer, data: ArrayBuffer): Promise { - return EcCrypto.verify(algorithm, getCryptoKey(key) as EcPublicKey, new Uint8Array(signature), new Uint8Array(data)); + public async onVerify( + algorithm: EcdsaParams, + key: EcPublicKey, + signature: ArrayBuffer, + data: ArrayBuffer + ): Promise { + return EcCrypto.verify( + algorithm, + getCryptoKey(key) as EcPublicKey, + new Uint8Array(signature), + new Uint8Array(data) + ); } - public async onExportKey(format: KeyFormat, key: CryptoKey): Promise { + public async onExportKey( + format: KeyFormat, + key: CryptoKey + ): Promise { return EcCrypto.exportKey(format, getCryptoKey(key)); } - public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: EcKeyImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { - const key = await EcCrypto.importKey(format, keyData, { ...algorithm, name: this.name }, extractable, keyUsages); + public async onImportKey( + format: KeyFormat, + keyData: JsonWebKey | ArrayBuffer, + algorithm: EcKeyImportParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { + const key = await EcCrypto.importKey( + format, + keyData, + { ...algorithm, name: this.name }, + extractable, + keyUsages + ); return setCryptoKey(key); } public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { super.checkCryptoKey(key, keyUsage); const internalKey = getCryptoKey(key); - if (!(internalKey instanceof EcPrivateKey || internalKey instanceof EcPublicKey)) { + if ( + !( + internalKey instanceof EcPrivateKey || + internalKey instanceof EcPublicKey + ) + ) { throw new TypeError("key: Is not EC CryptoKey"); } } - } diff --git a/src/mechs/ec/helper.ts b/src/mechs/ec/helper.ts index 88f68b1..218720a 100644 --- a/src/mechs/ec/helper.ts +++ b/src/mechs/ec/helper.ts @@ -15,40 +15,42 @@ const namedOIDs: { [key: string]: string } = { "K-256": "1.3.132.0.10", // brainpool - "brainpoolP160r1": "1.3.36.3.3.2.8.1.1.1", + brainpoolP160r1: "1.3.36.3.3.2.8.1.1.1", "1.3.36.3.3.2.8.1.1.1": "brainpoolP160r1", - "brainpoolP160t1": "1.3.36.3.3.2.8.1.1.2", + brainpoolP160t1: "1.3.36.3.3.2.8.1.1.2", "1.3.36.3.3.2.8.1.1.2": "brainpoolP160t1", - "brainpoolP192r1": "1.3.36.3.3.2.8.1.1.3", + brainpoolP192r1: "1.3.36.3.3.2.8.1.1.3", "1.3.36.3.3.2.8.1.1.3": "brainpoolP192r1", - "brainpoolP192t1": "1.3.36.3.3.2.8.1.1.4", + brainpoolP192t1: "1.3.36.3.3.2.8.1.1.4", "1.3.36.3.3.2.8.1.1.4": "brainpoolP192t1", - "brainpoolP224r1": "1.3.36.3.3.2.8.1.1.5", + brainpoolP224r1: "1.3.36.3.3.2.8.1.1.5", "1.3.36.3.3.2.8.1.1.5": "brainpoolP224r1", - "brainpoolP224t1": "1.3.36.3.3.2.8.1.1.6", + brainpoolP224t1: "1.3.36.3.3.2.8.1.1.6", "1.3.36.3.3.2.8.1.1.6": "brainpoolP224t1", - "brainpoolP256r1": "1.3.36.3.3.2.8.1.1.7", + brainpoolP256r1: "1.3.36.3.3.2.8.1.1.7", "1.3.36.3.3.2.8.1.1.7": "brainpoolP256r1", - "brainpoolP256t1": "1.3.36.3.3.2.8.1.1.8", + brainpoolP256t1: "1.3.36.3.3.2.8.1.1.8", "1.3.36.3.3.2.8.1.1.8": "brainpoolP256t1", - "brainpoolP320r1": "1.3.36.3.3.2.8.1.1.9", + brainpoolP320r1: "1.3.36.3.3.2.8.1.1.9", "1.3.36.3.3.2.8.1.1.9": "brainpoolP320r1", - "brainpoolP320t1": "1.3.36.3.3.2.8.1.1.10", + brainpoolP320t1: "1.3.36.3.3.2.8.1.1.10", "1.3.36.3.3.2.8.1.1.10": "brainpoolP320t1", - "brainpoolP384r1": "1.3.36.3.3.2.8.1.1.11", + brainpoolP384r1: "1.3.36.3.3.2.8.1.1.11", "1.3.36.3.3.2.8.1.1.11": "brainpoolP384r1", - "brainpoolP384t1": "1.3.36.3.3.2.8.1.1.12", + brainpoolP384t1: "1.3.36.3.3.2.8.1.1.12", "1.3.36.3.3.2.8.1.1.12": "brainpoolP384t1", - "brainpoolP512r1": "1.3.36.3.3.2.8.1.1.13", + brainpoolP512r1: "1.3.36.3.3.2.8.1.1.13", "1.3.36.3.3.2.8.1.1.13": "brainpoolP512r1", - "brainpoolP512t1": "1.3.36.3.3.2.8.1.1.14", + brainpoolP512t1: "1.3.36.3.3.2.8.1.1.14", "1.3.36.3.3.2.8.1.1.14": "brainpoolP512t1", }; export function getNamedCurveByOid(oid: string) { const namedCurve = namedOIDs[oid]; if (!namedCurve) { - throw new core.OperationError(`Cannot convert OID(${oid}) to WebCrypto named curve`); + throw new core.OperationError( + `Cannot convert OID(${oid}) to WebCrypto named curve` + ); } return namedCurve; } @@ -56,7 +58,9 @@ export function getNamedCurveByOid(oid: string) { export function getOidByNamedCurve(namedCurve: string) { const oid = namedOIDs[namedCurve]; if (!oid) { - throw new core.OperationError(`Cannot convert WebCrypto named curve '${namedCurve}' to OID`); + throw new core.OperationError( + `Cannot convert WebCrypto named curve '${namedCurve}' to OID` + ); } return oid; } diff --git a/src/mechs/ed/ecdh_es.ts b/src/mechs/ed/ecdh_es.ts index 0b29d7b..2e04963 100644 --- a/src/mechs/ed/ecdh_es.ts +++ b/src/mechs/ed/ecdh_es.ts @@ -4,15 +4,19 @@ import { CryptoKey } from "../../keys"; import { getCryptoKey, setCryptoKey } from "../storage"; export class EcdhEsProvider extends core.EcdhEsProvider { - - public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { + public async onGenerateKey( + algorithm: EcKeyGenParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { const keys = await EdCrypto.generateKey( { name: this.name, namedCurve: algorithm.namedCurve.toUpperCase(), }, extractable, - keyUsages); + keyUsages + ); return { privateKey: setCryptoKey(keys.privateKey as CryptoKey), @@ -20,18 +24,40 @@ export class EcdhEsProvider extends core.EcdhEsProvider { }; } - public async onDeriveBits(algorithm: EcdhKeyDeriveParams, baseKey: core.CryptoKey, length: number): Promise { - const bits = await EdCrypto.deriveBits({...algorithm, public: getCryptoKey(algorithm.public)}, getCryptoKey(baseKey), length); + public async onDeriveBits( + algorithm: EcdhKeyDeriveParams, + baseKey: core.CryptoKey, + length: number + ): Promise { + const bits = await EdCrypto.deriveBits( + { ...algorithm, public: getCryptoKey(algorithm.public) }, + getCryptoKey(baseKey), + length + ); return bits; } - - public async onExportKey(format: KeyFormat, key: CryptoKey): Promise { + + public async onExportKey( + format: KeyFormat, + key: CryptoKey + ): Promise { return EdCrypto.exportKey(format, getCryptoKey(key)); } - - public async onImportKey(format: KeyFormat, keyData: ArrayBuffer | JsonWebKey, algorithm: EcKeyImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { - const key = await EdCrypto.importKey(format, keyData, { ...algorithm, name: this.name }, extractable, keyUsages); + + public async onImportKey( + format: KeyFormat, + keyData: ArrayBuffer | JsonWebKey, + algorithm: EcKeyImportParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { + const key = await EdCrypto.importKey( + format, + keyData, + { ...algorithm, name: this.name }, + extractable, + keyUsages + ); return setCryptoKey(key); } - -} \ No newline at end of file +} diff --git a/src/mechs/ed/eddsa.ts b/src/mechs/ed/eddsa.ts index 516ba3e..ee00fd7 100644 --- a/src/mechs/ed/eddsa.ts +++ b/src/mechs/ed/eddsa.ts @@ -6,15 +6,19 @@ import { CryptoKey } from "../../keys"; import { getCryptoKey, setCryptoKey } from "../storage"; export class EdDsaProvider extends core.EdDsaProvider { - - public async onGenerateKey(algorithm: EcKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { + public async onGenerateKey( + algorithm: EcKeyGenParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { const keys = await EdCrypto.generateKey( { name: this.name, namedCurve: algorithm.namedCurve.replace(/^ed/i, "Ed"), }, extractable, - keyUsages); + keyUsages + ); return { privateKey: setCryptoKey(keys.privateKey as CryptoKey), @@ -22,21 +26,53 @@ export class EdDsaProvider extends core.EdDsaProvider { }; } - public async onSign(algorithm: EcdsaParams, key: CryptoKey, data: ArrayBuffer): Promise { - return EdCrypto.sign(algorithm, getCryptoKey(key) as EdPrivateKey, new Uint8Array(data)); + public async onSign( + algorithm: EcdsaParams, + key: CryptoKey, + data: ArrayBuffer + ): Promise { + return EdCrypto.sign( + algorithm, + getCryptoKey(key) as EdPrivateKey, + new Uint8Array(data) + ); } - public async onVerify(algorithm: EcdsaParams, key: CryptoKey, signature: ArrayBuffer, data: ArrayBuffer): Promise { - return EdCrypto.verify(algorithm, getCryptoKey(key) as EdPublicKey, new Uint8Array(signature), new Uint8Array(data)); + public async onVerify( + algorithm: EcdsaParams, + key: CryptoKey, + signature: ArrayBuffer, + data: ArrayBuffer + ): Promise { + return EdCrypto.verify( + algorithm, + getCryptoKey(key) as EdPublicKey, + new Uint8Array(signature), + new Uint8Array(data) + ); } - public async onExportKey(format: KeyFormat, key: CryptoKey): Promise { + public async onExportKey( + format: KeyFormat, + key: CryptoKey + ): Promise { return EdCrypto.exportKey(format, getCryptoKey(key)); } - public async onImportKey(format: KeyFormat, keyData: ArrayBuffer | JsonWebKey, algorithm: EcKeyImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { - const key = await EdCrypto.importKey(format, keyData, { ...algorithm, name: this.name }, extractable, keyUsages); + public async onImportKey( + format: KeyFormat, + keyData: ArrayBuffer | JsonWebKey, + algorithm: EcKeyImportParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { + const key = await EdCrypto.importKey( + format, + keyData, + { ...algorithm, name: this.name }, + extractable, + keyUsages + ); return setCryptoKey(key); } - -} \ No newline at end of file +} diff --git a/src/mechs/ed/helper.ts b/src/mechs/ed/helper.ts index e9482ab..4d0cde9 100644 --- a/src/mechs/ed/helper.ts +++ b/src/mechs/ed/helper.ts @@ -3,22 +3,24 @@ import * as core from "webcrypto-core"; const edOIDs: { [key: string]: string } = { // Ed448 [core.asn1.idEd448]: "Ed448", - "ed448": core.asn1.idEd448, + ed448: core.asn1.idEd448, // X448 [core.asn1.idX448]: "X448", - "x448": core.asn1.idX448, + x448: core.asn1.idX448, // Ed25519 [core.asn1.idEd25519]: "Ed25519", - "ed25519": core.asn1.idEd25519, + ed25519: core.asn1.idEd25519, // X25519 [core.asn1.idX25519]: "X25519", - "x25519": core.asn1.idX25519, + x25519: core.asn1.idX25519, }; export function getNamedCurveByOid(oid: string) { const namedCurve = edOIDs[oid]; if (!namedCurve) { - throw new core.OperationError(`Cannot convert OID(${oid}) to WebCrypto named curve`); + throw new core.OperationError( + `Cannot convert OID(${oid}) to WebCrypto named curve` + ); } return namedCurve; } @@ -26,7 +28,9 @@ export function getNamedCurveByOid(oid: string) { export function getOidByNamedCurve(namedCurve: string) { const oid = edOIDs[namedCurve.toLowerCase()]; if (!oid) { - throw new core.OperationError(`Cannot convert WebCrypto named curve '${namedCurve}' to OID`); + throw new core.OperationError( + `Cannot convert WebCrypto named curve '${namedCurve}' to OID` + ); } return oid; } diff --git a/src/mechs/hkdf/key.ts b/src/mechs/hkdf/key.ts index 222b4e3..c8e2a2e 100644 --- a/src/mechs/hkdf/key.ts +++ b/src/mechs/hkdf/key.ts @@ -1,7 +1,6 @@ import { CryptoKey } from "../../keys"; export class HkdfCryptoKey extends CryptoKey { - public override data!: Buffer; public override algorithm!: KeyAlgorithm; diff --git a/src/mechs/hmac/key.ts b/src/mechs/hmac/key.ts index 15aa9f3..515ec56 100644 --- a/src/mechs/hmac/key.ts +++ b/src/mechs/hmac/key.ts @@ -3,7 +3,6 @@ import { JsonBase64UrlConverter } from "../../converters"; import { CryptoKey } from "../../keys"; export class HmacCryptoKey extends CryptoKey { - @JsonProp({ name: "k", converter: JsonBase64UrlConverter }) public override data!: Buffer; @@ -19,5 +18,4 @@ export class HmacCryptoKey extends CryptoKey { protected override set alg(value: string) { // nothing, cause set is needed for json-schema, but is not used by module } - } diff --git a/src/mechs/pbkdf/key.ts b/src/mechs/pbkdf/key.ts index 9849979..a387c95 100644 --- a/src/mechs/pbkdf/key.ts +++ b/src/mechs/pbkdf/key.ts @@ -1,4 +1,3 @@ import { CryptoKey } from "../../keys"; -export class PbkdfCryptoKey extends CryptoKey { -} +export class PbkdfCryptoKey extends CryptoKey {} diff --git a/src/mechs/rsa/rsa_es.ts b/src/mechs/rsa/rsa_es.ts index 23b1e98..6d1df16 100644 --- a/src/mechs/rsa/rsa_es.ts +++ b/src/mechs/rsa/rsa_es.ts @@ -7,21 +7,25 @@ import { RsaPublicKey } from "./public_key"; import { setCryptoKey, getCryptoKey } from "../storage"; export class RsaEsProvider extends core.ProviderCrypto { - public name = "RSAES-PKCS1-v1_5"; public usages = { publicKey: ["encrypt", "wrapKey"] as core.KeyUsages, privateKey: ["decrypt", "unwrapKey"] as core.KeyUsages, }; - public override async onGenerateKey(algorithm: RsaKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { + public override async onGenerateKey( + algorithm: RsaKeyGenParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { const keys = await RsaCrypto.generateKey( { ...algorithm, name: this.name, }, extractable, - keyUsages); + keyUsages + ); return { privateKey: setCryptoKey(keys.privateKey as RsaPrivateKey), @@ -32,7 +36,12 @@ export class RsaEsProvider extends core.ProviderCrypto { public override checkGenerateKeyParams(algorithm: RsaKeyGenParams) { // public exponent this.checkRequiredProperty(algorithm, "publicExponent"); - if (!(algorithm.publicExponent && algorithm.publicExponent instanceof Uint8Array)) { + if ( + !( + algorithm.publicExponent && + algorithm.publicExponent instanceof Uint8Array + ) + ) { throw new TypeError("publicExponent: Missing or not a Uint8Array"); } const publicExponent = Convert.ToBase64(algorithm.publicExponent); @@ -52,31 +61,59 @@ export class RsaEsProvider extends core.ProviderCrypto { } } - public override async onEncrypt(algorithm: Algorithm, key: RsaPublicKey, data: ArrayBuffer): Promise { + public override async onEncrypt( + algorithm: Algorithm, + key: RsaPublicKey, + data: ArrayBuffer + ): Promise { const options = this.toCryptoOptions(key); const enc = crypto.publicEncrypt(options, new Uint8Array(data)); return new Uint8Array(enc).buffer; } - public override async onDecrypt(algorithm: Algorithm, key: RsaPrivateKey, data: ArrayBuffer): Promise { + public override async onDecrypt( + algorithm: Algorithm, + key: RsaPrivateKey, + data: ArrayBuffer + ): Promise { const options = this.toCryptoOptions(key); const dec = crypto.privateDecrypt(options, new Uint8Array(data)); return new Uint8Array(dec).buffer; } - public override async onExportKey(format: KeyFormat, key: CryptoKey): Promise { + public override async onExportKey( + format: KeyFormat, + key: CryptoKey + ): Promise { return RsaCrypto.exportKey(format, getCryptoKey(key)); } - public override async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: RsaHashedImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { - const key = await RsaCrypto.importKey(format, keyData, { ...algorithm, name: this.name }, extractable, keyUsages); + public override async onImportKey( + format: KeyFormat, + keyData: JsonWebKey | ArrayBuffer, + algorithm: RsaHashedImportParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { + const key = await RsaCrypto.importKey( + format, + keyData, + { ...algorithm, name: this.name }, + extractable, + keyUsages + ); return setCryptoKey(key); } public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { super.checkCryptoKey(key, keyUsage); const internalKey = getCryptoKey(key); - if (!(internalKey instanceof RsaPrivateKey || internalKey instanceof RsaPublicKey)) { + if ( + !( + internalKey instanceof RsaPrivateKey || + internalKey instanceof RsaPublicKey + ) + ) { throw new TypeError("key: Is not RSA CryptoKey"); } } @@ -86,7 +123,9 @@ export class RsaEsProvider extends core.ProviderCrypto { private toCryptoOptions(key: RsaPrivateKey | RsaPublicKey) { const type = key.type.toUpperCase(); return { - key: `-----BEGIN ${type} KEY-----\n${getCryptoKey(key).data.toString("base64")}\n-----END ${type} KEY-----`, + key: `-----BEGIN ${type} KEY-----\n${getCryptoKey(key).data.toString( + "base64" + )}\n-----END ${type} KEY-----`, padding: crypto.constants.RSA_PKCS1_PADDING, }; } diff --git a/src/mechs/rsa/rsa_pss.ts b/src/mechs/rsa/rsa_pss.ts index 358514a..d183f9e 100644 --- a/src/mechs/rsa/rsa_pss.ts +++ b/src/mechs/rsa/rsa_pss.ts @@ -5,50 +5,98 @@ import { RsaPublicKey } from "./public_key"; import { setCryptoKey, getCryptoKey } from "../storage"; export class RsaPssProvider extends core.RsaPssProvider { - public override hashAlgorithms = [ - "SHA-1", "SHA-256", "SHA-384", "SHA-512", - "shake128", "shake256", - "SHA3-256", "SHA3-384", "SHA3-512"]; + "SHA-1", + "SHA-256", + "SHA-384", + "SHA-512", + "shake128", + "shake256", + "SHA3-256", + "SHA3-384", + "SHA3-512", + ]; - public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { + public async onGenerateKey( + algorithm: RsaHashedKeyGenParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { const keys = await RsaCrypto.generateKey( { ...algorithm, name: this.name, }, extractable, - keyUsages); + keyUsages + ); - return { - privateKey: setCryptoKey(keys.privateKey as RsaPrivateKey), - publicKey: setCryptoKey(keys.publicKey as RsaPublicKey), - }; + return { + privateKey: setCryptoKey(keys.privateKey as RsaPrivateKey), + publicKey: setCryptoKey(keys.publicKey as RsaPublicKey), + }; } - public async onSign(algorithm: RsaPssParams, key: RsaPrivateKey, data: ArrayBuffer): Promise { - return RsaCrypto.sign(algorithm, getCryptoKey(key) as RsaPrivateKey, new Uint8Array(data)); + public async onSign( + algorithm: RsaPssParams, + key: RsaPrivateKey, + data: ArrayBuffer + ): Promise { + return RsaCrypto.sign( + algorithm, + getCryptoKey(key) as RsaPrivateKey, + new Uint8Array(data) + ); } - public async onVerify(algorithm: RsaPssParams, key: RsaPublicKey, signature: ArrayBuffer, data: ArrayBuffer): Promise { - return RsaCrypto.verify(algorithm, getCryptoKey(key) as RsaPublicKey, new Uint8Array(signature), new Uint8Array(data)); + public async onVerify( + algorithm: RsaPssParams, + key: RsaPublicKey, + signature: ArrayBuffer, + data: ArrayBuffer + ): Promise { + return RsaCrypto.verify( + algorithm, + getCryptoKey(key) as RsaPublicKey, + new Uint8Array(signature), + new Uint8Array(data) + ); } - public async onExportKey(format: KeyFormat, key: CryptoKey): Promise { + public async onExportKey( + format: KeyFormat, + key: CryptoKey + ): Promise { return RsaCrypto.exportKey(format, getCryptoKey(key)); } - public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: RsaHashedImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { - const key = await RsaCrypto.importKey(format, keyData, {...algorithm, name: this.name}, extractable, keyUsages); + public async onImportKey( + format: KeyFormat, + keyData: JsonWebKey | ArrayBuffer, + algorithm: RsaHashedImportParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { + const key = await RsaCrypto.importKey( + format, + keyData, + { ...algorithm, name: this.name }, + extractable, + keyUsages + ); return setCryptoKey(key); } public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { super.checkCryptoKey(key, keyUsage); const internalKey = getCryptoKey(key); - if (!(internalKey instanceof RsaPrivateKey || internalKey instanceof RsaPublicKey)) { + if ( + !( + internalKey instanceof RsaPrivateKey || + internalKey instanceof RsaPublicKey + ) + ) { throw new TypeError("key: Is not RSA CryptoKey"); } } - } diff --git a/src/mechs/rsa/rsa_ssa.ts b/src/mechs/rsa/rsa_ssa.ts index e0ff26a..18fcce9 100644 --- a/src/mechs/rsa/rsa_ssa.ts +++ b/src/mechs/rsa/rsa_ssa.ts @@ -5,50 +5,98 @@ import { RsaPublicKey } from "./public_key"; import { setCryptoKey, getCryptoKey } from "../storage"; export class RsaSsaProvider extends core.RsaSsaProvider { - public override hashAlgorithms = [ - "SHA-1", "SHA-256", "SHA-384", "SHA-512", - "shake128", "shake256", - "SHA3-256", "SHA3-384", "SHA3-512"]; + "SHA-1", + "SHA-256", + "SHA-384", + "SHA-512", + "shake128", + "shake256", + "SHA3-256", + "SHA3-384", + "SHA3-512", + ]; - public async onGenerateKey(algorithm: RsaHashedKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { + public async onGenerateKey( + algorithm: RsaHashedKeyGenParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { const keys = await RsaCrypto.generateKey( { ...algorithm, name: this.name, }, extractable, - keyUsages); + keyUsages + ); - return { - privateKey: setCryptoKey(keys.privateKey as RsaPrivateKey), - publicKey: setCryptoKey(keys.publicKey as RsaPublicKey), - }; + return { + privateKey: setCryptoKey(keys.privateKey as RsaPrivateKey), + publicKey: setCryptoKey(keys.publicKey as RsaPublicKey), + }; } - public async onSign(algorithm: Algorithm, key: RsaPrivateKey, data: ArrayBuffer): Promise { - return RsaCrypto.sign(algorithm, getCryptoKey(key) as RsaPrivateKey, new Uint8Array(data)); + public async onSign( + algorithm: Algorithm, + key: RsaPrivateKey, + data: ArrayBuffer + ): Promise { + return RsaCrypto.sign( + algorithm, + getCryptoKey(key) as RsaPrivateKey, + new Uint8Array(data) + ); } - public async onVerify(algorithm: Algorithm, key: RsaPublicKey, signature: ArrayBuffer, data: ArrayBuffer): Promise { - return RsaCrypto.verify(algorithm, getCryptoKey(key) as RsaPublicKey, new Uint8Array(signature), new Uint8Array(data)); + public async onVerify( + algorithm: Algorithm, + key: RsaPublicKey, + signature: ArrayBuffer, + data: ArrayBuffer + ): Promise { + return RsaCrypto.verify( + algorithm, + getCryptoKey(key) as RsaPublicKey, + new Uint8Array(signature), + new Uint8Array(data) + ); } - public async onExportKey(format: KeyFormat, key: CryptoKey): Promise { + public async onExportKey( + format: KeyFormat, + key: CryptoKey + ): Promise { return RsaCrypto.exportKey(format, getCryptoKey(key)); } - public async onImportKey(format: KeyFormat, keyData: JsonWebKey | ArrayBuffer, algorithm: RsaHashedImportParams, extractable: boolean, keyUsages: KeyUsage[]): Promise { - const key = await RsaCrypto.importKey(format, keyData, {...algorithm, name: this.name}, extractable, keyUsages); + public async onImportKey( + format: KeyFormat, + keyData: JsonWebKey | ArrayBuffer, + algorithm: RsaHashedImportParams, + extractable: boolean, + keyUsages: KeyUsage[] + ): Promise { + const key = await RsaCrypto.importKey( + format, + keyData, + { ...algorithm, name: this.name }, + extractable, + keyUsages + ); return setCryptoKey(key); } public override checkCryptoKey(key: CryptoKey, keyUsage?: KeyUsage) { super.checkCryptoKey(key, keyUsage); const internalKey = getCryptoKey(key); - if (!(internalKey instanceof RsaPrivateKey || internalKey instanceof RsaPublicKey)) { + if ( + !( + internalKey instanceof RsaPrivateKey || + internalKey instanceof RsaPublicKey + ) + ) { throw new TypeError("key: Is not RSA CryptoKey"); } } - } diff --git a/src/mechs/sha/sha3_256.ts b/src/mechs/sha/sha3_256.ts index 907ec60..225f687 100644 --- a/src/mechs/sha/sha3_256.ts +++ b/src/mechs/sha/sha3_256.ts @@ -5,8 +5,10 @@ export class Sha3256Provider extends core.ProviderCrypto { public name = "SHA3-256"; public usages = []; - public override async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise { + public override async onDigest( + algorithm: Algorithm, + data: ArrayBuffer + ): Promise { return ShaCrypto.digest(algorithm, data); } - } diff --git a/src/mechs/sha/sha3_384.ts b/src/mechs/sha/sha3_384.ts index 169a605..cd726cc 100644 --- a/src/mechs/sha/sha3_384.ts +++ b/src/mechs/sha/sha3_384.ts @@ -5,8 +5,10 @@ export class Sha3384Provider extends core.ProviderCrypto { public name = "SHA3-384"; public usages = []; - public override async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise { + public override async onDigest( + algorithm: Algorithm, + data: ArrayBuffer + ): Promise { return ShaCrypto.digest(algorithm, data); } - } diff --git a/src/mechs/sha/sha3_512.ts b/src/mechs/sha/sha3_512.ts index 8b10713..3d8814e 100644 --- a/src/mechs/sha/sha3_512.ts +++ b/src/mechs/sha/sha3_512.ts @@ -5,8 +5,10 @@ export class Sha3512Provider extends core.ProviderCrypto { public name = "SHA3-512"; public usages = []; - public override async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise { + public override async onDigest( + algorithm: Algorithm, + data: ArrayBuffer + ): Promise { return ShaCrypto.digest(algorithm, data); } - } diff --git a/src/mechs/sha/sha_1.ts b/src/mechs/sha/sha_1.ts index 1c56b58..84caf22 100644 --- a/src/mechs/sha/sha_1.ts +++ b/src/mechs/sha/sha_1.ts @@ -5,8 +5,10 @@ export class Sha1Provider extends core.ProviderCrypto { public name = "SHA-1"; public usages = []; - public override async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise { + public override async onDigest( + algorithm: Algorithm, + data: ArrayBuffer + ): Promise { return ShaCrypto.digest(algorithm, data); } - } diff --git a/src/mechs/sha/sha_256.ts b/src/mechs/sha/sha_256.ts index 6067dbe..8a0f7e4 100644 --- a/src/mechs/sha/sha_256.ts +++ b/src/mechs/sha/sha_256.ts @@ -5,8 +5,10 @@ export class Sha256Provider extends core.ProviderCrypto { public name = "SHA-256"; public usages = []; - public override async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise { + public override async onDigest( + algorithm: Algorithm, + data: ArrayBuffer + ): Promise { return ShaCrypto.digest(algorithm, data); } - } diff --git a/src/mechs/sha/sha_384.ts b/src/mechs/sha/sha_384.ts index b82462d..23126f9 100644 --- a/src/mechs/sha/sha_384.ts +++ b/src/mechs/sha/sha_384.ts @@ -5,8 +5,10 @@ export class Sha384Provider extends core.ProviderCrypto { public name = "SHA-384"; public usages = []; - public override async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise { + public override async onDigest( + algorithm: Algorithm, + data: ArrayBuffer + ): Promise { return ShaCrypto.digest(algorithm, data); } - } diff --git a/src/mechs/sha/sha_512.ts b/src/mechs/sha/sha_512.ts index 06e09da..abe82a5 100644 --- a/src/mechs/sha/sha_512.ts +++ b/src/mechs/sha/sha_512.ts @@ -5,8 +5,10 @@ export class Sha512Provider extends core.ProviderCrypto { public name = "SHA-512"; public usages = []; - public override async onDigest(algorithm: Algorithm, data: ArrayBuffer): Promise { + public override async onDigest( + algorithm: Algorithm, + data: ArrayBuffer + ): Promise { return ShaCrypto.digest(algorithm, data); } - } diff --git a/src/mechs/shake/shake128.ts b/src/mechs/shake/shake128.ts index ac26d38..06e7651 100644 --- a/src/mechs/shake/shake128.ts +++ b/src/mechs/shake/shake128.ts @@ -2,9 +2,10 @@ import * as core from "webcrypto-core"; import { ShakeCrypto } from "./crypto"; export class Shake128Provider extends core.Shake128Provider { - - public override async onDigest(algorithm: Required, data: ArrayBuffer): Promise { + public override async onDigest( + algorithm: Required, + data: ArrayBuffer + ): Promise { return ShakeCrypto.digest(algorithm, data); } - } diff --git a/src/mechs/shake/shake256.ts b/src/mechs/shake/shake256.ts index 7ab43ce..ccc0506 100644 --- a/src/mechs/shake/shake256.ts +++ b/src/mechs/shake/shake256.ts @@ -2,9 +2,10 @@ import * as core from "webcrypto-core"; import { ShakeCrypto } from "./crypto"; export class Shake256Provider extends core.Shake256Provider { - - public override async onDigest(algorithm: Required, data: ArrayBuffer): Promise { + public override async onDigest( + algorithm: Required, + data: ArrayBuffer + ): Promise { return ShakeCrypto.digest(algorithm, data); } - } diff --git a/src/mechs/storage.ts b/src/mechs/storage.ts index 3d3e2e9..7ac7c31 100644 --- a/src/mechs/storage.ts +++ b/src/mechs/storage.ts @@ -12,7 +12,12 @@ export function getCryptoKey(key: core.CryptoKey) { } export function setCryptoKey(value: InternalCryptoKey) { - const key = core.CryptoKey.create(value.algorithm, value.type, value.extractable, value.usages); + const key = core.CryptoKey.create( + value.algorithm, + value.type, + value.extractable, + value.usages + ); Object.freeze(key); keyStorage.set(key, value); diff --git a/src/subtle.ts b/src/subtle.ts index 898a6e4..c32cb3b 100644 --- a/src/subtle.ts +++ b/src/subtle.ts @@ -2,18 +2,34 @@ import * as crypto from "crypto"; import * as process from "process"; import * as core from "webcrypto-core"; import { - AesCbcProvider, AesCmacProvider, AesCtrProvider, AesEcbProvider, AesGcmProvider, AesKwProvider, + AesCbcProvider, + AesCmacProvider, + AesCtrProvider, + AesEcbProvider, + AesGcmProvider, + AesKwProvider, DesCbcProvider, - DesEde3CbcProvider, EcdhProvider, - EcdsaProvider, HkdfProvider, + DesEde3CbcProvider, + EcdhProvider, + EcdsaProvider, + HkdfProvider, EdDsaProvider, EcdhEsProvider, HmacProvider, Pbkdf2Provider, - RsaEsProvider, RsaOaepProvider, RsaPssProvider, RsaSsaProvider, - Sha1Provider, Sha256Provider, Sha384Provider, Sha512Provider, - Shake128Provider, Shake256Provider, - Sha3256Provider, Sha3384Provider, Sha3512Provider, + RsaEsProvider, + RsaOaepProvider, + RsaPssProvider, + RsaSsaProvider, + Sha1Provider, + Sha256Provider, + Sha384Provider, + Sha512Provider, + Shake128Provider, + Shake256Provider, + Sha3256Provider, + Sha3384Provider, + Sha3512Provider, } from "./mechs"; export class SubtleCrypto extends core.SubtleCrypto {