@@ -445,6 +445,9 @@ export class VetKey {
445445 }
446446}
447447
448+ // The size of the nonce used for encryption by DerivedKeyMaterial
449+ const DerivedKeyMaterialNonceLength = 12 ;
450+
448451export class DerivedKeyMaterial {
449452 readonly #hkdf: CryptoKey ;
450453
@@ -523,7 +526,9 @@ export class DerivedKeyMaterial {
523526 const gcmKey = await this . deriveAesGcmCryptoKey ( domainSep ) ;
524527
525528 // The nonce must never be reused with a given key
526- const nonce = globalThis . crypto . getRandomValues ( new Uint8Array ( 12 ) ) ;
529+ const nonce = globalThis . crypto . getRandomValues (
530+ new Uint8Array ( DerivedKeyMaterialNonceLength ) ,
531+ ) ;
527532
528533 const ciphertext = new Uint8Array (
529534 await globalThis . crypto . subtle . encrypt (
@@ -546,17 +551,16 @@ export class DerivedKeyMaterial {
546551 message : Uint8Array ,
547552 domainSep : Uint8Array | string ,
548553 ) : Promise < Uint8Array > {
549- const NonceLength = 12 ;
550554 const TagLength = 16 ;
551555
552- if ( message . length < NonceLength + TagLength ) {
556+ if ( message . length < DerivedKeyMaterialNonceLength + TagLength ) {
553557 throw new Error (
554558 "Invalid ciphertext, too short to possibly be valid" ,
555559 ) ;
556560 }
557561
558- const nonce = message . slice ( 0 , NonceLength ) ; // first 12 bytes are the nonce
559- const ciphertext = message . slice ( NonceLength ) ; // remainder GCM ciphertext
562+ const nonce = message . slice ( 0 , DerivedKeyMaterialNonceLength ) ; // first 12 bytes are the nonce
563+ const ciphertext = message . slice ( DerivedKeyMaterialNonceLength ) ; // remainder GCM ciphertext
560564
561565 const gcmKey = await this . deriveAesGcmCryptoKey ( domainSep ) ;
562566
0 commit comments