Skip to content

Commit d91c9ce

Browse files
committed
more refactors
1 parent 3834fbb commit d91c9ce

File tree

2 files changed

+23
-33
lines changed

2 files changed

+23
-33
lines changed

keeperapi/src/endpoint.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -348,29 +348,12 @@ export async function prepareApiRequest(
348348
requestPayload.encryptedSessionToken = normal64Bytes(sessionToken);
349349
}
350350
requestPayload.apiVersion = apiVersion || 0
351-
let requestPayloadBytes = ApiRequestPayload.encode(requestPayload).finish()
352-
let encryptedRequestPayload = await platform.aesGcmEncrypt(requestPayloadBytes, transmissionKey.key)
351+
const requestPayloadBytes = ApiRequestPayload.encode(requestPayload).finish()
352+
const encryptedRequestPayload = await platform.aesGcmEncrypt(requestPayloadBytes, transmissionKey.key)
353353

354354
// Use QRC to wrap the transmission key
355-
const {ecKeyId, mlKemKeyId} = transmissionKey
356-
if (!isAllowedNumber(ecKeyId)) { // TODO: validate mlKemKeyId as well
357-
throw new Error(`Invalid EC key ID: ${ecKeyId}`)
358-
}
359-
const serverEcPublicKey = platform.keys[ecKeyId]
360-
const serverMlKemPublicKey = platform.mlKemKeys[mlKemKeyId]
361-
if (!serverEcPublicKey) {
362-
throw new Error(`EC public key not found for ID: ${ecKeyId}`)
363-
}
364-
if (!serverMlKemPublicKey) {
365-
throw new Error(`ML-KEM public key not found for ID: ${mlKemKeyId}`)
366-
}
367-
368355
const hpkeTransmissionKey = await generateHpkeTransmissionKey(
369-
transmissionKey.key,
370-
mlKemKeyId,
371-
serverEcPublicKey,
372-
serverMlKemPublicKey,
373-
ecKeyId,
356+
transmissionKey,
374357
true // use optional data
375358
)
376359

keeperapi/src/utils.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {KeyWrapper, LogOptions, Platform, platform} from "./platform";
22
import type {KeeperHost, TransmissionKey, TransmissionKeyHpke} from './configuration';
3-
import { AllowedNumbers } from "./transmissionKeys";
3+
import { AllowedNumbers, isAllowedNumber } from "./transmissionKeys";
44
import { Ciphersuite, HPKE_ECDH_KYBER, OPTIONAL_DATA_LENGTH } from "./qrc";
55

66
export const log = (message: string, options: LogOptions = 'default') => {
@@ -47,25 +47,32 @@ export function getKeeperAutomatorAdminUrl(host: KeeperHost, forPath: string, au
4747

4848
export async function generateTransmissionKey(keyNumber: AllowedNumbers): Promise<TransmissionKey> {
4949
const transmissionKey = platform.getRandomBytes(32)
50-
// Hmm. Do we switch this to HPKE only?
5150
return {
52-
ecKeyId: keyNumber, // TODO: rename to ecKeyId
51+
ecKeyId: keyNumber,
5352
key: transmissionKey,
5453
mlKemKeyId: 100 // HARD CODED FOR NOW, should be passed in
55-
// TODO: add the mlKemKeyId
56-
// encryptedKey: await platform.publicEncryptEC(transmissionKey, platform.keys[keyNumber]) // deprecate this
5754
}
5855
}
5956

60-
// call this instead (is it exported? also, should we just return the protobuf instead?
6157
export async function generateHpkeTransmissionKey(
62-
transmissionKey: Uint8Array,
63-
mlKemKeyId: number,
64-
serverEcPublicKey: Uint8Array,
65-
serverMlKemPublicKey: Uint8Array,
66-
ecKeyId: number,
58+
transmissionKey: TransmissionKey,
6759
useOptionalData: boolean = true
6860
): Promise<TransmissionKeyHpke> {
61+
const {ecKeyId, mlKemKeyId} = transmissionKey
62+
if (!isAllowedNumber(ecKeyId)) { // TODO: validate mlKemKeyId as well
63+
throw new Error(`Invalid EC key ID: ${ecKeyId}`)
64+
}
65+
66+
const serverEcPublicKey = platform.keys[ecKeyId]
67+
const serverMlKemPublicKey = platform.mlKemKeys[mlKemKeyId]
68+
if (!serverEcPublicKey) {
69+
throw new Error(`EC public key not found for ID: ${ecKeyId}`)
70+
}
71+
if (!serverMlKemPublicKey) {
72+
throw new Error(`ML-KEM public key not found for ID: ${mlKemKeyId}`)
73+
}
74+
75+
6976
// Generate optional data if requested (recommended for unique request binding)
7077
const optionalData = useOptionalData ? platform.getRandomBytes(OPTIONAL_DATA_LENGTH) : undefined;
7178

@@ -74,15 +81,15 @@ export async function generateHpkeTransmissionKey(
7481

7582
// Encrypt transmission key using QRC
7683
const qrcResult = await hpke.encrypt(
77-
transmissionKey,
84+
transmissionKey.key,
7885
serverEcPublicKey,
7986
serverMlKemPublicKey,
8087
optionalData
8188
);
8289

8390
return {
8491
publicKeyId: mlKemKeyId, // ML-KEM key ID
85-
key: transmissionKey,
92+
key: transmissionKey.key,
8693
qrcMessageKey: {
8794
clientEcPublicKey: qrcResult.clientEcPublicKey,
8895
mlKemEncapsulatedKey: qrcResult.mlKemEncapsulatedKey,

0 commit comments

Comments
 (0)