From 4d394bcb1e71a7a5f1c1d65a97aa12894f0d3466 Mon Sep 17 00:00:00 2001 From: Ajitomi Daisuke Date: Fri, 11 Aug 2023 14:17:10 +0900 Subject: [PATCH] Remove init() from AeadInterface. --- src/aeads/aesGcm.ts | 51 ++++++++++++++++----------------- src/cipherSuiteNative.ts | 1 - src/interfaces/aeadInterface.ts | 7 ----- test/encryptionContext.test.ts | 2 -- 4 files changed, 25 insertions(+), 36 deletions(-) diff --git a/src/aeads/aesGcm.ts b/src/aeads/aesGcm.ts index 89dc3f3c9..8929dda01 100644 --- a/src/aeads/aesGcm.ts +++ b/src/aeads/aesGcm.ts @@ -1,17 +1,17 @@ import type { AeadEncryptionContext } from "../interfaces/aeadEncryptionContext.ts"; import type { AeadInterface } from "../interfaces/aeadInterface.ts"; -import { Algorithm } from "../algorithm.ts"; +import { AlgorithmBase } from "../algorithm.ts"; import { AeadId } from "../identifiers.ts"; +import { loadSubtleCrypto } from "../webCrypto.ts"; import { AEAD_USAGES } from "../interfaces/aeadEncryptionContext.ts"; export class AesGcmContext implements AeadEncryptionContext { private _rawKey: ArrayBuffer; private _key: CryptoKey | undefined = undefined; - private _api: SubtleCrypto; + private _api: SubtleCrypto | undefined = undefined; - public constructor(api: SubtleCrypto, key: ArrayBuffer) { - this._api = api; + public constructor(key: ArrayBuffer) { this._rawKey = key; } @@ -20,18 +20,15 @@ export class AesGcmContext implements AeadEncryptionContext { data: ArrayBuffer, aad: ArrayBuffer, ): Promise { - if (this._key === undefined) { - this._key = await this._importKey(this._rawKey); - (new Uint8Array(this._rawKey)).fill(0); - } + await this._setup(); const alg = { name: "AES-GCM", iv: iv, additionalData: aad, }; - const ct: ArrayBuffer = await this._api.encrypt( + const ct: ArrayBuffer = await (this._api as SubtleCrypto).encrypt( alg, - this._key, + this._key as CryptoKey, data, ); return ct; @@ -42,25 +39,33 @@ export class AesGcmContext implements AeadEncryptionContext { data: ArrayBuffer, aad: ArrayBuffer, ): Promise { - if (this._key === undefined) { - this._key = await this._importKey(this._rawKey); - (new Uint8Array(this._rawKey)).fill(0); - } + await this._setup(); const alg = { name: "AES-GCM", iv: iv, additionalData: aad, }; - const pt: ArrayBuffer = await this._api.decrypt( + const pt: ArrayBuffer = await (this._api as SubtleCrypto).decrypt( alg, - this._key, + this._key as CryptoKey, data, ); return pt; } + private async _setup() { + if (this._key !== undefined) { + return; + } + this._api = await loadSubtleCrypto(); + const key = await this._importKey(this._rawKey); + (new Uint8Array(this._rawKey)).fill(0); + this._key = key; + return; + } + private async _importKey(key: ArrayBuffer): Promise { - return await this._api.importKey( + return await (this._api as SubtleCrypto).importKey( "raw", key, { name: "AES-GCM" }, @@ -70,26 +75,20 @@ export class AesGcmContext implements AeadEncryptionContext { } } -export class Aes128Gcm extends Algorithm implements AeadInterface { +export class Aes128Gcm extends AlgorithmBase implements AeadInterface { public readonly id: AeadId = AeadId.Aes128Gcm; public readonly keySize: number = 16; public readonly nonceSize: number = 12; public readonly tagSize: number = 16; public createEncryptionContext(key: ArrayBuffer): AeadEncryptionContext { - this.checkInit(); - return new AesGcmContext(this._api as SubtleCrypto, key); + return new AesGcmContext(key); } } -export class Aes256Gcm extends Algorithm implements AeadInterface { +export class Aes256Gcm extends Aes128Gcm { public readonly id: AeadId = AeadId.Aes256Gcm; public readonly keySize: number = 32; public readonly nonceSize: number = 12; public readonly tagSize: number = 16; - - public createEncryptionContext(key: ArrayBuffer): AeadEncryptionContext { - this.checkInit(); - return new AesGcmContext(this._api as SubtleCrypto, key); - } } diff --git a/src/cipherSuiteNative.ts b/src/cipherSuiteNative.ts index d847d8d95..82b116765 100644 --- a/src/cipherSuiteNative.ts +++ b/src/cipherSuiteNative.ts @@ -419,7 +419,6 @@ export class CipherSuiteNative { } const api = await loadSubtleCrypto(); this._kdf.init(api as SubtleCrypto, this._suiteId); - this._aead.init(api as SubtleCrypto); this._api = api; return; } diff --git a/src/interfaces/aeadInterface.ts b/src/interfaces/aeadInterface.ts index ae5702021..9822f21ba 100644 --- a/src/interfaces/aeadInterface.ts +++ b/src/interfaces/aeadInterface.ts @@ -15,13 +15,6 @@ export interface AeadInterface { /** The length in bytes of an AEAD authentication tag (Nt). */ readonly tagSize: number; - /** - * Initializes the instance by setting a SubtleCrypto API. - * - * @param api A SubtleCrypto API. - */ - init(api: SubtleCrypto): void; - /** * Creates an AEAD encryption context which has seal/open operation. * diff --git a/test/encryptionContext.test.ts b/test/encryptionContext.test.ts index 1c2c5e393..ec4517766 100644 --- a/test/encryptionContext.test.ts +++ b/test/encryptionContext.test.ts @@ -57,7 +57,6 @@ describe("constructor", () => { }; // assert - params.aead.init(api); assertEquals( typeof new EncryptionContextImpl(api, kdf, params), "object", @@ -91,7 +90,6 @@ describe("constructor", () => { }; // assert - params.aead.init(api); assertThrows( () => { new EncryptionContextImpl(api, kdf, params);