@@ -48,7 +48,9 @@ export class ChaCha20Poly1305 extends BaseDriver implements EncryptionDriverCont
4848 /**
4949 * Creating cipher
5050 */
51- const cipher = createCipheriv ( 'chacha20-poly1305' , this . cryptoKey , iv , { authTagLength : 16 } )
51+ const cipher = createCipheriv ( 'chacha20-poly1305' , this . getFirstKey ( ) . key , iv , {
52+ authTagLength : 16 ,
53+ } )
5254
5355 if ( purpose ) {
5456 cipher . setAAD ( Buffer . from ( purpose ) , { plaintextLength : Buffer . byteLength ( purpose ) } )
@@ -76,7 +78,7 @@ export class ChaCha20Poly1305 extends BaseDriver implements EncryptionDriverCont
7678 /**
7779 * Returns the id + result + nounce + hmac
7880 */
79- const hmac = new Hmac ( this . cryptoKey ) . generate ( result )
81+ const hmac = new Hmac ( this . getFirstKey ( ) . key ) . generate ( result )
8082 return this . computeReturns ( [ this . #config. id , result , nounce , hmac ] )
8183 }
8284
@@ -90,10 +92,10 @@ export class ChaCha20Poly1305 extends BaseDriver implements EncryptionDriverCont
9092
9193 /**
9294 * Make sure the encrypted value is in correct format. ie
93- * [id].[encrypted value].[iv].[nounce].[hash ]
95+ * [id].[encrypted value].[iv].[nounce].[hmac ]
9496 */
95- const [ id , encryptedEncoded , ivEncoded , nounceEncoded , hash ] = value . split ( this . separator )
96- if ( ! id || ! encryptedEncoded || ! ivEncoded || ! nounceEncoded || ! hash ) {
97+ const [ id , encryptedEncoded , ivEncoded , nounceEncoded , hmac ] = value . split ( this . separator )
98+ if ( ! id || ! encryptedEncoded || ! ivEncoded || ! nounceEncoded || ! hmac ) {
9799 return null
98100 }
99101
@@ -132,39 +134,42 @@ export class ChaCha20Poly1305 extends BaseDriver implements EncryptionDriverCont
132134 * Make sure the hash is correct, it means the first 2 parts of the
133135 * string are not tampered.
134136 */
135- const isValidHmac = new Hmac ( this . cryptoKey ) . compare (
136- `${ encryptedEncoded } ${ this . separator } ${ ivEncoded } ` ,
137- hash
138- )
139- if ( ! isValidHmac ) {
140- return null
141- }
142-
143- /**
144- * The Decipher can raise exceptions with malformed input, so we wrap it
145- * to avoid leaking sensitive information
146- */
147- try {
148- const decipher = createDecipheriv ( 'chacha20-poly1305' , this . cryptoKey , iv , {
149- authTagLength : 16 ,
150- } )
151-
152- /**
153- * Set the purpose to decipher
154- */
155- if ( purpose ) {
156- decipher . setAAD ( Buffer . from ( purpose ) , { plaintextLength : Buffer . byteLength ( purpose ) } )
137+ for ( const { key } of this . cryptoKeys ) {
138+ const isValidHmac = new Hmac ( key ) . compare (
139+ `${ encryptedEncoded } ${ this . separator } ${ ivEncoded } ` ,
140+ hmac
141+ )
142+
143+ if ( ! isValidHmac ) {
144+ continue
157145 }
158146
159147 /**
160- * Set the nounce
148+ * The Decipher can raise exceptions with malformed input, so we wrap it
149+ * to avoid leaking sensitive information
161150 */
162- decipher . setAuthTag ( nounce )
163-
164- const decrypted = decipher . update ( encrypted ) + decipher . final ( 'utf8' )
165- return new MessageBuilder ( ) . verify ( decrypted )
166- } catch {
167- return null
151+ try {
152+ const decipher = createDecipheriv ( 'chacha20-poly1305' , key , iv , {
153+ authTagLength : 16 ,
154+ } )
155+
156+ /**
157+ * Set the purpose to decipher
158+ */
159+ if ( purpose ) {
160+ decipher . setAAD ( Buffer . from ( purpose ) , { plaintextLength : Buffer . byteLength ( purpose ) } )
161+ }
162+
163+ /**
164+ * Set the nounce
165+ */
166+ decipher . setAuthTag ( nounce )
167+
168+ const decrypted = decipher . update ( encrypted ) + decipher . final ( 'utf8' )
169+ return new MessageBuilder ( ) . verify ( decrypted )
170+ } catch { }
168171 }
172+
173+ return null
169174 }
170175}
0 commit comments