Skip to content

Commit

Permalink
Merge pull request #237 from dajiaji/divide-consts
Browse files Browse the repository at this point in the history
Separate consts to minimize the size of submodules.
  • Loading branch information
dajiaji authored Aug 7, 2023
2 parents 01edfc5 + 85c4cbf commit c7151f8
Show file tree
Hide file tree
Showing 17 changed files with 391 additions and 323 deletions.
4 changes: 2 additions & 2 deletions src/aeads/aesGcm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { AeadInterface } from "../interfaces/aeadInterface.ts";

import { Algorithm } from "../algorithm.ts";
import { AeadId } from "../identifiers.ts";
import * as consts from "../consts.ts";
import { AEAD_USAGES } from "../interfaces/aeadEncryptionContext.ts";

export class AesGcmContext implements AeadEncryptionContext {
private _rawKey: ArrayBuffer;
Expand Down Expand Up @@ -65,7 +65,7 @@ export class AesGcmContext implements AeadEncryptionContext {
key,
{ name: "AES-GCM" },
true,
consts.AEAD_USAGES,
AEAD_USAGES,
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/algorithm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as consts from "./consts.ts";
import { EMPTY } from "./consts.ts";

export class AlgorithmBase {
protected _api: SubtleCrypto | undefined = undefined;
Expand All @@ -23,7 +23,7 @@ export class Algorithm extends AlgorithmBase {
}

export class KdfAlgorithm extends AlgorithmBase {
protected _suiteId: Uint8Array = consts.EMPTY;
protected _suiteId: Uint8Array = EMPTY;

constructor() {
super();
Expand Down
73 changes: 66 additions & 7 deletions src/cipherSuiteNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,65 @@ import { i2Osp } from "./utils/misc.ts";
import * as consts from "./consts.ts";
import * as errors from "./errors.ts";

// b"base_nonce"
const LABEL_BASE_NONCE = new Uint8Array([
98,
97,
115,
101,
95,
110,
111,
110,
99,
101,
]);
// b"exp"
const LABEL_EXP = new Uint8Array([101, 120, 112]);
// b"info_hash"
const LABEL_INFO_HASH = new Uint8Array([
105,
110,
102,
111,
95,
104,
97,
115,
104,
]);
// b"key"
const LABEL_KEY = new Uint8Array([107, 101, 121]);
// b"psk_id_hash"
const LABEL_PSK_ID_HASH = new Uint8Array([
112,
115,
107,
95,
105,
100,
95,
104,
97,
115,
104,
]);
// b"secret"
const LABEL_SECRET = new Uint8Array([115, 101, 99, 114, 101, 116]);
// b"HPKE"
const SUITE_ID_HEADER_HPKE = new Uint8Array([
72,
80,
75,
69,
0,
0,
0,
0,
0,
0,
]);

/**
* The Hybrid Public Key Encryption (HPKE) ciphersuite,
* which is implemented using only
Expand Down Expand Up @@ -168,7 +227,7 @@ export class CipherSuiteNative {
}
}

this._suiteId = new Uint8Array(consts.SUITE_ID_HEADER_HPKE);
this._suiteId = new Uint8Array(SUITE_ID_HEADER_HPKE);
this._suiteId.set(i2Osp(this._kem.id, 2), 4);
this._suiteId.set(i2Osp(this._kdf.id, 2), 6);
this._suiteId.set(i2Osp(this._aead.id, 2), 8);
Expand Down Expand Up @@ -399,7 +458,7 @@ export class CipherSuiteNative {
: new Uint8Array(params.psk.id);
const pskIdHash = await this._kdf.labeledExtract(
consts.EMPTY,
consts.LABEL_PSK_ID_HASH,
LABEL_PSK_ID_HASH,
pskId,
);

Expand All @@ -408,7 +467,7 @@ export class CipherSuiteNative {
: new Uint8Array(params.info);
const infoHash = await this._kdf.labeledExtract(
consts.EMPTY,
consts.LABEL_INFO_HASH,
LABEL_INFO_HASH,
info,
);

Expand All @@ -422,10 +481,10 @@ export class CipherSuiteNative {
const psk = params.psk === undefined
? consts.EMPTY
: new Uint8Array(params.psk.key);
const ikm = this._kdf.buildLabeledIkm(consts.LABEL_SECRET, psk);
const ikm = this._kdf.buildLabeledIkm(LABEL_SECRET, psk);

const exporterSecretInfo = this._kdf.buildLabeledInfo(
consts.LABEL_EXP,
LABEL_EXP,
keyScheduleContext,
this._kdf.hashSize,
);
Expand All @@ -441,7 +500,7 @@ export class CipherSuiteNative {
}

const keyInfo = this._kdf.buildLabeledInfo(
consts.LABEL_KEY,
LABEL_KEY,
keyScheduleContext,
this._aead.keySize,
);
Expand All @@ -453,7 +512,7 @@ export class CipherSuiteNative {
);

const baseNonceInfo = this._kdf.buildLabeledInfo(
consts.LABEL_BASE_NONCE,
LABEL_BASE_NONCE,
keyScheduleContext,
this._aead.nonceSize,
);
Expand Down
Loading

0 comments on commit c7151f8

Please sign in to comment.