Skip to content

Commit

Permalink
Merge pull request #245 from dajiaji/remove-aead-init
Browse files Browse the repository at this point in the history
Remove init() from AeadInterface.
  • Loading branch information
dajiaji authored Aug 11, 2023
2 parents 9079b4e + 4d394bc commit 20c9dc8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 36 deletions.
51 changes: 25 additions & 26 deletions src/aeads/aesGcm.ts
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -20,18 +20,15 @@ export class AesGcmContext implements AeadEncryptionContext {
data: ArrayBuffer,
aad: ArrayBuffer,
): Promise<ArrayBuffer> {
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;
Expand All @@ -42,25 +39,33 @@ export class AesGcmContext implements AeadEncryptionContext {
data: ArrayBuffer,
aad: ArrayBuffer,
): Promise<ArrayBuffer> {
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<CryptoKey> {
return await this._api.importKey(
return await (this._api as SubtleCrypto).importKey(
"raw",
key,
{ name: "AES-GCM" },
Expand All @@ -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);
}
}
1 change: 0 additions & 1 deletion src/cipherSuiteNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 0 additions & 7 deletions src/interfaces/aeadInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 0 additions & 2 deletions test/encryptionContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ describe("constructor", () => {
};

// assert
params.aead.init(api);
assertEquals(
typeof new EncryptionContextImpl(api, kdf, params),
"object",
Expand Down Expand Up @@ -91,7 +90,6 @@ describe("constructor", () => {
};

// assert
params.aead.init(api);
assertThrows(
() => {
new EncryptionContextImpl(api, kdf, params);
Expand Down

0 comments on commit 20c9dc8

Please sign in to comment.