11import { KeyWrapper , LogOptions , Platform , platform } from "./platform" ;
22import type { KeeperHost , TransmissionKey , TransmissionKeyHpke } from './configuration' ;
3- import { AllowedNumbers } from "./transmissionKeys" ;
3+ import { AllowedNumbers , isAllowedNumber } from "./transmissionKeys" ;
44import { Ciphersuite , HPKE_ECDH_KYBER , OPTIONAL_DATA_LENGTH } from "./qrc" ;
55
66export const log = ( message : string , options : LogOptions = 'default' ) => {
@@ -47,25 +47,32 @@ export function getKeeperAutomatorAdminUrl(host: KeeperHost, forPath: string, au
4747
4848export 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?
6157export 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