@@ -7,6 +7,16 @@ import type { ICryptoInterface } from '../crypto.js'
77import { logger } from '../logger.js'
88import { Nonce } from '../nonce.js'
99
10+ export interface DecryptedResult {
11+ plaintext : bytes
12+ valid : boolean
13+ }
14+
15+ export interface SplitState {
16+ cs1 : CipherState
17+ cs2 : CipherState
18+ }
19+
1020export abstract class AbstractHandshake {
1121 public crypto : ICryptoInterface
1222
@@ -21,7 +31,7 @@ export abstract class AbstractHandshake {
2131 return e
2232 }
2333
24- public decryptWithAd ( cs : CipherState , ad : Uint8Array , ciphertext : Uint8Array , dst ?: Uint8Array ) : { plaintext : bytes , valid : boolean } {
34+ public decryptWithAd ( cs : CipherState , ad : Uint8Array , ciphertext : Uint8Array , dst ?: Uint8Array ) : DecryptedResult {
2535 const { plaintext, valid } = this . decrypt ( cs . k , cs . n , ad , ciphertext , dst )
2636 if ( valid ) cs . n . increment ( )
2737
@@ -60,7 +70,7 @@ export abstract class AbstractHandshake {
6070 return ciphertext
6171 }
6272
63- protected decrypt ( k : bytes32 , n : Nonce , ad : bytes , ciphertext : bytes , dst ?: Uint8Array ) : { plaintext : bytes , valid : boolean } {
73+ protected decrypt ( k : bytes32 , n : Nonce , ad : bytes , ciphertext : bytes , dst ?: Uint8Array ) : DecryptedResult {
6474 n . assertValue ( )
6575
6676 const encryptedMessage = this . crypto . chaCha20Poly1305Decrypt ( ciphertext , n . getBytes ( ) , ad , k , dst )
@@ -78,7 +88,7 @@ export abstract class AbstractHandshake {
7888 }
7989 }
8090
81- protected decryptAndHash ( ss : SymmetricState , ciphertext : bytes ) : { plaintext : bytes , valid : boolean } {
91+ protected decryptAndHash ( ss : SymmetricState , ciphertext : bytes ) : DecryptedResult {
8292 let plaintext : bytes ; let valid = true
8393 if ( this . hasKey ( ss . cs ) ) {
8494 ( { plaintext, valid } = this . decryptWithAd ( ss . cs , ss . h , ciphertext ) )
@@ -148,7 +158,7 @@ export abstract class AbstractHandshake {
148158 }
149159 }
150160
151- protected split ( ss : SymmetricState ) : { cs1 : CipherState , cs2 : CipherState } {
161+ protected split ( ss : SymmetricState ) : SplitState {
152162 const [ tempk1 , tempk2 ] = this . crypto . getHKDF ( ss . ck , new Uint8Array ( 0 ) )
153163 const cs1 = this . initializeKey ( tempk1 )
154164 const cs2 = this . initializeKey ( tempk2 )
@@ -164,7 +174,7 @@ export abstract class AbstractHandshake {
164174 return { ne, ns, ciphertext }
165175 }
166176
167- protected readMessageRegular ( cs : CipherState , message : MessageBuffer ) : { plaintext : bytes , valid : boolean } {
177+ protected readMessageRegular ( cs : CipherState , message : MessageBuffer ) : DecryptedResult {
168178 return this . decryptWithAd ( cs , new Uint8Array ( 0 ) , message . ciphertext )
169179 }
170180}
0 commit comments